blob: 18f5392aa550aa8a90fa5eeaeaedfb47eef89e83 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 clnt->cl_procinfo = version->procs;
398 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300399 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500402 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400403 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500404 err = -ENOMEM;
405 if (clnt->cl_metrics == NULL)
406 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500407 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400408 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400409 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400411 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500412 if (args->timeout != NULL) {
413 memcpy(&clnt->cl_timeout_default, args->timeout,
414 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400415 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500416 }
417
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400418 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500419 xprt_iter_init(&clnt->cl_xpi, xps);
420 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500423 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Trond Myklebust006abe82010-09-12 19:55:25 -0400425 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400426
Trond Myklebust03a9a422015-01-30 18:12:28 -0500427 if (nodename == NULL)
428 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500430 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400431
Chuck Leverd746e542013-10-17 14:12:17 -0400432 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400433 if (err)
434 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400435 if (parent)
436 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return clnt;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500440 rpc_free_iostats(clnt->cl_metrics);
441out_no_stats:
Trond Myklebust2f048db2013-09-04 21:51:44 -0400442 rpc_free_clid(clnt);
443out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 kfree(clnt);
445out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400446 rpciod_down();
447out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500448 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400449 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return ERR_PTR(err);
451}
452
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400453static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800454 struct rpc_xprt *xprt)
455{
456 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500457 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800458
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400459 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400460 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400461 xps = args->bc_xprt->xpt_bc_xps;
462 xprt_switch_get(xps);
463 } else {
464 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
465 if (xps == NULL) {
466 xprt_put(xprt);
467 return ERR_PTR(-ENOMEM);
468 }
469 if (xprt->bc_xprt) {
470 xprt_switch_get(xps);
471 xprt->bc_xprt->xpt_bc_xps = xps;
472 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400473 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500474 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800475 if (IS_ERR(clnt))
476 return clnt;
477
478 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
479 int err = rpc_ping(clnt);
480 if (err != 0) {
481 rpc_shutdown_client(clnt);
482 return ERR_PTR(err);
483 }
484 }
485
486 clnt->cl_softrtry = 1;
487 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
488 clnt->cl_softrtry = 0;
489
490 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
491 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400492 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
493 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800494 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
495 clnt->cl_discrtry = 1;
496 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
497 clnt->cl_chatty = 1;
498
499 return clnt;
500}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800501
Ben Hutchings2c530402012-07-10 10:55:09 +0000502/**
Chuck Leverc2866762006-08-22 20:06:20 -0400503 * rpc_create - create an RPC client and transport with one call
504 * @args: rpc_clnt create argument structure
505 *
506 * Creates and initializes an RPC transport and an RPC client.
507 *
508 * It can ping the server in order to determine if it is up, and to see if
509 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
510 * this behavior so asynchronous tasks can also use rpc_create.
511 */
512struct rpc_clnt *rpc_create(struct rpc_create_args *args)
513{
514 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400515 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400516 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400517 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200518 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200519 .dstaddr = args->address,
520 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500521 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300522 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200523 };
Chuck Lever510deb02007-12-10 14:56:24 -0500524 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400525
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400526 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400527 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400528 xprt = args->bc_xprt->xpt_bc_xprt;
529 if (xprt) {
530 xprt_get(xprt);
531 return rpc_create_xprt(args, xprt);
532 }
533 }
534
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400535 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
536 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400537 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
538 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400539 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400540 * If the caller chooses not to specify a hostname, whip
541 * up a string representation of the passed-in address.
542 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500543 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400544 struct sockaddr_un *sun =
545 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400546 struct sockaddr_in *sin =
547 (struct sockaddr_in *)args->address;
548 struct sockaddr_in6 *sin6 =
549 (struct sockaddr_in6 *)args->address;
550
Chuck Lever510deb02007-12-10 14:56:24 -0500551 servername[0] = '\0';
552 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400553 case AF_LOCAL:
554 snprintf(servername, sizeof(servername), "%s",
555 sun->sun_path);
556 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400557 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700558 snprintf(servername, sizeof(servername), "%pI4",
559 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500560 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400561 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700562 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400563 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500564 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500565 default:
566 /* caller wants default server name, but
567 * address family isn't recognized. */
568 return ERR_PTR(-EINVAL);
569 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500570 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400571 }
572
Chuck Lever510deb02007-12-10 14:56:24 -0500573 xprt = xprt_create_transport(&xprtargs);
574 if (IS_ERR(xprt))
575 return (struct rpc_clnt *)xprt;
576
Chuck Lever43780b82007-07-01 12:13:22 -0400577 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400578 * By default, kernel RPC client connects from a reserved port.
579 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
580 * but it is always enabled for rpciod, which handles the connect
581 * operation.
582 */
583 xprt->resvport = 1;
584 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
585 xprt->resvport = 0;
586
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800587 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400588}
Chuck Leverb86acd52006-08-22 20:06:22 -0400589EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * This function clones the RPC client structure. It allows us to share the
593 * same transport while varying parameters such as the authentication
594 * flavour.
595 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400596static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
597 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500599 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500600 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400601 struct rpc_clnt *new;
602 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Chuck Lever1b63a752012-09-14 17:23:52 -0400604 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500605 rcu_read_lock();
606 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500607 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500608 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500609 if (xprt == NULL || xps == NULL) {
610 xprt_put(xprt);
611 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400612 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500613 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400614 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500615 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400616
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500617 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400618 if (IS_ERR(new)) {
619 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400620 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400621 }
622
Chuck Lever1b63a752012-09-14 17:23:52 -0400623 /* Turn off autobind on clones */
624 new->cl_autobind = 0;
625 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400626 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400627 new->cl_discrtry = clnt->cl_discrtry;
628 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100629 new->cl_principal = clnt->cl_principal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400631
Chuck Lever1b63a752012-09-14 17:23:52 -0400632out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800633 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500634 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Chuck Lever1b63a752012-09-14 17:23:52 -0400636
637/**
638 * rpc_clone_client - Clone an RPC client structure
639 *
640 * @clnt: RPC client whose parameters are copied
641 *
642 * Returns a fresh RPC client or an ERR_PTR.
643 */
644struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
645{
646 struct rpc_create_args args = {
647 .program = clnt->cl_program,
648 .prognumber = clnt->cl_prog,
649 .version = clnt->cl_vers,
650 .authflavor = clnt->cl_auth->au_flavor,
Chuck Lever1b63a752012-09-14 17:23:52 -0400651 };
652 return __rpc_clone_client(&args, clnt);
653}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400654EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Chuck Leverba9b5842012-09-14 17:24:02 -0400656/**
657 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
658 *
659 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800660 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400661 *
662 * Returns a fresh RPC client or an ERR_PTR.
663 */
664struct rpc_clnt *
665rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
666{
667 struct rpc_create_args args = {
668 .program = clnt->cl_program,
669 .prognumber = clnt->cl_prog,
670 .version = clnt->cl_vers,
671 .authflavor = flavor,
Chuck Leverba9b5842012-09-14 17:24:02 -0400672 };
673 return __rpc_clone_client(&args, clnt);
674}
675EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
676
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400677/**
678 * rpc_switch_client_transport: switch the RPC transport on the fly
679 * @clnt: pointer to a struct rpc_clnt
680 * @args: pointer to the new transport arguments
681 * @timeout: pointer to the new timeout parameters
682 *
683 * This function allows the caller to switch the RPC transport for the
684 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
685 * server, for instance. It assumes that the caller has ensured that
686 * there are no active RPC tasks by using some form of locking.
687 *
688 * Returns zero if "clnt" is now using the new xprt. Otherwise a
689 * negative errno is returned, and "clnt" continues to use the old
690 * xprt.
691 */
692int rpc_switch_client_transport(struct rpc_clnt *clnt,
693 struct xprt_create *args,
694 const struct rpc_timeout *timeout)
695{
696 const struct rpc_timeout *old_timeo;
697 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500698 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400699 struct rpc_xprt *xprt, *old;
700 struct rpc_clnt *parent;
701 int err;
702
703 xprt = xprt_create_transport(args);
704 if (IS_ERR(xprt)) {
705 dprintk("RPC: failed to create new xprt for clnt %p\n",
706 clnt);
707 return PTR_ERR(xprt);
708 }
709
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500710 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
711 if (xps == NULL) {
712 xprt_put(xprt);
713 return -ENOMEM;
714 }
715
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400716 pseudoflavor = clnt->cl_auth->au_flavor;
717
718 old_timeo = clnt->cl_timeout;
719 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500720 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400721
722 rpc_unregister_client(clnt);
723 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500724 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400725
726 /*
727 * A new transport was created. "clnt" therefore
728 * becomes the root of a new cl_parent tree. clnt's
729 * children, if it has any, still point to the old xprt.
730 */
731 parent = clnt->cl_parent;
732 clnt->cl_parent = clnt;
733
734 /*
735 * The old rpc_auth cache cannot be re-used. GSS
736 * contexts in particular are between a single
737 * client and server.
738 */
739 err = rpc_client_register(clnt, pseudoflavor, NULL);
740 if (err)
741 goto out_revert;
742
743 synchronize_rcu();
744 if (parent != clnt)
745 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500746 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400747 xprt_put(old);
748 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
749 return 0;
750
751out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500752 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400753 rpc_clnt_set_transport(clnt, old, old_timeo);
754 clnt->cl_parent = parent;
755 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500756 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400757 xprt_put(xprt);
758 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
759 return err;
760}
761EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
762
Trond Myklebust32278862016-01-30 20:39:19 -0500763static
764int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
765{
766 struct rpc_xprt_switch *xps;
767
768 rcu_read_lock();
769 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
770 rcu_read_unlock();
771 if (xps == NULL)
772 return -EAGAIN;
773 xprt_iter_init_listall(xpi, xps);
774 xprt_switch_put(xps);
775 return 0;
776}
777
778/**
779 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
780 * @clnt: pointer to client
781 * @fn: function to apply
782 * @data: void pointer to function data
783 *
784 * Iterates through the list of RPC transports currently attached to the
785 * client and applies the function fn(clnt, xprt, data).
786 *
787 * On error, the iteration stops, and the function returns the error value.
788 */
789int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
790 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
791 void *data)
792{
793 struct rpc_xprt_iter xpi;
794 int ret;
795
796 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
797 if (ret)
798 return ret;
799 for (;;) {
800 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
801
802 if (!xprt)
803 break;
804 ret = fn(clnt, xprt, data);
805 xprt_put(xprt);
806 if (ret < 0)
807 break;
808 }
809 xprt_iter_destroy(&xpi);
810 return ret;
811}
812EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400815 * Kill all tasks for the given client.
816 * XXX: kill their descendants as well?
817 */
818void rpc_killall_tasks(struct rpc_clnt *clnt)
819{
820 struct rpc_task *rovr;
821
822
823 if (list_empty(&clnt->cl_tasks))
824 return;
825 dprintk("RPC: killing all tasks for client %p\n", clnt);
826 /*
827 * Spin lock all_tasks to prevent changes...
828 */
829 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400830 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
831 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400832 spin_unlock(&clnt->cl_lock);
833}
834EXPORT_SYMBOL_GPL(rpc_killall_tasks);
835
836/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400838 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400840void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400842 might_sleep();
843
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500844 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400845 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500846 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Trond Myklebust34f52e32007-06-14 16:40:31 -0400848 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800850 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400851 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Trond Myklebust4c402b42007-06-14 16:40:32 -0400854 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400856EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400859 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500861static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400862rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500864 struct rpc_clnt *parent = NULL;
865
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500866 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400867 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500868 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500869 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500870 parent = clnt->cl_parent;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500871 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400872 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400873 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500874 rpc_free_iostats(clnt->cl_metrics);
875 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500876 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500877 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400878 rpciod_down();
Trond Myklebust2f048db2013-09-04 21:51:44 -0400879 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500881 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400885 * Free an RPC client
886 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700887static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400888rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400889{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500890 if (clnt->cl_auth == NULL)
891 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400892
893 /*
894 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
895 * release remaining GSS contexts. This mechanism ensures
896 * that it can do so safely.
897 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400898 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400899 rpcauth_release(clnt->cl_auth);
900 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400901 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500902 return rpc_free_client(clnt);
903 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400904}
905
906/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400907 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909void
910rpc_release_client(struct rpc_clnt *clnt)
911{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400912 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Trond Myklebustd07ba842013-11-12 17:24:36 -0500914 do {
915 if (list_empty(&clnt->cl_tasks))
916 wake_up(&destroy_wait);
917 if (!atomic_dec_and_test(&clnt->cl_count))
918 break;
919 clnt = rpc_free_auth(clnt);
920 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400921}
Simo Sorce1d658332012-05-25 18:09:55 -0400922EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400923
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000924/**
925 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800926 * @old: old rpc_client
927 * @program: rpc program to set
928 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000929 *
930 * Clones the rpc client and sets up a new RPC program. This is mainly
931 * of use for enabling different RPC programs to share the same transport.
932 * The Sun NFSv2/v3 ACL protocol can do this.
933 */
934struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500935 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400936 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000937{
Trond Myklebustf994c432012-11-01 12:14:14 -0400938 struct rpc_create_args args = {
939 .program = program,
940 .prognumber = program->number,
941 .version = vers,
942 .authflavor = old->cl_auth->au_flavor,
Trond Myklebustf994c432012-11-01 12:14:14 -0400943 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000944 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000945 int err;
946
Trond Myklebustf994c432012-11-01 12:14:14 -0400947 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000948 if (IS_ERR(clnt))
949 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500950 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000951 if (err != 0) {
952 rpc_shutdown_client(clnt);
953 clnt = ERR_PTR(err);
954 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800955out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000956 return clnt;
957}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400958EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000959
Bill Baker0f90be12018-06-19 16:24:58 -0500960void rpc_task_release_transport(struct rpc_task *task)
961{
962 struct rpc_xprt *xprt = task->tk_xprt;
963
964 if (xprt) {
965 task->tk_xprt = NULL;
966 xprt_put(xprt);
967 }
968}
969EXPORT_SYMBOL_GPL(rpc_task_release_transport);
970
Trond Myklebust58f96122010-07-31 14:29:08 -0400971void rpc_task_release_client(struct rpc_task *task)
972{
973 struct rpc_clnt *clnt = task->tk_client;
974
975 if (clnt != NULL) {
976 /* Remove from client task list */
977 spin_lock(&clnt->cl_lock);
978 list_del(&task->tk_task);
979 spin_unlock(&clnt->cl_lock);
980 task->tk_client = NULL;
981
982 rpc_release_client(clnt);
983 }
Bill Baker0f90be12018-06-19 16:24:58 -0500984 rpc_task_release_transport(task);
985}
Trond Myklebustfb43d172016-01-30 16:39:26 -0500986
Bill Baker0f90be12018-06-19 16:24:58 -0500987static
988void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
989{
990 if (!task->tk_xprt)
991 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -0400992}
993
994static
995void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
996{
Trond Myklebustfb43d172016-01-30 16:39:26 -0500997
Trond Myklebust58f96122010-07-31 14:29:08 -0400998 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -0500999 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001000 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001001 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001002 if (clnt->cl_softrtry)
1003 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001004 if (clnt->cl_noretranstimeo)
1005 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001006 if (atomic_read(&clnt->cl_swapper))
1007 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001008 /* Add to the client's list of all tasks */
1009 spin_lock(&clnt->cl_lock);
1010 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1011 spin_unlock(&clnt->cl_lock);
1012 }
1013}
1014
1015static void
1016rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1017{
1018 if (msg != NULL) {
1019 task->tk_msg.rpc_proc = msg->rpc_proc;
1020 task->tk_msg.rpc_argp = msg->rpc_argp;
1021 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001022 if (msg->rpc_cred != NULL)
NeilBrowna52458b2018-12-03 11:30:31 +11001023 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001024 }
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
1028 * Default callback for async RPC calls
1029 */
1030static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001031rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033}
1034
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001035static const struct rpc_call_ops rpc_default_ops = {
1036 .rpc_call_done = rpc_default_callback,
1037};
1038
Trond Myklebustc970aa82007-07-14 15:39:59 -04001039/**
1040 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1041 * @task_setup_data: pointer to task initialisation data
1042 */
1043struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001044{
Trond Myklebust19445b92010-04-16 16:41:10 -04001045 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001046
Trond Myklebust84115e12007-07-14 15:39:59 -04001047 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001048
Trond Myklebust58f96122010-07-31 14:29:08 -04001049 rpc_task_set_client(task, task_setup_data->rpc_client);
1050 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1051
Trond Myklebust58f96122010-07-31 14:29:08 -04001052 if (task->tk_action == NULL)
1053 rpc_call_start(task);
1054
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001055 atomic_inc(&task->tk_count);
1056 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001057 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001058}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001059EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001060
1061/**
1062 * rpc_call_sync - Perform a synchronous RPC call
1063 * @clnt: pointer to RPC client
1064 * @msg: RPC call parameters
1065 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001067int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001070 struct rpc_task_setup task_setup_data = {
1071 .rpc_client = clnt,
1072 .rpc_message = msg,
1073 .callback_ops = &rpc_default_ops,
1074 .flags = flags,
1075 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001076 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001078 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1079 if (flags & RPC_TASK_ASYNC) {
1080 rpc_release_calldata(task_setup_data.callback_ops,
1081 task_setup_data.callback_data);
1082 return -EINVAL;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Trond Myklebustc970aa82007-07-14 15:39:59 -04001085 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001086 if (IS_ERR(task))
1087 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001088 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001089 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return status;
1091}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001092EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001094/**
1095 * rpc_call_async - Perform an asynchronous RPC call
1096 * @clnt: pointer to RPC client
1097 * @msg: RPC call parameters
1098 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001099 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001100 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
1102int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001103rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001104 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001107 struct rpc_task_setup task_setup_data = {
1108 .rpc_client = clnt,
1109 .rpc_message = msg,
1110 .callback_ops = tk_ops,
1111 .callback_data = data,
1112 .flags = flags|RPC_TASK_ASYNC,
1113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Trond Myklebustc970aa82007-07-14 15:39:59 -04001115 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001116 if (IS_ERR(task))
1117 return PTR_ERR(task);
1118 rpc_put_task(task);
1119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001121EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001123#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001124static void call_bc_encode(struct rpc_task *task);
1125
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001126/**
1127 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1128 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001129 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001130 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001131struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001132{
1133 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001134 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001135 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001136 .flags = RPC_TASK_SOFTCONN |
1137 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001138 };
1139
1140 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1141 /*
1142 * Create an rpc_task to send the data
1143 */
1144 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001145 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001146
Trond Myklebust477687e2019-03-05 07:30:48 -05001147 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001148 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001149 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001150 rpc_execute(task);
1151
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001152 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1153 return task;
1154}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001155#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001156
Chuck Levercf500ba2019-02-11 11:25:20 -05001157/**
1158 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1159 * @req: RPC request to prepare
1160 * @pages: vector of struct page pointers
1161 * @base: offset in first page where receive should start, in bytes
1162 * @len: expected size of the upper layer data payload, in bytes
1163 * @hdrsize: expected size of upper layer reply header, in XDR words
1164 *
1165 */
1166void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1167 unsigned int base, unsigned int len,
1168 unsigned int hdrsize)
1169{
Chuck Lever02ef04e2019-02-11 11:25:25 -05001170 /* Subtract one to force an extra word of buffer space for the
1171 * payload's XDR pad to fall into the rcv_buf's tail iovec.
1172 */
Chuck Lever35e77d22019-02-11 11:25:36 -05001173 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001174
Chuck Levercf500ba2019-02-11 11:25:20 -05001175 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1176 trace_rpc_reply_pages(req);
1177}
1178EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001181rpc_call_start(struct rpc_task *task)
1182{
1183 task->tk_action = call_start;
1184}
1185EXPORT_SYMBOL_GPL(rpc_call_start);
1186
Chuck Levered394402006-08-22 20:06:17 -04001187/**
1188 * rpc_peeraddr - extract remote peer address from clnt's xprt
1189 * @clnt: RPC client structure
1190 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001191 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001192 *
1193 * Returns the number of bytes that are actually in the stored address.
1194 */
1195size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1196{
1197 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001198 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001199
Trond Myklebust2446ab62012-03-01 17:00:56 -05001200 rcu_read_lock();
1201 xprt = rcu_dereference(clnt->cl_xprt);
1202
1203 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001204 if (bytes > bufsize)
1205 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001206 memcpy(buf, &xprt->addr, bytes);
1207 rcu_read_unlock();
1208
1209 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001210}
Chuck Leverb86acd52006-08-22 20:06:22 -04001211EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001212
Chuck Leverf425eba2006-08-22 20:06:18 -04001213/**
1214 * rpc_peeraddr2str - return remote peer address in printable format
1215 * @clnt: RPC client structure
1216 * @format: address format
1217 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001218 * NB: the lifetime of the memory referenced by the returned pointer is
1219 * the same as the rpc_xprt itself. As long as the caller uses this
1220 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001221 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001222const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1223 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001224{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001225 struct rpc_xprt *xprt;
1226
1227 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001228
1229 if (xprt->address_strings[format] != NULL)
1230 return xprt->address_strings[format];
1231 else
1232 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001233}
Chuck Leverb86acd52006-08-22 20:06:22 -04001234EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001235
Chuck Lever2e738fd2012-03-01 17:01:14 -05001236static const struct sockaddr_in rpc_inaddr_loopback = {
1237 .sin_family = AF_INET,
1238 .sin_addr.s_addr = htonl(INADDR_ANY),
1239};
1240
1241static const struct sockaddr_in6 rpc_in6addr_loopback = {
1242 .sin6_family = AF_INET6,
1243 .sin6_addr = IN6ADDR_ANY_INIT,
1244};
1245
1246/*
1247 * Try a getsockname() on a connected datagram socket. Using a
1248 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1249 * This conserves the ephemeral port number space.
1250 *
1251 * Returns zero and fills in "buf" if successful; otherwise, a
1252 * negative errno is returned.
1253 */
1254static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001255 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001256{
1257 struct socket *sock;
1258 int err;
1259
1260 err = __sock_create(net, sap->sa_family,
1261 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1262 if (err < 0) {
1263 dprintk("RPC: can't create UDP socket (%d)\n", err);
1264 goto out;
1265 }
1266
1267 switch (sap->sa_family) {
1268 case AF_INET:
1269 err = kernel_bind(sock,
1270 (struct sockaddr *)&rpc_inaddr_loopback,
1271 sizeof(rpc_inaddr_loopback));
1272 break;
1273 case AF_INET6:
1274 err = kernel_bind(sock,
1275 (struct sockaddr *)&rpc_in6addr_loopback,
1276 sizeof(rpc_in6addr_loopback));
1277 break;
1278 default:
1279 err = -EAFNOSUPPORT;
1280 goto out;
1281 }
1282 if (err < 0) {
1283 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1284 goto out_release;
1285 }
1286
1287 err = kernel_connect(sock, sap, salen, 0);
1288 if (err < 0) {
1289 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1290 goto out_release;
1291 }
1292
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001293 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001294 if (err < 0) {
1295 dprintk("RPC: getsockname failed (%d)\n", err);
1296 goto out_release;
1297 }
1298
1299 err = 0;
1300 if (buf->sa_family == AF_INET6) {
1301 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1302 sin6->sin6_scope_id = 0;
1303 }
1304 dprintk("RPC: %s succeeded\n", __func__);
1305
1306out_release:
1307 sock_release(sock);
1308out:
1309 return err;
1310}
1311
1312/*
1313 * Scraping a connected socket failed, so we don't have a useable
1314 * local address. Fallback: generate an address that will prevent
1315 * the server from calling us back.
1316 *
1317 * Returns zero and fills in "buf" if successful; otherwise, a
1318 * negative errno is returned.
1319 */
1320static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1321{
1322 switch (family) {
1323 case AF_INET:
1324 if (buflen < sizeof(rpc_inaddr_loopback))
1325 return -EINVAL;
1326 memcpy(buf, &rpc_inaddr_loopback,
1327 sizeof(rpc_inaddr_loopback));
1328 break;
1329 case AF_INET6:
1330 if (buflen < sizeof(rpc_in6addr_loopback))
1331 return -EINVAL;
1332 memcpy(buf, &rpc_in6addr_loopback,
1333 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001334 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001335 default:
1336 dprintk("RPC: %s: address family not supported\n",
1337 __func__);
1338 return -EAFNOSUPPORT;
1339 }
1340 dprintk("RPC: %s: succeeded\n", __func__);
1341 return 0;
1342}
1343
1344/**
1345 * rpc_localaddr - discover local endpoint address for an RPC client
1346 * @clnt: RPC client structure
1347 * @buf: target buffer
1348 * @buflen: size of target buffer, in bytes
1349 *
1350 * Returns zero and fills in "buf" and "buflen" if successful;
1351 * otherwise, a negative errno is returned.
1352 *
1353 * This works even if the underlying transport is not currently connected,
1354 * or if the upper layer never previously provided a source address.
1355 *
1356 * The result of this function call is transient: multiple calls in
1357 * succession may give different results, depending on how local
1358 * networking configuration changes over time.
1359 */
1360int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1361{
1362 struct sockaddr_storage address;
1363 struct sockaddr *sap = (struct sockaddr *)&address;
1364 struct rpc_xprt *xprt;
1365 struct net *net;
1366 size_t salen;
1367 int err;
1368
1369 rcu_read_lock();
1370 xprt = rcu_dereference(clnt->cl_xprt);
1371 salen = xprt->addrlen;
1372 memcpy(sap, &xprt->addr, salen);
1373 net = get_net(xprt->xprt_net);
1374 rcu_read_unlock();
1375
1376 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001377 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001378 put_net(net);
1379 if (err != 0)
1380 /* Couldn't discover local address, return ANYADDR */
1381 return rpc_anyaddr(sap->sa_family, buf, buflen);
1382 return 0;
1383}
1384EXPORT_SYMBOL_GPL(rpc_localaddr);
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386void
1387rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1388{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001389 struct rpc_xprt *xprt;
1390
1391 rcu_read_lock();
1392 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001393 if (xprt->ops->set_buffer_size)
1394 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001395 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001397EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Trond Myklebust2446ab62012-03-01 17:00:56 -05001399/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001400 * rpc_net_ns - Get the network namespace for this RPC client
1401 * @clnt: RPC client to query
1402 *
1403 */
1404struct net *rpc_net_ns(struct rpc_clnt *clnt)
1405{
1406 struct net *ret;
1407
1408 rcu_read_lock();
1409 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1410 rcu_read_unlock();
1411 return ret;
1412}
1413EXPORT_SYMBOL_GPL(rpc_net_ns);
1414
1415/**
1416 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1417 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *
1419 * For stream transports, this is one RPC record fragment (see RFC
1420 * 1831), as we don't support multi-record requests yet. For datagram
1421 * transports, this is the size of an IP packet minus the IP, UDP, and
1422 * RPC header sizes.
1423 */
1424size_t rpc_max_payload(struct rpc_clnt *clnt)
1425{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001426 size_t ret;
1427
1428 rcu_read_lock();
1429 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1430 rcu_read_unlock();
1431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
Chuck Leverb86acd52006-08-22 20:06:22 -04001433EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Chuck Lever35f5a422006-01-03 09:55:50 +01001435/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001436 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1437 * @clnt: RPC client to query
1438 */
1439size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1440{
1441 struct rpc_xprt *xprt;
1442 size_t ret;
1443
1444 rcu_read_lock();
1445 xprt = rcu_dereference(clnt->cl_xprt);
1446 ret = xprt->ops->bc_maxpayload(xprt);
1447 rcu_read_unlock();
1448 return ret;
1449}
1450EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1451
1452/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001453 * rpc_force_rebind - force transport to check that remote port is unchanged
1454 * @clnt: client to rebind
1455 *
1456 */
1457void rpc_force_rebind(struct rpc_clnt *clnt)
1458{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001459 if (clnt->cl_autobind) {
1460 rcu_read_lock();
1461 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1462 rcu_read_unlock();
1463 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001464}
Chuck Leverb86acd52006-08-22 20:06:22 -04001465EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001468 * Restart an (async) RPC call from the call_prepare state.
1469 * Usually called from within the exit handler.
1470 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001471int
Andy Adamsonaae20062009-04-01 09:22:40 -04001472rpc_restart_call_prepare(struct rpc_task *task)
1473{
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001474 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001475 task->tk_status = 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001476 if (task->tk_ops->rpc_call_prepare != NULL)
1477 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001478 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001479}
1480EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1481
1482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * Restart an (async) RPC call. Usually called from within the
1484 * exit handler.
1485 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001486int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487rpc_restart_call(struct rpc_task *task)
1488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001490 task->tk_status = 0;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001491 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001493EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001495const char
1496*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001497{
1498 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1499
1500 if (proc) {
1501 if (proc->p_name)
1502 return proc->p_name;
1503 else
1504 return "NULL";
1505 } else
1506 return "no proc";
1507}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/*
1510 * 0. Initial state
1511 *
1512 * Other FSM states can be visited zero or more times, but
1513 * this state is visited exactly once for each RPC.
1514 */
1515static void
1516call_start(struct rpc_task *task)
1517{
1518 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001519 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Chuck Leverc435da62017-11-03 13:46:14 -04001521 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001522 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001523 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001524 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001525 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001527 /* Increment call count (version might not be valid for ping) */
1528 if (clnt->cl_program->version[clnt->cl_vers])
1529 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 clnt->cl_stats->rpccnt++;
1531 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001532 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
1536 * 1. Reserve an RPC call slot
1537 */
1538static void
1539call_reserve(struct rpc_task *task)
1540{
Chuck Lever46121cf2007-01-31 12:14:08 -05001541 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 task->tk_status = 0;
1544 task->tk_action = call_reserveresult;
1545 xprt_reserve(task);
1546}
1547
Trond Myklebustba60eb22013-04-14 10:49:37 -04001548static void call_retry_reserve(struct rpc_task *task);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * 1b. Grok the result of xprt_reserve()
1552 */
1553static void
1554call_reserveresult(struct rpc_task *task)
1555{
1556 int status = task->tk_status;
1557
Chuck Lever46121cf2007-01-31 12:14:08 -05001558 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /*
1561 * After a call to xprt_reserve(), we must have either
1562 * a request slot or else an error status.
1563 */
1564 task->tk_status = 0;
1565 if (status >= 0) {
1566 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001567 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return;
1569 }
1570
1571 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001572 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 rpc_exit(task, -EIO);
1574 return;
1575 }
1576
1577 /*
1578 * Even though there was an error, we may have acquired
1579 * a request slot somehow. Make sure not to leak it.
1580 */
1581 if (task->tk_rqstp) {
1582 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001583 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 xprt_release(task);
1585 }
1586
1587 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001588 case -ENOMEM:
1589 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001590 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001592 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return;
1594 case -EIO: /* probably a shutdown */
1595 break;
1596 default:
1597 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001598 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 }
1601 rpc_exit(task, status);
1602}
1603
1604/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001605 * 1c. Retry reserving an RPC call slot
1606 */
1607static void
1608call_retry_reserve(struct rpc_task *task)
1609{
1610 dprint_status(task);
1611
1612 task->tk_status = 0;
1613 task->tk_action = call_reserveresult;
1614 xprt_retry_reserve(task);
1615}
1616
1617/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001618 * 2. Bind and/or refresh the credentials
1619 */
1620static void
1621call_refresh(struct rpc_task *task)
1622{
1623 dprint_status(task);
1624
1625 task->tk_action = call_refreshresult;
1626 task->tk_status = 0;
1627 task->tk_client->cl_stats->rpcauthrefresh++;
1628 rpcauth_refreshcred(task);
1629}
1630
1631/*
1632 * 2a. Process the results of a credential refresh
1633 */
1634static void
1635call_refreshresult(struct rpc_task *task)
1636{
1637 int status = task->tk_status;
1638
1639 dprint_status(task);
1640
1641 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001642 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001643 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001644 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001645 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001646 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001647 return;
1648 }
1649 /* Use rate-limiting and a max number of retries if refresh
1650 * had status 0 but failed to update the cred.
1651 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001652 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001653 case -ETIMEDOUT:
1654 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001655 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001656 case -EAGAIN:
1657 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001658 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001659 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001660 if (!task->tk_cred_retry)
1661 break;
1662 task->tk_cred_retry--;
1663 dprintk("RPC: %5u %s: retry refresh creds\n",
1664 task->tk_pid, __func__);
1665 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001666 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001667 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1668 task->tk_pid, __func__, status);
1669 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001670}
1671
1672/*
1673 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001674 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
1676static void
1677call_allocate(struct rpc_task *task)
1678{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001679 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001680 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001681 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001682 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001683 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Chuck Lever46121cf2007-01-31 12:14:08 -05001685 dprint_status(task);
1686
Chuck Lever2bea90d2007-03-29 16:47:53 -04001687 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001688 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001689
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001690 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return;
1692
Chuck Lever2bea90d2007-03-29 16:47:53 -04001693 if (proc->p_proc != 0) {
1694 BUG_ON(proc->p_arglen == 0);
1695 if (proc->p_decode != NULL)
1696 BUG_ON(proc->p_replen == 0);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Chuck Lever2bea90d2007-03-29 16:47:53 -04001699 /*
1700 * Calculate the size (in quads) of the RPC call
1701 * and reply headers, and convert both values
1702 * to byte sizes.
1703 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001704 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1705 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001706 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001707 /*
1708 * Note: the reply buffer must at minimum allocate enough space
1709 * for the 'struct accepted_reply' from RFC5531.
1710 */
1711 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1712 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001713 req->rq_rcvsize <<= 2;
1714
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001715 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001716 xprt_inject_disconnect(xprt);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001717 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001718 return;
1719 if (status != -ENOMEM) {
1720 rpc_exit(task, status);
1721 return;
1722 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001723
1724 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Trond Myklebust5afa91332011-06-17 10:14:59 -04001726 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001727 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 rpc_delay(task, HZ>>4);
1729 return;
1730 }
1731
1732 rpc_exit(task, -ERESTARTSYS);
1733}
1734
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001735static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001736rpc_task_need_encode(struct rpc_task *task)
1737{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001738 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1739 (!(task->tk_flags & RPC_TASK_SENT) ||
1740 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1741 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001745rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001748 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001750 xdr_buf_init(&req->rq_snd_buf,
1751 req->rq_buffer,
1752 req->rq_callsize);
1753 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001754 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001755 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Chuck Levere8680a22019-02-11 11:24:48 -05001757 req->rq_snd_buf.head[0].iov_len = 0;
1758 xdr_init_encode(&xdr, &req->rq_snd_buf,
1759 req->rq_snd_buf.head[0].iov_base, req);
1760 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001762
Chuck Levere8680a22019-02-11 11:24:48 -05001763 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001767 * 3. Encode arguments of an RPC call
1768 */
1769static void
1770call_encode(struct rpc_task *task)
1771{
1772 if (!rpc_task_need_encode(task))
1773 goto out;
Chuck Levere8680a22019-02-11 11:24:48 -05001774 dprint_status(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001775 /* Encode here so that rpcsec_gss can use correct sequence number. */
1776 rpc_xdr_encode(task);
1777 /* Did the encode result in an error condition? */
1778 if (task->tk_status != 0) {
1779 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001780 switch (task->tk_status) {
1781 case -EAGAIN:
1782 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001783 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001784 break;
1785 case -EKEYEXPIRED:
1786 task->tk_action = call_refresh;
1787 break;
1788 default:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001789 rpc_exit(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001790 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001791 return;
Chuck Levere8680a22019-02-11 11:24:48 -05001792 } else {
1793 xprt_request_prepare(task->tk_rqstp);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001794 }
1795
1796 /* Add task to reply queue before transmission to avoid races */
1797 if (rpc_reply_expected(task))
1798 xprt_request_enqueue_receive(task);
1799 xprt_request_enqueue_transmit(task);
1800out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001801 task->tk_action = call_transmit;
1802 /* Check that the connection is OK */
1803 if (!xprt_bound(task->tk_xprt))
1804 task->tk_action = call_bind;
1805 else if (!xprt_connected(task->tk_xprt))
1806 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001807}
1808
1809/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001810 * Helpers to check if the task was already transmitted, and
1811 * to take action when that is the case.
1812 */
1813static bool
1814rpc_task_transmitted(struct rpc_task *task)
1815{
1816 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1817}
1818
1819static void
1820rpc_task_handle_transmitted(struct rpc_task *task)
1821{
1822 xprt_end_transmit(task);
1823 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001824}
1825
1826/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * 4. Get the server port number if not yet set
1828 */
1829static void
1830call_bind(struct rpc_task *task)
1831{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001832 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Trond Myklebust03e51d32019-03-10 11:17:13 -04001834 if (rpc_task_transmitted(task)) {
1835 rpc_task_handle_transmitted(task);
1836 return;
1837 }
1838
Trond Myklebust009a82f2019-03-09 12:07:17 -05001839 if (xprt_bound(xprt)) {
1840 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001841 return;
1842 }
1843
Chuck Lever46121cf2007-01-31 12:14:08 -05001844 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Trond Myklebust009a82f2019-03-09 12:07:17 -05001846 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001847 if (!xprt_prepare_transmit(task))
1848 return;
1849
Trond Myklebust009a82f2019-03-09 12:07:17 -05001850 task->tk_timeout = xprt->bind_timeout;
1851 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854/*
Chuck Leverda351872005-08-11 16:25:11 -04001855 * 4a. Sort out bind result
1856 */
1857static void
1858call_bind_status(struct rpc_task *task)
1859{
Chuck Lever906462a2007-09-11 18:00:47 -04001860 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001861
Trond Myklebust03e51d32019-03-10 11:17:13 -04001862 if (rpc_task_transmitted(task)) {
1863 rpc_task_handle_transmitted(task);
1864 return;
1865 }
1866
Chuck Leverda351872005-08-11 16:25:11 -04001867 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001868 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001869 task->tk_status = 0;
1870 task->tk_action = call_connect;
1871 return;
1872 }
1873
Steve Dickson5753cba2012-02-06 10:08:08 -05001874 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001875 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001876 case -ENOMEM:
1877 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1878 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001879 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001880 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001881 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1882 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001883 /* fail immediately if this is an RPC ping */
1884 if (task->tk_msg.rpc_proc->p_proc == 0) {
1885 status = -EOPNOTSUPP;
1886 break;
1887 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001888 if (task->tk_rebind_retry == 0)
1889 break;
1890 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001891 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001892 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001893 case -EAGAIN:
1894 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001895 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001896 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001897 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001898 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001899 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001900 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001901 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001902 task->tk_pid);
1903 break;
1904 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001905 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001906 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001907 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001908 case -ECONNREFUSED: /* connection problems */
1909 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001910 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001911 case -ENOTCONN:
1912 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001913 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001914 case -EHOSTUNREACH:
1915 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001916 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001917 case -EPIPE:
1918 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1919 task->tk_pid, task->tk_status);
1920 if (!RPC_IS_SOFTCONN(task)) {
1921 rpc_delay(task, 5*HZ);
1922 goto retry_timeout;
1923 }
1924 status = task->tk_status;
1925 break;
Chuck Leverda351872005-08-11 16:25:11 -04001926 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001927 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001928 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001929 }
1930
1931 rpc_exit(task, status);
1932 return;
1933
Trond Myklebustda45828e2006-08-31 15:44:52 -04001934retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001935 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001936 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05001937 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04001938}
1939
1940/*
1941 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
1943static void
1944call_connect(struct rpc_task *task)
1945{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001946 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Trond Myklebust03e51d32019-03-10 11:17:13 -04001948 if (rpc_task_transmitted(task)) {
1949 rpc_task_handle_transmitted(task);
1950 return;
1951 }
1952
Trond Myklebust009a82f2019-03-09 12:07:17 -05001953 if (xprt_connected(xprt)) {
1954 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001955 return;
1956 }
1957
Chuck Lever46121cf2007-01-31 12:14:08 -05001958 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001959 task->tk_pid, xprt,
1960 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Trond Myklebust009a82f2019-03-09 12:07:17 -05001962 task->tk_action = call_connect_status;
1963 if (task->tk_status < 0)
1964 return;
1965 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1966 rpc_exit(task, -ENOTCONN);
1967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001969 if (!xprt_prepare_transmit(task))
1970 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001971 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/*
Chuck Leverda351872005-08-11 16:25:11 -04001975 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977static void
1978call_connect_status(struct rpc_task *task)
1979{
1980 struct rpc_clnt *clnt = task->tk_client;
1981 int status = task->tk_status;
1982
Trond Myklebust03e51d32019-03-10 11:17:13 -04001983 if (rpc_task_transmitted(task)) {
1984 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05001985 return;
1986 }
1987
Chuck Lever46121cf2007-01-31 12:14:08 -05001988 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001989
Chuck Levere671edb2018-03-16 10:33:44 -04001990 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04001991 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001993 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10001994 /* A positive refusal suggests a rebind is needed. */
1995 if (RPC_IS_SOFTCONN(task))
1996 break;
1997 if (clnt->cl_autobind) {
1998 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05001999 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002000 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002001 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002002 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002003 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002004 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002005 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002006 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002007 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002008 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002009 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002010 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2011 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002012 if (RPC_IS_SOFTCONN(task))
2013 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002014 /* retry with existing socket, after a delay */
2015 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002016 /* fall through */
Trond Myklebust0445f922018-12-17 13:34:59 -05002017 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002018 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002019 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002020 goto out_retry;
Trond Myklebust561ec162013-09-26 15:22:45 -04002021 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002022 clnt->cl_stats->netreconn++;
2023 task->tk_action = call_transmit;
2024 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002026 rpc_exit(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002027 return;
2028out_retry:
2029 /* Check for timeouts before looping back to call_bind */
2030 task->tk_action = call_bind;
2031 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/*
2035 * 5. Transmit the RPC request, and wait for reply
2036 */
2037static void
2038call_transmit(struct rpc_task *task)
2039{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002040 if (rpc_task_transmitted(task)) {
2041 rpc_task_handle_transmitted(task);
2042 return;
2043 }
2044
Chuck Lever46121cf2007-01-31 12:14:08 -05002045 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Trond Myklebusted7dc972019-03-04 14:19:31 -05002047 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002048 if (!xprt_prepare_transmit(task))
2049 return;
2050 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002051 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002052 if (!xprt_connected(task->tk_xprt)) {
2053 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002054 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002055 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002056 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002057 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002058 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002059}
2060
2061/*
2062 * 5a. Handle cleanup after a transmission
2063 */
2064static void
2065call_transmit_status(struct rpc_task *task)
2066{
2067 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002068
2069 /*
2070 * Common case: success. Force the compiler to put this
2071 * test first.
2072 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002073 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002074 task->tk_status = 0;
2075 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002076 return;
2077 }
2078
Trond Myklebust15f081c2009-03-11 14:37:57 -04002079 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002080 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002081 dprint_status(task);
Trond Myklebust75891f52018-09-03 17:37:36 -04002082 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002083 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002084 task->tk_status = 0;
2085 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002086 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002087 /*
2088 * Special cases: if we've been waiting on the
2089 * socket's write_space() callback, or if the
2090 * socket just returned a connection error,
2091 * then hold onto the transport lock.
2092 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002093 case -ENOBUFS:
2094 rpc_delay(task, HZ>>2);
2095 /* fall through */
Trond Myklebustc5445772018-09-03 23:39:27 -04002096 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002097 case -EAGAIN:
2098 task->tk_action = call_transmit;
2099 task->tk_status = 0;
2100 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002101 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002102 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002103 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002104 case -EHOSTUNREACH:
2105 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002106 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002107 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002108 if (!task->tk_msg.rpc_proc->p_proc)
2109 trace_xprt_ping(task->tk_xprt,
2110 task->tk_status);
Chuck Lever09a21c42009-12-03 15:58:56 -05002111 rpc_exit(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002112 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002113 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002114 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002115 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002116 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002117 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002118 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002119 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002120 task->tk_action = call_bind;
2121 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002122 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002123 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002124 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002127#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002128static void call_bc_transmit(struct rpc_task *task);
2129static void call_bc_transmit_status(struct rpc_task *task);
2130
2131static void
2132call_bc_encode(struct rpc_task *task)
2133{
2134 xprt_request_enqueue_transmit(task);
2135 task->tk_action = call_bc_transmit;
2136}
2137
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002138/*
2139 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2140 * addition, disconnect on connectivity errors.
2141 */
2142static void
2143call_bc_transmit(struct rpc_task *task)
2144{
Trond Myklebust477687e2019-03-05 07:30:48 -05002145 task->tk_action = call_bc_transmit_status;
2146 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2147 if (!xprt_prepare_transmit(task))
2148 return;
2149 task->tk_status = 0;
2150 xprt_transmit(task);
2151 }
2152 xprt_end_transmit(task);
2153}
2154
2155static void
2156call_bc_transmit_status(struct rpc_task *task)
2157{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002158 struct rpc_rqst *req = task->tk_rqstp;
2159
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002160 if (rpc_task_transmitted(task))
2161 task->tk_status = 0;
2162
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002163 dprint_status(task);
Trond Myklebust477687e2019-03-05 07:30:48 -05002164
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002165 switch (task->tk_status) {
2166 case 0:
2167 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002168 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002169 case -EHOSTDOWN:
2170 case -EHOSTUNREACH:
2171 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002172 case -ECONNRESET:
2173 case -ECONNREFUSED:
2174 case -EADDRINUSE:
2175 case -ENOTCONN:
2176 case -EPIPE:
2177 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002178 case -ENOBUFS:
2179 rpc_delay(task, HZ>>2);
2180 /* fall through */
2181 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002182 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002183 task->tk_status = 0;
2184 task->tk_action = call_bc_transmit;
2185 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002186 case -ETIMEDOUT:
2187 /*
2188 * Problem reaching the server. Disconnect and let the
2189 * forechannel reestablish the connection. The server will
2190 * have to retransmit the backchannel request and we'll
2191 * reprocess it. Since these ops are idempotent, there's no
2192 * need to cache our reply at this time.
2193 */
2194 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2195 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002196 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002197 req->rq_connect_cookie);
2198 break;
2199 default:
2200 /*
2201 * We were unable to reply and will have to drop the
2202 * request. The server should reconnect and retransmit.
2203 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002204 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2205 "error: %d\n", task->tk_status);
2206 break;
2207 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002208 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002209}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002210#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/*
2213 * 6. Sort out the RPC call status
2214 */
2215static void
2216call_status(struct rpc_task *task)
2217{
2218 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int status;
2220
Chuck Levera25a4cb2018-03-16 10:33:55 -04002221 if (!task->tk_msg.rpc_proc->p_proc)
2222 trace_xprt_ping(task->tk_xprt, task->tk_status);
2223
Chuck Lever46121cf2007-01-31 12:14:08 -05002224 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 status = task->tk_status;
2227 if (status >= 0) {
2228 task->tk_action = call_decode;
2229 return;
2230 }
2231
Steve Dickson5753cba2012-02-06 10:08:08 -05002232 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 task->tk_status = 0;
2234 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002235 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002236 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002237 case -EHOSTUNREACH:
2238 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002239 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002240 if (RPC_IS_SOFTCONN(task))
2241 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002242 /*
2243 * Delay any retries for 3 seconds, then handle as if it
2244 * were a timeout.
2245 */
2246 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002247 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 break;
2250 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002251 case -ECONNRESET:
2252 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002253 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002254 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002255 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002256 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002257 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002258 case -EPIPE:
2259 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 break;
2262 case -EIO:
2263 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002264 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002266 if (clnt->cl_chatty)
2267 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002268 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002269 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002271 task->tk_action = call_encode;
2272 rpc_check_timeout(task);
2273 return;
2274out_exit:
2275 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002278static bool
2279rpc_check_connected(const struct rpc_rqst *req)
2280{
2281 /* No allocated request or transport? return true */
2282 if (!req || !req->rq_xprt)
2283 return true;
2284 return xprt_connected(req->rq_xprt);
2285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002288rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 struct rpc_clnt *clnt = task->tk_client;
2291
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002292 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Chuck Lever46121cf2007-01-31 12:14:08 -05002295 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002296 task->tk_timeouts++;
2297
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002298 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Chuck Lever3a28bec2009-12-03 15:58:56 -05002299 rpc_exit(task, -ETIMEDOUT);
2300 return;
2301 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07002304 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002305 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002306 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002307 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002308 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002309 if (task->tk_flags & RPC_TASK_TIMEOUT)
2310 rpc_exit(task, -ETIMEDOUT);
2311 else
2312 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return;
2314 }
2315
Chuck Leverf518e35a2006-01-03 09:55:52 +01002316 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002318 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002319 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002320 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002321 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002324 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002325 /*
2326 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2327 * event? RFC2203 requires the server to drop all such requests.
2328 */
2329 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 * 7. Decode the RPC reply
2334 */
2335static void
2336call_decode(struct rpc_task *task)
2337{
2338 struct rpc_clnt *clnt = task->tk_client;
2339 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002340 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Vasily Averin726fd6a2011-06-01 16:23:59 +04002342 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Chuck Levera0584ee2019-02-11 11:24:58 -05002344 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002345 task->tk_action = rpc_exit_task;
2346 return;
2347 }
2348
Chuck Leverf518e35a2006-01-03 09:55:52 +01002349 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002350 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002351 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002352 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002353 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2356 }
2357
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002358 /*
2359 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002360 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002361 */
2362 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 req->rq_rcv_buf.len = req->rq_private_buf.len;
2364
2365 /* Check that the softirq receive buffer is valid */
2366 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2367 sizeof(req->rq_rcv_buf)) != 0);
2368
Chuck Levera0584ee2019-02-11 11:24:58 -05002369 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2370 req->rq_rcv_buf.head[0].iov_base, req);
2371 switch (rpc_decode_header(task, &xdr)) {
2372 case 0:
2373 task->tk_action = rpc_exit_task;
2374 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2375 dprintk("RPC: %5u %s result %d\n",
2376 task->tk_pid, __func__, task->tk_status);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002377 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002378 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002379 task->tk_status = 0;
2380 /* Note: rpc_decode_header() may have freed the RPC slot */
2381 if (task->tk_rqstp == req) {
2382 xdr_free_bvec(&req->rq_rcv_buf);
2383 req->rq_reply_bytes_recvd = 0;
2384 req->rq_rcv_buf.len = 0;
2385 if (task->tk_client->cl_discrtry)
2386 xprt_conditional_disconnect(req->rq_xprt,
2387 req->rq_connect_cookie);
2388 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002389 task->tk_action = call_encode;
2390 rpc_check_timeout(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Chuck Levere8680a22019-02-11 11:24:48 -05002394static int
2395rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002399 __be32 *p;
2400 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Chuck Levere8680a22019-02-11 11:24:48 -05002402 error = -EMSGSIZE;
2403 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2404 if (!p)
2405 goto out_fail;
2406 *p++ = req->rq_xid;
2407 *p++ = rpc_call;
2408 *p++ = cpu_to_be32(RPC_VERSION);
2409 *p++ = cpu_to_be32(clnt->cl_prog);
2410 *p++ = cpu_to_be32(clnt->cl_vers);
2411 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002412
Chuck Levere8680a22019-02-11 11:24:48 -05002413 error = rpcauth_marshcred(task, xdr);
2414 if (error < 0)
2415 goto out_fail;
2416 return 0;
2417out_fail:
2418 trace_rpc_bad_callhdr(task);
2419 rpc_exit(task, error);
2420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Chuck Levera0584ee2019-02-11 11:24:58 -05002423static noinline int
2424rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002426 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002427 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002428 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Chuck Lever7f5667a2019-02-11 11:24:53 -05002430 /* RFC-1014 says that the representation of XDR data must be a
2431 * multiple of four bytes
2432 * - if it isn't pointer subtraction in the NFS client may give
2433 * undefined results
2434 */
2435 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002436 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Chuck Levera0584ee2019-02-11 11:24:58 -05002438 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2439 if (!p)
2440 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002441 p++; /* skip XID */
2442 if (*p++ != rpc_reply)
2443 goto out_unparsable;
2444 if (*p++ != rpc_msg_accepted)
2445 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002446
Chuck Levera0584ee2019-02-11 11:24:58 -05002447 error = rpcauth_checkverf(task, xdr);
2448 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002449 goto out_verifier;
2450
Chuck Levera0584ee2019-02-11 11:24:58 -05002451 p = xdr_inline_decode(xdr, sizeof(*p));
2452 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002453 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002454 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002455 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002456 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002457 case rpc_prog_unavail:
2458 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002459 error = -EPFNOSUPPORT;
2460 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002461 case rpc_prog_mismatch:
2462 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002463 error = -EPROTONOSUPPORT;
2464 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002465 case rpc_proc_unavail:
2466 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002467 error = -EOPNOTSUPP;
2468 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002469 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002470 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002471 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002472 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002475 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477
Trond Myklebustabbcf282006-01-03 09:55:03 +01002478out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002479 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (task->tk_garb_retry) {
2481 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002482 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002483 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485out_err:
2486 rpc_exit(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002487 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002488
Chuck Lever7f5667a2019-02-11 11:24:53 -05002489out_unparsable:
2490 trace_rpc__unparsable(task);
2491 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002492 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002493
2494out_verifier:
2495 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002496 goto out_garbage;
2497
2498out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002499 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002500 p = xdr_inline_decode(xdr, sizeof(*p));
2501 if (!p)
2502 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002503 switch (*p++) {
2504 case rpc_auth_error:
2505 break;
2506 case rpc_mismatch:
2507 trace_rpc__mismatch(task);
2508 error = -EPROTONOSUPPORT;
2509 goto out_err;
2510 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002511 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002512 }
2513
Chuck Levera0584ee2019-02-11 11:24:58 -05002514 p = xdr_inline_decode(xdr, sizeof(*p));
2515 if (!p)
2516 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002517 switch (*p++) {
2518 case rpc_autherr_rejectedcred:
2519 case rpc_autherr_rejectedverf:
2520 case rpcsec_gsserr_credproblem:
2521 case rpcsec_gsserr_ctxproblem:
2522 if (!task->tk_cred_retry)
2523 break;
2524 task->tk_cred_retry--;
2525 trace_rpc__stale_creds(task);
2526 rpcauth_invalcred(task);
2527 /* Ensure we obtain a new XID! */
2528 xprt_release(task);
2529 task->tk_action = call_reserve;
Chuck Levera0584ee2019-02-11 11:24:58 -05002530 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002531 case rpc_autherr_badcred:
2532 case rpc_autherr_badverf:
2533 /* possibly garbled cred/verf? */
2534 if (!task->tk_garb_retry)
2535 break;
2536 task->tk_garb_retry--;
2537 trace_rpc__bad_creds(task);
2538 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002539 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002540 case rpc_autherr_tooweak:
2541 trace_rpc__auth_tooweak(task);
2542 pr_warn("RPC: server %s requires stronger authentication.\n",
2543 task->tk_xprt->servername);
2544 break;
2545 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002546 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002547 }
2548 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002550
Christoph Hellwigc512f362017-05-08 09:31:19 +02002551static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2552 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002553{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002554}
2555
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002556static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2557 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002558{
2559 return 0;
2560}
2561
Christoph Hellwig499b4982017-05-12 15:36:49 +02002562static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002563 .p_encode = rpcproc_encode_null,
2564 .p_decode = rpcproc_decode_null,
2565};
2566
Chuck Levercaabea82009-12-03 15:58:56 -05002567static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002568{
2569 struct rpc_message msg = {
2570 .rpc_proc = &rpcproc_null,
2571 };
2572 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002573 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2574 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002575 return err;
2576}
Trond Myklebust188fef12007-06-16 14:18:40 -04002577
Trond Myklebust7f554892016-01-30 23:43:35 -05002578static
2579struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2580 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2581 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002582{
2583 struct rpc_message msg = {
2584 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002585 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002586 struct rpc_task_setup task_setup_data = {
2587 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002588 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002589 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002590 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002591 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2592 .callback_data = data,
NeilBrowna52458b2018-12-03 11:30:31 +11002593 .flags = flags | RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002594 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002595
Trond Myklebustc970aa82007-07-14 15:39:59 -04002596 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002597}
Trond Myklebust7f554892016-01-30 23:43:35 -05002598
2599struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2600{
2601 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2602}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002603EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002604
Trond Myklebust7f554892016-01-30 23:43:35 -05002605struct rpc_cb_add_xprt_calldata {
2606 struct rpc_xprt_switch *xps;
2607 struct rpc_xprt *xprt;
2608};
2609
2610static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2611{
2612 struct rpc_cb_add_xprt_calldata *data = calldata;
2613
2614 if (task->tk_status == 0)
2615 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2616}
2617
2618static void rpc_cb_add_xprt_release(void *calldata)
2619{
2620 struct rpc_cb_add_xprt_calldata *data = calldata;
2621
2622 xprt_put(data->xprt);
2623 xprt_switch_put(data->xps);
2624 kfree(data);
2625}
2626
Trond Myklebustce272302016-07-24 17:06:28 -04002627static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002628 .rpc_call_done = rpc_cb_add_xprt_done,
2629 .rpc_release = rpc_cb_add_xprt_release,
2630};
2631
2632/**
2633 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2634 * @clnt: pointer to struct rpc_clnt
2635 * @xps: pointer to struct rpc_xprt_switch,
2636 * @xprt: pointer struct rpc_xprt
2637 * @dummy: unused
2638 */
2639int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2640 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2641 void *dummy)
2642{
2643 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002644 struct rpc_task *task;
2645
2646 data = kmalloc(sizeof(*data), GFP_NOFS);
2647 if (!data)
2648 return -ENOMEM;
2649 data->xps = xprt_switch_get(xps);
2650 data->xprt = xprt_get(xprt);
2651
NeilBrowna68a72e2018-12-03 11:30:30 +11002652 task = rpc_call_null_helper(clnt, xprt, NULL,
2653 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
Trond Myklebust7f554892016-01-30 23:43:35 -05002654 &rpc_cb_add_xprt_call_ops, data);
Trond Myklebust7f554892016-01-30 23:43:35 -05002655 if (IS_ERR(task))
2656 return PTR_ERR(task);
2657 rpc_put_task(task);
2658 return 1;
2659}
2660EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2661
2662/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002663 * rpc_clnt_setup_test_and_add_xprt()
2664 *
2665 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2666 * 1) caller of the test function must dereference the rpc_xprt_switch
2667 * and the rpc_xprt.
2668 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2669 * the rpc_call_done routine.
2670 *
2671 * Upon success (return of 1), the test function adds the new
2672 * transport to the rpc_clnt xprt switch
2673 *
2674 * @clnt: struct rpc_clnt to get the new transport
2675 * @xps: the rpc_xprt_switch to hold the new transport
2676 * @xprt: the rpc_xprt to test
2677 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2678 * and test function call data
2679 */
2680int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2681 struct rpc_xprt_switch *xps,
2682 struct rpc_xprt *xprt,
2683 void *data)
2684{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002685 struct rpc_task *task;
2686 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2687 int status = -EADDRINUSE;
2688
2689 xprt = xprt_get(xprt);
2690 xprt_switch_get(xps);
2691
2692 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2693 goto out_err;
2694
2695 /* Test the connection */
NeilBrowna68a72e2018-12-03 11:30:30 +11002696 task = rpc_call_null_helper(clnt, xprt, NULL,
2697 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002698 NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002699 if (IS_ERR(task)) {
2700 status = PTR_ERR(task);
2701 goto out_err;
2702 }
2703 status = task->tk_status;
2704 rpc_put_task(task);
2705
2706 if (status < 0)
2707 goto out_err;
2708
2709 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2710 xtest->add_xprt_test(clnt, xprt, xtest->data);
2711
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302712 xprt_put(xprt);
2713 xprt_switch_put(xps);
2714
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002715 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2716 return 1;
2717out_err:
2718 xprt_put(xprt);
2719 xprt_switch_put(xps);
2720 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2721 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2722 return status;
2723}
2724EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2725
2726/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002727 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2728 * @clnt: pointer to struct rpc_clnt
2729 * @xprtargs: pointer to struct xprt_create
2730 * @setup: callback to test and/or set up the connection
2731 * @data: pointer to setup function data
2732 *
2733 * Creates a new transport using the parameters set in args and
2734 * adds it to clnt.
2735 * If ping is set, then test that connectivity succeeds before
2736 * adding the new transport.
2737 *
2738 */
2739int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2740 struct xprt_create *xprtargs,
2741 int (*setup)(struct rpc_clnt *,
2742 struct rpc_xprt_switch *,
2743 struct rpc_xprt *,
2744 void *),
2745 void *data)
2746{
2747 struct rpc_xprt_switch *xps;
2748 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002749 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002750 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002751 unsigned char resvport;
2752 int ret = 0;
2753
2754 rcu_read_lock();
2755 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2756 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2757 if (xps == NULL || xprt == NULL) {
2758 rcu_read_unlock();
2759 return -EAGAIN;
2760 }
2761 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002762 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002763 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002764 rcu_read_unlock();
2765
2766 xprt = xprt_create_transport(xprtargs);
2767 if (IS_ERR(xprt)) {
2768 ret = PTR_ERR(xprt);
2769 goto out_put_switch;
2770 }
2771 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002772 if (xprt->ops->set_connect_timeout != NULL)
2773 xprt->ops->set_connect_timeout(xprt,
2774 connect_timeout,
2775 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002776
2777 rpc_xprt_switch_set_roundrobin(xps);
2778 if (setup) {
2779 ret = setup(clnt, xps, xprt, data);
2780 if (ret != 0)
2781 goto out_put_xprt;
2782 }
2783 rpc_xprt_switch_add_xprt(xps, xprt);
2784out_put_xprt:
2785 xprt_put(xprt);
2786out_put_switch:
2787 xprt_switch_put(xps);
2788 return ret;
2789}
2790EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2791
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002792struct connect_timeout_data {
2793 unsigned long connect_timeout;
2794 unsigned long reconnect_timeout;
2795};
2796
Trond Myklebust8d480322016-08-05 19:03:31 -04002797static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002798rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002799 struct rpc_xprt *xprt,
2800 void *data)
2801{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002802 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002803
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002804 if (xprt->ops->set_connect_timeout)
2805 xprt->ops->set_connect_timeout(xprt,
2806 timeo->connect_timeout,
2807 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002808 return 0;
2809}
2810
2811void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002812rpc_set_connect_timeout(struct rpc_clnt *clnt,
2813 unsigned long connect_timeout,
2814 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002815{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002816 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002817 .connect_timeout = connect_timeout,
2818 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002819 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002820 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002821 rpc_xprt_set_connect_timeout,
2822 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002823}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002824EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002825
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002826void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2827{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002828 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002829 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002830 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002831}
2832EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2833
Andy Adamsondd691712016-09-09 09:22:24 -04002834void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2835{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002836 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002837 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2838 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002839 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002840}
2841EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2842
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002843bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2844 const struct sockaddr *sap)
2845{
2846 struct rpc_xprt_switch *xps;
2847 bool ret;
2848
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002849 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002850 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002851 ret = rpc_xprt_switch_has_addr(xps, sap);
2852 rcu_read_unlock();
2853 return ret;
2854}
2855EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2856
Jeff Laytonf895b252014-11-17 16:58:04 -05002857#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002858static void rpc_show_header(void)
2859{
Chuck Levercb3997b2008-05-21 17:09:41 -04002860 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2861 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002862}
2863
Chuck Lever38e886e2008-05-21 17:09:33 -04002864static void rpc_show_task(const struct rpc_clnt *clnt,
2865 const struct rpc_task *task)
2866{
2867 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002868
2869 if (RPC_IS_QUEUED(task))
2870 rpc_waitq = rpc_qname(task->tk_waitqueue);
2871
Joe Perchesb3bceda2010-12-21 10:52:24 -05002872 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 -04002873 task->tk_pid, task->tk_flags, task->tk_status,
2874 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002875 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002876 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002877}
2878
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002879void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002880{
2881 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002882 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002883 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002884 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002885
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002886 spin_lock(&sn->rpc_client_lock);
2887 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002888 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002889 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002890 if (!header) {
2891 rpc_show_header();
2892 header++;
2893 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002894 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002895 }
2896 spin_unlock(&clnt->cl_lock);
2897 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002898 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002899}
2900#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002901
2902#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002903static int
2904rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2905 struct rpc_xprt *xprt,
2906 void *dummy)
2907{
2908 return xprt_enable_swap(xprt);
2909}
2910
Jeff Layton3c87ef62015-06-03 16:14:25 -04002911int
2912rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2913{
Trond Myklebust15001e52016-01-30 20:05:34 -05002914 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2915 return rpc_clnt_iterate_for_each_xprt(clnt,
2916 rpc_clnt_swap_activate_callback, NULL);
2917 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002918}
2919EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2920
Trond Myklebust15001e52016-01-30 20:05:34 -05002921static int
2922rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2923 struct rpc_xprt *xprt,
2924 void *dummy)
2925{
2926 xprt_disable_swap(xprt);
2927 return 0;
2928}
2929
Jeff Layton3c87ef62015-06-03 16:14:25 -04002930void
2931rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2932{
Trond Myklebust15001e52016-01-30 20:05:34 -05002933 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2934 rpc_clnt_iterate_for_each_xprt(clnt,
2935 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002936}
2937EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2938#endif /* CONFIG_SUNRPC_SWAP */