blob: b25f317d0ee213b56cd64e3664200213400e9537 [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
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001467static int
1468__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001469{
Trond Myklebust494314c2014-03-20 12:59:09 -04001470 task->tk_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001471 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001472 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001473}
Andy Adamsonaae20062009-04-01 09:22:40 -04001474
1475/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * Restart an (async) RPC call. Usually called from within the
1477 * exit handler.
1478 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001479int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480rpc_restart_call(struct rpc_task *task)
1481{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001482 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001484EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001486/*
1487 * Restart an (async) RPC call from the call_prepare state.
1488 * Usually called from within the exit handler.
1489 */
1490int
1491rpc_restart_call_prepare(struct rpc_task *task)
1492{
1493 if (task->tk_ops->rpc_call_prepare != NULL)
1494 return __rpc_restart_call(task, rpc_prepare_task);
1495 return rpc_restart_call(task);
1496}
1497EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1498
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001499const char
1500*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001501{
1502 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1503
1504 if (proc) {
1505 if (proc->p_name)
1506 return proc->p_name;
1507 else
1508 return "NULL";
1509 } else
1510 return "no proc";
1511}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/*
1514 * 0. Initial state
1515 *
1516 * Other FSM states can be visited zero or more times, but
1517 * this state is visited exactly once for each RPC.
1518 */
1519static void
1520call_start(struct rpc_task *task)
1521{
1522 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001523 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Chuck Leverc435da62017-11-03 13:46:14 -04001525 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001526 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001527 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001528 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001529 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001531 /* Increment call count (version might not be valid for ping) */
1532 if (clnt->cl_program->version[clnt->cl_vers])
1533 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 clnt->cl_stats->rpccnt++;
1535 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001536 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
1540 * 1. Reserve an RPC call slot
1541 */
1542static void
1543call_reserve(struct rpc_task *task)
1544{
Chuck Lever46121cf2007-01-31 12:14:08 -05001545 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 task->tk_status = 0;
1548 task->tk_action = call_reserveresult;
1549 xprt_reserve(task);
1550}
1551
Trond Myklebustba60eb22013-04-14 10:49:37 -04001552static void call_retry_reserve(struct rpc_task *task);
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * 1b. Grok the result of xprt_reserve()
1556 */
1557static void
1558call_reserveresult(struct rpc_task *task)
1559{
1560 int status = task->tk_status;
1561
Chuck Lever46121cf2007-01-31 12:14:08 -05001562 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /*
1565 * After a call to xprt_reserve(), we must have either
1566 * a request slot or else an error status.
1567 */
1568 task->tk_status = 0;
1569 if (status >= 0) {
1570 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001571 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return;
1573 }
1574
1575 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001576 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 rpc_exit(task, -EIO);
1578 return;
1579 }
1580
1581 /*
1582 * Even though there was an error, we may have acquired
1583 * a request slot somehow. Make sure not to leak it.
1584 */
1585 if (task->tk_rqstp) {
1586 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001587 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 xprt_release(task);
1589 }
1590
1591 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001592 case -ENOMEM:
1593 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001594 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001596 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return;
1598 case -EIO: /* probably a shutdown */
1599 break;
1600 default:
1601 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001602 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
1604 }
1605 rpc_exit(task, status);
1606}
1607
1608/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001609 * 1c. Retry reserving an RPC call slot
1610 */
1611static void
1612call_retry_reserve(struct rpc_task *task)
1613{
1614 dprint_status(task);
1615
1616 task->tk_status = 0;
1617 task->tk_action = call_reserveresult;
1618 xprt_retry_reserve(task);
1619}
1620
1621/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001622 * 2. Bind and/or refresh the credentials
1623 */
1624static void
1625call_refresh(struct rpc_task *task)
1626{
1627 dprint_status(task);
1628
1629 task->tk_action = call_refreshresult;
1630 task->tk_status = 0;
1631 task->tk_client->cl_stats->rpcauthrefresh++;
1632 rpcauth_refreshcred(task);
1633}
1634
1635/*
1636 * 2a. Process the results of a credential refresh
1637 */
1638static void
1639call_refreshresult(struct rpc_task *task)
1640{
1641 int status = task->tk_status;
1642
1643 dprint_status(task);
1644
1645 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001646 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001647 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001648 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001649 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001650 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001651 return;
1652 }
1653 /* Use rate-limiting and a max number of retries if refresh
1654 * had status 0 but failed to update the cred.
1655 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001656 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001657 case -ETIMEDOUT:
1658 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001659 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001660 case -EAGAIN:
1661 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001662 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001663 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001664 if (!task->tk_cred_retry)
1665 break;
1666 task->tk_cred_retry--;
1667 dprintk("RPC: %5u %s: retry refresh creds\n",
1668 task->tk_pid, __func__);
1669 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001670 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001671 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1672 task->tk_pid, __func__, status);
1673 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001674}
1675
1676/*
1677 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001678 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
1680static void
1681call_allocate(struct rpc_task *task)
1682{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001683 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001684 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001685 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001686 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001687 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Chuck Lever46121cf2007-01-31 12:14:08 -05001689 dprint_status(task);
1690
Chuck Lever2bea90d2007-03-29 16:47:53 -04001691 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001692 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001693
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001694 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return;
1696
Chuck Lever2bea90d2007-03-29 16:47:53 -04001697 if (proc->p_proc != 0) {
1698 BUG_ON(proc->p_arglen == 0);
1699 if (proc->p_decode != NULL)
1700 BUG_ON(proc->p_replen == 0);
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Chuck Lever2bea90d2007-03-29 16:47:53 -04001703 /*
1704 * Calculate the size (in quads) of the RPC call
1705 * and reply headers, and convert both values
1706 * to byte sizes.
1707 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001708 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1709 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001710 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001711 /*
1712 * Note: the reply buffer must at minimum allocate enough space
1713 * for the 'struct accepted_reply' from RFC5531.
1714 */
1715 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1716 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001717 req->rq_rcvsize <<= 2;
1718
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001719 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001720 xprt_inject_disconnect(xprt);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001721 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001722 return;
1723 if (status != -ENOMEM) {
1724 rpc_exit(task, status);
1725 return;
1726 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001727
1728 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Trond Myklebust5afa91332011-06-17 10:14:59 -04001730 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001731 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 rpc_delay(task, HZ>>4);
1733 return;
1734 }
1735
1736 rpc_exit(task, -ERESTARTSYS);
1737}
1738
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001739static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001740rpc_task_need_encode(struct rpc_task *task)
1741{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001742 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1743 (!(task->tk_flags & RPC_TASK_SENT) ||
1744 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1745 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001749rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001752 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001754 xdr_buf_init(&req->rq_snd_buf,
1755 req->rq_buffer,
1756 req->rq_callsize);
1757 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001758 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001759 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Chuck Levere8680a22019-02-11 11:24:48 -05001761 req->rq_snd_buf.head[0].iov_len = 0;
1762 xdr_init_encode(&xdr, &req->rq_snd_buf,
1763 req->rq_snd_buf.head[0].iov_base, req);
1764 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001766
Chuck Levere8680a22019-02-11 11:24:48 -05001767 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
1770/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001771 * 3. Encode arguments of an RPC call
1772 */
1773static void
1774call_encode(struct rpc_task *task)
1775{
1776 if (!rpc_task_need_encode(task))
1777 goto out;
Chuck Levere8680a22019-02-11 11:24:48 -05001778 dprint_status(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001779 /* Encode here so that rpcsec_gss can use correct sequence number. */
1780 rpc_xdr_encode(task);
1781 /* Did the encode result in an error condition? */
1782 if (task->tk_status != 0) {
1783 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001784 switch (task->tk_status) {
1785 case -EAGAIN:
1786 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001787 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001788 break;
1789 case -EKEYEXPIRED:
1790 task->tk_action = call_refresh;
1791 break;
1792 default:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001793 rpc_exit(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001794 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001795 return;
Chuck Levere8680a22019-02-11 11:24:48 -05001796 } else {
1797 xprt_request_prepare(task->tk_rqstp);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001798 }
1799
1800 /* Add task to reply queue before transmission to avoid races */
1801 if (rpc_reply_expected(task))
1802 xprt_request_enqueue_receive(task);
1803 xprt_request_enqueue_transmit(task);
1804out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001805 task->tk_action = call_transmit;
1806 /* Check that the connection is OK */
1807 if (!xprt_bound(task->tk_xprt))
1808 task->tk_action = call_bind;
1809 else if (!xprt_connected(task->tk_xprt))
1810 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001811}
1812
1813/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001814 * Helpers to check if the task was already transmitted, and
1815 * to take action when that is the case.
1816 */
1817static bool
1818rpc_task_transmitted(struct rpc_task *task)
1819{
1820 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1821}
1822
1823static void
1824rpc_task_handle_transmitted(struct rpc_task *task)
1825{
1826 xprt_end_transmit(task);
1827 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001828}
1829
1830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 * 4. Get the server port number if not yet set
1832 */
1833static void
1834call_bind(struct rpc_task *task)
1835{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001836 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Trond Myklebust03e51d32019-03-10 11:17:13 -04001838 if (rpc_task_transmitted(task)) {
1839 rpc_task_handle_transmitted(task);
1840 return;
1841 }
1842
Trond Myklebust009a82f2019-03-09 12:07:17 -05001843 if (xprt_bound(xprt)) {
1844 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001845 return;
1846 }
1847
Chuck Lever46121cf2007-01-31 12:14:08 -05001848 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Trond Myklebust009a82f2019-03-09 12:07:17 -05001850 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001851 if (!xprt_prepare_transmit(task))
1852 return;
1853
Trond Myklebust009a82f2019-03-09 12:07:17 -05001854 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857/*
Chuck Leverda351872005-08-11 16:25:11 -04001858 * 4a. Sort out bind result
1859 */
1860static void
1861call_bind_status(struct rpc_task *task)
1862{
Chuck Lever906462a2007-09-11 18:00:47 -04001863 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001864
Trond Myklebust03e51d32019-03-10 11:17:13 -04001865 if (rpc_task_transmitted(task)) {
1866 rpc_task_handle_transmitted(task);
1867 return;
1868 }
1869
Chuck Leverda351872005-08-11 16:25:11 -04001870 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001871 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001872 task->tk_status = 0;
1873 task->tk_action = call_connect;
1874 return;
1875 }
1876
Steve Dickson5753cba2012-02-06 10:08:08 -05001877 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001878 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001879 case -ENOMEM:
1880 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1881 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001882 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001883 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001884 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1885 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001886 /* fail immediately if this is an RPC ping */
1887 if (task->tk_msg.rpc_proc->p_proc == 0) {
1888 status = -EOPNOTSUPP;
1889 break;
1890 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001891 if (task->tk_rebind_retry == 0)
1892 break;
1893 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001894 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001895 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001896 case -EAGAIN:
1897 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001898 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001899 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001900 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001901 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001902 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001903 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001904 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001905 task->tk_pid);
1906 break;
1907 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001908 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001909 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001910 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001911 case -ECONNREFUSED: /* connection problems */
1912 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001913 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001914 case -ENOTCONN:
1915 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001916 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001917 case -EHOSTUNREACH:
1918 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001919 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001920 case -EPIPE:
1921 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1922 task->tk_pid, task->tk_status);
1923 if (!RPC_IS_SOFTCONN(task)) {
1924 rpc_delay(task, 5*HZ);
1925 goto retry_timeout;
1926 }
1927 status = task->tk_status;
1928 break;
Chuck Leverda351872005-08-11 16:25:11 -04001929 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001930 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001931 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001932 }
1933
1934 rpc_exit(task, status);
1935 return;
1936
Trond Myklebustda45828e2006-08-31 15:44:52 -04001937retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001938 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001939 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05001940 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04001941}
1942
1943/*
1944 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
1946static void
1947call_connect(struct rpc_task *task)
1948{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001949 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Trond Myklebust03e51d32019-03-10 11:17:13 -04001951 if (rpc_task_transmitted(task)) {
1952 rpc_task_handle_transmitted(task);
1953 return;
1954 }
1955
Trond Myklebust009a82f2019-03-09 12:07:17 -05001956 if (xprt_connected(xprt)) {
1957 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001958 return;
1959 }
1960
Chuck Lever46121cf2007-01-31 12:14:08 -05001961 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001962 task->tk_pid, xprt,
1963 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Trond Myklebust009a82f2019-03-09 12:07:17 -05001965 task->tk_action = call_connect_status;
1966 if (task->tk_status < 0)
1967 return;
1968 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1969 rpc_exit(task, -ENOTCONN);
1970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001972 if (!xprt_prepare_transmit(task))
1973 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001974 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977/*
Chuck Leverda351872005-08-11 16:25:11 -04001978 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
1980static void
1981call_connect_status(struct rpc_task *task)
1982{
1983 struct rpc_clnt *clnt = task->tk_client;
1984 int status = task->tk_status;
1985
Trond Myklebust03e51d32019-03-10 11:17:13 -04001986 if (rpc_task_transmitted(task)) {
1987 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05001988 return;
1989 }
1990
Chuck Lever46121cf2007-01-31 12:14:08 -05001991 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001992
Chuck Levere671edb2018-03-16 10:33:44 -04001993 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04001994 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001996 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10001997 /* A positive refusal suggests a rebind is needed. */
1998 if (RPC_IS_SOFTCONN(task))
1999 break;
2000 if (clnt->cl_autobind) {
2001 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002002 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002003 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002004 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002005 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002006 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002007 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002008 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002009 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002010 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002011 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002012 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002013 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2014 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002015 if (RPC_IS_SOFTCONN(task))
2016 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002017 /* retry with existing socket, after a delay */
2018 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002019 /* fall through */
Trond Myklebust0445f922018-12-17 13:34:59 -05002020 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002021 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002022 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002023 goto out_retry;
Trond Myklebust561ec162013-09-26 15:22:45 -04002024 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002025 clnt->cl_stats->netreconn++;
2026 task->tk_action = call_transmit;
2027 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002029 rpc_exit(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002030 return;
2031out_retry:
2032 /* Check for timeouts before looping back to call_bind */
2033 task->tk_action = call_bind;
2034 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
2037/*
2038 * 5. Transmit the RPC request, and wait for reply
2039 */
2040static void
2041call_transmit(struct rpc_task *task)
2042{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002043 if (rpc_task_transmitted(task)) {
2044 rpc_task_handle_transmitted(task);
2045 return;
2046 }
2047
Chuck Lever46121cf2007-01-31 12:14:08 -05002048 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Trond Myklebusted7dc972019-03-04 14:19:31 -05002050 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002051 if (!xprt_prepare_transmit(task))
2052 return;
2053 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002054 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002055 if (!xprt_connected(task->tk_xprt)) {
2056 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002057 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002058 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002059 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002060 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002061 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002062}
2063
2064/*
2065 * 5a. Handle cleanup after a transmission
2066 */
2067static void
2068call_transmit_status(struct rpc_task *task)
2069{
2070 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002071
2072 /*
2073 * Common case: success. Force the compiler to put this
2074 * test first.
2075 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002076 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002077 task->tk_status = 0;
2078 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002079 return;
2080 }
2081
Trond Myklebust15f081c2009-03-11 14:37:57 -04002082 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002083 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002084 dprint_status(task);
Trond Myklebust75891f52018-09-03 17:37:36 -04002085 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002086 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002087 task->tk_status = 0;
2088 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002089 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002090 /*
2091 * Special cases: if we've been waiting on the
2092 * socket's write_space() callback, or if the
2093 * socket just returned a connection error,
2094 * then hold onto the transport lock.
2095 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002096 case -ENOBUFS:
2097 rpc_delay(task, HZ>>2);
2098 /* fall through */
Trond Myklebustc5445772018-09-03 23:39:27 -04002099 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002100 case -EAGAIN:
2101 task->tk_action = call_transmit;
2102 task->tk_status = 0;
2103 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002104 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002105 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002106 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002107 case -EHOSTUNREACH:
2108 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002109 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002110 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002111 if (!task->tk_msg.rpc_proc->p_proc)
2112 trace_xprt_ping(task->tk_xprt,
2113 task->tk_status);
Chuck Lever09a21c42009-12-03 15:58:56 -05002114 rpc_exit(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002115 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002116 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002117 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002118 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002119 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002120 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002121 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002122 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002123 task->tk_action = call_bind;
2124 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002125 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002126 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002127 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002130#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002131static void call_bc_transmit(struct rpc_task *task);
2132static void call_bc_transmit_status(struct rpc_task *task);
2133
2134static void
2135call_bc_encode(struct rpc_task *task)
2136{
2137 xprt_request_enqueue_transmit(task);
2138 task->tk_action = call_bc_transmit;
2139}
2140
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002141/*
2142 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2143 * addition, disconnect on connectivity errors.
2144 */
2145static void
2146call_bc_transmit(struct rpc_task *task)
2147{
Trond Myklebust477687e2019-03-05 07:30:48 -05002148 task->tk_action = call_bc_transmit_status;
2149 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2150 if (!xprt_prepare_transmit(task))
2151 return;
2152 task->tk_status = 0;
2153 xprt_transmit(task);
2154 }
2155 xprt_end_transmit(task);
2156}
2157
2158static void
2159call_bc_transmit_status(struct rpc_task *task)
2160{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002161 struct rpc_rqst *req = task->tk_rqstp;
2162
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002163 if (rpc_task_transmitted(task))
2164 task->tk_status = 0;
2165
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002166 dprint_status(task);
Trond Myklebust477687e2019-03-05 07:30:48 -05002167
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002168 switch (task->tk_status) {
2169 case 0:
2170 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002171 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002172 case -EHOSTDOWN:
2173 case -EHOSTUNREACH:
2174 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002175 case -ECONNRESET:
2176 case -ECONNREFUSED:
2177 case -EADDRINUSE:
2178 case -ENOTCONN:
2179 case -EPIPE:
2180 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002181 case -ENOBUFS:
2182 rpc_delay(task, HZ>>2);
2183 /* fall through */
2184 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002185 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002186 task->tk_status = 0;
2187 task->tk_action = call_bc_transmit;
2188 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002189 case -ETIMEDOUT:
2190 /*
2191 * Problem reaching the server. Disconnect and let the
2192 * forechannel reestablish the connection. The server will
2193 * have to retransmit the backchannel request and we'll
2194 * reprocess it. Since these ops are idempotent, there's no
2195 * need to cache our reply at this time.
2196 */
2197 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2198 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002199 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002200 req->rq_connect_cookie);
2201 break;
2202 default:
2203 /*
2204 * We were unable to reply and will have to drop the
2205 * request. The server should reconnect and retransmit.
2206 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002207 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2208 "error: %d\n", task->tk_status);
2209 break;
2210 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002211 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002212}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002213#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215/*
2216 * 6. Sort out the RPC call status
2217 */
2218static void
2219call_status(struct rpc_task *task)
2220{
2221 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int status;
2223
Chuck Levera25a4cb2018-03-16 10:33:55 -04002224 if (!task->tk_msg.rpc_proc->p_proc)
2225 trace_xprt_ping(task->tk_xprt, task->tk_status);
2226
Chuck Lever46121cf2007-01-31 12:14:08 -05002227 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 status = task->tk_status;
2230 if (status >= 0) {
2231 task->tk_action = call_decode;
2232 return;
2233 }
2234
Steve Dickson5753cba2012-02-06 10:08:08 -05002235 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 task->tk_status = 0;
2237 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002238 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002239 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002240 case -EHOSTUNREACH:
2241 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002242 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002243 if (RPC_IS_SOFTCONN(task))
2244 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002245 /*
2246 * Delay any retries for 3 seconds, then handle as if it
2247 * were a timeout.
2248 */
2249 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002250 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 break;
2253 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002254 case -ECONNRESET:
2255 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002256 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002257 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002258 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002259 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002260 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002261 case -EPIPE:
2262 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 break;
2265 case -EIO:
2266 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002267 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002269 if (clnt->cl_chatty)
2270 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002271 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002272 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002274 task->tk_action = call_encode;
2275 rpc_check_timeout(task);
2276 return;
2277out_exit:
2278 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002281static bool
2282rpc_check_connected(const struct rpc_rqst *req)
2283{
2284 /* No allocated request or transport? return true */
2285 if (!req || !req->rq_xprt)
2286 return true;
2287 return xprt_connected(req->rq_xprt);
2288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002291rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 struct rpc_clnt *clnt = task->tk_client;
2294
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002295 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Chuck Lever46121cf2007-01-31 12:14:08 -05002298 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002299 task->tk_timeouts++;
2300
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002301 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Chuck Lever3a28bec2009-12-03 15:58:56 -05002302 rpc_exit(task, -ETIMEDOUT);
2303 return;
2304 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07002307 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002308 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002309 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002310 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002311 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002312 if (task->tk_flags & RPC_TASK_TIMEOUT)
2313 rpc_exit(task, -ETIMEDOUT);
2314 else
2315 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return;
2317 }
2318
Chuck Leverf518e35a2006-01-03 09:55:52 +01002319 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002321 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002322 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002323 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002324 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002327 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002328 /*
2329 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2330 * event? RFC2203 requires the server to drop all such requests.
2331 */
2332 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 * 7. Decode the RPC reply
2337 */
2338static void
2339call_decode(struct rpc_task *task)
2340{
2341 struct rpc_clnt *clnt = task->tk_client;
2342 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002343 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Vasily Averin726fd6a2011-06-01 16:23:59 +04002345 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Chuck Levera0584ee2019-02-11 11:24:58 -05002347 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002348 task->tk_action = rpc_exit_task;
2349 return;
2350 }
2351
Chuck Leverf518e35a2006-01-03 09:55:52 +01002352 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002353 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002354 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002355 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002356 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2359 }
2360
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002361 /*
2362 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002363 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002364 */
2365 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 req->rq_rcv_buf.len = req->rq_private_buf.len;
2367
2368 /* Check that the softirq receive buffer is valid */
2369 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2370 sizeof(req->rq_rcv_buf)) != 0);
2371
Chuck Levera0584ee2019-02-11 11:24:58 -05002372 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2373 req->rq_rcv_buf.head[0].iov_base, req);
2374 switch (rpc_decode_header(task, &xdr)) {
2375 case 0:
2376 task->tk_action = rpc_exit_task;
2377 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2378 dprintk("RPC: %5u %s result %d\n",
2379 task->tk_pid, __func__, task->tk_status);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002380 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002381 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002382 task->tk_status = 0;
2383 /* Note: rpc_decode_header() may have freed the RPC slot */
2384 if (task->tk_rqstp == req) {
2385 xdr_free_bvec(&req->rq_rcv_buf);
2386 req->rq_reply_bytes_recvd = 0;
2387 req->rq_rcv_buf.len = 0;
2388 if (task->tk_client->cl_discrtry)
2389 xprt_conditional_disconnect(req->rq_xprt,
2390 req->rq_connect_cookie);
2391 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002392 task->tk_action = call_encode;
2393 rpc_check_timeout(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Chuck Levere8680a22019-02-11 11:24:48 -05002397static int
2398rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
2400 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002402 __be32 *p;
2403 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Chuck Levere8680a22019-02-11 11:24:48 -05002405 error = -EMSGSIZE;
2406 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2407 if (!p)
2408 goto out_fail;
2409 *p++ = req->rq_xid;
2410 *p++ = rpc_call;
2411 *p++ = cpu_to_be32(RPC_VERSION);
2412 *p++ = cpu_to_be32(clnt->cl_prog);
2413 *p++ = cpu_to_be32(clnt->cl_vers);
2414 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002415
Chuck Levere8680a22019-02-11 11:24:48 -05002416 error = rpcauth_marshcred(task, xdr);
2417 if (error < 0)
2418 goto out_fail;
2419 return 0;
2420out_fail:
2421 trace_rpc_bad_callhdr(task);
2422 rpc_exit(task, error);
2423 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Chuck Levera0584ee2019-02-11 11:24:58 -05002426static noinline int
2427rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002429 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002430 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002431 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Chuck Lever7f5667a2019-02-11 11:24:53 -05002433 /* RFC-1014 says that the representation of XDR data must be a
2434 * multiple of four bytes
2435 * - if it isn't pointer subtraction in the NFS client may give
2436 * undefined results
2437 */
2438 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002439 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Chuck Levera0584ee2019-02-11 11:24:58 -05002441 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2442 if (!p)
2443 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002444 p++; /* skip XID */
2445 if (*p++ != rpc_reply)
2446 goto out_unparsable;
2447 if (*p++ != rpc_msg_accepted)
2448 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002449
Chuck Levera0584ee2019-02-11 11:24:58 -05002450 error = rpcauth_checkverf(task, xdr);
2451 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002452 goto out_verifier;
2453
Chuck Levera0584ee2019-02-11 11:24:58 -05002454 p = xdr_inline_decode(xdr, sizeof(*p));
2455 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002456 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002457 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002458 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002459 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002460 case rpc_prog_unavail:
2461 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002462 error = -EPFNOSUPPORT;
2463 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002464 case rpc_prog_mismatch:
2465 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002466 error = -EPROTONOSUPPORT;
2467 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002468 case rpc_proc_unavail:
2469 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002470 error = -EOPNOTSUPP;
2471 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002472 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002473 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002474 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002475 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002478 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
2480
Trond Myklebustabbcf282006-01-03 09:55:03 +01002481out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002482 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (task->tk_garb_retry) {
2484 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002485 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002486 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488out_err:
2489 rpc_exit(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002490 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002491
Chuck Lever7f5667a2019-02-11 11:24:53 -05002492out_unparsable:
2493 trace_rpc__unparsable(task);
2494 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002495 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002496
2497out_verifier:
2498 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002499 goto out_garbage;
2500
2501out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002502 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002503 p = xdr_inline_decode(xdr, sizeof(*p));
2504 if (!p)
2505 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002506 switch (*p++) {
2507 case rpc_auth_error:
2508 break;
2509 case rpc_mismatch:
2510 trace_rpc__mismatch(task);
2511 error = -EPROTONOSUPPORT;
2512 goto out_err;
2513 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002514 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002515 }
2516
Chuck Levera0584ee2019-02-11 11:24:58 -05002517 p = xdr_inline_decode(xdr, sizeof(*p));
2518 if (!p)
2519 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002520 switch (*p++) {
2521 case rpc_autherr_rejectedcred:
2522 case rpc_autherr_rejectedverf:
2523 case rpcsec_gsserr_credproblem:
2524 case rpcsec_gsserr_ctxproblem:
2525 if (!task->tk_cred_retry)
2526 break;
2527 task->tk_cred_retry--;
2528 trace_rpc__stale_creds(task);
2529 rpcauth_invalcred(task);
2530 /* Ensure we obtain a new XID! */
2531 xprt_release(task);
2532 task->tk_action = call_reserve;
Chuck Levera0584ee2019-02-11 11:24:58 -05002533 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002534 case rpc_autherr_badcred:
2535 case rpc_autherr_badverf:
2536 /* possibly garbled cred/verf? */
2537 if (!task->tk_garb_retry)
2538 break;
2539 task->tk_garb_retry--;
2540 trace_rpc__bad_creds(task);
2541 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002542 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002543 case rpc_autherr_tooweak:
2544 trace_rpc__auth_tooweak(task);
2545 pr_warn("RPC: server %s requires stronger authentication.\n",
2546 task->tk_xprt->servername);
2547 break;
2548 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002549 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002550 }
2551 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002553
Christoph Hellwigc512f362017-05-08 09:31:19 +02002554static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2555 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002556{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002557}
2558
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002559static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2560 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002561{
2562 return 0;
2563}
2564
Christoph Hellwig499b4982017-05-12 15:36:49 +02002565static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002566 .p_encode = rpcproc_encode_null,
2567 .p_decode = rpcproc_decode_null,
2568};
2569
Chuck Levercaabea82009-12-03 15:58:56 -05002570static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002571{
2572 struct rpc_message msg = {
2573 .rpc_proc = &rpcproc_null,
2574 };
2575 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002576 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2577 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002578 return err;
2579}
Trond Myklebust188fef12007-06-16 14:18:40 -04002580
Trond Myklebust7f554892016-01-30 23:43:35 -05002581static
2582struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2583 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2584 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002585{
2586 struct rpc_message msg = {
2587 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002588 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002589 struct rpc_task_setup task_setup_data = {
2590 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002591 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002592 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002593 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002594 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2595 .callback_data = data,
NeilBrowna52458b2018-12-03 11:30:31 +11002596 .flags = flags | RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002597 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002598
Trond Myklebustc970aa82007-07-14 15:39:59 -04002599 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002600}
Trond Myklebust7f554892016-01-30 23:43:35 -05002601
2602struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2603{
2604 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2605}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002606EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002607
Trond Myklebust7f554892016-01-30 23:43:35 -05002608struct rpc_cb_add_xprt_calldata {
2609 struct rpc_xprt_switch *xps;
2610 struct rpc_xprt *xprt;
2611};
2612
2613static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2614{
2615 struct rpc_cb_add_xprt_calldata *data = calldata;
2616
2617 if (task->tk_status == 0)
2618 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2619}
2620
2621static void rpc_cb_add_xprt_release(void *calldata)
2622{
2623 struct rpc_cb_add_xprt_calldata *data = calldata;
2624
2625 xprt_put(data->xprt);
2626 xprt_switch_put(data->xps);
2627 kfree(data);
2628}
2629
Trond Myklebustce272302016-07-24 17:06:28 -04002630static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002631 .rpc_call_done = rpc_cb_add_xprt_done,
2632 .rpc_release = rpc_cb_add_xprt_release,
2633};
2634
2635/**
2636 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2637 * @clnt: pointer to struct rpc_clnt
2638 * @xps: pointer to struct rpc_xprt_switch,
2639 * @xprt: pointer struct rpc_xprt
2640 * @dummy: unused
2641 */
2642int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2643 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2644 void *dummy)
2645{
2646 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002647 struct rpc_task *task;
2648
2649 data = kmalloc(sizeof(*data), GFP_NOFS);
2650 if (!data)
2651 return -ENOMEM;
2652 data->xps = xprt_switch_get(xps);
2653 data->xprt = xprt_get(xprt);
2654
NeilBrowna68a72e2018-12-03 11:30:30 +11002655 task = rpc_call_null_helper(clnt, xprt, NULL,
2656 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
Trond Myklebust7f554892016-01-30 23:43:35 -05002657 &rpc_cb_add_xprt_call_ops, data);
Trond Myklebust7f554892016-01-30 23:43:35 -05002658 if (IS_ERR(task))
2659 return PTR_ERR(task);
2660 rpc_put_task(task);
2661 return 1;
2662}
2663EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2664
2665/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002666 * rpc_clnt_setup_test_and_add_xprt()
2667 *
2668 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2669 * 1) caller of the test function must dereference the rpc_xprt_switch
2670 * and the rpc_xprt.
2671 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2672 * the rpc_call_done routine.
2673 *
2674 * Upon success (return of 1), the test function adds the new
2675 * transport to the rpc_clnt xprt switch
2676 *
2677 * @clnt: struct rpc_clnt to get the new transport
2678 * @xps: the rpc_xprt_switch to hold the new transport
2679 * @xprt: the rpc_xprt to test
2680 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2681 * and test function call data
2682 */
2683int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2684 struct rpc_xprt_switch *xps,
2685 struct rpc_xprt *xprt,
2686 void *data)
2687{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002688 struct rpc_task *task;
2689 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2690 int status = -EADDRINUSE;
2691
2692 xprt = xprt_get(xprt);
2693 xprt_switch_get(xps);
2694
2695 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2696 goto out_err;
2697
2698 /* Test the connection */
NeilBrowna68a72e2018-12-03 11:30:30 +11002699 task = rpc_call_null_helper(clnt, xprt, NULL,
2700 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002701 NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002702 if (IS_ERR(task)) {
2703 status = PTR_ERR(task);
2704 goto out_err;
2705 }
2706 status = task->tk_status;
2707 rpc_put_task(task);
2708
2709 if (status < 0)
2710 goto out_err;
2711
2712 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2713 xtest->add_xprt_test(clnt, xprt, xtest->data);
2714
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302715 xprt_put(xprt);
2716 xprt_switch_put(xps);
2717
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002718 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2719 return 1;
2720out_err:
2721 xprt_put(xprt);
2722 xprt_switch_put(xps);
2723 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2724 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2725 return status;
2726}
2727EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2728
2729/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002730 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2731 * @clnt: pointer to struct rpc_clnt
2732 * @xprtargs: pointer to struct xprt_create
2733 * @setup: callback to test and/or set up the connection
2734 * @data: pointer to setup function data
2735 *
2736 * Creates a new transport using the parameters set in args and
2737 * adds it to clnt.
2738 * If ping is set, then test that connectivity succeeds before
2739 * adding the new transport.
2740 *
2741 */
2742int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2743 struct xprt_create *xprtargs,
2744 int (*setup)(struct rpc_clnt *,
2745 struct rpc_xprt_switch *,
2746 struct rpc_xprt *,
2747 void *),
2748 void *data)
2749{
2750 struct rpc_xprt_switch *xps;
2751 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002752 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002753 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002754 unsigned char resvport;
2755 int ret = 0;
2756
2757 rcu_read_lock();
2758 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2759 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2760 if (xps == NULL || xprt == NULL) {
2761 rcu_read_unlock();
2762 return -EAGAIN;
2763 }
2764 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002765 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002766 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002767 rcu_read_unlock();
2768
2769 xprt = xprt_create_transport(xprtargs);
2770 if (IS_ERR(xprt)) {
2771 ret = PTR_ERR(xprt);
2772 goto out_put_switch;
2773 }
2774 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002775 if (xprt->ops->set_connect_timeout != NULL)
2776 xprt->ops->set_connect_timeout(xprt,
2777 connect_timeout,
2778 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002779
2780 rpc_xprt_switch_set_roundrobin(xps);
2781 if (setup) {
2782 ret = setup(clnt, xps, xprt, data);
2783 if (ret != 0)
2784 goto out_put_xprt;
2785 }
2786 rpc_xprt_switch_add_xprt(xps, xprt);
2787out_put_xprt:
2788 xprt_put(xprt);
2789out_put_switch:
2790 xprt_switch_put(xps);
2791 return ret;
2792}
2793EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2794
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002795struct connect_timeout_data {
2796 unsigned long connect_timeout;
2797 unsigned long reconnect_timeout;
2798};
2799
Trond Myklebust8d480322016-08-05 19:03:31 -04002800static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002801rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002802 struct rpc_xprt *xprt,
2803 void *data)
2804{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002805 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002806
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002807 if (xprt->ops->set_connect_timeout)
2808 xprt->ops->set_connect_timeout(xprt,
2809 timeo->connect_timeout,
2810 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002811 return 0;
2812}
2813
2814void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002815rpc_set_connect_timeout(struct rpc_clnt *clnt,
2816 unsigned long connect_timeout,
2817 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002818{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002819 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002820 .connect_timeout = connect_timeout,
2821 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002822 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002823 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002824 rpc_xprt_set_connect_timeout,
2825 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002826}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002827EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002828
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002829void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2830{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002831 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002832 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002833 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002834}
2835EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2836
Andy Adamsondd691712016-09-09 09:22:24 -04002837void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2838{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002839 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002840 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2841 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002842 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002843}
2844EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2845
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002846bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2847 const struct sockaddr *sap)
2848{
2849 struct rpc_xprt_switch *xps;
2850 bool ret;
2851
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002852 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002853 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002854 ret = rpc_xprt_switch_has_addr(xps, sap);
2855 rcu_read_unlock();
2856 return ret;
2857}
2858EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2859
Jeff Laytonf895b252014-11-17 16:58:04 -05002860#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002861static void rpc_show_header(void)
2862{
Chuck Levercb3997b2008-05-21 17:09:41 -04002863 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2864 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002865}
2866
Chuck Lever38e886e2008-05-21 17:09:33 -04002867static void rpc_show_task(const struct rpc_clnt *clnt,
2868 const struct rpc_task *task)
2869{
2870 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002871
2872 if (RPC_IS_QUEUED(task))
2873 rpc_waitq = rpc_qname(task->tk_waitqueue);
2874
Joe Perchesb3bceda2010-12-21 10:52:24 -05002875 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 -04002876 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04002877 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002878 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002879 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002880}
2881
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002882void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002883{
2884 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002885 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002886 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002887 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002888
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002889 spin_lock(&sn->rpc_client_lock);
2890 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002891 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002892 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002893 if (!header) {
2894 rpc_show_header();
2895 header++;
2896 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002897 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002898 }
2899 spin_unlock(&clnt->cl_lock);
2900 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002901 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002902}
2903#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002904
2905#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002906static int
2907rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2908 struct rpc_xprt *xprt,
2909 void *dummy)
2910{
2911 return xprt_enable_swap(xprt);
2912}
2913
Jeff Layton3c87ef62015-06-03 16:14:25 -04002914int
2915rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2916{
Trond Myklebust15001e52016-01-30 20:05:34 -05002917 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2918 return rpc_clnt_iterate_for_each_xprt(clnt,
2919 rpc_clnt_swap_activate_callback, NULL);
2920 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002921}
2922EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2923
Trond Myklebust15001e52016-01-30 20:05:34 -05002924static int
2925rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2926 struct rpc_xprt *xprt,
2927 void *dummy)
2928{
2929 xprt_disable_swap(xprt);
2930 return 0;
2931}
2932
Jeff Layton3c87ef62015-06-03 16:14:25 -04002933void
2934rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2935{
Trond Myklebust15001e52016-01-30 20:05:34 -05002936 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2937 rpc_clnt_iterate_for_each_xprt(clnt,
2938 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002939}
2940EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2941#endif /* CONFIG_SUNRPC_SWAP */