blob: a02de2bddb28b48bb6798327c0814e769314621b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/xprt.c
4 *
5 * This is a generic RPC call interface supporting congestion avoidance,
6 * and asynchronous calls.
7 *
8 * The interface works like this:
9 *
10 * - When a process places a call, it allocates a request slot if
11 * one is available. Otherwise, it sleeps on the backlog queue
12 * (xprt_reserve).
13 * - Next, the caller puts together the RPC message, stuffs it into
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 * the request struct, and calls xprt_transmit().
15 * - xprt_transmit sends the message and installs the caller on the
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040016 * transport's wait list. At the same time, if a reply is expected,
17 * it installs a timer that is run after the packet's timeout has
18 * expired.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040020 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * caller is woken up, and the timer removed.
22 * - When no reply arrives within the timeout interval, the timer is
23 * fired by the kernel and runs xprt_timer(). It either adjusts the
24 * timeout values (minor timeout) or wakes up the caller with a status
25 * of -ETIMEDOUT.
26 * - When the caller receives a notification from RPC that a reply arrived,
27 * it should release the RPC slot, and process the reply.
28 * If the call timed out, it may choose to retry the operation by
29 * adjusting the initial timeout value, and simply calling rpc_call
30 * again.
31 *
32 * Support for async RPC is done through a set of RPC-specific scheduling
33 * primitives that `transparently' work for processes as well as async
34 * tasks that rely on callbacks.
35 *
36 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
Chuck Lever55aa4f52005-08-11 16:25:47 -040037 *
38 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Chuck Levera246b012005-08-11 16:25:23 -040041#include <linux/module.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040044#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040046#include <linux/net.h>
Chuck Leverff839972010-05-07 13:34:47 -040047#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Chuck Levera246b012005-08-11 16:25:23 -040049#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050050#include <linux/sunrpc/metrics.h>
Trond Myklebustc9acb422010-03-19 15:36:22 -040051#include <linux/sunrpc/bc_xprt.h>
Trond Myklebustfda1bfe2015-02-14 17:48:49 -050052#include <linux/rcupdate.h>
Trond Myklebusta1231fd2019-02-18 10:02:29 -050053#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Layton3705ad62014-10-28 14:24:13 -040055#include <trace/events/sunrpc.h>
56
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040057#include "sunrpc.h"
Olga Kornievskaia587bc722021-06-08 15:59:19 -040058#include "sysfs.h"
Chuck Levera4ae3082021-08-05 10:25:49 -040059#include "fail.h"
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Local variables
63 */
64
Jeff Laytonf895b252014-11-17 16:58:04 -050065#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066# define RPCDBG_FACILITY RPCDBG_XPRT
67#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Local functions
71 */
Trond Myklebust21de0a92011-07-17 16:57:32 -040072static void xprt_init(struct rpc_xprt *xprt, struct net *net);
Chuck Lever37ac86c2018-05-04 15:34:53 -040073static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Trond Myklebust4e0038b2012-03-01 17:01:05 -050074static void xprt_destroy(struct rpc_xprt *xprt);
NeilBrowne877a882021-05-17 09:59:10 +100075static void xprt_request_init(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jiri Slaby5ba03e82007-11-22 19:40:22 +080077static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040078static LIST_HEAD(xprt_list);
79
Trond Myklebust9e910bf2019-04-07 13:58:53 -040080static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
81{
82 unsigned long timeout = jiffies + req->rq_timeout;
83
84 if (time_before(timeout, req->rq_majortimeo))
85 return timeout;
86 return req->rq_majortimeo;
87}
88
Chuck Lever12a80462005-08-25 16:25:51 -070089/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040090 * xprt_register_transport - register a transport implementation
91 * @transport: transport to register
92 *
93 * If a transport implementation is loaded as a kernel module, it can
94 * call this interface to make itself known to the RPC client.
95 *
96 * Returns:
97 * 0: transport successfully registered
98 * -EEXIST: transport already registered
99 * -EINVAL: transport module being unloaded
100 */
101int xprt_register_transport(struct xprt_class *transport)
102{
103 struct xprt_class *t;
104 int result;
105
106 result = -EEXIST;
107 spin_lock(&xprt_list_lock);
108 list_for_each_entry(t, &xprt_list, list) {
109 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400110 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400111 goto out;
112 }
113
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400114 list_add_tail(&transport->list, &xprt_list);
115 printk(KERN_INFO "RPC: Registered %s transport module.\n",
116 transport->name);
117 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400118
119out:
120 spin_unlock(&xprt_list_lock);
121 return result;
122}
123EXPORT_SYMBOL_GPL(xprt_register_transport);
124
125/**
126 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800127 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400128 *
129 * Returns:
130 * 0: transport successfully unregistered
131 * -ENOENT: transport never registered
132 */
133int xprt_unregister_transport(struct xprt_class *transport)
134{
135 struct xprt_class *t;
136 int result;
137
138 result = 0;
139 spin_lock(&xprt_list_lock);
140 list_for_each_entry(t, &xprt_list, list) {
141 if (t == transport) {
142 printk(KERN_INFO
143 "RPC: Unregistered %s transport module.\n",
144 transport->name);
145 list_del_init(&transport->list);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400146 goto out;
147 }
148 }
149 result = -ENOENT;
150
151out:
152 spin_unlock(&xprt_list_lock);
153 return result;
154}
155EXPORT_SYMBOL_GPL(xprt_unregister_transport);
156
Trond Myklebustd5aa6b22020-11-06 16:33:38 -0500157static void
158xprt_class_release(const struct xprt_class *t)
159{
160 module_put(t->owner);
161}
162
163static const struct xprt_class *
Trond Myklebust9bccd262020-11-10 12:58:22 -0500164xprt_class_find_by_ident_locked(int ident)
165{
166 const struct xprt_class *t;
167
168 list_for_each_entry(t, &xprt_list, list) {
169 if (t->ident != ident)
170 continue;
171 if (!try_module_get(t->owner))
172 continue;
173 return t;
174 }
175 return NULL;
176}
177
178static const struct xprt_class *
179xprt_class_find_by_ident(int ident)
180{
181 const struct xprt_class *t;
182
183 spin_lock(&xprt_list_lock);
184 t = xprt_class_find_by_ident_locked(ident);
185 spin_unlock(&xprt_list_lock);
186 return t;
187}
188
189static const struct xprt_class *
Trond Myklebustd5aa6b22020-11-06 16:33:38 -0500190xprt_class_find_by_netid_locked(const char *netid)
191{
192 const struct xprt_class *t;
193 unsigned int i;
194
195 list_for_each_entry(t, &xprt_list, list) {
196 for (i = 0; t->netid[i][0] != '\0'; i++) {
197 if (strcmp(t->netid[i], netid) != 0)
198 continue;
199 if (!try_module_get(t->owner))
200 continue;
201 return t;
202 }
203 }
204 return NULL;
205}
206
207static const struct xprt_class *
208xprt_class_find_by_netid(const char *netid)
209{
210 const struct xprt_class *t;
211
212 spin_lock(&xprt_list_lock);
213 t = xprt_class_find_by_netid_locked(netid);
214 if (!t) {
215 spin_unlock(&xprt_list_lock);
216 request_module("rpc%s", netid);
217 spin_lock(&xprt_list_lock);
218 t = xprt_class_find_by_netid_locked(netid);
219 }
220 spin_unlock(&xprt_list_lock);
221 return t;
222}
223
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400224/**
Trond Myklebust1fc5f132020-11-10 09:41:21 -0500225 * xprt_find_transport_ident - convert a netid into a transport identifier
226 * @netid: transport to load
227 *
228 * Returns:
229 * > 0: transport identifier
230 * -ENOENT: transport module not available
231 */
232int xprt_find_transport_ident(const char *netid)
233{
234 const struct xprt_class *t;
235 int ret;
236
237 t = xprt_class_find_by_netid(netid);
238 if (!t)
239 return -ENOENT;
240 ret = t->ident;
241 xprt_class_release(t);
242 return ret;
243}
244EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
245
Trond Myklebustc5445772018-09-03 23:39:27 -0400246static void xprt_clear_locked(struct rpc_xprt *xprt)
247{
248 xprt->snd_task = NULL;
Trond Myklebust33c32142021-07-12 12:34:34 -0400249 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state))
250 clear_bit_unlock(XPRT_LOCKED, &xprt->state);
251 else
Trond Myklebustc5445772018-09-03 23:39:27 -0400252 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
253}
254
Tom Talpey441e3e22009-03-11 14:37:56 -0400255/**
Chuck Lever12a80462005-08-25 16:25:51 -0700256 * xprt_reserve_xprt - serialize write access to transports
257 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000258 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700259 *
260 * This prevents mixing the payload of separate requests, and prevents
261 * transport connects from colliding with writes. No congestion control
262 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400264int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Chuck Lever12a80462005-08-25 16:25:51 -0700266 struct rpc_rqst *req = task->tk_rqstp;
267
268 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
269 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400270 goto out_locked;
Chuck Lever12a80462005-08-25 16:25:51 -0700271 goto out_sleep;
272 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400273 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
274 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700275 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500276
Chuck Leverbf7ca702019-10-09 12:58:14 -0400277out_locked:
278 trace_xprt_reserve_xprt(xprt, task);
Chuck Lever12a80462005-08-25 16:25:51 -0700279 return 1;
280
Trond Myklebustc5445772018-09-03 23:39:27 -0400281out_unlock:
282 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700283out_sleep:
Chuck Lever12a80462005-08-25 16:25:51 -0700284 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400285 if (RPC_IS_SOFT(task))
286 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400287 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400288 else
289 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700290 return 0;
291}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400292EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700293
Trond Myklebust75891f52018-09-03 17:37:36 -0400294static bool
295xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
296{
297 return test_bit(XPRT_CWND_WAIT, &xprt->state);
298}
299
300static void
301xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
302{
303 if (!list_empty(&xprt->xmit_queue)) {
304 /* Peek at head of queue to see if it can make progress */
305 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
306 rq_xmit)->rq_cong)
307 return;
308 }
309 set_bit(XPRT_CWND_WAIT, &xprt->state);
310}
311
312static void
313xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
314{
315 if (!RPCXPRT_CONGESTED(xprt))
316 clear_bit(XPRT_CWND_WAIT, &xprt->state);
317}
318
Chuck Lever12a80462005-08-25 16:25:51 -0700319/*
320 * xprt_reserve_xprt_cong - serialize write access to transports
321 * @task: task that is requesting access to the transport
322 *
323 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
324 * integrated into the decision of whether a request is allowed to be
325 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400326 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700327 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400328int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700329{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct rpc_rqst *req = task->tk_rqstp;
331
Chuck Lever2226feb2005-08-11 16:25:38 -0400332 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400334 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto out_sleep;
336 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400337 if (req == NULL) {
338 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400339 goto out_locked;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400340 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400341 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
342 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400343 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400345 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400347out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100348 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349out_sleep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400351 if (RPC_IS_SOFT(task))
352 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400353 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400354 else
355 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400357out_locked:
358 trace_xprt_reserve_cong(xprt, task);
359 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400361EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Chuck Lever12a80462005-08-25 16:25:51 -0700363static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 int retval;
366
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400367 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
368 return 1;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400369 spin_lock(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400370 retval = xprt->ops->reserve_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400371 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return retval;
373}
374
Trond Myklebust961a8282012-01-17 22:57:37 -0500375static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Trond Myklebust961a8282012-01-17 22:57:37 -0500377 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700378
Chuck Lever49e9a892005-08-25 16:25:51 -0700379 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500380 return true;
381}
Chuck Lever49e9a892005-08-25 16:25:51 -0700382
Trond Myklebust961a8282012-01-17 22:57:37 -0500383static void __xprt_lock_write_next(struct rpc_xprt *xprt)
384{
385 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
386 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400387 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
388 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400389 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
390 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500391 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400392out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100393 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700394}
395
Trond Myklebust961a8282012-01-17 22:57:37 -0500396static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
397{
398 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
399 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400400 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
401 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400402 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500403 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400404 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400405 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100408 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Chuck Lever49e9a892005-08-25 16:25:51 -0700411/**
412 * xprt_release_xprt - allow other requests to use a transport
413 * @xprt: transport with other tasks potentially waiting
414 * @task: task that is releasing access to the transport
415 *
416 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700418void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100421 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 __xprt_lock_write_next(xprt);
423 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400424 trace_xprt_release_xprt(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400426EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Chuck Lever49e9a892005-08-25 16:25:51 -0700428/**
429 * xprt_release_xprt_cong - allow other requests to use a transport
430 * @xprt: transport with other tasks potentially waiting
431 * @task: task that is releasing access to the transport
432 *
433 * Note that "task" can be NULL. Another task is awoken to use the
434 * transport if the transport's congestion window allows it.
435 */
436void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
437{
438 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100439 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700440 __xprt_lock_write_next_cong(xprt);
441 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400442 trace_xprt_release_cong(xprt, task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700443}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400444EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700445
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400446void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400448 if (xprt->snd_task != task)
449 return;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400450 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700451 xprt->ops->release_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400452 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Van Jacobson congestion avoidance. Check if the congestion window
457 * overflowed. Put the task to sleep if this is the case.
458 */
459static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400460__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (req->rq_cong)
463 return 1;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400464 trace_xprt_get_cong(xprt, req->rq_task);
Trond Myklebust75891f52018-09-03 17:37:36 -0400465 if (RPCXPRT_CONGESTED(xprt)) {
466 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 req->rq_cong = 1;
470 xprt->cong += RPC_CWNDSCALE;
471 return 1;
472}
473
474/*
475 * Adjust the congestion window, and wake up the next task
476 * that has been sleeping due to congestion
477 */
478static void
479__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
480{
481 if (!req->rq_cong)
482 return;
483 req->rq_cong = 0;
484 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400485 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Leverbf7ca702019-10-09 12:58:14 -0400486 trace_xprt_put_cong(xprt, req->rq_task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700487 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Chuck Lever46c0ee82005-08-25 16:25:52 -0700490/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400491 * xprt_request_get_cong - Request congestion control credits
492 * @xprt: pointer to transport
493 * @req: pointer to RPC request
494 *
495 * Useful for transports that require congestion control.
496 */
497bool
498xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
499{
500 bool ret = false;
501
502 if (req->rq_cong)
503 return true;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400504 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400505 ret = __xprt_get_cong(xprt, req) != 0;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400506 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400507 return ret;
508}
509EXPORT_SYMBOL_GPL(xprt_request_get_cong);
510
511/**
Chuck Levera58dd392005-08-25 16:25:53 -0700512 * xprt_release_rqst_cong - housekeeping when request is complete
513 * @task: RPC request that recently completed
514 *
515 * Useful for transports that require congestion control.
516 */
517void xprt_release_rqst_cong(struct rpc_task *task)
518{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500519 struct rpc_rqst *req = task->tk_rqstp;
520
521 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700522}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400523EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700524
Chuck Lever8593e012019-09-13 16:01:07 -0400525static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
526{
527 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
528 __xprt_lock_write_next_cong(xprt);
529}
530
Trond Myklebust75891f52018-09-03 17:37:36 -0400531/*
532 * Clear the congestion window wait flag and wake up the next
533 * entry on xprt->sending
534 */
535static void
536xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
537{
538 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
Trond Myklebustb5e92412019-05-02 11:21:08 -0400539 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400540 __xprt_lock_write_next_cong(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400541 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400542 }
543}
544
Chuck Levera58dd392005-08-25 16:25:53 -0700545/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700546 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500547 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700548 * @task: recently completed RPC request used to adjust window
549 * @result: result code of completed RPC request
550 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400551 * The transport code maintains an estimate on the maximum number of out-
552 * standing RPC requests, using a smoothed version of the congestion
553 * avoidance implemented in 44BSD. This is basically the Van Jacobson
554 * congestion algorithm: If a retransmit occurs, the congestion window is
555 * halved; otherwise, it is incremented by 1/cwnd when
556 *
557 * - a reply is received and
558 * - a full number of requests are outstanding and
559 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500561void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700563 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700564 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (result >= 0 && cwnd <= xprt->cong) {
567 /* The (cwnd >> 1) term makes sure
568 * the result gets rounded properly. */
569 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
570 if (cwnd > RPC_MAXCWND(xprt))
571 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700572 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 } else if (result == -ETIMEDOUT) {
574 cwnd >>= 1;
575 if (cwnd < RPC_CWNDSCALE)
576 cwnd = RPC_CWNDSCALE;
577 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500578 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 xprt->cong, xprt->cwnd, cwnd);
580 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700581 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400583EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Chuck Lever44fbac22005-08-11 16:25:44 -0400585/**
586 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
587 * @xprt: transport with waiting tasks
588 * @status: result code to plant in each task before waking it
589 *
590 */
591void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
592{
593 if (status < 0)
594 rpc_wake_up_status(&xprt->pending, status);
595 else
596 rpc_wake_up(&xprt->pending);
597}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400598EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400599
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400600/**
601 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400602 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500603 *
604 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
605 * we don't in general want to force a socket disconnection due to
606 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400607 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400608void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400609{
Trond Myklebustc5445772018-09-03 23:39:27 -0400610 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400611}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400612EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400613
Trond Myklebustc5445772018-09-03 23:39:27 -0400614static bool
615xprt_clear_write_space_locked(struct rpc_xprt *xprt)
616{
617 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
618 __xprt_lock_write_next(xprt);
619 dprintk("RPC: write space: waking waiting task on "
620 "xprt %p\n", xprt);
621 return true;
622 }
623 return false;
624}
625
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400626/**
627 * xprt_write_space - wake the task waiting for transport output buffer space
628 * @xprt: transport with waiting tasks
629 *
630 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
631 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400632bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400633{
Trond Myklebustc5445772018-09-03 23:39:27 -0400634 bool ret;
635
636 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
637 return false;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400638 spin_lock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400639 ret = xprt_clear_write_space_locked(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400640 spin_unlock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400641 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400642}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400643EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400644
Trond Myklebustda953062019-04-07 13:58:56 -0400645static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
646{
647 s64 delta = ktime_to_ns(ktime_get() - abstime);
648 return likely(delta >= 0) ?
649 jiffies - nsecs_to_jiffies(delta) :
650 jiffies + nsecs_to_jiffies(-delta);
651}
652
653static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500655 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Trond Myklebustda953062019-04-07 13:58:56 -0400656 unsigned long majortimeo = req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (to->to_exponential)
Trond Myklebustda953062019-04-07 13:58:56 -0400659 majortimeo <<= to->to_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 else
Trond Myklebustda953062019-04-07 13:58:56 -0400661 majortimeo += to->to_increment * to->to_retries;
662 if (majortimeo > to->to_maxval || majortimeo == 0)
663 majortimeo = to->to_maxval;
664 return majortimeo;
665}
666
667static void xprt_reset_majortimeo(struct rpc_rqst *req)
668{
669 req->rq_majortimeo += xprt_calc_majortimeo(req);
670}
671
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400672static void xprt_reset_minortimeo(struct rpc_rqst *req)
673{
674 req->rq_minortimeo += req->rq_timeout;
675}
676
Trond Myklebustda953062019-04-07 13:58:56 -0400677static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
678{
679 unsigned long time_init;
680 struct rpc_xprt *xprt = req->rq_xprt;
681
682 if (likely(xprt && xprt_connected(xprt)))
683 time_init = jiffies;
684 else
685 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
686 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
687 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400688 req->rq_minortimeo = time_init + req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Chuck Lever9903cd12005-08-11 16:25:26 -0400691/**
692 * xprt_adjust_timeout - adjust timeout values for next retransmit
693 * @req: RPC request containing parameters to use for the adjustment
694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
696int xprt_adjust_timeout(struct rpc_rqst *req)
697{
698 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500699 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int status = 0;
701
702 if (time_before(jiffies, req->rq_majortimeo)) {
Chris Dion09252172021-04-04 21:29:26 -0400703 if (time_before(jiffies, req->rq_minortimeo))
704 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (to->to_exponential)
706 req->rq_timeout <<= 1;
707 else
708 req->rq_timeout += to->to_increment;
709 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
710 req->rq_timeout = to->to_maxval;
711 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else {
713 req->rq_timeout = to->to_initval;
714 req->rq_retries = 0;
715 xprt_reset_majortimeo(req);
716 /* Reset the RTT counters == "slow start" */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400717 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400719 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 status = -ETIMEDOUT;
721 }
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400722 xprt_reset_minortimeo(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (req->rq_timeout == 0) {
725 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
726 req->rq_timeout = 5 * HZ;
727 }
728 return status;
729}
730
David Howells65f27f32006-11-22 14:55:48 +0000731static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
David Howells65f27f32006-11-22 14:55:48 +0000733 struct rpc_xprt *xprt =
734 container_of(work, struct rpc_xprt, task_cleanup);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500735 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Chuck Lever911813d2020-05-12 17:13:34 -0400737 trace_xprt_disconnect_auto(xprt);
Trond Myklebustd896ba82021-10-29 12:26:17 -0400738 xprt->connect_cookie++;
739 smp_mb__before_atomic();
Trond Myklebust66af1e552007-11-06 10:18:36 -0500740 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400741 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400743 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500744 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Chuck Lever9903cd12005-08-11 16:25:26 -0400747/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500748 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400749 * @xprt: transport to flag for disconnect
750 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500752void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Chuck Lever911813d2020-05-12 17:13:34 -0400754 trace_xprt_disconnect_done(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400755 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400757 xprt_clear_write_space_locked(xprt);
Chuck Lever8593e012019-09-13 16:01:07 -0400758 xprt_clear_congestion_window_wait_locked(xprt);
Trond Myklebust27adc782019-03-15 08:01:16 -0400759 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400760 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500762EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Trond Myklebust66af1e552007-11-06 10:18:36 -0500764/**
Trond Myklebuste26d9972021-07-26 07:59:24 -0400765 * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
766 * @xprt: transport to disconnect
767 */
768static void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
769{
Trond Myklebust3be232f2021-10-26 18:01:07 -0400770 if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
771 return;
Trond Myklebuste26d9972021-07-26 07:59:24 -0400772 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
773 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
774 else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
775 rpc_wake_up_queued_task_set_status(&xprt->pending,
776 xprt->snd_task, -ENOTCONN);
777}
778
779/**
Trond Myklebust66af1e552007-11-06 10:18:36 -0500780 * xprt_force_disconnect - force a transport to disconnect
781 * @xprt: transport to disconnect
782 *
783 */
784void xprt_force_disconnect(struct rpc_xprt *xprt)
785{
Chuck Lever911813d2020-05-12 17:13:34 -0400786 trace_xprt_disconnect_force(xprt);
787
Trond Myklebust66af1e552007-11-06 10:18:36 -0500788 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400789 spin_lock(&xprt->transport_lock);
Trond Myklebuste26d9972021-07-26 07:59:24 -0400790 xprt_schedule_autoclose_locked(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400791 spin_unlock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500792}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400793EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500794
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400795static unsigned int
796xprt_connect_cookie(struct rpc_xprt *xprt)
797{
798 return READ_ONCE(xprt->connect_cookie);
799}
800
801static bool
802xprt_request_retransmit_after_disconnect(struct rpc_task *task)
803{
804 struct rpc_rqst *req = task->tk_rqstp;
805 struct rpc_xprt *xprt = req->rq_xprt;
806
807 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
808 !xprt_connected(xprt);
809}
810
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400811/**
812 * xprt_conditional_disconnect - force a transport to disconnect
813 * @xprt: transport to disconnect
814 * @cookie: 'connection cookie'
815 *
816 * This attempts to break the connection if and only if 'cookie' matches
817 * the current transport 'connection cookie'. It ensures that we don't
818 * try to break the connection more than once when we need to retransmit
819 * a batch of RPC requests.
820 *
821 */
822void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
823{
824 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400825 spin_lock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400826 if (cookie != xprt->connect_cookie)
827 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100828 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400829 goto out;
Trond Myklebuste26d9972021-07-26 07:59:24 -0400830 xprt_schedule_autoclose_locked(xprt);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400831out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400832 spin_unlock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400833}
834
Trond Myklebustad3331a2016-08-02 13:47:43 -0400835static bool
836xprt_has_timer(const struct rpc_xprt *xprt)
837{
838 return xprt->idle_timeout != 0;
839}
840
841static void
842xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
843 __must_hold(&xprt->transport_lock)
844{
Dave Wysochanski80d3c452019-06-26 16:30:24 -0400845 xprt->last_used = jiffies;
Trond Myklebust95f76912018-09-07 08:35:22 -0400846 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400847 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static void
Kees Cookff861c42017-10-16 17:29:42 -0700851xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Kees Cookff861c42017-10-16 17:29:42 -0700853 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Trond Myklebust95f76912018-09-07 08:35:22 -0400855 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400856 return;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400857 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
858 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400859 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400860 return;
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400861 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Chuck Levera4ae3082021-08-05 10:25:49 -0400864#if IS_ENABLED(CONFIG_FAIL_SUNRPC)
865static void xprt_inject_disconnect(struct rpc_xprt *xprt)
866{
867 if (!fail_sunrpc.ignore_client_disconnect &&
868 should_fail(&fail_sunrpc.attr, 1))
869 xprt->ops->inject_disconnect(xprt);
870}
871#else
872static inline void xprt_inject_disconnect(struct rpc_xprt *xprt)
873{
874}
875#endif
876
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500877bool xprt_lock_connect(struct rpc_xprt *xprt,
878 struct rpc_task *task,
879 void *cookie)
880{
881 bool ret = false;
882
Trond Myklebustb5e92412019-05-02 11:21:08 -0400883 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500884 if (!test_bit(XPRT_LOCKED, &xprt->state))
885 goto out;
886 if (xprt->snd_task != task)
887 goto out;
Trond Myklebustc2dc3e52021-07-26 07:59:23 -0400888 set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500889 xprt->snd_task = cookie;
890 ret = true;
891out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400892 spin_unlock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500893 return ret;
894}
Trond Myklebustf99fa502021-07-26 08:03:12 -0400895EXPORT_SYMBOL_GPL(xprt_lock_connect);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500896
897void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
898{
Trond Myklebustb5e92412019-05-02 11:21:08 -0400899 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500900 if (xprt->snd_task != cookie)
901 goto out;
902 if (!test_bit(XPRT_LOCKED, &xprt->state))
903 goto out;
904 xprt->snd_task =NULL;
Trond Myklebustc2dc3e52021-07-26 07:59:23 -0400905 clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500906 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400907 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500908out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400909 spin_unlock(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400910 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500911}
Trond Myklebustf99fa502021-07-26 08:03:12 -0400912EXPORT_SYMBOL_GPL(xprt_unlock_connect);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500913
Chuck Lever9903cd12005-08-11 16:25:26 -0400914/**
915 * xprt_connect - schedule a transport connect operation
916 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 */
919void xprt_connect(struct rpc_task *task)
920{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500921 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Chuck Leverdb0a86c2020-07-08 16:09:47 -0400923 trace_xprt_connect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Chuck Leverec739ef2006-08-22 20:06:15 -0400925 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400926 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return;
928 }
929 if (!xprt_lock_write(xprt, task))
930 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500931
Chuck Lever911813d2020-05-12 17:13:34 -0400932 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
933 trace_xprt_disconnect_cleanup(xprt);
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500934 xprt->ops->close(xprt);
Chuck Lever911813d2020-05-12 17:13:34 -0400935 }
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500936
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500937 if (!xprt_connected(xprt)) {
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100938 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400939 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400940 xprt_request_timeout(task->tk_rqstp));
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400941
942 if (test_bit(XPRT_CLOSING, &xprt->state))
943 return;
944 if (xprt_test_and_set_connecting(xprt))
945 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500946 /* Race breaker */
947 if (!xprt_connected(xprt)) {
948 xprt->stat.connect_start = jiffies;
949 xprt->ops->connect(xprt, task);
950 } else {
951 xprt_clear_connecting(xprt);
952 task->tk_status = 0;
953 rpc_wake_up_queued_task(&xprt->pending, task);
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500956 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Chuck Lever675dd902019-06-19 10:33:42 -0400959/**
960 * xprt_reconnect_delay - compute the wait before scheduling a connect
961 * @xprt: transport instance
962 *
963 */
964unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
965{
966 unsigned long start, now = jiffies;
967
968 start = xprt->stat.connect_start + xprt->reestablish_timeout;
969 if (time_after(start, now))
970 return start - now;
971 return 0;
972}
973EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
974
975/**
976 * xprt_reconnect_backoff - compute the new re-establish timeout
977 * @xprt: transport instance
978 * @init_to: initial reestablish timeout
979 *
980 */
981void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
982{
983 xprt->reestablish_timeout <<= 1;
984 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
985 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
986 if (xprt->reestablish_timeout < init_to)
987 xprt->reestablish_timeout = init_to;
988}
989EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
990
Trond Myklebust95f76912018-09-07 08:35:22 -0400991enum xprt_xid_rb_cmp {
992 XID_RB_EQUAL,
993 XID_RB_LEFT,
994 XID_RB_RIGHT,
995};
996static enum xprt_xid_rb_cmp
997xprt_xid_cmp(__be32 xid1, __be32 xid2)
998{
999 if (xid1 == xid2)
1000 return XID_RB_EQUAL;
1001 if ((__force u32)xid1 < (__force u32)xid2)
1002 return XID_RB_LEFT;
1003 return XID_RB_RIGHT;
1004}
1005
1006static struct rpc_rqst *
1007xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
1008{
1009 struct rb_node *n = xprt->recv_queue.rb_node;
1010 struct rpc_rqst *req;
1011
1012 while (n != NULL) {
1013 req = rb_entry(n, struct rpc_rqst, rq_recv);
1014 switch (xprt_xid_cmp(xid, req->rq_xid)) {
1015 case XID_RB_LEFT:
1016 n = n->rb_left;
1017 break;
1018 case XID_RB_RIGHT:
1019 n = n->rb_right;
1020 break;
1021 case XID_RB_EQUAL:
1022 return req;
1023 }
1024 }
1025 return NULL;
1026}
1027
1028static void
1029xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
1030{
1031 struct rb_node **p = &xprt->recv_queue.rb_node;
1032 struct rb_node *n = NULL;
1033 struct rpc_rqst *req;
1034
1035 while (*p != NULL) {
1036 n = *p;
1037 req = rb_entry(n, struct rpc_rqst, rq_recv);
1038 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
1039 case XID_RB_LEFT:
1040 p = &n->rb_left;
1041 break;
1042 case XID_RB_RIGHT:
1043 p = &n->rb_right;
1044 break;
1045 case XID_RB_EQUAL:
1046 WARN_ON_ONCE(new != req);
1047 return;
1048 }
1049 }
1050 rb_link_node(&new->rq_recv, n, p);
1051 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
1052}
1053
1054static void
1055xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
1056{
1057 rb_erase(&req->rq_recv, &xprt->recv_queue);
1058}
1059
Chuck Lever9903cd12005-08-11 16:25:26 -04001060/**
1061 * xprt_lookup_rqst - find an RPC request corresponding to an XID
1062 * @xprt: transport on which the original request was transmitted
1063 * @xid: RPC XID of incoming reply
1064 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001065 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001067struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001069 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Trond Myklebust95f76912018-09-07 08:35:22 -04001071 entry = xprt_request_rb_find(xprt, xid);
1072 if (entry != NULL) {
1073 trace_xprt_lookup_rqst(xprt, xid, 0);
1074 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1075 return entry;
1076 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001077
1078 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
1079 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -04001080 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -05001081 xprt->stat.bad_xids++;
1082 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001084EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001086static bool
1087xprt_is_pinned_rqst(struct rpc_rqst *req)
1088{
1089 return atomic_read(&req->rq_pin) != 0;
1090}
1091
Trond Myklebust729749b2017-08-13 10:03:59 -04001092/**
1093 * xprt_pin_rqst - Pin a request on the transport receive list
1094 * @req: Request to pin
1095 *
1096 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001097 * so should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001098 */
1099void xprt_pin_rqst(struct rpc_rqst *req)
1100{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001101 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001102}
Chuck Lever9590d082017-08-23 17:05:58 -04001103EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001104
1105/**
1106 * xprt_unpin_rqst - Unpin a request on the transport receive list
1107 * @req: Request to pin
1108 *
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001109 * Caller should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001110 */
1111void xprt_unpin_rqst(struct rpc_rqst *req)
1112{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001113 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1114 atomic_dec(&req->rq_pin);
1115 return;
1116 }
1117 if (atomic_dec_and_test(&req->rq_pin))
1118 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001119}
Chuck Lever9590d082017-08-23 17:05:58 -04001120EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001121
1122static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -04001123{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001124 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -04001125}
1126
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001127static bool
1128xprt_request_data_received(struct rpc_task *task)
1129{
1130 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1131 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1132}
1133
1134static bool
1135xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1136{
1137 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1138 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1139}
1140
1141/**
1142 * xprt_request_enqueue_receive - Add an request to the receive queue
1143 * @task: RPC task
1144 *
1145 */
1146void
1147xprt_request_enqueue_receive(struct rpc_task *task)
1148{
1149 struct rpc_rqst *req = task->tk_rqstp;
1150 struct rpc_xprt *xprt = req->rq_xprt;
1151
1152 if (!xprt_request_need_enqueue_receive(task, req))
1153 return;
Trond Myklebust75369082019-07-17 21:22:38 -04001154
1155 xprt_request_prepare(task->tk_rqstp);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001156 spin_lock(&xprt->queue_lock);
1157
1158 /* Update the softirq receive buffer */
1159 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1160 sizeof(req->rq_private_buf));
1161
1162 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001163 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001164 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1165 spin_unlock(&xprt->queue_lock);
1166
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001167 /* Turn off autodisconnect */
1168 del_singleshot_timer_sync(&xprt->timer);
1169}
1170
1171/**
1172 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1173 * @task: RPC task
1174 *
1175 * Caller must hold xprt->queue_lock.
1176 */
1177static void
1178xprt_request_dequeue_receive_locked(struct rpc_task *task)
1179{
Trond Myklebust95f76912018-09-07 08:35:22 -04001180 struct rpc_rqst *req = task->tk_rqstp;
1181
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001182 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001183 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001184}
1185
Chuck Leverecd465e2018-03-05 15:12:57 -05001186/**
1187 * xprt_update_rtt - Update RPC RTT statistics
1188 * @task: RPC request that recently completed
1189 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001190 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001191 */
1192void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001194 struct rpc_rqst *req = task->tk_rqstp;
1195 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001196 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001197 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Chuck Lever1570c1e2005-08-25 16:25:52 -07001199 if (timer) {
1200 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001201 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001202 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001204}
Chuck Leverecd465e2018-03-05 15:12:57 -05001205EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Chuck Lever1570c1e2005-08-25 16:25:52 -07001207/**
1208 * xprt_complete_rqst - called when reply processing is complete
1209 * @task: RPC request that recently completed
1210 * @copied: actual number of bytes received from the transport
1211 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001212 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001213 */
1214void xprt_complete_rqst(struct rpc_task *task, int copied)
1215{
1216 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001217 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Trond Myklebustfda13932008-02-22 16:34:12 -05001219 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001220
Trond Myklebust1e799b62008-03-21 16:19:41 -04001221 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001222 /* Ensure all writes are done before we update */
1223 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001224 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001225 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001226 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001227 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001229EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Chuck Lever46c0ee82005-08-25 16:25:52 -07001231static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001233 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct rpc_xprt *xprt = req->rq_xprt;
1235
Trond Myklebust5d008372008-02-22 16:34:17 -05001236 if (task->tk_status != -ETIMEDOUT)
1237 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001238
Chuck Lever82476d92018-01-03 15:38:25 -05001239 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001240 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001241 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001242 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001243 } else
1244 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Chuck Lever9903cd12005-08-11 16:25:26 -04001247/**
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001248 * xprt_wait_for_reply_request_def - wait for reply
1249 * @task: pointer to rpc_task
1250 *
1251 * Set a request's retransmit timeout based on the transport's
1252 * default timeout parameters. Used by transports that don't adjust
1253 * the retransmit timeout based on round-trip time estimation,
1254 * and put the task to sleep on the pending queue.
1255 */
1256void xprt_wait_for_reply_request_def(struct rpc_task *task)
1257{
1258 struct rpc_rqst *req = task->tk_rqstp;
1259
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001260 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
Trond Myklebust9e910bf2019-04-07 13:58:53 -04001261 xprt_request_timeout(req));
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001262}
1263EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1264
1265/**
1266 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1267 * @task: pointer to rpc_task
1268 *
1269 * Set a request's retransmit timeout using the RTT estimator,
1270 * and put the task to sleep on the pending queue.
1271 */
1272void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1273{
1274 int timer = task->tk_msg.rpc_proc->p_timer;
1275 struct rpc_clnt *clnt = task->tk_client;
1276 struct rpc_rtt *rtt = clnt->cl_rtt;
1277 struct rpc_rqst *req = task->tk_rqstp;
1278 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001279 unsigned long timeout;
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001280
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001281 timeout = rpc_calc_rto(rtt, timer);
1282 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1283 if (timeout > max_timeout || timeout == 0)
1284 timeout = max_timeout;
1285 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1286 jiffies + timeout);
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001287}
1288EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1289
1290/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001291 * xprt_request_wait_receive - wait for the reply to an RPC request
1292 * @task: RPC task about to send a request
1293 *
1294 */
1295void xprt_request_wait_receive(struct rpc_task *task)
1296{
1297 struct rpc_rqst *req = task->tk_rqstp;
1298 struct rpc_xprt *xprt = req->rq_xprt;
1299
1300 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1301 return;
1302 /*
1303 * Sleep on the pending queue if we're expecting a reply.
1304 * The spinlock ensures atomicity between the test of
1305 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1306 */
1307 spin_lock(&xprt->queue_lock);
1308 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001309 xprt->ops->wait_for_reply_request(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001310 /*
1311 * Send an extra queue wakeup call if the
1312 * connection was dropped in case the call to
1313 * rpc_sleep_on() raced.
1314 */
1315 if (xprt_request_retransmit_after_disconnect(task))
1316 rpc_wake_up_queued_task_set_status(&xprt->pending,
1317 task, -ENOTCONN);
1318 }
1319 spin_unlock(&xprt->queue_lock);
1320}
1321
Trond Myklebust944b0422018-08-09 23:33:21 -04001322static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001323xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1324{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001325 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001326}
1327
1328/**
1329 * xprt_request_enqueue_transmit - queue a task for transmission
1330 * @task: pointer to rpc_task
1331 *
1332 * Add a task to the transmission queue.
1333 */
1334void
1335xprt_request_enqueue_transmit(struct rpc_task *task)
1336{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001337 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001338 struct rpc_xprt *xprt = req->rq_xprt;
1339
1340 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001341 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001342 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001343 /*
1344 * Requests that carry congestion control credits are added
1345 * to the head of the list to avoid starvation issues.
1346 */
1347 if (req->rq_cong) {
1348 xprt_clear_congestion_window_wait(xprt);
1349 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1350 if (pos->rq_cong)
1351 continue;
1352 /* Note: req is added _before_ pos */
1353 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1354 INIT_LIST_HEAD(&req->rq_xmit2);
1355 goto out;
1356 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001357 } else if (RPC_IS_SWAPPER(task)) {
1358 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1359 if (pos->rq_cong || pos->rq_bytes_sent)
1360 continue;
1361 if (RPC_IS_SWAPPER(pos->rq_task))
1362 continue;
1363 /* Note: req is added _before_ pos */
1364 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1365 INIT_LIST_HEAD(&req->rq_xmit2);
1366 goto out;
1367 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001368 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001369 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1370 if (pos->rq_task->tk_owner != task->tk_owner)
1371 continue;
1372 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1373 INIT_LIST_HEAD(&req->rq_xmit);
1374 goto out;
1375 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001376 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001377 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001378 INIT_LIST_HEAD(&req->rq_xmit2);
1379out:
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001380 atomic_long_inc(&xprt->xmit_queuelen);
Trond Myklebust944b0422018-08-09 23:33:21 -04001381 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1382 spin_unlock(&xprt->queue_lock);
1383 }
1384}
1385
1386/**
1387 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1388 * @task: pointer to rpc_task
1389 *
1390 * Remove a task from the transmission queue
1391 * Caller must hold xprt->queue_lock
1392 */
1393static void
1394xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1395{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001396 struct rpc_rqst *req = task->tk_rqstp;
1397
1398 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1399 return;
1400 if (!list_empty(&req->rq_xmit)) {
1401 list_del(&req->rq_xmit);
1402 if (!list_empty(&req->rq_xmit2)) {
1403 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1404 struct rpc_rqst, rq_xmit2);
1405 list_del(&req->rq_xmit2);
1406 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1407 }
1408 } else
1409 list_del(&req->rq_xmit2);
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001410 atomic_long_dec(&req->rq_xprt->xmit_queuelen);
Trond Myklebust944b0422018-08-09 23:33:21 -04001411}
1412
1413/**
1414 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1415 * @task: pointer to rpc_task
1416 *
1417 * Remove a task from the transmission queue
1418 */
1419static void
1420xprt_request_dequeue_transmit(struct rpc_task *task)
1421{
1422 struct rpc_rqst *req = task->tk_rqstp;
1423 struct rpc_xprt *xprt = req->rq_xprt;
1424
1425 spin_lock(&xprt->queue_lock);
1426 xprt_request_dequeue_transmit_locked(task);
1427 spin_unlock(&xprt->queue_lock);
1428}
1429
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001430/**
Trond Myklebustcc204d02019-09-10 13:01:35 -04001431 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1432 * @task: pointer to rpc_task
1433 *
1434 * Remove a task from the transmit and receive queues, and ensure that
1435 * it is not pinned by the receive work item.
1436 */
1437void
1438xprt_request_dequeue_xprt(struct rpc_task *task)
1439{
1440 struct rpc_rqst *req = task->tk_rqstp;
1441 struct rpc_xprt *xprt = req->rq_xprt;
1442
1443 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1444 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1445 xprt_is_pinned_rqst(req)) {
1446 spin_lock(&xprt->queue_lock);
1447 xprt_request_dequeue_transmit_locked(task);
1448 xprt_request_dequeue_receive_locked(task);
1449 while (xprt_is_pinned_rqst(req)) {
1450 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1451 spin_unlock(&xprt->queue_lock);
1452 xprt_wait_on_pinned_rqst(req);
1453 spin_lock(&xprt->queue_lock);
1454 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1455 }
1456 spin_unlock(&xprt->queue_lock);
1457 }
1458}
1459
1460/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001461 * xprt_request_prepare - prepare an encoded request for transport
1462 * @req: pointer to rpc_rqst
1463 *
1464 * Calls into the transport layer to do whatever is needed to prepare
1465 * the request for transmission or receive.
1466 */
1467void
1468xprt_request_prepare(struct rpc_rqst *req)
1469{
1470 struct rpc_xprt *xprt = req->rq_xprt;
1471
1472 if (xprt->ops->prepare_request)
1473 xprt->ops->prepare_request(req);
1474}
1475
1476/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001477 * xprt_request_need_retransmit - Test if a task needs retransmission
1478 * @task: pointer to rpc_task
1479 *
1480 * Test for whether a connection breakage requires the task to retransmit
1481 */
1482bool
1483xprt_request_need_retransmit(struct rpc_task *task)
1484{
1485 return xprt_request_retransmit_after_disconnect(task);
1486}
1487
1488/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001489 * xprt_prepare_transmit - reserve the transport before sending a request
1490 * @task: RPC task about to send a request
1491 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001493bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct rpc_rqst *req = task->tk_rqstp;
1496 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001498 if (!xprt_lock_write(xprt, task)) {
1499 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001500 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001501 rpc_wake_up_queued_task_set_status(&xprt->sending,
1502 task, 0);
1503 return false;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001506 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001509void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001510{
Chuck Lever7638e0b2021-03-31 13:22:14 -04001511 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1512
1513 xprt_inject_disconnect(xprt);
1514 xprt_release_write(xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001515}
1516
Chuck Lever9903cd12005-08-11 16:25:26 -04001517/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001518 * xprt_request_transmit - send an RPC request on a transport
1519 * @req: pointer to request to transmit
1520 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001521 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001522 * This performs the transmission of a single request.
1523 * Note that if the request is not the same as snd_task, then it
1524 * does need to be pinned.
1525 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001526 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001527static int
1528xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001530 struct rpc_xprt *xprt = req->rq_xprt;
1531 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001532 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001533 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001534 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001536 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001537 if (xprt_request_data_received(task)) {
1538 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001539 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001540 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001541 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001542 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001543 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001544 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001545 }
Trond Myklebustae67bd32019-04-07 13:58:44 -04001546 if (RPC_SIGNALLED(task)) {
1547 status = -ERESTARTSYS;
1548 goto out_dequeue;
1549 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001552 /*
1553 * Update req->rq_ntrans before transmitting to avoid races with
1554 * xprt_update_rtt(), which needs to know that it is recording a
1555 * reply to the first transmission.
1556 */
1557 req->rq_ntrans++;
1558
Chuck Leverc509f15a2020-05-12 17:13:28 -04001559 trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001560 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001561 status = xprt->ops->send_request(req);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001562 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001563 req->rq_ntrans--;
Chuck Lever0c776682019-02-11 11:25:04 -05001564 trace_xprt_transmit(req, status);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001565 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001566 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001567
Chuck Levere936a592021-03-31 13:22:27 -04001568 if (is_retrans) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001569 task->tk_client->cl_stats->rpcretrans++;
Chuck Levere936a592021-03-31 13:22:27 -04001570 trace_xprt_retransmit(req);
1571 }
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001572
Chuck Lever4a068252015-05-11 14:02:25 -04001573 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Bryan Schumaker468f8612011-04-18 15:57:32 -04001575 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001576 spin_lock(&xprt->transport_lock);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001577
Trond Myklebustc8485e42009-03-11 14:37:59 -04001578 xprt->stat.sends++;
1579 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1580 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001581 xprt->stat.sending_u += xprt->sending.qlen;
1582 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001583 spin_unlock(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001584
1585 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001586out_dequeue:
Chuck Lever0c776682019-02-11 11:25:04 -05001587 trace_xprt_transmit(req, status);
Trond Myklebust944b0422018-08-09 23:33:21 -04001588 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001589 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1590 return status;
1591}
1592
1593/**
1594 * xprt_transmit - send an RPC request on a transport
1595 * @task: controlling RPC task
1596 *
1597 * Attempts to drain the transmit queue. On exit, either the transport
1598 * signalled an error that needs to be handled before transmission can
1599 * resume, or @task finished transmitting, and detected that it already
1600 * received a reply.
1601 */
1602void
1603xprt_transmit(struct rpc_task *task)
1604{
1605 struct rpc_rqst *next, *req = task->tk_rqstp;
1606 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustea7a1012021-07-12 09:52:59 -04001607 int status;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001608
1609 spin_lock(&xprt->queue_lock);
Trond Myklebustea7a1012021-07-12 09:52:59 -04001610 for (;;) {
1611 next = list_first_entry_or_null(&xprt->xmit_queue,
1612 struct rpc_rqst, rq_xmit);
1613 if (!next)
Chuck Lever6f9f1722020-07-08 16:09:53 -04001614 break;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001615 xprt_pin_rqst(next);
1616 spin_unlock(&xprt->queue_lock);
1617 status = xprt_request_transmit(next, task);
1618 if (status == -EBADMSG && next != req)
1619 status = 0;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001620 spin_lock(&xprt->queue_lock);
1621 xprt_unpin_rqst(next);
Trond Myklebustea7a1012021-07-12 09:52:59 -04001622 if (status < 0) {
1623 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1624 task->tk_status = status;
1625 break;
1626 }
1627 /* Was @task transmitted, and has it received a reply? */
1628 if (xprt_request_data_received(task) &&
1629 !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1630 break;
1631 cond_resched_lock(&xprt->queue_lock);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001632 }
1633 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001636static void xprt_complete_request_init(struct rpc_task *task)
1637{
1638 if (task->tk_rqstp)
1639 xprt_request_init(task);
1640}
1641
1642void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
Trond Myklebustba60eb22013-04-14 10:49:37 -04001643{
1644 set_bit(XPRT_CONGESTED, &xprt->state);
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001645 rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001646}
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001647EXPORT_SYMBOL_GPL(xprt_add_backlog);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001648
NeilBrowne877a882021-05-17 09:59:10 +10001649static bool __xprt_set_rq(struct rpc_task *task, void *data)
Trond Myklebustba60eb22013-04-14 10:49:37 -04001650{
NeilBrowne877a882021-05-17 09:59:10 +10001651 struct rpc_rqst *req = data;
1652
1653 if (task->tk_rqstp == NULL) {
1654 memset(req, 0, sizeof(*req)); /* mark unused */
NeilBrowne877a882021-05-17 09:59:10 +10001655 task->tk_rqstp = req;
1656 return true;
1657 }
1658 return false;
1659}
1660
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001661bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
NeilBrowne877a882021-05-17 09:59:10 +10001662{
1663 if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
Trond Myklebustba60eb22013-04-14 10:49:37 -04001664 clear_bit(XPRT_CONGESTED, &xprt->state);
NeilBrowne877a882021-05-17 09:59:10 +10001665 return false;
1666 }
1667 return true;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001668}
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001669EXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001670
1671static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1672{
1673 bool ret = false;
1674
1675 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1676 goto out;
1677 spin_lock(&xprt->reserve_lock);
1678 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001679 xprt_add_backlog(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001680 ret = true;
1681 }
1682 spin_unlock(&xprt->reserve_lock);
1683out:
1684 return ret;
1685}
1686
Trond Myklebust92ea0112017-06-20 19:35:39 -04001687static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001688{
1689 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1690
Chuck Leverff699ea82018-03-05 15:13:13 -05001691 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001692 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001693 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001694 spin_unlock(&xprt->reserve_lock);
1695 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1696 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001697 if (req != NULL)
1698 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001699 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001700 req = ERR_PTR(-ENOMEM);
1701out:
1702 return req;
1703}
1704
1705static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1706{
Chuck Leverff699ea82018-03-05 15:13:13 -05001707 if (xprt->num_reqs > xprt->min_reqs) {
1708 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001709 kfree(req);
1710 return true;
1711 }
1712 return false;
1713}
1714
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001715void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001717 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001719 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001721 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1722 list_del(&req->rq_list);
1723 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001725 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001726 if (!IS_ERR(req))
1727 goto out_init_req;
1728 switch (PTR_ERR(req)) {
1729 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001730 dprintk("RPC: dynamic allocation of request slot "
1731 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001732 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001733 break;
1734 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001735 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001736 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001737 fallthrough;
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001738 default:
1739 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001740 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001741 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001742 return;
1743out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001744 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1745 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001746 spin_unlock(&xprt->reserve_lock);
1747
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001748 task->tk_status = 0;
1749 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001751EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1752
Chuck Levera9cde232018-05-04 15:34:59 -04001753void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001754{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001755 spin_lock(&xprt->reserve_lock);
NeilBrowne877a882021-05-17 09:59:10 +10001756 if (!xprt_wake_up_backlog(xprt, req) &&
1757 !xprt_dynamic_free_slot(xprt, req)) {
Trond Myklebustc25573b2011-12-01 14:16:17 -05001758 memset(req, 0, sizeof(*req)); /* mark unused */
1759 list_add(&req->rq_list, &xprt->free);
1760 }
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001761 spin_unlock(&xprt->reserve_lock);
1762}
Chuck Levera9cde232018-05-04 15:34:59 -04001763EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001764
Trond Myklebust21de0a92011-07-17 16:57:32 -04001765static void xprt_free_all_slots(struct rpc_xprt *xprt)
1766{
1767 struct rpc_rqst *req;
1768 while (!list_empty(&xprt->free)) {
1769 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1770 list_del(&req->rq_list);
1771 kfree(req);
1772 }
1773}
1774
Olga Kornievskaia572caba2021-06-08 15:59:13 -04001775static DEFINE_IDA(rpc_xprt_ids);
1776
1777void xprt_cleanup_ids(void)
1778{
1779 ida_destroy(&rpc_xprt_ids);
1780}
1781
1782static int xprt_alloc_id(struct rpc_xprt *xprt)
1783{
1784 int id;
1785
1786 id = ida_simple_get(&rpc_xprt_ids, 0, 0, GFP_KERNEL);
1787 if (id < 0)
1788 return id;
1789
1790 xprt->id = id;
1791 return 0;
1792}
1793
1794static void xprt_free_id(struct rpc_xprt *xprt)
1795{
1796 ida_simple_remove(&rpc_xprt_ids, xprt->id);
1797}
1798
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001799struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1800 unsigned int num_prealloc,
1801 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001802{
1803 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001804 struct rpc_rqst *req;
1805 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001806
1807 xprt = kzalloc(size, GFP_KERNEL);
1808 if (xprt == NULL)
1809 goto out;
1810
Olga Kornievskaia572caba2021-06-08 15:59:13 -04001811 xprt_alloc_id(xprt);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001812 xprt_init(xprt, net);
1813
1814 for (i = 0; i < num_prealloc; i++) {
1815 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1816 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001817 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001818 list_add(&req->rq_list, &xprt->free);
1819 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001820 if (max_alloc > num_prealloc)
1821 xprt->max_reqs = max_alloc;
1822 else
1823 xprt->max_reqs = num_prealloc;
1824 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001825 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001826
1827 return xprt;
1828
1829out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001830 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001831out:
1832 return NULL;
1833}
1834EXPORT_SYMBOL_GPL(xprt_alloc);
1835
Pavel Emelyanove204e622010-09-29 16:03:13 +04001836void xprt_free(struct rpc_xprt *xprt)
1837{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001838 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001839 xprt_free_all_slots(xprt);
Olga Kornievskaia572caba2021-06-08 15:59:13 -04001840 xprt_free_id(xprt);
Olga Kornievskaia587bc722021-06-08 15:59:19 -04001841 rpc_sysfs_xprt_destroy(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001842 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001843}
1844EXPORT_SYMBOL_GPL(xprt_free);
1845
Trond Myklebust902c5882018-09-01 17:21:01 -04001846static void
1847xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1848{
1849 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1850}
1851
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001852static __be32
1853xprt_alloc_xid(struct rpc_xprt *xprt)
1854{
1855 __be32 xid;
1856
1857 spin_lock(&xprt->reserve_lock);
1858 xid = (__force __be32)xprt->xid++;
1859 spin_unlock(&xprt->reserve_lock);
1860 return xid;
1861}
1862
1863static void
1864xprt_init_xid(struct rpc_xprt *xprt)
1865{
1866 xprt->xid = prandom_u32();
1867}
1868
1869static void
1870xprt_request_init(struct rpc_task *task)
1871{
1872 struct rpc_xprt *xprt = task->tk_xprt;
1873 struct rpc_rqst *req = task->tk_rqstp;
1874
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001875 req->rq_task = task;
1876 req->rq_xprt = xprt;
1877 req->rq_buffer = NULL;
1878 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001879 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001880 req->rq_snd_buf.len = 0;
1881 req->rq_snd_buf.buflen = 0;
1882 req->rq_rcv_buf.len = 0;
1883 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001884 req->rq_snd_buf.bvec = NULL;
1885 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001886 req->rq_release_snd_buf = NULL;
Trond Myklebustda953062019-04-07 13:58:56 -04001887 xprt_init_majortimeo(task, req);
Chuck Lever09d2ba02020-07-08 16:09:21 -04001888
1889 trace_xprt_reserve(req);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001890}
1891
1892static void
1893xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1894{
1895 xprt->ops->alloc_slot(xprt, task);
1896 if (task->tk_rqstp != NULL)
1897 xprt_request_init(task);
1898}
1899
Chuck Lever9903cd12005-08-11 16:25:26 -04001900/**
1901 * xprt_reserve - allocate an RPC request slot
1902 * @task: RPC task requesting a slot allocation
1903 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001904 * If the transport is marked as being congested, or if no more
1905 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001906 * backlog queue.
1907 */
1908void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001910 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001912 task->tk_status = 0;
1913 if (task->tk_rqstp != NULL)
1914 return;
1915
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001916 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001917 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001918 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001919}
1920
1921/**
1922 * xprt_retry_reserve - allocate an RPC request slot
1923 * @task: RPC task requesting a slot allocation
1924 *
1925 * If no more slots are available, place the task on the transport's
1926 * backlog queue.
1927 * Note that the only difference with xprt_reserve is that we now
1928 * ignore the value of the XPRT_CONGESTED flag.
1929 */
1930void xprt_retry_reserve(struct rpc_task *task)
1931{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001932 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001933
1934 task->tk_status = 0;
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001935 if (task->tk_rqstp != NULL)
Trond Myklebustba60eb22013-04-14 10:49:37 -04001936 return;
1937
Trond Myklebustba60eb22013-04-14 10:49:37 -04001938 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001939 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Chuck Lever9903cd12005-08-11 16:25:26 -04001942/**
1943 * xprt_release - release an RPC request slot
1944 * @task: task which is finished with the slot
1945 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001947void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001949 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001950 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Trond Myklebust87ed5002013-01-07 14:30:46 -05001952 if (req == NULL) {
1953 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001954 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001955 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001958 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001959
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001960 xprt = req->rq_xprt;
Trond Myklebuste86be3a2021-05-25 18:43:38 -04001961 xprt_request_dequeue_xprt(task);
1962 spin_lock(&xprt->transport_lock);
1963 xprt->ops->release_xprt(xprt, task);
1964 if (xprt->ops->release_request)
1965 xprt->ops->release_request(task);
1966 xprt_schedule_autodisconnect(xprt);
1967 spin_unlock(&xprt->transport_lock);
1968 if (req->rq_buffer)
1969 xprt->ops->buf_free(task);
1970 xdr_free_bvec(&req->rq_rcv_buf);
1971 xdr_free_bvec(&req->rq_snd_buf);
1972 if (req->rq_cred != NULL)
1973 put_rpccred(req->rq_cred);
1974 if (req->rq_release_snd_buf)
1975 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001976
NeilBrowne877a882021-05-17 09:59:10 +10001977 task->tk_rqstp = NULL;
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001978 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001979 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001980 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001981 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Trond Myklebust902c5882018-09-01 17:21:01 -04001984#ifdef CONFIG_SUNRPC_BACKCHANNEL
1985void
1986xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1987{
1988 struct xdr_buf *xbufp = &req->rq_snd_buf;
1989
1990 task->tk_rqstp = req;
1991 req->rq_task = task;
1992 xprt_init_connect_cookie(req, req->rq_xprt);
1993 /*
1994 * Set up the xdr_buf length.
1995 * This also indicates that the buffer is XDR encoded already.
1996 */
1997 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1998 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001999}
2000#endif
2001
Trond Myklebust21de0a92011-07-17 16:57:32 -04002002static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04002003{
Trond Myklebust30c51162015-02-24 20:31:39 -05002004 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04002005
2006 spin_lock_init(&xprt->transport_lock);
2007 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04002008 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04002009
2010 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04002011 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04002012 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002013#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04002014 spin_lock_init(&xprt->bc_pa_lock);
2015 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002016#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05002017 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04002018
Chuck Leverc2866762006-08-22 20:06:20 -04002019 xprt->last_used = jiffies;
2020 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04002021 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04002022
2023 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
2024 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04002025 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04002026 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
2027
Chuck Leverc2866762006-08-22 20:06:20 -04002028 xprt_init_xid(xprt);
2029
Trond Myklebust21de0a92011-07-17 16:57:32 -04002030 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04002031}
2032
2033/**
2034 * xprt_create_transport - create an RPC transport
2035 * @args: rpc transport creation arguments
2036 *
2037 */
2038struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
2039{
2040 struct rpc_xprt *xprt;
Trond Myklebust9bccd262020-11-10 12:58:22 -05002041 const struct xprt_class *t;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04002042
Trond Myklebust9bccd262020-11-10 12:58:22 -05002043 t = xprt_class_find_by_ident(args->ident);
2044 if (!t) {
2045 dprintk("RPC: transport (%d) not supported\n", args->ident);
2046 return ERR_PTR(-EIO);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04002047 }
Trond Myklebust8d9266f2011-07-17 16:01:09 -04002048
Trond Myklebust8d9266f2011-07-17 16:01:09 -04002049 xprt = t->setup(args);
Trond Myklebust9bccd262020-11-10 12:58:22 -05002050 xprt_class_release(t);
2051
Chuck Lever911813d2020-05-12 17:13:34 -04002052 if (IS_ERR(xprt))
Trond Myklebust21de0a92011-07-17 16:57:32 -04002053 goto out;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04002054 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
2055 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04002056 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
2057 if (xprt_has_timer(xprt))
Anna Schumaker502980e2019-06-18 14:57:33 -04002058 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04002059 else
Kees Cookff861c42017-10-16 17:29:42 -07002060 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002061
2062 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
2063 xprt_destroy(xprt);
2064 return ERR_PTR(-EINVAL);
2065 }
2066 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
2067 if (xprt->servername == NULL) {
2068 xprt_destroy(xprt);
2069 return ERR_PTR(-ENOMEM);
2070 }
2071
Jeff Layton3f940092015-03-31 12:03:28 -04002072 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05002073
Chuck Lever911813d2020-05-12 17:13:34 -04002074 trace_xprt_create(xprt);
Trond Myklebust21de0a92011-07-17 16:57:32 -04002075out:
Chuck Leverc2866762006-08-22 20:06:20 -04002076 return xprt;
2077}
2078
Trond Myklebust528fd352017-10-19 12:13:10 -04002079static void xprt_destroy_cb(struct work_struct *work)
2080{
2081 struct rpc_xprt *xprt =
2082 container_of(work, struct rpc_xprt, task_cleanup);
2083
Chuck Lever911813d2020-05-12 17:13:34 -04002084 trace_xprt_destroy(xprt);
2085
Trond Myklebust528fd352017-10-19 12:13:10 -04002086 rpc_xprt_debugfs_unregister(xprt);
2087 rpc_destroy_wait_queue(&xprt->binding);
2088 rpc_destroy_wait_queue(&xprt->pending);
2089 rpc_destroy_wait_queue(&xprt->sending);
2090 rpc_destroy_wait_queue(&xprt->backlog);
2091 kfree(xprt->servername);
2092 /*
Trond Myklebust669996a2019-10-17 09:02:21 -04002093 * Destroy any existing back channel
2094 */
2095 xprt_destroy_backchannel(xprt, UINT_MAX);
2096
2097 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04002098 * Tear down transport state and free the rpc_xprt
2099 */
2100 xprt->ops->destroy(xprt);
2101}
2102
Chuck Lever9903cd12005-08-11 16:25:26 -04002103/**
2104 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04002105 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04002106 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04002108static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Trond Myklebust528fd352017-10-19 12:13:10 -04002110 /*
2111 * Exclude transport connect/disconnect handlers and autoclose
2112 */
Trond Myklebust79234c32015-09-18 15:53:24 -04002113 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
2114
Trond Myklebust0065db32006-01-03 09:55:56 +01002115 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002116
2117 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04002118 * Destroy sockets etc from the system workqueue so they can
2119 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04002120 */
Trond Myklebust528fd352017-10-19 12:13:10 -04002121 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2122 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Trond Myklebust30c51162015-02-24 20:31:39 -05002125static void xprt_destroy_kref(struct kref *kref)
2126{
2127 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
2128}
2129
2130/**
2131 * xprt_get - return a reference to an RPC transport.
2132 * @xprt: pointer to the transport
2133 *
2134 */
2135struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
2136{
2137 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
2138 return xprt;
2139 return NULL;
2140}
2141EXPORT_SYMBOL_GPL(xprt_get);
2142
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002143/**
2144 * xprt_put - release a reference to an RPC transport.
2145 * @xprt: pointer to the transport
2146 *
2147 */
2148void xprt_put(struct rpc_xprt *xprt)
2149{
Trond Myklebust30c51162015-02-24 20:31:39 -05002150 if (xprt != NULL)
2151 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002152}
Chuck Lever5d252f92016-01-07 14:50:10 -05002153EXPORT_SYMBOL_GPL(xprt_put);