blob: 533df198a0e9f57b12b7d4603d6e307dc08c4e4b [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
172/**
Chuck Lever12a80462005-08-25 16:25:51 -0700173 * xprt_reserve_xprt - serialize write access to transports
174 * @task: task that is requesting access to the transport
Randy Dunlap177c27b2011-07-28 06:54:36 +0000175 * @xprt: pointer to the target transport
Chuck Lever12a80462005-08-25 16:25:51 -0700176 *
177 * This prevents mixing the payload of separate requests, and prevents
178 * transport connects from colliding with writes. No congestion control
179 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400181int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Chuck Lever12a80462005-08-25 16:25:51 -0700183 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust34006ce2011-07-17 18:11:34 -0400184 int priority;
Chuck Lever12a80462005-08-25 16:25:51 -0700185
186 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
187 if (task == xprt->snd_task)
188 return 1;
Chuck Lever12a80462005-08-25 16:25:51 -0700189 goto out_sleep;
190 }
191 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500192
Chuck Lever12a80462005-08-25 16:25:51 -0700193 return 1;
194
195out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500196 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700197 task->tk_pid, xprt);
Trond Myklebustf05d54e2018-09-03 23:11:15 -0400198 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
Chuck Lever12a80462005-08-25 16:25:51 -0700199 task->tk_status = -EAGAIN;
Trond Myklebust34006ce2011-07-17 18:11:34 -0400200 if (req == NULL)
201 priority = RPC_PRIORITY_LOW;
202 else if (!req->rq_ntrans)
203 priority = RPC_PRIORITY_NORMAL;
Chuck Lever12a80462005-08-25 16:25:51 -0700204 else
Trond Myklebust34006ce2011-07-17 18:11:34 -0400205 priority = RPC_PRIORITY_HIGH;
206 rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
Chuck Lever12a80462005-08-25 16:25:51 -0700207 return 0;
208}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400209EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700210
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100211static void xprt_clear_locked(struct rpc_xprt *xprt)
212{
213 xprt->snd_task = NULL;
Trond Myklebustd19751e2012-09-11 17:21:25 -0400214 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100215 smp_mb__before_atomic();
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100216 clear_bit(XPRT_LOCKED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100217 smp_mb__after_atomic();
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100218 } else
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400219 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100220}
221
Trond Myklebust75891f52018-09-03 17:37:36 -0400222static bool
223xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
224{
225 return test_bit(XPRT_CWND_WAIT, &xprt->state);
226}
227
228static void
229xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
230{
231 if (!list_empty(&xprt->xmit_queue)) {
232 /* Peek at head of queue to see if it can make progress */
233 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
234 rq_xmit)->rq_cong)
235 return;
236 }
237 set_bit(XPRT_CWND_WAIT, &xprt->state);
238}
239
240static void
241xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
242{
243 if (!RPCXPRT_CONGESTED(xprt))
244 clear_bit(XPRT_CWND_WAIT, &xprt->state);
245}
246
Chuck Lever12a80462005-08-25 16:25:51 -0700247/*
248 * xprt_reserve_xprt_cong - serialize write access to transports
249 * @task: task that is requesting access to the transport
250 *
251 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
252 * integrated into the decision of whether a request is allowed to be
253 * woken up and given access to the transport.
Trond Myklebust75891f52018-09-03 17:37:36 -0400254 * Note that the lock is only granted if we know there are free slots.
Chuck Lever12a80462005-08-25 16:25:51 -0700255 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400256int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust34006ce2011-07-17 18:11:34 -0400259 int priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Chuck Lever2226feb2005-08-11 16:25:38 -0400261 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (task == xprt->snd_task)
263 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out_sleep;
265 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400266 if (req == NULL) {
267 xprt->snd_task = task;
268 return 1;
269 }
Trond Myklebust75891f52018-09-03 17:37:36 -0400270 if (!xprt_need_congestion_window_wait(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 xprt->snd_task = task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 1;
273 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100274 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500276 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Trond Myklebustf05d54e2018-09-03 23:11:15 -0400277 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 task->tk_status = -EAGAIN;
Trond Myklebust34006ce2011-07-17 18:11:34 -0400279 if (req == NULL)
280 priority = RPC_PRIORITY_LOW;
281 else if (!req->rq_ntrans)
282 priority = RPC_PRIORITY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 else
Trond Myklebust34006ce2011-07-17 18:11:34 -0400284 priority = RPC_PRIORITY_HIGH;
285 rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400288EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Chuck Lever12a80462005-08-25 16:25:51 -0700290static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int retval;
293
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400294 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400295 retval = xprt->ops->reserve_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400296 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return retval;
298}
299
Trond Myklebust961a8282012-01-17 22:57:37 -0500300static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Trond Myklebust961a8282012-01-17 22:57:37 -0500302 struct rpc_xprt *xprt = data;
Chuck Lever49e9a892005-08-25 16:25:51 -0700303
Chuck Lever49e9a892005-08-25 16:25:51 -0700304 xprt->snd_task = task;
Trond Myklebust961a8282012-01-17 22:57:37 -0500305 return true;
306}
Chuck Lever49e9a892005-08-25 16:25:51 -0700307
Trond Myklebust961a8282012-01-17 22:57:37 -0500308static void __xprt_lock_write_next(struct rpc_xprt *xprt)
309{
310 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
311 return;
312
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400313 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
314 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500315 return;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100316 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700317}
318
Trond Myklebust961a8282012-01-17 22:57:37 -0500319static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
320{
321 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
322 return;
Trond Myklebust75891f52018-09-03 17:37:36 -0400323 if (xprt_need_congestion_window_wait(xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500324 goto out_unlock;
Trond Myklebustf1dc2372016-05-27 12:59:33 -0400325 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
Trond Myklebust75891f52018-09-03 17:37:36 -0400326 __xprt_lock_write_func, xprt))
Trond Myklebust961a8282012-01-17 22:57:37 -0500327 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100329 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Chuck Lever49e9a892005-08-25 16:25:51 -0700332/**
333 * xprt_release_xprt - allow other requests to use a transport
334 * @xprt: transport with other tasks potentially waiting
335 * @task: task that is releasing access to the transport
336 *
337 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700339void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100342 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 __xprt_lock_write_next(xprt);
344 }
345}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400346EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Chuck Lever49e9a892005-08-25 16:25:51 -0700348/**
349 * xprt_release_xprt_cong - allow other requests to use a transport
350 * @xprt: transport with other tasks potentially waiting
351 * @task: task that is releasing access to the transport
352 *
353 * Note that "task" can be NULL. Another task is awoken to use the
354 * transport if the transport's congestion window allows it.
355 */
356void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
357{
358 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100359 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700360 __xprt_lock_write_next_cong(xprt);
361 }
362}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400363EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700364
365static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400367 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700368 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400369 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * Van Jacobson congestion avoidance. Check if the congestion window
374 * overflowed. Put the task to sleep if this is the case.
375 */
376static int
Trond Myklebust75891f52018-09-03 17:37:36 -0400377__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (req->rq_cong)
380 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500381 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Trond Myklebust75891f52018-09-03 17:37:36 -0400382 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
383 if (RPCXPRT_CONGESTED(xprt)) {
384 xprt_set_congestion_window_wait(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
Trond Myklebust75891f52018-09-03 17:37:36 -0400386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 req->rq_cong = 1;
388 xprt->cong += RPC_CWNDSCALE;
389 return 1;
390}
391
392/*
393 * Adjust the congestion window, and wake up the next task
394 * that has been sleeping due to congestion
395 */
396static void
397__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
398{
399 if (!req->rq_cong)
400 return;
401 req->rq_cong = 0;
402 xprt->cong -= RPC_CWNDSCALE;
Trond Myklebust75891f52018-09-03 17:37:36 -0400403 xprt_test_and_clear_congestion_window_wait(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700404 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Chuck Lever46c0ee82005-08-25 16:25:52 -0700407/**
Trond Myklebust75891f52018-09-03 17:37:36 -0400408 * xprt_request_get_cong - Request congestion control credits
409 * @xprt: pointer to transport
410 * @req: pointer to RPC request
411 *
412 * Useful for transports that require congestion control.
413 */
414bool
415xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
416{
417 bool ret = false;
418
419 if (req->rq_cong)
420 return true;
421 spin_lock_bh(&xprt->transport_lock);
422 ret = __xprt_get_cong(xprt, req) != 0;
423 spin_unlock_bh(&xprt->transport_lock);
424 return ret;
425}
426EXPORT_SYMBOL_GPL(xprt_request_get_cong);
427
428/**
Chuck Levera58dd392005-08-25 16:25:53 -0700429 * xprt_release_rqst_cong - housekeeping when request is complete
430 * @task: RPC request that recently completed
431 *
432 * Useful for transports that require congestion control.
433 */
434void xprt_release_rqst_cong(struct rpc_task *task)
435{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500436 struct rpc_rqst *req = task->tk_rqstp;
437
438 __xprt_put_cong(req->rq_xprt, req);
Chuck Levera58dd392005-08-25 16:25:53 -0700439}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400440EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700441
Trond Myklebust75891f52018-09-03 17:37:36 -0400442/*
443 * Clear the congestion window wait flag and wake up the next
444 * entry on xprt->sending
445 */
446static void
447xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
448{
449 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
450 spin_lock_bh(&xprt->transport_lock);
451 __xprt_lock_write_next_cong(xprt);
452 spin_unlock_bh(&xprt->transport_lock);
453 }
454}
455
Chuck Levera58dd392005-08-25 16:25:53 -0700456/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700457 * xprt_adjust_cwnd - adjust transport congestion window
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500458 * @xprt: pointer to xprt
Chuck Lever46c0ee82005-08-25 16:25:52 -0700459 * @task: recently completed RPC request used to adjust window
460 * @result: result code of completed RPC request
461 *
Chuck Lever4f4cf5a2014-05-28 10:34:49 -0400462 * The transport code maintains an estimate on the maximum number of out-
463 * standing RPC requests, using a smoothed version of the congestion
464 * avoidance implemented in 44BSD. This is basically the Van Jacobson
465 * congestion algorithm: If a retransmit occurs, the congestion window is
466 * halved; otherwise, it is incremented by 1/cwnd when
467 *
468 * - a reply is received and
469 * - a full number of requests are outstanding and
470 * - the congestion window hasn't been updated recently.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -0500472void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700474 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700475 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (result >= 0 && cwnd <= xprt->cong) {
478 /* The (cwnd >> 1) term makes sure
479 * the result gets rounded properly. */
480 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
481 if (cwnd > RPC_MAXCWND(xprt))
482 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700483 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else if (result == -ETIMEDOUT) {
485 cwnd >>= 1;
486 if (cwnd < RPC_CWNDSCALE)
487 cwnd = RPC_CWNDSCALE;
488 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500489 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xprt->cong, xprt->cwnd, cwnd);
491 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700492 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400494EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Chuck Lever44fbac22005-08-11 16:25:44 -0400496/**
497 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
498 * @xprt: transport with waiting tasks
499 * @status: result code to plant in each task before waking it
500 *
501 */
502void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
503{
504 if (status < 0)
505 rpc_wake_up_status(&xprt->pending, status);
506 else
507 rpc_wake_up(&xprt->pending);
508}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400509EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400510
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400511/**
512 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
513 * @task: task to be put to sleep
Randy Dunlap0b80ae42008-04-26 22:59:02 -0700514 * @action: function pointer to be executed after wait
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 Myklebustb6ddf642008-04-17 18:52:19 -0400520void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400521{
522 struct rpc_rqst *req = task->tk_rqstp;
523 struct rpc_xprt *xprt = req->rq_xprt;
524
Trond Myklebusta9a6b522013-02-22 14:57:57 -0500525 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400526 rpc_sleep_on(&xprt->pending, task, action);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400527}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400528EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400529
530/**
531 * xprt_write_space - wake the task waiting for transport output buffer space
532 * @xprt: transport with waiting tasks
533 *
534 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
535 */
536void xprt_write_space(struct rpc_xprt *xprt)
537{
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400538 spin_lock_bh(&xprt->transport_lock);
539 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500540 dprintk("RPC: write space: waking waiting task on "
541 "xprt %p\n", xprt);
Trond Myklebust2275cde2018-02-07 09:13:04 -0500542 rpc_wake_up_queued_task_on_wq(xprtiod_workqueue,
543 &xprt->pending, xprt->snd_task);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400544 }
545 spin_unlock_bh(&xprt->transport_lock);
546}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400547EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400548
Chuck Leverfe3aca22005-08-25 16:25:50 -0700549/**
550 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
551 * @task: task whose timeout is to be set
552 *
553 * Set a request's retransmit timeout based on the transport's
554 * default timeout parameters. Used by transports that don't adjust
555 * the retransmit timeout based on round-trip time estimation.
556 */
557void xprt_set_retrans_timeout_def(struct rpc_task *task)
558{
559 task->tk_timeout = task->tk_rqstp->rq_timeout;
560}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400561EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700562
Ben Hutchings2c530402012-07-10 10:55:09 +0000563/**
Chuck Leverfe3aca22005-08-25 16:25:50 -0700564 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
565 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800566 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700567 * Set a request's retransmit timeout using the RTT estimator.
568 */
569void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
570{
571 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500572 struct rpc_clnt *clnt = task->tk_client;
573 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700574 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500575 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700576
577 task->tk_timeout = rpc_calc_rto(rtt, timer);
578 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
579 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
580 task->tk_timeout = max_timeout;
581}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400582EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static void xprt_reset_majortimeo(struct rpc_rqst *req)
585{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500586 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 req->rq_majortimeo = req->rq_timeout;
589 if (to->to_exponential)
590 req->rq_majortimeo <<= to->to_retries;
591 else
592 req->rq_majortimeo += to->to_increment * to->to_retries;
593 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
594 req->rq_majortimeo = to->to_maxval;
595 req->rq_majortimeo += jiffies;
596}
597
Chuck Lever9903cd12005-08-11 16:25:26 -0400598/**
599 * xprt_adjust_timeout - adjust timeout values for next retransmit
600 * @req: RPC request containing parameters to use for the adjustment
601 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603int xprt_adjust_timeout(struct rpc_rqst *req)
604{
605 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500606 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int status = 0;
608
609 if (time_before(jiffies, req->rq_majortimeo)) {
610 if (to->to_exponential)
611 req->rq_timeout <<= 1;
612 else
613 req->rq_timeout += to->to_increment;
614 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
615 req->rq_timeout = to->to_maxval;
616 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
618 req->rq_timeout = to->to_initval;
619 req->rq_retries = 0;
620 xprt_reset_majortimeo(req);
621 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400622 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400624 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 status = -ETIMEDOUT;
626 }
627
628 if (req->rq_timeout == 0) {
629 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
630 req->rq_timeout = 5 * HZ;
631 }
632 return status;
633}
634
David Howells65f27f32006-11-22 14:55:48 +0000635static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
David Howells65f27f32006-11-22 14:55:48 +0000637 struct rpc_xprt *xprt =
638 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Trond Myklebust66af1e52007-11-06 10:18:36 -0500640 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400641 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 xprt_release_write(xprt, NULL);
Trond Myklebust79234c32015-09-18 15:53:24 -0400643 wake_up_bit(&xprt->state, XPRT_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Chuck Lever9903cd12005-08-11 16:25:26 -0400646/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500647 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400648 * @xprt: transport to flag for disconnect
649 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500651void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Chuck Lever46121cf2007-01-31 12:14:08 -0500653 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400654 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 xprt_clear_connected(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400656 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400657 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500659EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Trond Myklebust66af1e52007-11-06 10:18:36 -0500661/**
662 * xprt_force_disconnect - force a transport to disconnect
663 * @xprt: transport to disconnect
664 *
665 */
666void xprt_force_disconnect(struct rpc_xprt *xprt)
667{
668 /* Don't race with the test_bit() in xprt_clear_locked() */
669 spin_lock_bh(&xprt->transport_lock);
670 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
671 /* Try to schedule an autoclose RPC call */
672 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400673 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400674 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500675 spin_unlock_bh(&xprt->transport_lock);
676}
Chuck Levere2a4f4f2017-04-11 13:22:38 -0400677EXPORT_SYMBOL_GPL(xprt_force_disconnect);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500678
Trond Myklebust7f3a1d12018-08-23 00:03:43 -0400679static unsigned int
680xprt_connect_cookie(struct rpc_xprt *xprt)
681{
682 return READ_ONCE(xprt->connect_cookie);
683}
684
685static bool
686xprt_request_retransmit_after_disconnect(struct rpc_task *task)
687{
688 struct rpc_rqst *req = task->tk_rqstp;
689 struct rpc_xprt *xprt = req->rq_xprt;
690
691 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
692 !xprt_connected(xprt);
693}
694
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400695/**
696 * xprt_conditional_disconnect - force a transport to disconnect
697 * @xprt: transport to disconnect
698 * @cookie: 'connection cookie'
699 *
700 * This attempts to break the connection if and only if 'cookie' matches
701 * the current transport 'connection cookie'. It ensures that we don't
702 * try to break the connection more than once when we need to retransmit
703 * a batch of RPC requests.
704 *
705 */
706void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
707{
708 /* Don't race with the test_bit() in xprt_clear_locked() */
709 spin_lock_bh(&xprt->transport_lock);
710 if (cookie != xprt->connect_cookie)
711 goto out;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100712 if (test_bit(XPRT_CLOSING, &xprt->state))
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400713 goto out;
714 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
715 /* Try to schedule an autoclose RPC call */
716 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400717 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400718 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400719out:
720 spin_unlock_bh(&xprt->transport_lock);
721}
722
Trond Myklebustad3331a2016-08-02 13:47:43 -0400723static bool
724xprt_has_timer(const struct rpc_xprt *xprt)
725{
726 return xprt->idle_timeout != 0;
727}
728
729static void
730xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
731 __must_hold(&xprt->transport_lock)
732{
Trond Myklebustef3f54342018-08-08 09:23:32 -0400733 if (list_empty(&xprt->recv_queue) && xprt_has_timer(xprt))
Trond Myklebustad3331a2016-08-02 13:47:43 -0400734 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static void
Kees Cookff861c42017-10-16 17:29:42 -0700738xprt_init_autodisconnect(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Kees Cookff861c42017-10-16 17:29:42 -0700740 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400742 spin_lock(&xprt->transport_lock);
Trond Myklebustef3f54342018-08-08 09:23:32 -0400743 if (!list_empty(&xprt->recv_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto out_abort;
Trond Myklebustad3331a2016-08-02 13:47:43 -0400745 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
746 xprt->last_used = jiffies;
Chuck Lever2226feb2005-08-11 16:25:38 -0400747 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400749 spin_unlock(&xprt->transport_lock);
Trond Myklebust40a5f1b2016-05-27 10:39:50 -0400750 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400753 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500756bool xprt_lock_connect(struct rpc_xprt *xprt,
757 struct rpc_task *task,
758 void *cookie)
759{
760 bool ret = false;
761
762 spin_lock_bh(&xprt->transport_lock);
763 if (!test_bit(XPRT_LOCKED, &xprt->state))
764 goto out;
765 if (xprt->snd_task != task)
766 goto out;
767 xprt->snd_task = cookie;
768 ret = true;
769out:
770 spin_unlock_bh(&xprt->transport_lock);
771 return ret;
772}
773
774void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
775{
776 spin_lock_bh(&xprt->transport_lock);
777 if (xprt->snd_task != cookie)
778 goto out;
779 if (!test_bit(XPRT_LOCKED, &xprt->state))
780 goto out;
781 xprt->snd_task =NULL;
782 xprt->ops->release_xprt(xprt, NULL);
Trond Myklebustad3331a2016-08-02 13:47:43 -0400783 xprt_schedule_autodisconnect(xprt);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500784out:
785 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust79234c32015-09-18 15:53:24 -0400786 wake_up_bit(&xprt->state, XPRT_LOCKED);
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500787}
788
Chuck Lever9903cd12005-08-11 16:25:26 -0400789/**
790 * xprt_connect - schedule a transport connect operation
791 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
793 */
794void xprt_connect(struct rpc_task *task)
795{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500796 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Chuck Lever46121cf2007-01-31 12:14:08 -0500798 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xprt, (xprt_connected(xprt) ? "is" : "is not"));
800
Chuck Leverec739ef2006-08-22 20:06:15 -0400801 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400802 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
805 if (!xprt_lock_write(xprt, task))
806 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500807
808 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
809 xprt->ops->close(xprt);
810
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500811 if (!xprt_connected(xprt)) {
Trond Myklebusta8ce4a82010-04-16 16:42:12 -0400812 task->tk_timeout = task->tk_rqstp->rq_timeout;
NeilBrown2c2ee6d2016-11-23 14:44:58 +1100813 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
Trond Myklebust5d008372008-02-22 16:34:17 -0500814 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400815
816 if (test_bit(XPRT_CLOSING, &xprt->state))
817 return;
818 if (xprt_test_and_set_connecting(xprt))
819 return;
Chuck Lever262ca072006-03-20 13:44:16 -0500820 xprt->stat.connect_start = jiffies;
Trond Myklebust1b092092013-01-08 09:26:49 -0500821 xprt->ops->connect(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Trond Myklebust718ba5b2015-02-08 18:19:25 -0500823 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Chuck Lever9903cd12005-08-11 16:25:26 -0400826static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Trond Myklebustad2368d2013-01-08 10:08:33 -0500828 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Chuck Levercd983ef2008-06-11 17:56:13 -0400830 if (task->tk_status == 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500831 xprt->stat.connect_count++;
832 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500833 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 task->tk_pid);
835 return;
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 switch (task->tk_status) {
Trond Myklebust0fe8d042013-12-31 13:13:30 -0500839 case -ECONNREFUSED:
840 case -ECONNRESET:
841 case -ECONNABORTED:
842 case -ENETUNREACH:
843 case -EHOSTUNREACH:
Trond Myklebust2fc193c2014-07-03 00:02:57 -0400844 case -EPIPE:
Trond Myklebust2a491992009-03-11 14:38:00 -0400845 case -EAGAIN:
846 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
Chuck Lever23475d62005-08-11 16:25:08 -0400847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500849 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
850 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500853 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
854 "server %s\n", task->tk_pid, -task->tk_status,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500855 xprt->servername);
Chuck Lever23475d62005-08-11 16:25:08 -0400856 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Chuck Lever9903cd12005-08-11 16:25:26 -0400860/**
861 * xprt_lookup_rqst - find an RPC request corresponding to an XID
862 * @xprt: transport on which the original request was transmitted
863 * @xid: RPC XID of incoming reply
864 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400865 * Caller holds xprt->queue_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700867struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400869 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Trond Myklebustef3f54342018-08-08 09:23:32 -0400871 list_for_each_entry(entry, &xprt->recv_queue, rq_recv)
Jeff Layton3705ad62014-10-28 14:24:13 -0400872 if (entry->rq_xid == xid) {
873 trace_xprt_lookup_rqst(xprt, xid, 0);
Chuck Lever0b87a462018-03-05 15:13:02 -0500874 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
Chuck Lever262ca072006-03-20 13:44:16 -0500875 return entry;
Jeff Layton3705ad62014-10-28 14:24:13 -0400876 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500877
878 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
879 ntohl(xid));
Jeff Layton3705ad62014-10-28 14:24:13 -0400880 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
Chuck Lever262ca072006-03-20 13:44:16 -0500881 xprt->stat.bad_xids++;
882 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400884EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400886static bool
887xprt_is_pinned_rqst(struct rpc_rqst *req)
888{
889 return atomic_read(&req->rq_pin) != 0;
890}
891
Trond Myklebust729749b2017-08-13 10:03:59 -0400892/**
893 * xprt_pin_rqst - Pin a request on the transport receive list
894 * @req: Request to pin
895 *
896 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400897 * so should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400898 */
899void xprt_pin_rqst(struct rpc_rqst *req)
900{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400901 atomic_inc(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400902}
Chuck Lever9590d082017-08-23 17:05:58 -0400903EXPORT_SYMBOL_GPL(xprt_pin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400904
905/**
906 * xprt_unpin_rqst - Unpin a request on the transport receive list
907 * @req: Request to pin
908 *
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400909 * Caller should be holding the xprt receive lock.
Trond Myklebust729749b2017-08-13 10:03:59 -0400910 */
911void xprt_unpin_rqst(struct rpc_rqst *req)
912{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400913 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
914 atomic_dec(&req->rq_pin);
915 return;
916 }
917 if (atomic_dec_and_test(&req->rq_pin))
918 wake_up_var(&req->rq_pin);
Trond Myklebust729749b2017-08-13 10:03:59 -0400919}
Chuck Lever9590d082017-08-23 17:05:58 -0400920EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
Trond Myklebust729749b2017-08-13 10:03:59 -0400921
922static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
Trond Myklebust729749b2017-08-13 10:03:59 -0400923{
Trond Myklebustcf9946c2018-08-06 12:55:34 -0400924 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
Trond Myklebust729749b2017-08-13 10:03:59 -0400925}
926
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400927static bool
928xprt_request_data_received(struct rpc_task *task)
929{
930 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
931 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
932}
933
934static bool
935xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
936{
937 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
938 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
939}
940
941/**
942 * xprt_request_enqueue_receive - Add an request to the receive queue
943 * @task: RPC task
944 *
945 */
946void
947xprt_request_enqueue_receive(struct rpc_task *task)
948{
949 struct rpc_rqst *req = task->tk_rqstp;
950 struct rpc_xprt *xprt = req->rq_xprt;
951
952 if (!xprt_request_need_enqueue_receive(task, req))
953 return;
954 spin_lock(&xprt->queue_lock);
955
956 /* Update the softirq receive buffer */
957 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
958 sizeof(req->rq_private_buf));
959
960 /* Add request to the receive list */
Trond Myklebustef3f54342018-08-08 09:23:32 -0400961 list_add_tail(&req->rq_recv, &xprt->recv_queue);
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400962 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
963 spin_unlock(&xprt->queue_lock);
964
965 xprt_reset_majortimeo(req);
966 /* Turn off autodisconnect */
967 del_singleshot_timer_sync(&xprt->timer);
968}
969
970/**
971 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
972 * @task: RPC task
973 *
974 * Caller must hold xprt->queue_lock.
975 */
976static void
977xprt_request_dequeue_receive_locked(struct rpc_task *task)
978{
979 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
Trond Myklebustef3f54342018-08-08 09:23:32 -0400980 list_del(&task->tk_rqstp->rq_recv);
Trond Myklebustedc81dc2018-08-22 17:55:46 -0400981}
982
Chuck Leverecd465e2018-03-05 15:12:57 -0500983/**
984 * xprt_update_rtt - Update RPC RTT statistics
985 * @task: RPC request that recently completed
986 *
Trond Myklebust75c84152018-08-31 10:21:00 -0400987 * Caller holds xprt->queue_lock.
Chuck Leverecd465e2018-03-05 15:12:57 -0500988 */
989void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700991 struct rpc_rqst *req = task->tk_rqstp;
992 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
Eric Dumazet95c96172012-04-15 05:58:06 +0000993 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400994 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Chuck Lever1570c1e2005-08-25 16:25:52 -0700996 if (timer) {
997 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -0400998 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -0700999 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Chuck Lever1570c1e2005-08-25 16:25:52 -07001001}
Chuck Leverecd465e2018-03-05 15:12:57 -05001002EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Chuck Lever1570c1e2005-08-25 16:25:52 -07001004/**
1005 * xprt_complete_rqst - called when reply processing is complete
1006 * @task: RPC request that recently completed
1007 * @copied: actual number of bytes received from the transport
1008 *
Trond Myklebust75c84152018-08-31 10:21:00 -04001009 * Caller holds xprt->queue_lock.
Chuck Lever1570c1e2005-08-25 16:25:52 -07001010 */
1011void xprt_complete_rqst(struct rpc_task *task, int copied)
1012{
1013 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -05001014 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Chuck Lever1570c1e2005-08-25 16:25:52 -07001016 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1017 task->tk_pid, ntohl(req->rq_xid), copied);
Jeff Layton3705ad62014-10-28 14:24:13 -04001018 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Trond Myklebustfda13932008-02-22 16:34:12 -05001020 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -05001021
Trond Myklebust1e799b62008-03-21 16:19:41 -04001022 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001023 /* Ensure all writes are done before we update */
1024 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001025 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001026 req->rq_reply_bytes_recvd = copied;
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001027 xprt_request_dequeue_receive_locked(task);
Trond Myklebustfda13932008-02-22 16:34:12 -05001028 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -04001030EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Chuck Lever46c0ee82005-08-25 16:25:52 -07001032static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Chuck Lever46c0ee82005-08-25 16:25:52 -07001034 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct rpc_xprt *xprt = req->rq_xprt;
1036
Trond Myklebust5d008372008-02-22 16:34:17 -05001037 if (task->tk_status != -ETIMEDOUT)
1038 return;
Chuck Lever46c0ee82005-08-25 16:25:52 -07001039
Chuck Lever82476d92018-01-03 15:38:25 -05001040 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001041 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -07001042 if (xprt->ops->timer)
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001043 xprt->ops->timer(xprt, task);
Trond Myklebust5d008372008-02-22 16:34:17 -05001044 } else
1045 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Chuck Lever9903cd12005-08-11 16:25:26 -04001048/**
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001049 * xprt_request_wait_receive - wait for the reply to an RPC request
1050 * @task: RPC task about to send a request
1051 *
1052 */
1053void xprt_request_wait_receive(struct rpc_task *task)
1054{
1055 struct rpc_rqst *req = task->tk_rqstp;
1056 struct rpc_xprt *xprt = req->rq_xprt;
1057
1058 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1059 return;
1060 /*
1061 * Sleep on the pending queue if we're expecting a reply.
1062 * The spinlock ensures atomicity between the test of
1063 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1064 */
1065 spin_lock(&xprt->queue_lock);
1066 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1067 xprt->ops->set_retrans_timeout(task);
1068 rpc_sleep_on(&xprt->pending, task, xprt_timer);
1069 /*
1070 * Send an extra queue wakeup call if the
1071 * connection was dropped in case the call to
1072 * rpc_sleep_on() raced.
1073 */
1074 if (xprt_request_retransmit_after_disconnect(task))
1075 rpc_wake_up_queued_task_set_status(&xprt->pending,
1076 task, -ENOTCONN);
1077 }
1078 spin_unlock(&xprt->queue_lock);
1079}
1080
Trond Myklebust944b0422018-08-09 23:33:21 -04001081static bool
Trond Myklebust944b0422018-08-09 23:33:21 -04001082xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1083{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001084 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
Trond Myklebust944b0422018-08-09 23:33:21 -04001085}
1086
1087/**
1088 * xprt_request_enqueue_transmit - queue a task for transmission
1089 * @task: pointer to rpc_task
1090 *
1091 * Add a task to the transmission queue.
1092 */
1093void
1094xprt_request_enqueue_transmit(struct rpc_task *task)
1095{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001096 struct rpc_rqst *pos, *req = task->tk_rqstp;
Trond Myklebust944b0422018-08-09 23:33:21 -04001097 struct rpc_xprt *xprt = req->rq_xprt;
1098
1099 if (xprt_request_need_enqueue_transmit(task, req)) {
1100 spin_lock(&xprt->queue_lock);
Trond Myklebust75891f52018-09-03 17:37:36 -04001101 /*
1102 * Requests that carry congestion control credits are added
1103 * to the head of the list to avoid starvation issues.
1104 */
1105 if (req->rq_cong) {
1106 xprt_clear_congestion_window_wait(xprt);
1107 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1108 if (pos->rq_cong)
1109 continue;
1110 /* Note: req is added _before_ pos */
1111 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1112 INIT_LIST_HEAD(&req->rq_xmit2);
1113 goto out;
1114 }
Trond Myklebust86aeee02018-09-08 14:22:41 -04001115 } else if (RPC_IS_SWAPPER(task)) {
1116 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1117 if (pos->rq_cong || pos->rq_bytes_sent)
1118 continue;
1119 if (RPC_IS_SWAPPER(pos->rq_task))
1120 continue;
1121 /* Note: req is added _before_ pos */
1122 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1123 INIT_LIST_HEAD(&req->rq_xmit2);
1124 goto out;
1125 }
Trond Myklebust75891f52018-09-03 17:37:36 -04001126 } else {
1127 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1128 if (pos->rq_task->tk_owner != task->tk_owner)
1129 continue;
1130 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1131 INIT_LIST_HEAD(&req->rq_xmit);
1132 goto out;
1133 }
Trond Myklebust918f3c12018-09-09 11:37:22 -04001134 }
Trond Myklebust944b0422018-08-09 23:33:21 -04001135 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
Trond Myklebust918f3c12018-09-09 11:37:22 -04001136 INIT_LIST_HEAD(&req->rq_xmit2);
1137out:
Trond Myklebust944b0422018-08-09 23:33:21 -04001138 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1139 spin_unlock(&xprt->queue_lock);
1140 }
1141}
1142
1143/**
1144 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1145 * @task: pointer to rpc_task
1146 *
1147 * Remove a task from the transmission queue
1148 * Caller must hold xprt->queue_lock
1149 */
1150static void
1151xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1152{
Trond Myklebust918f3c12018-09-09 11:37:22 -04001153 struct rpc_rqst *req = task->tk_rqstp;
1154
1155 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1156 return;
1157 if (!list_empty(&req->rq_xmit)) {
1158 list_del(&req->rq_xmit);
1159 if (!list_empty(&req->rq_xmit2)) {
1160 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1161 struct rpc_rqst, rq_xmit2);
1162 list_del(&req->rq_xmit2);
1163 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1164 }
1165 } else
1166 list_del(&req->rq_xmit2);
Trond Myklebust944b0422018-08-09 23:33:21 -04001167}
1168
1169/**
1170 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1171 * @task: pointer to rpc_task
1172 *
1173 * Remove a task from the transmission queue
1174 */
1175static void
1176xprt_request_dequeue_transmit(struct rpc_task *task)
1177{
1178 struct rpc_rqst *req = task->tk_rqstp;
1179 struct rpc_xprt *xprt = req->rq_xprt;
1180
1181 spin_lock(&xprt->queue_lock);
1182 xprt_request_dequeue_transmit_locked(task);
1183 spin_unlock(&xprt->queue_lock);
1184}
1185
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04001186/**
Trond Myklebust762e4e62018-08-24 16:28:28 -04001187 * xprt_request_need_retransmit - Test if a task needs retransmission
1188 * @task: pointer to rpc_task
1189 *
1190 * Test for whether a connection breakage requires the task to retransmit
1191 */
1192bool
1193xprt_request_need_retransmit(struct rpc_task *task)
1194{
1195 return xprt_request_retransmit_after_disconnect(task);
1196}
1197
1198/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001199 * xprt_prepare_transmit - reserve the transport before sending a request
1200 * @task: RPC task about to send a request
1201 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Trond Myklebust90051ea2013-09-25 12:17:18 -04001203bool xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct rpc_rqst *req = task->tk_rqstp;
1206 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Chuck Lever46121cf2007-01-31 12:14:08 -05001208 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001210 if (!xprt_lock_write(xprt, task)) {
1211 /* Race breaker: someone may have transmitted us */
Trond Myklebust944b0422018-08-09 23:33:21 -04001212 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001213 rpc_wake_up_queued_task_set_status(&xprt->sending,
1214 task, 0);
1215 return false;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
Trond Myklebust5f2f6bd2018-09-01 14:25:24 -04001218 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001221void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001222{
Rahul Iyer343952f2009-04-01 09:23:17 -04001223 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001224}
1225
Chuck Lever9903cd12005-08-11 16:25:26 -04001226/**
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001227 * xprt_request_transmit - send an RPC request on a transport
1228 * @req: pointer to request to transmit
1229 * @snd_task: RPC task that owns the transport lock
Chuck Lever9903cd12005-08-11 16:25:26 -04001230 *
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001231 * This performs the transmission of a single request.
1232 * Note that if the request is not the same as snd_task, then it
1233 * does need to be pinned.
1234 * Returns '0' on success.
Chuck Lever9903cd12005-08-11 16:25:26 -04001235 */
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001236static int
1237xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001239 struct rpc_xprt *xprt = req->rq_xprt;
1240 struct rpc_task *task = req->rq_task;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001241 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001242 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001243 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Chuck Lever46121cf2007-01-31 12:14:08 -05001245 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001247 if (!req->rq_bytes_sent) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001248 if (xprt_request_data_received(task)) {
1249 status = 0;
Trond Myklebust944b0422018-08-09 23:33:21 -04001250 goto out_dequeue;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001251 }
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001252 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001253 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001254 status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001255 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001256 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001259 /*
1260 * Update req->rq_ntrans before transmitting to avoid races with
1261 * xprt_update_rtt(), which needs to know that it is recording a
1262 * reply to the first transmission.
1263 */
1264 req->rq_ntrans++;
1265
Trond Myklebust90d91b02017-12-14 21:24:08 -05001266 connect_cookie = xprt->connect_cookie;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001267 status = xprt->ops->send_request(req, snd_task);
Jeff Layton3705ad62014-10-28 14:24:13 -04001268 trace_xprt_transmit(xprt, req->rq_xid, status);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001269 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001270 req->rq_ntrans--;
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001271 return status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001272 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001273
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001274 if (is_retrans)
1275 task->tk_client->cl_stats->rpcretrans++;
1276
Chuck Lever4a068252015-05-11 14:02:25 -04001277 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Trond Myklebustc8485e42009-03-11 14:37:59 -04001279 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -04001280 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001281 spin_lock_bh(&xprt->transport_lock);
1282
Trond Myklebustc8485e42009-03-11 14:37:59 -04001283 xprt->stat.sends++;
1284 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1285 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001286 xprt->stat.sending_u += xprt->sending.qlen;
1287 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001288 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001289
1290 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001291out_dequeue:
1292 xprt_request_dequeue_transmit(task);
Trond Myklebust89f90fe2018-08-29 17:40:55 -04001293 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1294 return status;
1295}
1296
1297/**
1298 * xprt_transmit - send an RPC request on a transport
1299 * @task: controlling RPC task
1300 *
1301 * Attempts to drain the transmit queue. On exit, either the transport
1302 * signalled an error that needs to be handled before transmission can
1303 * resume, or @task finished transmitting, and detected that it already
1304 * received a reply.
1305 */
1306void
1307xprt_transmit(struct rpc_task *task)
1308{
1309 struct rpc_rqst *next, *req = task->tk_rqstp;
1310 struct rpc_xprt *xprt = req->rq_xprt;
1311 int status;
1312
1313 spin_lock(&xprt->queue_lock);
1314 while (!list_empty(&xprt->xmit_queue)) {
1315 next = list_first_entry(&xprt->xmit_queue,
1316 struct rpc_rqst, rq_xmit);
1317 xprt_pin_rqst(next);
1318 spin_unlock(&xprt->queue_lock);
1319 status = xprt_request_transmit(next, task);
1320 if (status == -EBADMSG && next != req)
1321 status = 0;
1322 cond_resched();
1323 spin_lock(&xprt->queue_lock);
1324 xprt_unpin_rqst(next);
1325 if (status == 0) {
1326 if (!xprt_request_data_received(task) ||
1327 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1328 continue;
1329 } else if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1330 rpc_wake_up_queued_task(&xprt->pending, task);
1331 else
1332 task->tk_status = status;
1333 break;
1334 }
1335 spin_unlock(&xprt->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Trond Myklebustba60eb22013-04-14 10:49:37 -04001338static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1339{
1340 set_bit(XPRT_CONGESTED, &xprt->state);
1341 rpc_sleep_on(&xprt->backlog, task, NULL);
1342}
1343
1344static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1345{
1346 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1347 clear_bit(XPRT_CONGESTED, &xprt->state);
1348}
1349
1350static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1351{
1352 bool ret = false;
1353
1354 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1355 goto out;
1356 spin_lock(&xprt->reserve_lock);
1357 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1358 rpc_sleep_on(&xprt->backlog, task, NULL);
1359 ret = true;
1360 }
1361 spin_unlock(&xprt->reserve_lock);
1362out:
1363 return ret;
1364}
1365
Trond Myklebust92ea0112017-06-20 19:35:39 -04001366static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001367{
1368 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1369
Chuck Leverff699ea82018-03-05 15:13:13 -05001370 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001371 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001372 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001373 spin_unlock(&xprt->reserve_lock);
1374 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1375 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001376 if (req != NULL)
1377 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001378 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001379 req = ERR_PTR(-ENOMEM);
1380out:
1381 return req;
1382}
1383
1384static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1385{
Chuck Leverff699ea82018-03-05 15:13:13 -05001386 if (xprt->num_reqs > xprt->min_reqs) {
1387 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001388 kfree(req);
1389 return true;
1390 }
1391 return false;
1392}
1393
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001394void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001396 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001398 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001400 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1401 list_del(&req->rq_list);
1402 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001404 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001405 if (!IS_ERR(req))
1406 goto out_init_req;
1407 switch (PTR_ERR(req)) {
1408 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001409 dprintk("RPC: dynamic allocation of request slot "
1410 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001411 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001412 break;
1413 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001414 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001415 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001416 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001417 default:
1418 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001419 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001420 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001421 return;
1422out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001423 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1424 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001425 spin_unlock(&xprt->reserve_lock);
1426
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001427 task->tk_status = 0;
1428 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001430EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1431
1432void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1433{
1434 /* Note: grabbing the xprt_lock_write() ensures that we throttle
1435 * new slot allocation if the transport is congested (i.e. when
1436 * reconnecting a stream transport or when out of socket write
1437 * buffer space).
1438 */
1439 if (xprt_lock_write(xprt, task)) {
1440 xprt_alloc_slot(xprt, task);
1441 xprt_release_write(xprt, task);
1442 }
1443}
1444EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Chuck Levera9cde232018-05-04 15:34:59 -04001446void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001447{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001448 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001449 if (!xprt_dynamic_free_slot(xprt, req)) {
1450 memset(req, 0, sizeof(*req)); /* mark unused */
1451 list_add(&req->rq_list, &xprt->free);
1452 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001453 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001454 spin_unlock(&xprt->reserve_lock);
1455}
Chuck Levera9cde232018-05-04 15:34:59 -04001456EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001457
Trond Myklebust21de0a92011-07-17 16:57:32 -04001458static void xprt_free_all_slots(struct rpc_xprt *xprt)
1459{
1460 struct rpc_rqst *req;
1461 while (!list_empty(&xprt->free)) {
1462 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1463 list_del(&req->rq_list);
1464 kfree(req);
1465 }
1466}
1467
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001468struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1469 unsigned int num_prealloc,
1470 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001471{
1472 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001473 struct rpc_rqst *req;
1474 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001475
1476 xprt = kzalloc(size, GFP_KERNEL);
1477 if (xprt == NULL)
1478 goto out;
1479
Trond Myklebust21de0a92011-07-17 16:57:32 -04001480 xprt_init(xprt, net);
1481
1482 for (i = 0; i < num_prealloc; i++) {
1483 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1484 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001485 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001486 list_add(&req->rq_list, &xprt->free);
1487 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001488 if (max_alloc > num_prealloc)
1489 xprt->max_reqs = max_alloc;
1490 else
1491 xprt->max_reqs = num_prealloc;
1492 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001493 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001494
1495 return xprt;
1496
1497out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001498 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001499out:
1500 return NULL;
1501}
1502EXPORT_SYMBOL_GPL(xprt_alloc);
1503
Pavel Emelyanove204e622010-09-29 16:03:13 +04001504void xprt_free(struct rpc_xprt *xprt)
1505{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001506 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001507 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001508 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001509}
1510EXPORT_SYMBOL_GPL(xprt_free);
1511
Trond Myklebust902c5882018-09-01 17:21:01 -04001512static void
1513xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1514{
1515 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1516}
1517
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001518static __be32
1519xprt_alloc_xid(struct rpc_xprt *xprt)
1520{
1521 __be32 xid;
1522
1523 spin_lock(&xprt->reserve_lock);
1524 xid = (__force __be32)xprt->xid++;
1525 spin_unlock(&xprt->reserve_lock);
1526 return xid;
1527}
1528
1529static void
1530xprt_init_xid(struct rpc_xprt *xprt)
1531{
1532 xprt->xid = prandom_u32();
1533}
1534
1535static void
1536xprt_request_init(struct rpc_task *task)
1537{
1538 struct rpc_xprt *xprt = task->tk_xprt;
1539 struct rpc_rqst *req = task->tk_rqstp;
1540
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001541 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1542 req->rq_task = task;
1543 req->rq_xprt = xprt;
1544 req->rq_buffer = NULL;
1545 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001546 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001547 req->rq_bytes_sent = 0;
1548 req->rq_snd_buf.len = 0;
1549 req->rq_snd_buf.buflen = 0;
1550 req->rq_rcv_buf.len = 0;
1551 req->rq_rcv_buf.buflen = 0;
1552 req->rq_release_snd_buf = NULL;
1553 xprt_reset_majortimeo(req);
1554 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1555 req, ntohl(req->rq_xid));
1556}
1557
1558static void
1559xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1560{
1561 xprt->ops->alloc_slot(xprt, task);
1562 if (task->tk_rqstp != NULL)
1563 xprt_request_init(task);
1564}
1565
Chuck Lever9903cd12005-08-11 16:25:26 -04001566/**
1567 * xprt_reserve - allocate an RPC request slot
1568 * @task: RPC task requesting a slot allocation
1569 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001570 * If the transport is marked as being congested, or if no more
1571 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001572 * backlog queue.
1573 */
1574void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001576 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001578 task->tk_status = 0;
1579 if (task->tk_rqstp != NULL)
1580 return;
1581
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001582 task->tk_timeout = 0;
1583 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001584 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001585 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001586}
1587
1588/**
1589 * xprt_retry_reserve - allocate an RPC request slot
1590 * @task: RPC task requesting a slot allocation
1591 *
1592 * If no more slots are available, place the task on the transport's
1593 * backlog queue.
1594 * Note that the only difference with xprt_reserve is that we now
1595 * ignore the value of the XPRT_CONGESTED flag.
1596 */
1597void xprt_retry_reserve(struct rpc_task *task)
1598{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001599 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001600
1601 task->tk_status = 0;
1602 if (task->tk_rqstp != NULL)
1603 return;
1604
1605 task->tk_timeout = 0;
1606 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001607 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001610static void
1611xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1612{
1613 struct rpc_xprt *xprt = req->rq_xprt;
1614
Trond Myklebust944b0422018-08-09 23:33:21 -04001615 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1616 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001617 xprt_is_pinned_rqst(req)) {
1618 spin_lock(&xprt->queue_lock);
Trond Myklebust944b0422018-08-09 23:33:21 -04001619 xprt_request_dequeue_transmit_locked(task);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001620 xprt_request_dequeue_receive_locked(task);
1621 while (xprt_is_pinned_rqst(req)) {
1622 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1623 spin_unlock(&xprt->queue_lock);
1624 xprt_wait_on_pinned_rqst(req);
1625 spin_lock(&xprt->queue_lock);
1626 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1627 }
1628 spin_unlock(&xprt->queue_lock);
1629 }
1630}
1631
Chuck Lever9903cd12005-08-11 16:25:26 -04001632/**
1633 * xprt_release - release an RPC request slot
1634 * @task: task which is finished with the slot
1635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001637void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001639 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001640 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Trond Myklebust87ed5002013-01-07 14:30:46 -05001642 if (req == NULL) {
1643 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001644 xprt = task->tk_xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001645 if (xprt->snd_task == task)
1646 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001649 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001650
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001651 xprt = req->rq_xprt;
Weston Andros Adamson0a702192012-02-17 13:15:24 -05001652 if (task->tk_ops->rpc_count_stats != NULL)
1653 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1654 else if (task->tk_client)
1655 rpc_count_iostats(task, task->tk_client->cl_metrics);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001656 xprt_request_dequeue_all(task, req);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001657 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001658 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001659 if (xprt->ops->release_request)
1660 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 xprt->last_used = jiffies;
Trond Myklebustad3331a2016-08-02 13:47:43 -04001662 xprt_schedule_autodisconnect(xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001663 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001664 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001665 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001666 xprt_inject_disconnect(xprt);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001667 if (req->rq_cred != NULL)
1668 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001670 if (req->rq_release_snd_buf)
1671 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001672
Chuck Lever46121cf2007-01-31 12:14:08 -05001673 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001674 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001675 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001676 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001677 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Trond Myklebust902c5882018-09-01 17:21:01 -04001680#ifdef CONFIG_SUNRPC_BACKCHANNEL
1681void
1682xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1683{
1684 struct xdr_buf *xbufp = &req->rq_snd_buf;
1685
1686 task->tk_rqstp = req;
1687 req->rq_task = task;
1688 xprt_init_connect_cookie(req, req->rq_xprt);
1689 /*
1690 * Set up the xdr_buf length.
1691 * This also indicates that the buffer is XDR encoded already.
1692 */
1693 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1694 xbufp->tail[0].iov_len;
1695 req->rq_bytes_sent = 0;
1696}
1697#endif
1698
Trond Myklebust21de0a92011-07-17 16:57:32 -04001699static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001700{
Trond Myklebust30c51162015-02-24 20:31:39 -05001701 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001702
1703 spin_lock_init(&xprt->transport_lock);
1704 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001705 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001706
1707 INIT_LIST_HEAD(&xprt->free);
Trond Myklebustef3f54342018-08-08 09:23:32 -04001708 INIT_LIST_HEAD(&xprt->recv_queue);
Trond Myklebust944b0422018-08-09 23:33:21 -04001709 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001710#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001711 spin_lock_init(&xprt->bc_pa_lock);
1712 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001713#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001714 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001715
Chuck Leverc2866762006-08-22 20:06:20 -04001716 xprt->last_used = jiffies;
1717 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001718 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001719
1720 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1721 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust34006ce2011-07-17 18:11:34 -04001722 rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001723 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1724
Chuck Leverc2866762006-08-22 20:06:20 -04001725 xprt_init_xid(xprt);
1726
Trond Myklebust21de0a92011-07-17 16:57:32 -04001727 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001728}
1729
1730/**
1731 * xprt_create_transport - create an RPC transport
1732 * @args: rpc transport creation arguments
1733 *
1734 */
1735struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1736{
1737 struct rpc_xprt *xprt;
1738 struct xprt_class *t;
1739
1740 spin_lock(&xprt_list_lock);
1741 list_for_each_entry(t, &xprt_list, list) {
1742 if (t->ident == args->ident) {
1743 spin_unlock(&xprt_list_lock);
1744 goto found;
1745 }
1746 }
1747 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001748 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001749 return ERR_PTR(-EIO);
1750
1751found:
1752 xprt = t->setup(args);
1753 if (IS_ERR(xprt)) {
1754 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1755 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001756 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001757 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001758 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1759 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001760 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1761 if (xprt_has_timer(xprt))
Kees Cookff861c42017-10-16 17:29:42 -07001762 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001763 else
Kees Cookff861c42017-10-16 17:29:42 -07001764 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001765
1766 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1767 xprt_destroy(xprt);
1768 return ERR_PTR(-EINVAL);
1769 }
1770 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1771 if (xprt->servername == NULL) {
1772 xprt_destroy(xprt);
1773 return ERR_PTR(-ENOMEM);
1774 }
1775
Jeff Layton3f940092015-03-31 12:03:28 -04001776 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001777
Chuck Lever46121cf2007-01-31 12:14:08 -05001778 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001779 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001780out:
Chuck Leverc2866762006-08-22 20:06:20 -04001781 return xprt;
1782}
1783
Trond Myklebust528fd352017-10-19 12:13:10 -04001784static void xprt_destroy_cb(struct work_struct *work)
1785{
1786 struct rpc_xprt *xprt =
1787 container_of(work, struct rpc_xprt, task_cleanup);
1788
1789 rpc_xprt_debugfs_unregister(xprt);
1790 rpc_destroy_wait_queue(&xprt->binding);
1791 rpc_destroy_wait_queue(&xprt->pending);
1792 rpc_destroy_wait_queue(&xprt->sending);
1793 rpc_destroy_wait_queue(&xprt->backlog);
1794 kfree(xprt->servername);
1795 /*
1796 * Tear down transport state and free the rpc_xprt
1797 */
1798 xprt->ops->destroy(xprt);
1799}
1800
Chuck Lever9903cd12005-08-11 16:25:26 -04001801/**
1802 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001803 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001806static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Chuck Lever46121cf2007-01-31 12:14:08 -05001808 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001809
Trond Myklebust528fd352017-10-19 12:13:10 -04001810 /*
1811 * Exclude transport connect/disconnect handlers and autoclose
1812 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001813 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1814
Trond Myklebust0065db32006-01-03 09:55:56 +01001815 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001816
1817 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001818 * Destroy sockets etc from the system workqueue so they can
1819 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001820 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001821 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1822 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust30c51162015-02-24 20:31:39 -05001825static void xprt_destroy_kref(struct kref *kref)
1826{
1827 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1828}
1829
1830/**
1831 * xprt_get - return a reference to an RPC transport.
1832 * @xprt: pointer to the transport
1833 *
1834 */
1835struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1836{
1837 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1838 return xprt;
1839 return NULL;
1840}
1841EXPORT_SYMBOL_GPL(xprt_get);
1842
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001843/**
1844 * xprt_put - release a reference to an RPC transport.
1845 * @xprt: pointer to the transport
1846 *
1847 */
1848void xprt_put(struct rpc_xprt *xprt)
1849{
Trond Myklebust30c51162015-02-24 20:31:39 -05001850 if (xprt != NULL)
1851 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001852}
Chuck Lever5d252f92016-01-07 14:50:10 -05001853EXPORT_SYMBOL_GPL(xprt_put);