blob: f03911f849535195a7d2bd03e5cb2fd22656a114 [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);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040069#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040070static void call_bc_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040071#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050073static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void call_refresh(struct rpc_task *task);
75static void call_refreshresult(struct rpc_task *task);
76static void call_timeout(struct rpc_task *task);
77static void call_connect(struct rpc_task *task);
78static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Chuck Leverb0e1c572008-05-21 17:09:19 -040080static __be32 *rpc_encode_header(struct rpc_task *task);
81static __be32 *rpc_verify_header(struct rpc_task *task);
Chuck Levercaabea82009-12-03 15:58:56 -050082static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust64c91a12007-06-23 10:17:16 -040083
Trond Myklebust188fef12007-06-16 14:18:40 -040084static void rpc_register_client(struct rpc_clnt *clnt)
85{
Trond Myklebust2446ab62012-03-01 17:00:56 -050086 struct net *net = rpc_net_ns(clnt);
87 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040088
89 spin_lock(&sn->rpc_client_lock);
90 list_add(&clnt->cl_clients, &sn->all_clients);
91 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040092}
93
94static void rpc_unregister_client(struct rpc_clnt *clnt)
95{
Trond Myklebust2446ab62012-03-01 17:00:56 -050096 struct net *net = rpc_net_ns(clnt);
97 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040098
99 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400100 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +0400101 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400104static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
105{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400106 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400107}
108
109static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
110{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500111 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400113
Trond Myklebust2446ab62012-03-01 17:00:56 -0500114 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400116 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500117 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400118 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400119}
120
121static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400122 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Trond Myklebustf1345852005-09-23 11:08:25 -0400124 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400125 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400126 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400127 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400129 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400130 if (dir == NULL) {
131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400132 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400133 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400134 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400135 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400136 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400137 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400138 if (!IS_ERR(dentry))
139 break;
Al Viroa95e6912013-07-14 16:43:54 +0400140 if (dentry == ERR_PTR(-EEXIST))
141 continue;
142 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
143 " %s/%s, error %ld\n",
144 dir_name, name, PTR_ERR(dentry));
145 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400146 }
147 dput(dir);
148 return dentry;
149}
150
151static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400152rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400153{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400154 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400155
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400156 if (clnt->cl_program->pipe_dir_name != NULL) {
157 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
158 if (IS_ERR(dentry))
159 return PTR_ERR(dentry);
160 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400164static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400165{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400166 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400167 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400168
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400169 switch (event) {
170 case RPC_PIPEFS_MOUNT:
171 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
172 return 1;
173 if (atomic_read(&clnt->cl_count) == 0)
174 return 1;
175 break;
176 case RPC_PIPEFS_UMOUNT:
177 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
178 return 1;
179 break;
180 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400181 return 0;
182}
183
184static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
185 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400186{
187 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400188
189 switch (event) {
190 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400191 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400192 if (!dentry)
193 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400194 if (IS_ERR(dentry))
195 return PTR_ERR(dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400196 break;
197 case RPC_PIPEFS_UMOUNT:
198 __rpc_clnt_remove_pipedir(clnt);
199 break;
200 default:
201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
202 return -ENOTSUPP;
203 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530204 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400205}
206
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400207static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
209{
210 int error = 0;
211
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
216 break;
217 }
218 return error;
219}
220
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400221static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
222{
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
225
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400228 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400229 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400230 spin_unlock(&sn->rpc_client_lock);
231 return clnt;
232 }
233 spin_unlock(&sn->rpc_client_lock);
234 return NULL;
235}
236
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400237static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238 void *ptr)
239{
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
242 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400243
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400245 error = __rpc_pipefs_event(clnt, event, sb);
246 if (error)
247 break;
248 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400249 return error;
250}
251
252static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400255};
256
257int rpc_clients_notifier_register(void)
258{
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
260}
261
262void rpc_clients_notifier_unregister(void)
263{
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265}
266
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400267static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
268 struct rpc_xprt *xprt,
269 const struct rpc_timeout *timeout)
270{
271 struct rpc_xprt *old;
272
273 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400274 old = rcu_dereference_protected(clnt->cl_xprt,
275 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400276
277 if (!xprt_bound(xprt))
278 clnt->cl_autobind = 1;
279
280 clnt->cl_timeout = timeout;
281 rcu_assign_pointer(clnt->cl_xprt, xprt);
282 spin_unlock(&clnt->cl_lock);
283
284 return old;
285}
286
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400287static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
288{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500289 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
290 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400291}
292
Chuck Leverd746e542013-10-17 14:12:17 -0400293static int rpc_client_register(struct rpc_clnt *clnt,
294 rpc_authflavor_t pseudoflavor,
295 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400296{
Trond Myklebustc2190662013-08-26 19:23:04 -0400297 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400298 .pseudoflavor = pseudoflavor,
299 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400300 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400301 struct rpc_auth *auth;
302 struct net *net = rpc_net_ns(clnt);
303 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400304 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400305
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400306 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500307
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400308 pipefs_sb = rpc_get_sb_net(net);
309 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400310 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400311 if (err)
312 goto out;
313 }
314
Trond Myklebusteeee2452013-07-10 15:33:01 -0400315 rpc_register_client(clnt);
316 if (pipefs_sb)
317 rpc_put_sb_net(net);
318
Trond Myklebustc2190662013-08-26 19:23:04 -0400319 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400320 if (IS_ERR(auth)) {
321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400322 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400323 err = PTR_ERR(auth);
324 goto err_auth;
325 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400326 return 0;
327err_auth:
328 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400329 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400330 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400331out:
332 if (pipefs_sb)
333 rpc_put_sb_net(net);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500334 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400335 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400336}
337
Trond Myklebust2f048db2013-09-04 21:51:44 -0400338static DEFINE_IDA(rpc_clids);
339
Kinglong Meec929ea02017-01-20 16:48:39 +0800340void rpc_cleanup_clids(void)
341{
342 ida_destroy(&rpc_clids);
343}
344
Trond Myklebust2f048db2013-09-04 21:51:44 -0400345static int rpc_alloc_clid(struct rpc_clnt *clnt)
346{
347 int clid;
348
349 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
350 if (clid < 0)
351 return clid;
352 clnt->cl_clid = clid;
353 return 0;
354}
355
356static void rpc_free_clid(struct rpc_clnt *clnt)
357{
358 ida_simple_remove(&rpc_clids, clnt->cl_clid);
359}
360
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400361static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500362 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400363 struct rpc_xprt *xprt,
364 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500366 const struct rpc_program *program = args->program;
367 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400368 struct rpc_clnt *clnt = NULL;
369 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500370 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400372
373 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500374 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500375 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Trond Myklebust4ada5392007-06-14 17:26:17 -0400377 err = rpciod_up();
378 if (err)
379 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500380
Trond Myklebustf05c1242013-04-05 14:13:21 -0400381 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500382 if (args->version >= program->nrvers)
383 goto out_err;
384 version = program->version[args->version];
385 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto out_err;
387
388 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700389 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!clnt)
391 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400392 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Trond Myklebust2f048db2013-09-04 21:51:44 -0400394 err = rpc_alloc_clid(clnt);
395 if (err)
396 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300400 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500403 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400404 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500405 err = -ENOMEM;
406 if (clnt->cl_metrics == NULL)
407 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500408 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400409 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400410 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400412 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500413 if (args->timeout != NULL) {
414 memcpy(&clnt->cl_timeout_default, args->timeout,
415 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400416 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500417 }
418
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400419 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500420 xprt_iter_init(&clnt->cl_xpi, xps);
421 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500424 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Trond Myklebust006abe82010-09-12 19:55:25 -0400426 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400427
Trond Myklebust03a9a422015-01-30 18:12:28 -0500428 if (nodename == NULL)
429 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500431 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400432
Chuck Leverd746e542013-10-17 14:12:17 -0400433 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400434 if (err)
435 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400436 if (parent)
437 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return clnt;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500441 rpc_free_iostats(clnt->cl_metrics);
442out_no_stats:
Trond Myklebust2f048db2013-09-04 21:51:44 -0400443 rpc_free_clid(clnt);
444out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(clnt);
446out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400447 rpciod_down();
448out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500449 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400450 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ERR_PTR(err);
452}
453
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400454static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800455 struct rpc_xprt *xprt)
456{
457 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500458 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800459
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400460 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400461 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400462 xps = args->bc_xprt->xpt_bc_xps;
463 xprt_switch_get(xps);
464 } else {
465 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
466 if (xps == NULL) {
467 xprt_put(xprt);
468 return ERR_PTR(-ENOMEM);
469 }
470 if (xprt->bc_xprt) {
471 xprt_switch_get(xps);
472 xprt->bc_xprt->xpt_bc_xps = xps;
473 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400474 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500475 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800476 if (IS_ERR(clnt))
477 return clnt;
478
479 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
480 int err = rpc_ping(clnt);
481 if (err != 0) {
482 rpc_shutdown_client(clnt);
483 return ERR_PTR(err);
484 }
485 }
486
487 clnt->cl_softrtry = 1;
488 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
489 clnt->cl_softrtry = 0;
490
491 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
492 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400493 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
494 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800495 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
496 clnt->cl_discrtry = 1;
497 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
498 clnt->cl_chatty = 1;
499
500 return clnt;
501}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800502
Ben Hutchings2c530402012-07-10 10:55:09 +0000503/**
Chuck Leverc2866762006-08-22 20:06:20 -0400504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
506 *
507 * Creates and initializes an RPC transport and an RPC client.
508 *
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
512 */
513struct rpc_clnt *rpc_create(struct rpc_create_args *args)
514{
515 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400516 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400517 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400518 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200519 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200520 .dstaddr = args->address,
521 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500522 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300523 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200524 };
Chuck Lever510deb02007-12-10 14:56:24 -0500525 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400526
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400527 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400528 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400529 xprt = args->bc_xprt->xpt_bc_xprt;
530 if (xprt) {
531 xprt_get(xprt);
532 return rpc_create_xprt(args, xprt);
533 }
534 }
535
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400536 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
537 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400538 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
539 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400540 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400541 * If the caller chooses not to specify a hostname, whip
542 * up a string representation of the passed-in address.
543 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500544 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400545 struct sockaddr_un *sun =
546 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400547 struct sockaddr_in *sin =
548 (struct sockaddr_in *)args->address;
549 struct sockaddr_in6 *sin6 =
550 (struct sockaddr_in6 *)args->address;
551
Chuck Lever510deb02007-12-10 14:56:24 -0500552 servername[0] = '\0';
553 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400554 case AF_LOCAL:
555 snprintf(servername, sizeof(servername), "%s",
556 sun->sun_path);
557 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400558 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700559 snprintf(servername, sizeof(servername), "%pI4",
560 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500561 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400562 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700563 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400564 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500565 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500566 default:
567 /* caller wants default server name, but
568 * address family isn't recognized. */
569 return ERR_PTR(-EINVAL);
570 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500571 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400572 }
573
Chuck Lever510deb02007-12-10 14:56:24 -0500574 xprt = xprt_create_transport(&xprtargs);
575 if (IS_ERR(xprt))
576 return (struct rpc_clnt *)xprt;
577
Chuck Lever43780b82007-07-01 12:13:22 -0400578 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400579 * By default, kernel RPC client connects from a reserved port.
580 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
581 * but it is always enabled for rpciod, which handles the connect
582 * operation.
583 */
584 xprt->resvport = 1;
585 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
586 xprt->resvport = 0;
587
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800588 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400589}
Chuck Leverb86acd52006-08-22 20:06:22 -0400590EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * This function clones the RPC client structure. It allows us to share the
594 * same transport while varying parameters such as the authentication
595 * flavour.
596 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400597static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
598 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500600 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500601 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400602 struct rpc_clnt *new;
603 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Chuck Lever1b63a752012-09-14 17:23:52 -0400605 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500606 rcu_read_lock();
607 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500608 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500609 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500610 if (xprt == NULL || xps == NULL) {
611 xprt_put(xprt);
612 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400613 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500614 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400615 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500616 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400617
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500618 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400619 if (IS_ERR(new)) {
620 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400621 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400622 }
623
Chuck Lever1b63a752012-09-14 17:23:52 -0400624 /* Turn off autobind on clones */
625 new->cl_autobind = 0;
626 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400627 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400628 new->cl_discrtry = clnt->cl_discrtry;
629 new->cl_chatty = clnt->cl_chatty;
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);
830 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
831 if (!RPC_IS_ACTIVATED(rovr))
832 continue;
833 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
834 rovr->tk_flags |= RPC_TASK_KILLED;
835 rpc_exit(rovr, -EIO);
Stanislav Kinsbursky8e26de22011-03-17 18:54:23 +0300836 if (RPC_IS_QUEUED(rovr))
837 rpc_wake_up_queued_task(rovr->tk_waitqueue,
838 rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400839 }
840 }
841 spin_unlock(&clnt->cl_lock);
842}
843EXPORT_SYMBOL_GPL(rpc_killall_tasks);
844
845/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400847 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400849void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400851 might_sleep();
852
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500853 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400854 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500855 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Trond Myklebust34f52e32007-06-14 16:40:31 -0400857 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800859 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400860 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Trond Myklebust4c402b42007-06-14 16:40:32 -0400863 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400865EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400868 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500870static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400871rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500873 struct rpc_clnt *parent = NULL;
874
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500875 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400876 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500877 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500878 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500879 parent = clnt->cl_parent;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500880 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400881 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400882 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500883 rpc_free_iostats(clnt->cl_metrics);
884 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500885 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500886 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400887 rpciod_down();
Trond Myklebust2f048db2013-09-04 21:51:44 -0400888 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500890 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400894 * Free an RPC client
895 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700896static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400897rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400898{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500899 if (clnt->cl_auth == NULL)
900 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400901
902 /*
903 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
904 * release remaining GSS contexts. This mechanism ensures
905 * that it can do so safely.
906 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400907 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400908 rpcauth_release(clnt->cl_auth);
909 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400910 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500911 return rpc_free_client(clnt);
912 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400913}
914
915/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400916 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
918void
919rpc_release_client(struct rpc_clnt *clnt)
920{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400921 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Trond Myklebustd07ba842013-11-12 17:24:36 -0500923 do {
924 if (list_empty(&clnt->cl_tasks))
925 wake_up(&destroy_wait);
926 if (!atomic_dec_and_test(&clnt->cl_count))
927 break;
928 clnt = rpc_free_auth(clnt);
929 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400930}
Simo Sorce1d658332012-05-25 18:09:55 -0400931EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400932
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000933/**
934 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800935 * @old: old rpc_client
936 * @program: rpc program to set
937 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000938 *
939 * Clones the rpc client and sets up a new RPC program. This is mainly
940 * of use for enabling different RPC programs to share the same transport.
941 * The Sun NFSv2/v3 ACL protocol can do this.
942 */
943struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500944 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400945 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000946{
Trond Myklebustf994c432012-11-01 12:14:14 -0400947 struct rpc_create_args args = {
948 .program = program,
949 .prognumber = program->number,
950 .version = vers,
951 .authflavor = old->cl_auth->au_flavor,
Trond Myklebustf994c432012-11-01 12:14:14 -0400952 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000953 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000954 int err;
955
Trond Myklebustf994c432012-11-01 12:14:14 -0400956 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000957 if (IS_ERR(clnt))
958 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500959 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000960 if (err != 0) {
961 rpc_shutdown_client(clnt);
962 clnt = ERR_PTR(err);
963 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800964out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000965 return clnt;
966}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400967EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000968
Bill Baker0f90be12018-06-19 16:24:58 -0500969void rpc_task_release_transport(struct rpc_task *task)
970{
971 struct rpc_xprt *xprt = task->tk_xprt;
972
973 if (xprt) {
974 task->tk_xprt = NULL;
975 xprt_put(xprt);
976 }
977}
978EXPORT_SYMBOL_GPL(rpc_task_release_transport);
979
Trond Myklebust58f96122010-07-31 14:29:08 -0400980void rpc_task_release_client(struct rpc_task *task)
981{
982 struct rpc_clnt *clnt = task->tk_client;
983
984 if (clnt != NULL) {
985 /* Remove from client task list */
986 spin_lock(&clnt->cl_lock);
987 list_del(&task->tk_task);
988 spin_unlock(&clnt->cl_lock);
989 task->tk_client = NULL;
990
991 rpc_release_client(clnt);
992 }
Bill Baker0f90be12018-06-19 16:24:58 -0500993 rpc_task_release_transport(task);
994}
Trond Myklebustfb43d172016-01-30 16:39:26 -0500995
Bill Baker0f90be12018-06-19 16:24:58 -0500996static
997void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
998{
999 if (!task->tk_xprt)
1000 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -04001001}
1002
1003static
1004void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1005{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001006
Trond Myklebust58f96122010-07-31 14:29:08 -04001007 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001008 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001009 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001010 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001011 if (clnt->cl_softrtry)
1012 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001013 if (clnt->cl_noretranstimeo)
1014 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001015 if (atomic_read(&clnt->cl_swapper))
1016 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001017 /* Add to the client's list of all tasks */
1018 spin_lock(&clnt->cl_lock);
1019 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1020 spin_unlock(&clnt->cl_lock);
1021 }
1022}
1023
1024static void
1025rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1026{
1027 if (msg != NULL) {
1028 task->tk_msg.rpc_proc = msg->rpc_proc;
1029 task->tk_msg.rpc_argp = msg->rpc_argp;
1030 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001031 if (msg->rpc_cred != NULL)
1032 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001033 }
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/*
1037 * Default callback for async RPC calls
1038 */
1039static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001040rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042}
1043
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001044static const struct rpc_call_ops rpc_default_ops = {
1045 .rpc_call_done = rpc_default_callback,
1046};
1047
Trond Myklebustc970aa82007-07-14 15:39:59 -04001048/**
1049 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1050 * @task_setup_data: pointer to task initialisation data
1051 */
1052struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001053{
Trond Myklebust19445b92010-04-16 16:41:10 -04001054 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001055
Trond Myklebust84115e12007-07-14 15:39:59 -04001056 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001057
Trond Myklebust58f96122010-07-31 14:29:08 -04001058 rpc_task_set_client(task, task_setup_data->rpc_client);
1059 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1060
Trond Myklebust58f96122010-07-31 14:29:08 -04001061 if (task->tk_action == NULL)
1062 rpc_call_start(task);
1063
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001064 atomic_inc(&task->tk_count);
1065 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001066 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001067}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001068EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001069
1070/**
1071 * rpc_call_sync - Perform a synchronous RPC call
1072 * @clnt: pointer to RPC client
1073 * @msg: RPC call parameters
1074 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001076int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001079 struct rpc_task_setup task_setup_data = {
1080 .rpc_client = clnt,
1081 .rpc_message = msg,
1082 .callback_ops = &rpc_default_ops,
1083 .flags = flags,
1084 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001085 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001087 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1088 if (flags & RPC_TASK_ASYNC) {
1089 rpc_release_calldata(task_setup_data.callback_ops,
1090 task_setup_data.callback_data);
1091 return -EINVAL;
1092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Trond Myklebustc970aa82007-07-14 15:39:59 -04001094 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001095 if (IS_ERR(task))
1096 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001097 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001098 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return status;
1100}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001101EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001103/**
1104 * rpc_call_async - Perform an asynchronous RPC call
1105 * @clnt: pointer to RPC client
1106 * @msg: RPC call parameters
1107 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001108 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001109 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001112rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001113 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001116 struct rpc_task_setup task_setup_data = {
1117 .rpc_client = clnt,
1118 .rpc_message = msg,
1119 .callback_ops = tk_ops,
1120 .callback_data = data,
1121 .flags = flags|RPC_TASK_ASYNC,
1122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Trond Myklebustc970aa82007-07-14 15:39:59 -04001124 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001125 if (IS_ERR(task))
1126 return PTR_ERR(task);
1127 rpc_put_task(task);
1128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001130EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001132#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001133/**
1134 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1135 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001136 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001137 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001138struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001139{
1140 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001141 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001142 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001143 .flags = RPC_TASK_SOFTCONN |
1144 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001145 };
1146
1147 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1148 /*
1149 * Create an rpc_task to send the data
1150 */
1151 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001152 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001153
1154 task->tk_action = call_bc_transmit;
1155 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001156 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001157 rpc_execute(task);
1158
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001159 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1160 return task;
1161}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001162#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001165rpc_call_start(struct rpc_task *task)
1166{
1167 task->tk_action = call_start;
1168}
1169EXPORT_SYMBOL_GPL(rpc_call_start);
1170
Chuck Levered394402006-08-22 20:06:17 -04001171/**
1172 * rpc_peeraddr - extract remote peer address from clnt's xprt
1173 * @clnt: RPC client structure
1174 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001175 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001176 *
1177 * Returns the number of bytes that are actually in the stored address.
1178 */
1179size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1180{
1181 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001182 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001183
Trond Myklebust2446ab62012-03-01 17:00:56 -05001184 rcu_read_lock();
1185 xprt = rcu_dereference(clnt->cl_xprt);
1186
1187 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001188 if (bytes > bufsize)
1189 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001190 memcpy(buf, &xprt->addr, bytes);
1191 rcu_read_unlock();
1192
1193 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001194}
Chuck Leverb86acd52006-08-22 20:06:22 -04001195EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001196
Chuck Leverf425eba2006-08-22 20:06:18 -04001197/**
1198 * rpc_peeraddr2str - return remote peer address in printable format
1199 * @clnt: RPC client structure
1200 * @format: address format
1201 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001202 * NB: the lifetime of the memory referenced by the returned pointer is
1203 * the same as the rpc_xprt itself. As long as the caller uses this
1204 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001205 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001206const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1207 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001208{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001209 struct rpc_xprt *xprt;
1210
1211 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001212
1213 if (xprt->address_strings[format] != NULL)
1214 return xprt->address_strings[format];
1215 else
1216 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001217}
Chuck Leverb86acd52006-08-22 20:06:22 -04001218EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001219
Chuck Lever2e738fd2012-03-01 17:01:14 -05001220static const struct sockaddr_in rpc_inaddr_loopback = {
1221 .sin_family = AF_INET,
1222 .sin_addr.s_addr = htonl(INADDR_ANY),
1223};
1224
1225static const struct sockaddr_in6 rpc_in6addr_loopback = {
1226 .sin6_family = AF_INET6,
1227 .sin6_addr = IN6ADDR_ANY_INIT,
1228};
1229
1230/*
1231 * Try a getsockname() on a connected datagram socket. Using a
1232 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1233 * This conserves the ephemeral port number space.
1234 *
1235 * Returns zero and fills in "buf" if successful; otherwise, a
1236 * negative errno is returned.
1237 */
1238static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001239 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001240{
1241 struct socket *sock;
1242 int err;
1243
1244 err = __sock_create(net, sap->sa_family,
1245 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1246 if (err < 0) {
1247 dprintk("RPC: can't create UDP socket (%d)\n", err);
1248 goto out;
1249 }
1250
1251 switch (sap->sa_family) {
1252 case AF_INET:
1253 err = kernel_bind(sock,
1254 (struct sockaddr *)&rpc_inaddr_loopback,
1255 sizeof(rpc_inaddr_loopback));
1256 break;
1257 case AF_INET6:
1258 err = kernel_bind(sock,
1259 (struct sockaddr *)&rpc_in6addr_loopback,
1260 sizeof(rpc_in6addr_loopback));
1261 break;
1262 default:
1263 err = -EAFNOSUPPORT;
1264 goto out;
1265 }
1266 if (err < 0) {
1267 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1268 goto out_release;
1269 }
1270
1271 err = kernel_connect(sock, sap, salen, 0);
1272 if (err < 0) {
1273 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1274 goto out_release;
1275 }
1276
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001277 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001278 if (err < 0) {
1279 dprintk("RPC: getsockname failed (%d)\n", err);
1280 goto out_release;
1281 }
1282
1283 err = 0;
1284 if (buf->sa_family == AF_INET6) {
1285 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1286 sin6->sin6_scope_id = 0;
1287 }
1288 dprintk("RPC: %s succeeded\n", __func__);
1289
1290out_release:
1291 sock_release(sock);
1292out:
1293 return err;
1294}
1295
1296/*
1297 * Scraping a connected socket failed, so we don't have a useable
1298 * local address. Fallback: generate an address that will prevent
1299 * the server from calling us back.
1300 *
1301 * Returns zero and fills in "buf" if successful; otherwise, a
1302 * negative errno is returned.
1303 */
1304static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1305{
1306 switch (family) {
1307 case AF_INET:
1308 if (buflen < sizeof(rpc_inaddr_loopback))
1309 return -EINVAL;
1310 memcpy(buf, &rpc_inaddr_loopback,
1311 sizeof(rpc_inaddr_loopback));
1312 break;
1313 case AF_INET6:
1314 if (buflen < sizeof(rpc_in6addr_loopback))
1315 return -EINVAL;
1316 memcpy(buf, &rpc_in6addr_loopback,
1317 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001318 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001319 default:
1320 dprintk("RPC: %s: address family not supported\n",
1321 __func__);
1322 return -EAFNOSUPPORT;
1323 }
1324 dprintk("RPC: %s: succeeded\n", __func__);
1325 return 0;
1326}
1327
1328/**
1329 * rpc_localaddr - discover local endpoint address for an RPC client
1330 * @clnt: RPC client structure
1331 * @buf: target buffer
1332 * @buflen: size of target buffer, in bytes
1333 *
1334 * Returns zero and fills in "buf" and "buflen" if successful;
1335 * otherwise, a negative errno is returned.
1336 *
1337 * This works even if the underlying transport is not currently connected,
1338 * or if the upper layer never previously provided a source address.
1339 *
1340 * The result of this function call is transient: multiple calls in
1341 * succession may give different results, depending on how local
1342 * networking configuration changes over time.
1343 */
1344int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1345{
1346 struct sockaddr_storage address;
1347 struct sockaddr *sap = (struct sockaddr *)&address;
1348 struct rpc_xprt *xprt;
1349 struct net *net;
1350 size_t salen;
1351 int err;
1352
1353 rcu_read_lock();
1354 xprt = rcu_dereference(clnt->cl_xprt);
1355 salen = xprt->addrlen;
1356 memcpy(sap, &xprt->addr, salen);
1357 net = get_net(xprt->xprt_net);
1358 rcu_read_unlock();
1359
1360 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001361 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001362 put_net(net);
1363 if (err != 0)
1364 /* Couldn't discover local address, return ANYADDR */
1365 return rpc_anyaddr(sap->sa_family, buf, buflen);
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(rpc_localaddr);
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370void
1371rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1372{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001373 struct rpc_xprt *xprt;
1374
1375 rcu_read_lock();
1376 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001377 if (xprt->ops->set_buffer_size)
1378 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001379 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001381EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Trond Myklebust2446ab62012-03-01 17:00:56 -05001383/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001384 * rpc_net_ns - Get the network namespace for this RPC client
1385 * @clnt: RPC client to query
1386 *
1387 */
1388struct net *rpc_net_ns(struct rpc_clnt *clnt)
1389{
1390 struct net *ret;
1391
1392 rcu_read_lock();
1393 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1394 rcu_read_unlock();
1395 return ret;
1396}
1397EXPORT_SYMBOL_GPL(rpc_net_ns);
1398
1399/**
1400 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1401 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
1403 * For stream transports, this is one RPC record fragment (see RFC
1404 * 1831), as we don't support multi-record requests yet. For datagram
1405 * transports, this is the size of an IP packet minus the IP, UDP, and
1406 * RPC header sizes.
1407 */
1408size_t rpc_max_payload(struct rpc_clnt *clnt)
1409{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001410 size_t ret;
1411
1412 rcu_read_lock();
1413 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1414 rcu_read_unlock();
1415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
Chuck Leverb86acd52006-08-22 20:06:22 -04001417EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Chuck Lever35f5a422006-01-03 09:55:50 +01001419/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001420 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1421 * @clnt: RPC client to query
1422 */
1423size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1424{
1425 struct rpc_xprt *xprt;
1426 size_t ret;
1427
1428 rcu_read_lock();
1429 xprt = rcu_dereference(clnt->cl_xprt);
1430 ret = xprt->ops->bc_maxpayload(xprt);
1431 rcu_read_unlock();
1432 return ret;
1433}
1434EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1435
1436/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001437 * rpc_force_rebind - force transport to check that remote port is unchanged
1438 * @clnt: client to rebind
1439 *
1440 */
1441void rpc_force_rebind(struct rpc_clnt *clnt)
1442{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001443 if (clnt->cl_autobind) {
1444 rcu_read_lock();
1445 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1446 rcu_read_unlock();
1447 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001448}
Chuck Leverb86acd52006-08-22 20:06:22 -04001449EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001452 * Restart an (async) RPC call from the call_prepare state.
1453 * Usually called from within the exit handler.
1454 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001455int
Andy Adamsonaae20062009-04-01 09:22:40 -04001456rpc_restart_call_prepare(struct rpc_task *task)
1457{
1458 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001459 return 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001460 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001461 task->tk_status = 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001462 if (task->tk_ops->rpc_call_prepare != NULL)
1463 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001464 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001465}
1466EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1467
1468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * Restart an (async) RPC call. Usually called from within the
1470 * exit handler.
1471 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001472int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473rpc_restart_call(struct rpc_task *task)
1474{
1475 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001478 task->tk_status = 0;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001479 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001481EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001483const char
1484*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001485{
1486 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1487
1488 if (proc) {
1489 if (proc->p_name)
1490 return proc->p_name;
1491 else
1492 return "NULL";
1493 } else
1494 return "no proc";
1495}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497/*
1498 * 0. Initial state
1499 *
1500 * Other FSM states can be visited zero or more times, but
1501 * this state is visited exactly once for each RPC.
1502 */
1503static void
1504call_start(struct rpc_task *task)
1505{
1506 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001507 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Chuck Leverc435da62017-11-03 13:46:14 -04001509 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001510 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001511 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001512 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001513 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001515 /* Increment call count (version might not be valid for ping) */
1516 if (clnt->cl_program->version[clnt->cl_vers])
1517 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 clnt->cl_stats->rpccnt++;
1519 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001520 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523/*
1524 * 1. Reserve an RPC call slot
1525 */
1526static void
1527call_reserve(struct rpc_task *task)
1528{
Chuck Lever46121cf2007-01-31 12:14:08 -05001529 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 task->tk_status = 0;
1532 task->tk_action = call_reserveresult;
1533 xprt_reserve(task);
1534}
1535
Trond Myklebustba60eb22013-04-14 10:49:37 -04001536static void call_retry_reserve(struct rpc_task *task);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
1539 * 1b. Grok the result of xprt_reserve()
1540 */
1541static void
1542call_reserveresult(struct rpc_task *task)
1543{
1544 int status = task->tk_status;
1545
Chuck Lever46121cf2007-01-31 12:14:08 -05001546 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /*
1549 * After a call to xprt_reserve(), we must have either
1550 * a request slot or else an error status.
1551 */
1552 task->tk_status = 0;
1553 if (status >= 0) {
1554 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001555 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return;
1557 }
1558
1559 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001560 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 rpc_exit(task, -EIO);
1562 return;
1563 }
1564
1565 /*
1566 * Even though there was an error, we may have acquired
1567 * a request slot somehow. Make sure not to leak it.
1568 */
1569 if (task->tk_rqstp) {
1570 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001571 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 xprt_release(task);
1573 }
1574
1575 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001576 case -ENOMEM:
1577 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001578 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001580 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return;
1582 case -EIO: /* probably a shutdown */
1583 break;
1584 default:
1585 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001586 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 }
1589 rpc_exit(task, status);
1590}
1591
1592/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001593 * 1c. Retry reserving an RPC call slot
1594 */
1595static void
1596call_retry_reserve(struct rpc_task *task)
1597{
1598 dprint_status(task);
1599
1600 task->tk_status = 0;
1601 task->tk_action = call_reserveresult;
1602 xprt_retry_reserve(task);
1603}
1604
1605/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001606 * 2. Bind and/or refresh the credentials
1607 */
1608static void
1609call_refresh(struct rpc_task *task)
1610{
1611 dprint_status(task);
1612
1613 task->tk_action = call_refreshresult;
1614 task->tk_status = 0;
1615 task->tk_client->cl_stats->rpcauthrefresh++;
1616 rpcauth_refreshcred(task);
1617}
1618
1619/*
1620 * 2a. Process the results of a credential refresh
1621 */
1622static void
1623call_refreshresult(struct rpc_task *task)
1624{
1625 int status = task->tk_status;
1626
1627 dprint_status(task);
1628
1629 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001630 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001631 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001632 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001633 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001634 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001635 return;
1636 }
1637 /* Use rate-limiting and a max number of retries if refresh
1638 * had status 0 but failed to update the cred.
1639 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001640 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001641 case -ETIMEDOUT:
1642 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001643 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001644 case -EAGAIN:
1645 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001646 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001647 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001648 if (!task->tk_cred_retry)
1649 break;
1650 task->tk_cred_retry--;
1651 dprintk("RPC: %5u %s: retry refresh creds\n",
1652 task->tk_pid, __func__);
1653 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001654 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001655 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1656 task->tk_pid, __func__, status);
1657 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001658}
1659
1660/*
1661 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001662 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
1664static void
1665call_allocate(struct rpc_task *task)
1666{
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001667 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +01001668 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001669 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001670 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001671 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Chuck Lever46121cf2007-01-31 12:14:08 -05001673 dprint_status(task);
1674
Chuck Lever2bea90d2007-03-29 16:47:53 -04001675 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001676 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001677
Chuck Lever02107142006-01-03 09:55:49 +01001678 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return;
1680
Chuck Lever2bea90d2007-03-29 16:47:53 -04001681 if (proc->p_proc != 0) {
1682 BUG_ON(proc->p_arglen == 0);
1683 if (proc->p_decode != NULL)
1684 BUG_ON(proc->p_replen == 0);
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Chuck Lever2bea90d2007-03-29 16:47:53 -04001687 /*
1688 * Calculate the size (in quads) of the RPC call
1689 * and reply headers, and convert both values
1690 * to byte sizes.
1691 */
1692 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1693 req->rq_callsize <<= 2;
1694 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1695 req->rq_rcvsize <<= 2;
1696
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001697 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001698 xprt_inject_disconnect(xprt);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001699 if (status == 0)
1700 return;
1701 if (status != -ENOMEM) {
1702 rpc_exit(task, status);
1703 return;
1704 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001705
1706 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Trond Myklebust5afa91332011-06-17 10:14:59 -04001708 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001709 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 rpc_delay(task, HZ>>4);
1711 return;
1712 }
1713
1714 rpc_exit(task, -ERESTARTSYS);
1715}
1716
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001717static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001718rpc_task_need_encode(struct rpc_task *task)
1719{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001720 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1721 (!(task->tk_flags & RPC_TASK_SENT) ||
1722 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1723 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001727rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever9f06c712010-12-14 14:59:18 +00001730 kxdreproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001731 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Chuck Lever46121cf2007-01-31 12:14:08 -05001733 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001735 xdr_buf_init(&req->rq_snd_buf,
1736 req->rq_buffer,
1737 req->rq_callsize);
1738 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001739 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001740 req->rq_rcvsize);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001741 req->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Chuck Leverb0e1c572008-05-21 17:09:19 -04001743 p = rpc_encode_header(task);
1744 if (p == NULL) {
1745 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 rpc_exit(task, -EIO);
1747 return;
1748 }
Chuck Leverb0e1c572008-05-21 17:09:19 -04001749
1750 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -04001751 if (encode == NULL)
1752 return;
1753
1754 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1755 task->tk_msg.rpc_argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001759 * 3. Encode arguments of an RPC call
1760 */
1761static void
1762call_encode(struct rpc_task *task)
1763{
1764 if (!rpc_task_need_encode(task))
1765 goto out;
1766 /* Encode here so that rpcsec_gss can use correct sequence number. */
1767 rpc_xdr_encode(task);
1768 /* Did the encode result in an error condition? */
1769 if (task->tk_status != 0) {
1770 /* Was the error nonfatal? */
1771 if (task->tk_status == -EAGAIN)
1772 rpc_delay(task, HZ >> 4);
1773 else
1774 rpc_exit(task, task->tk_status);
1775 return;
1776 }
1777
1778 /* Add task to reply queue before transmission to avoid races */
1779 if (rpc_reply_expected(task))
1780 xprt_request_enqueue_receive(task);
1781 xprt_request_enqueue_transmit(task);
1782out:
1783 task->tk_action = call_bind;
1784}
1785
1786/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * 4. Get the server port number if not yet set
1788 */
1789static void
1790call_bind(struct rpc_task *task)
1791{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001792 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Chuck Lever46121cf2007-01-31 12:14:08 -05001794 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Chuck Leverda351872005-08-11 16:25:11 -04001796 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -04001797 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001798 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -04001799 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001800 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802}
1803
1804/*
Chuck Leverda351872005-08-11 16:25:11 -04001805 * 4a. Sort out bind result
1806 */
1807static void
1808call_bind_status(struct rpc_task *task)
1809{
Chuck Lever906462a2007-09-11 18:00:47 -04001810 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001811
1812 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001813 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001814 task->tk_status = 0;
1815 task->tk_action = call_connect;
1816 return;
1817 }
1818
Steve Dickson5753cba2012-02-06 10:08:08 -05001819 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001820 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001821 case -ENOMEM:
1822 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1823 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001824 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001825 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001826 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1827 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001828 /* fail immediately if this is an RPC ping */
1829 if (task->tk_msg.rpc_proc->p_proc == 0) {
1830 status = -EOPNOTSUPP;
1831 break;
1832 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001833 if (task->tk_rebind_retry == 0)
1834 break;
1835 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001836 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001837 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001838 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001839 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001840 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001841 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001842 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001843 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001844 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001845 task->tk_pid);
1846 break;
1847 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001848 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001849 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001850 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001851 case -ECONNREFUSED: /* connection problems */
1852 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001853 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001854 case -ENOTCONN:
1855 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001856 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001857 case -EHOSTUNREACH:
1858 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001859 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001860 case -EPIPE:
1861 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1862 task->tk_pid, task->tk_status);
1863 if (!RPC_IS_SOFTCONN(task)) {
1864 rpc_delay(task, 5*HZ);
1865 goto retry_timeout;
1866 }
1867 status = task->tk_status;
1868 break;
Chuck Leverda351872005-08-11 16:25:11 -04001869 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001870 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001871 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001872 }
1873
1874 rpc_exit(task, status);
1875 return;
1876
Trond Myklebustda45828e2006-08-31 15:44:52 -04001877retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001878 task->tk_status = 0;
Trond Myklebustda45828e2006-08-31 15:44:52 -04001879 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001880}
1881
1882/*
1883 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
1885static void
1886call_connect(struct rpc_task *task)
1887{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001888 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Chuck Lever46121cf2007-01-31 12:14:08 -05001890 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001891 task->tk_pid, xprt,
1892 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Chuck Leverda351872005-08-11 16:25:11 -04001894 task->tk_action = call_transmit;
1895 if (!xprt_connected(xprt)) {
1896 task->tk_action = call_connect_status;
1897 if (task->tk_status < 0)
1898 return;
Trond Myklebust786615b2013-08-05 16:04:47 -04001899 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1900 rpc_exit(task, -ENOTCONN);
1901 return;
1902 }
Chuck Leverda351872005-08-11 16:25:11 -04001903 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
1907/*
Chuck Leverda351872005-08-11 16:25:11 -04001908 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
1910static void
1911call_connect_status(struct rpc_task *task)
1912{
1913 struct rpc_clnt *clnt = task->tk_client;
1914 int status = task->tk_status;
1915
Chuck Lever46121cf2007-01-31 12:14:08 -05001916 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001917
Chuck Levere671edb2018-03-16 10:33:44 -04001918 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04001919 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001921 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10001922 /* A positive refusal suggests a rebind is needed. */
1923 if (RPC_IS_SOFTCONN(task))
1924 break;
1925 if (clnt->cl_autobind) {
1926 rpc_force_rebind(clnt);
1927 task->tk_action = call_bind;
1928 return;
1929 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001930 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001931 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001932 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001933 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001934 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05001935 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05001936 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001937 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04001938 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11001939 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
1940 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001941 if (RPC_IS_SOFTCONN(task))
1942 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04001943 /* retry with existing socket, after a delay */
1944 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001945 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001946 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04001947 /* Check for timeouts before looping back to call_bind */
1948 case -ETIMEDOUT:
1949 task->tk_action = call_timeout;
Trond Myklebust561ec162013-09-26 15:22:45 -04001950 return;
1951 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001952 clnt->cl_stats->netreconn++;
1953 task->tk_action = call_transmit;
1954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001956 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
1959/*
1960 * 5. Transmit the RPC request, and wait for reply
1961 */
1962static void
1963call_transmit(struct rpc_task *task)
1964{
Chuck Lever46121cf2007-01-31 12:14:08 -05001965 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Trond Myklebust78b576c2018-08-28 09:20:10 -04001967 task->tk_action = call_transmit_status;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001968 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1969 return;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001970
Trond Myklebust3a038182018-08-14 13:58:34 -04001971 if (!xprt_prepare_transmit(task))
1972 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 xprt_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001974}
1975
1976/*
1977 * 5a. Handle cleanup after a transmission
1978 */
1979static void
1980call_transmit_status(struct rpc_task *task)
1981{
1982 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05001983
1984 /*
1985 * Common case: success. Force the compiler to put this
1986 * test first.
1987 */
1988 if (task->tk_status == 0) {
1989 xprt_end_transmit(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001990 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05001991 return;
1992 }
1993
Trond Myklebust15f081c2009-03-11 14:37:57 -04001994 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04001995 default:
Chuck Lever206a1342009-12-03 15:58:56 -05001996 dprint_status(task);
Trond Myklebust15f081c2009-03-11 14:37:57 -04001997 xprt_end_transmit(task);
Chuck Lever09a21c42009-12-03 15:58:56 -05001998 break;
Trond Myklebust75891f52018-09-03 17:37:36 -04001999 case -EBADSLT:
2000 xprt_end_transmit(task);
2001 task->tk_action = call_transmit;
2002 task->tk_status = 0;
2003 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002004 case -EBADMSG:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002005 xprt_end_transmit(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04002006 task->tk_status = 0;
2007 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002008 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002009 /*
2010 * Special cases: if we've been waiting on the
2011 * socket's write_space() callback, or if the
2012 * socket just returned a connection error,
2013 * then hold onto the transport lock.
2014 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002015 case -ENOBUFS:
2016 rpc_delay(task, HZ>>2);
2017 /* fall through */
2018 case -EAGAIN:
2019 task->tk_action = call_transmit;
2020 task->tk_status = 0;
2021 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002022 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002023 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002024 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002025 case -EHOSTUNREACH:
2026 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002027 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002028 if (RPC_IS_SOFTCONN(task)) {
2029 xprt_end_transmit(task);
Chuck Levera25a4cb2018-03-16 10:33:55 -04002030 if (!task->tk_msg.rpc_proc->p_proc)
2031 trace_xprt_ping(task->tk_xprt,
2032 task->tk_status);
Chuck Lever09a21c42009-12-03 15:58:56 -05002033 rpc_exit(task, task->tk_status);
2034 break;
2035 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002036 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002037 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002038 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002039 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002040 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002041 case -EPIPE:
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002042 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002046#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002047/*
2048 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2049 * addition, disconnect on connectivity errors.
2050 */
2051static void
2052call_bc_transmit(struct rpc_task *task)
2053{
2054 struct rpc_rqst *req = task->tk_rqstp;
2055
Trond Myklebust762e4e62018-08-24 16:28:28 -04002056 if (rpc_task_need_encode(task))
2057 xprt_request_enqueue_transmit(task);
Trond Myklebust902c5882018-09-01 17:21:01 -04002058 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
2059 goto out_wakeup;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002060
Trond Myklebust1193d582015-06-02 11:53:21 -04002061 if (!xprt_prepare_transmit(task))
2062 goto out_retry;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002063
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002064 if (task->tk_status < 0) {
2065 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2066 "error: %d\n", task->tk_status);
Trond Myklebust1193d582015-06-02 11:53:21 -04002067 goto out_done;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002068 }
2069
2070 xprt_transmit(task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002071
2072 if (task->tk_status == -EAGAIN)
Trond Myklebust902c5882018-09-01 17:21:01 -04002073 goto out_retry;
Trond Myklebust1193d582015-06-02 11:53:21 -04002074
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002075 xprt_end_transmit(task);
2076 dprint_status(task);
2077 switch (task->tk_status) {
2078 case 0:
2079 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002080 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002081 case -EHOSTDOWN:
2082 case -EHOSTUNREACH:
2083 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002084 case -ECONNRESET:
2085 case -ECONNREFUSED:
2086 case -EADDRINUSE:
2087 case -ENOTCONN:
2088 case -EPIPE:
2089 break;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002090 case -ETIMEDOUT:
2091 /*
2092 * Problem reaching the server. Disconnect and let the
2093 * forechannel reestablish the connection. The server will
2094 * have to retransmit the backchannel request and we'll
2095 * reprocess it. Since these ops are idempotent, there's no
2096 * need to cache our reply at this time.
2097 */
2098 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2099 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002100 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002101 req->rq_connect_cookie);
2102 break;
2103 default:
2104 /*
2105 * We were unable to reply and will have to drop the
2106 * request. The server should reconnect and retransmit.
2107 */
Weston Andros Adamson1facf4c2012-10-23 10:43:30 -04002108 WARN_ON_ONCE(task->tk_status == -EAGAIN);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002109 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2110 "error: %d\n", task->tk_status);
2111 break;
2112 }
Trond Myklebust902c5882018-09-01 17:21:01 -04002113out_wakeup:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002114 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002115out_done:
2116 task->tk_action = rpc_exit_task;
2117 return;
Trond Myklebust1193d582015-06-02 11:53:21 -04002118out_retry:
2119 task->tk_status = 0;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002120}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002121#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
2124 * 6. Sort out the RPC call status
2125 */
2126static void
2127call_status(struct rpc_task *task)
2128{
2129 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 int status;
2131
Chuck Levera25a4cb2018-03-16 10:33:55 -04002132 if (!task->tk_msg.rpc_proc->p_proc)
2133 trace_xprt_ping(task->tk_xprt, task->tk_status);
2134
Chuck Lever46121cf2007-01-31 12:14:08 -05002135 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 status = task->tk_status;
2138 if (status >= 0) {
2139 task->tk_action = call_decode;
2140 return;
2141 }
2142
Steve Dickson5753cba2012-02-06 10:08:08 -05002143 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 task->tk_status = 0;
2145 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002146 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002147 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002148 case -EHOSTUNREACH:
2149 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002150 case -EPERM:
Trond Myklebust9455e3f2014-03-19 13:25:43 -04002151 if (RPC_IS_SOFTCONN(task)) {
2152 rpc_exit(task, status);
2153 break;
2154 }
Trond Myklebust76303992006-08-30 14:32:49 -04002155 /*
2156 * Delay any retries for 3 seconds, then handle as if it
2157 * were a timeout.
2158 */
2159 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002160 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 case -ETIMEDOUT:
2162 task->tk_action = call_timeout;
2163 break;
2164 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002165 case -ECONNRESET:
2166 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002167 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002168 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002169 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002170 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002171 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002172 case -EPIPE:
2173 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 case -EAGAIN:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002175 task->tk_action = call_encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 break;
2177 case -EIO:
2178 /* shutdown or soft timeout */
2179 rpc_exit(task, status);
2180 break;
2181 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002182 if (clnt->cl_chatty)
2183 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002184 clnt->cl_program->name, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187}
2188
2189/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002190 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * We do not release the request slot, so we keep using the
2192 * same XID for all retransmits.
2193 */
2194static void
2195call_timeout(struct rpc_task *task)
2196{
2197 struct rpc_clnt *clnt = task->tk_client;
2198
2199 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002200 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto retry;
2202 }
2203
Chuck Lever46121cf2007-01-31 12:14:08 -05002204 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002205 task->tk_timeouts++;
2206
Chuck Lever3a28bec2009-12-03 15:58:56 -05002207 if (RPC_IS_SOFTCONN(task)) {
2208 rpc_exit(task, -ETIMEDOUT);
2209 return;
2210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07002212 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002213 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002214 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002215 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002216 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002217 if (task->tk_flags & RPC_TASK_TIMEOUT)
2218 rpc_exit(task, -ETIMEDOUT);
2219 else
2220 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return;
2222 }
2223
Chuck Leverf518e35a2006-01-03 09:55:52 +01002224 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002226 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002227 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002228 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002229 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002232 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002233 /*
2234 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2235 * event? RFC2203 requires the server to drop all such requests.
2236 */
2237 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239retry:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002240 task->tk_action = call_encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 task->tk_status = 0;
2242}
2243
2244/*
2245 * 7. Decode the RPC reply
2246 */
2247static void
2248call_decode(struct rpc_task *task)
2249{
2250 struct rpc_clnt *clnt = task->tk_client;
2251 struct rpc_rqst *req = task->tk_rqstp;
Chuck Leverbf269552010-12-14 14:59:29 +00002252 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002253 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Vasily Averin726fd6a2011-06-01 16:23:59 +04002255 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002257 if (!decode) {
2258 task->tk_action = rpc_exit_task;
2259 return;
2260 }
2261
Chuck Leverf518e35a2006-01-03 09:55:52 +01002262 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002263 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002264 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002265 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002266 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2269 }
2270
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002271 /*
2272 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002273 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002274 */
2275 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 req->rq_rcv_buf.len = req->rq_private_buf.len;
2277
2278 /* Check that the softirq receive buffer is valid */
2279 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2280 sizeof(req->rq_rcv_buf)) != 0);
2281
Trond Myklebust1e799b62008-03-21 16:19:41 -04002282 if (req->rq_rcv_buf.len < 12) {
2283 if (!RPC_IS_SOFT(task)) {
Trond Myklebust762e4e62018-08-24 16:28:28 -04002284 task->tk_action = call_encode;
Trond Myklebust1e799b62008-03-21 16:19:41 -04002285 goto out_retry;
2286 }
2287 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002288 clnt->cl_program->name, task->tk_status);
Trond Myklebust1e799b62008-03-21 16:19:41 -04002289 task->tk_action = call_timeout;
2290 goto out_retry;
2291 }
2292
Chuck Leverb0e1c572008-05-21 17:09:19 -04002293 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002294 if (IS_ERR(p)) {
2295 if (p == ERR_PTR(-EAGAIN))
2296 goto out_retry;
2297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Trond Myklebustabbcf282006-01-03 09:55:03 +01002299 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002301 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2302 task->tk_msg.rpc_resp);
2303
Chuck Lever46121cf2007-01-31 12:14:08 -05002304 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2305 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return;
2307out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04002309 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002310 if (task->tk_rqstp == req) {
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002311 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002312 if (task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05002313 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04002314 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002318static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002319rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002323 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07002326
Trond Myklebusta4f08352013-01-08 09:10:21 -05002327 p = xprt_skip_transport_header(req->rq_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 *p++ = req->rq_xid; /* XID */
2329 *p++ = htonl(RPC_CALL); /* CALL */
2330 *p++ = htonl(RPC_VERSION); /* RPC version */
2331 *p++ = htonl(clnt->cl_prog); /* program number */
2332 *p++ = htonl(clnt->cl_vers); /* program version */
2333 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00002334 p = rpcauth_marshcred(task, p);
2335 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2336 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002339static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002340rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002342 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2344 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002345 __be32 *p = iov->iov_base;
2346 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 int error = -EACCES;
2348
David Howellse8896492006-08-24 15:44:19 -04002349 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2350 /* RFC-1014 says that the representation of XDR data must be a
2351 * multiple of four bytes
2352 * - if it isn't pointer subtraction in the NFS client may give
2353 * undefined results
2354 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002355 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08002356 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002357 task->tk_rqstp->rq_rcv_buf.len);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002358 error = -EIO;
2359 goto out_err;
David Howellse8896492006-08-24 15:44:19 -04002360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if ((len -= 3) < 0)
2362 goto out_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002364 p += 1; /* skip XID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002366 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002367 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002368 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002369 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2373 if (--len < 0)
2374 goto out_overflow;
2375 switch ((n = ntohl(*p++))) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00002376 case RPC_AUTH_ERROR:
2377 break;
2378 case RPC_MISMATCH:
2379 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2380 task->tk_pid, __func__);
2381 error = -EPROTONOSUPPORT;
2382 goto out_err;
2383 default:
2384 dprintk("RPC: %5u %s: RPC call rejected, "
2385 "unknown error: %x\n",
2386 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002387 error = -EIO;
2388 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390 if (--len < 0)
2391 goto out_overflow;
2392 switch ((n = ntohl(*p++))) {
2393 case RPC_AUTH_REJECTEDCRED:
2394 case RPC_AUTH_REJECTEDVERF:
2395 case RPCSEC_GSS_CREDPROBLEM:
2396 case RPCSEC_GSS_CTXPROBLEM:
2397 if (!task->tk_cred_retry)
2398 break;
2399 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002400 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002401 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04002403 /* Ensure we obtain a new XID! */
2404 xprt_release(task);
Trond Myklebust118df3d2010-10-24 17:17:31 -04002405 task->tk_action = call_reserve;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002406 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 case RPC_AUTH_BADCRED:
2408 case RPC_AUTH_BADVERF:
2409 /* possibly garbled cred/verf? */
2410 if (!task->tk_garb_retry)
2411 break;
2412 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002413 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002414 task->tk_pid, __func__);
Trond Myklebust762e4e62018-08-24 16:28:28 -04002415 task->tk_action = call_encode;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002416 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 case RPC_AUTH_TOOWEAK:
Chuck Leverb0e1c572008-05-21 17:09:19 -04002418 printk(KERN_NOTICE "RPC: server %s requires stronger "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002419 "authentication.\n",
Trond Myklebustfb43d172016-01-30 16:39:26 -05002420 task->tk_xprt->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 break;
2422 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002423 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002424 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 error = -EIO;
2426 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002427 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002428 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 goto out_err;
2430 }
Andy Adamson35fa5f72013-08-14 11:59:17 -04002431 p = rpcauth_checkverf(task, p);
2432 if (IS_ERR(p)) {
2433 error = PTR_ERR(p);
2434 dprintk("RPC: %5u %s: auth check failed with %d\n",
2435 task->tk_pid, __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002436 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002438 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (len < 0)
2440 goto out_overflow;
2441 switch ((n = ntohl(*p++))) {
2442 case RPC_SUCCESS:
2443 return p;
2444 case RPC_PROG_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002445 dprintk("RPC: %5u %s: program %u is unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002446 "by server %s\n", task->tk_pid, __func__,
2447 (unsigned int)clnt->cl_prog,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002448 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002449 error = -EPFNOSUPPORT;
2450 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 case RPC_PROG_MISMATCH:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002452 dprintk("RPC: %5u %s: program %u, version %u unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002453 "by server %s\n", task->tk_pid, __func__,
2454 (unsigned int)clnt->cl_prog,
2455 (unsigned int)clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002456 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002457 error = -EPROTONOSUPPORT;
2458 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 case RPC_PROC_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002460 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05002461 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002462 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04002463 rpc_proc_name(task),
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002464 clnt->cl_prog, clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002465 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002466 error = -EOPNOTSUPP;
2467 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05002469 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002470 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 break; /* retry */
2472 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002473 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002474 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* Also retry */
2476 }
2477
Trond Myklebustabbcf282006-01-03 09:55:03 +01002478out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002479 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (task->tk_garb_retry) {
2481 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002482 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002483 task->tk_pid, __func__);
Trond Myklebust762e4e62018-08-24 16:28:28 -04002484 task->tk_action = call_encode;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002485out_retry:
2486 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488out_err:
2489 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002490 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002491 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002492 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002494 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002495 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002496 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002498
Christoph Hellwigc512f362017-05-08 09:31:19 +02002499static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2500 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002501{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002502}
2503
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002504static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2505 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002506{
2507 return 0;
2508}
2509
Christoph Hellwig499b4982017-05-12 15:36:49 +02002510static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002511 .p_encode = rpcproc_encode_null,
2512 .p_decode = rpcproc_decode_null,
2513};
2514
Chuck Levercaabea82009-12-03 15:58:56 -05002515static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002516{
2517 struct rpc_message msg = {
2518 .rpc_proc = &rpcproc_null,
2519 };
2520 int err;
2521 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
Chuck Levercaabea82009-12-03 15:58:56 -05002522 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002523 put_rpccred(msg.rpc_cred);
2524 return err;
2525}
Trond Myklebust188fef12007-06-16 14:18:40 -04002526
Trond Myklebust7f554892016-01-30 23:43:35 -05002527static
2528struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2529 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2530 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002531{
2532 struct rpc_message msg = {
2533 .rpc_proc = &rpcproc_null,
2534 .rpc_cred = cred,
2535 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002536 struct rpc_task_setup task_setup_data = {
2537 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002538 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002539 .rpc_message = &msg,
Trond Myklebust7f554892016-01-30 23:43:35 -05002540 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2541 .callback_data = data,
Trond Myklebust84115e12007-07-14 15:39:59 -04002542 .flags = flags,
2543 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002544
Trond Myklebustc970aa82007-07-14 15:39:59 -04002545 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002546}
Trond Myklebust7f554892016-01-30 23:43:35 -05002547
2548struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2549{
2550 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2551}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002552EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002553
Trond Myklebust7f554892016-01-30 23:43:35 -05002554struct rpc_cb_add_xprt_calldata {
2555 struct rpc_xprt_switch *xps;
2556 struct rpc_xprt *xprt;
2557};
2558
2559static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2560{
2561 struct rpc_cb_add_xprt_calldata *data = calldata;
2562
2563 if (task->tk_status == 0)
2564 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2565}
2566
2567static void rpc_cb_add_xprt_release(void *calldata)
2568{
2569 struct rpc_cb_add_xprt_calldata *data = calldata;
2570
2571 xprt_put(data->xprt);
2572 xprt_switch_put(data->xps);
2573 kfree(data);
2574}
2575
Trond Myklebustce272302016-07-24 17:06:28 -04002576static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002577 .rpc_call_done = rpc_cb_add_xprt_done,
2578 .rpc_release = rpc_cb_add_xprt_release,
2579};
2580
2581/**
2582 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2583 * @clnt: pointer to struct rpc_clnt
2584 * @xps: pointer to struct rpc_xprt_switch,
2585 * @xprt: pointer struct rpc_xprt
2586 * @dummy: unused
2587 */
2588int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2589 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2590 void *dummy)
2591{
2592 struct rpc_cb_add_xprt_calldata *data;
2593 struct rpc_cred *cred;
2594 struct rpc_task *task;
2595
2596 data = kmalloc(sizeof(*data), GFP_NOFS);
2597 if (!data)
2598 return -ENOMEM;
2599 data->xps = xprt_switch_get(xps);
2600 data->xprt = xprt_get(xprt);
2601
2602 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2603 task = rpc_call_null_helper(clnt, xprt, cred,
2604 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
2605 &rpc_cb_add_xprt_call_ops, data);
2606 put_rpccred(cred);
2607 if (IS_ERR(task))
2608 return PTR_ERR(task);
2609 rpc_put_task(task);
2610 return 1;
2611}
2612EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2613
2614/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002615 * rpc_clnt_setup_test_and_add_xprt()
2616 *
2617 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2618 * 1) caller of the test function must dereference the rpc_xprt_switch
2619 * and the rpc_xprt.
2620 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2621 * the rpc_call_done routine.
2622 *
2623 * Upon success (return of 1), the test function adds the new
2624 * transport to the rpc_clnt xprt switch
2625 *
2626 * @clnt: struct rpc_clnt to get the new transport
2627 * @xps: the rpc_xprt_switch to hold the new transport
2628 * @xprt: the rpc_xprt to test
2629 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2630 * and test function call data
2631 */
2632int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2633 struct rpc_xprt_switch *xps,
2634 struct rpc_xprt *xprt,
2635 void *data)
2636{
2637 struct rpc_cred *cred;
2638 struct rpc_task *task;
2639 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2640 int status = -EADDRINUSE;
2641
2642 xprt = xprt_get(xprt);
2643 xprt_switch_get(xps);
2644
2645 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2646 goto out_err;
2647
2648 /* Test the connection */
2649 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2650 task = rpc_call_null_helper(clnt, xprt, cred,
2651 RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
2652 NULL, NULL);
2653 put_rpccred(cred);
2654 if (IS_ERR(task)) {
2655 status = PTR_ERR(task);
2656 goto out_err;
2657 }
2658 status = task->tk_status;
2659 rpc_put_task(task);
2660
2661 if (status < 0)
2662 goto out_err;
2663
2664 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2665 xtest->add_xprt_test(clnt, xprt, xtest->data);
2666
2667 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2668 return 1;
2669out_err:
2670 xprt_put(xprt);
2671 xprt_switch_put(xps);
2672 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2673 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2674 return status;
2675}
2676EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2677
2678/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002679 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2680 * @clnt: pointer to struct rpc_clnt
2681 * @xprtargs: pointer to struct xprt_create
2682 * @setup: callback to test and/or set up the connection
2683 * @data: pointer to setup function data
2684 *
2685 * Creates a new transport using the parameters set in args and
2686 * adds it to clnt.
2687 * If ping is set, then test that connectivity succeeds before
2688 * adding the new transport.
2689 *
2690 */
2691int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2692 struct xprt_create *xprtargs,
2693 int (*setup)(struct rpc_clnt *,
2694 struct rpc_xprt_switch *,
2695 struct rpc_xprt *,
2696 void *),
2697 void *data)
2698{
2699 struct rpc_xprt_switch *xps;
2700 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002701 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002702 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002703 unsigned char resvport;
2704 int ret = 0;
2705
2706 rcu_read_lock();
2707 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2708 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2709 if (xps == NULL || xprt == NULL) {
2710 rcu_read_unlock();
2711 return -EAGAIN;
2712 }
2713 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002714 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002715 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002716 rcu_read_unlock();
2717
2718 xprt = xprt_create_transport(xprtargs);
2719 if (IS_ERR(xprt)) {
2720 ret = PTR_ERR(xprt);
2721 goto out_put_switch;
2722 }
2723 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002724 if (xprt->ops->set_connect_timeout != NULL)
2725 xprt->ops->set_connect_timeout(xprt,
2726 connect_timeout,
2727 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002728
2729 rpc_xprt_switch_set_roundrobin(xps);
2730 if (setup) {
2731 ret = setup(clnt, xps, xprt, data);
2732 if (ret != 0)
2733 goto out_put_xprt;
2734 }
2735 rpc_xprt_switch_add_xprt(xps, xprt);
2736out_put_xprt:
2737 xprt_put(xprt);
2738out_put_switch:
2739 xprt_switch_put(xps);
2740 return ret;
2741}
2742EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2743
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002744struct connect_timeout_data {
2745 unsigned long connect_timeout;
2746 unsigned long reconnect_timeout;
2747};
2748
Trond Myklebust8d480322016-08-05 19:03:31 -04002749static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002750rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002751 struct rpc_xprt *xprt,
2752 void *data)
2753{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002754 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002755
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002756 if (xprt->ops->set_connect_timeout)
2757 xprt->ops->set_connect_timeout(xprt,
2758 timeo->connect_timeout,
2759 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002760 return 0;
2761}
2762
2763void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002764rpc_set_connect_timeout(struct rpc_clnt *clnt,
2765 unsigned long connect_timeout,
2766 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002767{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002768 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002769 .connect_timeout = connect_timeout,
2770 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002771 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002772 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002773 rpc_xprt_set_connect_timeout,
2774 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002775}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002776EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002777
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002778void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2779{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002780 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002781 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002782 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002783}
2784EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2785
Andy Adamsondd691712016-09-09 09:22:24 -04002786void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2787{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002788 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002789 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2790 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002791 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002792}
2793EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2794
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002795bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2796 const struct sockaddr *sap)
2797{
2798 struct rpc_xprt_switch *xps;
2799 bool ret;
2800
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002801 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002802 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002803 ret = rpc_xprt_switch_has_addr(xps, sap);
2804 rcu_read_unlock();
2805 return ret;
2806}
2807EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2808
Jeff Laytonf895b252014-11-17 16:58:04 -05002809#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002810static void rpc_show_header(void)
2811{
Chuck Levercb3997b2008-05-21 17:09:41 -04002812 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2813 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002814}
2815
Chuck Lever38e886e2008-05-21 17:09:33 -04002816static void rpc_show_task(const struct rpc_clnt *clnt,
2817 const struct rpc_task *task)
2818{
2819 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002820
2821 if (RPC_IS_QUEUED(task))
2822 rpc_waitq = rpc_qname(task->tk_waitqueue);
2823
Joe Perchesb3bceda2010-12-21 10:52:24 -05002824 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 -04002825 task->tk_pid, task->tk_flags, task->tk_status,
2826 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002827 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002828 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002829}
2830
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002831void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002832{
2833 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002834 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002835 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002836 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002837
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002838 spin_lock(&sn->rpc_client_lock);
2839 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002840 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002841 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002842 if (!header) {
2843 rpc_show_header();
2844 header++;
2845 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002846 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002847 }
2848 spin_unlock(&clnt->cl_lock);
2849 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002850 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002851}
2852#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002853
2854#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002855static int
2856rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2857 struct rpc_xprt *xprt,
2858 void *dummy)
2859{
2860 return xprt_enable_swap(xprt);
2861}
2862
Jeff Layton3c87ef62015-06-03 16:14:25 -04002863int
2864rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2865{
Trond Myklebust15001e52016-01-30 20:05:34 -05002866 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2867 return rpc_clnt_iterate_for_each_xprt(clnt,
2868 rpc_clnt_swap_activate_callback, NULL);
2869 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002870}
2871EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2872
Trond Myklebust15001e52016-01-30 20:05:34 -05002873static int
2874rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2875 struct rpc_xprt *xprt,
2876 void *dummy)
2877{
2878 xprt_disable_swap(xprt);
2879 return 0;
2880}
2881
Jeff Layton3c87ef62015-06-03 16:14:25 -04002882void
2883rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2884{
Trond Myklebust15001e52016-01-30 20:05:34 -05002885 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2886 rpc_clnt_iterate_for_each_xprt(clnt,
2887 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002888}
2889EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2890#endif /* CONFIG_SUNRPC_SWAP */