xref: /openbmc/linux/net/sunrpc/xprt.c (revision 1fc5f131)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/net/sunrpc/xprt.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This is a generic RPC call interface supporting congestion avoidance,
61da177e4SLinus Torvalds  *  and asynchronous calls.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  The interface works like this:
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  -	When a process places a call, it allocates a request slot if
111da177e4SLinus Torvalds  *	one is available. Otherwise, it sleeps on the backlog queue
121da177e4SLinus Torvalds  *	(xprt_reserve).
131da177e4SLinus Torvalds  *  -	Next, the caller puts together the RPC message, stuffs it into
1455aa4f58SChuck Lever  *	the request struct, and calls xprt_transmit().
1555aa4f58SChuck Lever  *  -	xprt_transmit sends the message and installs the caller on the
1655ae1aabSRicardo Labiaga  *	transport's wait list. At the same time, if a reply is expected,
1755ae1aabSRicardo Labiaga  *	it installs a timer that is run after the packet's timeout has
1855ae1aabSRicardo Labiaga  *	expired.
191da177e4SLinus Torvalds  *  -	When a packet arrives, the data_ready handler walks the list of
2055aa4f58SChuck Lever  *	pending requests for that transport. If a matching XID is found, the
211da177e4SLinus Torvalds  *	caller is woken up, and the timer removed.
221da177e4SLinus Torvalds  *  -	When no reply arrives within the timeout interval, the timer is
231da177e4SLinus Torvalds  *	fired by the kernel and runs xprt_timer(). It either adjusts the
241da177e4SLinus Torvalds  *	timeout values (minor timeout) or wakes up the caller with a status
251da177e4SLinus Torvalds  *	of -ETIMEDOUT.
261da177e4SLinus Torvalds  *  -	When the caller receives a notification from RPC that a reply arrived,
271da177e4SLinus Torvalds  *	it should release the RPC slot, and process the reply.
281da177e4SLinus Torvalds  *	If the call timed out, it may choose to retry the operation by
291da177e4SLinus Torvalds  *	adjusting the initial timeout value, and simply calling rpc_call
301da177e4SLinus Torvalds  *	again.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  *  Support for async RPC is done through a set of RPC-specific scheduling
331da177e4SLinus Torvalds  *  primitives that `transparently' work for processes as well as async
341da177e4SLinus Torvalds  *  tasks that rely on callbacks.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
3755aa4f58SChuck Lever  *
3855aa4f58SChuck Lever  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
41a246b010SChuck Lever #include <linux/module.h>
42a246b010SChuck Lever 
431da177e4SLinus Torvalds #include <linux/types.h>
44a246b010SChuck Lever #include <linux/interrupt.h>
451da177e4SLinus Torvalds #include <linux/workqueue.h>
46bf3fcf89SChuck Lever #include <linux/net.h>
47ff839970SChuck Lever #include <linux/ktime.h>
481da177e4SLinus Torvalds 
49a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
5011c556b3SChuck Lever #include <linux/sunrpc/metrics.h>
51c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
52fda1bfefSTrond Myklebust #include <linux/rcupdate.h>
53a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
541da177e4SLinus Torvalds 
553705ad64SJeff Layton #include <trace/events/sunrpc.h>
563705ad64SJeff Layton 
5755ae1aabSRicardo Labiaga #include "sunrpc.h"
5855ae1aabSRicardo Labiaga 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * Local variables
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds 
63f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
641da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Local functions
691da177e4SLinus Torvalds  */
7021de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
7137ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
724e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
731da177e4SLinus Torvalds 
745ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7581c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7681c098afS\"Talpey, Thomas\ 
779e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
789e910bffSTrond Myklebust {
799e910bffSTrond Myklebust 	unsigned long timeout = jiffies + req->rq_timeout;
809e910bffSTrond Myklebust 
819e910bffSTrond Myklebust 	if (time_before(timeout, req->rq_majortimeo))
829e910bffSTrond Myklebust 		return timeout;
839e910bffSTrond Myklebust 	return req->rq_majortimeo;
849e910bffSTrond Myklebust }
859e910bffSTrond Myklebust 
8612a80469SChuck Lever /**
8781c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
8881c098afS\"Talpey, Thomas\  * @transport: transport to register
8981c098afS\"Talpey, Thomas\  *
9081c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9181c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9281c098afS\"Talpey, Thomas\  *
9381c098afS\"Talpey, Thomas\  * Returns:
9481c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
9581c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
9681c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
9781c098afS\"Talpey, Thomas\  */
9881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
9981c098afS\"Talpey, Thomas\ {
10081c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10181c098afS\"Talpey, Thomas\ 	int result;
10281c098afS\"Talpey, Thomas\ 
10381c098afS\"Talpey, Thomas\ 	result = -EEXIST;
10481c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
10581c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
10681c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1074fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
10881c098afS\"Talpey, Thomas\ 			goto out;
10981c098afS\"Talpey, Thomas\ 	}
11081c098afS\"Talpey, Thomas\ 
11181c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11281c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11381c098afS\"Talpey, Thomas\ 	       transport->name);
11481c098afS\"Talpey, Thomas\ 	result = 0;
11581c098afS\"Talpey, Thomas\ 
11681c098afS\"Talpey, Thomas\ out:
11781c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
11881c098afS\"Talpey, Thomas\ 	return result;
11981c098afS\"Talpey, Thomas\ }
12081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12181c098afS\"Talpey, Thomas\ 
12281c098afS\"Talpey, Thomas\ /**
12381c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
12465b6e42cSRandy Dunlap  * @transport: transport to unregister
12581c098afS\"Talpey, Thomas\  *
12681c098afS\"Talpey, Thomas\  * Returns:
12781c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
12881c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
12981c098afS\"Talpey, Thomas\  */
13081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13181c098afS\"Talpey, Thomas\ {
13281c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13381c098afS\"Talpey, Thomas\ 	int result;
13481c098afS\"Talpey, Thomas\ 
13581c098afS\"Talpey, Thomas\ 	result = 0;
13681c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
13781c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
13881c098afS\"Talpey, Thomas\ 		if (t == transport) {
13981c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
14081c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14181c098afS\"Talpey, Thomas\ 				transport->name);
14281c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14381c098afS\"Talpey, Thomas\ 			goto out;
14481c098afS\"Talpey, Thomas\ 		}
14581c098afS\"Talpey, Thomas\ 	}
14681c098afS\"Talpey, Thomas\ 	result = -ENOENT;
14781c098afS\"Talpey, Thomas\ 
14881c098afS\"Talpey, Thomas\ out:
14981c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
15081c098afS\"Talpey, Thomas\ 	return result;
15181c098afS\"Talpey, Thomas\ }
15281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15381c098afS\"Talpey, Thomas\ 
154d5aa6b22STrond Myklebust static void
155d5aa6b22STrond Myklebust xprt_class_release(const struct xprt_class *t)
156d5aa6b22STrond Myklebust {
157d5aa6b22STrond Myklebust 	module_put(t->owner);
158d5aa6b22STrond Myklebust }
159d5aa6b22STrond Myklebust 
160d5aa6b22STrond Myklebust static const struct xprt_class *
1619bccd264STrond Myklebust xprt_class_find_by_ident_locked(int ident)
1629bccd264STrond Myklebust {
1639bccd264STrond Myklebust 	const struct xprt_class *t;
1649bccd264STrond Myklebust 
1659bccd264STrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
1669bccd264STrond Myklebust 		if (t->ident != ident)
1679bccd264STrond Myklebust 			continue;
1689bccd264STrond Myklebust 		if (!try_module_get(t->owner))
1699bccd264STrond Myklebust 			continue;
1709bccd264STrond Myklebust 		return t;
1719bccd264STrond Myklebust 	}
1729bccd264STrond Myklebust 	return NULL;
1739bccd264STrond Myklebust }
1749bccd264STrond Myklebust 
1759bccd264STrond Myklebust static const struct xprt_class *
1769bccd264STrond Myklebust xprt_class_find_by_ident(int ident)
1779bccd264STrond Myklebust {
1789bccd264STrond Myklebust 	const struct xprt_class *t;
1799bccd264STrond Myklebust 
1809bccd264STrond Myklebust 	spin_lock(&xprt_list_lock);
1819bccd264STrond Myklebust 	t = xprt_class_find_by_ident_locked(ident);
1829bccd264STrond Myklebust 	spin_unlock(&xprt_list_lock);
1839bccd264STrond Myklebust 	return t;
1849bccd264STrond Myklebust }
1859bccd264STrond Myklebust 
1869bccd264STrond Myklebust static const struct xprt_class *
187d5aa6b22STrond Myklebust xprt_class_find_by_netid_locked(const char *netid)
188d5aa6b22STrond Myklebust {
189d5aa6b22STrond Myklebust 	const struct xprt_class *t;
190d5aa6b22STrond Myklebust 	unsigned int i;
191d5aa6b22STrond Myklebust 
192d5aa6b22STrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
193d5aa6b22STrond Myklebust 		for (i = 0; t->netid[i][0] != '\0'; i++) {
194d5aa6b22STrond Myklebust 			if (strcmp(t->netid[i], netid) != 0)
195d5aa6b22STrond Myklebust 				continue;
196d5aa6b22STrond Myklebust 			if (!try_module_get(t->owner))
197d5aa6b22STrond Myklebust 				continue;
198d5aa6b22STrond Myklebust 			return t;
199d5aa6b22STrond Myklebust 		}
200d5aa6b22STrond Myklebust 	}
201d5aa6b22STrond Myklebust 	return NULL;
202d5aa6b22STrond Myklebust }
203d5aa6b22STrond Myklebust 
204d5aa6b22STrond Myklebust static const struct xprt_class *
205d5aa6b22STrond Myklebust xprt_class_find_by_netid(const char *netid)
206d5aa6b22STrond Myklebust {
207d5aa6b22STrond Myklebust 	const struct xprt_class *t;
208d5aa6b22STrond Myklebust 
209d5aa6b22STrond Myklebust 	spin_lock(&xprt_list_lock);
210d5aa6b22STrond Myklebust 	t = xprt_class_find_by_netid_locked(netid);
211d5aa6b22STrond Myklebust 	if (!t) {
212d5aa6b22STrond Myklebust 		spin_unlock(&xprt_list_lock);
213d5aa6b22STrond Myklebust 		request_module("rpc%s", netid);
214d5aa6b22STrond Myklebust 		spin_lock(&xprt_list_lock);
215d5aa6b22STrond Myklebust 		t = xprt_class_find_by_netid_locked(netid);
216d5aa6b22STrond Myklebust 	}
217d5aa6b22STrond Myklebust 	spin_unlock(&xprt_list_lock);
218d5aa6b22STrond Myklebust 	return t;
219d5aa6b22STrond Myklebust }
220d5aa6b22STrond Myklebust 
22181c098afS\"Talpey, Thomas\ /**
222*1fc5f131STrond Myklebust  * xprt_find_transport_ident - convert a netid into a transport identifier
223*1fc5f131STrond Myklebust  * @netid: transport to load
224*1fc5f131STrond Myklebust  *
225*1fc5f131STrond Myklebust  * Returns:
226*1fc5f131STrond Myklebust  * > 0:		transport identifier
227*1fc5f131STrond Myklebust  * -ENOENT:	transport module not available
228*1fc5f131STrond Myklebust  */
229*1fc5f131STrond Myklebust int xprt_find_transport_ident(const char *netid)
230*1fc5f131STrond Myklebust {
231*1fc5f131STrond Myklebust 	const struct xprt_class *t;
232*1fc5f131STrond Myklebust 	int ret;
233*1fc5f131STrond Myklebust 
234*1fc5f131STrond Myklebust 	t = xprt_class_find_by_netid(netid);
235*1fc5f131STrond Myklebust 	if (!t)
236*1fc5f131STrond Myklebust 		return -ENOENT;
237*1fc5f131STrond Myklebust 	ret = t->ident;
238*1fc5f131STrond Myklebust 	xprt_class_release(t);
239*1fc5f131STrond Myklebust 	return ret;
240*1fc5f131STrond Myklebust }
241*1fc5f131STrond Myklebust EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
242*1fc5f131STrond Myklebust 
243*1fc5f131STrond Myklebust /**
244441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
245d5aa6b22STrond Myklebust  * @netid: transport to load
246441e3e24STom Talpey  *
247441e3e24STom Talpey  * Returns:
248441e3e24STom Talpey  * 0:		transport successfully loaded
249441e3e24STom Talpey  * -ENOENT:	transport module not available
250441e3e24STom Talpey  */
251d5aa6b22STrond Myklebust int xprt_load_transport(const char *netid)
252441e3e24STom Talpey {
253*1fc5f131STrond Myklebust 	int ret = xprt_find_transport_ident(netid);
254*1fc5f131STrond Myklebust 	return ret < 0 ? ret : 0;
255441e3e24STom Talpey }
256441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
257441e3e24STom Talpey 
258c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
259c544577dSTrond Myklebust {
260c544577dSTrond Myklebust 	xprt->snd_task = NULL;
261c544577dSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
262c544577dSTrond Myklebust 		smp_mb__before_atomic();
263c544577dSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
264c544577dSTrond Myklebust 		smp_mb__after_atomic();
265c544577dSTrond Myklebust 	} else
266c544577dSTrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
267c544577dSTrond Myklebust }
268c544577dSTrond Myklebust 
269441e3e24STom Talpey /**
27012a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
27112a80469SChuck Lever  * @task: task that is requesting access to the transport
272177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
27312a80469SChuck Lever  *
27412a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
27512a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
27612a80469SChuck Lever  * is provided.
2771da177e4SLinus Torvalds  */
27843cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
2791da177e4SLinus Torvalds {
28012a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
28112a80469SChuck Lever 
28212a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
28312a80469SChuck Lever 		if (task == xprt->snd_task)
284bf7ca707SChuck Lever 			goto out_locked;
28512a80469SChuck Lever 		goto out_sleep;
28612a80469SChuck Lever 	}
287c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
288c544577dSTrond Myklebust 		goto out_unlock;
28912a80469SChuck Lever 	xprt->snd_task = task;
2904d4a76f3Sj223yang@asset.uwaterloo.ca 
291bf7ca707SChuck Lever out_locked:
292bf7ca707SChuck Lever 	trace_xprt_reserve_xprt(xprt, task);
29312a80469SChuck Lever 	return 1;
29412a80469SChuck Lever 
295c544577dSTrond Myklebust out_unlock:
296c544577dSTrond Myklebust 	xprt_clear_locked(xprt);
29712a80469SChuck Lever out_sleep:
29812a80469SChuck Lever 	task->tk_status = -EAGAIN;
2996b2e6856STrond Myklebust 	if  (RPC_IS_SOFT(task))
3006b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
3019e910bffSTrond Myklebust 				xprt_request_timeout(req));
3026b2e6856STrond Myklebust 	else
30379c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
30412a80469SChuck Lever 	return 0;
30512a80469SChuck Lever }
30612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
30712a80469SChuck Lever 
30875891f50STrond Myklebust static bool
30975891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
31075891f50STrond Myklebust {
31175891f50STrond Myklebust 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
31275891f50STrond Myklebust }
31375891f50STrond Myklebust 
31475891f50STrond Myklebust static void
31575891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
31675891f50STrond Myklebust {
31775891f50STrond Myklebust 	if (!list_empty(&xprt->xmit_queue)) {
31875891f50STrond Myklebust 		/* Peek at head of queue to see if it can make progress */
31975891f50STrond Myklebust 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
32075891f50STrond Myklebust 					rq_xmit)->rq_cong)
32175891f50STrond Myklebust 			return;
32275891f50STrond Myklebust 	}
32375891f50STrond Myklebust 	set_bit(XPRT_CWND_WAIT, &xprt->state);
32475891f50STrond Myklebust }
32575891f50STrond Myklebust 
32675891f50STrond Myklebust static void
32775891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
32875891f50STrond Myklebust {
32975891f50STrond Myklebust 	if (!RPCXPRT_CONGESTED(xprt))
33075891f50STrond Myklebust 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
33175891f50STrond Myklebust }
33275891f50STrond Myklebust 
33312a80469SChuck Lever /*
33412a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
33512a80469SChuck Lever  * @task: task that is requesting access to the transport
33612a80469SChuck Lever  *
33712a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
33812a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
33912a80469SChuck Lever  * woken up and given access to the transport.
34075891f50STrond Myklebust  * Note that the lock is only granted if we know there are free slots.
34112a80469SChuck Lever  */
34243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
34312a80469SChuck Lever {
3441da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3451da177e4SLinus Torvalds 
3462226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
3471da177e4SLinus Torvalds 		if (task == xprt->snd_task)
348bf7ca707SChuck Lever 			goto out_locked;
3491da177e4SLinus Torvalds 		goto out_sleep;
3501da177e4SLinus Torvalds 	}
35143cedbf0STrond Myklebust 	if (req == NULL) {
35243cedbf0STrond Myklebust 		xprt->snd_task = task;
353bf7ca707SChuck Lever 		goto out_locked;
35443cedbf0STrond Myklebust 	}
355c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
356c544577dSTrond Myklebust 		goto out_unlock;
35775891f50STrond Myklebust 	if (!xprt_need_congestion_window_wait(xprt)) {
3581da177e4SLinus Torvalds 		xprt->snd_task = task;
359bf7ca707SChuck Lever 		goto out_locked;
3601da177e4SLinus Torvalds 	}
361c544577dSTrond Myklebust out_unlock:
362632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3631da177e4SLinus Torvalds out_sleep:
3641da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
3656b2e6856STrond Myklebust 	if (RPC_IS_SOFT(task))
3666b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
3679e910bffSTrond Myklebust 				xprt_request_timeout(req));
3686b2e6856STrond Myklebust 	else
36979c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
3701da177e4SLinus Torvalds 	return 0;
371bf7ca707SChuck Lever out_locked:
372bf7ca707SChuck Lever 	trace_xprt_reserve_cong(xprt, task);
373bf7ca707SChuck Lever 	return 1;
3741da177e4SLinus Torvalds }
37512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
3761da177e4SLinus Torvalds 
37712a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	int retval;
3801da177e4SLinus Torvalds 
381bd79bc57STrond Myklebust 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
382bd79bc57STrond Myklebust 		return 1;
383b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
38443cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
385b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
3861da177e4SLinus Torvalds 	return retval;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
3901da177e4SLinus Torvalds {
391961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
39249e9a890SChuck Lever 
39349e9a890SChuck Lever 	xprt->snd_task = task;
394961a828dSTrond Myklebust 	return true;
395961a828dSTrond Myklebust }
396961a828dSTrond Myklebust 
397961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
398961a828dSTrond Myklebust {
399961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
40049e9a890SChuck Lever 		return;
401c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
402c544577dSTrond Myklebust 		goto out_unlock;
403f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
404f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
405961a828dSTrond Myklebust 		return;
406c544577dSTrond Myklebust out_unlock:
407632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
40849e9a890SChuck Lever }
40949e9a890SChuck Lever 
410961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
411961a828dSTrond Myklebust {
412961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
413961a828dSTrond Myklebust 		return;
414c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
415c544577dSTrond Myklebust 		goto out_unlock;
41675891f50STrond Myklebust 	if (xprt_need_congestion_window_wait(xprt))
417961a828dSTrond Myklebust 		goto out_unlock;
418f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
41975891f50STrond Myklebust 				__xprt_lock_write_func, xprt))
420961a828dSTrond Myklebust 		return;
4211da177e4SLinus Torvalds out_unlock:
422632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
42549e9a890SChuck Lever /**
42649e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
42749e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
42849e9a890SChuck Lever  * @task: task that is releasing access to the transport
42949e9a890SChuck Lever  *
43049e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
4311da177e4SLinus Torvalds  */
43249e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
435632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
4361da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
4371da177e4SLinus Torvalds 	}
438bf7ca707SChuck Lever 	trace_xprt_release_xprt(xprt, task);
4391da177e4SLinus Torvalds }
44012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
4411da177e4SLinus Torvalds 
44249e9a890SChuck Lever /**
44349e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
44449e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
44549e9a890SChuck Lever  * @task: task that is releasing access to the transport
44649e9a890SChuck Lever  *
44749e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
44849e9a890SChuck Lever  * transport if the transport's congestion window allows it.
44949e9a890SChuck Lever  */
45049e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
45149e9a890SChuck Lever {
45249e9a890SChuck Lever 	if (xprt->snd_task == task) {
453632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
45449e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
45549e9a890SChuck Lever 	}
456bf7ca707SChuck Lever 	trace_xprt_release_cong(xprt, task);
45749e9a890SChuck Lever }
45812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
45949e9a890SChuck Lever 
46049e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
4611da177e4SLinus Torvalds {
462bd79bc57STrond Myklebust 	if (xprt->snd_task != task)
463bd79bc57STrond Myklebust 		return;
464b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
46549e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
466b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
4711da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds static int
47475891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	if (req->rq_cong)
4771da177e4SLinus Torvalds 		return 1;
478bf7ca707SChuck Lever 	trace_xprt_get_cong(xprt, req->rq_task);
47975891f50STrond Myklebust 	if (RPCXPRT_CONGESTED(xprt)) {
48075891f50STrond Myklebust 		xprt_set_congestion_window_wait(xprt);
4811da177e4SLinus Torvalds 		return 0;
48275891f50STrond Myklebust 	}
4831da177e4SLinus Torvalds 	req->rq_cong = 1;
4841da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4851da177e4SLinus Torvalds 	return 1;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /*
4891da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4901da177e4SLinus Torvalds  * that has been sleeping due to congestion
4911da177e4SLinus Torvalds  */
4921da177e4SLinus Torvalds static void
4931da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	if (!req->rq_cong)
4961da177e4SLinus Torvalds 		return;
4971da177e4SLinus Torvalds 	req->rq_cong = 0;
4981da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
49975891f50STrond Myklebust 	xprt_test_and_clear_congestion_window_wait(xprt);
500bf7ca707SChuck Lever 	trace_xprt_put_cong(xprt, req->rq_task);
50149e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
50446c0ee8bSChuck Lever /**
50575891f50STrond Myklebust  * xprt_request_get_cong - Request congestion control credits
50675891f50STrond Myklebust  * @xprt: pointer to transport
50775891f50STrond Myklebust  * @req: pointer to RPC request
50875891f50STrond Myklebust  *
50975891f50STrond Myklebust  * Useful for transports that require congestion control.
51075891f50STrond Myklebust  */
51175891f50STrond Myklebust bool
51275891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
51375891f50STrond Myklebust {
51475891f50STrond Myklebust 	bool ret = false;
51575891f50STrond Myklebust 
51675891f50STrond Myklebust 	if (req->rq_cong)
51775891f50STrond Myklebust 		return true;
518b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
51975891f50STrond Myklebust 	ret = __xprt_get_cong(xprt, req) != 0;
520b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
52175891f50STrond Myklebust 	return ret;
52275891f50STrond Myklebust }
52375891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong);
52475891f50STrond Myklebust 
52575891f50STrond Myklebust /**
526a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
527a58dd398SChuck Lever  * @task: RPC request that recently completed
528a58dd398SChuck Lever  *
529a58dd398SChuck Lever  * Useful for transports that require congestion control.
530a58dd398SChuck Lever  */
531a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
532a58dd398SChuck Lever {
533a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
534a4f0835cSTrond Myklebust 
535a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
536a58dd398SChuck Lever }
53712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
538a58dd398SChuck Lever 
5398593e010SChuck Lever static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
5408593e010SChuck Lever {
5418593e010SChuck Lever 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
5428593e010SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5438593e010SChuck Lever }
5448593e010SChuck Lever 
54575891f50STrond Myklebust /*
54675891f50STrond Myklebust  * Clear the congestion window wait flag and wake up the next
54775891f50STrond Myklebust  * entry on xprt->sending
54875891f50STrond Myklebust  */
54975891f50STrond Myklebust static void
55075891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
55175891f50STrond Myklebust {
55275891f50STrond Myklebust 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
553b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
55475891f50STrond Myklebust 		__xprt_lock_write_next_cong(xprt);
555b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
55675891f50STrond Myklebust 	}
55775891f50STrond Myklebust }
55875891f50STrond Myklebust 
559a58dd398SChuck Lever /**
56046c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
5616a24dfb6STrond Myklebust  * @xprt: pointer to xprt
56246c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
56346c0ee8bSChuck Lever  * @result: result code of completed RPC request
56446c0ee8bSChuck Lever  *
5654f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
5664f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
5674f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
5684f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
5694f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
5704f4cf5adSChuck Lever  *
5714f4cf5adSChuck Lever  *	-	a reply is received and
5724f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
5734f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
5741da177e4SLinus Torvalds  */
5756a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
5761da177e4SLinus Torvalds {
57746c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
57846c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
5811da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
5821da177e4SLinus Torvalds 		 * the result gets rounded properly. */
5831da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
5841da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
5851da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
58649e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5871da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
5881da177e4SLinus Torvalds 		cwnd >>= 1;
5891da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
5901da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
5931da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
5941da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
59546c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
5961da177e4SLinus Torvalds }
59712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
5981da177e4SLinus Torvalds 
59944fbac22SChuck Lever /**
60044fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
60144fbac22SChuck Lever  * @xprt: transport with waiting tasks
60244fbac22SChuck Lever  * @status: result code to plant in each task before waking it
60344fbac22SChuck Lever  *
60444fbac22SChuck Lever  */
60544fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
60644fbac22SChuck Lever {
60744fbac22SChuck Lever 	if (status < 0)
60844fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
60944fbac22SChuck Lever 	else
61044fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
61144fbac22SChuck Lever }
61212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
61344fbac22SChuck Lever 
614c7b2cae8SChuck Lever /**
615c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
616c544577dSTrond Myklebust  * @xprt: transport
617a9a6b52eSTrond Myklebust  *
618a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
619a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
620a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
621c7b2cae8SChuck Lever  */
622c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
623c7b2cae8SChuck Lever {
624c544577dSTrond Myklebust 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
625c7b2cae8SChuck Lever }
62612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
627c7b2cae8SChuck Lever 
628c544577dSTrond Myklebust static bool
629c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt)
630c544577dSTrond Myklebust {
631c544577dSTrond Myklebust 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
632c544577dSTrond Myklebust 		__xprt_lock_write_next(xprt);
633c544577dSTrond Myklebust 		dprintk("RPC:       write space: waking waiting task on "
634c544577dSTrond Myklebust 				"xprt %p\n", xprt);
635c544577dSTrond Myklebust 		return true;
636c544577dSTrond Myklebust 	}
637c544577dSTrond Myklebust 	return false;
638c544577dSTrond Myklebust }
639c544577dSTrond Myklebust 
640c7b2cae8SChuck Lever /**
641c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
642c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
643c7b2cae8SChuck Lever  *
644c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
645c7b2cae8SChuck Lever  */
646c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt)
647c7b2cae8SChuck Lever {
648c544577dSTrond Myklebust 	bool ret;
649c544577dSTrond Myklebust 
650c544577dSTrond Myklebust 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
651c544577dSTrond Myklebust 		return false;
652b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
653c544577dSTrond Myklebust 	ret = xprt_clear_write_space_locked(xprt);
654b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
655c544577dSTrond Myklebust 	return ret;
656c7b2cae8SChuck Lever }
65712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
658c7b2cae8SChuck Lever 
659da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
660da953063STrond Myklebust {
661da953063STrond Myklebust 	s64 delta = ktime_to_ns(ktime_get() - abstime);
662da953063STrond Myklebust 	return likely(delta >= 0) ?
663da953063STrond Myklebust 		jiffies - nsecs_to_jiffies(delta) :
664da953063STrond Myklebust 		jiffies + nsecs_to_jiffies(-delta);
665da953063STrond Myklebust }
666da953063STrond Myklebust 
667da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
6681da177e4SLinus Torvalds {
669ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
670da953063STrond Myklebust 	unsigned long majortimeo = req->rq_timeout;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if (to->to_exponential)
673da953063STrond Myklebust 		majortimeo <<= to->to_retries;
6741da177e4SLinus Torvalds 	else
675da953063STrond Myklebust 		majortimeo += to->to_increment * to->to_retries;
676da953063STrond Myklebust 	if (majortimeo > to->to_maxval || majortimeo == 0)
677da953063STrond Myklebust 		majortimeo = to->to_maxval;
678da953063STrond Myklebust 	return majortimeo;
679da953063STrond Myklebust }
680da953063STrond Myklebust 
681da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req)
682da953063STrond Myklebust {
683da953063STrond Myklebust 	req->rq_majortimeo += xprt_calc_majortimeo(req);
684da953063STrond Myklebust }
685da953063STrond Myklebust 
6867de62bc0SOlga Kornievskaia static void xprt_reset_minortimeo(struct rpc_rqst *req)
6877de62bc0SOlga Kornievskaia {
6887de62bc0SOlga Kornievskaia 	req->rq_minortimeo += req->rq_timeout;
6897de62bc0SOlga Kornievskaia }
6907de62bc0SOlga Kornievskaia 
691da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
692da953063STrond Myklebust {
693da953063STrond Myklebust 	unsigned long time_init;
694da953063STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
695da953063STrond Myklebust 
696da953063STrond Myklebust 	if (likely(xprt && xprt_connected(xprt)))
697da953063STrond Myklebust 		time_init = jiffies;
698da953063STrond Myklebust 	else
699da953063STrond Myklebust 		time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
700da953063STrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
701da953063STrond Myklebust 	req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
7027de62bc0SOlga Kornievskaia 	req->rq_minortimeo = time_init + req->rq_timeout;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7059903cd1cSChuck Lever /**
7069903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
7079903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
7089903cd1cSChuck Lever  *
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
713ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
7141da177e4SLinus Torvalds 	int status = 0;
7151da177e4SLinus Torvalds 
7167de62bc0SOlga Kornievskaia 	if (time_before(jiffies, req->rq_minortimeo))
7177de62bc0SOlga Kornievskaia 		return status;
7181da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
7191da177e4SLinus Torvalds 		if (to->to_exponential)
7201da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
7211da177e4SLinus Torvalds 		else
7221da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
7231da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
7241da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
7251da177e4SLinus Torvalds 		req->rq_retries++;
7261da177e4SLinus Torvalds 	} else {
7271da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
7281da177e4SLinus Torvalds 		req->rq_retries = 0;
7291da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
7301da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
731b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
7321da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
733b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
7341da177e4SLinus Torvalds 		status = -ETIMEDOUT;
7351da177e4SLinus Torvalds 	}
7367de62bc0SOlga Kornievskaia 	xprt_reset_minortimeo(req);
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
7391da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
7401da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 	return status;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
74565f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
7461da177e4SLinus Torvalds {
74765f27f38SDavid Howells 	struct rpc_xprt *xprt =
74865f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
749a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
7501da177e4SLinus Torvalds 
751911813d7SChuck Lever 	trace_xprt_disconnect_auto(xprt);
75266af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7534876cc77STrond Myklebust 	xprt->ops->close(xprt);
7541da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
75579234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
756a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7599903cd1cSChuck Lever /**
76062da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
7619903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
7629903cd1cSChuck Lever  *
7631da177e4SLinus Torvalds  */
76462da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
7651da177e4SLinus Torvalds {
766911813d7SChuck Lever 	trace_xprt_disconnect_done(xprt);
767b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
7681da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
769c544577dSTrond Myklebust 	xprt_clear_write_space_locked(xprt);
7708593e010SChuck Lever 	xprt_clear_congestion_window_wait_locked(xprt);
77127adc785STrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
772b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
7731da177e4SLinus Torvalds }
77462da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
7751da177e4SLinus Torvalds 
77666af1e55STrond Myklebust /**
77766af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
77866af1e55STrond Myklebust  * @xprt: transport to disconnect
77966af1e55STrond Myklebust  *
78066af1e55STrond Myklebust  */
78166af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
78266af1e55STrond Myklebust {
783911813d7SChuck Lever 	trace_xprt_disconnect_force(xprt);
784911813d7SChuck Lever 
78566af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
786b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
78766af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
78866af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
78966af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
79040a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7910445f92cSTrond Myklebust 	else if (xprt->snd_task)
7920445f92cSTrond Myklebust 		rpc_wake_up_queued_task_set_status(&xprt->pending,
7930445f92cSTrond Myklebust 				xprt->snd_task, -ENOTCONN);
794b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
79566af1e55STrond Myklebust }
796e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
79766af1e55STrond Myklebust 
7987f3a1d1eSTrond Myklebust static unsigned int
7997f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
8007f3a1d1eSTrond Myklebust {
8017f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
8027f3a1d1eSTrond Myklebust }
8037f3a1d1eSTrond Myklebust 
8047f3a1d1eSTrond Myklebust static bool
8057f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
8067f3a1d1eSTrond Myklebust {
8077f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
8087f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
8097f3a1d1eSTrond Myklebust 
8107f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
8117f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
8127f3a1d1eSTrond Myklebust }
8137f3a1d1eSTrond Myklebust 
8147c1d71cfSTrond Myklebust /**
8157c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
8167c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
8177c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
8187c1d71cfSTrond Myklebust  *
8197c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
8207c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
8217c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
8227c1d71cfSTrond Myklebust  * a batch of RPC requests.
8237c1d71cfSTrond Myklebust  *
8247c1d71cfSTrond Myklebust  */
8257c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
8267c1d71cfSTrond Myklebust {
8277c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
828b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
8297c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
8307c1d71cfSTrond Myklebust 		goto out;
8312c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
8327c1d71cfSTrond Myklebust 		goto out;
8337c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
8347c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
8357c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
83640a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
8372a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
8387c1d71cfSTrond Myklebust out:
839b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
8407c1d71cfSTrond Myklebust }
8417c1d71cfSTrond Myklebust 
842ad3331acSTrond Myklebust static bool
843ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
844ad3331acSTrond Myklebust {
845ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
846ad3331acSTrond Myklebust }
847ad3331acSTrond Myklebust 
848ad3331acSTrond Myklebust static void
849ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
850ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
851ad3331acSTrond Myklebust {
85280d3c45fSDave Wysochanski 	xprt->last_used = jiffies;
85395f7691dSTrond Myklebust 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
854ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
855ad3331acSTrond Myklebust }
856ad3331acSTrond Myklebust 
8571da177e4SLinus Torvalds static void
858ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
8591da177e4SLinus Torvalds {
860ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
8611da177e4SLinus Torvalds 
86295f7691dSTrond Myklebust 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
863b5e92419STrond Myklebust 		return;
864ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
865ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
8662226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
8671da177e4SLinus Torvalds 		return;
868b5e92419STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
871718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
872718ba5b8STrond Myklebust 		struct rpc_task *task,
873718ba5b8STrond Myklebust 		void *cookie)
874718ba5b8STrond Myklebust {
875718ba5b8STrond Myklebust 	bool ret = false;
876718ba5b8STrond Myklebust 
877b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
878718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
879718ba5b8STrond Myklebust 		goto out;
880718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
881718ba5b8STrond Myklebust 		goto out;
882718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
883718ba5b8STrond Myklebust 	ret = true;
884718ba5b8STrond Myklebust out:
885b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
886718ba5b8STrond Myklebust 	return ret;
887718ba5b8STrond Myklebust }
888718ba5b8STrond Myklebust 
889718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
890718ba5b8STrond Myklebust {
891b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
892718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
893718ba5b8STrond Myklebust 		goto out;
894718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
895718ba5b8STrond Myklebust 		goto out;
896718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
897718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
898ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
899718ba5b8STrond Myklebust out:
900b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
90179234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
902718ba5b8STrond Myklebust }
903718ba5b8STrond Myklebust 
9049903cd1cSChuck Lever /**
9059903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
9069903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
9071da177e4SLinus Torvalds  *
9081da177e4SLinus Torvalds  */
9091da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
9101da177e4SLinus Torvalds {
911ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
9121da177e4SLinus Torvalds 
913db0a86c4SChuck Lever 	trace_xprt_connect(xprt);
9141da177e4SLinus Torvalds 
915ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
91601d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
9171da177e4SLinus Torvalds 		return;
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
9201da177e4SLinus Torvalds 		return;
921feb8ca37STrond Myklebust 
922911813d7SChuck Lever 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
923911813d7SChuck Lever 		trace_xprt_disconnect_cleanup(xprt);
924feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
925911813d7SChuck Lever 	}
926feb8ca37STrond Myklebust 
927718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
9282c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
9296b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
9309e910bffSTrond Myklebust 				xprt_request_timeout(task->tk_rqstp));
9310b9e7943STrond Myklebust 
9320b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
9330b9e7943STrond Myklebust 			return;
9340b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
9350b9e7943STrond Myklebust 			return;
9360a9a4304STrond Myklebust 		/* Race breaker */
9370a9a4304STrond Myklebust 		if (!xprt_connected(xprt)) {
938262ca07dSChuck Lever 			xprt->stat.connect_start = jiffies;
9391b092092STrond Myklebust 			xprt->ops->connect(xprt, task);
9400a9a4304STrond Myklebust 		} else {
9410a9a4304STrond Myklebust 			xprt_clear_connecting(xprt);
9420a9a4304STrond Myklebust 			task->tk_status = 0;
9430a9a4304STrond Myklebust 			rpc_wake_up_queued_task(&xprt->pending, task);
9440a9a4304STrond Myklebust 		}
9451da177e4SLinus Torvalds 	}
946718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
949675dd90aSChuck Lever /**
950675dd90aSChuck Lever  * xprt_reconnect_delay - compute the wait before scheduling a connect
951675dd90aSChuck Lever  * @xprt: transport instance
952675dd90aSChuck Lever  *
953675dd90aSChuck Lever  */
954675dd90aSChuck Lever unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
955675dd90aSChuck Lever {
956675dd90aSChuck Lever 	unsigned long start, now = jiffies;
957675dd90aSChuck Lever 
958675dd90aSChuck Lever 	start = xprt->stat.connect_start + xprt->reestablish_timeout;
959675dd90aSChuck Lever 	if (time_after(start, now))
960675dd90aSChuck Lever 		return start - now;
961675dd90aSChuck Lever 	return 0;
962675dd90aSChuck Lever }
963675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
964675dd90aSChuck Lever 
965675dd90aSChuck Lever /**
966675dd90aSChuck Lever  * xprt_reconnect_backoff - compute the new re-establish timeout
967675dd90aSChuck Lever  * @xprt: transport instance
968675dd90aSChuck Lever  * @init_to: initial reestablish timeout
969675dd90aSChuck Lever  *
970675dd90aSChuck Lever  */
971675dd90aSChuck Lever void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
972675dd90aSChuck Lever {
973675dd90aSChuck Lever 	xprt->reestablish_timeout <<= 1;
974675dd90aSChuck Lever 	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
975675dd90aSChuck Lever 		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
976675dd90aSChuck Lever 	if (xprt->reestablish_timeout < init_to)
977675dd90aSChuck Lever 		xprt->reestablish_timeout = init_to;
978675dd90aSChuck Lever }
979675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
980675dd90aSChuck Lever 
98195f7691dSTrond Myklebust enum xprt_xid_rb_cmp {
98295f7691dSTrond Myklebust 	XID_RB_EQUAL,
98395f7691dSTrond Myklebust 	XID_RB_LEFT,
98495f7691dSTrond Myklebust 	XID_RB_RIGHT,
98595f7691dSTrond Myklebust };
98695f7691dSTrond Myklebust static enum xprt_xid_rb_cmp
98795f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2)
98895f7691dSTrond Myklebust {
98995f7691dSTrond Myklebust 	if (xid1 == xid2)
99095f7691dSTrond Myklebust 		return XID_RB_EQUAL;
99195f7691dSTrond Myklebust 	if ((__force u32)xid1 < (__force u32)xid2)
99295f7691dSTrond Myklebust 		return XID_RB_LEFT;
99395f7691dSTrond Myklebust 	return XID_RB_RIGHT;
99495f7691dSTrond Myklebust }
99595f7691dSTrond Myklebust 
99695f7691dSTrond Myklebust static struct rpc_rqst *
99795f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
99895f7691dSTrond Myklebust {
99995f7691dSTrond Myklebust 	struct rb_node *n = xprt->recv_queue.rb_node;
100095f7691dSTrond Myklebust 	struct rpc_rqst *req;
100195f7691dSTrond Myklebust 
100295f7691dSTrond Myklebust 	while (n != NULL) {
100395f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
100495f7691dSTrond Myklebust 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
100595f7691dSTrond Myklebust 		case XID_RB_LEFT:
100695f7691dSTrond Myklebust 			n = n->rb_left;
100795f7691dSTrond Myklebust 			break;
100895f7691dSTrond Myklebust 		case XID_RB_RIGHT:
100995f7691dSTrond Myklebust 			n = n->rb_right;
101095f7691dSTrond Myklebust 			break;
101195f7691dSTrond Myklebust 		case XID_RB_EQUAL:
101295f7691dSTrond Myklebust 			return req;
101395f7691dSTrond Myklebust 		}
101495f7691dSTrond Myklebust 	}
101595f7691dSTrond Myklebust 	return NULL;
101695f7691dSTrond Myklebust }
101795f7691dSTrond Myklebust 
101895f7691dSTrond Myklebust static void
101995f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
102095f7691dSTrond Myklebust {
102195f7691dSTrond Myklebust 	struct rb_node **p = &xprt->recv_queue.rb_node;
102295f7691dSTrond Myklebust 	struct rb_node *n = NULL;
102395f7691dSTrond Myklebust 	struct rpc_rqst *req;
102495f7691dSTrond Myklebust 
102595f7691dSTrond Myklebust 	while (*p != NULL) {
102695f7691dSTrond Myklebust 		n = *p;
102795f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
102895f7691dSTrond Myklebust 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
102995f7691dSTrond Myklebust 		case XID_RB_LEFT:
103095f7691dSTrond Myklebust 			p = &n->rb_left;
103195f7691dSTrond Myklebust 			break;
103295f7691dSTrond Myklebust 		case XID_RB_RIGHT:
103395f7691dSTrond Myklebust 			p = &n->rb_right;
103495f7691dSTrond Myklebust 			break;
103595f7691dSTrond Myklebust 		case XID_RB_EQUAL:
103695f7691dSTrond Myklebust 			WARN_ON_ONCE(new != req);
103795f7691dSTrond Myklebust 			return;
103895f7691dSTrond Myklebust 		}
103995f7691dSTrond Myklebust 	}
104095f7691dSTrond Myklebust 	rb_link_node(&new->rq_recv, n, p);
104195f7691dSTrond Myklebust 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
104295f7691dSTrond Myklebust }
104395f7691dSTrond Myklebust 
104495f7691dSTrond Myklebust static void
104595f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
104695f7691dSTrond Myklebust {
104795f7691dSTrond Myklebust 	rb_erase(&req->rq_recv, &xprt->recv_queue);
104895f7691dSTrond Myklebust }
104995f7691dSTrond Myklebust 
10509903cd1cSChuck Lever /**
10519903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
10529903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
10539903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
10549903cd1cSChuck Lever  *
105575c84151STrond Myklebust  * Caller holds xprt->queue_lock.
10561da177e4SLinus Torvalds  */
1057d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
10581da177e4SLinus Torvalds {
10598f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
10601da177e4SLinus Torvalds 
106195f7691dSTrond Myklebust 	entry = xprt_request_rb_find(xprt, xid);
106295f7691dSTrond Myklebust 	if (entry != NULL) {
10633705ad64SJeff Layton 		trace_xprt_lookup_rqst(xprt, xid, 0);
10640b87a46bSChuck Lever 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1065262ca07dSChuck Lever 		return entry;
10663705ad64SJeff Layton 	}
106746121cf7SChuck Lever 
106846121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
106946121cf7SChuck Lever 			ntohl(xid));
10703705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
1071262ca07dSChuck Lever 	xprt->stat.bad_xids++;
1072262ca07dSChuck Lever 	return NULL;
10731da177e4SLinus Torvalds }
107412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
10751da177e4SLinus Torvalds 
1076cf9946cdSTrond Myklebust static bool
1077cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
1078cf9946cdSTrond Myklebust {
1079cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
1080cf9946cdSTrond Myklebust }
1081cf9946cdSTrond Myklebust 
1082729749bbSTrond Myklebust /**
1083729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
1084729749bbSTrond Myklebust  * @req: Request to pin
1085729749bbSTrond Myklebust  *
1086729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
10871f7d1c73SChuck Lever  * so should be holding xprt->queue_lock.
1088729749bbSTrond Myklebust  */
1089729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
1090729749bbSTrond Myklebust {
1091cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
1092729749bbSTrond Myklebust }
10939590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
1094729749bbSTrond Myklebust 
1095729749bbSTrond Myklebust /**
1096729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
1097729749bbSTrond Myklebust  * @req: Request to pin
1098729749bbSTrond Myklebust  *
10991f7d1c73SChuck Lever  * Caller should be holding xprt->queue_lock.
1100729749bbSTrond Myklebust  */
1101729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
1102729749bbSTrond Myklebust {
1103cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1104cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
1105cf9946cdSTrond Myklebust 		return;
1106cf9946cdSTrond Myklebust 	}
1107cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
1108cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
1109729749bbSTrond Myklebust }
11109590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1111729749bbSTrond Myklebust 
1112729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
1113729749bbSTrond Myklebust {
1114cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1115729749bbSTrond Myklebust }
1116729749bbSTrond Myklebust 
1117edc81dcdSTrond Myklebust static bool
1118edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
1119edc81dcdSTrond Myklebust {
1120edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1121edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1122edc81dcdSTrond Myklebust }
1123edc81dcdSTrond Myklebust 
1124edc81dcdSTrond Myklebust static bool
1125edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1126edc81dcdSTrond Myklebust {
1127edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1128edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1129edc81dcdSTrond Myklebust }
1130edc81dcdSTrond Myklebust 
1131edc81dcdSTrond Myklebust /**
1132edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
1133edc81dcdSTrond Myklebust  * @task: RPC task
1134edc81dcdSTrond Myklebust  *
1135edc81dcdSTrond Myklebust  */
1136edc81dcdSTrond Myklebust void
1137edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
1138edc81dcdSTrond Myklebust {
1139edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1140edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1141edc81dcdSTrond Myklebust 
1142edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
1143edc81dcdSTrond Myklebust 		return;
114475369089STrond Myklebust 
114575369089STrond Myklebust 	xprt_request_prepare(task->tk_rqstp);
1146edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
1147edc81dcdSTrond Myklebust 
1148edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
1149edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1150edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
1151edc81dcdSTrond Myklebust 
1152edc81dcdSTrond Myklebust 	/* Add request to the receive list */
115395f7691dSTrond Myklebust 	xprt_request_rb_insert(xprt, req);
1154edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1155edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
1156edc81dcdSTrond Myklebust 
1157edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
1158edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
1159edc81dcdSTrond Myklebust }
1160edc81dcdSTrond Myklebust 
1161edc81dcdSTrond Myklebust /**
1162edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1163edc81dcdSTrond Myklebust  * @task: RPC task
1164edc81dcdSTrond Myklebust  *
1165edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
1166edc81dcdSTrond Myklebust  */
1167edc81dcdSTrond Myklebust static void
1168edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
1169edc81dcdSTrond Myklebust {
117095f7691dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
117195f7691dSTrond Myklebust 
1172edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
117395f7691dSTrond Myklebust 		xprt_request_rb_remove(req->rq_xprt, req);
1174edc81dcdSTrond Myklebust }
1175edc81dcdSTrond Myklebust 
1176ecd465eeSChuck Lever /**
1177ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
1178ecd465eeSChuck Lever  * @task: RPC request that recently completed
1179ecd465eeSChuck Lever  *
118075c84151STrond Myklebust  * Caller holds xprt->queue_lock.
1181ecd465eeSChuck Lever  */
1182ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
11831da177e4SLinus Torvalds {
11841570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
11851570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
118695c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1187d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
11881570c1e4SChuck Lever 
11891da177e4SLinus Torvalds 	if (timer) {
11901da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
1191ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
11921570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
11931da177e4SLinus Torvalds 	}
11941da177e4SLinus Torvalds }
1195ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
11961da177e4SLinus Torvalds 
11971570c1e4SChuck Lever /**
11981570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
11991570c1e4SChuck Lever  * @task: RPC request that recently completed
12001570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
12011570c1e4SChuck Lever  *
120275c84151STrond Myklebust  * Caller holds xprt->queue_lock.
12031570c1e4SChuck Lever  */
12041570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
12051570c1e4SChuck Lever {
12061570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
1207fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12081da177e4SLinus Torvalds 
1209fda13939STrond Myklebust 	xprt->stat.recvs++;
1210ef759a2eSChuck Lever 
12111e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1212dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1213dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
121443ac3f29STrond Myklebust 	smp_wmb();
1215dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1216edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1217fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
12181da177e4SLinus Torvalds }
121912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
12201da177e4SLinus Torvalds 
122146c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
12241da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
12251da177e4SLinus Torvalds 
12265d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
12275d00837bSTrond Myklebust 		return;
122846c0ee8bSChuck Lever 
122982476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1230dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
123146c0ee8bSChuck Lever 		if (xprt->ops->timer)
12326a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
12335d00837bSTrond Myklebust 	} else
12345d00837bSTrond Myklebust 		task->tk_status = 0;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
12379903cd1cSChuck Lever /**
12388ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_def - wait for reply
12398ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
12408ba6a92dSTrond Myklebust  *
12418ba6a92dSTrond Myklebust  * Set a request's retransmit timeout based on the transport's
12428ba6a92dSTrond Myklebust  * default timeout parameters.  Used by transports that don't adjust
12438ba6a92dSTrond Myklebust  * the retransmit timeout based on round-trip time estimation,
12448ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
12458ba6a92dSTrond Myklebust  */
12468ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task)
12478ba6a92dSTrond Myklebust {
12488ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12498ba6a92dSTrond Myklebust 
12506b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12519e910bffSTrond Myklebust 			xprt_request_timeout(req));
12528ba6a92dSTrond Myklebust }
12538ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
12548ba6a92dSTrond Myklebust 
12558ba6a92dSTrond Myklebust /**
12568ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
12578ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
12588ba6a92dSTrond Myklebust  *
12598ba6a92dSTrond Myklebust  * Set a request's retransmit timeout using the RTT estimator,
12608ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
12618ba6a92dSTrond Myklebust  */
12628ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
12638ba6a92dSTrond Myklebust {
12648ba6a92dSTrond Myklebust 	int timer = task->tk_msg.rpc_proc->p_timer;
12658ba6a92dSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
12668ba6a92dSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
12678ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12688ba6a92dSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
12696b2e6856STrond Myklebust 	unsigned long timeout;
12708ba6a92dSTrond Myklebust 
12716b2e6856STrond Myklebust 	timeout = rpc_calc_rto(rtt, timer);
12726b2e6856STrond Myklebust 	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
12736b2e6856STrond Myklebust 	if (timeout > max_timeout || timeout == 0)
12746b2e6856STrond Myklebust 		timeout = max_timeout;
12756b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12766b2e6856STrond Myklebust 			jiffies + timeout);
12778ba6a92dSTrond Myklebust }
12788ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
12798ba6a92dSTrond Myklebust 
12808ba6a92dSTrond Myklebust /**
12817f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
12827f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
12837f3a1d1eSTrond Myklebust  *
12847f3a1d1eSTrond Myklebust  */
12857f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
12867f3a1d1eSTrond Myklebust {
12877f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12887f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12897f3a1d1eSTrond Myklebust 
12907f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
12917f3a1d1eSTrond Myklebust 		return;
12927f3a1d1eSTrond Myklebust 	/*
12937f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
12947f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
12957f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
12967f3a1d1eSTrond Myklebust 	 */
12977f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
12987f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
12998ba6a92dSTrond Myklebust 		xprt->ops->wait_for_reply_request(task);
13007f3a1d1eSTrond Myklebust 		/*
13017f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
13027f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
13037f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
13047f3a1d1eSTrond Myklebust 		 */
13057f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
13067f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
13077f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
13087f3a1d1eSTrond Myklebust 	}
13097f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
13107f3a1d1eSTrond Myklebust }
13117f3a1d1eSTrond Myklebust 
1312944b0429STrond Myklebust static bool
1313944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1314944b0429STrond Myklebust {
1315762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1316944b0429STrond Myklebust }
1317944b0429STrond Myklebust 
1318944b0429STrond Myklebust /**
1319944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1320944b0429STrond Myklebust  * @task: pointer to rpc_task
1321944b0429STrond Myklebust  *
1322944b0429STrond Myklebust  * Add a task to the transmission queue.
1323944b0429STrond Myklebust  */
1324944b0429STrond Myklebust void
1325944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1326944b0429STrond Myklebust {
1327918f3c1fSTrond Myklebust 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1328944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1329944b0429STrond Myklebust 
1330944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1331e66721f0STrond Myklebust 		req->rq_bytes_sent = 0;
1332944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
133375891f50STrond Myklebust 		/*
133475891f50STrond Myklebust 		 * Requests that carry congestion control credits are added
133575891f50STrond Myklebust 		 * to the head of the list to avoid starvation issues.
133675891f50STrond Myklebust 		 */
133775891f50STrond Myklebust 		if (req->rq_cong) {
133875891f50STrond Myklebust 			xprt_clear_congestion_window_wait(xprt);
133975891f50STrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
134075891f50STrond Myklebust 				if (pos->rq_cong)
134175891f50STrond Myklebust 					continue;
134275891f50STrond Myklebust 				/* Note: req is added _before_ pos */
134375891f50STrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
134475891f50STrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
134575891f50STrond Myklebust 				goto out;
134675891f50STrond Myklebust 			}
134786aeee0eSTrond Myklebust 		} else if (RPC_IS_SWAPPER(task)) {
134886aeee0eSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
134986aeee0eSTrond Myklebust 				if (pos->rq_cong || pos->rq_bytes_sent)
135086aeee0eSTrond Myklebust 					continue;
135186aeee0eSTrond Myklebust 				if (RPC_IS_SWAPPER(pos->rq_task))
135286aeee0eSTrond Myklebust 					continue;
135386aeee0eSTrond Myklebust 				/* Note: req is added _before_ pos */
135486aeee0eSTrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
135586aeee0eSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
135686aeee0eSTrond Myklebust 				goto out;
135786aeee0eSTrond Myklebust 			}
1358deaa5c96SChuck Lever 		} else if (!req->rq_seqno) {
1359918f3c1fSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1360918f3c1fSTrond Myklebust 				if (pos->rq_task->tk_owner != task->tk_owner)
1361918f3c1fSTrond Myklebust 					continue;
1362918f3c1fSTrond Myklebust 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1363918f3c1fSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit);
1364918f3c1fSTrond Myklebust 				goto out;
1365918f3c1fSTrond Myklebust 			}
136675891f50STrond Myklebust 		}
1367944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1368918f3c1fSTrond Myklebust 		INIT_LIST_HEAD(&req->rq_xmit2);
1369918f3c1fSTrond Myklebust out:
1370944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1371944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1372944b0429STrond Myklebust 	}
1373944b0429STrond Myklebust }
1374944b0429STrond Myklebust 
1375944b0429STrond Myklebust /**
1376944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1377944b0429STrond Myklebust  * @task: pointer to rpc_task
1378944b0429STrond Myklebust  *
1379944b0429STrond Myklebust  * Remove a task from the transmission queue
1380944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1381944b0429STrond Myklebust  */
1382944b0429STrond Myklebust static void
1383944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1384944b0429STrond Myklebust {
1385918f3c1fSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1386918f3c1fSTrond Myklebust 
1387918f3c1fSTrond Myklebust 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1388918f3c1fSTrond Myklebust 		return;
1389918f3c1fSTrond Myklebust 	if (!list_empty(&req->rq_xmit)) {
1390918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit);
1391918f3c1fSTrond Myklebust 		if (!list_empty(&req->rq_xmit2)) {
1392918f3c1fSTrond Myklebust 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1393918f3c1fSTrond Myklebust 					struct rpc_rqst, rq_xmit2);
1394918f3c1fSTrond Myklebust 			list_del(&req->rq_xmit2);
1395918f3c1fSTrond Myklebust 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1396918f3c1fSTrond Myklebust 		}
1397918f3c1fSTrond Myklebust 	} else
1398918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit2);
1399944b0429STrond Myklebust }
1400944b0429STrond Myklebust 
1401944b0429STrond Myklebust /**
1402944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1403944b0429STrond Myklebust  * @task: pointer to rpc_task
1404944b0429STrond Myklebust  *
1405944b0429STrond Myklebust  * Remove a task from the transmission queue
1406944b0429STrond Myklebust  */
1407944b0429STrond Myklebust static void
1408944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1409944b0429STrond Myklebust {
1410944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1411944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1412944b0429STrond Myklebust 
1413944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1414944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1415944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1416944b0429STrond Myklebust }
1417944b0429STrond Myklebust 
14187f3a1d1eSTrond Myklebust /**
1419cc204d01STrond Myklebust  * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1420cc204d01STrond Myklebust  * @task: pointer to rpc_task
1421cc204d01STrond Myklebust  *
1422cc204d01STrond Myklebust  * Remove a task from the transmit and receive queues, and ensure that
1423cc204d01STrond Myklebust  * it is not pinned by the receive work item.
1424cc204d01STrond Myklebust  */
1425cc204d01STrond Myklebust void
1426cc204d01STrond Myklebust xprt_request_dequeue_xprt(struct rpc_task *task)
1427cc204d01STrond Myklebust {
1428cc204d01STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
1429cc204d01STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1430cc204d01STrond Myklebust 
1431cc204d01STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1432cc204d01STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1433cc204d01STrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1434cc204d01STrond Myklebust 		spin_lock(&xprt->queue_lock);
1435cc204d01STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1436cc204d01STrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1437cc204d01STrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1438cc204d01STrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1439cc204d01STrond Myklebust 			spin_unlock(&xprt->queue_lock);
1440cc204d01STrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1441cc204d01STrond Myklebust 			spin_lock(&xprt->queue_lock);
1442cc204d01STrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1443cc204d01STrond Myklebust 		}
1444cc204d01STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1445cc204d01STrond Myklebust 	}
1446cc204d01STrond Myklebust }
1447cc204d01STrond Myklebust 
1448cc204d01STrond Myklebust /**
14499d96acbcSTrond Myklebust  * xprt_request_prepare - prepare an encoded request for transport
14509d96acbcSTrond Myklebust  * @req: pointer to rpc_rqst
14519d96acbcSTrond Myklebust  *
14529d96acbcSTrond Myklebust  * Calls into the transport layer to do whatever is needed to prepare
14539d96acbcSTrond Myklebust  * the request for transmission or receive.
14549d96acbcSTrond Myklebust  */
14559d96acbcSTrond Myklebust void
14569d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req)
14579d96acbcSTrond Myklebust {
14589d96acbcSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
14599d96acbcSTrond Myklebust 
14609d96acbcSTrond Myklebust 	if (xprt->ops->prepare_request)
14619d96acbcSTrond Myklebust 		xprt->ops->prepare_request(req);
14629d96acbcSTrond Myklebust }
14639d96acbcSTrond Myklebust 
14649d96acbcSTrond Myklebust /**
1465762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1466762e4e67STrond Myklebust  * @task: pointer to rpc_task
1467762e4e67STrond Myklebust  *
1468762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1469762e4e67STrond Myklebust  */
1470762e4e67STrond Myklebust bool
1471762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1472762e4e67STrond Myklebust {
1473762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1474762e4e67STrond Myklebust }
1475762e4e67STrond Myklebust 
1476762e4e67STrond Myklebust /**
14779903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
14789903cd1cSChuck Lever  * @task: RPC task about to send a request
14799903cd1cSChuck Lever  *
14801da177e4SLinus Torvalds  */
148190051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
14821da177e4SLinus Torvalds {
14831da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
14841da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
14851da177e4SLinus Torvalds 
14865f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
14879ce07ae5SChuck Lever 		trace_xprt_transmit_queued(xprt, task);
14889ce07ae5SChuck Lever 
14895f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1490944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
14915f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
14925f2f6bd9STrond Myklebust 					task, 0);
14935f2f6bd9STrond Myklebust 		return false;
14945f2f6bd9STrond Myklebust 
14958a19a0b6STrond Myklebust 	}
14965f2f6bd9STrond Myklebust 	return true;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
1499e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
15005e5ce5beSTrond Myklebust {
1501343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
15025e5ce5beSTrond Myklebust }
15035e5ce5beSTrond Myklebust 
15049903cd1cSChuck Lever /**
150589f90fe1STrond Myklebust  * xprt_request_transmit - send an RPC request on a transport
150689f90fe1STrond Myklebust  * @req: pointer to request to transmit
150789f90fe1STrond Myklebust  * @snd_task: RPC task that owns the transport lock
15089903cd1cSChuck Lever  *
150989f90fe1STrond Myklebust  * This performs the transmission of a single request.
151089f90fe1STrond Myklebust  * Note that if the request is not the same as snd_task, then it
151189f90fe1STrond Myklebust  * does need to be pinned.
151289f90fe1STrond Myklebust  * Returns '0' on success.
15139903cd1cSChuck Lever  */
151489f90fe1STrond Myklebust static int
151589f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
151889f90fe1STrond Myklebust 	struct rpc_task *task = req->rq_task;
151990d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1520dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1521ff699ea8SChuck Lever 	int status;
15221da177e4SLinus Torvalds 
1523edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
152489f90fe1STrond Myklebust 		if (xprt_request_data_received(task)) {
152589f90fe1STrond Myklebust 			status = 0;
1526944b0429STrond Myklebust 			goto out_dequeue;
152789f90fe1STrond Myklebust 		}
15283021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1529edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
153089f90fe1STrond Myklebust 			status = -EBADMSG;
1531944b0429STrond Myklebust 			goto out_dequeue;
15323021a5bbSTrond Myklebust 		}
1533ae67bd38STrond Myklebust 		if (RPC_SIGNALLED(task)) {
1534ae67bd38STrond Myklebust 			status = -ERESTARTSYS;
1535ae67bd38STrond Myklebust 			goto out_dequeue;
1536ae67bd38STrond Myklebust 		}
15371da177e4SLinus Torvalds 	}
15381da177e4SLinus Torvalds 
1539dcbbeda8STrond Myklebust 	/*
1540dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1541dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1542dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1543dcbbeda8STrond Myklebust 	 */
1544dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1545dcbbeda8STrond Myklebust 
1546c509f15aSChuck Lever 	trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
154790d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1548adfa7144STrond Myklebust 	status = xprt->ops->send_request(req);
1549c8485e4dSTrond Myklebust 	if (status != 0) {
1550dcbbeda8STrond Myklebust 		req->rq_ntrans--;
15510c77668dSChuck Lever 		trace_xprt_transmit(req, status);
155289f90fe1STrond Myklebust 		return status;
1553c8485e4dSTrond Myklebust 	}
15547ebbbc6eSTrond Myklebust 
1555dcbbeda8STrond Myklebust 	if (is_retrans)
1556dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1557dcbbeda8STrond Myklebust 
15584a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1559c8485e4dSTrond Myklebust 
1560468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1561b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
1562262ca07dSChuck Lever 
1563262ca07dSChuck Lever 	xprt->stat.sends++;
1564262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1565262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
156615a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
156715a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1568b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
156990d91b0cSTrond Myklebust 
157090d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1571944b0429STrond Myklebust out_dequeue:
15720c77668dSChuck Lever 	trace_xprt_transmit(req, status);
1573944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
157489f90fe1STrond Myklebust 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
157589f90fe1STrond Myklebust 	return status;
157689f90fe1STrond Myklebust }
157789f90fe1STrond Myklebust 
157889f90fe1STrond Myklebust /**
157989f90fe1STrond Myklebust  * xprt_transmit - send an RPC request on a transport
158089f90fe1STrond Myklebust  * @task: controlling RPC task
158189f90fe1STrond Myklebust  *
158289f90fe1STrond Myklebust  * Attempts to drain the transmit queue. On exit, either the transport
158389f90fe1STrond Myklebust  * signalled an error that needs to be handled before transmission can
158489f90fe1STrond Myklebust  * resume, or @task finished transmitting, and detected that it already
158589f90fe1STrond Myklebust  * received a reply.
158689f90fe1STrond Myklebust  */
158789f90fe1STrond Myklebust void
158889f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task)
158989f90fe1STrond Myklebust {
159089f90fe1STrond Myklebust 	struct rpc_rqst *next, *req = task->tk_rqstp;
159189f90fe1STrond Myklebust 	struct rpc_xprt	*xprt = req->rq_xprt;
15926f9f1728SChuck Lever 	int counter, status;
159389f90fe1STrond Myklebust 
159489f90fe1STrond Myklebust 	spin_lock(&xprt->queue_lock);
15956f9f1728SChuck Lever 	counter = 0;
159689f90fe1STrond Myklebust 	while (!list_empty(&xprt->xmit_queue)) {
15976f9f1728SChuck Lever 		if (++counter == 20)
15986f9f1728SChuck Lever 			break;
159989f90fe1STrond Myklebust 		next = list_first_entry(&xprt->xmit_queue,
160089f90fe1STrond Myklebust 				struct rpc_rqst, rq_xmit);
160189f90fe1STrond Myklebust 		xprt_pin_rqst(next);
160289f90fe1STrond Myklebust 		spin_unlock(&xprt->queue_lock);
160389f90fe1STrond Myklebust 		status = xprt_request_transmit(next, task);
160489f90fe1STrond Myklebust 		if (status == -EBADMSG && next != req)
160589f90fe1STrond Myklebust 			status = 0;
160689f90fe1STrond Myklebust 		spin_lock(&xprt->queue_lock);
160789f90fe1STrond Myklebust 		xprt_unpin_rqst(next);
160889f90fe1STrond Myklebust 		if (status == 0) {
160989f90fe1STrond Myklebust 			if (!xprt_request_data_received(task) ||
161089f90fe1STrond Myklebust 			    test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
161189f90fe1STrond Myklebust 				continue;
1612c544577dSTrond Myklebust 		} else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
161389f90fe1STrond Myklebust 			task->tk_status = status;
161489f90fe1STrond Myklebust 		break;
161589f90fe1STrond Myklebust 	}
161689f90fe1STrond Myklebust 	spin_unlock(&xprt->queue_lock);
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
1619ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1620ba60eb25STrond Myklebust {
1621ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1622ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1623ba60eb25STrond Myklebust }
1624ba60eb25STrond Myklebust 
1625ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1626ba60eb25STrond Myklebust {
1627ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1628ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1629ba60eb25STrond Myklebust }
1630ba60eb25STrond Myklebust 
1631ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1632ba60eb25STrond Myklebust {
1633ba60eb25STrond Myklebust 	bool ret = false;
1634ba60eb25STrond Myklebust 
1635ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1636ba60eb25STrond Myklebust 		goto out;
1637ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1638ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1639ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1640ba60eb25STrond Myklebust 		ret = true;
1641ba60eb25STrond Myklebust 	}
1642ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1643ba60eb25STrond Myklebust out:
1644ba60eb25STrond Myklebust 	return ret;
1645ba60eb25STrond Myklebust }
1646ba60eb25STrond Myklebust 
164792ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1648d9ba131dSTrond Myklebust {
1649d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1650d9ba131dSTrond Myklebust 
1651ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1652d9ba131dSTrond Myklebust 		goto out;
1653ff699ea8SChuck Lever 	++xprt->num_reqs;
165492ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
165592ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
165692ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1657d9ba131dSTrond Myklebust 	if (req != NULL)
1658d9ba131dSTrond Myklebust 		goto out;
1659ff699ea8SChuck Lever 	--xprt->num_reqs;
1660d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1661d9ba131dSTrond Myklebust out:
1662d9ba131dSTrond Myklebust 	return req;
1663d9ba131dSTrond Myklebust }
1664d9ba131dSTrond Myklebust 
1665d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1666d9ba131dSTrond Myklebust {
1667ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1668ff699ea8SChuck Lever 		--xprt->num_reqs;
1669d9ba131dSTrond Myklebust 		kfree(req);
1670d9ba131dSTrond Myklebust 		return true;
1671d9ba131dSTrond Myklebust 	}
1672d9ba131dSTrond Myklebust 	return false;
1673d9ba131dSTrond Myklebust }
1674d9ba131dSTrond Myklebust 
1675f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
16761da177e4SLinus Torvalds {
1677d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
16781da177e4SLinus Torvalds 
1679f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
16801da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1681d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1682d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1683d9ba131dSTrond Myklebust 		goto out_init_req;
1684d9ba131dSTrond Myklebust 	}
168592ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1686d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1687d9ba131dSTrond Myklebust 		goto out_init_req;
1688d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1689d9ba131dSTrond Myklebust 	case -ENOMEM:
1690d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1691d9ba131dSTrond Myklebust 				"failed! Retrying\n");
16921afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1693d9ba131dSTrond Myklebust 		break;
1694d9ba131dSTrond Myklebust 	case -EAGAIN:
1695ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1696d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1697df561f66SGustavo A. R. Silva 		fallthrough;
16981afeaf5cSTrond Myklebust 	default:
1699d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
17001afeaf5cSTrond Myklebust 	}
1701f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1702d9ba131dSTrond Myklebust 	return;
1703d9ba131dSTrond Myklebust out_init_req:
1704ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1705ff699ea8SChuck Lever 				     xprt->num_reqs);
170637ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
170737ac86c3SChuck Lever 
1708d9ba131dSTrond Myklebust 	task->tk_status = 0;
17091da177e4SLinus Torvalds 	task->tk_rqstp = req;
17101da177e4SLinus Torvalds }
1711f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1712f39c1bfbSTrond Myklebust 
1713a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1714ee5ebe85STrond Myklebust {
1715ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1716c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1717c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1718ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1719c25573b5STrond Myklebust 	}
1720ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1721ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1722ee5ebe85STrond Myklebust }
1723a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1724ee5ebe85STrond Myklebust 
172521de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
172621de0a95STrond Myklebust {
172721de0a95STrond Myklebust 	struct rpc_rqst *req;
172821de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
172921de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
173021de0a95STrond Myklebust 		list_del(&req->rq_list);
173121de0a95STrond Myklebust 		kfree(req);
173221de0a95STrond Myklebust 	}
173321de0a95STrond Myklebust }
173421de0a95STrond Myklebust 
1735d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1736d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1737d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1738bd1722d4SPavel Emelyanov {
1739bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
174021de0a95STrond Myklebust 	struct rpc_rqst *req;
174121de0a95STrond Myklebust 	int i;
1742bd1722d4SPavel Emelyanov 
1743bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1744bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1745bd1722d4SPavel Emelyanov 		goto out;
1746bd1722d4SPavel Emelyanov 
174721de0a95STrond Myklebust 	xprt_init(xprt, net);
174821de0a95STrond Myklebust 
174921de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
175021de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
175121de0a95STrond Myklebust 		if (!req)
17528313164cSwangweidong 			goto out_free;
175321de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
175421de0a95STrond Myklebust 	}
1755d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1756d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1757d9ba131dSTrond Myklebust 	else
175821de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1759d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1760ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1761bd1722d4SPavel Emelyanov 
1762bd1722d4SPavel Emelyanov 	return xprt;
1763bd1722d4SPavel Emelyanov 
1764bd1722d4SPavel Emelyanov out_free:
176521de0a95STrond Myklebust 	xprt_free(xprt);
1766bd1722d4SPavel Emelyanov out:
1767bd1722d4SPavel Emelyanov 	return NULL;
1768bd1722d4SPavel Emelyanov }
1769bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1770bd1722d4SPavel Emelyanov 
1771e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1772e204e621SPavel Emelyanov {
177337aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
177421de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1775fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1776e204e621SPavel Emelyanov }
1777e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1778e204e621SPavel Emelyanov 
1779902c5887STrond Myklebust static void
1780902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1781902c5887STrond Myklebust {
1782902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1783902c5887STrond Myklebust }
1784902c5887STrond Myklebust 
17859dc6edcfSTrond Myklebust static __be32
17869dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
17879dc6edcfSTrond Myklebust {
17889dc6edcfSTrond Myklebust 	__be32 xid;
17899dc6edcfSTrond Myklebust 
17909dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
17919dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
17929dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
17939dc6edcfSTrond Myklebust 	return xid;
17949dc6edcfSTrond Myklebust }
17959dc6edcfSTrond Myklebust 
17969dc6edcfSTrond Myklebust static void
17979dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
17989dc6edcfSTrond Myklebust {
17999dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
18009dc6edcfSTrond Myklebust }
18019dc6edcfSTrond Myklebust 
18029dc6edcfSTrond Myklebust static void
18039dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
18049dc6edcfSTrond Myklebust {
18059dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
18069dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
18079dc6edcfSTrond Myklebust 
18089dc6edcfSTrond Myklebust 	req->rq_task	= task;
18099dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
18109dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
18119dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1812902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
18139dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
18149dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
18159dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
18169dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
181771700bb9STrond Myklebust 	req->rq_snd_buf.bvec = NULL;
181871700bb9STrond Myklebust 	req->rq_rcv_buf.bvec = NULL;
18199dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
1820da953063STrond Myklebust 	xprt_init_majortimeo(task, req);
182109d2ba0cSChuck Lever 
182209d2ba0cSChuck Lever 	trace_xprt_reserve(req);
18239dc6edcfSTrond Myklebust }
18249dc6edcfSTrond Myklebust 
18259dc6edcfSTrond Myklebust static void
18269dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
18279dc6edcfSTrond Myklebust {
18289dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
18299dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
18309dc6edcfSTrond Myklebust 		xprt_request_init(task);
18319dc6edcfSTrond Myklebust }
18329dc6edcfSTrond Myklebust 
18339903cd1cSChuck Lever /**
18349903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
18359903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
18369903cd1cSChuck Lever  *
1837ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1838ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
18399903cd1cSChuck Lever  * backlog queue.
18409903cd1cSChuck Lever  */
18419903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
18421da177e4SLinus Torvalds {
1843fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
18441da177e4SLinus Torvalds 
184543cedbf0STrond Myklebust 	task->tk_status = 0;
184643cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
184743cedbf0STrond Myklebust 		return;
184843cedbf0STrond Myklebust 
184943cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1850ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
18519dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1852ba60eb25STrond Myklebust }
1853ba60eb25STrond Myklebust 
1854ba60eb25STrond Myklebust /**
1855ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1856ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1857ba60eb25STrond Myklebust  *
1858ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1859ba60eb25STrond Myklebust  * backlog queue.
1860ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1861ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1862ba60eb25STrond Myklebust  */
1863ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1864ba60eb25STrond Myklebust {
1865fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1866ba60eb25STrond Myklebust 
1867ba60eb25STrond Myklebust 	task->tk_status = 0;
1868ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1869ba60eb25STrond Myklebust 		return;
1870ba60eb25STrond Myklebust 
1871ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
18729dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18759903cd1cSChuck Lever /**
18769903cd1cSChuck Lever  * xprt_release - release an RPC request slot
18779903cd1cSChuck Lever  * @task: task which is finished with the slot
18789903cd1cSChuck Lever  *
18791da177e4SLinus Torvalds  */
18809903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
18811da177e4SLinus Torvalds {
188255ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
188387ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
18841da177e4SLinus Torvalds 
188587ed5003STrond Myklebust 	if (req == NULL) {
188687ed5003STrond Myklebust 		if (task->tk_client) {
1887fb43d172STrond Myklebust 			xprt = task->tk_xprt;
188887ed5003STrond Myklebust 			xprt_release_write(xprt, task);
188987ed5003STrond Myklebust 		}
18901da177e4SLinus Torvalds 		return;
189187ed5003STrond Myklebust 	}
189255ae1aabSRicardo Labiaga 
189355ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
1894cc204d01STrond Myklebust 	xprt_request_dequeue_xprt(task);
1895b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
189649e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1897a58dd398SChuck Lever 	if (xprt->ops->release_request)
1898a58dd398SChuck Lever 		xprt->ops->release_request(task);
1899ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
1900b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
1901ee5ebe85STrond Myklebust 	if (req->rq_buffer)
19023435c74aSChuck Lever 		xprt->ops->buf_free(task);
19034a068258SChuck Lever 	xprt_inject_disconnect(xprt);
19049d96acbcSTrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
19050472e476STrond Myklebust 	xdr_free_bvec(&req->rq_snd_buf);
1906a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1907a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
19081da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1909ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1910ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
191155ae1aabSRicardo Labiaga 
1912ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1913a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1914ee5ebe85STrond Myklebust 	else
1915c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
1918902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1919902c5887STrond Myklebust void
1920902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1921902c5887STrond Myklebust {
1922902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1923902c5887STrond Myklebust 
1924902c5887STrond Myklebust 	task->tk_rqstp = req;
1925902c5887STrond Myklebust 	req->rq_task = task;
1926902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1927902c5887STrond Myklebust 	/*
1928902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1929902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1930902c5887STrond Myklebust 	 */
1931902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1932902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1933902c5887STrond Myklebust }
1934902c5887STrond Myklebust #endif
1935902c5887STrond Myklebust 
193621de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1937c2866763SChuck Lever {
193830c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1939c2866763SChuck Lever 
1940c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1941c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
194275c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1943c2866763SChuck Lever 
1944c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
194595f7691dSTrond Myklebust 	xprt->recv_queue = RB_ROOT;
1946944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
19479e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1948f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1949f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
19509e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
195180b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1952f9acac1aSRicardo Labiaga 
1953c2866763SChuck Lever 	xprt->last_used = jiffies;
1954c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1955a509050bSChuck Lever 	xprt->bind_index = 0;
1956c2866763SChuck Lever 
1957c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1958c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
195979c99152STrond Myklebust 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1960c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1961c2866763SChuck Lever 
1962c2866763SChuck Lever 	xprt_init_xid(xprt);
1963c2866763SChuck Lever 
196421de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
19658d9266ffSTrond Myklebust }
19668d9266ffSTrond Myklebust 
19678d9266ffSTrond Myklebust /**
19688d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
19698d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
19708d9266ffSTrond Myklebust  *
19718d9266ffSTrond Myklebust  */
19728d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
19738d9266ffSTrond Myklebust {
19748d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
19759bccd264STrond Myklebust 	const struct xprt_class *t;
19768d9266ffSTrond Myklebust 
19779bccd264STrond Myklebust 	t = xprt_class_find_by_ident(args->ident);
19789bccd264STrond Myklebust 	if (!t) {
19793c45ddf8SChuck Lever 		dprintk("RPC: transport (%d) not supported\n", args->ident);
19808d9266ffSTrond Myklebust 		return ERR_PTR(-EIO);
19819bccd264STrond Myklebust 	}
19828d9266ffSTrond Myklebust 
19838d9266ffSTrond Myklebust 	xprt = t->setup(args);
19849bccd264STrond Myklebust 	xprt_class_release(t);
19859bccd264STrond Myklebust 
1986911813d7SChuck Lever 	if (IS_ERR(xprt))
198721de0a95STrond Myklebust 		goto out;
198833d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
198933d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
199021de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
199121de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
1992502980e8SAnna Schumaker 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
199321de0a95STrond Myklebust 	else
1994ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
19954e0038b6STrond Myklebust 
19964e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
19974e0038b6STrond Myklebust 		xprt_destroy(xprt);
19984e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
19994e0038b6STrond Myklebust 	}
20004e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
20014e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
20024e0038b6STrond Myklebust 		xprt_destroy(xprt);
20034e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
20044e0038b6STrond Myklebust 	}
20054e0038b6STrond Myklebust 
20063f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
2007388f0c77SJeff Layton 
2008911813d7SChuck Lever 	trace_xprt_create(xprt);
200921de0a95STrond Myklebust out:
2010c2866763SChuck Lever 	return xprt;
2011c2866763SChuck Lever }
2012c2866763SChuck Lever 
2013528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
2014528fd354STrond Myklebust {
2015528fd354STrond Myklebust 	struct rpc_xprt *xprt =
2016528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
2017528fd354STrond Myklebust 
2018911813d7SChuck Lever 	trace_xprt_destroy(xprt);
2019911813d7SChuck Lever 
2020528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
2021528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
2022528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
2023528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
2024528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
2025528fd354STrond Myklebust 	kfree(xprt->servername);
2026528fd354STrond Myklebust 	/*
2027669996adSTrond Myklebust 	 * Destroy any existing back channel
2028669996adSTrond Myklebust 	 */
2029669996adSTrond Myklebust 	xprt_destroy_backchannel(xprt, UINT_MAX);
2030669996adSTrond Myklebust 
2031669996adSTrond Myklebust 	/*
2032528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
2033528fd354STrond Myklebust 	 */
2034528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
2035528fd354STrond Myklebust }
2036528fd354STrond Myklebust 
20379903cd1cSChuck Lever /**
20389903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
2039a8de240aSTrond Myklebust  * @xprt: transport to destroy
20409903cd1cSChuck Lever  *
20411da177e4SLinus Torvalds  */
2042a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
20431da177e4SLinus Torvalds {
2044528fd354STrond Myklebust 	/*
2045528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
2046528fd354STrond Myklebust 	 */
204779234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
204879234c3dSTrond Myklebust 
20490065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
2050c8541ecdSChuck Lever 
2051c8541ecdSChuck Lever 	/*
2052528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
2053528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
2054c8541ecdSChuck Lever 	 */
2055528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2056528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
20576b6ca86bSTrond Myklebust }
20581da177e4SLinus Torvalds 
205930c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
206030c5116bSTrond Myklebust {
206130c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
206230c5116bSTrond Myklebust }
206330c5116bSTrond Myklebust 
206430c5116bSTrond Myklebust /**
206530c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
206630c5116bSTrond Myklebust  * @xprt: pointer to the transport
206730c5116bSTrond Myklebust  *
206830c5116bSTrond Myklebust  */
206930c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
207030c5116bSTrond Myklebust {
207130c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
207230c5116bSTrond Myklebust 		return xprt;
207330c5116bSTrond Myklebust 	return NULL;
207430c5116bSTrond Myklebust }
207530c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
207630c5116bSTrond Myklebust 
20776b6ca86bSTrond Myklebust /**
20786b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
20796b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
20806b6ca86bSTrond Myklebust  *
20816b6ca86bSTrond Myklebust  */
20826b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
20836b6ca86bSTrond Myklebust {
208430c5116bSTrond Myklebust 	if (xprt != NULL)
208530c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
20866b6ca86bSTrond Myklebust }
20875d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
2088