blob: 642cc0f64e44cafaa1556e790090f168cb3ac00f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/xprt.c
3 *
4 * This is a generic RPC call interface supporting congestion avoidance,
5 * and asynchronous calls.
6 *
7 * The interface works like this:
8 *
9 * - When a process places a call, it allocates a request slot if
10 * one is available. Otherwise, it sleeps on the backlog queue
11 * (xprt_reserve).
12 * - Next, the caller puts together the RPC message, stuffs it into
Chuck Lever55aa4f52005-08-11 16:25:47 -040013 * the request struct, and calls xprt_transmit().
14 * - xprt_transmit sends the message and installs the caller on the
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040015 * transport's wait list. At the same time, if a reply is expected,
16 * it installs a timer that is run after the packet's timeout has
17 * expired.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040019 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * caller is woken up, and the timer removed.
21 * - When no reply arrives within the timeout interval, the timer is
22 * fired by the kernel and runs xprt_timer(). It either adjusts the
23 * timeout values (minor timeout) or wakes up the caller with a status
24 * of -ETIMEDOUT.
25 * - When the caller receives a notification from RPC that a reply arrived,
26 * it should release the RPC slot, and process the reply.
27 * If the call timed out, it may choose to retry the operation by
28 * adjusting the initial timeout value, and simply calling rpc_call
29 * again.
30 *
31 * Support for async RPC is done through a set of RPC-specific scheduling
32 * primitives that `transparently' work for processes as well as async
33 * tasks that rely on callbacks.
34 *
35 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
Chuck Lever55aa4f52005-08-11 16:25:47 -040036 *
37 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/module.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040043#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040045#include <linux/net.h>
Chuck Leverff839972010-05-07 13:34:47 -040046#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Chuck Levera246b012005-08-11 16:25:23 -040048#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050049#include <linux/sunrpc/metrics.h>
Trond Myklebustc9acb422010-03-19 15:36:22 -040050#include <linux/sunrpc/bc_xprt.h>
Trond Myklebustfda1bfe2015-02-14 17:48:49 -050051#include <linux/rcupdate.h>
Trond Myklebusta1231fd2019-02-18 10:02:29 -050052#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jeff Layton3705ad62014-10-28 14:24:13 -040054#include <trace/events/sunrpc.h>
55
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040056#include "sunrpc.h"
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Local variables
60 */
61
Jeff Laytonf895b252014-11-17 16:58:04 -050062#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063# define RPCDBG_FACILITY RPCDBG_XPRT
64#endif
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Local functions
68 */
Trond Myklebust21de0a92011-07-17 16:57:32 -040069static void xprt_init(struct rpc_xprt *xprt, struct net *net);
Chuck Lever37ac86c2018-05-04 15:34:53 -040070static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Trond Myklebust4e0038b2012-03-01 17:01:05 -050071static void xprt_destroy(struct rpc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jiri Slaby5ba03e82007-11-22 19:40:22 +080073static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040074static LIST_HEAD(xprt_list);
75
Trond Myklebust9e910bf2019-04-07 13:58:53 -040076static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
77{
78 unsigned long timeout = jiffies + req->rq_timeout;
79
80 if (time_before(timeout, req->rq_majortimeo))
81 return timeout;
82 return req->rq_majortimeo;
83}
84
Chuck Lever12a80462005-08-25 16:25:51 -070085/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040086 * xprt_register_transport - register a transport implementation
87 * @transport: transport to register
88 *
89 * If a transport implementation is loaded as a kernel module, it can
90 * call this interface to make itself known to the RPC client.
91 *
92 * Returns:
93 * 0: transport successfully registered
94 * -EEXIST: transport already registered
95 * -EINVAL: transport module being unloaded
96 */
97int xprt_register_transport(struct xprt_class *transport)
98{
99 struct xprt_class *t;
100 int result;
101
102 result = -EEXIST;
103 spin_lock(&xprt_list_lock);
104 list_for_each_entry(t, &xprt_list, list) {
105 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400106 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400107 goto out;
108 }
109
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400110 list_add_tail(&transport->list, &xprt_list);
111 printk(KERN_INFO "RPC: Registered %s transport module.\n",
112 transport->name);
113 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400114
115out:
116 spin_unlock(&xprt_list_lock);
117 return result;
118}
119EXPORT_SYMBOL_GPL(xprt_register_transport);
120
121/**
122 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800123 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400124 *
125 * Returns:
126 * 0: transport successfully unregistered
127 * -ENOENT: transport never registered
128 */
129int xprt_unregister_transport(struct xprt_class *transport)
130{
131 struct xprt_class *t;
132 int result;
133
134 result = 0;
135 spin_lock(&xprt_list_lock);
136 list_for_each_entry(t, &xprt_list, list) {
137 if (t == transport) {
138 printk(KERN_INFO
139 "RPC: Unregistered %s transport module.\n",
140 transport->name);
141 list_del_init(&transport->list);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400142 goto out;
143 }
144 }
145 result = -ENOENT;
146
147out:
148 spin_unlock(&xprt_list_lock);
149 return result;
150}
151EXPORT_SYMBOL_GPL(xprt_unregister_transport);
152
153/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400154 * xprt_load_transport - load a transport implementation
155 * @transport_name: transport to load
156 *
157 * Returns:
158 * 0: transport successfully loaded
159 * -ENOENT: transport module not available
160 */
161int xprt_load_transport(const char *transport_name)
162{
163 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400164 int result;
165
166 result = 0;
167 spin_lock(&xprt_list_lock);
168 list_for_each_entry(t, &xprt_list, list) {
169 if (strcmp(t->name, transport_name) == 0) {
170 spin_unlock(&xprt_list_lock);
171 goto out;
172 }
173 }
174 spin_unlock(&xprt_list_lock);
Alex Riesenef7ffe82010-05-24 14:33:05 -0700175 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400176out:
177 return result;
178}
179EXPORT_SYMBOL_GPL(xprt_load_transport);
180
Trond Myklebustc5445772018-09-03 23:39:27 -0400181static void xprt_clear_locked(struct rpc_xprt *xprt)
182{
183 xprt->snd_task = NULL;
184 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
185 smp_mb__before_atomic();
186 clear_bit(XPRT_LOCKED, &xprt->state);
187 smp_mb__after_atomic();
188 } else
189 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
190}
191
Tom Talpey441e3e22009-03-11 14:37:56 -0400192/**
Chuck Lever12a80462005-08-25 16:25:51 -0700193 * xprt_reserve_xprt - serialize write access to transports
194 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000195 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700196 *
197 * This prevents mixing the payload of separate requests, and prevents
198 * transport connects from colliding with writes. No congestion control
199 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400201int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Chuck Lever12a80462005-08-25 16:25:51 -0700203 struct rpc_rqst *req = task->tk_rqstp;
204
205 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
206 if (task == xprt->snd_task)
207 return 1;
Chuck Lever12a80462005-08-25 16:25:51 -0700208 goto out_sleep;
209 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400210 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
211 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700212 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500213
Chuck Lever12a80462005-08-25 16:25:51 -0700214 return 1;
215
Trond Myklebustc5445772018-09-03 23:39:27 -0400216out_unlock:
217 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700218out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500219 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700220 task->tk_pid, xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700221 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400222 if (RPC_IS_SOFT(task))
223 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400224 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400225 else
226 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700227 return 0;
228}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400229EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700230
Trond Myklebust75891f52018-09-03 17:37:36 -0400231static bool
232xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
233{
234 return test_bit(XPRT_CWND_WAIT, &xprt->state);
235}
236
237static void
238xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
239{
240 if (!list_empty(&xprt->xmit_queue)) {
241 /* Peek at head of queue to see if it can make progress */
242 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
243 rq_xmit)->rq_cong)
244 return;
245 }
246 set_bit(XPRT_CWND_WAIT, &xprt->state);
247}
248
249static void
250xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
251{
252 if (!RPCXPRT_CONGESTED(xprt))
253 clear_bit(XPRT_CWND_WAIT, &xprt->state);
254}
255
Chuck Lever12a80462005-08-25 16:25:51 -0700256/*
257 * xprt_reserve_xprt_cong - serialize write access to transports
258 * @task: task that is requesting access to the transport
259 *
260 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
261 * integrated into the decision of whether a request is allowed to be
262 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400263 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700264 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400265int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct rpc_rqst *req = task->tk_rqstp;
268
Chuck Lever2226feb2005-08-11 16:25:38 -0400269 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (task == xprt->snd_task)
271 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto out_sleep;
273 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400274 if (req == NULL) {
275 xprt->snd_task = task;
276 return 1;
277 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400278 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
279 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400280 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 xprt->snd_task = task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 1;
283 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400284out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100285 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500287 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 task->tk_status = -EAGAIN;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400289 if (RPC_IS_SOFT(task))
290 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400291 xprt_request_timeout(req));
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400292 else
293 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400296EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Chuck Lever12a80462005-08-25 16:25:51 -0700298static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int retval;
301
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400302 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
303 return 1;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400304 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400305 retval = xprt->ops->reserve_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400306 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return retval;
308}
309
Trond Myklebust961a8282012-01-17 22:57:37 -0500310static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Trond Myklebust961a8282012-01-17 22:57:37 -0500312 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700313
Chuck Lever49e9a892005-08-25 16:25:51 -0700314 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500315 return true;
316}
Chuck Lever49e9a892005-08-25 16:25:51 -0700317
Trond Myklebust961a8282012-01-17 22:57:37 -0500318static void __xprt_lock_write_next(struct rpc_xprt *xprt)
319{
320 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
321 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400322 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
323 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400324 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
325 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500326 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400327out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100328 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700329}
330
Trond Myklebust961a8282012-01-17 22:57:37 -0500331static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
332{
333 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
334 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400335 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
336 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400337 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500338 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400339 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400340 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500341 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100343 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Chuck Lever49e9a892005-08-25 16:25:51 -0700346/**
347 * xprt_release_xprt - allow other requests to use a transport
348 * @xprt: transport with other tasks potentially waiting
349 * @task: task that is releasing access to the transport
350 *
351 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700353void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100356 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 __xprt_lock_write_next(xprt);
358 }
359}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400360EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Chuck Lever49e9a892005-08-25 16:25:51 -0700362/**
363 * xprt_release_xprt_cong - allow other requests to use a transport
364 * @xprt: transport with other tasks potentially waiting
365 * @task: task that is releasing access to the transport
366 *
367 * Note that "task" can be NULL. Another task is awoken to use the
368 * transport if the transport's congestion window allows it.
369 */
370void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
371{
372 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100373 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700374 __xprt_lock_write_next_cong(xprt);
375 }
376}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400377EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700378
379static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400381 if (xprt->snd_task != task)
382 return;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400383 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700384 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400385 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * Van Jacobson congestion avoidance. Check if the congestion window
390 * overflowed. Put the task to sleep if this is the case.
391 */
392static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400393__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (req->rq_cong)
396 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500397 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Trond Myklebust75891f52018-09-03 17:37:36 -0400398 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
399 if (RPCXPRT_CONGESTED(xprt)) {
400 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 req->rq_cong = 1;
404 xprt->cong += RPC_CWNDSCALE;
405 return 1;
406}
407
408/*
409 * Adjust the congestion window, and wake up the next task
410 * that has been sleeping due to congestion
411 */
412static void
413__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
414{
415 if (!req->rq_cong)
416 return;
417 req->rq_cong = 0;
418 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400419 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700420 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Chuck Lever46c0ee82005-08-25 16:25:52 -0700423/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400424 * xprt_request_get_cong - Request congestion control credits
425 * @xprt: pointer to transport
426 * @req: pointer to RPC request
427 *
428 * Useful for transports that require congestion control.
429 */
430bool
431xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
432{
433 bool ret = false;
434
435 if (req->rq_cong)
436 return true;
437 spin_lock_bh(&xprt->transport_lock);
438 ret = __xprt_get_cong(xprt, req) != 0;
439 spin_unlock_bh(&xprt->transport_lock);
440 return ret;
441}
442EXPORT_SYMBOL_GPL(xprt_request_get_cong);
443
444/**
Chuck Levera58dd392005-08-25 16:25:53 -0700445 * xprt_release_rqst_cong - housekeeping when request is complete
446 * @task: RPC request that recently completed
447 *
448 * Useful for transports that require congestion control.
449 */
450void xprt_release_rqst_cong(struct rpc_task *task)
451{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500452 struct rpc_rqst *req = task->tk_rqstp;
453
454 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700455}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400456EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700457
Trond Myklebust75891f52018-09-03 17:37:36 -0400458/*
459 * Clear the congestion window wait flag and wake up the next
460 * entry on xprt->sending
461 */
462static void
463xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
464{
465 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
466 spin_lock_bh(&xprt->transport_lock);
467 __xprt_lock_write_next_cong(xprt);
468 spin_unlock_bh(&xprt->transport_lock);
469 }
470}
471
Chuck Levera58dd392005-08-25 16:25:53 -0700472/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700473 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500474 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700475 * @task: recently completed RPC request used to adjust window
476 * @result: result code of completed RPC request
477 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400478 * The transport code maintains an estimate on the maximum number of out-
479 * standing RPC requests, using a smoothed version of the congestion
480 * avoidance implemented in 44BSD. This is basically the Van Jacobson
481 * congestion algorithm: If a retransmit occurs, the congestion window is
482 * halved; otherwise, it is incremented by 1/cwnd when
483 *
484 * - a reply is received and
485 * - a full number of requests are outstanding and
486 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500488void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700490 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700491 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (result >= 0 && cwnd <= xprt->cong) {
494 /* The (cwnd >> 1) term makes sure
495 * the result gets rounded properly. */
496 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
497 if (cwnd > RPC_MAXCWND(xprt))
498 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700499 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 } else if (result == -ETIMEDOUT) {
501 cwnd >>= 1;
502 if (cwnd < RPC_CWNDSCALE)
503 cwnd = RPC_CWNDSCALE;
504 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500505 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 xprt->cong, xprt->cwnd, cwnd);
507 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700508 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400510EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Chuck Lever44fbac22005-08-11 16:25:44 -0400512/**
513 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
514 * @xprt: transport with waiting tasks
515 * @status: result code to plant in each task before waking it
516 *
517 */
518void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
519{
520 if (status < 0)
521 rpc_wake_up_status(&xprt->pending, status);
522 else
523 rpc_wake_up(&xprt->pending);
524}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400525EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400526
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400527/**
528 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400529 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500530 *
531 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
532 * we don't in general want to force a socket disconnection due to
533 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400534 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400535void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400536{
Trond Myklebustc5445772018-09-03 23:39:27 -0400537 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400538}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400539EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400540
Trond Myklebustc5445772018-09-03 23:39:27 -0400541static bool
542xprt_clear_write_space_locked(struct rpc_xprt *xprt)
543{
544 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
545 __xprt_lock_write_next(xprt);
546 dprintk("RPC: write space: waking waiting task on "
547 "xprt %p\n", xprt);
548 return true;
549 }
550 return false;
551}
552
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400553/**
554 * xprt_write_space - wake the task waiting for transport output buffer space
555 * @xprt: transport with waiting tasks
556 *
557 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
558 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400559bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400560{
Trond Myklebustc5445772018-09-03 23:39:27 -0400561 bool ret;
562
563 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
564 return false;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400565 spin_lock_bh(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400566 ret = xprt_clear_write_space_locked(xprt);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400567 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400568 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400569}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400570EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static void xprt_reset_majortimeo(struct rpc_rqst *req)
573{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500574 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 req->rq_majortimeo = req->rq_timeout;
577 if (to->to_exponential)
578 req->rq_majortimeo <<= to->to_retries;
579 else
580 req->rq_majortimeo += to->to_increment * to->to_retries;
581 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
582 req->rq_majortimeo = to->to_maxval;
583 req->rq_majortimeo += jiffies;
584}
585
Chuck Lever9903cd12005-08-11 16:25:26 -0400586/**
587 * xprt_adjust_timeout - adjust timeout values for next retransmit
588 * @req: RPC request containing parameters to use for the adjustment
589 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591int xprt_adjust_timeout(struct rpc_rqst *req)
592{
593 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500594 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int status = 0;
596
597 if (time_before(jiffies, req->rq_majortimeo)) {
598 if (to->to_exponential)
599 req->rq_timeout <<= 1;
600 else
601 req->rq_timeout += to->to_increment;
602 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
603 req->rq_timeout = to->to_maxval;
604 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 } else {
606 req->rq_timeout = to->to_initval;
607 req->rq_retries = 0;
608 xprt_reset_majortimeo(req);
609 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400610 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400612 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 status = -ETIMEDOUT;
614 }
615
616 if (req->rq_timeout == 0) {
617 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
618 req->rq_timeout = 5 * HZ;
619 }
620 return status;
621}
622
David Howells65f27f32006-11-22 14:55:48 +0000623static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
David Howells65f27f32006-11-22 14:55:48 +0000625 struct rpc_xprt *xprt =
626 container_of(work, struct rpc_xprt, task_cleanup);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500627 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Trond Myklebust66af1e552007-11-06 10:18:36 -0500629 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400630 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400632 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500633 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Chuck Lever9903cd12005-08-11 16:25:26 -0400636/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500637 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400638 * @xprt: transport to flag for disconnect
639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500641void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Chuck Lever46121cf2007-01-31 12:14:08 -0500643 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400644 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400646 xprt_clear_write_space_locked(xprt);
Trond Myklebust27adc782019-03-15 08:01:16 -0400647 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400648 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500650EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Trond Myklebust66af1e552007-11-06 10:18:36 -0500652/**
653 * xprt_force_disconnect - force a transport to disconnect
654 * @xprt: transport to disconnect
655 *
656 */
657void xprt_force_disconnect(struct rpc_xprt *xprt)
658{
659 /* Don't race with the test_bit() in xprt_clear_locked() */
660 spin_lock_bh(&xprt->transport_lock);
661 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
662 /* Try to schedule an autoclose RPC call */
663 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400664 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust0445f922018-12-17 13:34:59 -0500665 else if (xprt->snd_task)
666 rpc_wake_up_queued_task_set_status(&xprt->pending,
667 xprt->snd_task, -ENOTCONN);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500668 spin_unlock_bh(&xprt->transport_lock);
669}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400670EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500671
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400672static unsigned int
673xprt_connect_cookie(struct rpc_xprt *xprt)
674{
675 return READ_ONCE(xprt->connect_cookie);
676}
677
678static bool
679xprt_request_retransmit_after_disconnect(struct rpc_task *task)
680{
681 struct rpc_rqst *req = task->tk_rqstp;
682 struct rpc_xprt *xprt = req->rq_xprt;
683
684 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
685 !xprt_connected(xprt);
686}
687
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400688/**
689 * xprt_conditional_disconnect - force a transport to disconnect
690 * @xprt: transport to disconnect
691 * @cookie: 'connection cookie'
692 *
693 * This attempts to break the connection if and only if 'cookie' matches
694 * the current transport 'connection cookie'. It ensures that we don't
695 * try to break the connection more than once when we need to retransmit
696 * a batch of RPC requests.
697 *
698 */
699void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
700{
701 /* Don't race with the test_bit() in xprt_clear_locked() */
702 spin_lock_bh(&xprt->transport_lock);
703 if (cookie != xprt->connect_cookie)
704 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100705 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400706 goto out;
707 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
708 /* Try to schedule an autoclose RPC call */
709 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400710 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400711 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400712out:
713 spin_unlock_bh(&xprt->transport_lock);
714}
715
Trond Myklebustad3331a2016-08-02 13:47:43 -0400716static bool
717xprt_has_timer(const struct rpc_xprt *xprt)
718{
719 return xprt->idle_timeout != 0;
720}
721
722static void
723xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
724 __must_hold(&xprt->transport_lock)
725{
Trond Myklebust95f76912018-09-07 08:35:22 -0400726 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400727 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static void
Kees Cookff861c42017-10-16 17:29:42 -0700731xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Kees Cookff861c42017-10-16 17:29:42 -0700733 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400735 spin_lock(&xprt->transport_lock);
Trond Myklebust95f76912018-09-07 08:35:22 -0400736 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto out_abort;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400738 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
739 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400740 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400742 spin_unlock(&xprt->transport_lock);
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400743 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return;
745out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400746 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500749bool xprt_lock_connect(struct rpc_xprt *xprt,
750 struct rpc_task *task,
751 void *cookie)
752{
753 bool ret = false;
754
755 spin_lock_bh(&xprt->transport_lock);
756 if (!test_bit(XPRT_LOCKED, &xprt->state))
757 goto out;
758 if (xprt->snd_task != task)
759 goto out;
760 xprt->snd_task = cookie;
761 ret = true;
762out:
763 spin_unlock_bh(&xprt->transport_lock);
764 return ret;
765}
766
767void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
768{
769 spin_lock_bh(&xprt->transport_lock);
770 if (xprt->snd_task != cookie)
771 goto out;
772 if (!test_bit(XPRT_LOCKED, &xprt->state))
773 goto out;
774 xprt->snd_task =NULL;
775 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400776 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500777out:
778 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400779 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500780}
781
Chuck Lever9903cd12005-08-11 16:25:26 -0400782/**
783 * xprt_connect - schedule a transport connect operation
784 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 *
786 */
787void xprt_connect(struct rpc_task *task)
788{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500789 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Chuck Lever46121cf2007-01-31 12:14:08 -0500791 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 xprt, (xprt_connected(xprt) ? "is" : "is not"));
793
Chuck Leverec739ef2006-08-22 20:06:15 -0400794 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400795 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return;
797 }
798 if (!xprt_lock_write(xprt, task))
799 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500800
801 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
802 xprt->ops->close(xprt);
803
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500804 if (!xprt_connected(xprt)) {
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100805 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400806 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
Trond Myklebust9e910bf2019-04-07 13:58:53 -0400807 xprt_request_timeout(task->tk_rqstp));
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400808
809 if (test_bit(XPRT_CLOSING, &xprt->state))
810 return;
811 if (xprt_test_and_set_connecting(xprt))
812 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500813 /* Race breaker */
814 if (!xprt_connected(xprt)) {
815 xprt->stat.connect_start = jiffies;
816 xprt->ops->connect(xprt, task);
817 } else {
818 xprt_clear_connecting(xprt);
819 task->tk_status = 0;
820 rpc_wake_up_queued_task(&xprt->pending, task);
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500823 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Trond Myklebust95f76912018-09-07 08:35:22 -0400826enum xprt_xid_rb_cmp {
827 XID_RB_EQUAL,
828 XID_RB_LEFT,
829 XID_RB_RIGHT,
830};
831static enum xprt_xid_rb_cmp
832xprt_xid_cmp(__be32 xid1, __be32 xid2)
833{
834 if (xid1 == xid2)
835 return XID_RB_EQUAL;
836 if ((__force u32)xid1 < (__force u32)xid2)
837 return XID_RB_LEFT;
838 return XID_RB_RIGHT;
839}
840
841static struct rpc_rqst *
842xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
843{
844 struct rb_node *n = xprt->recv_queue.rb_node;
845 struct rpc_rqst *req;
846
847 while (n != NULL) {
848 req = rb_entry(n, struct rpc_rqst, rq_recv);
849 switch (xprt_xid_cmp(xid, req->rq_xid)) {
850 case XID_RB_LEFT:
851 n = n->rb_left;
852 break;
853 case XID_RB_RIGHT:
854 n = n->rb_right;
855 break;
856 case XID_RB_EQUAL:
857 return req;
858 }
859 }
860 return NULL;
861}
862
863static void
864xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
865{
866 struct rb_node **p = &xprt->recv_queue.rb_node;
867 struct rb_node *n = NULL;
868 struct rpc_rqst *req;
869
870 while (*p != NULL) {
871 n = *p;
872 req = rb_entry(n, struct rpc_rqst, rq_recv);
873 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
874 case XID_RB_LEFT:
875 p = &n->rb_left;
876 break;
877 case XID_RB_RIGHT:
878 p = &n->rb_right;
879 break;
880 case XID_RB_EQUAL:
881 WARN_ON_ONCE(new != req);
882 return;
883 }
884 }
885 rb_link_node(&new->rq_recv, n, p);
886 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
887}
888
889static void
890xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
891{
892 rb_erase(&req->rq_recv, &xprt->recv_queue);
893}
894
Chuck Lever9903cd12005-08-11 16:25:26 -0400895/**
896 * xprt_lookup_rqst - find an RPC request corresponding to an XID
897 * @xprt: transport on which the original request was transmitted
898 * @xid: RPC XID of incoming reply
899 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400900 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700902struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400904 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Trond Myklebust95f76912018-09-07 08:35:22 -0400906 entry = xprt_request_rb_find(xprt, xid);
907 if (entry != NULL) {
908 trace_xprt_lookup_rqst(xprt, xid, 0);
909 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
910 return entry;
911 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500912
913 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
914 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400915 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500916 xprt->stat.bad_xids++;
917 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400919EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400921static bool
922xprt_is_pinned_rqst(struct rpc_rqst *req)
923{
924 return atomic_read(&req->rq_pin) != 0;
925}
926
Trond Myklebust729749b2017-08-13 10:03:59 -0400927/**
928 * xprt_pin_rqst - Pin a request on the transport receive list
929 * @req: Request to pin
930 *
931 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400932 * so should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400933 */
934void xprt_pin_rqst(struct rpc_rqst *req)
935{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400936 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400937}
Chuck Lever9590d082017-08-23 17:05:58 -0400938EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400939
940/**
941 * xprt_unpin_rqst - Unpin a request on the transport receive list
942 * @req: Request to pin
943 *
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400944 * Caller should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400945 */
946void xprt_unpin_rqst(struct rpc_rqst *req)
947{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400948 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
949 atomic_dec(&req->rq_pin);
950 return;
951 }
952 if (atomic_dec_and_test(&req->rq_pin))
953 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400954}
Chuck Lever9590d082017-08-23 17:05:58 -0400955EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400956
957static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -0400958{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400959 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -0400960}
961
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400962static bool
963xprt_request_data_received(struct rpc_task *task)
964{
965 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
966 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
967}
968
969static bool
970xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
971{
972 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
973 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
974}
975
976/**
977 * xprt_request_enqueue_receive - Add an request to the receive queue
978 * @task: RPC task
979 *
980 */
981void
982xprt_request_enqueue_receive(struct rpc_task *task)
983{
984 struct rpc_rqst *req = task->tk_rqstp;
985 struct rpc_xprt *xprt = req->rq_xprt;
986
987 if (!xprt_request_need_enqueue_receive(task, req))
988 return;
989 spin_lock(&xprt->queue_lock);
990
991 /* Update the softirq receive buffer */
992 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
993 sizeof(req->rq_private_buf));
994
995 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -0400996 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400997 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
998 spin_unlock(&xprt->queue_lock);
999
1000 xprt_reset_majortimeo(req);
1001 /* Turn off autodisconnect */
1002 del_singleshot_timer_sync(&xprt->timer);
1003}
1004
1005/**
1006 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1007 * @task: RPC task
1008 *
1009 * Caller must hold xprt->queue_lock.
1010 */
1011static void
1012xprt_request_dequeue_receive_locked(struct rpc_task *task)
1013{
Trond Myklebust95f76912018-09-07 08:35:22 -04001014 struct rpc_rqst *req = task->tk_rqstp;
1015
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001016 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001017 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001018}
1019
Chuck Leverecd465e2018-03-05 15:12:57 -05001020/**
1021 * xprt_update_rtt - Update RPC RTT statistics
1022 * @task: RPC request that recently completed
1023 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001024 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001025 */
1026void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001028 struct rpc_rqst *req = task->tk_rqstp;
1029 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001030 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001031 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Chuck Lever1570c1e2005-08-25 16:25:52 -07001033 if (timer) {
1034 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001035 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001036 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001038}
Chuck Leverecd465e2018-03-05 15:12:57 -05001039EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Chuck Lever1570c1e2005-08-25 16:25:52 -07001041/**
1042 * xprt_complete_rqst - called when reply processing is complete
1043 * @task: RPC request that recently completed
1044 * @copied: actual number of bytes received from the transport
1045 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001046 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001047 */
1048void xprt_complete_rqst(struct rpc_task *task, int copied)
1049{
1050 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001051 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Chuck Lever1570c1e2005-08-25 16:25:52 -07001053 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1054 task->tk_pid, ntohl(req->rq_xid), copied);
Jeff Layton3705ad62014-10-28 14:24:13 -04001055 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Trond Myklebustfda13932008-02-22 16:34:12 -05001057 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001058
Trond Myklebust1e799b62008-03-21 16:19:41 -04001059 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001060 /* Ensure all writes are done before we update */
1061 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001062 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001063 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001064 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001065 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001067EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Chuck Lever46c0ee82005-08-25 16:25:52 -07001069static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001071 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct rpc_xprt *xprt = req->rq_xprt;
1073
Trond Myklebust5d008372008-02-22 16:34:17 -05001074 if (task->tk_status != -ETIMEDOUT)
1075 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001076
Chuck Lever82476d92018-01-03 15:38:25 -05001077 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001078 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001079 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001080 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001081 } else
1082 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Chuck Lever9903cd12005-08-11 16:25:26 -04001085/**
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001086 * xprt_wait_for_reply_request_def - wait for reply
1087 * @task: pointer to rpc_task
1088 *
1089 * Set a request's retransmit timeout based on the transport's
1090 * default timeout parameters. Used by transports that don't adjust
1091 * the retransmit timeout based on round-trip time estimation,
1092 * and put the task to sleep on the pending queue.
1093 */
1094void xprt_wait_for_reply_request_def(struct rpc_task *task)
1095{
1096 struct rpc_rqst *req = task->tk_rqstp;
1097
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001098 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
Trond Myklebust9e910bf2019-04-07 13:58:53 -04001099 xprt_request_timeout(req));
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001100}
1101EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1102
1103/**
1104 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1105 * @task: pointer to rpc_task
1106 *
1107 * Set a request's retransmit timeout using the RTT estimator,
1108 * and put the task to sleep on the pending queue.
1109 */
1110void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1111{
1112 int timer = task->tk_msg.rpc_proc->p_timer;
1113 struct rpc_clnt *clnt = task->tk_client;
1114 struct rpc_rtt *rtt = clnt->cl_rtt;
1115 struct rpc_rqst *req = task->tk_rqstp;
1116 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001117 unsigned long timeout;
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001118
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001119 timeout = rpc_calc_rto(rtt, timer);
1120 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1121 if (timeout > max_timeout || timeout == 0)
1122 timeout = max_timeout;
1123 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1124 jiffies + timeout);
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001125}
1126EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1127
1128/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001129 * xprt_request_wait_receive - wait for the reply to an RPC request
1130 * @task: RPC task about to send a request
1131 *
1132 */
1133void xprt_request_wait_receive(struct rpc_task *task)
1134{
1135 struct rpc_rqst *req = task->tk_rqstp;
1136 struct rpc_xprt *xprt = req->rq_xprt;
1137
1138 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1139 return;
1140 /*
1141 * Sleep on the pending queue if we're expecting a reply.
1142 * The spinlock ensures atomicity between the test of
1143 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1144 */
1145 spin_lock(&xprt->queue_lock);
1146 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
Trond Myklebust8ba6a922019-04-07 13:58:46 -04001147 xprt->ops->wait_for_reply_request(task);
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001148 /*
1149 * Send an extra queue wakeup call if the
1150 * connection was dropped in case the call to
1151 * rpc_sleep_on() raced.
1152 */
1153 if (xprt_request_retransmit_after_disconnect(task))
1154 rpc_wake_up_queued_task_set_status(&xprt->pending,
1155 task, -ENOTCONN);
1156 }
1157 spin_unlock(&xprt->queue_lock);
1158}
1159
Trond Myklebust944b0422018-08-09 23:33:21 -04001160static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001161xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1162{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001163 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001164}
1165
1166/**
1167 * xprt_request_enqueue_transmit - queue a task for transmission
1168 * @task: pointer to rpc_task
1169 *
1170 * Add a task to the transmission queue.
1171 */
1172void
1173xprt_request_enqueue_transmit(struct rpc_task *task)
1174{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001175 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001176 struct rpc_xprt *xprt = req->rq_xprt;
1177
1178 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001179 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001180 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001181 /*
1182 * Requests that carry congestion control credits are added
1183 * to the head of the list to avoid starvation issues.
1184 */
1185 if (req->rq_cong) {
1186 xprt_clear_congestion_window_wait(xprt);
1187 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1188 if (pos->rq_cong)
1189 continue;
1190 /* Note: req is added _before_ pos */
1191 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1192 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001193 trace_xprt_enq_xmit(task, 1);
Trond Myklebust75891f52018-09-03 17:37:36 -04001194 goto out;
1195 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001196 } else if (RPC_IS_SWAPPER(task)) {
1197 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1198 if (pos->rq_cong || pos->rq_bytes_sent)
1199 continue;
1200 if (RPC_IS_SWAPPER(pos->rq_task))
1201 continue;
1202 /* Note: req is added _before_ pos */
1203 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1204 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001205 trace_xprt_enq_xmit(task, 2);
Trond Myklebust86aeee02018-09-08 14:22:41 -04001206 goto out;
1207 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001208 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001209 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1210 if (pos->rq_task->tk_owner != task->tk_owner)
1211 continue;
1212 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1213 INIT_LIST_HEAD(&req->rq_xmit);
Chuck Lever0c776682019-02-11 11:25:04 -05001214 trace_xprt_enq_xmit(task, 3);
Trond Myklebust75891f52018-09-03 17:37:36 -04001215 goto out;
1216 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001217 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001218 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001219 INIT_LIST_HEAD(&req->rq_xmit2);
Chuck Lever0c776682019-02-11 11:25:04 -05001220 trace_xprt_enq_xmit(task, 4);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001221out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001222 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1223 spin_unlock(&xprt->queue_lock);
1224 }
1225}
1226
1227/**
1228 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1229 * @task: pointer to rpc_task
1230 *
1231 * Remove a task from the transmission queue
1232 * Caller must hold xprt->queue_lock
1233 */
1234static void
1235xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1236{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001237 struct rpc_rqst *req = task->tk_rqstp;
1238
1239 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1240 return;
1241 if (!list_empty(&req->rq_xmit)) {
1242 list_del(&req->rq_xmit);
1243 if (!list_empty(&req->rq_xmit2)) {
1244 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1245 struct rpc_rqst, rq_xmit2);
1246 list_del(&req->rq_xmit2);
1247 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1248 }
1249 } else
1250 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001251}
1252
1253/**
1254 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1255 * @task: pointer to rpc_task
1256 *
1257 * Remove a task from the transmission queue
1258 */
1259static void
1260xprt_request_dequeue_transmit(struct rpc_task *task)
1261{
1262 struct rpc_rqst *req = task->tk_rqstp;
1263 struct rpc_xprt *xprt = req->rq_xprt;
1264
1265 spin_lock(&xprt->queue_lock);
1266 xprt_request_dequeue_transmit_locked(task);
1267 spin_unlock(&xprt->queue_lock);
1268}
1269
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001270/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001271 * xprt_request_prepare - prepare an encoded request for transport
1272 * @req: pointer to rpc_rqst
1273 *
1274 * Calls into the transport layer to do whatever is needed to prepare
1275 * the request for transmission or receive.
1276 */
1277void
1278xprt_request_prepare(struct rpc_rqst *req)
1279{
1280 struct rpc_xprt *xprt = req->rq_xprt;
1281
1282 if (xprt->ops->prepare_request)
1283 xprt->ops->prepare_request(req);
1284}
1285
1286/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001287 * xprt_request_need_retransmit - Test if a task needs retransmission
1288 * @task: pointer to rpc_task
1289 *
1290 * Test for whether a connection breakage requires the task to retransmit
1291 */
1292bool
1293xprt_request_need_retransmit(struct rpc_task *task)
1294{
1295 return xprt_request_retransmit_after_disconnect(task);
1296}
1297
1298/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001299 * xprt_prepare_transmit - reserve the transport before sending a request
1300 * @task: RPC task about to send a request
1301 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001303bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct rpc_rqst *req = task->tk_rqstp;
1306 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Chuck Lever46121cf2007-01-31 12:14:08 -05001308 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001310 if (!xprt_lock_write(xprt, task)) {
1311 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001312 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001313 rpc_wake_up_queued_task_set_status(&xprt->sending,
1314 task, 0);
1315 return false;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001318 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001321void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001322{
Rahul Iyer343952f2009-04-01 09:23:17 -04001323 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001324}
1325
Chuck Lever9903cd12005-08-11 16:25:26 -04001326/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001327 * xprt_request_transmit - send an RPC request on a transport
1328 * @req: pointer to request to transmit
1329 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001330 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001331 * This performs the transmission of a single request.
1332 * Note that if the request is not the same as snd_task, then it
1333 * does need to be pinned.
1334 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001335 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001336static int
1337xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001339 struct rpc_xprt *xprt = req->rq_xprt;
1340 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001341 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001342 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001343 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001345 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001346 if (xprt_request_data_received(task)) {
1347 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001348 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001349 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001350 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001351 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001352 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001353 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001354 }
Trond Myklebusta79f1942019-02-27 15:37:36 -05001355 if (task->tk_ops->rpc_call_prepare_transmit) {
1356 task->tk_ops->rpc_call_prepare_transmit(task,
1357 task->tk_calldata);
1358 status = task->tk_status;
1359 if (status < 0)
1360 goto out_dequeue;
1361 }
Trond Myklebustae67bd32019-04-07 13:58:44 -04001362 if (RPC_SIGNALLED(task)) {
1363 status = -ERESTARTSYS;
1364 goto out_dequeue;
1365 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001368 /*
1369 * Update req->rq_ntrans before transmitting to avoid races with
1370 * xprt_update_rtt(), which needs to know that it is recording a
1371 * reply to the first transmission.
1372 */
1373 req->rq_ntrans++;
1374
Trond Myklebust90d91b02017-12-14 21:24:08 -05001375 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001376 status = xprt->ops->send_request(req);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001377 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001378 req->rq_ntrans--;
Chuck Lever0c776682019-02-11 11:25:04 -05001379 trace_xprt_transmit(req, status);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001380 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001381 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001382
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001383 if (is_retrans)
1384 task->tk_client->cl_stats->rpcretrans++;
1385
Chuck Lever4a068252015-05-11 14:02:25 -04001386 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Bryan Schumaker468f8612011-04-18 15:57:32 -04001388 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001389 spin_lock_bh(&xprt->transport_lock);
1390
Trond Myklebustc8485e42009-03-11 14:37:59 -04001391 xprt->stat.sends++;
1392 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1393 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001394 xprt->stat.sending_u += xprt->sending.qlen;
1395 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001396 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001397
1398 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001399out_dequeue:
Chuck Lever0c776682019-02-11 11:25:04 -05001400 trace_xprt_transmit(req, status);
Trond Myklebust944b0422018-08-09 23:33:21 -04001401 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001402 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1403 return status;
1404}
1405
1406/**
1407 * xprt_transmit - send an RPC request on a transport
1408 * @task: controlling RPC task
1409 *
1410 * Attempts to drain the transmit queue. On exit, either the transport
1411 * signalled an error that needs to be handled before transmission can
1412 * resume, or @task finished transmitting, and detected that it already
1413 * received a reply.
1414 */
1415void
1416xprt_transmit(struct rpc_task *task)
1417{
1418 struct rpc_rqst *next, *req = task->tk_rqstp;
1419 struct rpc_xprt *xprt = req->rq_xprt;
1420 int status;
1421
1422 spin_lock(&xprt->queue_lock);
1423 while (!list_empty(&xprt->xmit_queue)) {
1424 next = list_first_entry(&xprt->xmit_queue,
1425 struct rpc_rqst, rq_xmit);
1426 xprt_pin_rqst(next);
1427 spin_unlock(&xprt->queue_lock);
1428 status = xprt_request_transmit(next, task);
1429 if (status == -EBADMSG && next != req)
1430 status = 0;
1431 cond_resched();
1432 spin_lock(&xprt->queue_lock);
1433 xprt_unpin_rqst(next);
1434 if (status == 0) {
1435 if (!xprt_request_data_received(task) ||
1436 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1437 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001438 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001439 task->tk_status = status;
1440 break;
1441 }
1442 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Trond Myklebustba60eb22013-04-14 10:49:37 -04001445static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1446{
1447 set_bit(XPRT_CONGESTED, &xprt->state);
1448 rpc_sleep_on(&xprt->backlog, task, NULL);
1449}
1450
1451static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1452{
1453 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1454 clear_bit(XPRT_CONGESTED, &xprt->state);
1455}
1456
1457static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1458{
1459 bool ret = false;
1460
1461 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1462 goto out;
1463 spin_lock(&xprt->reserve_lock);
1464 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1465 rpc_sleep_on(&xprt->backlog, task, NULL);
1466 ret = true;
1467 }
1468 spin_unlock(&xprt->reserve_lock);
1469out:
1470 return ret;
1471}
1472
Trond Myklebust92ea0112017-06-20 19:35:39 -04001473static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001474{
1475 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1476
Chuck Leverff699ea82018-03-05 15:13:13 -05001477 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001478 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001479 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001480 spin_unlock(&xprt->reserve_lock);
1481 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1482 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001483 if (req != NULL)
1484 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001485 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001486 req = ERR_PTR(-ENOMEM);
1487out:
1488 return req;
1489}
1490
1491static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1492{
Chuck Leverff699ea82018-03-05 15:13:13 -05001493 if (xprt->num_reqs > xprt->min_reqs) {
1494 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001495 kfree(req);
1496 return true;
1497 }
1498 return false;
1499}
1500
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001501void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001503 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001505 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001507 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1508 list_del(&req->rq_list);
1509 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001511 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001512 if (!IS_ERR(req))
1513 goto out_init_req;
1514 switch (PTR_ERR(req)) {
1515 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001516 dprintk("RPC: dynamic allocation of request slot "
1517 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001518 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001519 break;
1520 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001521 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001522 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001523 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001524 default:
1525 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001526 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001527 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001528 return;
1529out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001530 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1531 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001532 spin_unlock(&xprt->reserve_lock);
1533
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001534 task->tk_status = 0;
1535 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001537EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1538
Chuck Levera9cde232018-05-04 15:34:59 -04001539void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001540{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001541 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001542 if (!xprt_dynamic_free_slot(xprt, req)) {
1543 memset(req, 0, sizeof(*req)); /* mark unused */
1544 list_add(&req->rq_list, &xprt->free);
1545 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001546 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001547 spin_unlock(&xprt->reserve_lock);
1548}
Chuck Levera9cde232018-05-04 15:34:59 -04001549EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001550
Trond Myklebust21de0a92011-07-17 16:57:32 -04001551static void xprt_free_all_slots(struct rpc_xprt *xprt)
1552{
1553 struct rpc_rqst *req;
1554 while (!list_empty(&xprt->free)) {
1555 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1556 list_del(&req->rq_list);
1557 kfree(req);
1558 }
1559}
1560
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001561struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1562 unsigned int num_prealloc,
1563 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001564{
1565 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001566 struct rpc_rqst *req;
1567 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001568
1569 xprt = kzalloc(size, GFP_KERNEL);
1570 if (xprt == NULL)
1571 goto out;
1572
Trond Myklebust21de0a92011-07-17 16:57:32 -04001573 xprt_init(xprt, net);
1574
1575 for (i = 0; i < num_prealloc; i++) {
1576 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1577 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001578 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001579 list_add(&req->rq_list, &xprt->free);
1580 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001581 if (max_alloc > num_prealloc)
1582 xprt->max_reqs = max_alloc;
1583 else
1584 xprt->max_reqs = num_prealloc;
1585 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001586 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001587
1588 return xprt;
1589
1590out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001591 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001592out:
1593 return NULL;
1594}
1595EXPORT_SYMBOL_GPL(xprt_alloc);
1596
Pavel Emelyanove204e622010-09-29 16:03:13 +04001597void xprt_free(struct rpc_xprt *xprt)
1598{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001599 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001600 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001601 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001602}
1603EXPORT_SYMBOL_GPL(xprt_free);
1604
Trond Myklebust902c5882018-09-01 17:21:01 -04001605static void
1606xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1607{
1608 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1609}
1610
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001611static __be32
1612xprt_alloc_xid(struct rpc_xprt *xprt)
1613{
1614 __be32 xid;
1615
1616 spin_lock(&xprt->reserve_lock);
1617 xid = (__force __be32)xprt->xid++;
1618 spin_unlock(&xprt->reserve_lock);
1619 return xid;
1620}
1621
1622static void
1623xprt_init_xid(struct rpc_xprt *xprt)
1624{
1625 xprt->xid = prandom_u32();
1626}
1627
1628static void
1629xprt_request_init(struct rpc_task *task)
1630{
1631 struct rpc_xprt *xprt = task->tk_xprt;
1632 struct rpc_rqst *req = task->tk_rqstp;
1633
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001634 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1635 req->rq_task = task;
1636 req->rq_xprt = xprt;
1637 req->rq_buffer = NULL;
1638 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001639 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001640 req->rq_snd_buf.len = 0;
1641 req->rq_snd_buf.buflen = 0;
1642 req->rq_rcv_buf.len = 0;
1643 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001644 req->rq_snd_buf.bvec = NULL;
1645 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001646 req->rq_release_snd_buf = NULL;
1647 xprt_reset_majortimeo(req);
1648 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1649 req, ntohl(req->rq_xid));
1650}
1651
1652static void
1653xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1654{
1655 xprt->ops->alloc_slot(xprt, task);
1656 if (task->tk_rqstp != NULL)
1657 xprt_request_init(task);
1658}
1659
Chuck Lever9903cd12005-08-11 16:25:26 -04001660/**
1661 * xprt_reserve - allocate an RPC request slot
1662 * @task: RPC task requesting a slot allocation
1663 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001664 * If the transport is marked as being congested, or if no more
1665 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001666 * backlog queue.
1667 */
1668void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001670 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001672 task->tk_status = 0;
1673 if (task->tk_rqstp != NULL)
1674 return;
1675
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001676 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001677 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001678 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001679}
1680
1681/**
1682 * xprt_retry_reserve - allocate an RPC request slot
1683 * @task: RPC task requesting a slot allocation
1684 *
1685 * If no more slots are available, place the task on the transport's
1686 * backlog queue.
1687 * Note that the only difference with xprt_reserve is that we now
1688 * ignore the value of the XPRT_CONGESTED flag.
1689 */
1690void xprt_retry_reserve(struct rpc_task *task)
1691{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001692 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001693
1694 task->tk_status = 0;
1695 if (task->tk_rqstp != NULL)
1696 return;
1697
Trond Myklebustba60eb22013-04-14 10:49:37 -04001698 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001699 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001702static void
1703xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1704{
1705 struct rpc_xprt *xprt = req->rq_xprt;
1706
Trond Myklebust944b0422018-08-09 23:33:21 -04001707 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1708 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001709 xprt_is_pinned_rqst(req)) {
1710 spin_lock(&xprt->queue_lock);
Trond Myklebust944b0422018-08-09 23:33:21 -04001711 xprt_request_dequeue_transmit_locked(task);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001712 xprt_request_dequeue_receive_locked(task);
1713 while (xprt_is_pinned_rqst(req)) {
1714 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1715 spin_unlock(&xprt->queue_lock);
1716 xprt_wait_on_pinned_rqst(req);
1717 spin_lock(&xprt->queue_lock);
1718 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1719 }
1720 spin_unlock(&xprt->queue_lock);
1721 }
1722}
1723
Chuck Lever9903cd12005-08-11 16:25:26 -04001724/**
1725 * xprt_release - release an RPC request slot
1726 * @task: task which is finished with the slot
1727 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001729void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001731 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001732 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Trond Myklebust87ed5002013-01-07 14:30:46 -05001734 if (req == NULL) {
1735 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001736 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001737 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001740 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001741
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001742 xprt = req->rq_xprt;
Weston Andros Adamson0a702192012-02-17 13:15:24 -05001743 if (task->tk_ops->rpc_count_stats != NULL)
1744 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1745 else if (task->tk_client)
1746 rpc_count_iostats(task, task->tk_client->cl_metrics);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001747 xprt_request_dequeue_all(task, req);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001748 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001749 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001750 if (xprt->ops->release_request)
1751 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 xprt->last_used = jiffies;
Trond Myklebustad3331a2016-08-02 13:47:43 -04001753 xprt_schedule_autodisconnect(xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001754 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001755 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001756 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001757 xprt_inject_disconnect(xprt);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001758 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust0472e472019-02-19 13:00:13 -05001759 xdr_free_bvec(&req->rq_snd_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001760 if (req->rq_cred != NULL)
1761 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001763 if (req->rq_release_snd_buf)
1764 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001765
Chuck Lever46121cf2007-01-31 12:14:08 -05001766 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001767 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001768 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001769 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001770 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Trond Myklebust902c5882018-09-01 17:21:01 -04001773#ifdef CONFIG_SUNRPC_BACKCHANNEL
1774void
1775xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1776{
1777 struct xdr_buf *xbufp = &req->rq_snd_buf;
1778
1779 task->tk_rqstp = req;
1780 req->rq_task = task;
1781 xprt_init_connect_cookie(req, req->rq_xprt);
1782 /*
1783 * Set up the xdr_buf length.
1784 * This also indicates that the buffer is XDR encoded already.
1785 */
1786 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1787 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001788}
1789#endif
1790
Trond Myklebust21de0a92011-07-17 16:57:32 -04001791static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001792{
Trond Myklebust30c51162015-02-24 20:31:39 -05001793 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001794
1795 spin_lock_init(&xprt->transport_lock);
1796 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001797 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001798
1799 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001800 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001801 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001802#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001803 spin_lock_init(&xprt->bc_pa_lock);
1804 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001805#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001806 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001807
Chuck Leverc2866762006-08-22 20:06:20 -04001808 xprt->last_used = jiffies;
1809 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001810 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001811
1812 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1813 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001814 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001815 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1816
Chuck Leverc2866762006-08-22 20:06:20 -04001817 xprt_init_xid(xprt);
1818
Trond Myklebust21de0a92011-07-17 16:57:32 -04001819 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001820}
1821
1822/**
1823 * xprt_create_transport - create an RPC transport
1824 * @args: rpc transport creation arguments
1825 *
1826 */
1827struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1828{
1829 struct rpc_xprt *xprt;
1830 struct xprt_class *t;
1831
1832 spin_lock(&xprt_list_lock);
1833 list_for_each_entry(t, &xprt_list, list) {
1834 if (t->ident == args->ident) {
1835 spin_unlock(&xprt_list_lock);
1836 goto found;
1837 }
1838 }
1839 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001840 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001841 return ERR_PTR(-EIO);
1842
1843found:
1844 xprt = t->setup(args);
1845 if (IS_ERR(xprt)) {
1846 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1847 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001848 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001849 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001850 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1851 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001852 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1853 if (xprt_has_timer(xprt))
Trond Myklebust43123582019-04-07 13:58:52 -04001854 timer_setup(&xprt->timer,
1855 xprt_init_autodisconnect,
1856 TIMER_DEFERRABLE);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001857 else
Kees Cookff861c42017-10-16 17:29:42 -07001858 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001859
1860 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1861 xprt_destroy(xprt);
1862 return ERR_PTR(-EINVAL);
1863 }
1864 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1865 if (xprt->servername == NULL) {
1866 xprt_destroy(xprt);
1867 return ERR_PTR(-ENOMEM);
1868 }
1869
Jeff Layton3f940092015-03-31 12:03:28 -04001870 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001871
Chuck Lever46121cf2007-01-31 12:14:08 -05001872 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001873 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001874out:
Chuck Leverc2866762006-08-22 20:06:20 -04001875 return xprt;
1876}
1877
Trond Myklebust528fd352017-10-19 12:13:10 -04001878static void xprt_destroy_cb(struct work_struct *work)
1879{
1880 struct rpc_xprt *xprt =
1881 container_of(work, struct rpc_xprt, task_cleanup);
1882
1883 rpc_xprt_debugfs_unregister(xprt);
1884 rpc_destroy_wait_queue(&xprt->binding);
1885 rpc_destroy_wait_queue(&xprt->pending);
1886 rpc_destroy_wait_queue(&xprt->sending);
1887 rpc_destroy_wait_queue(&xprt->backlog);
1888 kfree(xprt->servername);
1889 /*
1890 * Tear down transport state and free the rpc_xprt
1891 */
1892 xprt->ops->destroy(xprt);
1893}
1894
Chuck Lever9903cd12005-08-11 16:25:26 -04001895/**
1896 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001897 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001900static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Chuck Lever46121cf2007-01-31 12:14:08 -05001902 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001903
Trond Myklebust528fd352017-10-19 12:13:10 -04001904 /*
1905 * Exclude transport connect/disconnect handlers and autoclose
1906 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001907 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1908
Trond Myklebust0065db32006-01-03 09:55:56 +01001909 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001910
1911 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001912 * Destroy sockets etc from the system workqueue so they can
1913 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001914 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001915 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1916 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Trond Myklebust30c51162015-02-24 20:31:39 -05001919static void xprt_destroy_kref(struct kref *kref)
1920{
1921 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1922}
1923
1924/**
1925 * xprt_get - return a reference to an RPC transport.
1926 * @xprt: pointer to the transport
1927 *
1928 */
1929struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1930{
1931 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1932 return xprt;
1933 return NULL;
1934}
1935EXPORT_SYMBOL_GPL(xprt_get);
1936
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001937/**
1938 * xprt_put - release a reference to an RPC transport.
1939 * @xprt: pointer to the transport
1940 *
1941 */
1942void xprt_put(struct rpc_xprt *xprt)
1943{
Trond Myklebust30c51162015-02-24 20:31:39 -05001944 if (xprt != NULL)
1945 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001946}
Chuck Lever5d252f92016-01-07 14:50:10 -05001947EXPORT_SYMBOL_GPL(xprt_put);