blob: e5b5a960a69b874a409e7fa7dae9cdc9dcfc1ddd [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
Trond Myklebustd5aa6b22020-11-06 16:33:38 -0500154static void
155xprt_class_release(const struct xprt_class *t)
156{
157 module_put(t->owner);
158}
159
160static const struct xprt_class *
Trond Myklebust9bccd262020-11-10 12:58:22 -0500161xprt_class_find_by_ident_locked(int ident)
162{
163 const struct xprt_class *t;
164
165 list_for_each_entry(t, &xprt_list, list) {
166 if (t->ident != ident)
167 continue;
168 if (!try_module_get(t->owner))
169 continue;
170 return t;
171 }
172 return NULL;
173}
174
175static const struct xprt_class *
176xprt_class_find_by_ident(int ident)
177{
178 const struct xprt_class *t;
179
180 spin_lock(&xprt_list_lock);
181 t = xprt_class_find_by_ident_locked(ident);
182 spin_unlock(&xprt_list_lock);
183 return t;
184}
185
186static const struct xprt_class *
Trond Myklebustd5aa6b22020-11-06 16:33:38 -0500187xprt_class_find_by_netid_locked(const char *netid)
188{
189 const struct xprt_class *t;
190 unsigned int i;
191
192 list_for_each_entry(t, &xprt_list, list) {
193 for (i = 0; t->netid[i][0] != '\0'; i++) {
194 if (strcmp(t->netid[i], netid) != 0)
195 continue;
196 if (!try_module_get(t->owner))
197 continue;
198 return t;
199 }
200 }
201 return NULL;
202}
203
204static const struct xprt_class *
205xprt_class_find_by_netid(const char *netid)
206{
207 const struct xprt_class *t;
208
209 spin_lock(&xprt_list_lock);
210 t = xprt_class_find_by_netid_locked(netid);
211 if (!t) {
212 spin_unlock(&xprt_list_lock);
213 request_module("rpc%s", netid);
214 spin_lock(&xprt_list_lock);
215 t = xprt_class_find_by_netid_locked(netid);
216 }
217 spin_unlock(&xprt_list_lock);
218 return t;
219}
220
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400221/**
Trond Myklebust1fc5f132020-11-10 09:41:21 -0500222 * xprt_find_transport_ident - convert a netid into a transport identifier
223 * @netid: transport to load
224 *
225 * Returns:
226 * > 0: transport identifier
227 * -ENOENT: transport module not available
228 */
229int xprt_find_transport_ident(const char *netid)
230{
231 const struct xprt_class *t;
232 int ret;
233
234 t = xprt_class_find_by_netid(netid);
235 if (!t)
236 return -ENOENT;
237 ret = t->ident;
238 xprt_class_release(t);
239 return ret;
240}
241EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
242
Trond Myklebustc5445772018-09-03 23:39:27 -0400243static void xprt_clear_locked(struct rpc_xprt *xprt)
244{
245 xprt->snd_task = NULL;
246 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
247 smp_mb__before_atomic();
248 clear_bit(XPRT_LOCKED, &xprt->state);
249 smp_mb__after_atomic();
250 } else
251 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
252}
253
Tom Talpey441e3e22009-03-11 14:37:56 -0400254/**
Chuck Lever12a80462005-08-25 16:25:51 -0700255 * xprt_reserve_xprt - serialize write access to transports
256 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000257 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700258 *
259 * This prevents mixing the payload of separate requests, and prevents
260 * transport connects from colliding with writes. No congestion control
261 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400263int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Chuck Lever12a80462005-08-25 16:25:51 -0700265 struct rpc_rqst *req = task->tk_rqstp;
266
267 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
268 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400269 goto out_locked;
Chuck Lever12a80462005-08-25 16:25:51 -0700270 goto out_sleep;
271 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400272 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
273 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700274 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500275
Chuck Leverbf7ca702019-10-09 12:58:14 -0400276out_locked:
277 trace_xprt_reserve_xprt(xprt, task);
Chuck Lever12a80462005-08-25 16:25:51 -0700278 return 1;
279
Trond Myklebustc5445772018-09-03 23:39:27 -0400280out_unlock:
281 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700282out_sleep:
Chuck Lever12a80462005-08-25 16:25:51 -0700283 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400284 if (RPC_IS_SOFT(task))
285 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400286 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400287 else
288 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700289 return 0;
290}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400291EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700292
Trond Myklebust75891f52018-09-03 17:37:36 -0400293static bool
294xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
295{
296 return test_bit(XPRT_CWND_WAIT, &xprt->state);
297}
298
299static void
300xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
301{
302 if (!list_empty(&xprt->xmit_queue)) {
303 /* Peek at head of queue to see if it can make progress */
304 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
305 rq_xmit)->rq_cong)
306 return;
307 }
308 set_bit(XPRT_CWND_WAIT, &xprt->state);
309}
310
311static void
312xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
313{
314 if (!RPCXPRT_CONGESTED(xprt))
315 clear_bit(XPRT_CWND_WAIT, &xprt->state);
316}
317
Chuck Lever12a80462005-08-25 16:25:51 -0700318/*
319 * xprt_reserve_xprt_cong - serialize write access to transports
320 * @task: task that is requesting access to the transport
321 *
322 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
323 * integrated into the decision of whether a request is allowed to be
324 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400325 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700326 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400327int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct rpc_rqst *req = task->tk_rqstp;
330
Chuck Lever2226feb2005-08-11 16:25:38 -0400331 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (task == xprt->snd_task)
Chuck Leverbf7ca702019-10-09 12:58:14 -0400333 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto out_sleep;
335 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400336 if (req == NULL) {
337 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400338 goto out_locked;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400339 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400340 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
341 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400342 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 xprt->snd_task = task;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400344 goto out_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400346out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100347 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348out_sleep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400350 if (RPC_IS_SOFT(task))
351 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400352 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400353 else
354 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400356out_locked:
357 trace_xprt_reserve_cong(xprt, task);
358 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400360EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Chuck Lever12a80462005-08-25 16:25:51 -0700362static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int retval;
365
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400366 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
367 return 1;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400368 spin_lock(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400369 retval = xprt->ops->reserve_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400370 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return retval;
372}
373
Trond Myklebust961a8282012-01-17 22:57:37 -0500374static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Trond Myklebust961a8282012-01-17 22:57:37 -0500376 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700377
Chuck Lever49e9a892005-08-25 16:25:51 -0700378 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500379 return true;
380}
Chuck Lever49e9a892005-08-25 16:25:51 -0700381
Trond Myklebust961a8282012-01-17 22:57:37 -0500382static void __xprt_lock_write_next(struct rpc_xprt *xprt)
383{
384 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
385 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400386 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
387 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400388 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
389 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500390 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400391out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100392 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700393}
394
Trond Myklebust961a8282012-01-17 22:57:37 -0500395static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
396{
397 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
398 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400399 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
400 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400401 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500402 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400403 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400404 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100407 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Chuck Lever49e9a892005-08-25 16:25:51 -0700410/**
411 * xprt_release_xprt - allow other requests to use a transport
412 * @xprt: transport with other tasks potentially waiting
413 * @task: task that is releasing access to the transport
414 *
415 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700417void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100420 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 __xprt_lock_write_next(xprt);
422 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400423 trace_xprt_release_xprt(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400425EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Chuck Lever49e9a892005-08-25 16:25:51 -0700427/**
428 * xprt_release_xprt_cong - allow other requests to use a transport
429 * @xprt: transport with other tasks potentially waiting
430 * @task: task that is releasing access to the transport
431 *
432 * Note that "task" can be NULL. Another task is awoken to use the
433 * transport if the transport's congestion window allows it.
434 */
435void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
436{
437 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100438 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700439 __xprt_lock_write_next_cong(xprt);
440 }
Chuck Leverbf7ca702019-10-09 12:58:14 -0400441 trace_xprt_release_cong(xprt, task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700442}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400443EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700444
445static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400447 if (xprt->snd_task != task)
448 return;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400449 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700450 xprt->ops->release_xprt(xprt, task);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400451 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Van Jacobson congestion avoidance. Check if the congestion window
456 * overflowed. Put the task to sleep if this is the case.
457 */
458static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400459__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (req->rq_cong)
462 return 1;
Chuck Leverbf7ca702019-10-09 12:58:14 -0400463 trace_xprt_get_cong(xprt, req->rq_task);
Trond Myklebust75891f52018-09-03 17:37:36 -0400464 if (RPCXPRT_CONGESTED(xprt)) {
465 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 req->rq_cong = 1;
469 xprt->cong += RPC_CWNDSCALE;
470 return 1;
471}
472
473/*
474 * Adjust the congestion window, and wake up the next task
475 * that has been sleeping due to congestion
476 */
477static void
478__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
479{
480 if (!req->rq_cong)
481 return;
482 req->rq_cong = 0;
483 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400484 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Leverbf7ca702019-10-09 12:58:14 -0400485 trace_xprt_put_cong(xprt, req->rq_task);
Chuck Lever49e9a892005-08-25 16:25:51 -0700486 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Chuck Lever46c0ee82005-08-25 16:25:52 -0700489/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400490 * xprt_request_get_cong - Request congestion control credits
491 * @xprt: pointer to transport
492 * @req: pointer to RPC request
493 *
494 * Useful for transports that require congestion control.
495 */
496bool
497xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
498{
499 bool ret = false;
500
501 if (req->rq_cong)
502 return true;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400503 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400504 ret = __xprt_get_cong(xprt, req) != 0;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400505 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400506 return ret;
507}
508EXPORT_SYMBOL_GPL(xprt_request_get_cong);
509
510/**
Chuck Levera58dd392005-08-25 16:25:53 -0700511 * xprt_release_rqst_cong - housekeeping when request is complete
512 * @task: RPC request that recently completed
513 *
514 * Useful for transports that require congestion control.
515 */
516void xprt_release_rqst_cong(struct rpc_task *task)
517{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500518 struct rpc_rqst *req = task->tk_rqstp;
519
520 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700521}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400522EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700523
Chuck Lever8593e012019-09-13 16:01:07 -0400524static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
525{
526 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
527 __xprt_lock_write_next_cong(xprt);
528}
529
Trond Myklebust75891f52018-09-03 17:37:36 -0400530/*
531 * Clear the congestion window wait flag and wake up the next
532 * entry on xprt->sending
533 */
534static void
535xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
536{
537 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
Trond Myklebustb5e92412019-05-02 11:21:08 -0400538 spin_lock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400539 __xprt_lock_write_next_cong(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400540 spin_unlock(&xprt->transport_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -0400541 }
542}
543
Chuck Levera58dd392005-08-25 16:25:53 -0700544/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700545 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500546 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700547 * @task: recently completed RPC request used to adjust window
548 * @result: result code of completed RPC request
549 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400550 * The transport code maintains an estimate on the maximum number of out-
551 * standing RPC requests, using a smoothed version of the congestion
552 * avoidance implemented in 44BSD. This is basically the Van Jacobson
553 * congestion algorithm: If a retransmit occurs, the congestion window is
554 * halved; otherwise, it is incremented by 1/cwnd when
555 *
556 * - a reply is received and
557 * - a full number of requests are outstanding and
558 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500560void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700562 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700563 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (result >= 0 && cwnd <= xprt->cong) {
566 /* The (cwnd >> 1) term makes sure
567 * the result gets rounded properly. */
568 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
569 if (cwnd > RPC_MAXCWND(xprt))
570 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700571 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 } else if (result == -ETIMEDOUT) {
573 cwnd >>= 1;
574 if (cwnd < RPC_CWNDSCALE)
575 cwnd = RPC_CWNDSCALE;
576 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500577 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xprt->cong, xprt->cwnd, cwnd);
579 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700580 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400582EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Chuck Lever44fbac22005-08-11 16:25:44 -0400584/**
585 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
586 * @xprt: transport with waiting tasks
587 * @status: result code to plant in each task before waking it
588 *
589 */
590void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
591{
592 if (status < 0)
593 rpc_wake_up_status(&xprt->pending, status);
594 else
595 rpc_wake_up(&xprt->pending);
596}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400597EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400598
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400599/**
600 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400601 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500602 *
603 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
604 * we don't in general want to force a socket disconnection due to
605 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400606 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400607void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400608{
Trond Myklebustc5445772018-09-03 23:39:27 -0400609 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400610}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400611EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400612
Trond Myklebustc5445772018-09-03 23:39:27 -0400613static bool
614xprt_clear_write_space_locked(struct rpc_xprt *xprt)
615{
616 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
617 __xprt_lock_write_next(xprt);
618 dprintk("RPC: write space: waking waiting task on "
619 "xprt %p\n", xprt);
620 return true;
621 }
622 return false;
623}
624
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400625/**
626 * xprt_write_space - wake the task waiting for transport output buffer space
627 * @xprt: transport with waiting tasks
628 *
629 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
630 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400631bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400632{
Trond Myklebustc5445772018-09-03 23:39:27 -0400633 bool ret;
634
635 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
636 return false;
Trond Myklebustb5e92412019-05-02 11:21:08 -0400637 spin_lock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400638 ret = xprt_clear_write_space_locked(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400639 spin_unlock(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400640 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400641}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400642EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400643
Trond Myklebustda953062019-04-07 13:58:56 -0400644static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
645{
646 s64 delta = ktime_to_ns(ktime_get() - abstime);
647 return likely(delta >= 0) ?
648 jiffies - nsecs_to_jiffies(delta) :
649 jiffies + nsecs_to_jiffies(-delta);
650}
651
652static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500654 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Trond Myklebustda953062019-04-07 13:58:56 -0400655 unsigned long majortimeo = req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (to->to_exponential)
Trond Myklebustda953062019-04-07 13:58:56 -0400658 majortimeo <<= to->to_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 else
Trond Myklebustda953062019-04-07 13:58:56 -0400660 majortimeo += to->to_increment * to->to_retries;
661 if (majortimeo > to->to_maxval || majortimeo == 0)
662 majortimeo = to->to_maxval;
663 return majortimeo;
664}
665
666static void xprt_reset_majortimeo(struct rpc_rqst *req)
667{
668 req->rq_majortimeo += xprt_calc_majortimeo(req);
669}
670
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400671static void xprt_reset_minortimeo(struct rpc_rqst *req)
672{
673 req->rq_minortimeo += req->rq_timeout;
674}
675
Trond Myklebustda953062019-04-07 13:58:56 -0400676static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
677{
678 unsigned long time_init;
679 struct rpc_xprt *xprt = req->rq_xprt;
680
681 if (likely(xprt && xprt_connected(xprt)))
682 time_init = jiffies;
683 else
684 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
685 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
686 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400687 req->rq_minortimeo = time_init + req->rq_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Chuck Lever9903cd12005-08-11 16:25:26 -0400690/**
691 * xprt_adjust_timeout - adjust timeout values for next retransmit
692 * @req: RPC request containing parameters to use for the adjustment
693 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695int xprt_adjust_timeout(struct rpc_rqst *req)
696{
697 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500698 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int status = 0;
700
701 if (time_before(jiffies, req->rq_majortimeo)) {
Chris Dion09252172021-04-04 21:29:26 -0400702 if (time_before(jiffies, req->rq_minortimeo))
703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (to->to_exponential)
705 req->rq_timeout <<= 1;
706 else
707 req->rq_timeout += to->to_increment;
708 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
709 req->rq_timeout = to->to_maxval;
710 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else {
712 req->rq_timeout = to->to_initval;
713 req->rq_retries = 0;
714 xprt_reset_majortimeo(req);
715 /* Reset the RTT counters == "slow start" */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400716 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400718 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 status = -ETIMEDOUT;
720 }
Olga Kornievskaia7de62bc2020-07-15 13:17:52 -0400721 xprt_reset_minortimeo(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (req->rq_timeout == 0) {
724 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
725 req->rq_timeout = 5 * HZ;
726 }
727 return status;
728}
729
David Howells65f27f32006-11-22 14:55:48 +0000730static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
David Howells65f27f32006-11-22 14:55:48 +0000732 struct rpc_xprt *xprt =
733 container_of(work, struct rpc_xprt, task_cleanup);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500734 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Chuck Lever911813d2020-05-12 17:13:34 -0400736 trace_xprt_disconnect_auto(xprt);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500737 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400738 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400740 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500741 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Chuck Lever9903cd12005-08-11 16:25:26 -0400744/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500745 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400746 * @xprt: transport to flag for disconnect
747 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500749void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Chuck Lever911813d2020-05-12 17:13:34 -0400751 trace_xprt_disconnect_done(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400752 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400754 xprt_clear_write_space_locked(xprt);
Chuck Lever8593e012019-09-13 16:01:07 -0400755 xprt_clear_congestion_window_wait_locked(xprt);
Trond Myklebust27adc782019-03-15 08:01:16 -0400756 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400757 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500759EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Trond Myklebust66af1e552007-11-06 10:18:36 -0500761/**
762 * xprt_force_disconnect - force a transport to disconnect
763 * @xprt: transport to disconnect
764 *
765 */
766void xprt_force_disconnect(struct rpc_xprt *xprt)
767{
Chuck Lever911813d2020-05-12 17:13:34 -0400768 trace_xprt_disconnect_force(xprt);
769
Trond Myklebust66af1e552007-11-06 10:18:36 -0500770 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400771 spin_lock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500772 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
773 /* Try to schedule an autoclose RPC call */
774 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400775 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust0445f922018-12-17 13:34:59 -0500776 else if (xprt->snd_task)
777 rpc_wake_up_queued_task_set_status(&xprt->pending,
778 xprt->snd_task, -ENOTCONN);
Trond Myklebustb5e92412019-05-02 11:21:08 -0400779 spin_unlock(&xprt->transport_lock);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500780}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400781EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500782
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400783static unsigned int
784xprt_connect_cookie(struct rpc_xprt *xprt)
785{
786 return READ_ONCE(xprt->connect_cookie);
787}
788
789static bool
790xprt_request_retransmit_after_disconnect(struct rpc_task *task)
791{
792 struct rpc_rqst *req = task->tk_rqstp;
793 struct rpc_xprt *xprt = req->rq_xprt;
794
795 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
796 !xprt_connected(xprt);
797}
798
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400799/**
800 * xprt_conditional_disconnect - force a transport to disconnect
801 * @xprt: transport to disconnect
802 * @cookie: 'connection cookie'
803 *
804 * This attempts to break the connection if and only if 'cookie' matches
805 * the current transport 'connection cookie'. It ensures that we don't
806 * try to break the connection more than once when we need to retransmit
807 * a batch of RPC requests.
808 *
809 */
810void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
811{
812 /* Don't race with the test_bit() in xprt_clear_locked() */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400813 spin_lock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400814 if (cookie != xprt->connect_cookie)
815 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100816 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400817 goto out;
818 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
819 /* Try to schedule an autoclose RPC call */
820 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400821 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400822 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400823out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400824 spin_unlock(&xprt->transport_lock);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400825}
826
Trond Myklebustad3331a2016-08-02 13:47:43 -0400827static bool
828xprt_has_timer(const struct rpc_xprt *xprt)
829{
830 return xprt->idle_timeout != 0;
831}
832
833static void
834xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
835 __must_hold(&xprt->transport_lock)
836{
Dave Wysochanski80d3c452019-06-26 16:30:24 -0400837 xprt->last_used = jiffies;
Trond Myklebust95f76912018-09-07 08:35:22 -0400838 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400839 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static void
Kees Cookff861c42017-10-16 17:29:42 -0700843xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Kees Cookff861c42017-10-16 17:29:42 -0700845 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Trond Myklebust95f76912018-09-07 08:35:22 -0400847 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400848 return;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400849 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
850 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400851 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Trond Myklebustb5e92412019-05-02 11:21:08 -0400852 return;
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400853 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500856bool xprt_lock_connect(struct rpc_xprt *xprt,
857 struct rpc_task *task,
858 void *cookie)
859{
860 bool ret = false;
861
Trond Myklebustb5e92412019-05-02 11:21:08 -0400862 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500863 if (!test_bit(XPRT_LOCKED, &xprt->state))
864 goto out;
865 if (xprt->snd_task != task)
866 goto out;
867 xprt->snd_task = cookie;
868 ret = true;
869out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400870 spin_unlock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500871 return ret;
872}
873
874void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
875{
Trond Myklebustb5e92412019-05-02 11:21:08 -0400876 spin_lock(&xprt->transport_lock);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500877 if (xprt->snd_task != cookie)
878 goto out;
879 if (!test_bit(XPRT_LOCKED, &xprt->state))
880 goto out;
881 xprt->snd_task =NULL;
882 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400883 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500884out:
Trond Myklebustb5e92412019-05-02 11:21:08 -0400885 spin_unlock(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400886 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500887}
888
Chuck Lever9903cd12005-08-11 16:25:26 -0400889/**
890 * xprt_connect - schedule a transport connect operation
891 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *
893 */
894void xprt_connect(struct rpc_task *task)
895{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500896 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Chuck Leverdb0a86c2020-07-08 16:09:47 -0400898 trace_xprt_connect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Chuck Leverec739ef2006-08-22 20:06:15 -0400900 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400901 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return;
903 }
904 if (!xprt_lock_write(xprt, task))
905 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500906
Chuck Lever911813d2020-05-12 17:13:34 -0400907 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
908 trace_xprt_disconnect_cleanup(xprt);
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500909 xprt->ops->close(xprt);
Chuck Lever911813d2020-05-12 17:13:34 -0400910 }
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500911
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500912 if (!xprt_connected(xprt)) {
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100913 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400914 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400915 xprt_request_timeout(task->tk_rqstp));
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400916
917 if (test_bit(XPRT_CLOSING, &xprt->state))
918 return;
919 if (xprt_test_and_set_connecting(xprt))
920 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500921 /* Race breaker */
922 if (!xprt_connected(xprt)) {
923 xprt->stat.connect_start = jiffies;
924 xprt->ops->connect(xprt, task);
925 } else {
926 xprt_clear_connecting(xprt);
927 task->tk_status = 0;
928 rpc_wake_up_queued_task(&xprt->pending, task);
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500931 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Chuck Lever675dd902019-06-19 10:33:42 -0400934/**
935 * xprt_reconnect_delay - compute the wait before scheduling a connect
936 * @xprt: transport instance
937 *
938 */
939unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
940{
941 unsigned long start, now = jiffies;
942
943 start = xprt->stat.connect_start + xprt->reestablish_timeout;
944 if (time_after(start, now))
945 return start - now;
946 return 0;
947}
948EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
949
950/**
951 * xprt_reconnect_backoff - compute the new re-establish timeout
952 * @xprt: transport instance
953 * @init_to: initial reestablish timeout
954 *
955 */
956void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
957{
958 xprt->reestablish_timeout <<= 1;
959 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
960 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
961 if (xprt->reestablish_timeout < init_to)
962 xprt->reestablish_timeout = init_to;
963}
964EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
965
Trond Myklebust95f76912018-09-07 08:35:22 -0400966enum xprt_xid_rb_cmp {
967 XID_RB_EQUAL,
968 XID_RB_LEFT,
969 XID_RB_RIGHT,
970};
971static enum xprt_xid_rb_cmp
972xprt_xid_cmp(__be32 xid1, __be32 xid2)
973{
974 if (xid1 == xid2)
975 return XID_RB_EQUAL;
976 if ((__force u32)xid1 < (__force u32)xid2)
977 return XID_RB_LEFT;
978 return XID_RB_RIGHT;
979}
980
981static struct rpc_rqst *
982xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
983{
984 struct rb_node *n = xprt->recv_queue.rb_node;
985 struct rpc_rqst *req;
986
987 while (n != NULL) {
988 req = rb_entry(n, struct rpc_rqst, rq_recv);
989 switch (xprt_xid_cmp(xid, req->rq_xid)) {
990 case XID_RB_LEFT:
991 n = n->rb_left;
992 break;
993 case XID_RB_RIGHT:
994 n = n->rb_right;
995 break;
996 case XID_RB_EQUAL:
997 return req;
998 }
999 }
1000 return NULL;
1001}
1002
1003static void
1004xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
1005{
1006 struct rb_node **p = &xprt->recv_queue.rb_node;
1007 struct rb_node *n = NULL;
1008 struct rpc_rqst *req;
1009
1010 while (*p != NULL) {
1011 n = *p;
1012 req = rb_entry(n, struct rpc_rqst, rq_recv);
1013 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
1014 case XID_RB_LEFT:
1015 p = &n->rb_left;
1016 break;
1017 case XID_RB_RIGHT:
1018 p = &n->rb_right;
1019 break;
1020 case XID_RB_EQUAL:
1021 WARN_ON_ONCE(new != req);
1022 return;
1023 }
1024 }
1025 rb_link_node(&new->rq_recv, n, p);
1026 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
1027}
1028
1029static void
1030xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
1031{
1032 rb_erase(&req->rq_recv, &xprt->recv_queue);
1033}
1034
Chuck Lever9903cd12005-08-11 16:25:26 -04001035/**
1036 * xprt_lookup_rqst - find an RPC request corresponding to an XID
1037 * @xprt: transport on which the original request was transmitted
1038 * @xid: RPC XID of incoming reply
1039 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001040 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001042struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001044 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Trond Myklebust95f76912018-09-07 08:35:22 -04001046 entry = xprt_request_rb_find(xprt, xid);
1047 if (entry != NULL) {
1048 trace_xprt_lookup_rqst(xprt, xid, 0);
1049 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1050 return entry;
1051 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001052
1053 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
1054 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -04001055 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -05001056 xprt->stat.bad_xids++;
1057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001059EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001061static bool
1062xprt_is_pinned_rqst(struct rpc_rqst *req)
1063{
1064 return atomic_read(&req->rq_pin) != 0;
1065}
1066
Trond Myklebust729749b2017-08-13 10:03:59 -04001067/**
1068 * xprt_pin_rqst - Pin a request on the transport receive list
1069 * @req: Request to pin
1070 *
1071 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001072 * so should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001073 */
1074void xprt_pin_rqst(struct rpc_rqst *req)
1075{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001076 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001077}
Chuck Lever9590d082017-08-23 17:05:58 -04001078EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001079
1080/**
1081 * xprt_unpin_rqst - Unpin a request on the transport receive list
1082 * @req: Request to pin
1083 *
Chuck Lever1f7d1c72019-04-24 09:40:09 -04001084 * Caller should be holding xprt->queue_lock.
Trond Myklebust729749b2017-08-13 10:03:59 -04001085 */
1086void xprt_unpin_rqst(struct rpc_rqst *req)
1087{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001088 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1089 atomic_dec(&req->rq_pin);
1090 return;
1091 }
1092 if (atomic_dec_and_test(&req->rq_pin))
1093 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -04001094}
Chuck Lever9590d082017-08-23 17:05:58 -04001095EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001096
1097static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -04001098{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001099 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -04001100}
1101
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001102static bool
1103xprt_request_data_received(struct rpc_task *task)
1104{
1105 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1106 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1107}
1108
1109static bool
1110xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1111{
1112 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1113 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1114}
1115
1116/**
1117 * xprt_request_enqueue_receive - Add an request to the receive queue
1118 * @task: RPC task
1119 *
1120 */
1121void
1122xprt_request_enqueue_receive(struct rpc_task *task)
1123{
1124 struct rpc_rqst *req = task->tk_rqstp;
1125 struct rpc_xprt *xprt = req->rq_xprt;
1126
1127 if (!xprt_request_need_enqueue_receive(task, req))
1128 return;
Trond Myklebust75369082019-07-17 21:22:38 -04001129
1130 xprt_request_prepare(task->tk_rqstp);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001131 spin_lock(&xprt->queue_lock);
1132
1133 /* Update the softirq receive buffer */
1134 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1135 sizeof(req->rq_private_buf));
1136
1137 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001138 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001139 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1140 spin_unlock(&xprt->queue_lock);
1141
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001142 /* Turn off autodisconnect */
1143 del_singleshot_timer_sync(&xprt->timer);
1144}
1145
1146/**
1147 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1148 * @task: RPC task
1149 *
1150 * Caller must hold xprt->queue_lock.
1151 */
1152static void
1153xprt_request_dequeue_receive_locked(struct rpc_task *task)
1154{
Trond Myklebust95f76912018-09-07 08:35:22 -04001155 struct rpc_rqst *req = task->tk_rqstp;
1156
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001157 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001158 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001159}
1160
Chuck Leverecd465e2018-03-05 15:12:57 -05001161/**
1162 * xprt_update_rtt - Update RPC RTT statistics
1163 * @task: RPC request that recently completed
1164 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001165 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001166 */
1167void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001169 struct rpc_rqst *req = task->tk_rqstp;
1170 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001171 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001172 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Chuck Lever1570c1e2005-08-25 16:25:52 -07001174 if (timer) {
1175 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001176 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001177 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001179}
Chuck Leverecd465e2018-03-05 15:12:57 -05001180EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Chuck Lever1570c1e2005-08-25 16:25:52 -07001182/**
1183 * xprt_complete_rqst - called when reply processing is complete
1184 * @task: RPC request that recently completed
1185 * @copied: actual number of bytes received from the transport
1186 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001187 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001188 */
1189void xprt_complete_rqst(struct rpc_task *task, int copied)
1190{
1191 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001192 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Trond Myklebustfda13932008-02-22 16:34:12 -05001194 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001195
Trond Myklebust1e799b62008-03-21 16:19:41 -04001196 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001197 /* Ensure all writes are done before we update */
1198 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001199 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001200 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001201 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001202 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001204EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Chuck Lever46c0ee82005-08-25 16:25:52 -07001206static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001208 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct rpc_xprt *xprt = req->rq_xprt;
1210
Trond Myklebust5d008372008-02-22 16:34:17 -05001211 if (task->tk_status != -ETIMEDOUT)
1212 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001213
Chuck Lever82476d92018-01-03 15:38:25 -05001214 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001215 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001216 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001217 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001218 } else
1219 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Chuck Lever9903cd12005-08-11 16:25:26 -04001222/**
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001223 * xprt_wait_for_reply_request_def - wait for reply
1224 * @task: pointer to rpc_task
1225 *
1226 * Set a request's retransmit timeout based on the transport's
1227 * default timeout parameters. Used by transports that don't adjust
1228 * the retransmit timeout based on round-trip time estimation,
1229 * and put the task to sleep on the pending queue.
1230 */
1231void xprt_wait_for_reply_request_def(struct rpc_task *task)
1232{
1233 struct rpc_rqst *req = task->tk_rqstp;
1234
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001235 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
Trond Myklebust9e910bf2019-04-07 13:58:53 -04001236 xprt_request_timeout(req));
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001237}
1238EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1239
1240/**
1241 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1242 * @task: pointer to rpc_task
1243 *
1244 * Set a request's retransmit timeout using the RTT estimator,
1245 * and put the task to sleep on the pending queue.
1246 */
1247void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1248{
1249 int timer = task->tk_msg.rpc_proc->p_timer;
1250 struct rpc_clnt *clnt = task->tk_client;
1251 struct rpc_rtt *rtt = clnt->cl_rtt;
1252 struct rpc_rqst *req = task->tk_rqstp;
1253 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001254 unsigned long timeout;
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001255
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001256 timeout = rpc_calc_rto(rtt, timer);
1257 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1258 if (timeout > max_timeout || timeout == 0)
1259 timeout = max_timeout;
1260 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1261 jiffies + timeout);
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001262}
1263EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1264
1265/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001266 * xprt_request_wait_receive - wait for the reply to an RPC request
1267 * @task: RPC task about to send a request
1268 *
1269 */
1270void xprt_request_wait_receive(struct rpc_task *task)
1271{
1272 struct rpc_rqst *req = task->tk_rqstp;
1273 struct rpc_xprt *xprt = req->rq_xprt;
1274
1275 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1276 return;
1277 /*
1278 * Sleep on the pending queue if we're expecting a reply.
1279 * The spinlock ensures atomicity between the test of
1280 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1281 */
1282 spin_lock(&xprt->queue_lock);
1283 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001284 xprt->ops->wait_for_reply_request(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001285 /*
1286 * Send an extra queue wakeup call if the
1287 * connection was dropped in case the call to
1288 * rpc_sleep_on() raced.
1289 */
1290 if (xprt_request_retransmit_after_disconnect(task))
1291 rpc_wake_up_queued_task_set_status(&xprt->pending,
1292 task, -ENOTCONN);
1293 }
1294 spin_unlock(&xprt->queue_lock);
1295}
1296
Trond Myklebust944b0422018-08-09 23:33:21 -04001297static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001298xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1299{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001300 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001301}
1302
1303/**
1304 * xprt_request_enqueue_transmit - queue a task for transmission
1305 * @task: pointer to rpc_task
1306 *
1307 * Add a task to the transmission queue.
1308 */
1309void
1310xprt_request_enqueue_transmit(struct rpc_task *task)
1311{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001312 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001313 struct rpc_xprt *xprt = req->rq_xprt;
1314
1315 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001316 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001317 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001318 /*
1319 * Requests that carry congestion control credits are added
1320 * to the head of the list to avoid starvation issues.
1321 */
1322 if (req->rq_cong) {
1323 xprt_clear_congestion_window_wait(xprt);
1324 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1325 if (pos->rq_cong)
1326 continue;
1327 /* Note: req is added _before_ pos */
1328 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1329 INIT_LIST_HEAD(&req->rq_xmit2);
1330 goto out;
1331 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001332 } else if (RPC_IS_SWAPPER(task)) {
1333 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1334 if (pos->rq_cong || pos->rq_bytes_sent)
1335 continue;
1336 if (RPC_IS_SWAPPER(pos->rq_task))
1337 continue;
1338 /* Note: req is added _before_ pos */
1339 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1340 INIT_LIST_HEAD(&req->rq_xmit2);
1341 goto out;
1342 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001343 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001344 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1345 if (pos->rq_task->tk_owner != task->tk_owner)
1346 continue;
1347 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1348 INIT_LIST_HEAD(&req->rq_xmit);
1349 goto out;
1350 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001351 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001352 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001353 INIT_LIST_HEAD(&req->rq_xmit2);
1354out:
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001355 atomic_long_inc(&xprt->xmit_queuelen);
Trond Myklebust944b0422018-08-09 23:33:21 -04001356 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1357 spin_unlock(&xprt->queue_lock);
1358 }
1359}
1360
1361/**
1362 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1363 * @task: pointer to rpc_task
1364 *
1365 * Remove a task from the transmission queue
1366 * Caller must hold xprt->queue_lock
1367 */
1368static void
1369xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1370{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001371 struct rpc_rqst *req = task->tk_rqstp;
1372
1373 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1374 return;
1375 if (!list_empty(&req->rq_xmit)) {
1376 list_del(&req->rq_xmit);
1377 if (!list_empty(&req->rq_xmit2)) {
1378 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1379 struct rpc_rqst, rq_xmit2);
1380 list_del(&req->rq_xmit2);
1381 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1382 }
1383 } else
1384 list_del(&req->rq_xmit2);
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001385 atomic_long_dec(&req->rq_xprt->xmit_queuelen);
Trond Myklebust944b0422018-08-09 23:33:21 -04001386}
1387
1388/**
1389 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1390 * @task: pointer to rpc_task
1391 *
1392 * Remove a task from the transmission queue
1393 */
1394static void
1395xprt_request_dequeue_transmit(struct rpc_task *task)
1396{
1397 struct rpc_rqst *req = task->tk_rqstp;
1398 struct rpc_xprt *xprt = req->rq_xprt;
1399
1400 spin_lock(&xprt->queue_lock);
1401 xprt_request_dequeue_transmit_locked(task);
1402 spin_unlock(&xprt->queue_lock);
1403}
1404
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001405/**
Trond Myklebustcc204d02019-09-10 13:01:35 -04001406 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1407 * @task: pointer to rpc_task
1408 *
1409 * Remove a task from the transmit and receive queues, and ensure that
1410 * it is not pinned by the receive work item.
1411 */
1412void
1413xprt_request_dequeue_xprt(struct rpc_task *task)
1414{
1415 struct rpc_rqst *req = task->tk_rqstp;
1416 struct rpc_xprt *xprt = req->rq_xprt;
1417
1418 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1419 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1420 xprt_is_pinned_rqst(req)) {
1421 spin_lock(&xprt->queue_lock);
1422 xprt_request_dequeue_transmit_locked(task);
1423 xprt_request_dequeue_receive_locked(task);
1424 while (xprt_is_pinned_rqst(req)) {
1425 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1426 spin_unlock(&xprt->queue_lock);
1427 xprt_wait_on_pinned_rqst(req);
1428 spin_lock(&xprt->queue_lock);
1429 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1430 }
1431 spin_unlock(&xprt->queue_lock);
1432 }
1433}
1434
1435/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001436 * xprt_request_prepare - prepare an encoded request for transport
1437 * @req: pointer to rpc_rqst
1438 *
1439 * Calls into the transport layer to do whatever is needed to prepare
1440 * the request for transmission or receive.
1441 */
1442void
1443xprt_request_prepare(struct rpc_rqst *req)
1444{
1445 struct rpc_xprt *xprt = req->rq_xprt;
1446
1447 if (xprt->ops->prepare_request)
1448 xprt->ops->prepare_request(req);
1449}
1450
1451/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001452 * xprt_request_need_retransmit - Test if a task needs retransmission
1453 * @task: pointer to rpc_task
1454 *
1455 * Test for whether a connection breakage requires the task to retransmit
1456 */
1457bool
1458xprt_request_need_retransmit(struct rpc_task *task)
1459{
1460 return xprt_request_retransmit_after_disconnect(task);
1461}
1462
1463/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001464 * xprt_prepare_transmit - reserve the transport before sending a request
1465 * @task: RPC task about to send a request
1466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001468bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct rpc_rqst *req = task->tk_rqstp;
1471 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001473 if (!xprt_lock_write(xprt, task)) {
1474 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001475 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001476 rpc_wake_up_queued_task_set_status(&xprt->sending,
1477 task, 0);
1478 return false;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001481 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001484void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001485{
Chuck Lever7638e0b2021-03-31 13:22:14 -04001486 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1487
1488 xprt_inject_disconnect(xprt);
1489 xprt_release_write(xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001490}
1491
Chuck Lever9903cd12005-08-11 16:25:26 -04001492/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001493 * xprt_request_transmit - send an RPC request on a transport
1494 * @req: pointer to request to transmit
1495 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001496 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001497 * This performs the transmission of a single request.
1498 * Note that if the request is not the same as snd_task, then it
1499 * does need to be pinned.
1500 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001501 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001502static int
1503xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001505 struct rpc_xprt *xprt = req->rq_xprt;
1506 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001507 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001508 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001509 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001511 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001512 if (xprt_request_data_received(task)) {
1513 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001514 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001515 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001516 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001517 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001518 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001519 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001520 }
Trond Myklebustae67bd32019-04-07 13:58:44 -04001521 if (RPC_SIGNALLED(task)) {
1522 status = -ERESTARTSYS;
1523 goto out_dequeue;
1524 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001527 /*
1528 * Update req->rq_ntrans before transmitting to avoid races with
1529 * xprt_update_rtt(), which needs to know that it is recording a
1530 * reply to the first transmission.
1531 */
1532 req->rq_ntrans++;
1533
Chuck Leverc509f15a2020-05-12 17:13:28 -04001534 trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001535 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001536 status = xprt->ops->send_request(req);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001537 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001538 req->rq_ntrans--;
Chuck Lever0c776682019-02-11 11:25:04 -05001539 trace_xprt_transmit(req, status);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001540 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001541 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001542
Chuck Levere936a592021-03-31 13:22:27 -04001543 if (is_retrans) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001544 task->tk_client->cl_stats->rpcretrans++;
Chuck Levere936a592021-03-31 13:22:27 -04001545 trace_xprt_retransmit(req);
1546 }
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001547
Chuck Lever4a068252015-05-11 14:02:25 -04001548 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Bryan Schumaker468f8612011-04-18 15:57:32 -04001550 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001551 spin_lock(&xprt->transport_lock);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001552
Trond Myklebustc8485e42009-03-11 14:37:59 -04001553 xprt->stat.sends++;
1554 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1555 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001556 xprt->stat.sending_u += xprt->sending.qlen;
1557 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustb5e92412019-05-02 11:21:08 -04001558 spin_unlock(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001559
1560 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001561out_dequeue:
Chuck Lever0c776682019-02-11 11:25:04 -05001562 trace_xprt_transmit(req, status);
Trond Myklebust944b0422018-08-09 23:33:21 -04001563 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001564 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1565 return status;
1566}
1567
1568/**
1569 * xprt_transmit - send an RPC request on a transport
1570 * @task: controlling RPC task
1571 *
1572 * Attempts to drain the transmit queue. On exit, either the transport
1573 * signalled an error that needs to be handled before transmission can
1574 * resume, or @task finished transmitting, and detected that it already
1575 * received a reply.
1576 */
1577void
1578xprt_transmit(struct rpc_task *task)
1579{
1580 struct rpc_rqst *next, *req = task->tk_rqstp;
1581 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever6f9f1722020-07-08 16:09:53 -04001582 int counter, status;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001583
1584 spin_lock(&xprt->queue_lock);
Chuck Lever6f9f1722020-07-08 16:09:53 -04001585 counter = 0;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001586 while (!list_empty(&xprt->xmit_queue)) {
Chuck Lever6f9f1722020-07-08 16:09:53 -04001587 if (++counter == 20)
1588 break;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001589 next = list_first_entry(&xprt->xmit_queue,
1590 struct rpc_rqst, rq_xmit);
1591 xprt_pin_rqst(next);
1592 spin_unlock(&xprt->queue_lock);
1593 status = xprt_request_transmit(next, task);
1594 if (status == -EBADMSG && next != req)
1595 status = 0;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001596 spin_lock(&xprt->queue_lock);
1597 xprt_unpin_rqst(next);
1598 if (status == 0) {
1599 if (!xprt_request_data_received(task) ||
1600 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1601 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001602 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001603 task->tk_status = status;
1604 break;
1605 }
1606 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Trond Myklebustba60eb22013-04-14 10:49:37 -04001609static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1610{
1611 set_bit(XPRT_CONGESTED, &xprt->state);
1612 rpc_sleep_on(&xprt->backlog, task, NULL);
1613}
1614
1615static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1616{
1617 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1618 clear_bit(XPRT_CONGESTED, &xprt->state);
1619}
1620
1621static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1622{
1623 bool ret = false;
1624
1625 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1626 goto out;
1627 spin_lock(&xprt->reserve_lock);
1628 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1629 rpc_sleep_on(&xprt->backlog, task, NULL);
1630 ret = true;
1631 }
1632 spin_unlock(&xprt->reserve_lock);
1633out:
1634 return ret;
1635}
1636
Trond Myklebust92ea0112017-06-20 19:35:39 -04001637static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001638{
1639 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1640
Chuck Leverff699ea82018-03-05 15:13:13 -05001641 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001642 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001643 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001644 spin_unlock(&xprt->reserve_lock);
1645 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1646 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001647 if (req != NULL)
1648 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001649 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001650 req = ERR_PTR(-ENOMEM);
1651out:
1652 return req;
1653}
1654
1655static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1656{
Chuck Leverff699ea82018-03-05 15:13:13 -05001657 if (xprt->num_reqs > xprt->min_reqs) {
1658 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001659 kfree(req);
1660 return true;
1661 }
1662 return false;
1663}
1664
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001665void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001667 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001669 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001671 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1672 list_del(&req->rq_list);
1673 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001675 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001676 if (!IS_ERR(req))
1677 goto out_init_req;
1678 switch (PTR_ERR(req)) {
1679 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001680 dprintk("RPC: dynamic allocation of request slot "
1681 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001682 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001683 break;
1684 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001685 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001686 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001687 fallthrough;
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001688 default:
1689 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001690 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001691 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001692 return;
1693out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001694 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1695 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001696 spin_unlock(&xprt->reserve_lock);
1697
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001698 task->tk_status = 0;
1699 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001701EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1702
Chuck Levera9cde232018-05-04 15:34:59 -04001703void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001704{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001705 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001706 if (!xprt_dynamic_free_slot(xprt, req)) {
1707 memset(req, 0, sizeof(*req)); /* mark unused */
1708 list_add(&req->rq_list, &xprt->free);
1709 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001710 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001711 spin_unlock(&xprt->reserve_lock);
1712}
Chuck Levera9cde232018-05-04 15:34:59 -04001713EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001714
Trond Myklebust21de0a92011-07-17 16:57:32 -04001715static void xprt_free_all_slots(struct rpc_xprt *xprt)
1716{
1717 struct rpc_rqst *req;
1718 while (!list_empty(&xprt->free)) {
1719 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1720 list_del(&req->rq_list);
1721 kfree(req);
1722 }
1723}
1724
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001725struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1726 unsigned int num_prealloc,
1727 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001728{
1729 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001730 struct rpc_rqst *req;
1731 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001732
1733 xprt = kzalloc(size, GFP_KERNEL);
1734 if (xprt == NULL)
1735 goto out;
1736
Trond Myklebust21de0a92011-07-17 16:57:32 -04001737 xprt_init(xprt, net);
1738
1739 for (i = 0; i < num_prealloc; i++) {
1740 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1741 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001742 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001743 list_add(&req->rq_list, &xprt->free);
1744 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001745 if (max_alloc > num_prealloc)
1746 xprt->max_reqs = max_alloc;
1747 else
1748 xprt->max_reqs = num_prealloc;
1749 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001750 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001751
1752 return xprt;
1753
1754out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001755 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001756out:
1757 return NULL;
1758}
1759EXPORT_SYMBOL_GPL(xprt_alloc);
1760
Pavel Emelyanove204e622010-09-29 16:03:13 +04001761void xprt_free(struct rpc_xprt *xprt)
1762{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001763 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001764 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001765 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001766}
1767EXPORT_SYMBOL_GPL(xprt_free);
1768
Trond Myklebust902c5882018-09-01 17:21:01 -04001769static void
1770xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1771{
1772 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1773}
1774
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001775static __be32
1776xprt_alloc_xid(struct rpc_xprt *xprt)
1777{
1778 __be32 xid;
1779
1780 spin_lock(&xprt->reserve_lock);
1781 xid = (__force __be32)xprt->xid++;
1782 spin_unlock(&xprt->reserve_lock);
1783 return xid;
1784}
1785
1786static void
1787xprt_init_xid(struct rpc_xprt *xprt)
1788{
1789 xprt->xid = prandom_u32();
1790}
1791
1792static void
1793xprt_request_init(struct rpc_task *task)
1794{
1795 struct rpc_xprt *xprt = task->tk_xprt;
1796 struct rpc_rqst *req = task->tk_rqstp;
1797
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001798 req->rq_task = task;
1799 req->rq_xprt = xprt;
1800 req->rq_buffer = NULL;
1801 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001802 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001803 req->rq_snd_buf.len = 0;
1804 req->rq_snd_buf.buflen = 0;
1805 req->rq_rcv_buf.len = 0;
1806 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001807 req->rq_snd_buf.bvec = NULL;
1808 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001809 req->rq_release_snd_buf = NULL;
Trond Myklebustda953062019-04-07 13:58:56 -04001810 xprt_init_majortimeo(task, req);
Chuck Lever09d2ba02020-07-08 16:09:21 -04001811
1812 trace_xprt_reserve(req);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001813}
1814
1815static void
1816xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1817{
1818 xprt->ops->alloc_slot(xprt, task);
1819 if (task->tk_rqstp != NULL)
1820 xprt_request_init(task);
1821}
1822
Chuck Lever9903cd12005-08-11 16:25:26 -04001823/**
1824 * xprt_reserve - allocate an RPC request slot
1825 * @task: RPC task requesting a slot allocation
1826 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001827 * If the transport is marked as being congested, or if no more
1828 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001829 * backlog queue.
1830 */
1831void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001833 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001835 task->tk_status = 0;
1836 if (task->tk_rqstp != NULL)
1837 return;
1838
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001839 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001840 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001841 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001842}
1843
1844/**
1845 * xprt_retry_reserve - allocate an RPC request slot
1846 * @task: RPC task requesting a slot allocation
1847 *
1848 * If no more slots are available, place the task on the transport's
1849 * backlog queue.
1850 * Note that the only difference with xprt_reserve is that we now
1851 * ignore the value of the XPRT_CONGESTED flag.
1852 */
1853void xprt_retry_reserve(struct rpc_task *task)
1854{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001855 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001856
1857 task->tk_status = 0;
1858 if (task->tk_rqstp != NULL)
1859 return;
1860
Trond Myklebustba60eb22013-04-14 10:49:37 -04001861 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001862 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Chuck Lever9903cd12005-08-11 16:25:26 -04001865/**
1866 * xprt_release - release an RPC request slot
1867 * @task: task which is finished with the slot
1868 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001870void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001872 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001873 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Trond Myklebust87ed5002013-01-07 14:30:46 -05001875 if (req == NULL) {
1876 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001877 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001878 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001881 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001882
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001883 xprt = req->rq_xprt;
Trond Myklebustcc204d02019-09-10 13:01:35 -04001884 xprt_request_dequeue_xprt(task);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001885 spin_lock(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001886 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001887 if (xprt->ops->release_request)
1888 xprt->ops->release_request(task);
Trond Myklebustad3331a2016-08-02 13:47:43 -04001889 xprt_schedule_autodisconnect(xprt);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001890 spin_unlock(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001891 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001892 xprt->ops->buf_free(task);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001893 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust0472e472019-02-19 13:00:13 -05001894 xdr_free_bvec(&req->rq_snd_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001895 if (req->rq_cred != NULL)
1896 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001898 if (req->rq_release_snd_buf)
1899 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001900
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001901 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001902 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001903 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001904 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Trond Myklebust902c5882018-09-01 17:21:01 -04001907#ifdef CONFIG_SUNRPC_BACKCHANNEL
1908void
1909xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1910{
1911 struct xdr_buf *xbufp = &req->rq_snd_buf;
1912
1913 task->tk_rqstp = req;
1914 req->rq_task = task;
1915 xprt_init_connect_cookie(req, req->rq_xprt);
1916 /*
1917 * Set up the xdr_buf length.
1918 * This also indicates that the buffer is XDR encoded already.
1919 */
1920 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1921 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001922}
1923#endif
1924
Trond Myklebust21de0a92011-07-17 16:57:32 -04001925static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001926{
Trond Myklebust30c51162015-02-24 20:31:39 -05001927 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001928
1929 spin_lock_init(&xprt->transport_lock);
1930 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001931 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001932
1933 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001934 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001935 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001936#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001937 spin_lock_init(&xprt->bc_pa_lock);
1938 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001939#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001940 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001941
Chuck Leverc2866762006-08-22 20:06:20 -04001942 xprt->last_used = jiffies;
1943 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001944 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001945
1946 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1947 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001948 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001949 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1950
Chuck Leverc2866762006-08-22 20:06:20 -04001951 xprt_init_xid(xprt);
1952
Trond Myklebust21de0a92011-07-17 16:57:32 -04001953 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001954}
1955
1956/**
1957 * xprt_create_transport - create an RPC transport
1958 * @args: rpc transport creation arguments
1959 *
1960 */
1961struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1962{
1963 struct rpc_xprt *xprt;
Trond Myklebust9bccd262020-11-10 12:58:22 -05001964 const struct xprt_class *t;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001965
Trond Myklebust9bccd262020-11-10 12:58:22 -05001966 t = xprt_class_find_by_ident(args->ident);
1967 if (!t) {
1968 dprintk("RPC: transport (%d) not supported\n", args->ident);
1969 return ERR_PTR(-EIO);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001970 }
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001971
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001972 xprt = t->setup(args);
Trond Myklebust9bccd262020-11-10 12:58:22 -05001973 xprt_class_release(t);
1974
Chuck Lever911813d2020-05-12 17:13:34 -04001975 if (IS_ERR(xprt))
Trond Myklebust21de0a92011-07-17 16:57:32 -04001976 goto out;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001977 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1978 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001979 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1980 if (xprt_has_timer(xprt))
Anna Schumaker502980e2019-06-18 14:57:33 -04001981 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001982 else
Kees Cookff861c42017-10-16 17:29:42 -07001983 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001984
1985 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1986 xprt_destroy(xprt);
1987 return ERR_PTR(-EINVAL);
1988 }
1989 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1990 if (xprt->servername == NULL) {
1991 xprt_destroy(xprt);
1992 return ERR_PTR(-ENOMEM);
1993 }
1994
Jeff Layton3f940092015-03-31 12:03:28 -04001995 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001996
Chuck Lever911813d2020-05-12 17:13:34 -04001997 trace_xprt_create(xprt);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001998out:
Chuck Leverc2866762006-08-22 20:06:20 -04001999 return xprt;
2000}
2001
Trond Myklebust528fd352017-10-19 12:13:10 -04002002static void xprt_destroy_cb(struct work_struct *work)
2003{
2004 struct rpc_xprt *xprt =
2005 container_of(work, struct rpc_xprt, task_cleanup);
2006
Chuck Lever911813d2020-05-12 17:13:34 -04002007 trace_xprt_destroy(xprt);
2008
Trond Myklebust528fd352017-10-19 12:13:10 -04002009 rpc_xprt_debugfs_unregister(xprt);
2010 rpc_destroy_wait_queue(&xprt->binding);
2011 rpc_destroy_wait_queue(&xprt->pending);
2012 rpc_destroy_wait_queue(&xprt->sending);
2013 rpc_destroy_wait_queue(&xprt->backlog);
2014 kfree(xprt->servername);
2015 /*
Trond Myklebust669996a2019-10-17 09:02:21 -04002016 * Destroy any existing back channel
2017 */
2018 xprt_destroy_backchannel(xprt, UINT_MAX);
2019
2020 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04002021 * Tear down transport state and free the rpc_xprt
2022 */
2023 xprt->ops->destroy(xprt);
2024}
2025
Chuck Lever9903cd12005-08-11 16:25:26 -04002026/**
2027 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04002028 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04002029 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04002031static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Trond Myklebust528fd352017-10-19 12:13:10 -04002033 /*
2034 * Exclude transport connect/disconnect handlers and autoclose
2035 */
Trond Myklebust79234c32015-09-18 15:53:24 -04002036 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
2037
Trond Myklebust0065db32006-01-03 09:55:56 +01002038 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002039
2040 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04002041 * Destroy sockets etc from the system workqueue so they can
2042 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04002043 */
Trond Myklebust528fd352017-10-19 12:13:10 -04002044 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2045 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust30c51162015-02-24 20:31:39 -05002048static void xprt_destroy_kref(struct kref *kref)
2049{
2050 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
2051}
2052
2053/**
2054 * xprt_get - return a reference to an RPC transport.
2055 * @xprt: pointer to the transport
2056 *
2057 */
2058struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
2059{
2060 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
2061 return xprt;
2062 return NULL;
2063}
2064EXPORT_SYMBOL_GPL(xprt_get);
2065
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002066/**
2067 * xprt_put - release a reference to an RPC transport.
2068 * @xprt: pointer to the transport
2069 *
2070 */
2071void xprt_put(struct rpc_xprt *xprt)
2072{
Trond Myklebust30c51162015-02-24 20:31:39 -05002073 if (xprt != NULL)
2074 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04002075}
Chuck Lever5d252f92016-01-07 14:50:10 -05002076EXPORT_SYMBOL_GPL(xprt_put);