blob: ce927002862a675a9f1169d12fbeb6999984a1c6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jeff Layton3705ad62014-10-28 14:24:13 -040053#include <trace/events/sunrpc.h>
54
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040055#include "sunrpc.h"
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Local variables
59 */
60
Jeff Laytonf895b252014-11-17 16:58:04 -050061#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062# define RPCDBG_FACILITY RPCDBG_XPRT
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * Local functions
67 */
Trond Myklebust21de0a92011-07-17 16:57:32 -040068static void xprt_init(struct rpc_xprt *xprt, struct net *net);
Chuck Lever37ac86c2018-05-04 15:34:53 -040069static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void xprt_connect_status(struct rpc_task *task);
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
Chuck Lever12a80462005-08-25 16:25:51 -070076/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040077 * xprt_register_transport - register a transport implementation
78 * @transport: transport to register
79 *
80 * If a transport implementation is loaded as a kernel module, it can
81 * call this interface to make itself known to the RPC client.
82 *
83 * Returns:
84 * 0: transport successfully registered
85 * -EEXIST: transport already registered
86 * -EINVAL: transport module being unloaded
87 */
88int xprt_register_transport(struct xprt_class *transport)
89{
90 struct xprt_class *t;
91 int result;
92
93 result = -EEXIST;
94 spin_lock(&xprt_list_lock);
95 list_for_each_entry(t, &xprt_list, list) {
96 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -040097 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040098 goto out;
99 }
100
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400101 list_add_tail(&transport->list, &xprt_list);
102 printk(KERN_INFO "RPC: Registered %s transport module.\n",
103 transport->name);
104 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400105
106out:
107 spin_unlock(&xprt_list_lock);
108 return result;
109}
110EXPORT_SYMBOL_GPL(xprt_register_transport);
111
112/**
113 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800114 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400115 *
116 * Returns:
117 * 0: transport successfully unregistered
118 * -ENOENT: transport never registered
119 */
120int xprt_unregister_transport(struct xprt_class *transport)
121{
122 struct xprt_class *t;
123 int result;
124
125 result = 0;
126 spin_lock(&xprt_list_lock);
127 list_for_each_entry(t, &xprt_list, list) {
128 if (t == transport) {
129 printk(KERN_INFO
130 "RPC: Unregistered %s transport module.\n",
131 transport->name);
132 list_del_init(&transport->list);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400133 goto out;
134 }
135 }
136 result = -ENOENT;
137
138out:
139 spin_unlock(&xprt_list_lock);
140 return result;
141}
142EXPORT_SYMBOL_GPL(xprt_unregister_transport);
143
144/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400145 * xprt_load_transport - load a transport implementation
146 * @transport_name: transport to load
147 *
148 * Returns:
149 * 0: transport successfully loaded
150 * -ENOENT: transport module not available
151 */
152int xprt_load_transport(const char *transport_name)
153{
154 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400155 int result;
156
157 result = 0;
158 spin_lock(&xprt_list_lock);
159 list_for_each_entry(t, &xprt_list, list) {
160 if (strcmp(t->name, transport_name) == 0) {
161 spin_unlock(&xprt_list_lock);
162 goto out;
163 }
164 }
165 spin_unlock(&xprt_list_lock);
Alex Riesenef7ffe82010-05-24 14:33:05 -0700166 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400167out:
168 return result;
169}
170EXPORT_SYMBOL_GPL(xprt_load_transport);
171
Trond Myklebustc5445772018-09-03 23:39:27 -0400172static void xprt_clear_locked(struct rpc_xprt *xprt)
173{
174 xprt->snd_task = NULL;
175 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
176 smp_mb__before_atomic();
177 clear_bit(XPRT_LOCKED, &xprt->state);
178 smp_mb__after_atomic();
179 } else
180 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
181}
182
Tom Talpey441e3e22009-03-11 14:37:56 -0400183/**
Chuck Lever12a80462005-08-25 16:25:51 -0700184 * xprt_reserve_xprt - serialize write access to transports
185 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000186 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700187 *
188 * This prevents mixing the payload of separate requests, and prevents
189 * transport connects from colliding with writes. No congestion control
190 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400192int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Chuck Lever12a80462005-08-25 16:25:51 -0700194 struct rpc_rqst *req = task->tk_rqstp;
195
196 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
197 if (task == xprt->snd_task)
198 return 1;
Chuck Lever12a80462005-08-25 16:25:51 -0700199 goto out_sleep;
200 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400201 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
202 goto out_unlock;
Chuck Lever12a80462005-08-25 16:25:51 -0700203 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500204
Chuck Lever12a80462005-08-25 16:25:51 -0700205 return 1;
206
Trond Myklebustc5445772018-09-03 23:39:27 -0400207out_unlock:
208 xprt_clear_locked(xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700209out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500210 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700211 task->tk_pid, xprt);
Trond Myklebustf05d54e2018-09-03 23:11:15 -0400212 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
Chuck Lever12a80462005-08-25 16:25:51 -0700213 task->tk_status = -EAGAIN;
Trond Myklebust79c99152018-09-09 13:53:05 -0400214 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700215 return 0;
216}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400217EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700218
Trond Myklebust75891f52018-09-03 17:37:36 -0400219static bool
220xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
221{
222 return test_bit(XPRT_CWND_WAIT, &xprt->state);
223}
224
225static void
226xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
227{
228 if (!list_empty(&xprt->xmit_queue)) {
229 /* Peek at head of queue to see if it can make progress */
230 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
231 rq_xmit)->rq_cong)
232 return;
233 }
234 set_bit(XPRT_CWND_WAIT, &xprt->state);
235}
236
237static void
238xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
239{
240 if (!RPCXPRT_CONGESTED(xprt))
241 clear_bit(XPRT_CWND_WAIT, &xprt->state);
242}
243
Chuck Lever12a80462005-08-25 16:25:51 -0700244/*
245 * xprt_reserve_xprt_cong - serialize write access to transports
246 * @task: task that is requesting access to the transport
247 *
248 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
249 * integrated into the decision of whether a request is allowed to be
250 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400251 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700252 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400253int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct rpc_rqst *req = task->tk_rqstp;
256
Chuck Lever2226feb2005-08-11 16:25:38 -0400257 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (task == xprt->snd_task)
259 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out_sleep;
261 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400262 if (req == NULL) {
263 xprt->snd_task = task;
264 return 1;
265 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400266 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
267 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400268 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 xprt->snd_task = task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 1;
271 }
Trond Myklebustc5445772018-09-03 23:39:27 -0400272out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100273 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500275 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Trond Myklebustf05d54e2018-09-03 23:11:15 -0400276 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 task->tk_status = -EAGAIN;
Trond Myklebust79c99152018-09-09 13:53:05 -0400278 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400281EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Chuck Lever12a80462005-08-25 16:25:51 -0700283static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int retval;
286
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400287 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
288 return 1;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400289 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400290 retval = xprt->ops->reserve_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400291 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return retval;
293}
294
Trond Myklebust961a8282012-01-17 22:57:37 -0500295static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Trond Myklebust961a8282012-01-17 22:57:37 -0500297 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700298
Chuck Lever49e9a892005-08-25 16:25:51 -0700299 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500300 return true;
301}
Chuck Lever49e9a892005-08-25 16:25:51 -0700302
Trond Myklebust961a8282012-01-17 22:57:37 -0500303static void __xprt_lock_write_next(struct rpc_xprt *xprt)
304{
305 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
306 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400307 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
308 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400309 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
310 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500311 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400312out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100313 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700314}
315
Trond Myklebust961a8282012-01-17 22:57:37 -0500316static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
317{
318 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
319 return;
Trond Myklebustc5445772018-09-03 23:39:27 -0400320 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
321 goto out_unlock;
Trond Myklebust75891f52018-09-03 17:37:36 -0400322 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500323 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400324 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400325 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100328 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Chuck Lever49e9a892005-08-25 16:25:51 -0700331/**
332 * xprt_release_xprt - allow other requests to use a transport
333 * @xprt: transport with other tasks potentially waiting
334 * @task: task that is releasing access to the transport
335 *
336 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700338void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100341 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 __xprt_lock_write_next(xprt);
343 }
344}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400345EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Chuck Lever49e9a892005-08-25 16:25:51 -0700347/**
348 * xprt_release_xprt_cong - allow other requests to use a transport
349 * @xprt: transport with other tasks potentially waiting
350 * @task: task that is releasing access to the transport
351 *
352 * Note that "task" can be NULL. Another task is awoken to use the
353 * transport if the transport's congestion window allows it.
354 */
355void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
356{
357 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100358 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700359 __xprt_lock_write_next_cong(xprt);
360 }
361}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400362EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700363
364static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Trond Myklebustbd79bc52018-09-07 19:38:55 -0400366 if (xprt->snd_task != task)
367 return;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400368 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700369 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400370 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * Van Jacobson congestion avoidance. Check if the congestion window
375 * overflowed. Put the task to sleep if this is the case.
376 */
377static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400378__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (req->rq_cong)
381 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500382 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Trond Myklebust75891f52018-09-03 17:37:36 -0400383 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
384 if (RPCXPRT_CONGESTED(xprt)) {
385 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 req->rq_cong = 1;
389 xprt->cong += RPC_CWNDSCALE;
390 return 1;
391}
392
393/*
394 * Adjust the congestion window, and wake up the next task
395 * that has been sleeping due to congestion
396 */
397static void
398__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
399{
400 if (!req->rq_cong)
401 return;
402 req->rq_cong = 0;
403 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400404 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700405 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Chuck Lever46c0ee82005-08-25 16:25:52 -0700408/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400409 * xprt_request_get_cong - Request congestion control credits
410 * @xprt: pointer to transport
411 * @req: pointer to RPC request
412 *
413 * Useful for transports that require congestion control.
414 */
415bool
416xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
417{
418 bool ret = false;
419
420 if (req->rq_cong)
421 return true;
422 spin_lock_bh(&xprt->transport_lock);
423 ret = __xprt_get_cong(xprt, req) != 0;
424 spin_unlock_bh(&xprt->transport_lock);
425 return ret;
426}
427EXPORT_SYMBOL_GPL(xprt_request_get_cong);
428
429/**
Chuck Levera58dd392005-08-25 16:25:53 -0700430 * xprt_release_rqst_cong - housekeeping when request is complete
431 * @task: RPC request that recently completed
432 *
433 * Useful for transports that require congestion control.
434 */
435void xprt_release_rqst_cong(struct rpc_task *task)
436{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500437 struct rpc_rqst *req = task->tk_rqstp;
438
439 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700440}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400441EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700442
Trond Myklebust75891f52018-09-03 17:37:36 -0400443/*
444 * Clear the congestion window wait flag and wake up the next
445 * entry on xprt->sending
446 */
447static void
448xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
449{
450 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
451 spin_lock_bh(&xprt->transport_lock);
452 __xprt_lock_write_next_cong(xprt);
453 spin_unlock_bh(&xprt->transport_lock);
454 }
455}
456
Chuck Levera58dd392005-08-25 16:25:53 -0700457/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700458 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500459 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700460 * @task: recently completed RPC request used to adjust window
461 * @result: result code of completed RPC request
462 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400463 * The transport code maintains an estimate on the maximum number of out-
464 * standing RPC requests, using a smoothed version of the congestion
465 * avoidance implemented in 44BSD. This is basically the Van Jacobson
466 * congestion algorithm: If a retransmit occurs, the congestion window is
467 * halved; otherwise, it is incremented by 1/cwnd when
468 *
469 * - a reply is received and
470 * - a full number of requests are outstanding and
471 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500473void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700475 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700476 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (result >= 0 && cwnd <= xprt->cong) {
479 /* The (cwnd >> 1) term makes sure
480 * the result gets rounded properly. */
481 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
482 if (cwnd > RPC_MAXCWND(xprt))
483 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700484 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else if (result == -ETIMEDOUT) {
486 cwnd >>= 1;
487 if (cwnd < RPC_CWNDSCALE)
488 cwnd = RPC_CWNDSCALE;
489 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500490 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 xprt->cong, xprt->cwnd, cwnd);
492 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700493 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400495EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Chuck Lever44fbac22005-08-11 16:25:44 -0400497/**
498 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
499 * @xprt: transport with waiting tasks
500 * @status: result code to plant in each task before waking it
501 *
502 */
503void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
504{
505 if (status < 0)
506 rpc_wake_up_status(&xprt->pending, status);
507 else
508 rpc_wake_up(&xprt->pending);
509}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400510EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400511
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400512/**
513 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
Trond Myklebustc5445772018-09-03 23:39:27 -0400514 * @xprt: transport
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500515 *
516 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
517 * we don't in general want to force a socket disconnection due to
518 * an incomplete RPC call transmission.
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400519 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400520void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400521{
Trond Myklebustc5445772018-09-03 23:39:27 -0400522 set_bit(XPRT_WRITE_SPACE, &xprt->state);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400523}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400524EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400525
Trond Myklebustc5445772018-09-03 23:39:27 -0400526static bool
527xprt_clear_write_space_locked(struct rpc_xprt *xprt)
528{
529 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
530 __xprt_lock_write_next(xprt);
531 dprintk("RPC: write space: waking waiting task on "
532 "xprt %p\n", xprt);
533 return true;
534 }
535 return false;
536}
537
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400538/**
539 * xprt_write_space - wake the task waiting for transport output buffer space
540 * @xprt: transport with waiting tasks
541 *
542 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
543 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400544bool xprt_write_space(struct rpc_xprt *xprt)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400545{
Trond Myklebustc5445772018-09-03 23:39:27 -0400546 bool ret;
547
548 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
549 return false;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400550 spin_lock_bh(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400551 ret = xprt_clear_write_space_locked(xprt);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400552 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustc5445772018-09-03 23:39:27 -0400553 return ret;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400554}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400555EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400556
Chuck Leverfe3aca22005-08-25 16:25:50 -0700557/**
558 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
559 * @task: task whose timeout is to be set
560 *
561 * Set a request's retransmit timeout based on the transport's
562 * default timeout parameters. Used by transports that don't adjust
563 * the retransmit timeout based on round-trip time estimation.
564 */
565void xprt_set_retrans_timeout_def(struct rpc_task *task)
566{
567 task->tk_timeout = task->tk_rqstp->rq_timeout;
568}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400569EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700570
Ben Hutchings2c530402012-07-10 10:55:09 +0000571/**
Chuck Leverfe3aca22005-08-25 16:25:50 -0700572 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
573 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800574 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700575 * Set a request's retransmit timeout using the RTT estimator.
576 */
577void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
578{
579 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500580 struct rpc_clnt *clnt = task->tk_client;
581 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700582 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500583 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700584
585 task->tk_timeout = rpc_calc_rto(rtt, timer);
586 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
587 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
588 task->tk_timeout = max_timeout;
589}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400590EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static void xprt_reset_majortimeo(struct rpc_rqst *req)
593{
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
596 req->rq_majortimeo = req->rq_timeout;
597 if (to->to_exponential)
598 req->rq_majortimeo <<= to->to_retries;
599 else
600 req->rq_majortimeo += to->to_increment * to->to_retries;
601 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
602 req->rq_majortimeo = to->to_maxval;
603 req->rq_majortimeo += jiffies;
604}
605
Chuck Lever9903cd12005-08-11 16:25:26 -0400606/**
607 * xprt_adjust_timeout - adjust timeout values for next retransmit
608 * @req: RPC request containing parameters to use for the adjustment
609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
611int xprt_adjust_timeout(struct rpc_rqst *req)
612{
613 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500614 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int status = 0;
616
617 if (time_before(jiffies, req->rq_majortimeo)) {
618 if (to->to_exponential)
619 req->rq_timeout <<= 1;
620 else
621 req->rq_timeout += to->to_increment;
622 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
623 req->rq_timeout = to->to_maxval;
624 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 } else {
626 req->rq_timeout = to->to_initval;
627 req->rq_retries = 0;
628 xprt_reset_majortimeo(req);
629 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400630 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400632 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 status = -ETIMEDOUT;
634 }
635
636 if (req->rq_timeout == 0) {
637 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
638 req->rq_timeout = 5 * HZ;
639 }
640 return status;
641}
642
David Howells65f27f32006-11-22 14:55:48 +0000643static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
David Howells65f27f32006-11-22 14:55:48 +0000645 struct rpc_xprt *xprt =
646 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Trond Myklebust66af1e52007-11-06 10:18:36 -0500648 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400649 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400651 wake_up_bit(&xprt->state, XPRT_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Chuck Lever9903cd12005-08-11 16:25:26 -0400654/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500655 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400656 * @xprt: transport to flag for disconnect
657 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500659void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Chuck Lever46121cf2007-01-31 12:14:08 -0500661 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400662 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400664 xprt_clear_write_space_locked(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400665 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400666 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500668EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Trond Myklebust66af1e52007-11-06 10:18:36 -0500670/**
671 * xprt_force_disconnect - force a transport to disconnect
672 * @xprt: transport to disconnect
673 *
674 */
675void xprt_force_disconnect(struct rpc_xprt *xprt)
676{
677 /* Don't race with the test_bit() in xprt_clear_locked() */
678 spin_lock_bh(&xprt->transport_lock);
679 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
680 /* Try to schedule an autoclose RPC call */
681 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400682 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400683 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500684 spin_unlock_bh(&xprt->transport_lock);
685}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400686EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500687
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400688static unsigned int
689xprt_connect_cookie(struct rpc_xprt *xprt)
690{
691 return READ_ONCE(xprt->connect_cookie);
692}
693
694static bool
695xprt_request_retransmit_after_disconnect(struct rpc_task *task)
696{
697 struct rpc_rqst *req = task->tk_rqstp;
698 struct rpc_xprt *xprt = req->rq_xprt;
699
700 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
701 !xprt_connected(xprt);
702}
703
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400704/**
705 * xprt_conditional_disconnect - force a transport to disconnect
706 * @xprt: transport to disconnect
707 * @cookie: 'connection cookie'
708 *
709 * This attempts to break the connection if and only if 'cookie' matches
710 * the current transport 'connection cookie'. It ensures that we don't
711 * try to break the connection more than once when we need to retransmit
712 * a batch of RPC requests.
713 *
714 */
715void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
716{
717 /* Don't race with the test_bit() in xprt_clear_locked() */
718 spin_lock_bh(&xprt->transport_lock);
719 if (cookie != xprt->connect_cookie)
720 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100721 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400722 goto out;
723 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
724 /* Try to schedule an autoclose RPC call */
725 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400726 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400727 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400728out:
729 spin_unlock_bh(&xprt->transport_lock);
730}
731
Trond Myklebustad3331a2016-08-02 13:47:43 -0400732static bool
733xprt_has_timer(const struct rpc_xprt *xprt)
734{
735 return xprt->idle_timeout != 0;
736}
737
738static void
739xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
740 __must_hold(&xprt->transport_lock)
741{
Trond Myklebust95f76912018-09-07 08:35:22 -0400742 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400743 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static void
Kees Cookff861c42017-10-16 17:29:42 -0700747xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Kees Cookff861c42017-10-16 17:29:42 -0700749 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400751 spin_lock(&xprt->transport_lock);
Trond Myklebust95f76912018-09-07 08:35:22 -0400752 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto out_abort;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400754 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
755 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400756 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400758 spin_unlock(&xprt->transport_lock);
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400759 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return;
761out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400762 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500765bool xprt_lock_connect(struct rpc_xprt *xprt,
766 struct rpc_task *task,
767 void *cookie)
768{
769 bool ret = false;
770
771 spin_lock_bh(&xprt->transport_lock);
772 if (!test_bit(XPRT_LOCKED, &xprt->state))
773 goto out;
774 if (xprt->snd_task != task)
775 goto out;
776 xprt->snd_task = cookie;
777 ret = true;
778out:
779 spin_unlock_bh(&xprt->transport_lock);
780 return ret;
781}
782
783void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
784{
785 spin_lock_bh(&xprt->transport_lock);
786 if (xprt->snd_task != cookie)
787 goto out;
788 if (!test_bit(XPRT_LOCKED, &xprt->state))
789 goto out;
790 xprt->snd_task =NULL;
791 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400792 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500793out:
794 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400795 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500796}
797
Chuck Lever9903cd12005-08-11 16:25:26 -0400798/**
799 * xprt_connect - schedule a transport connect operation
800 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
802 */
803void xprt_connect(struct rpc_task *task)
804{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500805 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chuck Lever46121cf2007-01-31 12:14:08 -0500807 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xprt, (xprt_connected(xprt) ? "is" : "is not"));
809
Chuck Leverec739ef2006-08-22 20:06:15 -0400810 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400811 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813 }
814 if (!xprt_lock_write(xprt, task))
815 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500816
817 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
818 xprt->ops->close(xprt);
819
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500820 if (!xprt_connected(xprt)) {
Trond Myklebusta8ce4a82010-04-16 16:42:12 -0400821 task->tk_timeout = task->tk_rqstp->rq_timeout;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100822 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust5d008372008-02-22 16:34:17 -0500823 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400824
825 if (test_bit(XPRT_CLOSING, &xprt->state))
826 return;
827 if (xprt_test_and_set_connecting(xprt))
828 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500829 /* Race breaker */
830 if (!xprt_connected(xprt)) {
831 xprt->stat.connect_start = jiffies;
832 xprt->ops->connect(xprt, task);
833 } else {
834 xprt_clear_connecting(xprt);
835 task->tk_status = 0;
836 rpc_wake_up_queued_task(&xprt->pending, task);
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500839 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Chuck Lever9903cd12005-08-11 16:25:26 -0400842static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Chuck Lever3968a8a2018-10-01 14:25:36 -0400844 switch (task->tk_status) {
845 case 0:
Chuck Lever46121cf2007-01-31 12:14:08 -0500846 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 task->tk_pid);
Chuck Lever3968a8a2018-10-01 14:25:36 -0400848 break;
Trond Myklebust0fe8d042013-12-31 13:13:30 -0500849 case -ECONNREFUSED:
850 case -ECONNRESET:
851 case -ECONNABORTED:
852 case -ENETUNREACH:
853 case -EHOSTUNREACH:
Trond Myklebust2fc193c2014-07-03 00:02:57 -0400854 case -EPIPE:
Trond Myklebust2a491992009-03-11 14:38:00 -0400855 case -EAGAIN:
856 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
Chuck Lever23475d62005-08-11 16:25:08 -0400857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500859 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
860 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500863 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
864 "server %s\n", task->tk_pid, -task->tk_status,
Chuck Lever3968a8a2018-10-01 14:25:36 -0400865 task->tk_rqstp->rq_xprt->servername);
Chuck Lever23475d62005-08-11 16:25:08 -0400866 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Trond Myklebust95f76912018-09-07 08:35:22 -0400870enum xprt_xid_rb_cmp {
871 XID_RB_EQUAL,
872 XID_RB_LEFT,
873 XID_RB_RIGHT,
874};
875static enum xprt_xid_rb_cmp
876xprt_xid_cmp(__be32 xid1, __be32 xid2)
877{
878 if (xid1 == xid2)
879 return XID_RB_EQUAL;
880 if ((__force u32)xid1 < (__force u32)xid2)
881 return XID_RB_LEFT;
882 return XID_RB_RIGHT;
883}
884
885static struct rpc_rqst *
886xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
887{
888 struct rb_node *n = xprt->recv_queue.rb_node;
889 struct rpc_rqst *req;
890
891 while (n != NULL) {
892 req = rb_entry(n, struct rpc_rqst, rq_recv);
893 switch (xprt_xid_cmp(xid, req->rq_xid)) {
894 case XID_RB_LEFT:
895 n = n->rb_left;
896 break;
897 case XID_RB_RIGHT:
898 n = n->rb_right;
899 break;
900 case XID_RB_EQUAL:
901 return req;
902 }
903 }
904 return NULL;
905}
906
907static void
908xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
909{
910 struct rb_node **p = &xprt->recv_queue.rb_node;
911 struct rb_node *n = NULL;
912 struct rpc_rqst *req;
913
914 while (*p != NULL) {
915 n = *p;
916 req = rb_entry(n, struct rpc_rqst, rq_recv);
917 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
918 case XID_RB_LEFT:
919 p = &n->rb_left;
920 break;
921 case XID_RB_RIGHT:
922 p = &n->rb_right;
923 break;
924 case XID_RB_EQUAL:
925 WARN_ON_ONCE(new != req);
926 return;
927 }
928 }
929 rb_link_node(&new->rq_recv, n, p);
930 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
931}
932
933static void
934xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
935{
936 rb_erase(&req->rq_recv, &xprt->recv_queue);
937}
938
Chuck Lever9903cd12005-08-11 16:25:26 -0400939/**
940 * xprt_lookup_rqst - find an RPC request corresponding to an XID
941 * @xprt: transport on which the original request was transmitted
942 * @xid: RPC XID of incoming reply
943 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400944 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700946struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400948 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Trond Myklebust95f76912018-09-07 08:35:22 -0400950 entry = xprt_request_rb_find(xprt, xid);
951 if (entry != NULL) {
952 trace_xprt_lookup_rqst(xprt, xid, 0);
953 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
954 return entry;
955 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500956
957 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
958 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400959 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500960 xprt->stat.bad_xids++;
961 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400963EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400965static bool
966xprt_is_pinned_rqst(struct rpc_rqst *req)
967{
968 return atomic_read(&req->rq_pin) != 0;
969}
970
Trond Myklebust729749b2017-08-13 10:03:59 -0400971/**
972 * xprt_pin_rqst - Pin a request on the transport receive list
973 * @req: Request to pin
974 *
975 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400976 * so should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400977 */
978void xprt_pin_rqst(struct rpc_rqst *req)
979{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400980 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400981}
Chuck Lever9590d082017-08-23 17:05:58 -0400982EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400983
984/**
985 * xprt_unpin_rqst - Unpin a request on the transport receive list
986 * @req: Request to pin
987 *
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400988 * Caller should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400989 */
990void xprt_unpin_rqst(struct rpc_rqst *req)
991{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400992 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
993 atomic_dec(&req->rq_pin);
994 return;
995 }
996 if (atomic_dec_and_test(&req->rq_pin))
997 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400998}
Chuck Lever9590d082017-08-23 17:05:58 -0400999EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -04001000
1001static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -04001002{
Trond Myklebustcf9946c2018-08-06 12:55:34 -04001003 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -04001004}
1005
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001006static bool
1007xprt_request_data_received(struct rpc_task *task)
1008{
1009 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1010 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1011}
1012
1013static bool
1014xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1015{
1016 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1017 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1018}
1019
1020/**
1021 * xprt_request_enqueue_receive - Add an request to the receive queue
1022 * @task: RPC task
1023 *
1024 */
1025void
1026xprt_request_enqueue_receive(struct rpc_task *task)
1027{
1028 struct rpc_rqst *req = task->tk_rqstp;
1029 struct rpc_xprt *xprt = req->rq_xprt;
1030
1031 if (!xprt_request_need_enqueue_receive(task, req))
1032 return;
1033 spin_lock(&xprt->queue_lock);
1034
1035 /* Update the softirq receive buffer */
1036 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1037 sizeof(req->rq_private_buf));
1038
1039 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001040 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001041 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1042 spin_unlock(&xprt->queue_lock);
1043
1044 xprt_reset_majortimeo(req);
1045 /* Turn off autodisconnect */
1046 del_singleshot_timer_sync(&xprt->timer);
1047}
1048
1049/**
1050 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1051 * @task: RPC task
1052 *
1053 * Caller must hold xprt->queue_lock.
1054 */
1055static void
1056xprt_request_dequeue_receive_locked(struct rpc_task *task)
1057{
Trond Myklebust95f76912018-09-07 08:35:22 -04001058 struct rpc_rqst *req = task->tk_rqstp;
1059
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001060 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001061 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001062}
1063
Chuck Leverecd465e2018-03-05 15:12:57 -05001064/**
1065 * xprt_update_rtt - Update RPC RTT statistics
1066 * @task: RPC request that recently completed
1067 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001068 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001069 */
1070void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001072 struct rpc_rqst *req = task->tk_rqstp;
1073 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001074 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001075 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Chuck Lever1570c1e2005-08-25 16:25:52 -07001077 if (timer) {
1078 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001079 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001080 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001082}
Chuck Leverecd465e2018-03-05 15:12:57 -05001083EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Chuck Lever1570c1e2005-08-25 16:25:52 -07001085/**
1086 * xprt_complete_rqst - called when reply processing is complete
1087 * @task: RPC request that recently completed
1088 * @copied: actual number of bytes received from the transport
1089 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001090 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001091 */
1092void xprt_complete_rqst(struct rpc_task *task, int copied)
1093{
1094 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001095 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Chuck Lever1570c1e2005-08-25 16:25:52 -07001097 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1098 task->tk_pid, ntohl(req->rq_xid), copied);
Jeff Layton3705ad62014-10-28 14:24:13 -04001099 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Trond Myklebustfda13932008-02-22 16:34:12 -05001101 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001102
Trond Myklebust1e799b62008-03-21 16:19:41 -04001103 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001104 /* Ensure all writes are done before we update */
1105 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001106 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001107 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001108 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001109 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001111EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Chuck Lever46c0ee82005-08-25 16:25:52 -07001113static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001115 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct rpc_xprt *xprt = req->rq_xprt;
1117
Trond Myklebust5d008372008-02-22 16:34:17 -05001118 if (task->tk_status != -ETIMEDOUT)
1119 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001120
Chuck Lever82476d92018-01-03 15:38:25 -05001121 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001122 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001123 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001124 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001125 } else
1126 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Chuck Lever9903cd12005-08-11 16:25:26 -04001129/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001130 * xprt_request_wait_receive - wait for the reply to an RPC request
1131 * @task: RPC task about to send a request
1132 *
1133 */
1134void xprt_request_wait_receive(struct rpc_task *task)
1135{
1136 struct rpc_rqst *req = task->tk_rqstp;
1137 struct rpc_xprt *xprt = req->rq_xprt;
1138
1139 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1140 return;
1141 /*
1142 * Sleep on the pending queue if we're expecting a reply.
1143 * The spinlock ensures atomicity between the test of
1144 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1145 */
1146 spin_lock(&xprt->queue_lock);
1147 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1148 xprt->ops->set_retrans_timeout(task);
1149 rpc_sleep_on(&xprt->pending, task, xprt_timer);
1150 /*
1151 * Send an extra queue wakeup call if the
1152 * connection was dropped in case the call to
1153 * rpc_sleep_on() raced.
1154 */
1155 if (xprt_request_retransmit_after_disconnect(task))
1156 rpc_wake_up_queued_task_set_status(&xprt->pending,
1157 task, -ENOTCONN);
1158 }
1159 spin_unlock(&xprt->queue_lock);
1160}
1161
Trond Myklebust944b0422018-08-09 23:33:21 -04001162static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001163xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1164{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001165 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001166}
1167
1168/**
1169 * xprt_request_enqueue_transmit - queue a task for transmission
1170 * @task: pointer to rpc_task
1171 *
1172 * Add a task to the transmission queue.
1173 */
1174void
1175xprt_request_enqueue_transmit(struct rpc_task *task)
1176{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001177 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001178 struct rpc_xprt *xprt = req->rq_xprt;
1179
1180 if (xprt_request_need_enqueue_transmit(task, req)) {
1181 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001182 /*
1183 * Requests that carry congestion control credits are added
1184 * to the head of the list to avoid starvation issues.
1185 */
1186 if (req->rq_cong) {
1187 xprt_clear_congestion_window_wait(xprt);
1188 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1189 if (pos->rq_cong)
1190 continue;
1191 /* Note: req is added _before_ pos */
1192 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1193 INIT_LIST_HEAD(&req->rq_xmit2);
1194 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);
1205 goto out;
1206 }
Trond Myklebust75891f52018-09-03 17:37:36 -04001207 } else {
1208 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1209 if (pos->rq_task->tk_owner != task->tk_owner)
1210 continue;
1211 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1212 INIT_LIST_HEAD(&req->rq_xmit);
1213 goto out;
1214 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001215 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001216 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001217 INIT_LIST_HEAD(&req->rq_xmit2);
1218out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001219 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1220 spin_unlock(&xprt->queue_lock);
1221 }
1222}
1223
1224/**
1225 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1226 * @task: pointer to rpc_task
1227 *
1228 * Remove a task from the transmission queue
1229 * Caller must hold xprt->queue_lock
1230 */
1231static void
1232xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1233{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001234 struct rpc_rqst *req = task->tk_rqstp;
1235
1236 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1237 return;
1238 if (!list_empty(&req->rq_xmit)) {
1239 list_del(&req->rq_xmit);
1240 if (!list_empty(&req->rq_xmit2)) {
1241 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1242 struct rpc_rqst, rq_xmit2);
1243 list_del(&req->rq_xmit2);
1244 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1245 }
1246 } else
1247 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001248}
1249
1250/**
1251 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1252 * @task: pointer to rpc_task
1253 *
1254 * Remove a task from the transmission queue
1255 */
1256static void
1257xprt_request_dequeue_transmit(struct rpc_task *task)
1258{
1259 struct rpc_rqst *req = task->tk_rqstp;
1260 struct rpc_xprt *xprt = req->rq_xprt;
1261
1262 spin_lock(&xprt->queue_lock);
1263 xprt_request_dequeue_transmit_locked(task);
1264 spin_unlock(&xprt->queue_lock);
1265}
1266
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001267/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001268 * xprt_request_prepare - prepare an encoded request for transport
1269 * @req: pointer to rpc_rqst
1270 *
1271 * Calls into the transport layer to do whatever is needed to prepare
1272 * the request for transmission or receive.
1273 */
1274void
1275xprt_request_prepare(struct rpc_rqst *req)
1276{
1277 struct rpc_xprt *xprt = req->rq_xprt;
1278
1279 if (xprt->ops->prepare_request)
1280 xprt->ops->prepare_request(req);
1281}
1282
1283/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001284 * xprt_request_need_retransmit - Test if a task needs retransmission
1285 * @task: pointer to rpc_task
1286 *
1287 * Test for whether a connection breakage requires the task to retransmit
1288 */
1289bool
1290xprt_request_need_retransmit(struct rpc_task *task)
1291{
1292 return xprt_request_retransmit_after_disconnect(task);
1293}
1294
1295/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001296 * xprt_prepare_transmit - reserve the transport before sending a request
1297 * @task: RPC task about to send a request
1298 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001300bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct rpc_rqst *req = task->tk_rqstp;
1303 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Chuck Lever46121cf2007-01-31 12:14:08 -05001305 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001307 if (!xprt_lock_write(xprt, task)) {
1308 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001309 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001310 rpc_wake_up_queued_task_set_status(&xprt->sending,
1311 task, 0);
1312 return false;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001315 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001318void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001319{
Rahul Iyer343952f2009-04-01 09:23:17 -04001320 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001321}
1322
Chuck Lever9903cd12005-08-11 16:25:26 -04001323/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001324 * xprt_request_transmit - send an RPC request on a transport
1325 * @req: pointer to request to transmit
1326 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001327 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001328 * This performs the transmission of a single request.
1329 * Note that if the request is not the same as snd_task, then it
1330 * does need to be pinned.
1331 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001332 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001333static int
1334xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001336 struct rpc_xprt *xprt = req->rq_xprt;
1337 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001338 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001339 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001340 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Chuck Lever46121cf2007-01-31 12:14:08 -05001342 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001344 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001345 if (xprt_request_data_received(task)) {
1346 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001347 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001348 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001349 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001350 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001351 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001352 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001353 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001356 /*
1357 * Update req->rq_ntrans before transmitting to avoid races with
1358 * xprt_update_rtt(), which needs to know that it is recording a
1359 * reply to the first transmission.
1360 */
1361 req->rq_ntrans++;
1362
Trond Myklebust90d91b02017-12-14 21:24:08 -05001363 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001364 status = xprt->ops->send_request(req);
Jeff Layton3705ad62014-10-28 14:24:13 -04001365 trace_xprt_transmit(xprt, req->rq_xid, status);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001366 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001367 req->rq_ntrans--;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001368 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001369 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001370
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001371 if (is_retrans)
1372 task->tk_client->cl_stats->rpcretrans++;
1373
Chuck Lever4a068252015-05-11 14:02:25 -04001374 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Trond Myklebustc8485e42009-03-11 14:37:59 -04001376 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -04001377 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001378 spin_lock_bh(&xprt->transport_lock);
1379
Trond Myklebustc8485e42009-03-11 14:37:59 -04001380 xprt->stat.sends++;
1381 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1382 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001383 xprt->stat.sending_u += xprt->sending.qlen;
1384 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001385 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001386
1387 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001388out_dequeue:
1389 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001390 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1391 return status;
1392}
1393
1394/**
1395 * xprt_transmit - send an RPC request on a transport
1396 * @task: controlling RPC task
1397 *
1398 * Attempts to drain the transmit queue. On exit, either the transport
1399 * signalled an error that needs to be handled before transmission can
1400 * resume, or @task finished transmitting, and detected that it already
1401 * received a reply.
1402 */
1403void
1404xprt_transmit(struct rpc_task *task)
1405{
1406 struct rpc_rqst *next, *req = task->tk_rqstp;
1407 struct rpc_xprt *xprt = req->rq_xprt;
1408 int status;
1409
1410 spin_lock(&xprt->queue_lock);
1411 while (!list_empty(&xprt->xmit_queue)) {
1412 next = list_first_entry(&xprt->xmit_queue,
1413 struct rpc_rqst, rq_xmit);
1414 xprt_pin_rqst(next);
1415 spin_unlock(&xprt->queue_lock);
1416 status = xprt_request_transmit(next, task);
1417 if (status == -EBADMSG && next != req)
1418 status = 0;
1419 cond_resched();
1420 spin_lock(&xprt->queue_lock);
1421 xprt_unpin_rqst(next);
1422 if (status == 0) {
1423 if (!xprt_request_data_received(task) ||
1424 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1425 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001426 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001427 task->tk_status = status;
1428 break;
1429 }
1430 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Trond Myklebustba60eb22013-04-14 10:49:37 -04001433static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1434{
1435 set_bit(XPRT_CONGESTED, &xprt->state);
1436 rpc_sleep_on(&xprt->backlog, task, NULL);
1437}
1438
1439static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1440{
1441 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1442 clear_bit(XPRT_CONGESTED, &xprt->state);
1443}
1444
1445static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1446{
1447 bool ret = false;
1448
1449 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1450 goto out;
1451 spin_lock(&xprt->reserve_lock);
1452 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1453 rpc_sleep_on(&xprt->backlog, task, NULL);
1454 ret = true;
1455 }
1456 spin_unlock(&xprt->reserve_lock);
1457out:
1458 return ret;
1459}
1460
Trond Myklebust92ea0112017-06-20 19:35:39 -04001461static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001462{
1463 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1464
Chuck Leverff699ea82018-03-05 15:13:13 -05001465 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001466 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001467 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001468 spin_unlock(&xprt->reserve_lock);
1469 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1470 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001471 if (req != NULL)
1472 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001473 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001474 req = ERR_PTR(-ENOMEM);
1475out:
1476 return req;
1477}
1478
1479static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1480{
Chuck Leverff699ea82018-03-05 15:13:13 -05001481 if (xprt->num_reqs > xprt->min_reqs) {
1482 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001483 kfree(req);
1484 return true;
1485 }
1486 return false;
1487}
1488
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001489void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001491 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001493 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001495 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1496 list_del(&req->rq_list);
1497 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001499 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001500 if (!IS_ERR(req))
1501 goto out_init_req;
1502 switch (PTR_ERR(req)) {
1503 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001504 dprintk("RPC: dynamic allocation of request slot "
1505 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001506 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001507 break;
1508 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001509 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001510 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001511 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001512 default:
1513 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001514 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001515 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001516 return;
1517out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001518 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1519 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001520 spin_unlock(&xprt->reserve_lock);
1521
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001522 task->tk_status = 0;
1523 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001525EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1526
Chuck Levera9cde232018-05-04 15:34:59 -04001527void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001528{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001529 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001530 if (!xprt_dynamic_free_slot(xprt, req)) {
1531 memset(req, 0, sizeof(*req)); /* mark unused */
1532 list_add(&req->rq_list, &xprt->free);
1533 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001534 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001535 spin_unlock(&xprt->reserve_lock);
1536}
Chuck Levera9cde232018-05-04 15:34:59 -04001537EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001538
Trond Myklebust21de0a92011-07-17 16:57:32 -04001539static void xprt_free_all_slots(struct rpc_xprt *xprt)
1540{
1541 struct rpc_rqst *req;
1542 while (!list_empty(&xprt->free)) {
1543 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1544 list_del(&req->rq_list);
1545 kfree(req);
1546 }
1547}
1548
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001549struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1550 unsigned int num_prealloc,
1551 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001552{
1553 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001554 struct rpc_rqst *req;
1555 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001556
1557 xprt = kzalloc(size, GFP_KERNEL);
1558 if (xprt == NULL)
1559 goto out;
1560
Trond Myklebust21de0a92011-07-17 16:57:32 -04001561 xprt_init(xprt, net);
1562
1563 for (i = 0; i < num_prealloc; i++) {
1564 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1565 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001566 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001567 list_add(&req->rq_list, &xprt->free);
1568 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001569 if (max_alloc > num_prealloc)
1570 xprt->max_reqs = max_alloc;
1571 else
1572 xprt->max_reqs = num_prealloc;
1573 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001574 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001575
1576 return xprt;
1577
1578out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001579 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001580out:
1581 return NULL;
1582}
1583EXPORT_SYMBOL_GPL(xprt_alloc);
1584
Pavel Emelyanove204e622010-09-29 16:03:13 +04001585void xprt_free(struct rpc_xprt *xprt)
1586{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001587 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001588 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001589 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001590}
1591EXPORT_SYMBOL_GPL(xprt_free);
1592
Trond Myklebust902c5882018-09-01 17:21:01 -04001593static void
1594xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1595{
1596 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1597}
1598
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001599static __be32
1600xprt_alloc_xid(struct rpc_xprt *xprt)
1601{
1602 __be32 xid;
1603
1604 spin_lock(&xprt->reserve_lock);
1605 xid = (__force __be32)xprt->xid++;
1606 spin_unlock(&xprt->reserve_lock);
1607 return xid;
1608}
1609
1610static void
1611xprt_init_xid(struct rpc_xprt *xprt)
1612{
1613 xprt->xid = prandom_u32();
1614}
1615
1616static void
1617xprt_request_init(struct rpc_task *task)
1618{
1619 struct rpc_xprt *xprt = task->tk_xprt;
1620 struct rpc_rqst *req = task->tk_rqstp;
1621
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001622 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1623 req->rq_task = task;
1624 req->rq_xprt = xprt;
1625 req->rq_buffer = NULL;
1626 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001627 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001628 req->rq_bytes_sent = 0;
1629 req->rq_snd_buf.len = 0;
1630 req->rq_snd_buf.buflen = 0;
1631 req->rq_rcv_buf.len = 0;
1632 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001633 req->rq_snd_buf.bvec = NULL;
1634 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001635 req->rq_release_snd_buf = NULL;
1636 xprt_reset_majortimeo(req);
1637 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1638 req, ntohl(req->rq_xid));
1639}
1640
1641static void
1642xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1643{
1644 xprt->ops->alloc_slot(xprt, task);
1645 if (task->tk_rqstp != NULL)
1646 xprt_request_init(task);
1647}
1648
Chuck Lever9903cd12005-08-11 16:25:26 -04001649/**
1650 * xprt_reserve - allocate an RPC request slot
1651 * @task: RPC task requesting a slot allocation
1652 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001653 * If the transport is marked as being congested, or if no more
1654 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001655 * backlog queue.
1656 */
1657void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001659 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001661 task->tk_status = 0;
1662 if (task->tk_rqstp != NULL)
1663 return;
1664
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001665 task->tk_timeout = 0;
1666 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001667 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001668 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001669}
1670
1671/**
1672 * xprt_retry_reserve - allocate an RPC request slot
1673 * @task: RPC task requesting a slot allocation
1674 *
1675 * If no more slots are available, place the task on the transport's
1676 * backlog queue.
1677 * Note that the only difference with xprt_reserve is that we now
1678 * ignore the value of the XPRT_CONGESTED flag.
1679 */
1680void xprt_retry_reserve(struct rpc_task *task)
1681{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001682 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001683
1684 task->tk_status = 0;
1685 if (task->tk_rqstp != NULL)
1686 return;
1687
1688 task->tk_timeout = 0;
1689 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001690 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001693static void
1694xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1695{
1696 struct rpc_xprt *xprt = req->rq_xprt;
1697
Trond Myklebust944b0422018-08-09 23:33:21 -04001698 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1699 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001700 xprt_is_pinned_rqst(req)) {
1701 spin_lock(&xprt->queue_lock);
Trond Myklebust944b0422018-08-09 23:33:21 -04001702 xprt_request_dequeue_transmit_locked(task);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001703 xprt_request_dequeue_receive_locked(task);
1704 while (xprt_is_pinned_rqst(req)) {
1705 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1706 spin_unlock(&xprt->queue_lock);
1707 xprt_wait_on_pinned_rqst(req);
1708 spin_lock(&xprt->queue_lock);
1709 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1710 }
1711 spin_unlock(&xprt->queue_lock);
1712 }
1713}
1714
Chuck Lever9903cd12005-08-11 16:25:26 -04001715/**
1716 * xprt_release - release an RPC request slot
1717 * @task: task which is finished with the slot
1718 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001720void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001722 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001723 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Trond Myklebust87ed5002013-01-07 14:30:46 -05001725 if (req == NULL) {
1726 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001727 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001728 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001731 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001732
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001733 xprt = req->rq_xprt;
Weston Andros Adamson0a702192012-02-17 13:15:24 -05001734 if (task->tk_ops->rpc_count_stats != NULL)
1735 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1736 else if (task->tk_client)
1737 rpc_count_iostats(task, task->tk_client->cl_metrics);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001738 xprt_request_dequeue_all(task, req);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001739 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001740 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001741 if (xprt->ops->release_request)
1742 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 xprt->last_used = jiffies;
Trond Myklebustad3331a2016-08-02 13:47:43 -04001744 xprt_schedule_autodisconnect(xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001745 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001746 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001747 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001748 xprt_inject_disconnect(xprt);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001749 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001750 if (req->rq_cred != NULL)
1751 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001753 if (req->rq_release_snd_buf)
1754 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001755
Chuck Lever46121cf2007-01-31 12:14:08 -05001756 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001757 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001758 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001759 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001760 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Trond Myklebust902c5882018-09-01 17:21:01 -04001763#ifdef CONFIG_SUNRPC_BACKCHANNEL
1764void
1765xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1766{
1767 struct xdr_buf *xbufp = &req->rq_snd_buf;
1768
1769 task->tk_rqstp = req;
1770 req->rq_task = task;
1771 xprt_init_connect_cookie(req, req->rq_xprt);
1772 /*
1773 * Set up the xdr_buf length.
1774 * This also indicates that the buffer is XDR encoded already.
1775 */
1776 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1777 xbufp->tail[0].iov_len;
1778 req->rq_bytes_sent = 0;
1779}
1780#endif
1781
Trond Myklebust21de0a92011-07-17 16:57:32 -04001782static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001783{
Trond Myklebust30c51162015-02-24 20:31:39 -05001784 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001785
1786 spin_lock_init(&xprt->transport_lock);
1787 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001788 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001789
1790 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001791 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001792 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001793#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001794 spin_lock_init(&xprt->bc_pa_lock);
1795 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001796#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001797 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001798
Chuck Leverc2866762006-08-22 20:06:20 -04001799 xprt->last_used = jiffies;
1800 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001801 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001802
1803 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1804 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001805 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001806 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1807
Chuck Leverc2866762006-08-22 20:06:20 -04001808 xprt_init_xid(xprt);
1809
Trond Myklebust21de0a92011-07-17 16:57:32 -04001810 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001811}
1812
1813/**
1814 * xprt_create_transport - create an RPC transport
1815 * @args: rpc transport creation arguments
1816 *
1817 */
1818struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1819{
1820 struct rpc_xprt *xprt;
1821 struct xprt_class *t;
1822
1823 spin_lock(&xprt_list_lock);
1824 list_for_each_entry(t, &xprt_list, list) {
1825 if (t->ident == args->ident) {
1826 spin_unlock(&xprt_list_lock);
1827 goto found;
1828 }
1829 }
1830 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001831 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001832 return ERR_PTR(-EIO);
1833
1834found:
1835 xprt = t->setup(args);
1836 if (IS_ERR(xprt)) {
1837 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1838 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001839 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001840 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001841 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1842 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001843 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1844 if (xprt_has_timer(xprt))
Kees Cookff861c42017-10-16 17:29:42 -07001845 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001846 else
Kees Cookff861c42017-10-16 17:29:42 -07001847 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001848
1849 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1850 xprt_destroy(xprt);
1851 return ERR_PTR(-EINVAL);
1852 }
1853 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1854 if (xprt->servername == NULL) {
1855 xprt_destroy(xprt);
1856 return ERR_PTR(-ENOMEM);
1857 }
1858
Jeff Layton3f940092015-03-31 12:03:28 -04001859 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001860
Chuck Lever46121cf2007-01-31 12:14:08 -05001861 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001862 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001863out:
Chuck Leverc2866762006-08-22 20:06:20 -04001864 return xprt;
1865}
1866
Trond Myklebust528fd352017-10-19 12:13:10 -04001867static void xprt_destroy_cb(struct work_struct *work)
1868{
1869 struct rpc_xprt *xprt =
1870 container_of(work, struct rpc_xprt, task_cleanup);
1871
1872 rpc_xprt_debugfs_unregister(xprt);
1873 rpc_destroy_wait_queue(&xprt->binding);
1874 rpc_destroy_wait_queue(&xprt->pending);
1875 rpc_destroy_wait_queue(&xprt->sending);
1876 rpc_destroy_wait_queue(&xprt->backlog);
1877 kfree(xprt->servername);
1878 /*
1879 * Tear down transport state and free the rpc_xprt
1880 */
1881 xprt->ops->destroy(xprt);
1882}
1883
Chuck Lever9903cd12005-08-11 16:25:26 -04001884/**
1885 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001886 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001887 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001889static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Chuck Lever46121cf2007-01-31 12:14:08 -05001891 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001892
Trond Myklebust528fd352017-10-19 12:13:10 -04001893 /*
1894 * Exclude transport connect/disconnect handlers and autoclose
1895 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001896 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1897
Trond Myklebust0065db32006-01-03 09:55:56 +01001898 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001899
1900 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001901 * Destroy sockets etc from the system workqueue so they can
1902 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001903 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001904 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1905 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Trond Myklebust30c51162015-02-24 20:31:39 -05001908static void xprt_destroy_kref(struct kref *kref)
1909{
1910 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1911}
1912
1913/**
1914 * xprt_get - return a reference to an RPC transport.
1915 * @xprt: pointer to the transport
1916 *
1917 */
1918struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1919{
1920 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1921 return xprt;
1922 return NULL;
1923}
1924EXPORT_SYMBOL_GPL(xprt_get);
1925
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001926/**
1927 * xprt_put - release a reference to an RPC transport.
1928 * @xprt: pointer to the transport
1929 *
1930 */
1931void xprt_put(struct rpc_xprt *xprt)
1932{
Trond Myklebust30c51162015-02-24 20:31:39 -05001933 if (xprt != NULL)
1934 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001935}
Chuck Lever5d252f92016-01-07 14:50:10 -05001936EXPORT_SYMBOL_GPL(xprt_put);