blob: d7585458dfe406898cb80dbdb2c9cae36cc69434 [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);
198 task->tk_timeout = 0;
199 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 task->tk_timeout = 0;
278 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/**
1227 * xprt_transmit - send an RPC request on a transport
1228 * @task: controlling RPC task
1229 *
1230 * We have to copy the iovec because sendmsg fiddles with its contents.
1231 */
1232void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct rpc_rqst *req = task->tk_rqstp;
1235 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebust90d91b02017-12-14 21:24:08 -05001236 unsigned int connect_cookie;
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001237 int is_retrans = RPC_WAS_SENT(task);
Chuck Leverff699ea82018-03-05 15:13:13 -05001238 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Chuck Lever46121cf2007-01-31 12:14:08 -05001240 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001242 if (!req->rq_bytes_sent) {
1243 if (xprt_request_data_received(task))
Trond Myklebust944b0422018-08-09 23:33:21 -04001244 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001245 /* Verify that our message lies in the RPCSEC_GSS window */
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001246 if (rpcauth_xmit_need_reencode(task)) {
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001247 task->tk_status = -EBADMSG;
Trond Myklebust944b0422018-08-09 23:33:21 -04001248 goto out_dequeue;
Trond Myklebust3021a5bb2018-08-14 13:50:21 -04001249 }
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001252 /*
1253 * Update req->rq_ntrans before transmitting to avoid races with
1254 * xprt_update_rtt(), which needs to know that it is recording a
1255 * reply to the first transmission.
1256 */
1257 req->rq_ntrans++;
1258
Trond Myklebust90d91b02017-12-14 21:24:08 -05001259 connect_cookie = xprt->connect_cookie;
Trond Myklebust50f484e2018-08-30 13:27:29 -04001260 status = xprt->ops->send_request(req, task);
Jeff Layton3705ad62014-10-28 14:24:13 -04001261 trace_xprt_transmit(xprt, req->rq_xid, status);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001262 if (status != 0) {
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001263 req->rq_ntrans--;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001264 task->tk_status = status;
Chuck Leverfe3aca22005-08-25 16:25:50 -07001265 return;
1266 }
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001267
Trond Myklebustdcbbeda2018-09-01 14:29:18 -04001268 if (is_retrans)
1269 task->tk_client->cl_stats->rpcretrans++;
1270
Chuck Lever4a068252015-05-11 14:02:25 -04001271 xprt_inject_disconnect(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Trond Myklebustc8485e42009-03-11 14:37:59 -04001273 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -04001274 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001275 spin_lock_bh(&xprt->transport_lock);
1276
Trond Myklebustc8485e42009-03-11 14:37:59 -04001277 xprt->stat.sends++;
1278 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1279 xprt->stat.bklog_u += xprt->backlog.qlen;
Andy Adamson15a45202012-02-14 16:19:18 -05001280 xprt->stat.sending_u += xprt->sending.qlen;
1281 xprt->stat.pending_u += xprt->pending.qlen;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001282 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust90d91b02017-12-14 21:24:08 -05001283
1284 req->rq_connect_cookie = connect_cookie;
Trond Myklebust944b0422018-08-09 23:33:21 -04001285out_dequeue:
1286 xprt_request_dequeue_transmit(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Trond Myklebustba60eb22013-04-14 10:49:37 -04001289static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1290{
1291 set_bit(XPRT_CONGESTED, &xprt->state);
1292 rpc_sleep_on(&xprt->backlog, task, NULL);
1293}
1294
1295static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1296{
1297 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1298 clear_bit(XPRT_CONGESTED, &xprt->state);
1299}
1300
1301static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1302{
1303 bool ret = false;
1304
1305 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1306 goto out;
1307 spin_lock(&xprt->reserve_lock);
1308 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1309 rpc_sleep_on(&xprt->backlog, task, NULL);
1310 ret = true;
1311 }
1312 spin_unlock(&xprt->reserve_lock);
1313out:
1314 return ret;
1315}
1316
Trond Myklebust92ea0112017-06-20 19:35:39 -04001317static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001318{
1319 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1320
Chuck Leverff699ea82018-03-05 15:13:13 -05001321 if (xprt->num_reqs >= xprt->max_reqs)
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001322 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001323 ++xprt->num_reqs;
Trond Myklebust92ea0112017-06-20 19:35:39 -04001324 spin_unlock(&xprt->reserve_lock);
1325 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1326 spin_lock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001327 if (req != NULL)
1328 goto out;
Chuck Leverff699ea82018-03-05 15:13:13 -05001329 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001330 req = ERR_PTR(-ENOMEM);
1331out:
1332 return req;
1333}
1334
1335static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1336{
Chuck Leverff699ea82018-03-05 15:13:13 -05001337 if (xprt->num_reqs > xprt->min_reqs) {
1338 --xprt->num_reqs;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001339 kfree(req);
1340 return true;
1341 }
1342 return false;
1343}
1344
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001345void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001347 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001349 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!list_empty(&xprt->free)) {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001351 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1352 list_del(&req->rq_list);
1353 goto out_init_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Trond Myklebust92ea0112017-06-20 19:35:39 -04001355 req = xprt_dynamic_alloc_slot(xprt);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001356 if (!IS_ERR(req))
1357 goto out_init_req;
1358 switch (PTR_ERR(req)) {
1359 case -ENOMEM:
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001360 dprintk("RPC: dynamic allocation of request slot "
1361 "failed! Retrying\n");
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001362 task->tk_status = -ENOMEM;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001363 break;
1364 case -EAGAIN:
Trond Myklebustba60eb22013-04-14 10:49:37 -04001365 xprt_add_backlog(xprt, task);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001366 dprintk("RPC: waiting for request slot\n");
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001367 /* fall through */
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001368 default:
1369 task->tk_status = -EAGAIN;
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001370 }
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001371 spin_unlock(&xprt->reserve_lock);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001372 return;
1373out_init_req:
Chuck Leverff699ea82018-03-05 15:13:13 -05001374 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1375 xprt->num_reqs);
Chuck Lever37ac86c2018-05-04 15:34:53 -04001376 spin_unlock(&xprt->reserve_lock);
1377
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001378 task->tk_status = 0;
1379 task->tk_rqstp = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04001381EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1382
1383void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1384{
1385 /* Note: grabbing the xprt_lock_write() ensures that we throttle
1386 * new slot allocation if the transport is congested (i.e. when
1387 * reconnecting a stream transport or when out of socket write
1388 * buffer space).
1389 */
1390 if (xprt_lock_write(xprt, task)) {
1391 xprt_alloc_slot(xprt, task);
1392 xprt_release_write(xprt, task);
1393 }
1394}
1395EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Chuck Levera9cde232018-05-04 15:34:59 -04001397void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001398{
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001399 spin_lock(&xprt->reserve_lock);
Trond Myklebustc25573b2011-12-01 14:16:17 -05001400 if (!xprt_dynamic_free_slot(xprt, req)) {
1401 memset(req, 0, sizeof(*req)); /* mark unused */
1402 list_add(&req->rq_list, &xprt->free);
1403 }
Trond Myklebustba60eb22013-04-14 10:49:37 -04001404 xprt_wake_up_backlog(xprt);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001405 spin_unlock(&xprt->reserve_lock);
1406}
Chuck Levera9cde232018-05-04 15:34:59 -04001407EXPORT_SYMBOL_GPL(xprt_free_slot);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001408
Trond Myklebust21de0a92011-07-17 16:57:32 -04001409static void xprt_free_all_slots(struct rpc_xprt *xprt)
1410{
1411 struct rpc_rqst *req;
1412 while (!list_empty(&xprt->free)) {
1413 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1414 list_del(&req->rq_list);
1415 kfree(req);
1416 }
1417}
1418
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001419struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1420 unsigned int num_prealloc,
1421 unsigned int max_alloc)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001422{
1423 struct rpc_xprt *xprt;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001424 struct rpc_rqst *req;
1425 int i;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001426
1427 xprt = kzalloc(size, GFP_KERNEL);
1428 if (xprt == NULL)
1429 goto out;
1430
Trond Myklebust21de0a92011-07-17 16:57:32 -04001431 xprt_init(xprt, net);
1432
1433 for (i = 0; i < num_prealloc; i++) {
1434 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1435 if (!req)
wangweidong83131642013-10-15 11:44:30 +08001436 goto out_free;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001437 list_add(&req->rq_list, &xprt->free);
1438 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04001439 if (max_alloc > num_prealloc)
1440 xprt->max_reqs = max_alloc;
1441 else
1442 xprt->max_reqs = num_prealloc;
1443 xprt->min_reqs = num_prealloc;
Chuck Leverff699ea82018-03-05 15:13:13 -05001444 xprt->num_reqs = num_prealloc;
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001445
1446 return xprt;
1447
1448out_free:
Trond Myklebust21de0a92011-07-17 16:57:32 -04001449 xprt_free(xprt);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04001450out:
1451 return NULL;
1452}
1453EXPORT_SYMBOL_GPL(xprt_alloc);
1454
Pavel Emelyanove204e622010-09-29 16:03:13 +04001455void xprt_free(struct rpc_xprt *xprt)
1456{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04001457 put_net(xprt->xprt_net);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001458 xprt_free_all_slots(xprt);
Trond Myklebustfda1bfe2015-02-14 17:48:49 -05001459 kfree_rcu(xprt, rcu);
Pavel Emelyanove204e622010-09-29 16:03:13 +04001460}
1461EXPORT_SYMBOL_GPL(xprt_free);
1462
Trond Myklebust902c5882018-09-01 17:21:01 -04001463static void
1464xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1465{
1466 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1467}
1468
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001469static __be32
1470xprt_alloc_xid(struct rpc_xprt *xprt)
1471{
1472 __be32 xid;
1473
1474 spin_lock(&xprt->reserve_lock);
1475 xid = (__force __be32)xprt->xid++;
1476 spin_unlock(&xprt->reserve_lock);
1477 return xid;
1478}
1479
1480static void
1481xprt_init_xid(struct rpc_xprt *xprt)
1482{
1483 xprt->xid = prandom_u32();
1484}
1485
1486static void
1487xprt_request_init(struct rpc_task *task)
1488{
1489 struct rpc_xprt *xprt = task->tk_xprt;
1490 struct rpc_rqst *req = task->tk_rqstp;
1491
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001492 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1493 req->rq_task = task;
1494 req->rq_xprt = xprt;
1495 req->rq_buffer = NULL;
1496 req->rq_xid = xprt_alloc_xid(xprt);
Trond Myklebust902c5882018-09-01 17:21:01 -04001497 xprt_init_connect_cookie(req, xprt);
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001498 req->rq_bytes_sent = 0;
1499 req->rq_snd_buf.len = 0;
1500 req->rq_snd_buf.buflen = 0;
1501 req->rq_rcv_buf.len = 0;
1502 req->rq_rcv_buf.buflen = 0;
1503 req->rq_release_snd_buf = NULL;
1504 xprt_reset_majortimeo(req);
1505 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1506 req, ntohl(req->rq_xid));
1507}
1508
1509static void
1510xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1511{
1512 xprt->ops->alloc_slot(xprt, task);
1513 if (task->tk_rqstp != NULL)
1514 xprt_request_init(task);
1515}
1516
Chuck Lever9903cd12005-08-11 16:25:26 -04001517/**
1518 * xprt_reserve - allocate an RPC request slot
1519 * @task: RPC task requesting a slot allocation
1520 *
Trond Myklebustba60eb22013-04-14 10:49:37 -04001521 * If the transport is marked as being congested, or if no more
1522 * slots are available, place the task on the transport's
Chuck Lever9903cd12005-08-11 16:25:26 -04001523 * backlog queue.
1524 */
1525void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001527 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001529 task->tk_status = 0;
1530 if (task->tk_rqstp != NULL)
1531 return;
1532
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001533 task->tk_timeout = 0;
1534 task->tk_status = -EAGAIN;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001535 if (!xprt_throttle_congested(xprt, task))
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001536 xprt_do_reserve(xprt, task);
Trond Myklebustba60eb22013-04-14 10:49:37 -04001537}
1538
1539/**
1540 * xprt_retry_reserve - allocate an RPC request slot
1541 * @task: RPC task requesting a slot allocation
1542 *
1543 * If no more slots are available, place the task on the transport's
1544 * backlog queue.
1545 * Note that the only difference with xprt_reserve is that we now
1546 * ignore the value of the XPRT_CONGESTED flag.
1547 */
1548void xprt_retry_reserve(struct rpc_task *task)
1549{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001550 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebustba60eb22013-04-14 10:49:37 -04001551
1552 task->tk_status = 0;
1553 if (task->tk_rqstp != NULL)
1554 return;
1555
1556 task->tk_timeout = 0;
1557 task->tk_status = -EAGAIN;
Trond Myklebust9dc6edc2018-08-22 14:24:16 -04001558 xprt_do_reserve(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001561static void
1562xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1563{
1564 struct rpc_xprt *xprt = req->rq_xprt;
1565
Trond Myklebust944b0422018-08-09 23:33:21 -04001566 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1567 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001568 xprt_is_pinned_rqst(req)) {
1569 spin_lock(&xprt->queue_lock);
Trond Myklebust944b0422018-08-09 23:33:21 -04001570 xprt_request_dequeue_transmit_locked(task);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001571 xprt_request_dequeue_receive_locked(task);
1572 while (xprt_is_pinned_rqst(req)) {
1573 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1574 spin_unlock(&xprt->queue_lock);
1575 xprt_wait_on_pinned_rqst(req);
1576 spin_lock(&xprt->queue_lock);
1577 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1578 }
1579 spin_unlock(&xprt->queue_lock);
1580 }
1581}
1582
Chuck Lever9903cd12005-08-11 16:25:26 -04001583/**
1584 * xprt_release - release an RPC request slot
1585 * @task: task which is finished with the slot
1586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001588void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001590 struct rpc_xprt *xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001591 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Trond Myklebust87ed5002013-01-07 14:30:46 -05001593 if (req == NULL) {
1594 if (task->tk_client) {
Trond Myklebustfb43d172016-01-30 16:39:26 -05001595 xprt = task->tk_xprt;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001596 if (xprt->snd_task == task)
1597 xprt_release_write(xprt, task);
Trond Myklebust87ed5002013-01-07 14:30:46 -05001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return;
Trond Myklebust87ed5002013-01-07 14:30:46 -05001600 }
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001601
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001602 xprt = req->rq_xprt;
Weston Andros Adamson0a702192012-02-17 13:15:24 -05001603 if (task->tk_ops->rpc_count_stats != NULL)
1604 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1605 else if (task->tk_client)
1606 rpc_count_iostats(task, task->tk_client->cl_metrics);
Trond Myklebustedc81dc2018-08-22 17:55:46 -04001607 xprt_request_dequeue_all(task, req);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001608 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001609 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001610 if (xprt->ops->release_request)
1611 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 xprt->last_used = jiffies;
Trond Myklebustad3331a2016-08-02 13:47:43 -04001613 xprt_schedule_autodisconnect(xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001614 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001615 if (req->rq_buffer)
Chuck Lever3435c742016-09-15 10:55:29 -04001616 xprt->ops->buf_free(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001617 xprt_inject_disconnect(xprt);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001618 if (req->rq_cred != NULL)
1619 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001621 if (req->rq_release_snd_buf)
1622 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001623
Chuck Lever46121cf2007-01-31 12:14:08 -05001624 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001625 if (likely(!bc_prealloc(req)))
Chuck Levera9cde232018-05-04 15:34:59 -04001626 xprt->ops->free_slot(xprt, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001627 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001628 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Trond Myklebust902c5882018-09-01 17:21:01 -04001631#ifdef CONFIG_SUNRPC_BACKCHANNEL
1632void
1633xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1634{
1635 struct xdr_buf *xbufp = &req->rq_snd_buf;
1636
1637 task->tk_rqstp = req;
1638 req->rq_task = task;
1639 xprt_init_connect_cookie(req, req->rq_xprt);
1640 /*
1641 * Set up the xdr_buf length.
1642 * This also indicates that the buffer is XDR encoded already.
1643 */
1644 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1645 xbufp->tail[0].iov_len;
1646 req->rq_bytes_sent = 0;
1647}
1648#endif
1649
Trond Myklebust21de0a92011-07-17 16:57:32 -04001650static void xprt_init(struct rpc_xprt *xprt, struct net *net)
Chuck Leverc2866762006-08-22 20:06:20 -04001651{
Trond Myklebust30c51162015-02-24 20:31:39 -05001652 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001653
1654 spin_lock_init(&xprt->transport_lock);
1655 spin_lock_init(&xprt->reserve_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001656 spin_lock_init(&xprt->queue_lock);
Chuck Leverc2866762006-08-22 20:06:20 -04001657
1658 INIT_LIST_HEAD(&xprt->free);
Trond Myklebustef3f54342018-08-08 09:23:32 -04001659 INIT_LIST_HEAD(&xprt->recv_queue);
Trond Myklebust944b0422018-08-09 23:33:21 -04001660 INIT_LIST_HEAD(&xprt->xmit_queue);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001661#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001662 spin_lock_init(&xprt->bc_pa_lock);
1663 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001664#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Trond Myklebust80b14d52015-02-14 20:31:59 -05001665 INIT_LIST_HEAD(&xprt->xprt_switch);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001666
Chuck Leverc2866762006-08-22 20:06:20 -04001667 xprt->last_used = jiffies;
1668 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001669 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001670
1671 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1672 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
Trond Myklebust34006ce2011-07-17 18:11:34 -04001673 rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
Chuck Leverc2866762006-08-22 20:06:20 -04001674 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1675
Chuck Leverc2866762006-08-22 20:06:20 -04001676 xprt_init_xid(xprt);
1677
Trond Myklebust21de0a92011-07-17 16:57:32 -04001678 xprt->xprt_net = get_net(net);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001679}
1680
1681/**
1682 * xprt_create_transport - create an RPC transport
1683 * @args: rpc transport creation arguments
1684 *
1685 */
1686struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1687{
1688 struct rpc_xprt *xprt;
1689 struct xprt_class *t;
1690
1691 spin_lock(&xprt_list_lock);
1692 list_for_each_entry(t, &xprt_list, list) {
1693 if (t->ident == args->ident) {
1694 spin_unlock(&xprt_list_lock);
1695 goto found;
1696 }
1697 }
1698 spin_unlock(&xprt_list_lock);
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001699 dprintk("RPC: transport (%d) not supported\n", args->ident);
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001700 return ERR_PTR(-EIO);
1701
1702found:
1703 xprt = t->setup(args);
1704 if (IS_ERR(xprt)) {
1705 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1706 -PTR_ERR(xprt));
Trond Myklebust21de0a92011-07-17 16:57:32 -04001707 goto out;
Trond Myklebust8d9266f2011-07-17 16:01:09 -04001708 }
J. Bruce Fields33d90ac2013-04-11 15:06:36 -04001709 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1710 xprt->idle_timeout = 0;
Trond Myklebust21de0a92011-07-17 16:57:32 -04001711 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1712 if (xprt_has_timer(xprt))
Kees Cookff861c42017-10-16 17:29:42 -07001713 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001714 else
Kees Cookff861c42017-10-16 17:29:42 -07001715 timer_setup(&xprt->timer, NULL, 0);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001716
1717 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1718 xprt_destroy(xprt);
1719 return ERR_PTR(-EINVAL);
1720 }
1721 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1722 if (xprt->servername == NULL) {
1723 xprt_destroy(xprt);
1724 return ERR_PTR(-ENOMEM);
1725 }
1726
Jeff Layton3f940092015-03-31 12:03:28 -04001727 rpc_xprt_debugfs_register(xprt);
Jeff Layton388f0c72014-11-26 14:44:44 -05001728
Chuck Lever46121cf2007-01-31 12:14:08 -05001729 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001730 xprt->max_reqs);
Trond Myklebust21de0a92011-07-17 16:57:32 -04001731out:
Chuck Leverc2866762006-08-22 20:06:20 -04001732 return xprt;
1733}
1734
Trond Myklebust528fd352017-10-19 12:13:10 -04001735static void xprt_destroy_cb(struct work_struct *work)
1736{
1737 struct rpc_xprt *xprt =
1738 container_of(work, struct rpc_xprt, task_cleanup);
1739
1740 rpc_xprt_debugfs_unregister(xprt);
1741 rpc_destroy_wait_queue(&xprt->binding);
1742 rpc_destroy_wait_queue(&xprt->pending);
1743 rpc_destroy_wait_queue(&xprt->sending);
1744 rpc_destroy_wait_queue(&xprt->backlog);
1745 kfree(xprt->servername);
1746 /*
1747 * Tear down transport state and free the rpc_xprt
1748 */
1749 xprt->ops->destroy(xprt);
1750}
1751
Chuck Lever9903cd12005-08-11 16:25:26 -04001752/**
1753 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001754 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001757static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Chuck Lever46121cf2007-01-31 12:14:08 -05001759 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust79234c32015-09-18 15:53:24 -04001760
Trond Myklebust528fd352017-10-19 12:13:10 -04001761 /*
1762 * Exclude transport connect/disconnect handlers and autoclose
1763 */
Trond Myklebust79234c32015-09-18 15:53:24 -04001764 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1765
Trond Myklebust0065db32006-01-03 09:55:56 +01001766 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001767
1768 /*
Trond Myklebust528fd352017-10-19 12:13:10 -04001769 * Destroy sockets etc from the system workqueue so they can
1770 * safely flush receive work running on rpciod.
Chuck Leverc8541ec2006-10-17 14:44:27 -04001771 */
Trond Myklebust528fd352017-10-19 12:13:10 -04001772 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1773 schedule_work(&xprt->task_cleanup);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Trond Myklebust30c51162015-02-24 20:31:39 -05001776static void xprt_destroy_kref(struct kref *kref)
1777{
1778 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1779}
1780
1781/**
1782 * xprt_get - return a reference to an RPC transport.
1783 * @xprt: pointer to the transport
1784 *
1785 */
1786struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1787{
1788 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1789 return xprt;
1790 return NULL;
1791}
1792EXPORT_SYMBOL_GPL(xprt_get);
1793
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001794/**
1795 * xprt_put - release a reference to an RPC transport.
1796 * @xprt: pointer to the transport
1797 *
1798 */
1799void xprt_put(struct rpc_xprt *xprt)
1800{
Trond Myklebust30c51162015-02-24 20:31:39 -05001801 if (xprt != NULL)
1802 kref_put(&xprt->kref, xprt_destroy_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001803}
Chuck Lever5d252f92016-01-07 14:50:10 -05001804EXPORT_SYMBOL_GPL(xprt_put);