blob: 2af6be9d6574624688fbe925dca88d4695d06076 [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
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);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500647 unsigned int pflags = memalloc_nofs_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Trond Myklebust66af1e552007-11-06 10:18:36 -0500649 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400650 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400652 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500653 memalloc_nofs_restore(pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Chuck Lever9903cd12005-08-11 16:25:26 -0400656/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500657 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400658 * @xprt: transport to flag for disconnect
659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500661void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Chuck Lever46121cf2007-01-31 12:14:08 -0500663 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400664 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xprt_clear_connected(xprt);
Trond Myklebustc5445772018-09-03 23:39:27 -0400666 xprt_clear_write_space_locked(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400667 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400668 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500670EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Trond Myklebust66af1e552007-11-06 10:18:36 -0500672/**
673 * xprt_force_disconnect - force a transport to disconnect
674 * @xprt: transport to disconnect
675 *
676 */
677void xprt_force_disconnect(struct rpc_xprt *xprt)
678{
679 /* Don't race with the test_bit() in xprt_clear_locked() */
680 spin_lock_bh(&xprt->transport_lock);
681 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
682 /* Try to schedule an autoclose RPC call */
683 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400684 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust0445f922018-12-17 13:34:59 -0500685 else if (xprt->snd_task)
686 rpc_wake_up_queued_task_set_status(&xprt->pending,
687 xprt->snd_task, -ENOTCONN);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500688 spin_unlock_bh(&xprt->transport_lock);
689}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400690EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500691
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400692static unsigned int
693xprt_connect_cookie(struct rpc_xprt *xprt)
694{
695 return READ_ONCE(xprt->connect_cookie);
696}
697
698static bool
699xprt_request_retransmit_after_disconnect(struct rpc_task *task)
700{
701 struct rpc_rqst *req = task->tk_rqstp;
702 struct rpc_xprt *xprt = req->rq_xprt;
703
704 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
705 !xprt_connected(xprt);
706}
707
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400708/**
709 * xprt_conditional_disconnect - force a transport to disconnect
710 * @xprt: transport to disconnect
711 * @cookie: 'connection cookie'
712 *
713 * This attempts to break the connection if and only if 'cookie' matches
714 * the current transport 'connection cookie'. It ensures that we don't
715 * try to break the connection more than once when we need to retransmit
716 * a batch of RPC requests.
717 *
718 */
719void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
720{
721 /* Don't race with the test_bit() in xprt_clear_locked() */
722 spin_lock_bh(&xprt->transport_lock);
723 if (cookie != xprt->connect_cookie)
724 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100725 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400726 goto out;
727 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
728 /* Try to schedule an autoclose RPC call */
729 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400730 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400731 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400732out:
733 spin_unlock_bh(&xprt->transport_lock);
734}
735
Trond Myklebustad3331a2016-08-02 13:47:43 -0400736static bool
737xprt_has_timer(const struct rpc_xprt *xprt)
738{
739 return xprt->idle_timeout != 0;
740}
741
742static void
743xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
744 __must_hold(&xprt->transport_lock)
745{
Trond Myklebust95f76912018-09-07 08:35:22 -0400746 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400747 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static void
Kees Cookff861c42017-10-16 17:29:42 -0700751xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Kees Cookff861c42017-10-16 17:29:42 -0700753 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400755 spin_lock(&xprt->transport_lock);
Trond Myklebust95f76912018-09-07 08:35:22 -0400756 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out_abort;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400758 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
759 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400760 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400762 spin_unlock(&xprt->transport_lock);
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400763 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return;
765out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400766 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500769bool xprt_lock_connect(struct rpc_xprt *xprt,
770 struct rpc_task *task,
771 void *cookie)
772{
773 bool ret = false;
774
775 spin_lock_bh(&xprt->transport_lock);
776 if (!test_bit(XPRT_LOCKED, &xprt->state))
777 goto out;
778 if (xprt->snd_task != task)
779 goto out;
780 xprt->snd_task = cookie;
781 ret = true;
782out:
783 spin_unlock_bh(&xprt->transport_lock);
784 return ret;
785}
786
787void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
788{
789 spin_lock_bh(&xprt->transport_lock);
790 if (xprt->snd_task != cookie)
791 goto out;
792 if (!test_bit(XPRT_LOCKED, &xprt->state))
793 goto out;
794 xprt->snd_task =NULL;
795 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400796 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500797out:
798 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400799 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500800}
801
Chuck Lever9903cd12005-08-11 16:25:26 -0400802/**
803 * xprt_connect - schedule a transport connect operation
804 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
806 */
807void xprt_connect(struct rpc_task *task)
808{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500809 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Chuck Lever46121cf2007-01-31 12:14:08 -0500811 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xprt, (xprt_connected(xprt) ? "is" : "is not"));
813
Chuck Leverec739ef2006-08-22 20:06:15 -0400814 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400815 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return;
817 }
818 if (!xprt_lock_write(xprt, task))
819 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500820
821 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
822 xprt->ops->close(xprt);
823
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500824 if (!xprt_connected(xprt)) {
Trond Myklebusta8ce4a82010-04-16 16:42:12 -0400825 task->tk_timeout = task->tk_rqstp->rq_timeout;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100826 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebustabc13272018-12-17 17:33:33 -0500827 rpc_sleep_on(&xprt->pending, task, NULL);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400828
829 if (test_bit(XPRT_CLOSING, &xprt->state))
830 return;
831 if (xprt_test_and_set_connecting(xprt))
832 return;
Trond Myklebust0a9a4302018-12-01 23:18:00 -0500833 /* Race breaker */
834 if (!xprt_connected(xprt)) {
835 xprt->stat.connect_start = jiffies;
836 xprt->ops->connect(xprt, task);
837 } else {
838 xprt_clear_connecting(xprt);
839 task->tk_status = 0;
840 rpc_wake_up_queued_task(&xprt->pending, task);
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500843 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Trond Myklebust95f76912018-09-07 08:35:22 -0400846enum xprt_xid_rb_cmp {
847 XID_RB_EQUAL,
848 XID_RB_LEFT,
849 XID_RB_RIGHT,
850};
851static enum xprt_xid_rb_cmp
852xprt_xid_cmp(__be32 xid1, __be32 xid2)
853{
854 if (xid1 == xid2)
855 return XID_RB_EQUAL;
856 if ((__force u32)xid1 < (__force u32)xid2)
857 return XID_RB_LEFT;
858 return XID_RB_RIGHT;
859}
860
861static struct rpc_rqst *
862xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
863{
864 struct rb_node *n = xprt->recv_queue.rb_node;
865 struct rpc_rqst *req;
866
867 while (n != NULL) {
868 req = rb_entry(n, struct rpc_rqst, rq_recv);
869 switch (xprt_xid_cmp(xid, req->rq_xid)) {
870 case XID_RB_LEFT:
871 n = n->rb_left;
872 break;
873 case XID_RB_RIGHT:
874 n = n->rb_right;
875 break;
876 case XID_RB_EQUAL:
877 return req;
878 }
879 }
880 return NULL;
881}
882
883static void
884xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
885{
886 struct rb_node **p = &xprt->recv_queue.rb_node;
887 struct rb_node *n = NULL;
888 struct rpc_rqst *req;
889
890 while (*p != NULL) {
891 n = *p;
892 req = rb_entry(n, struct rpc_rqst, rq_recv);
893 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
894 case XID_RB_LEFT:
895 p = &n->rb_left;
896 break;
897 case XID_RB_RIGHT:
898 p = &n->rb_right;
899 break;
900 case XID_RB_EQUAL:
901 WARN_ON_ONCE(new != req);
902 return;
903 }
904 }
905 rb_link_node(&new->rq_recv, n, p);
906 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
907}
908
909static void
910xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
911{
912 rb_erase(&req->rq_recv, &xprt->recv_queue);
913}
914
Chuck Lever9903cd12005-08-11 16:25:26 -0400915/**
916 * xprt_lookup_rqst - find an RPC request corresponding to an XID
917 * @xprt: transport on which the original request was transmitted
918 * @xid: RPC XID of incoming reply
919 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400920 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700922struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400924 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Trond Myklebust95f76912018-09-07 08:35:22 -0400926 entry = xprt_request_rb_find(xprt, xid);
927 if (entry != NULL) {
928 trace_xprt_lookup_rqst(xprt, xid, 0);
929 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
930 return entry;
931 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500932
933 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
934 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400935 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500936 xprt->stat.bad_xids++;
937 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400939EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400941static bool
942xprt_is_pinned_rqst(struct rpc_rqst *req)
943{
944 return atomic_read(&req->rq_pin) != 0;
945}
946
Trond Myklebust729749b2017-08-13 10:03:59 -0400947/**
948 * xprt_pin_rqst - Pin a request on the transport receive list
949 * @req: Request to pin
950 *
951 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400952 * so should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400953 */
954void xprt_pin_rqst(struct rpc_rqst *req)
955{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400956 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400957}
Chuck Lever9590d082017-08-23 17:05:58 -0400958EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400959
960/**
961 * xprt_unpin_rqst - Unpin a request on the transport receive list
962 * @req: Request to pin
963 *
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400964 * Caller should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400965 */
966void xprt_unpin_rqst(struct rpc_rqst *req)
967{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400968 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
969 atomic_dec(&req->rq_pin);
970 return;
971 }
972 if (atomic_dec_and_test(&req->rq_pin))
973 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400974}
Chuck Lever9590d082017-08-23 17:05:58 -0400975EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400976
977static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -0400978{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400979 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -0400980}
981
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400982static bool
983xprt_request_data_received(struct rpc_task *task)
984{
985 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
986 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
987}
988
989static bool
990xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
991{
992 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
993 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
994}
995
996/**
997 * xprt_request_enqueue_receive - Add an request to the receive queue
998 * @task: RPC task
999 *
1000 */
1001void
1002xprt_request_enqueue_receive(struct rpc_task *task)
1003{
1004 struct rpc_rqst *req = task->tk_rqstp;
1005 struct rpc_xprt *xprt = req->rq_xprt;
1006
1007 if (!xprt_request_need_enqueue_receive(task, req))
1008 return;
1009 spin_lock(&xprt->queue_lock);
1010
1011 /* Update the softirq receive buffer */
1012 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1013 sizeof(req->rq_private_buf));
1014
1015 /* Add request to the receive list */
Trond Myklebust95f76912018-09-07 08:35:22 -04001016 xprt_request_rb_insert(xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001017 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1018 spin_unlock(&xprt->queue_lock);
1019
1020 xprt_reset_majortimeo(req);
1021 /* Turn off autodisconnect */
1022 del_singleshot_timer_sync(&xprt->timer);
1023}
1024
1025/**
1026 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1027 * @task: RPC task
1028 *
1029 * Caller must hold xprt->queue_lock.
1030 */
1031static void
1032xprt_request_dequeue_receive_locked(struct rpc_task *task)
1033{
Trond Myklebust95f76912018-09-07 08:35:22 -04001034 struct rpc_rqst *req = task->tk_rqstp;
1035
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001036 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebust95f76912018-09-07 08:35:22 -04001037 xprt_request_rb_remove(req->rq_xprt, req);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001038}
1039
Chuck Leverecd465e2018-03-05 15:12:57 -05001040/**
1041 * xprt_update_rtt - Update RPC RTT statistics
1042 * @task: RPC request that recently completed
1043 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001044 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -05001045 */
1046void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Chuck Lever1570c1e2005-08-25 16:25:52 -07001048 struct rpc_rqst *req = task->tk_rqstp;
1049 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +00001050 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -04001051 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Chuck Lever1570c1e2005-08-25 16:25:52 -07001053 if (timer) {
1054 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -04001055 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001056 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001058}
Chuck Leverecd465e2018-03-05 15:12:57 -05001059EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Chuck Lever1570c1e2005-08-25 16:25:52 -07001061/**
1062 * xprt_complete_rqst - called when reply processing is complete
1063 * @task: RPC request that recently completed
1064 * @copied: actual number of bytes received from the transport
1065 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001066 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001067 */
1068void xprt_complete_rqst(struct rpc_task *task, int copied)
1069{
1070 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001071 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Chuck Lever1570c1e2005-08-25 16:25:52 -07001073 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1074 task->tk_pid, ntohl(req->rq_xid), copied);
Jeff Layton3705ad62014-10-28 14:24:13 -04001075 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Trond Myklebustfda13932008-02-22 16:34:12 -05001077 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001078
Trond Myklebust1e799b62008-03-21 16:19:41 -04001079 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001080 /* Ensure all writes are done before we update */
1081 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001082 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001083 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001084 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001085 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001087EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Chuck Lever46c0ee82005-08-25 16:25:52 -07001089static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001091 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct rpc_xprt *xprt = req->rq_xprt;
1093
Trond Myklebust5d008372008-02-22 16:34:17 -05001094 if (task->tk_status != -ETIMEDOUT)
1095 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001096
Chuck Lever82476d92018-01-03 15:38:25 -05001097 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001098 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001099 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001100 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001101 } else
1102 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Chuck Lever9903cd12005-08-11 16:25:26 -04001105/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001106 * xprt_request_wait_receive - wait for the reply to an RPC request
1107 * @task: RPC task about to send a request
1108 *
1109 */
1110void xprt_request_wait_receive(struct rpc_task *task)
1111{
1112 struct rpc_rqst *req = task->tk_rqstp;
1113 struct rpc_xprt *xprt = req->rq_xprt;
1114
1115 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1116 return;
1117 /*
1118 * Sleep on the pending queue if we're expecting a reply.
1119 * The spinlock ensures atomicity between the test of
1120 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1121 */
1122 spin_lock(&xprt->queue_lock);
1123 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1124 xprt->ops->set_retrans_timeout(task);
1125 rpc_sleep_on(&xprt->pending, task, xprt_timer);
1126 /*
1127 * Send an extra queue wakeup call if the
1128 * connection was dropped in case the call to
1129 * rpc_sleep_on() raced.
1130 */
1131 if (xprt_request_retransmit_after_disconnect(task))
1132 rpc_wake_up_queued_task_set_status(&xprt->pending,
1133 task, -ENOTCONN);
1134 }
1135 spin_unlock(&xprt->queue_lock);
1136}
1137
Trond Myklebust944b0422018-08-09 23:33:21 -04001138static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001139xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1140{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001141 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001142}
1143
1144/**
1145 * xprt_request_enqueue_transmit - queue a task for transmission
1146 * @task: pointer to rpc_task
1147 *
1148 * Add a task to the transmission queue.
1149 */
1150void
1151xprt_request_enqueue_transmit(struct rpc_task *task)
1152{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001153 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001154 struct rpc_xprt *xprt = req->rq_xprt;
1155
1156 if (xprt_request_need_enqueue_transmit(task, req)) {
Trond Myklebuste66721f2019-01-02 17:53:10 -05001157 req->rq_bytes_sent = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001158 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001159 /*
1160 * Requests that carry congestion control credits are added
1161 * to the head of the list to avoid starvation issues.
1162 */
1163 if (req->rq_cong) {
1164 xprt_clear_congestion_window_wait(xprt);
1165 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1166 if (pos->rq_cong)
1167 continue;
1168 /* Note: req is added _before_ pos */
1169 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1170 INIT_LIST_HEAD(&req->rq_xmit2);
1171 goto out;
1172 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001173 } else if (RPC_IS_SWAPPER(task)) {
1174 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1175 if (pos->rq_cong || pos->rq_bytes_sent)
1176 continue;
1177 if (RPC_IS_SWAPPER(pos->rq_task))
1178 continue;
1179 /* Note: req is added _before_ pos */
1180 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1181 INIT_LIST_HEAD(&req->rq_xmit2);
1182 goto out;
1183 }
Chuck Leverdeaa5c92019-01-09 10:04:57 -05001184 } else if (!req->rq_seqno) {
Trond Myklebust75891f52018-09-03 17:37:36 -04001185 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1186 if (pos->rq_task->tk_owner != task->tk_owner)
1187 continue;
1188 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1189 INIT_LIST_HEAD(&req->rq_xmit);
1190 goto out;
1191 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001192 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001193 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001194 INIT_LIST_HEAD(&req->rq_xmit2);
1195out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001196 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1197 spin_unlock(&xprt->queue_lock);
1198 }
1199}
1200
1201/**
1202 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1203 * @task: pointer to rpc_task
1204 *
1205 * Remove a task from the transmission queue
1206 * Caller must hold xprt->queue_lock
1207 */
1208static void
1209xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1210{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001211 struct rpc_rqst *req = task->tk_rqstp;
1212
1213 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1214 return;
1215 if (!list_empty(&req->rq_xmit)) {
1216 list_del(&req->rq_xmit);
1217 if (!list_empty(&req->rq_xmit2)) {
1218 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1219 struct rpc_rqst, rq_xmit2);
1220 list_del(&req->rq_xmit2);
1221 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1222 }
1223 } else
1224 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001225}
1226
1227/**
1228 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1229 * @task: pointer to rpc_task
1230 *
1231 * Remove a task from the transmission queue
1232 */
1233static void
1234xprt_request_dequeue_transmit(struct rpc_task *task)
1235{
1236 struct rpc_rqst *req = task->tk_rqstp;
1237 struct rpc_xprt *xprt = req->rq_xprt;
1238
1239 spin_lock(&xprt->queue_lock);
1240 xprt_request_dequeue_transmit_locked(task);
1241 spin_unlock(&xprt->queue_lock);
1242}
1243
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001244/**
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001245 * xprt_request_prepare - prepare an encoded request for transport
1246 * @req: pointer to rpc_rqst
1247 *
1248 * Calls into the transport layer to do whatever is needed to prepare
1249 * the request for transmission or receive.
1250 */
1251void
1252xprt_request_prepare(struct rpc_rqst *req)
1253{
1254 struct rpc_xprt *xprt = req->rq_xprt;
1255
1256 if (xprt->ops->prepare_request)
1257 xprt->ops->prepare_request(req);
1258}
1259
1260/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001261 * xprt_request_need_retransmit - Test if a task needs retransmission
1262 * @task: pointer to rpc_task
1263 *
1264 * Test for whether a connection breakage requires the task to retransmit
1265 */
1266bool
1267xprt_request_need_retransmit(struct rpc_task *task)
1268{
1269 return xprt_request_retransmit_after_disconnect(task);
1270}
1271
1272/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001273 * xprt_prepare_transmit - reserve the transport before sending a request
1274 * @task: RPC task about to send a request
1275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001277bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct rpc_rqst *req = task->tk_rqstp;
1280 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Chuck Lever46121cf2007-01-31 12:14:08 -05001282 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001284 if (!xprt_lock_write(xprt, task)) {
1285 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001286 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001287 rpc_wake_up_queued_task_set_status(&xprt->sending,
1288 task, 0);
1289 return false;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001292 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001295void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001296{
Rahul Iyer343952f2009-04-01 09:23:17 -04001297 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001298}
1299
Chuck Lever9903cd12005-08-11 16:25:26 -04001300/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001301 * xprt_request_transmit - send an RPC request on a transport
1302 * @req: pointer to request to transmit
1303 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001304 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001305 * This performs the transmission of a single request.
1306 * Note that if the request is not the same as snd_task, then it
1307 * does need to be pinned.
1308 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001309 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001310static int
1311xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001313 struct rpc_xprt *xprt = req->rq_xprt;
1314 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001315 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001316 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001317 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Chuck Lever46121cf2007-01-31 12:14:08 -05001319 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001321 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001322 if (xprt_request_data_received(task)) {
1323 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001324 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001325 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001326 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001327 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001328 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001329 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001330 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001333 /*
1334 * Update req->rq_ntrans before transmitting to avoid races with
1335 * xprt_update_rtt(), which needs to know that it is recording a
1336 * reply to the first transmission.
1337 */
1338 req->rq_ntrans++;
1339
Trond Myklebust90d91b02017-12-14 21:24:08 -05001340 connect_cookie = xprt->connect_cookie;
Trond Myklebustadfa7142018-09-03 23:58:59 -04001341 status = xprt->ops->send_request(req);
Jeff Layton3705ad62014-10-28 14:24:13 -04001342 trace_xprt_transmit(xprt, req->rq_xid, status);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001343 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001344 req->rq_ntrans--;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001345 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001346 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001347
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001348 if (is_retrans)
1349 task->tk_client->cl_stats->rpcretrans++;
1350
Chuck Lever4a068252015-05-11 14:02:25 -04001351 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Trond Myklebustc8485e42009-03-11 14:37:59 -04001353 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -04001354 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001355 spin_lock_bh(&xprt->transport_lock);
1356
Trond Myklebustc8485e42009-03-11 14:37:59 -04001357 xprt->stat.sends++;
1358 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1359 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001360 xprt->stat.sending_u += xprt->sending.qlen;
1361 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001362 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001363
1364 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001365out_dequeue:
1366 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001367 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1368 return status;
1369}
1370
1371/**
1372 * xprt_transmit - send an RPC request on a transport
1373 * @task: controlling RPC task
1374 *
1375 * Attempts to drain the transmit queue. On exit, either the transport
1376 * signalled an error that needs to be handled before transmission can
1377 * resume, or @task finished transmitting, and detected that it already
1378 * received a reply.
1379 */
1380void
1381xprt_transmit(struct rpc_task *task)
1382{
1383 struct rpc_rqst *next, *req = task->tk_rqstp;
1384 struct rpc_xprt *xprt = req->rq_xprt;
1385 int status;
1386
1387 spin_lock(&xprt->queue_lock);
1388 while (!list_empty(&xprt->xmit_queue)) {
1389 next = list_first_entry(&xprt->xmit_queue,
1390 struct rpc_rqst, rq_xmit);
1391 xprt_pin_rqst(next);
1392 spin_unlock(&xprt->queue_lock);
1393 status = xprt_request_transmit(next, task);
1394 if (status == -EBADMSG && next != req)
1395 status = 0;
1396 cond_resched();
1397 spin_lock(&xprt->queue_lock);
1398 xprt_unpin_rqst(next);
1399 if (status == 0) {
1400 if (!xprt_request_data_received(task) ||
1401 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1402 continue;
Trond Myklebustc5445772018-09-03 23:39:27 -04001403 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001404 task->tk_status = status;
1405 break;
1406 }
1407 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Trond Myklebustba60eb22013-04-14 10:49:37 -04001410static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1411{
1412 set_bit(XPRT_CONGESTED, &xprt->state);
1413 rpc_sleep_on(&xprt->backlog, task, NULL);
1414}
1415
1416static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1417{
1418 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1419 clear_bit(XPRT_CONGESTED, &xprt->state);
1420}
1421
1422static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1423{
1424 bool ret = false;
1425
1426 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1427 goto out;
1428 spin_lock(&xprt->reserve_lock);
1429 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1430 rpc_sleep_on(&xprt->backlog, task, NULL);
1431 ret = true;
1432 }
1433 spin_unlock(&xprt->reserve_lock);
1434out:
1435 return ret;
1436}
1437
Trond Myklebust92ea0112017-06-20 19:35:39 -04001438static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001439{
1440 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1441
Chuck Leverff699ea82018-03-05 15:13:13 -05001442 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001443 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001444 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001445 spin_unlock(&xprt->reserve_lock);
1446 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1447 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001448 if (req != NULL)
1449 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001450 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001451 req = ERR_PTR(-ENOMEM);
1452out:
1453 return req;
1454}
1455
1456static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1457{
Chuck Leverff699ea82018-03-05 15:13:13 -05001458 if (xprt->num_reqs > xprt->min_reqs) {
1459 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001460 kfree(req);
1461 return true;
1462 }
1463 return false;
1464}
1465
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001466void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001468 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001470 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001472 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1473 list_del(&req->rq_list);
1474 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001476 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001477 if (!IS_ERR(req))
1478 goto out_init_req;
1479 switch (PTR_ERR(req)) {
1480 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001481 dprintk("RPC: dynamic allocation of request slot "
1482 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001483 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001484 break;
1485 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001486 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001487 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001488 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001489 default:
1490 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001491 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001492 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001493 return;
1494out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001495 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1496 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001497 spin_unlock(&xprt->reserve_lock);
1498
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001499 task->tk_status = 0;
1500 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001502EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1503
Chuck Levera9cde232018-05-04 15:34:59 -04001504void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001505{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001506 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001507 if (!xprt_dynamic_free_slot(xprt, req)) {
1508 memset(req, 0, sizeof(*req)); /* mark unused */
1509 list_add(&req->rq_list, &xprt->free);
1510 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001511 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001512 spin_unlock(&xprt->reserve_lock);
1513}
Chuck Levera9cde232018-05-04 15:34:59 -04001514EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001515
Trond Myklebust21de0a92011-07-17 16:57:32 -04001516static void xprt_free_all_slots(struct rpc_xprt *xprt)
1517{
1518 struct rpc_rqst *req;
1519 while (!list_empty(&xprt->free)) {
1520 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1521 list_del(&req->rq_list);
1522 kfree(req);
1523 }
1524}
1525
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001526struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1527 unsigned int num_prealloc,
1528 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001529{
1530 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001531 struct rpc_rqst *req;
1532 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001533
1534 xprt = kzalloc(size, GFP_KERNEL);
1535 if (xprt == NULL)
1536 goto out;
1537
Trond Myklebust21de0a92011-07-17 16:57:32 -04001538 xprt_init(xprt, net);
1539
1540 for (i = 0; i < num_prealloc; i++) {
1541 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1542 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001543 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001544 list_add(&req->rq_list, &xprt->free);
1545 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001546 if (max_alloc > num_prealloc)
1547 xprt->max_reqs = max_alloc;
1548 else
1549 xprt->max_reqs = num_prealloc;
1550 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001551 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001552
1553 return xprt;
1554
1555out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001556 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001557out:
1558 return NULL;
1559}
1560EXPORT_SYMBOL_GPL(xprt_alloc);
1561
Pavel Emelyanove204e622010-09-29 16:03:13 +04001562void xprt_free(struct rpc_xprt *xprt)
1563{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001564 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001565 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001566 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001567}
1568EXPORT_SYMBOL_GPL(xprt_free);
1569
Trond Myklebust902c5882018-09-01 17:21:01 -04001570static void
1571xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1572{
1573 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1574}
1575
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001576static __be32
1577xprt_alloc_xid(struct rpc_xprt *xprt)
1578{
1579 __be32 xid;
1580
1581 spin_lock(&xprt->reserve_lock);
1582 xid = (__force __be32)xprt->xid++;
1583 spin_unlock(&xprt->reserve_lock);
1584 return xid;
1585}
1586
1587static void
1588xprt_init_xid(struct rpc_xprt *xprt)
1589{
1590 xprt->xid = prandom_u32();
1591}
1592
1593static void
1594xprt_request_init(struct rpc_task *task)
1595{
1596 struct rpc_xprt *xprt = task->tk_xprt;
1597 struct rpc_rqst *req = task->tk_rqstp;
1598
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001599 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1600 req->rq_task = task;
1601 req->rq_xprt = xprt;
1602 req->rq_buffer = NULL;
1603 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001604 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001605 req->rq_snd_buf.len = 0;
1606 req->rq_snd_buf.buflen = 0;
1607 req->rq_rcv_buf.len = 0;
1608 req->rq_rcv_buf.buflen = 0;
Trond Myklebust71700bb2018-11-30 16:11:15 -05001609 req->rq_snd_buf.bvec = NULL;
1610 req->rq_rcv_buf.bvec = NULL;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001611 req->rq_release_snd_buf = NULL;
1612 xprt_reset_majortimeo(req);
1613 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1614 req, ntohl(req->rq_xid));
1615}
1616
1617static void
1618xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1619{
1620 xprt->ops->alloc_slot(xprt, task);
1621 if (task->tk_rqstp != NULL)
1622 xprt_request_init(task);
1623}
1624
Chuck Lever9903cd12005-08-11 16:25:26 -04001625/**
1626 * xprt_reserve - allocate an RPC request slot
1627 * @task: RPC task requesting a slot allocation
1628 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001629 * If the transport is marked as being congested, or if no more
1630 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001631 * backlog queue.
1632 */
1633void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001635 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001637 task->tk_status = 0;
1638 if (task->tk_rqstp != NULL)
1639 return;
1640
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001641 task->tk_timeout = 0;
1642 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001643 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001644 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001645}
1646
1647/**
1648 * xprt_retry_reserve - allocate an RPC request slot
1649 * @task: RPC task requesting a slot allocation
1650 *
1651 * If no more slots are available, place the task on the transport's
1652 * backlog queue.
1653 * Note that the only difference with xprt_reserve is that we now
1654 * ignore the value of the XPRT_CONGESTED flag.
1655 */
1656void xprt_retry_reserve(struct rpc_task *task)
1657{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001658 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001659
1660 task->tk_status = 0;
1661 if (task->tk_rqstp != NULL)
1662 return;
1663
1664 task->tk_timeout = 0;
1665 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001666 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001669static void
1670xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1671{
1672 struct rpc_xprt *xprt = req->rq_xprt;
1673
Trond Myklebust944b0422018-08-09 23:33:21 -04001674 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1675 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001676 xprt_is_pinned_rqst(req)) {
1677 spin_lock(&xprt->queue_lock);
Trond Myklebust944b0422018-08-09 23:33:21 -04001678 xprt_request_dequeue_transmit_locked(task);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001679 xprt_request_dequeue_receive_locked(task);
1680 while (xprt_is_pinned_rqst(req)) {
1681 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1682 spin_unlock(&xprt->queue_lock);
1683 xprt_wait_on_pinned_rqst(req);
1684 spin_lock(&xprt->queue_lock);
1685 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1686 }
1687 spin_unlock(&xprt->queue_lock);
1688 }
1689}
1690
Chuck Lever9903cd12005-08-11 16:25:26 -04001691/**
1692 * xprt_release - release an RPC request slot
1693 * @task: task which is finished with the slot
1694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001696void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001698 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001699 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Trond Myklebust87ed5002013-01-07 14:30:46 -05001701 if (req == NULL) {
1702 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001703 xprt = task->tk_xprt;
Trond Myklebustbd79bc52018-09-07 19:38:55 -04001704 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001707 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001708
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001709 xprt = req->rq_xprt;
Weston Andros Adamson0a702192012-02-17 13:15:24 -05001710 if (task->tk_ops->rpc_count_stats != NULL)
1711 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1712 else if (task->tk_client)
1713 rpc_count_iostats(task, task->tk_client->cl_metrics);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001714 xprt_request_dequeue_all(task, req);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001715 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001716 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001717 if (xprt->ops->release_request)
1718 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 xprt->last_used = jiffies;
Trond Myklebustad3331a2016-08-02 13:47:43 -04001720 xprt_schedule_autodisconnect(xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001721 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001722 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001723 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001724 xprt_inject_disconnect(xprt);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001725 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust0472e472019-02-19 13:00:13 -05001726 xdr_free_bvec(&req->rq_snd_buf);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001727 if (req->rq_cred != NULL)
1728 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001730 if (req->rq_release_snd_buf)
1731 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001732
Chuck Lever46121cf2007-01-31 12:14:08 -05001733 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001734 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001735 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001736 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001737 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Trond Myklebust902c5882018-09-01 17:21:01 -04001740#ifdef CONFIG_SUNRPC_BACKCHANNEL
1741void
1742xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1743{
1744 struct xdr_buf *xbufp = &req->rq_snd_buf;
1745
1746 task->tk_rqstp = req;
1747 req->rq_task = task;
1748 xprt_init_connect_cookie(req, req->rq_xprt);
1749 /*
1750 * Set up the xdr_buf length.
1751 * This also indicates that the buffer is XDR encoded already.
1752 */
1753 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1754 xbufp->tail[0].iov_len;
Trond Myklebust902c5882018-09-01 17:21:01 -04001755}
1756#endif
1757
Trond Myklebust21de0a92011-07-17 16:57:32 -04001758static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001759{
Trond Myklebust30c51162015-02-24 20:31:39 -05001760 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001761
1762 spin_lock_init(&xprt->transport_lock);
1763 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001764 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001765
1766 INIT_LIST_HEAD(&xprt->free);
Trond Myklebust95f76912018-09-07 08:35:22 -04001767 xprt->recv_queue = RB_ROOT;
Trond Myklebust944b0422018-08-09 23:33:21 -04001768 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001769#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001770 spin_lock_init(&xprt->bc_pa_lock);
1771 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001772#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001773 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001774
Chuck Leverc2866762006-08-22 20:06:20 -04001775 xprt->last_used = jiffies;
1776 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001777 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001778
1779 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1780 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust79c99152018-09-09 13:53:05 -04001781 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001782 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1783
Chuck Leverc2866762006-08-22 20:06:20 -04001784 xprt_init_xid(xprt);
1785
Trond Myklebust21de0a92011-07-17 16:57:32 -04001786 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001787}
1788
1789/**
1790 * xprt_create_transport - create an RPC transport
1791 * @args: rpc transport creation arguments
1792 *
1793 */
1794struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1795{
1796 struct rpc_xprt *xprt;
1797 struct xprt_class *t;
1798
1799 spin_lock(&xprt_list_lock);
1800 list_for_each_entry(t, &xprt_list, list) {
1801 if (t->ident == args->ident) {
1802 spin_unlock(&xprt_list_lock);
1803 goto found;
1804 }
1805 }
1806 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001807 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001808 return ERR_PTR(-EIO);
1809
1810found:
1811 xprt = t->setup(args);
1812 if (IS_ERR(xprt)) {
1813 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1814 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001815 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001816 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001817 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1818 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001819 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1820 if (xprt_has_timer(xprt))
Kees Cookff861c42017-10-16 17:29:42 -07001821 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001822 else
Kees Cookff861c42017-10-16 17:29:42 -07001823 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001824
1825 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1826 xprt_destroy(xprt);
1827 return ERR_PTR(-EINVAL);
1828 }
1829 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1830 if (xprt->servername == NULL) {
1831 xprt_destroy(xprt);
1832 return ERR_PTR(-ENOMEM);
1833 }
1834
Jeff Layton3f940092015-03-31 12:03:28 -04001835 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001836
Chuck Lever46121cf2007-01-31 12:14:08 -05001837 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001838 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001839out:
Chuck Leverc2866762006-08-22 20:06:20 -04001840 return xprt;
1841}
1842
Trond Myklebust528fd352017-10-19 12:13:10 -04001843static void xprt_destroy_cb(struct work_struct *work)
1844{
1845 struct rpc_xprt *xprt =
1846 container_of(work, struct rpc_xprt, task_cleanup);
1847
1848 rpc_xprt_debugfs_unregister(xprt);
1849 rpc_destroy_wait_queue(&xprt->binding);
1850 rpc_destroy_wait_queue(&xprt->pending);
1851 rpc_destroy_wait_queue(&xprt->sending);
1852 rpc_destroy_wait_queue(&xprt->backlog);
1853 kfree(xprt->servername);
1854 /*
1855 * Tear down transport state and free the rpc_xprt
1856 */
1857 xprt->ops->destroy(xprt);
1858}
1859
Chuck Lever9903cd12005-08-11 16:25:26 -04001860/**
1861 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001862 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001865static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Chuck Lever46121cf2007-01-31 12:14:08 -05001867 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001868
Trond Myklebust528fd352017-10-19 12:13:10 -04001869 /*
1870 * Exclude transport connect/disconnect handlers and autoclose
1871 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001872 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1873
Trond Myklebust0065db32006-01-03 09:55:56 +01001874 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001875
1876 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001877 * Destroy sockets etc from the system workqueue so they can
1878 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001879 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001880 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1881 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust30c51162015-02-24 20:31:39 -05001884static void xprt_destroy_kref(struct kref *kref)
1885{
1886 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1887}
1888
1889/**
1890 * xprt_get - return a reference to an RPC transport.
1891 * @xprt: pointer to the transport
1892 *
1893 */
1894struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1895{
1896 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1897 return xprt;
1898 return NULL;
1899}
1900EXPORT_SYMBOL_GPL(xprt_get);
1901
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001902/**
1903 * xprt_put - release a reference to an RPC transport.
1904 * @xprt: pointer to the transport
1905 *
1906 */
1907void xprt_put(struct rpc_xprt *xprt)
1908{
Trond Myklebust30c51162015-02-24 20:31:39 -05001909 if (xprt != NULL)
1910 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001911}
Chuck Lever5d252f92016-01-07 14:50:10 -05001912EXPORT_SYMBOL_GPL(xprt_put);