xref: /openbmc/linux/net/sunrpc/xprt.c (revision deaa5c96)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/net/sunrpc/xprt.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This is a generic RPC call interface supporting congestion avoidance,
51da177e4SLinus Torvalds  *  and asynchronous calls.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  The interface works like this:
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  -	When a process places a call, it allocates a request slot if
101da177e4SLinus Torvalds  *	one is available. Otherwise, it sleeps on the backlog queue
111da177e4SLinus Torvalds  *	(xprt_reserve).
121da177e4SLinus Torvalds  *  -	Next, the caller puts together the RPC message, stuffs it into
1355aa4f58SChuck Lever  *	the request struct, and calls xprt_transmit().
1455aa4f58SChuck Lever  *  -	xprt_transmit sends the message and installs the caller on the
1555ae1aabSRicardo Labiaga  *	transport's wait list. At the same time, if a reply is expected,
1655ae1aabSRicardo Labiaga  *	it installs a timer that is run after the packet's timeout has
1755ae1aabSRicardo Labiaga  *	expired.
181da177e4SLinus Torvalds  *  -	When a packet arrives, the data_ready handler walks the list of
1955aa4f58SChuck Lever  *	pending requests for that transport. If a matching XID is found, the
201da177e4SLinus Torvalds  *	caller is woken up, and the timer removed.
211da177e4SLinus Torvalds  *  -	When no reply arrives within the timeout interval, the timer is
221da177e4SLinus Torvalds  *	fired by the kernel and runs xprt_timer(). It either adjusts the
231da177e4SLinus Torvalds  *	timeout values (minor timeout) or wakes up the caller with a status
241da177e4SLinus Torvalds  *	of -ETIMEDOUT.
251da177e4SLinus Torvalds  *  -	When the caller receives a notification from RPC that a reply arrived,
261da177e4SLinus Torvalds  *	it should release the RPC slot, and process the reply.
271da177e4SLinus Torvalds  *	If the call timed out, it may choose to retry the operation by
281da177e4SLinus Torvalds  *	adjusting the initial timeout value, and simply calling rpc_call
291da177e4SLinus Torvalds  *	again.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Support for async RPC is done through a set of RPC-specific scheduling
321da177e4SLinus Torvalds  *  primitives that `transparently' work for processes as well as async
331da177e4SLinus Torvalds  *  tasks that rely on callbacks.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
3655aa4f58SChuck Lever  *
3755aa4f58SChuck Lever  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
40a246b010SChuck Lever #include <linux/module.h>
41a246b010SChuck Lever 
421da177e4SLinus Torvalds #include <linux/types.h>
43a246b010SChuck Lever #include <linux/interrupt.h>
441da177e4SLinus Torvalds #include <linux/workqueue.h>
45bf3fcf89SChuck Lever #include <linux/net.h>
46ff839970SChuck Lever #include <linux/ktime.h>
471da177e4SLinus Torvalds 
48a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h>
50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
51fda1bfefSTrond Myklebust #include <linux/rcupdate.h>
521da177e4SLinus Torvalds 
533705ad64SJeff Layton #include <trace/events/sunrpc.h>
543705ad64SJeff Layton 
5555ae1aabSRicardo Labiaga #include "sunrpc.h"
5655ae1aabSRicardo Labiaga 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Local variables
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
61f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
621da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Local functions
671da177e4SLinus Torvalds  */
6821de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
6937ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
704e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
711da177e4SLinus Torvalds 
725ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7381c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7481c098afS\"Talpey, Thomas\ 
7512a80469SChuck Lever /**
7681c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
7781c098afS\"Talpey, Thomas\  * @transport: transport to register
7881c098afS\"Talpey, Thomas\  *
7981c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
8081c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
8181c098afS\"Talpey, Thomas\  *
8281c098afS\"Talpey, Thomas\  * Returns:
8381c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
8481c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
8581c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
8681c098afS\"Talpey, Thomas\  */
8781c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
8881c098afS\"Talpey, Thomas\ {
8981c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
9081c098afS\"Talpey, Thomas\ 	int result;
9181c098afS\"Talpey, Thomas\ 
9281c098afS\"Talpey, Thomas\ 	result = -EEXIST;
9381c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
9481c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
9581c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
964fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
9781c098afS\"Talpey, Thomas\ 			goto out;
9881c098afS\"Talpey, Thomas\ 	}
9981c098afS\"Talpey, Thomas\ 
10081c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
10181c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
10281c098afS\"Talpey, Thomas\ 	       transport->name);
10381c098afS\"Talpey, Thomas\ 	result = 0;
10481c098afS\"Talpey, Thomas\ 
10581c098afS\"Talpey, Thomas\ out:
10681c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
10781c098afS\"Talpey, Thomas\ 	return result;
10881c098afS\"Talpey, Thomas\ }
10981c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
11081c098afS\"Talpey, Thomas\ 
11181c098afS\"Talpey, Thomas\ /**
11281c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
11365b6e42cSRandy Dunlap  * @transport: transport to unregister
11481c098afS\"Talpey, Thomas\  *
11581c098afS\"Talpey, Thomas\  * Returns:
11681c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
11781c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
11881c098afS\"Talpey, Thomas\  */
11981c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
12081c098afS\"Talpey, Thomas\ {
12181c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
12281c098afS\"Talpey, Thomas\ 	int result;
12381c098afS\"Talpey, Thomas\ 
12481c098afS\"Talpey, Thomas\ 	result = 0;
12581c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
12681c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
12781c098afS\"Talpey, Thomas\ 		if (t == transport) {
12881c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
12981c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
13081c098afS\"Talpey, Thomas\ 				transport->name);
13181c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
13281c098afS\"Talpey, Thomas\ 			goto out;
13381c098afS\"Talpey, Thomas\ 		}
13481c098afS\"Talpey, Thomas\ 	}
13581c098afS\"Talpey, Thomas\ 	result = -ENOENT;
13681c098afS\"Talpey, Thomas\ 
13781c098afS\"Talpey, Thomas\ out:
13881c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
13981c098afS\"Talpey, Thomas\ 	return result;
14081c098afS\"Talpey, Thomas\ }
14181c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
14281c098afS\"Talpey, Thomas\ 
14381c098afS\"Talpey, Thomas\ /**
144441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
145441e3e24STom Talpey  * @transport_name: transport to load
146441e3e24STom Talpey  *
147441e3e24STom Talpey  * Returns:
148441e3e24STom Talpey  * 0:		transport successfully loaded
149441e3e24STom Talpey  * -ENOENT:	transport module not available
150441e3e24STom Talpey  */
151441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
152441e3e24STom Talpey {
153441e3e24STom Talpey 	struct xprt_class *t;
154441e3e24STom Talpey 	int result;
155441e3e24STom Talpey 
156441e3e24STom Talpey 	result = 0;
157441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
158441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
159441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
160441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
161441e3e24STom Talpey 			goto out;
162441e3e24STom Talpey 		}
163441e3e24STom Talpey 	}
164441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
165ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
166441e3e24STom Talpey out:
167441e3e24STom Talpey 	return result;
168441e3e24STom Talpey }
169441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
170441e3e24STom Talpey 
171c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
172c544577dSTrond Myklebust {
173c544577dSTrond Myklebust 	xprt->snd_task = NULL;
174c544577dSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
175c544577dSTrond Myklebust 		smp_mb__before_atomic();
176c544577dSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
177c544577dSTrond Myklebust 		smp_mb__after_atomic();
178c544577dSTrond Myklebust 	} else
179c544577dSTrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
180c544577dSTrond Myklebust }
181c544577dSTrond Myklebust 
182441e3e24STom Talpey /**
18312a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
18412a80469SChuck Lever  * @task: task that is requesting access to the transport
185177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
18612a80469SChuck Lever  *
18712a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
18812a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
18912a80469SChuck Lever  * is provided.
1901da177e4SLinus Torvalds  */
19143cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1921da177e4SLinus Torvalds {
19312a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
19412a80469SChuck Lever 
19512a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
19612a80469SChuck Lever 		if (task == xprt->snd_task)
19712a80469SChuck Lever 			return 1;
19812a80469SChuck Lever 		goto out_sleep;
19912a80469SChuck Lever 	}
200c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
201c544577dSTrond Myklebust 		goto out_unlock;
20212a80469SChuck Lever 	xprt->snd_task = task;
2034d4a76f3Sj223yang@asset.uwaterloo.ca 
20412a80469SChuck Lever 	return 1;
20512a80469SChuck Lever 
206c544577dSTrond Myklebust out_unlock:
207c544577dSTrond Myklebust 	xprt_clear_locked(xprt);
20812a80469SChuck Lever out_sleep:
20946121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
21012a80469SChuck Lever 			task->tk_pid, xprt);
211f05d54ecSTrond Myklebust 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
21212a80469SChuck Lever 	task->tk_status = -EAGAIN;
21379c99152STrond Myklebust 	rpc_sleep_on(&xprt->sending, task, NULL);
21412a80469SChuck Lever 	return 0;
21512a80469SChuck Lever }
21612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
21712a80469SChuck Lever 
21875891f50STrond Myklebust static bool
21975891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
22075891f50STrond Myklebust {
22175891f50STrond Myklebust 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
22275891f50STrond Myklebust }
22375891f50STrond Myklebust 
22475891f50STrond Myklebust static void
22575891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
22675891f50STrond Myklebust {
22775891f50STrond Myklebust 	if (!list_empty(&xprt->xmit_queue)) {
22875891f50STrond Myklebust 		/* Peek at head of queue to see if it can make progress */
22975891f50STrond Myklebust 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
23075891f50STrond Myklebust 					rq_xmit)->rq_cong)
23175891f50STrond Myklebust 			return;
23275891f50STrond Myklebust 	}
23375891f50STrond Myklebust 	set_bit(XPRT_CWND_WAIT, &xprt->state);
23475891f50STrond Myklebust }
23575891f50STrond Myklebust 
23675891f50STrond Myklebust static void
23775891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
23875891f50STrond Myklebust {
23975891f50STrond Myklebust 	if (!RPCXPRT_CONGESTED(xprt))
24075891f50STrond Myklebust 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
24175891f50STrond Myklebust }
24275891f50STrond Myklebust 
24312a80469SChuck Lever /*
24412a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
24512a80469SChuck Lever  * @task: task that is requesting access to the transport
24612a80469SChuck Lever  *
24712a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
24812a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
24912a80469SChuck Lever  * woken up and given access to the transport.
25075891f50STrond Myklebust  * Note that the lock is only granted if we know there are free slots.
25112a80469SChuck Lever  */
25243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
25312a80469SChuck Lever {
2541da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
2551da177e4SLinus Torvalds 
2562226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2571da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2581da177e4SLinus Torvalds 			return 1;
2591da177e4SLinus Torvalds 		goto out_sleep;
2601da177e4SLinus Torvalds 	}
26143cedbf0STrond Myklebust 	if (req == NULL) {
26243cedbf0STrond Myklebust 		xprt->snd_task = task;
26343cedbf0STrond Myklebust 		return 1;
26443cedbf0STrond Myklebust 	}
265c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
266c544577dSTrond Myklebust 		goto out_unlock;
26775891f50STrond Myklebust 	if (!xprt_need_congestion_window_wait(xprt)) {
2681da177e4SLinus Torvalds 		xprt->snd_task = task;
2691da177e4SLinus Torvalds 		return 1;
2701da177e4SLinus Torvalds 	}
271c544577dSTrond Myklebust out_unlock:
272632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2731da177e4SLinus Torvalds out_sleep:
27446121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
275f05d54ecSTrond Myklebust 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
2761da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
27779c99152STrond Myklebust 	rpc_sleep_on(&xprt->sending, task, NULL);
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds }
28012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2811da177e4SLinus Torvalds 
28212a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	int retval;
2851da177e4SLinus Torvalds 
286bd79bc57STrond Myklebust 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
287bd79bc57STrond Myklebust 		return 1;
2884a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
28943cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2904a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2911da177e4SLinus Torvalds 	return retval;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2951da177e4SLinus Torvalds {
296961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
29749e9a890SChuck Lever 
29849e9a890SChuck Lever 	xprt->snd_task = task;
299961a828dSTrond Myklebust 	return true;
300961a828dSTrond Myklebust }
301961a828dSTrond Myklebust 
302961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
303961a828dSTrond Myklebust {
304961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
30549e9a890SChuck Lever 		return;
306c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
307c544577dSTrond Myklebust 		goto out_unlock;
308f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
309f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
310961a828dSTrond Myklebust 		return;
311c544577dSTrond Myklebust out_unlock:
312632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
31349e9a890SChuck Lever }
31449e9a890SChuck Lever 
315961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
316961a828dSTrond Myklebust {
317961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
318961a828dSTrond Myklebust 		return;
319c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
320c544577dSTrond Myklebust 		goto out_unlock;
32175891f50STrond Myklebust 	if (xprt_need_congestion_window_wait(xprt))
322961a828dSTrond Myklebust 		goto out_unlock;
323f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
32475891f50STrond Myklebust 				__xprt_lock_write_func, xprt))
325961a828dSTrond Myklebust 		return;
3261da177e4SLinus Torvalds out_unlock:
327632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
33049e9a890SChuck Lever /**
33149e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
33249e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
33349e9a890SChuck Lever  * @task: task that is releasing access to the transport
33449e9a890SChuck Lever  *
33549e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3361da177e4SLinus Torvalds  */
33749e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
340632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3411da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds }
34412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3451da177e4SLinus Torvalds 
34649e9a890SChuck Lever /**
34749e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
34849e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
34949e9a890SChuck Lever  * @task: task that is releasing access to the transport
35049e9a890SChuck Lever  *
35149e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
35249e9a890SChuck Lever  * transport if the transport's congestion window allows it.
35349e9a890SChuck Lever  */
35449e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
35549e9a890SChuck Lever {
35649e9a890SChuck Lever 	if (xprt->snd_task == task) {
357632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
35849e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
35949e9a890SChuck Lever 	}
36049e9a890SChuck Lever }
36112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
36249e9a890SChuck Lever 
36349e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3641da177e4SLinus Torvalds {
365bd79bc57STrond Myklebust 	if (xprt->snd_task != task)
366bd79bc57STrond Myklebust 		return;
3674a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
36849e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3694a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3741da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3751da177e4SLinus Torvalds  */
3761da177e4SLinus Torvalds static int
37775891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	if (req->rq_cong)
3801da177e4SLinus Torvalds 		return 1;
38146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
38275891f50STrond Myklebust 			req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
38375891f50STrond Myklebust 	if (RPCXPRT_CONGESTED(xprt)) {
38475891f50STrond Myklebust 		xprt_set_congestion_window_wait(xprt);
3851da177e4SLinus Torvalds 		return 0;
38675891f50STrond Myklebust 	}
3871da177e4SLinus Torvalds 	req->rq_cong = 1;
3881da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
3891da177e4SLinus Torvalds 	return 1;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /*
3931da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
3941da177e4SLinus Torvalds  * that has been sleeping due to congestion
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds static void
3971da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	if (!req->rq_cong)
4001da177e4SLinus Torvalds 		return;
4011da177e4SLinus Torvalds 	req->rq_cong = 0;
4021da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
40375891f50STrond Myklebust 	xprt_test_and_clear_congestion_window_wait(xprt);
40449e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
40746c0ee8bSChuck Lever /**
40875891f50STrond Myklebust  * xprt_request_get_cong - Request congestion control credits
40975891f50STrond Myklebust  * @xprt: pointer to transport
41075891f50STrond Myklebust  * @req: pointer to RPC request
41175891f50STrond Myklebust  *
41275891f50STrond Myklebust  * Useful for transports that require congestion control.
41375891f50STrond Myklebust  */
41475891f50STrond Myklebust bool
41575891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
41675891f50STrond Myklebust {
41775891f50STrond Myklebust 	bool ret = false;
41875891f50STrond Myklebust 
41975891f50STrond Myklebust 	if (req->rq_cong)
42075891f50STrond Myklebust 		return true;
42175891f50STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
42275891f50STrond Myklebust 	ret = __xprt_get_cong(xprt, req) != 0;
42375891f50STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
42475891f50STrond Myklebust 	return ret;
42575891f50STrond Myklebust }
42675891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong);
42775891f50STrond Myklebust 
42875891f50STrond Myklebust /**
429a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
430a58dd398SChuck Lever  * @task: RPC request that recently completed
431a58dd398SChuck Lever  *
432a58dd398SChuck Lever  * Useful for transports that require congestion control.
433a58dd398SChuck Lever  */
434a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
435a58dd398SChuck Lever {
436a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
437a4f0835cSTrond Myklebust 
438a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
439a58dd398SChuck Lever }
44012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
441a58dd398SChuck Lever 
44275891f50STrond Myklebust /*
44375891f50STrond Myklebust  * Clear the congestion window wait flag and wake up the next
44475891f50STrond Myklebust  * entry on xprt->sending
44575891f50STrond Myklebust  */
44675891f50STrond Myklebust static void
44775891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
44875891f50STrond Myklebust {
44975891f50STrond Myklebust 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
45075891f50STrond Myklebust 		spin_lock_bh(&xprt->transport_lock);
45175891f50STrond Myklebust 		__xprt_lock_write_next_cong(xprt);
45275891f50STrond Myklebust 		spin_unlock_bh(&xprt->transport_lock);
45375891f50STrond Myklebust 	}
45475891f50STrond Myklebust }
45575891f50STrond Myklebust 
456a58dd398SChuck Lever /**
45746c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4586a24dfb6STrond Myklebust  * @xprt: pointer to xprt
45946c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
46046c0ee8bSChuck Lever  * @result: result code of completed RPC request
46146c0ee8bSChuck Lever  *
4624f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4634f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4644f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4654f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4664f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4674f4cf5adSChuck Lever  *
4684f4cf5adSChuck Lever  *	-	a reply is received and
4694f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4704f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4711da177e4SLinus Torvalds  */
4726a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4731da177e4SLinus Torvalds {
47446c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
47546c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4781da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4791da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4801da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4811da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4821da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
48349e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
4841da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
4851da177e4SLinus Torvalds 		cwnd >>= 1;
4861da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
4871da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
4901da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
4911da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
49246c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
4931da177e4SLinus Torvalds }
49412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
4951da177e4SLinus Torvalds 
49644fbac22SChuck Lever /**
49744fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
49844fbac22SChuck Lever  * @xprt: transport with waiting tasks
49944fbac22SChuck Lever  * @status: result code to plant in each task before waking it
50044fbac22SChuck Lever  *
50144fbac22SChuck Lever  */
50244fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
50344fbac22SChuck Lever {
50444fbac22SChuck Lever 	if (status < 0)
50544fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
50644fbac22SChuck Lever 	else
50744fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
50844fbac22SChuck Lever }
50912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
51044fbac22SChuck Lever 
511c7b2cae8SChuck Lever /**
512c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
513c544577dSTrond Myklebust  * @xprt: transport
514a9a6b52eSTrond Myklebust  *
515a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
516a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
517a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
518c7b2cae8SChuck Lever  */
519c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
520c7b2cae8SChuck Lever {
521c544577dSTrond Myklebust 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
522c7b2cae8SChuck Lever }
52312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
524c7b2cae8SChuck Lever 
525c544577dSTrond Myklebust static bool
526c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt)
527c544577dSTrond Myklebust {
528c544577dSTrond Myklebust 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
529c544577dSTrond Myklebust 		__xprt_lock_write_next(xprt);
530c544577dSTrond Myklebust 		dprintk("RPC:       write space: waking waiting task on "
531c544577dSTrond Myklebust 				"xprt %p\n", xprt);
532c544577dSTrond Myklebust 		return true;
533c544577dSTrond Myklebust 	}
534c544577dSTrond Myklebust 	return false;
535c544577dSTrond Myklebust }
536c544577dSTrond Myklebust 
537c7b2cae8SChuck Lever /**
538c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
539c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
540c7b2cae8SChuck Lever  *
541c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
542c7b2cae8SChuck Lever  */
543c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt)
544c7b2cae8SChuck Lever {
545c544577dSTrond Myklebust 	bool ret;
546c544577dSTrond Myklebust 
547c544577dSTrond Myklebust 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
548c544577dSTrond Myklebust 		return false;
549c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
550c544577dSTrond Myklebust 	ret = xprt_clear_write_space_locked(xprt);
551c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
552c544577dSTrond Myklebust 	return ret;
553c7b2cae8SChuck Lever }
55412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
555c7b2cae8SChuck Lever 
556fe3aca29SChuck Lever /**
557fe3aca29SChuck Lever  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
558fe3aca29SChuck Lever  * @task: task whose timeout is to be set
559fe3aca29SChuck Lever  *
560fe3aca29SChuck Lever  * Set a request's retransmit timeout based on the transport's
561fe3aca29SChuck Lever  * default timeout parameters.  Used by transports that don't adjust
562fe3aca29SChuck Lever  * the retransmit timeout based on round-trip time estimation.
563fe3aca29SChuck Lever  */
564fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task)
565fe3aca29SChuck Lever {
566fe3aca29SChuck Lever 	task->tk_timeout = task->tk_rqstp->rq_timeout;
567fe3aca29SChuck Lever }
56812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
569fe3aca29SChuck Lever 
5702c53040fSBen Hutchings /**
571fe3aca29SChuck Lever  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
572fe3aca29SChuck Lever  * @task: task whose timeout is to be set
573fe3aca29SChuck Lever  *
574fe3aca29SChuck Lever  * Set a request's retransmit timeout using the RTT estimator.
575fe3aca29SChuck Lever  */
576fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
577fe3aca29SChuck Lever {
578fe3aca29SChuck Lever 	int timer = task->tk_msg.rpc_proc->p_timer;
579ba7392bbSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
580ba7392bbSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
581fe3aca29SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
582ba7392bbSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
583fe3aca29SChuck Lever 
584fe3aca29SChuck Lever 	task->tk_timeout = rpc_calc_rto(rtt, timer);
585fe3aca29SChuck Lever 	task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
586fe3aca29SChuck Lever 	if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
587fe3aca29SChuck Lever 		task->tk_timeout = max_timeout;
588fe3aca29SChuck Lever }
58912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
590fe3aca29SChuck Lever 
5911da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5921da177e4SLinus Torvalds {
593ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5961da177e4SLinus Torvalds 	if (to->to_exponential)
5971da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5981da177e4SLinus Torvalds 	else
5991da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
6001da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
6011da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
6021da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6059903cd1cSChuck Lever /**
6069903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
6079903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
6089903cd1cSChuck Lever  *
6091da177e4SLinus Torvalds  */
6101da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
613ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
6141da177e4SLinus Torvalds 	int status = 0;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
6171da177e4SLinus Torvalds 		if (to->to_exponential)
6181da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
6191da177e4SLinus Torvalds 		else
6201da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
6211da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
6221da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
6231da177e4SLinus Torvalds 		req->rq_retries++;
6241da177e4SLinus Torvalds 	} else {
6251da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
6261da177e4SLinus Torvalds 		req->rq_retries = 0;
6271da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
6281da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
6294a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
6301da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
6314a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
6321da177e4SLinus Torvalds 		status = -ETIMEDOUT;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
6361da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6371da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 	return status;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
64265f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6431da177e4SLinus Torvalds {
64465f27f38SDavid Howells 	struct rpc_xprt *xprt =
64565f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
6461da177e4SLinus Torvalds 
64766af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6484876cc77STrond Myklebust 	xprt->ops->close(xprt);
6491da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
65079234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6539903cd1cSChuck Lever /**
65462da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6559903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6569903cd1cSChuck Lever  *
6571da177e4SLinus Torvalds  */
65862da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6614a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6621da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
663c544577dSTrond Myklebust 	xprt_clear_write_space_locked(xprt);
6642a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6654a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6661da177e4SLinus Torvalds }
66762da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6681da177e4SLinus Torvalds 
66966af1e55STrond Myklebust /**
67066af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
67166af1e55STrond Myklebust  * @xprt: transport to disconnect
67266af1e55STrond Myklebust  *
67366af1e55STrond Myklebust  */
67466af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
67566af1e55STrond Myklebust {
67666af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
67766af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
67866af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
67966af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
68066af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
68140a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6820445f92cSTrond Myklebust 	else if (xprt->snd_task)
6830445f92cSTrond Myklebust 		rpc_wake_up_queued_task_set_status(&xprt->pending,
6840445f92cSTrond Myklebust 				xprt->snd_task, -ENOTCONN);
68566af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
68666af1e55STrond Myklebust }
687e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
68866af1e55STrond Myklebust 
6897f3a1d1eSTrond Myklebust static unsigned int
6907f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
6917f3a1d1eSTrond Myklebust {
6927f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
6937f3a1d1eSTrond Myklebust }
6947f3a1d1eSTrond Myklebust 
6957f3a1d1eSTrond Myklebust static bool
6967f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
6977f3a1d1eSTrond Myklebust {
6987f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6997f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
7007f3a1d1eSTrond Myklebust 
7017f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
7027f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
7037f3a1d1eSTrond Myklebust }
7047f3a1d1eSTrond Myklebust 
7057c1d71cfSTrond Myklebust /**
7067c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
7077c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
7087c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
7097c1d71cfSTrond Myklebust  *
7107c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
7117c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
7127c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
7137c1d71cfSTrond Myklebust  * a batch of RPC requests.
7147c1d71cfSTrond Myklebust  *
7157c1d71cfSTrond Myklebust  */
7167c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
7177c1d71cfSTrond Myklebust {
7187c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
7197c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
7207c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
7217c1d71cfSTrond Myklebust 		goto out;
7222c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
7237c1d71cfSTrond Myklebust 		goto out;
7247c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
7257c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
7267c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
72740a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7282a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
7297c1d71cfSTrond Myklebust out:
7307c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
7317c1d71cfSTrond Myklebust }
7327c1d71cfSTrond Myklebust 
733ad3331acSTrond Myklebust static bool
734ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
735ad3331acSTrond Myklebust {
736ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
737ad3331acSTrond Myklebust }
738ad3331acSTrond Myklebust 
739ad3331acSTrond Myklebust static void
740ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
741ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
742ad3331acSTrond Myklebust {
74395f7691dSTrond Myklebust 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
744ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
745ad3331acSTrond Myklebust }
746ad3331acSTrond Myklebust 
7471da177e4SLinus Torvalds static void
748ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
7491da177e4SLinus Torvalds {
750ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
7511da177e4SLinus Torvalds 
7524a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
75395f7691dSTrond Myklebust 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
7541da177e4SLinus Torvalds 		goto out_abort;
755ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
756ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
7572226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
7581da177e4SLinus Torvalds 		goto out_abort;
7594a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
76040a5f1b1STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7611da177e4SLinus Torvalds 	return;
7621da177e4SLinus Torvalds out_abort:
7634a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
766718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
767718ba5b8STrond Myklebust 		struct rpc_task *task,
768718ba5b8STrond Myklebust 		void *cookie)
769718ba5b8STrond Myklebust {
770718ba5b8STrond Myklebust 	bool ret = false;
771718ba5b8STrond Myklebust 
772718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
773718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
774718ba5b8STrond Myklebust 		goto out;
775718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
776718ba5b8STrond Myklebust 		goto out;
777718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
778718ba5b8STrond Myklebust 	ret = true;
779718ba5b8STrond Myklebust out:
780718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
781718ba5b8STrond Myklebust 	return ret;
782718ba5b8STrond Myklebust }
783718ba5b8STrond Myklebust 
784718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
785718ba5b8STrond Myklebust {
786718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
787718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
788718ba5b8STrond Myklebust 		goto out;
789718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
790718ba5b8STrond Myklebust 		goto out;
791718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
792718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
793ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
794718ba5b8STrond Myklebust out:
795718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
79679234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
797718ba5b8STrond Myklebust }
798718ba5b8STrond Myklebust 
7999903cd1cSChuck Lever /**
8009903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
8019903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
8021da177e4SLinus Torvalds  *
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
8051da177e4SLinus Torvalds {
806ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
8071da177e4SLinus Torvalds 
80846121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
8091da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
8101da177e4SLinus Torvalds 
811ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
81201d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
8131da177e4SLinus Torvalds 		return;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
8161da177e4SLinus Torvalds 		return;
817feb8ca37STrond Myklebust 
818feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
819feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
820feb8ca37STrond Myklebust 
821718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
822a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
8232c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
824abc13275STrond Myklebust 		rpc_sleep_on(&xprt->pending, task, NULL);
8250b9e7943STrond Myklebust 
8260b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
8270b9e7943STrond Myklebust 			return;
8280b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
8290b9e7943STrond Myklebust 			return;
8300a9a4304STrond Myklebust 		/* Race breaker */
8310a9a4304STrond Myklebust 		if (!xprt_connected(xprt)) {
832262ca07dSChuck Lever 			xprt->stat.connect_start = jiffies;
8331b092092STrond Myklebust 			xprt->ops->connect(xprt, task);
8340a9a4304STrond Myklebust 		} else {
8350a9a4304STrond Myklebust 			xprt_clear_connecting(xprt);
8360a9a4304STrond Myklebust 			task->tk_status = 0;
8370a9a4304STrond Myklebust 			rpc_wake_up_queued_task(&xprt->pending, task);
8380a9a4304STrond Myklebust 		}
8391da177e4SLinus Torvalds 	}
840718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
84395f7691dSTrond Myklebust enum xprt_xid_rb_cmp {
84495f7691dSTrond Myklebust 	XID_RB_EQUAL,
84595f7691dSTrond Myklebust 	XID_RB_LEFT,
84695f7691dSTrond Myklebust 	XID_RB_RIGHT,
84795f7691dSTrond Myklebust };
84895f7691dSTrond Myklebust static enum xprt_xid_rb_cmp
84995f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2)
85095f7691dSTrond Myklebust {
85195f7691dSTrond Myklebust 	if (xid1 == xid2)
85295f7691dSTrond Myklebust 		return XID_RB_EQUAL;
85395f7691dSTrond Myklebust 	if ((__force u32)xid1 < (__force u32)xid2)
85495f7691dSTrond Myklebust 		return XID_RB_LEFT;
85595f7691dSTrond Myklebust 	return XID_RB_RIGHT;
85695f7691dSTrond Myklebust }
85795f7691dSTrond Myklebust 
85895f7691dSTrond Myklebust static struct rpc_rqst *
85995f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
86095f7691dSTrond Myklebust {
86195f7691dSTrond Myklebust 	struct rb_node *n = xprt->recv_queue.rb_node;
86295f7691dSTrond Myklebust 	struct rpc_rqst *req;
86395f7691dSTrond Myklebust 
86495f7691dSTrond Myklebust 	while (n != NULL) {
86595f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
86695f7691dSTrond Myklebust 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
86795f7691dSTrond Myklebust 		case XID_RB_LEFT:
86895f7691dSTrond Myklebust 			n = n->rb_left;
86995f7691dSTrond Myklebust 			break;
87095f7691dSTrond Myklebust 		case XID_RB_RIGHT:
87195f7691dSTrond Myklebust 			n = n->rb_right;
87295f7691dSTrond Myklebust 			break;
87395f7691dSTrond Myklebust 		case XID_RB_EQUAL:
87495f7691dSTrond Myklebust 			return req;
87595f7691dSTrond Myklebust 		}
87695f7691dSTrond Myklebust 	}
87795f7691dSTrond Myklebust 	return NULL;
87895f7691dSTrond Myklebust }
87995f7691dSTrond Myklebust 
88095f7691dSTrond Myklebust static void
88195f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
88295f7691dSTrond Myklebust {
88395f7691dSTrond Myklebust 	struct rb_node **p = &xprt->recv_queue.rb_node;
88495f7691dSTrond Myklebust 	struct rb_node *n = NULL;
88595f7691dSTrond Myklebust 	struct rpc_rqst *req;
88695f7691dSTrond Myklebust 
88795f7691dSTrond Myklebust 	while (*p != NULL) {
88895f7691dSTrond Myklebust 		n = *p;
88995f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
89095f7691dSTrond Myklebust 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
89195f7691dSTrond Myklebust 		case XID_RB_LEFT:
89295f7691dSTrond Myklebust 			p = &n->rb_left;
89395f7691dSTrond Myklebust 			break;
89495f7691dSTrond Myklebust 		case XID_RB_RIGHT:
89595f7691dSTrond Myklebust 			p = &n->rb_right;
89695f7691dSTrond Myklebust 			break;
89795f7691dSTrond Myklebust 		case XID_RB_EQUAL:
89895f7691dSTrond Myklebust 			WARN_ON_ONCE(new != req);
89995f7691dSTrond Myklebust 			return;
90095f7691dSTrond Myklebust 		}
90195f7691dSTrond Myklebust 	}
90295f7691dSTrond Myklebust 	rb_link_node(&new->rq_recv, n, p);
90395f7691dSTrond Myklebust 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
90495f7691dSTrond Myklebust }
90595f7691dSTrond Myklebust 
90695f7691dSTrond Myklebust static void
90795f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
90895f7691dSTrond Myklebust {
90995f7691dSTrond Myklebust 	rb_erase(&req->rq_recv, &xprt->recv_queue);
91095f7691dSTrond Myklebust }
91195f7691dSTrond Myklebust 
9129903cd1cSChuck Lever /**
9139903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
9149903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
9159903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
9169903cd1cSChuck Lever  *
91775c84151STrond Myklebust  * Caller holds xprt->queue_lock.
9181da177e4SLinus Torvalds  */
919d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
9201da177e4SLinus Torvalds {
9218f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
9221da177e4SLinus Torvalds 
92395f7691dSTrond Myklebust 	entry = xprt_request_rb_find(xprt, xid);
92495f7691dSTrond Myklebust 	if (entry != NULL) {
9253705ad64SJeff Layton 		trace_xprt_lookup_rqst(xprt, xid, 0);
9260b87a46bSChuck Lever 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
927262ca07dSChuck Lever 		return entry;
9283705ad64SJeff Layton 	}
92946121cf7SChuck Lever 
93046121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
93146121cf7SChuck Lever 			ntohl(xid));
9323705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
933262ca07dSChuck Lever 	xprt->stat.bad_xids++;
934262ca07dSChuck Lever 	return NULL;
9351da177e4SLinus Torvalds }
93612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
9371da177e4SLinus Torvalds 
938cf9946cdSTrond Myklebust static bool
939cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
940cf9946cdSTrond Myklebust {
941cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
942cf9946cdSTrond Myklebust }
943cf9946cdSTrond Myklebust 
944729749bbSTrond Myklebust /**
945729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
946729749bbSTrond Myklebust  * @req: Request to pin
947729749bbSTrond Myklebust  *
948729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
949cf9946cdSTrond Myklebust  * so should be holding the xprt receive lock.
950729749bbSTrond Myklebust  */
951729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
952729749bbSTrond Myklebust {
953cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
954729749bbSTrond Myklebust }
9559590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
956729749bbSTrond Myklebust 
957729749bbSTrond Myklebust /**
958729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
959729749bbSTrond Myklebust  * @req: Request to pin
960729749bbSTrond Myklebust  *
961cf9946cdSTrond Myklebust  * Caller should be holding the xprt receive lock.
962729749bbSTrond Myklebust  */
963729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
964729749bbSTrond Myklebust {
965cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
966cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
967cf9946cdSTrond Myklebust 		return;
968cf9946cdSTrond Myklebust 	}
969cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
970cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
971729749bbSTrond Myklebust }
9729590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
973729749bbSTrond Myklebust 
974729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
975729749bbSTrond Myklebust {
976cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
977729749bbSTrond Myklebust }
978729749bbSTrond Myklebust 
979edc81dcdSTrond Myklebust static bool
980edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
981edc81dcdSTrond Myklebust {
982edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
983edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
984edc81dcdSTrond Myklebust }
985edc81dcdSTrond Myklebust 
986edc81dcdSTrond Myklebust static bool
987edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
988edc81dcdSTrond Myklebust {
989edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
990edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
991edc81dcdSTrond Myklebust }
992edc81dcdSTrond Myklebust 
993edc81dcdSTrond Myklebust /**
994edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
995edc81dcdSTrond Myklebust  * @task: RPC task
996edc81dcdSTrond Myklebust  *
997edc81dcdSTrond Myklebust  */
998edc81dcdSTrond Myklebust void
999edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
1000edc81dcdSTrond Myklebust {
1001edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1002edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1003edc81dcdSTrond Myklebust 
1004edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
1005edc81dcdSTrond Myklebust 		return;
1006edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
1007edc81dcdSTrond Myklebust 
1008edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
1009edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1010edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
1011edc81dcdSTrond Myklebust 
1012edc81dcdSTrond Myklebust 	/* Add request to the receive list */
101395f7691dSTrond Myklebust 	xprt_request_rb_insert(xprt, req);
1014edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1015edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
1016edc81dcdSTrond Myklebust 
1017edc81dcdSTrond Myklebust 	xprt_reset_majortimeo(req);
1018edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
1019edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
1020edc81dcdSTrond Myklebust }
1021edc81dcdSTrond Myklebust 
1022edc81dcdSTrond Myklebust /**
1023edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1024edc81dcdSTrond Myklebust  * @task: RPC task
1025edc81dcdSTrond Myklebust  *
1026edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
1027edc81dcdSTrond Myklebust  */
1028edc81dcdSTrond Myklebust static void
1029edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
1030edc81dcdSTrond Myklebust {
103195f7691dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
103295f7691dSTrond Myklebust 
1033edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
103495f7691dSTrond Myklebust 		xprt_request_rb_remove(req->rq_xprt, req);
1035edc81dcdSTrond Myklebust }
1036edc81dcdSTrond Myklebust 
1037ecd465eeSChuck Lever /**
1038ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
1039ecd465eeSChuck Lever  * @task: RPC request that recently completed
1040ecd465eeSChuck Lever  *
104175c84151STrond Myklebust  * Caller holds xprt->queue_lock.
1042ecd465eeSChuck Lever  */
1043ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
10441da177e4SLinus Torvalds {
10451570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
10461570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
104795c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1048d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
10491570c1e4SChuck Lever 
10501da177e4SLinus Torvalds 	if (timer) {
10511da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
1052ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
10531570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds }
1056ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
10571da177e4SLinus Torvalds 
10581570c1e4SChuck Lever /**
10591570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
10601570c1e4SChuck Lever  * @task: RPC request that recently completed
10611570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
10621570c1e4SChuck Lever  *
106375c84151STrond Myklebust  * Caller holds xprt->queue_lock.
10641570c1e4SChuck Lever  */
10651570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
10661570c1e4SChuck Lever {
10671570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
1068fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
10691da177e4SLinus Torvalds 
10701570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
10711570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
10723705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
10731da177e4SLinus Torvalds 
1074fda13939STrond Myklebust 	xprt->stat.recvs++;
1075ef759a2eSChuck Lever 
10761e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1077dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1078dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
107943ac3f29STrond Myklebust 	smp_wmb();
1080dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1081edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1082fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
10831da177e4SLinus Torvalds }
108412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
10851da177e4SLinus Torvalds 
108646c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
10871da177e4SLinus Torvalds {
10881da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
10891da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
10901da177e4SLinus Torvalds 
10915d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
10925d00837bSTrond Myklebust 		return;
109346c0ee8bSChuck Lever 
109482476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1095dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
109646c0ee8bSChuck Lever 		if (xprt->ops->timer)
10976a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
10985d00837bSTrond Myklebust 	} else
10995d00837bSTrond Myklebust 		task->tk_status = 0;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11029903cd1cSChuck Lever /**
11037f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
11047f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
11057f3a1d1eSTrond Myklebust  *
11067f3a1d1eSTrond Myklebust  */
11077f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
11087f3a1d1eSTrond Myklebust {
11097f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11107f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
11117f3a1d1eSTrond Myklebust 
11127f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
11137f3a1d1eSTrond Myklebust 		return;
11147f3a1d1eSTrond Myklebust 	/*
11157f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
11167f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
11177f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
11187f3a1d1eSTrond Myklebust 	 */
11197f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
11207f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
11217f3a1d1eSTrond Myklebust 		xprt->ops->set_retrans_timeout(task);
11227f3a1d1eSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_timer);
11237f3a1d1eSTrond Myklebust 		/*
11247f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
11257f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
11267f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
11277f3a1d1eSTrond Myklebust 		 */
11287f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
11297f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
11307f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
11317f3a1d1eSTrond Myklebust 	}
11327f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
11337f3a1d1eSTrond Myklebust }
11347f3a1d1eSTrond Myklebust 
1135944b0429STrond Myklebust static bool
1136944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1137944b0429STrond Myklebust {
1138762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1139944b0429STrond Myklebust }
1140944b0429STrond Myklebust 
1141944b0429STrond Myklebust /**
1142944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1143944b0429STrond Myklebust  * @task: pointer to rpc_task
1144944b0429STrond Myklebust  *
1145944b0429STrond Myklebust  * Add a task to the transmission queue.
1146944b0429STrond Myklebust  */
1147944b0429STrond Myklebust void
1148944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1149944b0429STrond Myklebust {
1150918f3c1fSTrond Myklebust 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1151944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1152944b0429STrond Myklebust 
1153944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1154e66721f0STrond Myklebust 		req->rq_bytes_sent = 0;
1155944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
115675891f50STrond Myklebust 		/*
115775891f50STrond Myklebust 		 * Requests that carry congestion control credits are added
115875891f50STrond Myklebust 		 * to the head of the list to avoid starvation issues.
115975891f50STrond Myklebust 		 */
116075891f50STrond Myklebust 		if (req->rq_cong) {
116175891f50STrond Myklebust 			xprt_clear_congestion_window_wait(xprt);
116275891f50STrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
116375891f50STrond Myklebust 				if (pos->rq_cong)
116475891f50STrond Myklebust 					continue;
116575891f50STrond Myklebust 				/* Note: req is added _before_ pos */
116675891f50STrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
116775891f50STrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
116875891f50STrond Myklebust 				goto out;
116975891f50STrond Myklebust 			}
117086aeee0eSTrond Myklebust 		} else if (RPC_IS_SWAPPER(task)) {
117186aeee0eSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
117286aeee0eSTrond Myklebust 				if (pos->rq_cong || pos->rq_bytes_sent)
117386aeee0eSTrond Myklebust 					continue;
117486aeee0eSTrond Myklebust 				if (RPC_IS_SWAPPER(pos->rq_task))
117586aeee0eSTrond Myklebust 					continue;
117686aeee0eSTrond Myklebust 				/* Note: req is added _before_ pos */
117786aeee0eSTrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
117886aeee0eSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
117986aeee0eSTrond Myklebust 				goto out;
118086aeee0eSTrond Myklebust 			}
1181deaa5c96SChuck Lever 		} else if (!req->rq_seqno) {
1182918f3c1fSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1183918f3c1fSTrond Myklebust 				if (pos->rq_task->tk_owner != task->tk_owner)
1184918f3c1fSTrond Myklebust 					continue;
1185918f3c1fSTrond Myklebust 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1186918f3c1fSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit);
1187918f3c1fSTrond Myklebust 				goto out;
1188918f3c1fSTrond Myklebust 			}
118975891f50STrond Myklebust 		}
1190944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1191918f3c1fSTrond Myklebust 		INIT_LIST_HEAD(&req->rq_xmit2);
1192918f3c1fSTrond Myklebust out:
1193944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1194944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1195944b0429STrond Myklebust 	}
1196944b0429STrond Myklebust }
1197944b0429STrond Myklebust 
1198944b0429STrond Myklebust /**
1199944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1200944b0429STrond Myklebust  * @task: pointer to rpc_task
1201944b0429STrond Myklebust  *
1202944b0429STrond Myklebust  * Remove a task from the transmission queue
1203944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1204944b0429STrond Myklebust  */
1205944b0429STrond Myklebust static void
1206944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1207944b0429STrond Myklebust {
1208918f3c1fSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1209918f3c1fSTrond Myklebust 
1210918f3c1fSTrond Myklebust 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1211918f3c1fSTrond Myklebust 		return;
1212918f3c1fSTrond Myklebust 	if (!list_empty(&req->rq_xmit)) {
1213918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit);
1214918f3c1fSTrond Myklebust 		if (!list_empty(&req->rq_xmit2)) {
1215918f3c1fSTrond Myklebust 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1216918f3c1fSTrond Myklebust 					struct rpc_rqst, rq_xmit2);
1217918f3c1fSTrond Myklebust 			list_del(&req->rq_xmit2);
1218918f3c1fSTrond Myklebust 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1219918f3c1fSTrond Myklebust 		}
1220918f3c1fSTrond Myklebust 	} else
1221918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit2);
1222944b0429STrond Myklebust }
1223944b0429STrond Myklebust 
1224944b0429STrond Myklebust /**
1225944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1226944b0429STrond Myklebust  * @task: pointer to rpc_task
1227944b0429STrond Myklebust  *
1228944b0429STrond Myklebust  * Remove a task from the transmission queue
1229944b0429STrond Myklebust  */
1230944b0429STrond Myklebust static void
1231944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1232944b0429STrond Myklebust {
1233944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1234944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1235944b0429STrond Myklebust 
1236944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1237944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1238944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1239944b0429STrond Myklebust }
1240944b0429STrond Myklebust 
12417f3a1d1eSTrond Myklebust /**
12429d96acbcSTrond Myklebust  * xprt_request_prepare - prepare an encoded request for transport
12439d96acbcSTrond Myklebust  * @req: pointer to rpc_rqst
12449d96acbcSTrond Myklebust  *
12459d96acbcSTrond Myklebust  * Calls into the transport layer to do whatever is needed to prepare
12469d96acbcSTrond Myklebust  * the request for transmission or receive.
12479d96acbcSTrond Myklebust  */
12489d96acbcSTrond Myklebust void
12499d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req)
12509d96acbcSTrond Myklebust {
12519d96acbcSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12529d96acbcSTrond Myklebust 
12539d96acbcSTrond Myklebust 	if (xprt->ops->prepare_request)
12549d96acbcSTrond Myklebust 		xprt->ops->prepare_request(req);
12559d96acbcSTrond Myklebust }
12569d96acbcSTrond Myklebust 
12579d96acbcSTrond Myklebust /**
1258762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1259762e4e67STrond Myklebust  * @task: pointer to rpc_task
1260762e4e67STrond Myklebust  *
1261762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1262762e4e67STrond Myklebust  */
1263762e4e67STrond Myklebust bool
1264762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1265762e4e67STrond Myklebust {
1266762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1267762e4e67STrond Myklebust }
1268762e4e67STrond Myklebust 
1269762e4e67STrond Myklebust /**
12709903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
12719903cd1cSChuck Lever  * @task: RPC task about to send a request
12729903cd1cSChuck Lever  *
12731da177e4SLinus Torvalds  */
127490051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
12751da177e4SLinus Torvalds {
12761da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
12771da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
12781da177e4SLinus Torvalds 
127946121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
12801da177e4SLinus Torvalds 
12815f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
12825f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1283944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
12845f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
12855f2f6bd9STrond Myklebust 					task, 0);
12865f2f6bd9STrond Myklebust 		return false;
12875f2f6bd9STrond Myklebust 
12888a19a0b6STrond Myklebust 	}
12895f2f6bd9STrond Myklebust 	return true;
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds 
1292e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
12935e5ce5beSTrond Myklebust {
1294343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
12955e5ce5beSTrond Myklebust }
12965e5ce5beSTrond Myklebust 
12979903cd1cSChuck Lever /**
129889f90fe1STrond Myklebust  * xprt_request_transmit - send an RPC request on a transport
129989f90fe1STrond Myklebust  * @req: pointer to request to transmit
130089f90fe1STrond Myklebust  * @snd_task: RPC task that owns the transport lock
13019903cd1cSChuck Lever  *
130289f90fe1STrond Myklebust  * This performs the transmission of a single request.
130389f90fe1STrond Myklebust  * Note that if the request is not the same as snd_task, then it
130489f90fe1STrond Myklebust  * does need to be pinned.
130589f90fe1STrond Myklebust  * Returns '0' on success.
13069903cd1cSChuck Lever  */
130789f90fe1STrond Myklebust static int
130889f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
131189f90fe1STrond Myklebust 	struct rpc_task *task = req->rq_task;
131290d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1313dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1314ff699ea8SChuck Lever 	int status;
13151da177e4SLinus Torvalds 
131646121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
13171da177e4SLinus Torvalds 
1318edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
131989f90fe1STrond Myklebust 		if (xprt_request_data_received(task)) {
132089f90fe1STrond Myklebust 			status = 0;
1321944b0429STrond Myklebust 			goto out_dequeue;
132289f90fe1STrond Myklebust 		}
13233021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1324edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
132589f90fe1STrond Myklebust 			status = -EBADMSG;
1326944b0429STrond Myklebust 			goto out_dequeue;
13273021a5bbSTrond Myklebust 		}
13281da177e4SLinus Torvalds 	}
13291da177e4SLinus Torvalds 
1330dcbbeda8STrond Myklebust 	/*
1331dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1332dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1333dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1334dcbbeda8STrond Myklebust 	 */
1335dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1336dcbbeda8STrond Myklebust 
133790d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1338adfa7144STrond Myklebust 	status = xprt->ops->send_request(req);
13393705ad64SJeff Layton 	trace_xprt_transmit(xprt, req->rq_xid, status);
1340c8485e4dSTrond Myklebust 	if (status != 0) {
1341dcbbeda8STrond Myklebust 		req->rq_ntrans--;
134289f90fe1STrond Myklebust 		return status;
1343c8485e4dSTrond Myklebust 	}
13447ebbbc6eSTrond Myklebust 
1345dcbbeda8STrond Myklebust 	if (is_retrans)
1346dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1347dcbbeda8STrond Myklebust 
13484a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1349c8485e4dSTrond Myklebust 
135046121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
1351468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1352fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
1353262ca07dSChuck Lever 
1354262ca07dSChuck Lever 	xprt->stat.sends++;
1355262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1356262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
135715a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
135815a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1359fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
136090d91b0cSTrond Myklebust 
136190d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1362944b0429STrond Myklebust out_dequeue:
1363944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
136489f90fe1STrond Myklebust 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
136589f90fe1STrond Myklebust 	return status;
136689f90fe1STrond Myklebust }
136789f90fe1STrond Myklebust 
136889f90fe1STrond Myklebust /**
136989f90fe1STrond Myklebust  * xprt_transmit - send an RPC request on a transport
137089f90fe1STrond Myklebust  * @task: controlling RPC task
137189f90fe1STrond Myklebust  *
137289f90fe1STrond Myklebust  * Attempts to drain the transmit queue. On exit, either the transport
137389f90fe1STrond Myklebust  * signalled an error that needs to be handled before transmission can
137489f90fe1STrond Myklebust  * resume, or @task finished transmitting, and detected that it already
137589f90fe1STrond Myklebust  * received a reply.
137689f90fe1STrond Myklebust  */
137789f90fe1STrond Myklebust void
137889f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task)
137989f90fe1STrond Myklebust {
138089f90fe1STrond Myklebust 	struct rpc_rqst *next, *req = task->tk_rqstp;
138189f90fe1STrond Myklebust 	struct rpc_xprt	*xprt = req->rq_xprt;
138289f90fe1STrond Myklebust 	int status;
138389f90fe1STrond Myklebust 
138489f90fe1STrond Myklebust 	spin_lock(&xprt->queue_lock);
138589f90fe1STrond Myklebust 	while (!list_empty(&xprt->xmit_queue)) {
138689f90fe1STrond Myklebust 		next = list_first_entry(&xprt->xmit_queue,
138789f90fe1STrond Myklebust 				struct rpc_rqst, rq_xmit);
138889f90fe1STrond Myklebust 		xprt_pin_rqst(next);
138989f90fe1STrond Myklebust 		spin_unlock(&xprt->queue_lock);
139089f90fe1STrond Myklebust 		status = xprt_request_transmit(next, task);
139189f90fe1STrond Myklebust 		if (status == -EBADMSG && next != req)
139289f90fe1STrond Myklebust 			status = 0;
139389f90fe1STrond Myklebust 		cond_resched();
139489f90fe1STrond Myklebust 		spin_lock(&xprt->queue_lock);
139589f90fe1STrond Myklebust 		xprt_unpin_rqst(next);
139689f90fe1STrond Myklebust 		if (status == 0) {
139789f90fe1STrond Myklebust 			if (!xprt_request_data_received(task) ||
139889f90fe1STrond Myklebust 			    test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
139989f90fe1STrond Myklebust 				continue;
1400c544577dSTrond Myklebust 		} else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
140189f90fe1STrond Myklebust 			task->tk_status = status;
140289f90fe1STrond Myklebust 		break;
140389f90fe1STrond Myklebust 	}
140489f90fe1STrond Myklebust 	spin_unlock(&xprt->queue_lock);
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
1407ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1408ba60eb25STrond Myklebust {
1409ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1410ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1411ba60eb25STrond Myklebust }
1412ba60eb25STrond Myklebust 
1413ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1414ba60eb25STrond Myklebust {
1415ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1416ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1417ba60eb25STrond Myklebust }
1418ba60eb25STrond Myklebust 
1419ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1420ba60eb25STrond Myklebust {
1421ba60eb25STrond Myklebust 	bool ret = false;
1422ba60eb25STrond Myklebust 
1423ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1424ba60eb25STrond Myklebust 		goto out;
1425ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1426ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1427ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1428ba60eb25STrond Myklebust 		ret = true;
1429ba60eb25STrond Myklebust 	}
1430ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1431ba60eb25STrond Myklebust out:
1432ba60eb25STrond Myklebust 	return ret;
1433ba60eb25STrond Myklebust }
1434ba60eb25STrond Myklebust 
143592ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1436d9ba131dSTrond Myklebust {
1437d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1438d9ba131dSTrond Myklebust 
1439ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1440d9ba131dSTrond Myklebust 		goto out;
1441ff699ea8SChuck Lever 	++xprt->num_reqs;
144292ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
144392ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
144492ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1445d9ba131dSTrond Myklebust 	if (req != NULL)
1446d9ba131dSTrond Myklebust 		goto out;
1447ff699ea8SChuck Lever 	--xprt->num_reqs;
1448d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1449d9ba131dSTrond Myklebust out:
1450d9ba131dSTrond Myklebust 	return req;
1451d9ba131dSTrond Myklebust }
1452d9ba131dSTrond Myklebust 
1453d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1454d9ba131dSTrond Myklebust {
1455ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1456ff699ea8SChuck Lever 		--xprt->num_reqs;
1457d9ba131dSTrond Myklebust 		kfree(req);
1458d9ba131dSTrond Myklebust 		return true;
1459d9ba131dSTrond Myklebust 	}
1460d9ba131dSTrond Myklebust 	return false;
1461d9ba131dSTrond Myklebust }
1462d9ba131dSTrond Myklebust 
1463f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
14641da177e4SLinus Torvalds {
1465d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
14661da177e4SLinus Torvalds 
1467f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
14681da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1469d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1470d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1471d9ba131dSTrond Myklebust 		goto out_init_req;
1472d9ba131dSTrond Myklebust 	}
147392ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1474d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1475d9ba131dSTrond Myklebust 		goto out_init_req;
1476d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1477d9ba131dSTrond Myklebust 	case -ENOMEM:
1478d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1479d9ba131dSTrond Myklebust 				"failed! Retrying\n");
14801afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1481d9ba131dSTrond Myklebust 		break;
1482d9ba131dSTrond Myklebust 	case -EAGAIN:
1483ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1484d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1485e9d47639SGustavo A. R. Silva 		/* fall through */
14861afeaf5cSTrond Myklebust 	default:
1487d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
14881afeaf5cSTrond Myklebust 	}
1489f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1490d9ba131dSTrond Myklebust 	return;
1491d9ba131dSTrond Myklebust out_init_req:
1492ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1493ff699ea8SChuck Lever 				     xprt->num_reqs);
149437ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
149537ac86c3SChuck Lever 
1496d9ba131dSTrond Myklebust 	task->tk_status = 0;
14971da177e4SLinus Torvalds 	task->tk_rqstp = req;
14981da177e4SLinus Torvalds }
1499f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1500f39c1bfbSTrond Myklebust 
1501a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1502ee5ebe85STrond Myklebust {
1503ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1504c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1505c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1506ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1507c25573b5STrond Myklebust 	}
1508ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1509ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1510ee5ebe85STrond Myklebust }
1511a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1512ee5ebe85STrond Myklebust 
151321de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
151421de0a95STrond Myklebust {
151521de0a95STrond Myklebust 	struct rpc_rqst *req;
151621de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
151721de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
151821de0a95STrond Myklebust 		list_del(&req->rq_list);
151921de0a95STrond Myklebust 		kfree(req);
152021de0a95STrond Myklebust 	}
152121de0a95STrond Myklebust }
152221de0a95STrond Myklebust 
1523d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1524d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1525d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1526bd1722d4SPavel Emelyanov {
1527bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
152821de0a95STrond Myklebust 	struct rpc_rqst *req;
152921de0a95STrond Myklebust 	int i;
1530bd1722d4SPavel Emelyanov 
1531bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1532bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1533bd1722d4SPavel Emelyanov 		goto out;
1534bd1722d4SPavel Emelyanov 
153521de0a95STrond Myklebust 	xprt_init(xprt, net);
153621de0a95STrond Myklebust 
153721de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
153821de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
153921de0a95STrond Myklebust 		if (!req)
15408313164cSwangweidong 			goto out_free;
154121de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
154221de0a95STrond Myklebust 	}
1543d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1544d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1545d9ba131dSTrond Myklebust 	else
154621de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1547d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1548ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1549bd1722d4SPavel Emelyanov 
1550bd1722d4SPavel Emelyanov 	return xprt;
1551bd1722d4SPavel Emelyanov 
1552bd1722d4SPavel Emelyanov out_free:
155321de0a95STrond Myklebust 	xprt_free(xprt);
1554bd1722d4SPavel Emelyanov out:
1555bd1722d4SPavel Emelyanov 	return NULL;
1556bd1722d4SPavel Emelyanov }
1557bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1558bd1722d4SPavel Emelyanov 
1559e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1560e204e621SPavel Emelyanov {
156137aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
156221de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1563fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1564e204e621SPavel Emelyanov }
1565e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1566e204e621SPavel Emelyanov 
1567902c5887STrond Myklebust static void
1568902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1569902c5887STrond Myklebust {
1570902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1571902c5887STrond Myklebust }
1572902c5887STrond Myklebust 
15739dc6edcfSTrond Myklebust static __be32
15749dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
15759dc6edcfSTrond Myklebust {
15769dc6edcfSTrond Myklebust 	__be32 xid;
15779dc6edcfSTrond Myklebust 
15789dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
15799dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
15809dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
15819dc6edcfSTrond Myklebust 	return xid;
15829dc6edcfSTrond Myklebust }
15839dc6edcfSTrond Myklebust 
15849dc6edcfSTrond Myklebust static void
15859dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
15869dc6edcfSTrond Myklebust {
15879dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
15889dc6edcfSTrond Myklebust }
15899dc6edcfSTrond Myklebust 
15909dc6edcfSTrond Myklebust static void
15919dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
15929dc6edcfSTrond Myklebust {
15939dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
15949dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
15959dc6edcfSTrond Myklebust 
15969dc6edcfSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
15979dc6edcfSTrond Myklebust 	req->rq_task	= task;
15989dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
15999dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
16009dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1601902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
16029dc6edcfSTrond Myklebust 	req->rq_bytes_sent = 0;
16039dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
16049dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
16059dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
16069dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
160771700bb9STrond Myklebust 	req->rq_snd_buf.bvec = NULL;
160871700bb9STrond Myklebust 	req->rq_rcv_buf.bvec = NULL;
16099dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
16109dc6edcfSTrond Myklebust 	xprt_reset_majortimeo(req);
16119dc6edcfSTrond Myklebust 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
16129dc6edcfSTrond Myklebust 			req, ntohl(req->rq_xid));
16139dc6edcfSTrond Myklebust }
16149dc6edcfSTrond Myklebust 
16159dc6edcfSTrond Myklebust static void
16169dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
16179dc6edcfSTrond Myklebust {
16189dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
16199dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
16209dc6edcfSTrond Myklebust 		xprt_request_init(task);
16219dc6edcfSTrond Myklebust }
16229dc6edcfSTrond Myklebust 
16239903cd1cSChuck Lever /**
16249903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
16259903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
16269903cd1cSChuck Lever  *
1627ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1628ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
16299903cd1cSChuck Lever  * backlog queue.
16309903cd1cSChuck Lever  */
16319903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
16321da177e4SLinus Torvalds {
1633fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
16341da177e4SLinus Torvalds 
163543cedbf0STrond Myklebust 	task->tk_status = 0;
163643cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
163743cedbf0STrond Myklebust 		return;
163843cedbf0STrond Myklebust 
163943cedbf0STrond Myklebust 	task->tk_timeout = 0;
164043cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1641ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
16429dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1643ba60eb25STrond Myklebust }
1644ba60eb25STrond Myklebust 
1645ba60eb25STrond Myklebust /**
1646ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1647ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1648ba60eb25STrond Myklebust  *
1649ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1650ba60eb25STrond Myklebust  * backlog queue.
1651ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1652ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1653ba60eb25STrond Myklebust  */
1654ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1655ba60eb25STrond Myklebust {
1656fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1657ba60eb25STrond Myklebust 
1658ba60eb25STrond Myklebust 	task->tk_status = 0;
1659ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1660ba60eb25STrond Myklebust 		return;
1661ba60eb25STrond Myklebust 
1662ba60eb25STrond Myklebust 	task->tk_timeout = 0;
1663ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
16649dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
1667edc81dcdSTrond Myklebust static void
1668edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1669edc81dcdSTrond Myklebust {
1670edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1671edc81dcdSTrond Myklebust 
1672944b0429STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1673944b0429STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1674edc81dcdSTrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1675edc81dcdSTrond Myklebust 		spin_lock(&xprt->queue_lock);
1676944b0429STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1677edc81dcdSTrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1678edc81dcdSTrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1679edc81dcdSTrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1680edc81dcdSTrond Myklebust 			spin_unlock(&xprt->queue_lock);
1681edc81dcdSTrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1682edc81dcdSTrond Myklebust 			spin_lock(&xprt->queue_lock);
1683edc81dcdSTrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1684edc81dcdSTrond Myklebust 		}
1685edc81dcdSTrond Myklebust 		spin_unlock(&xprt->queue_lock);
1686edc81dcdSTrond Myklebust 	}
1687edc81dcdSTrond Myklebust }
1688edc81dcdSTrond Myklebust 
16899903cd1cSChuck Lever /**
16909903cd1cSChuck Lever  * xprt_release - release an RPC request slot
16919903cd1cSChuck Lever  * @task: task which is finished with the slot
16929903cd1cSChuck Lever  *
16931da177e4SLinus Torvalds  */
16949903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
16951da177e4SLinus Torvalds {
169655ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
169787ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
16981da177e4SLinus Torvalds 
169987ed5003STrond Myklebust 	if (req == NULL) {
170087ed5003STrond Myklebust 		if (task->tk_client) {
1701fb43d172STrond Myklebust 			xprt = task->tk_xprt;
170287ed5003STrond Myklebust 			xprt_release_write(xprt, task);
170387ed5003STrond Myklebust 		}
17041da177e4SLinus Torvalds 		return;
170587ed5003STrond Myklebust 	}
170655ae1aabSRicardo Labiaga 
170755ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
17080a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
17090a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
17100a702195SWeston Andros Adamson 	else if (task->tk_client)
17110a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
1712edc81dcdSTrond Myklebust 	xprt_request_dequeue_all(task, req);
17134a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
171449e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1715a58dd398SChuck Lever 	if (xprt->ops->release_request)
1716a58dd398SChuck Lever 		xprt->ops->release_request(task);
17171da177e4SLinus Torvalds 	xprt->last_used = jiffies;
1718ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
17194a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1720ee5ebe85STrond Myklebust 	if (req->rq_buffer)
17213435c74aSChuck Lever 		xprt->ops->buf_free(task);
17224a068258SChuck Lever 	xprt_inject_disconnect(xprt);
17239d96acbcSTrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
1724a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1725a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
17261da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1727ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1728ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
172955ae1aabSRicardo Labiaga 
173046121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1731ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1732a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1733ee5ebe85STrond Myklebust 	else
1734c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
1737902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1738902c5887STrond Myklebust void
1739902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1740902c5887STrond Myklebust {
1741902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1742902c5887STrond Myklebust 
1743902c5887STrond Myklebust 	task->tk_rqstp = req;
1744902c5887STrond Myklebust 	req->rq_task = task;
1745902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1746902c5887STrond Myklebust 	/*
1747902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1748902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1749902c5887STrond Myklebust 	 */
1750902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1751902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1752902c5887STrond Myklebust 	req->rq_bytes_sent = 0;
1753902c5887STrond Myklebust }
1754902c5887STrond Myklebust #endif
1755902c5887STrond Myklebust 
175621de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1757c2866763SChuck Lever {
175830c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1759c2866763SChuck Lever 
1760c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1761c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
176275c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1763c2866763SChuck Lever 
1764c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
176595f7691dSTrond Myklebust 	xprt->recv_queue = RB_ROOT;
1766944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
17679e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1768f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1769f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
17709e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
177180b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1772f9acac1aSRicardo Labiaga 
1773c2866763SChuck Lever 	xprt->last_used = jiffies;
1774c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1775a509050bSChuck Lever 	xprt->bind_index = 0;
1776c2866763SChuck Lever 
1777c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1778c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
177979c99152STrond Myklebust 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1780c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1781c2866763SChuck Lever 
1782c2866763SChuck Lever 	xprt_init_xid(xprt);
1783c2866763SChuck Lever 
178421de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
17858d9266ffSTrond Myklebust }
17868d9266ffSTrond Myklebust 
17878d9266ffSTrond Myklebust /**
17888d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
17898d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
17908d9266ffSTrond Myklebust  *
17918d9266ffSTrond Myklebust  */
17928d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
17938d9266ffSTrond Myklebust {
17948d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
17958d9266ffSTrond Myklebust 	struct xprt_class *t;
17968d9266ffSTrond Myklebust 
17978d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
17988d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
17998d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
18008d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
18018d9266ffSTrond Myklebust 			goto found;
18028d9266ffSTrond Myklebust 		}
18038d9266ffSTrond Myklebust 	}
18048d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
18053c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
18068d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
18078d9266ffSTrond Myklebust 
18088d9266ffSTrond Myklebust found:
18098d9266ffSTrond Myklebust 	xprt = t->setup(args);
18108d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
18118d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
18128d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
181321de0a95STrond Myklebust 		goto out;
18148d9266ffSTrond Myklebust 	}
181533d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
181633d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
181721de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
181821de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
1819ff861c4dSKees Cook 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
182021de0a95STrond Myklebust 	else
1821ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
18224e0038b6STrond Myklebust 
18234e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
18244e0038b6STrond Myklebust 		xprt_destroy(xprt);
18254e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
18264e0038b6STrond Myklebust 	}
18274e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
18284e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
18294e0038b6STrond Myklebust 		xprt_destroy(xprt);
18304e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
18314e0038b6STrond Myklebust 	}
18324e0038b6STrond Myklebust 
18333f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1834388f0c77SJeff Layton 
1835c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1836c2866763SChuck Lever 			xprt->max_reqs);
183721de0a95STrond Myklebust out:
1838c2866763SChuck Lever 	return xprt;
1839c2866763SChuck Lever }
1840c2866763SChuck Lever 
1841528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
1842528fd354STrond Myklebust {
1843528fd354STrond Myklebust 	struct rpc_xprt *xprt =
1844528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
1845528fd354STrond Myklebust 
1846528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
1847528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1848528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1849528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1850528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1851528fd354STrond Myklebust 	kfree(xprt->servername);
1852528fd354STrond Myklebust 	/*
1853528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
1854528fd354STrond Myklebust 	 */
1855528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
1856528fd354STrond Myklebust }
1857528fd354STrond Myklebust 
18589903cd1cSChuck Lever /**
18599903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1860a8de240aSTrond Myklebust  * @xprt: transport to destroy
18619903cd1cSChuck Lever  *
18621da177e4SLinus Torvalds  */
1863a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
186679234c3dSTrond Myklebust 
1867528fd354STrond Myklebust 	/*
1868528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
1869528fd354STrond Myklebust 	 */
187079234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
187179234c3dSTrond Myklebust 
18720065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1873c8541ecdSChuck Lever 
1874c8541ecdSChuck Lever 	/*
1875528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
1876528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
1877c8541ecdSChuck Lever 	 */
1878528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1879528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
18806b6ca86bSTrond Myklebust }
18811da177e4SLinus Torvalds 
188230c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
188330c5116bSTrond Myklebust {
188430c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
188530c5116bSTrond Myklebust }
188630c5116bSTrond Myklebust 
188730c5116bSTrond Myklebust /**
188830c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
188930c5116bSTrond Myklebust  * @xprt: pointer to the transport
189030c5116bSTrond Myklebust  *
189130c5116bSTrond Myklebust  */
189230c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
189330c5116bSTrond Myklebust {
189430c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
189530c5116bSTrond Myklebust 		return xprt;
189630c5116bSTrond Myklebust 	return NULL;
189730c5116bSTrond Myklebust }
189830c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
189930c5116bSTrond Myklebust 
19006b6ca86bSTrond Myklebust /**
19016b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
19026b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
19036b6ca86bSTrond Myklebust  *
19046b6ca86bSTrond Myklebust  */
19056b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
19066b6ca86bSTrond Myklebust {
190730c5116bSTrond Myklebust 	if (xprt != NULL)
190830c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
19096b6ca86bSTrond Myklebust }
19105d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
1911