blob: 1aafe8d3f3f431ce251820ed47058a9203739ef8 [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"
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Local variables
61 */
62
Jeff Laytonf895b252014-11-17 16:58:04 -050063#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064# define RPCDBG_FACILITY RPCDBG_XPRT
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Local functions
69 */
Trond Myklebust21de0a92011-07-17 16:57:32 -040070static void xprt_init(struct rpc_xprt *xprt, struct net *net);
Chuck Lever37ac86c2018-05-04 15:34:53 -040071static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Trond Myklebust4e0038b2012-03-01 17:01:05 -050072static void xprt_destroy(struct rpc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jiri Slaby5ba03e82007-11-22 19:40:22 +080074static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040075static LIST_HEAD(xprt_list);
76
Trond Myklebust9e910bf2019-04-07 13:58:53 -040077static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
78{
79 unsigned long timeout = jiffies + req->rq_timeout;
80
81 if (time_before(timeout, req->rq_majortimeo))
82 return timeout;
83 return req->rq_majortimeo;
84}
85
Chuck Lever12a80462005-08-25 16:25:51 -070086/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040087 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
89 *
90 * If a transport implementation is loaded as a kernel module, it can
91 * call this interface to make itself known to the RPC client.
92 *
93 * Returns:
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
97 */
98int xprt_register_transport(struct xprt_class *transport)
99{
100 struct xprt_class *t;
101 int result;
102
103 result = -EEXIST;
104 spin_lock(&xprt_list_lock);
105 list_for_each_entry(t, &xprt_list, list) {
106 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400107 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400108 goto out;
109 }
110
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400111 list_add_tail(&transport->list, &xprt_list);
112 printk(KERN_INFO "RPC: Registered %s transport module.\n",
113 transport->name);
114 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400115
116out:
117 spin_unlock(&xprt_list_lock);
118 return result;
119}
120EXPORT_SYMBOL_GPL(xprt_register_transport);
121
122/**
123 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800124 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400125 *
126 * Returns:
127 * 0: transport successfully unregistered
128 * -ENOENT: transport never registered
129 */
130int xprt_unregister_transport(struct xprt_class *transport)
131{
132 struct xprt_class *t;
133 int result;
134
135 result = 0;
136 spin_lock(&xprt_list_lock);
137 list_for_each_entry(t, &xprt_list, list) {
138 if (t == transport) {
139 printk(KERN_INFO
140 "RPC: Unregistered %s transport module.\n",
141 transport->name);
142 list_del_init(&transport->list);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400143 goto out;
144 }
145 }
146 result = -ENOENT;
147
148out:
149 spin_unlock(&xprt_list_lock);
150 return result;
151}
152EXPORT_SYMBOL_GPL(xprt_unregister_transport);
153
154/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400155 * xprt_load_transport - load a transport implementation
156 * @transport_name: transport to load
157 *
158 * Returns:
159 * 0: transport successfully loaded
160 * -ENOENT: transport module not available
161 */
162int xprt_load_transport(const char *transport_name)
163{
164 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400165 int result;
166
167 result = 0;
168 spin_lock(&xprt_list_lock);
169 list_for_each_entry(t, &xprt_list, list) {
170 if (strcmp(t->name, transport_name) == 0) {
171 spin_unlock(&xprt_list_lock);
172 goto out;
173 }
174 }
175 spin_unlock(&xprt_list_lock);
Alex Riesenef7ffe8f2010-05-24 14:33:05 -0700176 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400177out:
178 return result;
179}
180EXPORT_SYMBOL_GPL(xprt_load_transport);
181
Trond Myklebustc5445772018-09-03 23:39:27 -0400182static void xprt_clear_locked(struct rpc_xprt *xprt)
183{
184 xprt->snd_task = NULL;
185 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
186 smp_mb__before_atomic();
187 clear_bit(XPRT_LOCKED, &xprt->state);
188 smp_mb__after_atomic();
189 } else
190 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
191}
192
Tom Talpey441e3e22009-03-11 14:37:56 -0400193/**
Chuck Lever12a80462005-08-25 16:25:51 -0700194 * xprt_reserve_xprt - serialize write access to transports
195 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000196 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700197 *
198 * This prevents mixing the payload of separate requests, and prevents
199 * transport connects from colliding with writes. No congestion control
200 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400202int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Chuck Lever12a80462005-08-25 16:25:51 -0700204 struct rpc_rqst *req = task->tk_rqstp;
205
206 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
207 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400208 goto out_locked;
Chuck Lever12a80462005-08-25 16:25:51 -0700209 goto out_sleep;
210 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400211 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
212 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700213 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500214
Chuck Leverbf7ca702019-10-09 12:58:14 -0400215out_locked:
216 trace_xprt_reserve_xprt(xprt, task);
Chuck Lever12a80462005-08-25 16:25:51 -0700217 return 1;
218
Trond Myklebustc5445772018-09-03 23:39:27 -0400219out_unlock:
220 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700221out_sleep:
Chuck Lever12a80462005-08-25 16:25:51 -0700222 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400223 if (RPC_IS_SOFT(task))
224 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400225 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400226 else
227 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700228 return 0;
229}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400230EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700231
Trond Myklebust75891f52018-09-03 17:37:36 -0400232static bool
233xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
234{
235 return test_bit(XPRT_CWND_WAIT, &xprt->state);
236}
237
238static void
239xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
240{
241 if (!list_empty(&xprt->xmit_queue)) {
242 /* Peek at head of queue to see if it can make progress */
243 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
244 rq_xmit)->rq_cong)
245 return;
246 }
247 set_bit(XPRT_CWND_WAIT, &xprt->state);
248}
249
250static void
251xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
252{
253 if (!RPCXPRT_CONGESTED(xprt))
254 clear_bit(XPRT_CWND_WAIT, &xprt->state);
255}
256
Chuck Lever12a80462005-08-25 16:25:51 -0700257/*
258 * xprt_reserve_xprt_cong - serialize write access to transports
259 * @task: task that is requesting access to the transport
260 *
261 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
262 * integrated into the decision of whether a request is allowed to be
263 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400264 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700265 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400266int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct rpc_rqst *req = task->tk_rqstp;
269
Chuck Lever2226feb2005-08-11 16:25:38 -0400270 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400272 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto out_sleep;
274 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400275 if (req == NULL) {
276 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400277 goto out_locked;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400278 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400279 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
280 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400281 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400283 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400285out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100286 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287out_sleep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400289 if (RPC_IS_SOFT(task))
290 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400291 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400292 else
293 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400295out_locked:
296 trace_xprt_reserve_cong(xprt, task);
297 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400299EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Chuck Lever12a80462005-08-25 16:25:51 -0700301static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int retval;
304
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400305 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
306 return 1;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400307 spin_lock(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400308 retval = xprt->ops->reserve_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400309 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return retval;
311}
312
Trond Myklebust961a8282012-01-17 22:57:37 -0500313static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Trond Myklebust961a8282012-01-17 22:57:37 -0500315 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700316
Chuck Lever49e9a892005-08-25 16:25:51 -0700317 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500318 return true;
319}
Chuck Lever49e9a892005-08-25 16:25:51 -0700320
Trond Myklebust961a8282012-01-17 22:57:37 -0500321static void __xprt_lock_write_next(struct rpc_xprt *xprt)
322{
323 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
324 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400325 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
326 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400327 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
328 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500329 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400330out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100331 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700332}
333
Trond Myklebust961a8282012-01-17 22:57:37 -0500334static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
335{
336 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
337 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400338 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
339 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400340 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500341 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400342 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400343 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100346 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Chuck Lever49e9a892005-08-25 16:25:51 -0700349/**
350 * xprt_release_xprt - allow other requests to use a transport
351 * @xprt: transport with other tasks potentially waiting
352 * @task: task that is releasing access to the transport
353 *
354 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700356void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100359 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 __xprt_lock_write_next(xprt);
361 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400362 trace_xprt_release_xprt(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400364EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Chuck Lever49e9a892005-08-25 16:25:51 -0700366/**
367 * xprt_release_xprt_cong - allow other requests to use a transport
368 * @xprt: transport with other tasks potentially waiting
369 * @task: task that is releasing access to the transport
370 *
371 * Note that "task" can be NULL. Another task is awoken to use the
372 * transport if the transport's congestion window allows it.
373 */
374void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
375{
376 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100377 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700378 __xprt_lock_write_next_cong(xprt);
379 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400380 trace_xprt_release_cong(xprt, task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700381}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400382EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700383
384static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400386 if (xprt->snd_task != task)
387 return;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400388 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700389 xprt->ops->release_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400390 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * Van Jacobson congestion avoidance. Check if the congestion window
395 * overflowed. Put the task to sleep if this is the case.
396 */
397static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400398__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (req->rq_cong)
401 return 1;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400402 trace_xprt_get_cong(xprt, req->rq_task);
Trond Myklebust75891f52018-09-03 17:37:36 -0400403 if (RPCXPRT_CONGESTED(xprt)) {
404 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 req->rq_cong = 1;
408 xprt->cong += RPC_CWNDSCALE;
409 return 1;
410}
411
412/*
413 * Adjust the congestion window, and wake up the next task
414 * that has been sleeping due to congestion
415 */
416static void
417__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
418{
419 if (!req->rq_cong)
420 return;
421 req->rq_cong = 0;
422 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400423 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Leverbf7ca702019-10-09 12:58:14 -0400424 trace_xprt_put_cong(xprt, req->rq_task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700425 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Chuck Lever46c0ee82005-08-25 16:25:52 -0700428/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400429 * xprt_request_get_cong - Request congestion control credits
430 * @xprt: pointer to transport
431 * @req: pointer to RPC request
432 *
433 * Useful for transports that require congestion control.
434 */
435bool
436xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
437{
438 bool ret = false;
439
440 if (req->rq_cong)
441 return true;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400442 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400443 ret = __xprt_get_cong(xprt, req) != 0;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400444 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400445 return ret;
446}
447EXPORT_SYMBOL_GPL(xprt_request_get_cong);
448
449/**
Chuck Levera58dd392005-08-25 16:25:53 -0700450 * xprt_release_rqst_cong - housekeeping when request is complete
451 * @task: RPC request that recently completed
452 *
453 * Useful for transports that require congestion control.
454 */
455void xprt_release_rqst_cong(struct rpc_task *task)
456{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500457 struct rpc_rqst *req = task->tk_rqstp;
458
459 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700460}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400461EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700462
Chuck Lever8593e012019-09-13 16:01:07 -0400463static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
464{
465 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
466 __xprt_lock_write_next_cong(xprt);
467}
468
Trond Myklebust75891f52018-09-03 17:37:36 -0400469/*
470 * Clear the congestion window wait flag and wake up the next
471 * entry on xprt->sending
472 */
473static void
474xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
475{
476 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
Trond Myklebustb5e92412019-05-02 11:21:08 -0400477 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400478 __xprt_lock_write_next_cong(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400479 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400480 }
481}
482
Chuck Levera58dd392005-08-25 16:25:53 -0700483/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700484 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500485 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700486 * @task: recently completed RPC request used to adjust window
487 * @result: result code of completed RPC request
488 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400489 * The transport code maintains an estimate on the maximum number of out-
490 * standing RPC requests, using a smoothed version of the congestion
491 * avoidance implemented in 44BSD. This is basically the Van Jacobson
492 * congestion algorithm: If a retransmit occurs, the congestion window is
493 * halved; otherwise, it is incremented by 1/cwnd when
494 *
495 * - a reply is received and
496 * - a full number of requests are outstanding and
497 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500499void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700501 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700502 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (result >= 0 && cwnd <= xprt->cong) {
505 /* The (cwnd >> 1) term makes sure
506 * the result gets rounded properly. */
507 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
508 if (cwnd > RPC_MAXCWND(xprt))
509 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700510 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (result == -ETIMEDOUT) {
512 cwnd >>= 1;
513 if (cwnd < RPC_CWNDSCALE)
514 cwnd = RPC_CWNDSCALE;
515 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500516 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xprt->cong, xprt->cwnd, cwnd);
518 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700519 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400521EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Chuck Lever44fbac22005-08-11 16:25:44 -0400523/**
524 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
525 * @xprt: transport with waiting tasks
526 * @status: result code to plant in each task before waking it
527 *
528 */
529void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
530{
531 if (status < 0)
532 rpc_wake_up_status(&xprt->pending, status);
533 else
534 rpc_wake_up(&xprt->pending);
535}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400536EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400537
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400538/**
539 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400540 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500541 *
542 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
543 * we don't in general want to force a socket disconnection due to
544 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400545 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400546void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400547{
Trond Myklebustc5445772018-09-03 23:39:27 -0400548 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400549}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400550EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400551
Trond Myklebustc5445772018-09-03 23:39:27 -0400552static bool
553xprt_clear_write_space_locked(struct rpc_xprt *xprt)
554{
555 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
556 __xprt_lock_write_next(xprt);
557 dprintk("RPC: write space: waking waiting task on "
558 "xprt %p\n", xprt);
559 return true;
560 }
561 return false;
562}
563
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400564/**
565 * xprt_write_space - wake the task waiting for transport output buffer space
566 * @xprt: transport with waiting tasks
567 *
568 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
569 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400570bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400571{
Trond Myklebustc5445772018-09-03 23:39:27 -0400572 bool ret;
573
574 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
575 return false;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400576 spin_lock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400577 ret = xprt_clear_write_space_locked(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400578 spin_unlock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400579 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400580}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400581EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400582
Trond Myklebustda953062019-04-07 13:58:56 -0400583static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
584{
585 s64 delta = ktime_to_ns(ktime_get() - abstime);
586 return likely(delta >= 0) ?
587 jiffies - nsecs_to_jiffies(delta) :
588 jiffies + nsecs_to_jiffies(-delta);
589}
590
591static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500593 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Trond Myklebustda953062019-04-07 13:58:56 -0400594 unsigned long majortimeo = req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (to->to_exponential)
Trond Myklebustda953062019-04-07 13:58:56 -0400597 majortimeo <<= to->to_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 else
Trond Myklebustda953062019-04-07 13:58:56 -0400599 majortimeo += to->to_increment * to->to_retries;
600 if (majortimeo > to->to_maxval || majortimeo == 0)
601 majortimeo = to->to_maxval;
602 return majortimeo;
603}
604
605static void xprt_reset_majortimeo(struct rpc_rqst *req)
606{
607 req->rq_majortimeo += xprt_calc_majortimeo(req);
608}
609
610static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
611{
612 unsigned long time_init;
613 struct rpc_xprt *xprt = req->rq_xprt;
614
615 if (likely(xprt && xprt_connected(xprt)))
616 time_init = jiffies;
617 else
618 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
619 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
620 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Chuck Lever9903cd12005-08-11 16:25:26 -0400623/**
624 * xprt_adjust_timeout - adjust timeout values for next retransmit
625 * @req: RPC request containing parameters to use for the adjustment
626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628int xprt_adjust_timeout(struct rpc_rqst *req)
629{
630 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500631 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int status = 0;
633
634 if (time_before(jiffies, req->rq_majortimeo)) {
635 if (to->to_exponential)
636 req->rq_timeout <<= 1;
637 else
638 req->rq_timeout += to->to_increment;
639 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
640 req->rq_timeout = to->to_maxval;
641 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 } else {
643 req->rq_timeout = to->to_initval;
644 req->rq_retries = 0;
645 xprt_reset_majortimeo(req);
646 /* Reset the RTT counters == "slow start" */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400647 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400649 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 status = -ETIMEDOUT;
651 }
652
653 if (req->rq_timeout == 0) {
654 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
655 req->rq_timeout = 5 * HZ;
656 }
657 return status;
658}
659
David Howells65f27f32006-11-22 14:55:48 +0000660static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
David Howells65f27f32006-11-22 14:55:48 +0000662 struct rpc_xprt *xprt =
663 container_of(work, struct rpc_xprt, task_cleanup);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500664 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Trond Myklebust66af1e552007-11-06 10:18:36 -0500666 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400667 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400669 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500670 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Chuck Lever9903cd12005-08-11 16:25:26 -0400673/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500674 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 * @xprt: transport to flag for disconnect
676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500678void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Chuck Lever46121cf2007-01-31 12:14:08 -0500680 dprintk("RPC: disconnected transport %p\n", xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400681 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400683 xprt_clear_write_space_locked(xprt);
Chuck Lever8593e012019-09-13 16:01:07 -0400684 xprt_clear_congestion_window_wait_locked(xprt);
Trond Myklebust27adc782019-03-15 08:01:16 -0400685 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400686 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500688EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Trond Myklebust66af1e552007-11-06 10:18:36 -0500690/**
691 * xprt_force_disconnect - force a transport to disconnect
692 * @xprt: transport to disconnect
693 *
694 */
695void xprt_force_disconnect(struct rpc_xprt *xprt)
696{
697 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400698 spin_lock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500699 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
700 /* Try to schedule an autoclose RPC call */
701 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400702 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust0445f922018-12-17 13:34:59 -0500703 else if (xprt->snd_task)
704 rpc_wake_up_queued_task_set_status(&xprt->pending,
705 xprt->snd_task, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400706 spin_unlock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500707}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400708EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500709
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400710static unsigned int
711xprt_connect_cookie(struct rpc_xprt *xprt)
712{
713 return READ_ONCE(xprt->connect_cookie);
714}
715
716static bool
717xprt_request_retransmit_after_disconnect(struct rpc_task *task)
718{
719 struct rpc_rqst *req = task->tk_rqstp;
720 struct rpc_xprt *xprt = req->rq_xprt;
721
722 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
723 !xprt_connected(xprt);
724}
725
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400726/**
727 * xprt_conditional_disconnect - force a transport to disconnect
728 * @xprt: transport to disconnect
729 * @cookie: 'connection cookie'
730 *
731 * This attempts to break the connection if and only if 'cookie' matches
732 * the current transport 'connection cookie'. It ensures that we don't
733 * try to break the connection more than once when we need to retransmit
734 * a batch of RPC requests.
735 *
736 */
737void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
738{
739 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400740 spin_lock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400741 if (cookie != xprt->connect_cookie)
742 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100743 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400744 goto out;
745 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
746 /* Try to schedule an autoclose RPC call */
747 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400748 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400749 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400750out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400751 spin_unlock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400752}
753
Trond Myklebustad3331a2016-08-02 13:47:43 -0400754static bool
755xprt_has_timer(const struct rpc_xprt *xprt)
756{
757 return xprt->idle_timeout != 0;
758}
759
760static void
761xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
762 __must_hold(&xprt->transport_lock)
763{
Dave Wysochanski80d3c452019-06-26 16:30:24 -0400764 xprt->last_used = jiffies;
Trond Myklebust95f76912018-09-07 08:35:22 -0400765 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400766 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static void
Kees Cookff861c42017-10-16 17:29:42 -0700770xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Kees Cookff861c42017-10-16 17:29:42 -0700772 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust95f76912018-09-07 08:35:22 -0400774 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400775 return;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400776 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
777 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400778 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400779 return;
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400780 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500783bool xprt_lock_connect(struct rpc_xprt *xprt,
784 struct rpc_task *task,
785 void *cookie)
786{
787 bool ret = false;
788
Trond Myklebustb5e92412019-05-02 11:21:08 -0400789 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500790 if (!test_bit(XPRT_LOCKED, &xprt->state))
791 goto out;
792 if (xprt->snd_task != task)
793 goto out;
794 xprt->snd_task = cookie;
795 ret = true;
796out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400797 spin_unlock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500798 return ret;
799}
800
801void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
802{
Trond Myklebustb5e92412019-05-02 11:21:08 -0400803 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500804 if (xprt->snd_task != cookie)
805 goto out;
806 if (!test_bit(XPRT_LOCKED, &xprt->state))
807 goto out;
808 xprt->snd_task =NULL;
809 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400810 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500811out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400812 spin_unlock(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400813 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500814}
815
Chuck Lever9903cd12005-08-11 16:25:26 -0400816/**
817 * xprt_connect - schedule a transport connect operation
818 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 */
821void xprt_connect(struct rpc_task *task)
822{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500823 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Chuck Lever46121cf2007-01-31 12:14:08 -0500825 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xprt, (xprt_connected(xprt) ? "is" : "is not"));
827
Chuck Leverec739ef2006-08-22 20:06:15 -0400828 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400829 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return;
831 }
832 if (!xprt_lock_write(xprt, task))
833 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500834
835 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
836 xprt->ops->close(xprt);
837
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500838 if (!xprt_connected(xprt)) {
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100839 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400840 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400841 xprt_request_timeout(task->tk_rqstp));
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400842
843 if (test_bit(XPRT_CLOSING, &xprt->state))
844 return;
845 if (xprt_test_and_set_connecting(xprt))
846 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500847 /* Race breaker */
848 if (!xprt_connected(xprt)) {
849 xprt->stat.connect_start = jiffies;
850 xprt->ops->connect(xprt, task);
851 } else {
852 xprt_clear_connecting(xprt);
853 task->tk_status = 0;
854 rpc_wake_up_queued_task(&xprt->pending, task);
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500857 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Chuck Lever675dd902019-06-19 10:33:42 -0400860/**
861 * xprt_reconnect_delay - compute the wait before scheduling a connect
862 * @xprt: transport instance
863 *
864 */
865unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
866{
867 unsigned long start, now = jiffies;
868
869 start = xprt->stat.connect_start + xprt->reestablish_timeout;
870 if (time_after(start, now))
871 return start - now;
872 return 0;
873}
874EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
875
876/**
877 * xprt_reconnect_backoff - compute the new re-establish timeout
878 * @xprt: transport instance
879 * @init_to: initial reestablish timeout
880 *
881 */
882void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
883{
884 xprt->reestablish_timeout <<= 1;
885 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
886 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
887 if (xprt->reestablish_timeout < init_to)
888 xprt->reestablish_timeout = init_to;
889}
890EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
891
Trond Myklebust95f76912018-09-07 08:35:22 -0400892enum xprt_xid_rb_cmp {
893 XID_RB_EQUAL,
894 XID_RB_LEFT,
895 XID_RB_RIGHT,
896};
897static enum xprt_xid_rb_cmp
898xprt_xid_cmp(__be32 xid1, __be32 xid2)
899{
900 if (xid1 == xid2)
901 return XID_RB_EQUAL;
902 if ((__force u32)xid1 < (__force u32)xid2)
903 return XID_RB_LEFT;
904 return XID_RB_RIGHT;
905}
906
907static struct rpc_rqst *
908xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
909{
910 struct rb_node *n = xprt->recv_queue.rb_node;
911 struct rpc_rqst *req;
912
913 while (n != NULL) {
914 req = rb_entry(n, struct rpc_rqst, rq_recv);
915 switch (xprt_xid_cmp(xid, req->rq_xid)) {
916 case XID_RB_LEFT:
917 n = n->rb_left;
918 break;
919 case XID_RB_RIGHT:
920 n = n->rb_right;
921 break;
922 case XID_RB_EQUAL:
923 return req;
924 }
925 }
926 return NULL;
927}
928
929static void
930xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
931{
932 struct rb_node **p = &xprt->recv_queue.rb_node;
933 struct rb_node *n = NULL;
934 struct rpc_rqst *req;
935
936 while (*p != NULL) {
937 n = *p;
938 req = rb_entry(n, struct rpc_rqst, rq_recv);
939 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
940 case XID_RB_LEFT:
941 p = &n->rb_left;
942 break;
943 case XID_RB_RIGHT:
944 p = &n->rb_right;
945 break;
946 case XID_RB_EQUAL:
947 WARN_ON_ONCE(new != req);
948 return;
949 }
950 }
951 rb_link_node(&new->rq_recv, n, p);
952 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
953}
954
955static void
956xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
957{
958 rb_erase(&req->rq_recv, &xprt->recv_queue);
959}
960
Chuck Lever9903cd12005-08-11 16:25:26 -0400961/**
962 * xprt_lookup_rqst - find an RPC request corresponding to an XID
963 * @xprt: transport on which the original request was transmitted
964 * @xid: RPC XID of incoming reply
965 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400966 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700968struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400970 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Trond Myklebust95f76912018-09-07 08:35:22 -0400972 entry = xprt_request_rb_find(xprt, xid);
973 if (entry != NULL) {
974 trace_xprt_lookup_rqst(xprt, xid, 0);
975 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
976 return entry;
977 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500978
979 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
980 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400981 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500982 xprt->stat.bad_xids++;
983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400985EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400987static bool
988xprt_is_pinned_rqst(struct rpc_rqst *req)
989{
990 return atomic_read(&req->rq_pin) != 0;
991}
992
Trond Myklebust729749b2017-08-13 10:03:59 -0400993/**
994 * xprt_pin_rqst - Pin a request on the transport receive list
995 * @req: Request to pin
996 *
997 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Chuck Lever1f7d1c72019-04-24 09:40:09 -0400998 * so should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400999 */
1000void xprt_pin_rqst(struct rpc_rqst *req)
1001{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001002 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001003}
Chuck Lever9590d082017-08-23 17:05:58 -04001004EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001005
1006/**
1007 * xprt_unpin_rqst - Unpin a request on the transport receive list
1008 * @req: Request to pin
1009 *
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001010 * Caller should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001011 */
1012void xprt_unpin_rqst(struct rpc_rqst *req)
1013{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001014 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1015 atomic_dec(&req->rq_pin);
1016 return;
1017 }
1018 if (atomic_dec_and_test(&req->rq_pin))
1019 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001020}
Chuck Lever9590d082017-08-23 17:05:58 -04001021EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001022
1023static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -04001024{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001025 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -04001026}
1027
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001028static bool
1029xprt_request_data_received(struct rpc_task *task)
1030{
1031 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1032 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1033}
1034
1035static bool
1036xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1037{
1038 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1039 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1040}
1041
1042/**
1043 * xprt_request_enqueue_receive - Add an request to the receive queue
1044 * @task: RPC task
1045 *
1046 */
1047void
1048xprt_request_enqueue_receive(struct rpc_task *task)
1049{
1050 struct rpc_rqst *req = task->tk_rqstp;
1051 struct rpc_xprt *xprt = req->rq_xprt;
1052
1053 if (!xprt_request_need_enqueue_receive(task, req))
1054 return;
Trond Myklebust75369082019-07-17 21:22:38 -04001055
1056 xprt_request_prepare(task->tk_rqstp);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001057 spin_lock(&xprt->queue_lock);
1058
1059 /* Update the softirq receive buffer */
1060 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1061 sizeof(req->rq_private_buf));
1062
1063 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001064 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001065 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1066 spin_unlock(&xprt->queue_lock);
1067
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001068 /* Turn off autodisconnect */
1069 del_singleshot_timer_sync(&xprt->timer);
1070}
1071
1072/**
1073 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1074 * @task: RPC task
1075 *
1076 * Caller must hold xprt->queue_lock.
1077 */
1078static void
1079xprt_request_dequeue_receive_locked(struct rpc_task *task)
1080{
Trond Myklebust95f76912018-09-07 08:35:22 -04001081 struct rpc_rqst *req = task->tk_rqstp;
1082
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001083 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001084 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001085}
1086
Chuck Leverecd465e2018-03-05 15:12:57 -05001087/**
1088 * xprt_update_rtt - Update RPC RTT statistics
1089 * @task: RPC request that recently completed
1090 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001091 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001092 */
1093void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001095 struct rpc_rqst *req = task->tk_rqstp;
1096 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001097 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001098 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Chuck Lever1570c1e2005-08-25 16:25:52 -07001100 if (timer) {
1101 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001102 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001103 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001105}
Chuck Leverecd465e2018-03-05 15:12:57 -05001106EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Chuck Lever1570c1e2005-08-25 16:25:52 -07001108/**
1109 * xprt_complete_rqst - called when reply processing is complete
1110 * @task: RPC request that recently completed
1111 * @copied: actual number of bytes received from the transport
1112 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001113 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001114 */
1115void xprt_complete_rqst(struct rpc_task *task, int copied)
1116{
1117 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001118 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Chuck Lever1570c1e2005-08-25 16:25:52 -07001120 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1121 task->tk_pid, ntohl(req->rq_xid), copied);
Jeff Layton3705ad62014-10-28 14:24:13 -04001122 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Trond Myklebustfda13932008-02-22 16:34:12 -05001124 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001125
Trond Myklebust1e799b62008-03-21 16:19:41 -04001126 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001127 /* Ensure all writes are done before we update */
1128 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001129 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001130 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001131 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001132 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001134EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Chuck Lever46c0ee82005-08-25 16:25:52 -07001136static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001138 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct rpc_xprt *xprt = req->rq_xprt;
1140
Trond Myklebust5d008372008-02-22 16:34:17 -05001141 if (task->tk_status != -ETIMEDOUT)
1142 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001143
Chuck Lever82476d92018-01-03 15:38:25 -05001144 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001145 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001146 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001147 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001148 } else
1149 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Chuck Lever9903cd12005-08-11 16:25:26 -04001152/**
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001153 * xprt_wait_for_reply_request_def - wait for reply
1154 * @task: pointer to rpc_task
1155 *
1156 * Set a request's retransmit timeout based on the transport's
1157 * default timeout parameters. Used by transports that don't adjust
1158 * the retransmit timeout based on round-trip time estimation,
1159 * and put the task to sleep on the pending queue.
1160 */
1161void xprt_wait_for_reply_request_def(struct rpc_task *task)
1162{
1163 struct rpc_rqst *req = task->tk_rqstp;
1164
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001165 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
Trond Myklebust9e910bf2019-04-07 13:58:53 -04001166 xprt_request_timeout(req));
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001167}
1168EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1169
1170/**
1171 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1172 * @task: pointer to rpc_task
1173 *
1174 * Set a request's retransmit timeout using the RTT estimator,
1175 * and put the task to sleep on the pending queue.
1176 */
1177void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1178{
1179 int timer = task->tk_msg.rpc_proc->p_timer;
1180 struct rpc_clnt *clnt = task->tk_client;
1181 struct rpc_rtt *rtt = clnt->cl_rtt;
1182 struct rpc_rqst *req = task->tk_rqstp;
1183 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001184 unsigned long timeout;
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001185
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001186 timeout = rpc_calc_rto(rtt, timer);
1187 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1188 if (timeout > max_timeout || timeout == 0)
1189 timeout = max_timeout;
1190 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1191 jiffies + timeout);
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001192}
1193EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1194
1195/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001196 * xprt_request_wait_receive - wait for the reply to an RPC request
1197 * @task: RPC task about to send a request
1198 *
1199 */
1200void xprt_request_wait_receive(struct rpc_task *task)
1201{
1202 struct rpc_rqst *req = task->tk_rqstp;
1203 struct rpc_xprt *xprt = req->rq_xprt;
1204
1205 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1206 return;
1207 /*
1208 * Sleep on the pending queue if we're expecting a reply.
1209 * The spinlock ensures atomicity between the test of
1210 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1211 */
1212 spin_lock(&xprt->queue_lock);
1213 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001214 xprt->ops->wait_for_reply_request(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001215 /*
1216 * Send an extra queue wakeup call if the
1217 * connection was dropped in case the call to
1218 * rpc_sleep_on() raced.
1219 */
1220 if (xprt_request_retransmit_after_disconnect(task))
1221 rpc_wake_up_queued_task_set_status(&xprt->pending,
1222 task, -ENOTCONN);
1223 }
1224 spin_unlock(&xprt->queue_lock);
1225}
1226
Trond Myklebust944b0422018-08-09 23:33:21 -04001227static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001228xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1229{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001230 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001231}
1232
1233/**
1234 * xprt_request_enqueue_transmit - queue a task for transmission
1235 * @task: pointer to rpc_task
1236 *
1237 * Add a task to the transmission queue.
1238 */
1239void
1240xprt_request_enqueue_transmit(struct rpc_task *task)
1241{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001242 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001243 struct rpc_xprt *xprt = req->rq_xprt;
1244
1245 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001246 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001247 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001248 /*
1249 * Requests that carry congestion control credits are added
1250 * to the head of the list to avoid starvation issues.
1251 */
1252 if (req->rq_cong) {
1253 xprt_clear_congestion_window_wait(xprt);
1254 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1255 if (pos->rq_cong)
1256 continue;
1257 /* Note: req is added _before_ pos */
1258 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1259 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001260 trace_xprt_enq_xmit(task, 1);
Trond Myklebust75891f52018-09-03 17:37:36 -04001261 goto out;
1262 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001263 } else if (RPC_IS_SWAPPER(task)) {
1264 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1265 if (pos->rq_cong || pos->rq_bytes_sent)
1266 continue;
1267 if (RPC_IS_SWAPPER(pos->rq_task))
1268 continue;
1269 /* Note: req is added _before_ pos */
1270 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1271 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001272 trace_xprt_enq_xmit(task, 2);
Trond Myklebust86aeee02018-09-08 14:22:41 -04001273 goto out;
1274 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001275 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001276 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1277 if (pos->rq_task->tk_owner != task->tk_owner)
1278 continue;
1279 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1280 INIT_LIST_HEAD(&req->rq_xmit);
Chuck Lever0c776682019-02-11 11:25:04 -05001281 trace_xprt_enq_xmit(task, 3);
Trond Myklebust75891f52018-09-03 17:37:36 -04001282 goto out;
1283 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001284 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001285 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001286 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001287 trace_xprt_enq_xmit(task, 4);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001288out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001289 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1290 spin_unlock(&xprt->queue_lock);
1291 }
1292}
1293
1294/**
1295 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1296 * @task: pointer to rpc_task
1297 *
1298 * Remove a task from the transmission queue
1299 * Caller must hold xprt->queue_lock
1300 */
1301static void
1302xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1303{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001304 struct rpc_rqst *req = task->tk_rqstp;
1305
1306 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1307 return;
1308 if (!list_empty(&req->rq_xmit)) {
1309 list_del(&req->rq_xmit);
1310 if (!list_empty(&req->rq_xmit2)) {
1311 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1312 struct rpc_rqst, rq_xmit2);
1313 list_del(&req->rq_xmit2);
1314 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1315 }
1316 } else
1317 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001318}
1319
1320/**
1321 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1322 * @task: pointer to rpc_task
1323 *
1324 * Remove a task from the transmission queue
1325 */
1326static void
1327xprt_request_dequeue_transmit(struct rpc_task *task)
1328{
1329 struct rpc_rqst *req = task->tk_rqstp;
1330 struct rpc_xprt *xprt = req->rq_xprt;
1331
1332 spin_lock(&xprt->queue_lock);
1333 xprt_request_dequeue_transmit_locked(task);
1334 spin_unlock(&xprt->queue_lock);
1335}
1336
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001337/**
Trond Myklebustcc204d02019-09-10 13:01:35 -04001338 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1339 * @task: pointer to rpc_task
1340 *
1341 * Remove a task from the transmit and receive queues, and ensure that
1342 * it is not pinned by the receive work item.
1343 */
1344void
1345xprt_request_dequeue_xprt(struct rpc_task *task)
1346{
1347 struct rpc_rqst *req = task->tk_rqstp;
1348 struct rpc_xprt *xprt = req->rq_xprt;
1349
1350 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1351 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1352 xprt_is_pinned_rqst(req)) {
1353 spin_lock(&xprt->queue_lock);
1354 xprt_request_dequeue_transmit_locked(task);
1355 xprt_request_dequeue_receive_locked(task);
1356 while (xprt_is_pinned_rqst(req)) {
1357 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1358 spin_unlock(&xprt->queue_lock);
1359 xprt_wait_on_pinned_rqst(req);
1360 spin_lock(&xprt->queue_lock);
1361 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1362 }
1363 spin_unlock(&xprt->queue_lock);
1364 }
1365}
1366
1367/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001368 * xprt_request_prepare - prepare an encoded request for transport
1369 * @req: pointer to rpc_rqst
1370 *
1371 * Calls into the transport layer to do whatever is needed to prepare
1372 * the request for transmission or receive.
1373 */
1374void
1375xprt_request_prepare(struct rpc_rqst *req)
1376{
1377 struct rpc_xprt *xprt = req->rq_xprt;
1378
1379 if (xprt->ops->prepare_request)
1380 xprt->ops->prepare_request(req);
1381}
1382
1383/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001384 * xprt_request_need_retransmit - Test if a task needs retransmission
1385 * @task: pointer to rpc_task
1386 *
1387 * Test for whether a connection breakage requires the task to retransmit
1388 */
1389bool
1390xprt_request_need_retransmit(struct rpc_task *task)
1391{
1392 return xprt_request_retransmit_after_disconnect(task);
1393}
1394
1395/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001396 * xprt_prepare_transmit - reserve the transport before sending a request
1397 * @task: RPC task about to send a request
1398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001400bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct rpc_rqst *req = task->tk_rqstp;
1403 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Chuck Lever46121cf2007-01-31 12:14:08 -05001405 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001407 if (!xprt_lock_write(xprt, task)) {
1408 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001409 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001410 rpc_wake_up_queued_task_set_status(&xprt->sending,
1411 task, 0);
1412 return false;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001415 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001418void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001419{
Rahul Iyer343952f2009-04-01 09:23:17 -04001420 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001421}
1422
Chuck Lever9903cd12005-08-11 16:25:26 -04001423/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001424 * xprt_request_transmit - send an RPC request on a transport
1425 * @req: pointer to request to transmit
1426 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001427 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001428 * This performs the transmission of a single request.
1429 * Note that if the request is not the same as snd_task, then it
1430 * does need to be pinned.
1431 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001432 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001433static int
1434xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001436 struct rpc_xprt *xprt = req->rq_xprt;
1437 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001438 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001439 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001440 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001442 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001443 if (xprt_request_data_received(task)) {
1444 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001445 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001446 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001447 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001448 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001449 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001450 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001451 }
Trond Myklebustae67bd32019-04-07 13:58:44 -04001452 if (RPC_SIGNALLED(task)) {
1453 status = -ERESTARTSYS;
1454 goto out_dequeue;
1455 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001458 /*
1459 * Update req->rq_ntrans before transmitting to avoid races with
1460 * xprt_update_rtt(), which needs to know that it is recording a
1461 * reply to the first transmission.
1462 */
1463 req->rq_ntrans++;
1464
Trond Myklebust90d91b02017-12-14 21:24:08 -05001465 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001466 status = xprt->ops->send_request(req);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001467 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001468 req->rq_ntrans--;
Chuck Lever0c776682019-02-11 11:25:04 -05001469 trace_xprt_transmit(req, status);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001470 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001471 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001472
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001473 if (is_retrans)
1474 task->tk_client->cl_stats->rpcretrans++;
1475
Chuck Lever4a068252015-05-11 14:02:25 -04001476 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Bryan Schumaker468f8612011-04-18 15:57:32 -04001478 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001479 spin_lock(&xprt->transport_lock);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001480
Trond Myklebustc8485e42009-03-11 14:37:59 -04001481 xprt->stat.sends++;
1482 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1483 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001484 xprt->stat.sending_u += xprt->sending.qlen;
1485 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001486 spin_unlock(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001487
1488 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001489out_dequeue:
Chuck Lever0c776682019-02-11 11:25:04 -05001490 trace_xprt_transmit(req, status);
Trond Myklebust944b0422018-08-09 23:33:21 -04001491 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001492 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1493 return status;
1494}
1495
1496/**
1497 * xprt_transmit - send an RPC request on a transport
1498 * @task: controlling RPC task
1499 *
1500 * Attempts to drain the transmit queue. On exit, either the transport
1501 * signalled an error that needs to be handled before transmission can
1502 * resume, or @task finished transmitting, and detected that it already
1503 * received a reply.
1504 */
1505void
1506xprt_transmit(struct rpc_task *task)
1507{
1508 struct rpc_rqst *next, *req = task->tk_rqstp;
1509 struct rpc_xprt *xprt = req->rq_xprt;
1510 int status;
1511
1512 spin_lock(&xprt->queue_lock);
1513 while (!list_empty(&xprt->xmit_queue)) {
1514 next = list_first_entry(&xprt->xmit_queue,
1515 struct rpc_rqst, rq_xmit);
1516 xprt_pin_rqst(next);
1517 spin_unlock(&xprt->queue_lock);
1518 status = xprt_request_transmit(next, task);
1519 if (status == -EBADMSG && next != req)
1520 status = 0;
1521 cond_resched();
1522 spin_lock(&xprt->queue_lock);
1523 xprt_unpin_rqst(next);
1524 if (status == 0) {
1525 if (!xprt_request_data_received(task) ||
1526 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1527 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001528 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001529 task->tk_status = status;
1530 break;
1531 }
1532 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Trond Myklebustba60eb22013-04-14 10:49:37 -04001535static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1536{
1537 set_bit(XPRT_CONGESTED, &xprt->state);
1538 rpc_sleep_on(&xprt->backlog, task, NULL);
1539}
1540
1541static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1542{
1543 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1544 clear_bit(XPRT_CONGESTED, &xprt->state);
1545}
1546
1547static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1548{
1549 bool ret = false;
1550
1551 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1552 goto out;
1553 spin_lock(&xprt->reserve_lock);
1554 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1555 rpc_sleep_on(&xprt->backlog, task, NULL);
1556 ret = true;
1557 }
1558 spin_unlock(&xprt->reserve_lock);
1559out:
1560 return ret;
1561}
1562
Trond Myklebust92ea0112017-06-20 19:35:39 -04001563static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001564{
1565 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1566
Chuck Leverff699ea82018-03-05 15:13:13 -05001567 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001568 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001569 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001570 spin_unlock(&xprt->reserve_lock);
1571 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1572 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001573 if (req != NULL)
1574 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001575 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001576 req = ERR_PTR(-ENOMEM);
1577out:
1578 return req;
1579}
1580
1581static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1582{
Chuck Leverff699ea82018-03-05 15:13:13 -05001583 if (xprt->num_reqs > xprt->min_reqs) {
1584 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001585 kfree(req);
1586 return true;
1587 }
1588 return false;
1589}
1590
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001591void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001593 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001595 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001597 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1598 list_del(&req->rq_list);
1599 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001601 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001602 if (!IS_ERR(req))
1603 goto out_init_req;
1604 switch (PTR_ERR(req)) {
1605 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001606 dprintk("RPC: dynamic allocation of request slot "
1607 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001608 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001609 break;
1610 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001611 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001612 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001613 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001614 default:
1615 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001616 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001617 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001618 return;
1619out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001620 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1621 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001622 spin_unlock(&xprt->reserve_lock);
1623
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001624 task->tk_status = 0;
1625 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001627EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1628
Chuck Levera9cde232018-05-04 15:34:59 -04001629void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001630{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001631 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001632 if (!xprt_dynamic_free_slot(xprt, req)) {
1633 memset(req, 0, sizeof(*req)); /* mark unused */
1634 list_add(&req->rq_list, &xprt->free);
1635 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001636 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001637 spin_unlock(&xprt->reserve_lock);
1638}
Chuck Levera9cde232018-05-04 15:34:59 -04001639EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001640
Trond Myklebust21de0a92011-07-17 16:57:32 -04001641static void xprt_free_all_slots(struct rpc_xprt *xprt)
1642{
1643 struct rpc_rqst *req;
1644 while (!list_empty(&xprt->free)) {
1645 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1646 list_del(&req->rq_list);
1647 kfree(req);
1648 }
1649}
1650
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001651struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1652 unsigned int num_prealloc,
1653 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001654{
1655 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001656 struct rpc_rqst *req;
1657 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001658
1659 xprt = kzalloc(size, GFP_KERNEL);
1660 if (xprt == NULL)
1661 goto out;
1662
Trond Myklebust21de0a92011-07-17 16:57:32 -04001663 xprt_init(xprt, net);
1664
1665 for (i = 0; i < num_prealloc; i++) {
1666 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1667 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001668 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001669 list_add(&req->rq_list, &xprt->free);
1670 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001671 if (max_alloc > num_prealloc)
1672 xprt->max_reqs = max_alloc;
1673 else
1674 xprt->max_reqs = num_prealloc;
1675 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001676 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001677
1678 return xprt;
1679
1680out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001681 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001682out:
1683 return NULL;
1684}
1685EXPORT_SYMBOL_GPL(xprt_alloc);
1686
Pavel Emelyanove204e622010-09-29 16:03:13 +04001687void xprt_free(struct rpc_xprt *xprt)
1688{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001689 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001690 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001691 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001692}
1693EXPORT_SYMBOL_GPL(xprt_free);
1694
Trond Myklebust902c5882018-09-01 17:21:01 -04001695static void
1696xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1697{
1698 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1699}
1700
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001701static __be32
1702xprt_alloc_xid(struct rpc_xprt *xprt)
1703{
1704 __be32 xid;
1705
1706 spin_lock(&xprt->reserve_lock);
1707 xid = (__force __be32)xprt->xid++;
1708 spin_unlock(&xprt->reserve_lock);
1709 return xid;
1710}
1711
1712static void
1713xprt_init_xid(struct rpc_xprt *xprt)
1714{
1715 xprt->xid = prandom_u32();
1716}
1717
1718static void
1719xprt_request_init(struct rpc_task *task)
1720{
1721 struct rpc_xprt *xprt = task->tk_xprt;
1722 struct rpc_rqst *req = task->tk_rqstp;
1723
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001724 req->rq_task = task;
1725 req->rq_xprt = xprt;
1726 req->rq_buffer = NULL;
1727 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001728 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001729 req->rq_snd_buf.len = 0;
1730 req->rq_snd_buf.buflen = 0;
1731 req->rq_rcv_buf.len = 0;
1732 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001733 req->rq_snd_buf.bvec = NULL;
1734 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001735 req->rq_release_snd_buf = NULL;
Trond Myklebustda953062019-04-07 13:58:56 -04001736 xprt_init_majortimeo(task, req);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001737 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1738 req, ntohl(req->rq_xid));
1739}
1740
1741static void
1742xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1743{
1744 xprt->ops->alloc_slot(xprt, task);
1745 if (task->tk_rqstp != NULL)
1746 xprt_request_init(task);
1747}
1748
Chuck Lever9903cd12005-08-11 16:25:26 -04001749/**
1750 * xprt_reserve - allocate an RPC request slot
1751 * @task: RPC task requesting a slot allocation
1752 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001753 * If the transport is marked as being congested, or if no more
1754 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001755 * backlog queue.
1756 */
1757void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001759 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001761 task->tk_status = 0;
1762 if (task->tk_rqstp != NULL)
1763 return;
1764
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001765 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001766 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001767 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001768}
1769
1770/**
1771 * xprt_retry_reserve - allocate an RPC request slot
1772 * @task: RPC task requesting a slot allocation
1773 *
1774 * If no more slots are available, place the task on the transport's
1775 * backlog queue.
1776 * Note that the only difference with xprt_reserve is that we now
1777 * ignore the value of the XPRT_CONGESTED flag.
1778 */
1779void xprt_retry_reserve(struct rpc_task *task)
1780{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001781 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001782
1783 task->tk_status = 0;
1784 if (task->tk_rqstp != NULL)
1785 return;
1786
Trond Myklebustba60eb22013-04-14 10:49:37 -04001787 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001788 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Chuck Lever9903cd12005-08-11 16:25:26 -04001791/**
1792 * xprt_release - release an RPC request slot
1793 * @task: task which is finished with the slot
1794 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001796void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001798 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001799 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Trond Myklebust87ed5002013-01-07 14:30:46 -05001801 if (req == NULL) {
1802 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001803 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001804 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001807 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001808
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001809 xprt = req->rq_xprt;
Trond Myklebustcc204d02019-09-10 13:01:35 -04001810 xprt_request_dequeue_xprt(task);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001811 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001812 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001813 if (xprt->ops->release_request)
1814 xprt->ops->release_request(task);
Trond Myklebustad3331a2016-08-02 13:47:43 -04001815 xprt_schedule_autodisconnect(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001816 spin_unlock(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001817 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001818 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001819 xprt_inject_disconnect(xprt);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001820 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust0472e472019-02-19 13:00:13 -05001821 xdr_free_bvec(&req->rq_snd_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001822 if (req->rq_cred != NULL)
1823 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001825 if (req->rq_release_snd_buf)
1826 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001827
Chuck Lever46121cf2007-01-31 12:14:08 -05001828 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001829 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001830 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001831 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001832 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Trond Myklebust902c5882018-09-01 17:21:01 -04001835#ifdef CONFIG_SUNRPC_BACKCHANNEL
1836void
1837xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1838{
1839 struct xdr_buf *xbufp = &req->rq_snd_buf;
1840
1841 task->tk_rqstp = req;
1842 req->rq_task = task;
1843 xprt_init_connect_cookie(req, req->rq_xprt);
1844 /*
1845 * Set up the xdr_buf length.
1846 * This also indicates that the buffer is XDR encoded already.
1847 */
1848 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1849 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001850}
1851#endif
1852
Trond Myklebust21de0a92011-07-17 16:57:32 -04001853static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001854{
Trond Myklebust30c51162015-02-24 20:31:39 -05001855 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001856
1857 spin_lock_init(&xprt->transport_lock);
1858 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001859 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001860
1861 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001862 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001863 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001864#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001865 spin_lock_init(&xprt->bc_pa_lock);
1866 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001867#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001868 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001869
Chuck Leverc2866762006-08-22 20:06:20 -04001870 xprt->last_used = jiffies;
1871 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001872 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001873
1874 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1875 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001876 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001877 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1878
Chuck Leverc2866762006-08-22 20:06:20 -04001879 xprt_init_xid(xprt);
1880
Trond Myklebust21de0a92011-07-17 16:57:32 -04001881 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001882}
1883
1884/**
1885 * xprt_create_transport - create an RPC transport
1886 * @args: rpc transport creation arguments
1887 *
1888 */
1889struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1890{
1891 struct rpc_xprt *xprt;
1892 struct xprt_class *t;
1893
1894 spin_lock(&xprt_list_lock);
1895 list_for_each_entry(t, &xprt_list, list) {
1896 if (t->ident == args->ident) {
1897 spin_unlock(&xprt_list_lock);
1898 goto found;
1899 }
1900 }
1901 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001902 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001903 return ERR_PTR(-EIO);
1904
1905found:
1906 xprt = t->setup(args);
1907 if (IS_ERR(xprt)) {
1908 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1909 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001910 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001911 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001912 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1913 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001914 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1915 if (xprt_has_timer(xprt))
Anna Schumaker502980e2019-06-18 14:57:33 -04001916 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001917 else
Kees Cookff861c42017-10-16 17:29:42 -07001918 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001919
1920 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1921 xprt_destroy(xprt);
1922 return ERR_PTR(-EINVAL);
1923 }
1924 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1925 if (xprt->servername == NULL) {
1926 xprt_destroy(xprt);
1927 return ERR_PTR(-ENOMEM);
1928 }
1929
Jeff Layton3f940092015-03-31 12:03:28 -04001930 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001931
Chuck Lever46121cf2007-01-31 12:14:08 -05001932 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001933 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001934out:
Chuck Leverc2866762006-08-22 20:06:20 -04001935 return xprt;
1936}
1937
Trond Myklebust528fd352017-10-19 12:13:10 -04001938static void xprt_destroy_cb(struct work_struct *work)
1939{
1940 struct rpc_xprt *xprt =
1941 container_of(work, struct rpc_xprt, task_cleanup);
1942
1943 rpc_xprt_debugfs_unregister(xprt);
1944 rpc_destroy_wait_queue(&xprt->binding);
1945 rpc_destroy_wait_queue(&xprt->pending);
1946 rpc_destroy_wait_queue(&xprt->sending);
1947 rpc_destroy_wait_queue(&xprt->backlog);
1948 kfree(xprt->servername);
1949 /*
Trond Myklebust669996a2019-10-17 09:02:21 -04001950 * Destroy any existing back channel
1951 */
1952 xprt_destroy_backchannel(xprt, UINT_MAX);
1953
1954 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001955 * Tear down transport state and free the rpc_xprt
1956 */
1957 xprt->ops->destroy(xprt);
1958}
1959
Chuck Lever9903cd12005-08-11 16:25:26 -04001960/**
1961 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001962 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001963 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001965static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Chuck Lever46121cf2007-01-31 12:14:08 -05001967 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001968
Trond Myklebust528fd352017-10-19 12:13:10 -04001969 /*
1970 * Exclude transport connect/disconnect handlers and autoclose
1971 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001972 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1973
Trond Myklebust0065db32006-01-03 09:55:56 +01001974 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001975
1976 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001977 * Destroy sockets etc from the system workqueue so they can
1978 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001979 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001980 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1981 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001982}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Trond Myklebust30c51162015-02-24 20:31:39 -05001984static void xprt_destroy_kref(struct kref *kref)
1985{
1986 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1987}
1988
1989/**
1990 * xprt_get - return a reference to an RPC transport.
1991 * @xprt: pointer to the transport
1992 *
1993 */
1994struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1995{
1996 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1997 return xprt;
1998 return NULL;
1999}
2000EXPORT_SYMBOL_GPL(xprt_get);
2001
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002002/**
2003 * xprt_put - release a reference to an RPC transport.
2004 * @xprt: pointer to the transport
2005 *
2006 */
2007void xprt_put(struct rpc_xprt *xprt)
2008{
Trond Myklebust30c51162015-02-24 20:31:39 -05002009 if (xprt != NULL)
2010 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002011}
Chuck Lever5d252f92016-01-07 14:50:10 -05002012EXPORT_SYMBOL_GPL(xprt_put);