xref: /openbmc/linux/net/sunrpc/xprt.c (revision a4ae3081)
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"
58587bc725SOlga Kornievskaia #include "sysfs.h"
59*a4ae3081SChuck Lever #include "fail.h"
6055ae1aabSRicardo Labiaga 
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * Local variables
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds 
65f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
661da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * Local functions
711da177e4SLinus Torvalds  */
7221de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
7337ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
744e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
75e877a88dSNeilBrown static void	 xprt_request_init(struct rpc_task *task);
761da177e4SLinus Torvalds 
775ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7881c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7981c098afS\"Talpey, Thomas\ 
809e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
819e910bffSTrond Myklebust {
829e910bffSTrond Myklebust 	unsigned long timeout = jiffies + req->rq_timeout;
839e910bffSTrond Myklebust 
849e910bffSTrond Myklebust 	if (time_before(timeout, req->rq_majortimeo))
859e910bffSTrond Myklebust 		return timeout;
869e910bffSTrond Myklebust 	return req->rq_majortimeo;
879e910bffSTrond Myklebust }
889e910bffSTrond Myklebust 
8912a80469SChuck Lever /**
9081c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
9181c098afS\"Talpey, Thomas\  * @transport: transport to register
9281c098afS\"Talpey, Thomas\  *
9381c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9481c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9581c098afS\"Talpey, Thomas\  *
9681c098afS\"Talpey, Thomas\  * Returns:
9781c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
9881c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
9981c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
10081c098afS\"Talpey, Thomas\  */
10181c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
10281c098afS\"Talpey, Thomas\ {
10381c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10481c098afS\"Talpey, Thomas\ 	int result;
10581c098afS\"Talpey, Thomas\ 
10681c098afS\"Talpey, Thomas\ 	result = -EEXIST;
10781c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
10881c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
10981c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1104fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
11181c098afS\"Talpey, Thomas\ 			goto out;
11281c098afS\"Talpey, Thomas\ 	}
11381c098afS\"Talpey, Thomas\ 
11481c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11581c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11681c098afS\"Talpey, Thomas\ 	       transport->name);
11781c098afS\"Talpey, Thomas\ 	result = 0;
11881c098afS\"Talpey, Thomas\ 
11981c098afS\"Talpey, Thomas\ out:
12081c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
12181c098afS\"Talpey, Thomas\ 	return result;
12281c098afS\"Talpey, Thomas\ }
12381c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12481c098afS\"Talpey, Thomas\ 
12581c098afS\"Talpey, Thomas\ /**
12681c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
12765b6e42cSRandy Dunlap  * @transport: transport to unregister
12881c098afS\"Talpey, Thomas\  *
12981c098afS\"Talpey, Thomas\  * Returns:
13081c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
13181c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
13281c098afS\"Talpey, Thomas\  */
13381c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13481c098afS\"Talpey, Thomas\ {
13581c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13681c098afS\"Talpey, Thomas\ 	int result;
13781c098afS\"Talpey, Thomas\ 
13881c098afS\"Talpey, Thomas\ 	result = 0;
13981c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
14081c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
14181c098afS\"Talpey, Thomas\ 		if (t == transport) {
14281c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
14381c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14481c098afS\"Talpey, Thomas\ 				transport->name);
14581c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14681c098afS\"Talpey, Thomas\ 			goto out;
14781c098afS\"Talpey, Thomas\ 		}
14881c098afS\"Talpey, Thomas\ 	}
14981c098afS\"Talpey, Thomas\ 	result = -ENOENT;
15081c098afS\"Talpey, Thomas\ 
15181c098afS\"Talpey, Thomas\ out:
15281c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
15381c098afS\"Talpey, Thomas\ 	return result;
15481c098afS\"Talpey, Thomas\ }
15581c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15681c098afS\"Talpey, Thomas\ 
157d5aa6b22STrond Myklebust static void
158d5aa6b22STrond Myklebust xprt_class_release(const struct xprt_class *t)
159d5aa6b22STrond Myklebust {
160d5aa6b22STrond Myklebust 	module_put(t->owner);
161d5aa6b22STrond Myklebust }
162d5aa6b22STrond Myklebust 
163d5aa6b22STrond Myklebust static const struct xprt_class *
1649bccd264STrond Myklebust xprt_class_find_by_ident_locked(int ident)
1659bccd264STrond Myklebust {
1669bccd264STrond Myklebust 	const struct xprt_class *t;
1679bccd264STrond Myklebust 
1689bccd264STrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
1699bccd264STrond Myklebust 		if (t->ident != ident)
1709bccd264STrond Myklebust 			continue;
1719bccd264STrond Myklebust 		if (!try_module_get(t->owner))
1729bccd264STrond Myklebust 			continue;
1739bccd264STrond Myklebust 		return t;
1749bccd264STrond Myklebust 	}
1759bccd264STrond Myklebust 	return NULL;
1769bccd264STrond Myklebust }
1779bccd264STrond Myklebust 
1789bccd264STrond Myklebust static const struct xprt_class *
1799bccd264STrond Myklebust xprt_class_find_by_ident(int ident)
1809bccd264STrond Myklebust {
1819bccd264STrond Myklebust 	const struct xprt_class *t;
1829bccd264STrond Myklebust 
1839bccd264STrond Myklebust 	spin_lock(&xprt_list_lock);
1849bccd264STrond Myklebust 	t = xprt_class_find_by_ident_locked(ident);
1859bccd264STrond Myklebust 	spin_unlock(&xprt_list_lock);
1869bccd264STrond Myklebust 	return t;
1879bccd264STrond Myklebust }
1889bccd264STrond Myklebust 
1899bccd264STrond Myklebust static const struct xprt_class *
190d5aa6b22STrond Myklebust xprt_class_find_by_netid_locked(const char *netid)
191d5aa6b22STrond Myklebust {
192d5aa6b22STrond Myklebust 	const struct xprt_class *t;
193d5aa6b22STrond Myklebust 	unsigned int i;
194d5aa6b22STrond Myklebust 
195d5aa6b22STrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
196d5aa6b22STrond Myklebust 		for (i = 0; t->netid[i][0] != '\0'; i++) {
197d5aa6b22STrond Myklebust 			if (strcmp(t->netid[i], netid) != 0)
198d5aa6b22STrond Myklebust 				continue;
199d5aa6b22STrond Myklebust 			if (!try_module_get(t->owner))
200d5aa6b22STrond Myklebust 				continue;
201d5aa6b22STrond Myklebust 			return t;
202d5aa6b22STrond Myklebust 		}
203d5aa6b22STrond Myklebust 	}
204d5aa6b22STrond Myklebust 	return NULL;
205d5aa6b22STrond Myklebust }
206d5aa6b22STrond Myklebust 
207d5aa6b22STrond Myklebust static const struct xprt_class *
208d5aa6b22STrond Myklebust xprt_class_find_by_netid(const char *netid)
209d5aa6b22STrond Myklebust {
210d5aa6b22STrond Myklebust 	const struct xprt_class *t;
211d5aa6b22STrond Myklebust 
212d5aa6b22STrond Myklebust 	spin_lock(&xprt_list_lock);
213d5aa6b22STrond Myklebust 	t = xprt_class_find_by_netid_locked(netid);
214d5aa6b22STrond Myklebust 	if (!t) {
215d5aa6b22STrond Myklebust 		spin_unlock(&xprt_list_lock);
216d5aa6b22STrond Myklebust 		request_module("rpc%s", netid);
217d5aa6b22STrond Myklebust 		spin_lock(&xprt_list_lock);
218d5aa6b22STrond Myklebust 		t = xprt_class_find_by_netid_locked(netid);
219d5aa6b22STrond Myklebust 	}
220d5aa6b22STrond Myklebust 	spin_unlock(&xprt_list_lock);
221d5aa6b22STrond Myklebust 	return t;
222d5aa6b22STrond Myklebust }
223d5aa6b22STrond Myklebust 
22481c098afS\"Talpey, Thomas\ /**
2251fc5f131STrond Myklebust  * xprt_find_transport_ident - convert a netid into a transport identifier
2261fc5f131STrond Myklebust  * @netid: transport to load
2271fc5f131STrond Myklebust  *
2281fc5f131STrond Myklebust  * Returns:
2291fc5f131STrond Myklebust  * > 0:		transport identifier
2301fc5f131STrond Myklebust  * -ENOENT:	transport module not available
2311fc5f131STrond Myklebust  */
2321fc5f131STrond Myklebust int xprt_find_transport_ident(const char *netid)
2331fc5f131STrond Myklebust {
2341fc5f131STrond Myklebust 	const struct xprt_class *t;
2351fc5f131STrond Myklebust 	int ret;
2361fc5f131STrond Myklebust 
2371fc5f131STrond Myklebust 	t = xprt_class_find_by_netid(netid);
2381fc5f131STrond Myklebust 	if (!t)
2391fc5f131STrond Myklebust 		return -ENOENT;
2401fc5f131STrond Myklebust 	ret = t->ident;
2411fc5f131STrond Myklebust 	xprt_class_release(t);
2421fc5f131STrond Myklebust 	return ret;
2431fc5f131STrond Myklebust }
2441fc5f131STrond Myklebust EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
2451fc5f131STrond Myklebust 
246c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
247c544577dSTrond Myklebust {
248c544577dSTrond Myklebust 	xprt->snd_task = NULL;
249c544577dSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
250c544577dSTrond Myklebust 		smp_mb__before_atomic();
251c544577dSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
252c544577dSTrond Myklebust 		smp_mb__after_atomic();
253c544577dSTrond Myklebust 	} else
254c544577dSTrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
255c544577dSTrond Myklebust }
256c544577dSTrond Myklebust 
257441e3e24STom Talpey /**
25812a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
25912a80469SChuck Lever  * @task: task that is requesting access to the transport
260177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
26112a80469SChuck Lever  *
26212a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
26312a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
26412a80469SChuck Lever  * is provided.
2651da177e4SLinus Torvalds  */
26643cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
2671da177e4SLinus Torvalds {
26812a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
26912a80469SChuck Lever 
27012a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
27112a80469SChuck Lever 		if (task == xprt->snd_task)
272bf7ca707SChuck Lever 			goto out_locked;
27312a80469SChuck Lever 		goto out_sleep;
27412a80469SChuck Lever 	}
275c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
276c544577dSTrond Myklebust 		goto out_unlock;
27712a80469SChuck Lever 	xprt->snd_task = task;
2784d4a76f3Sj223yang@asset.uwaterloo.ca 
279bf7ca707SChuck Lever out_locked:
280bf7ca707SChuck Lever 	trace_xprt_reserve_xprt(xprt, task);
28112a80469SChuck Lever 	return 1;
28212a80469SChuck Lever 
283c544577dSTrond Myklebust out_unlock:
284c544577dSTrond Myklebust 	xprt_clear_locked(xprt);
28512a80469SChuck Lever out_sleep:
28612a80469SChuck Lever 	task->tk_status = -EAGAIN;
2876b2e6856STrond Myklebust 	if  (RPC_IS_SOFT(task))
2886b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2899e910bffSTrond Myklebust 				xprt_request_timeout(req));
2906b2e6856STrond Myklebust 	else
29179c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
29212a80469SChuck Lever 	return 0;
29312a80469SChuck Lever }
29412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
29512a80469SChuck Lever 
29675891f50STrond Myklebust static bool
29775891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
29875891f50STrond Myklebust {
29975891f50STrond Myklebust 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
30075891f50STrond Myklebust }
30175891f50STrond Myklebust 
30275891f50STrond Myklebust static void
30375891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
30475891f50STrond Myklebust {
30575891f50STrond Myklebust 	if (!list_empty(&xprt->xmit_queue)) {
30675891f50STrond Myklebust 		/* Peek at head of queue to see if it can make progress */
30775891f50STrond Myklebust 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
30875891f50STrond Myklebust 					rq_xmit)->rq_cong)
30975891f50STrond Myklebust 			return;
31075891f50STrond Myklebust 	}
31175891f50STrond Myklebust 	set_bit(XPRT_CWND_WAIT, &xprt->state);
31275891f50STrond Myklebust }
31375891f50STrond Myklebust 
31475891f50STrond Myklebust static void
31575891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
31675891f50STrond Myklebust {
31775891f50STrond Myklebust 	if (!RPCXPRT_CONGESTED(xprt))
31875891f50STrond Myklebust 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
31975891f50STrond Myklebust }
32075891f50STrond Myklebust 
32112a80469SChuck Lever /*
32212a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
32312a80469SChuck Lever  * @task: task that is requesting access to the transport
32412a80469SChuck Lever  *
32512a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
32612a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
32712a80469SChuck Lever  * woken up and given access to the transport.
32875891f50STrond Myklebust  * Note that the lock is only granted if we know there are free slots.
32912a80469SChuck Lever  */
33043cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
33112a80469SChuck Lever {
3321da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3331da177e4SLinus Torvalds 
3342226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
3351da177e4SLinus Torvalds 		if (task == xprt->snd_task)
336bf7ca707SChuck Lever 			goto out_locked;
3371da177e4SLinus Torvalds 		goto out_sleep;
3381da177e4SLinus Torvalds 	}
33943cedbf0STrond Myklebust 	if (req == NULL) {
34043cedbf0STrond Myklebust 		xprt->snd_task = task;
341bf7ca707SChuck Lever 		goto out_locked;
34243cedbf0STrond Myklebust 	}
343c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
344c544577dSTrond Myklebust 		goto out_unlock;
34575891f50STrond Myklebust 	if (!xprt_need_congestion_window_wait(xprt)) {
3461da177e4SLinus Torvalds 		xprt->snd_task = task;
347bf7ca707SChuck Lever 		goto out_locked;
3481da177e4SLinus Torvalds 	}
349c544577dSTrond Myklebust out_unlock:
350632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3511da177e4SLinus Torvalds out_sleep:
3521da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
3536b2e6856STrond Myklebust 	if (RPC_IS_SOFT(task))
3546b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
3559e910bffSTrond Myklebust 				xprt_request_timeout(req));
3566b2e6856STrond Myklebust 	else
35779c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
3581da177e4SLinus Torvalds 	return 0;
359bf7ca707SChuck Lever out_locked:
360bf7ca707SChuck Lever 	trace_xprt_reserve_cong(xprt, task);
361bf7ca707SChuck Lever 	return 1;
3621da177e4SLinus Torvalds }
36312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
3641da177e4SLinus Torvalds 
36512a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	int retval;
3681da177e4SLinus Torvalds 
369bd79bc57STrond Myklebust 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
370bd79bc57STrond Myklebust 		return 1;
371b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
37243cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
373b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
3741da177e4SLinus Torvalds 	return retval;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
377961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
3781da177e4SLinus Torvalds {
379961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
38049e9a890SChuck Lever 
38149e9a890SChuck Lever 	xprt->snd_task = task;
382961a828dSTrond Myklebust 	return true;
383961a828dSTrond Myklebust }
384961a828dSTrond Myklebust 
385961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
386961a828dSTrond Myklebust {
387961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
38849e9a890SChuck Lever 		return;
389c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
390c544577dSTrond Myklebust 		goto out_unlock;
391f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
392f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
393961a828dSTrond Myklebust 		return;
394c544577dSTrond Myklebust out_unlock:
395632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
39649e9a890SChuck Lever }
39749e9a890SChuck Lever 
398961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
399961a828dSTrond Myklebust {
400961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
401961a828dSTrond Myklebust 		return;
402c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
403c544577dSTrond Myklebust 		goto out_unlock;
40475891f50STrond Myklebust 	if (xprt_need_congestion_window_wait(xprt))
405961a828dSTrond Myklebust 		goto out_unlock;
406f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
40775891f50STrond Myklebust 				__xprt_lock_write_func, xprt))
408961a828dSTrond Myklebust 		return;
4091da177e4SLinus Torvalds out_unlock:
410632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
41349e9a890SChuck Lever /**
41449e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
41549e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
41649e9a890SChuck Lever  * @task: task that is releasing access to the transport
41749e9a890SChuck Lever  *
41849e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
4191da177e4SLinus Torvalds  */
42049e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
423632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
4241da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
4251da177e4SLinus Torvalds 	}
426bf7ca707SChuck Lever 	trace_xprt_release_xprt(xprt, task);
4271da177e4SLinus Torvalds }
42812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
4291da177e4SLinus Torvalds 
43049e9a890SChuck Lever /**
43149e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
43249e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
43349e9a890SChuck Lever  * @task: task that is releasing access to the transport
43449e9a890SChuck Lever  *
43549e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
43649e9a890SChuck Lever  * transport if the transport's congestion window allows it.
43749e9a890SChuck Lever  */
43849e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
43949e9a890SChuck Lever {
44049e9a890SChuck Lever 	if (xprt->snd_task == task) {
441632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
44249e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
44349e9a890SChuck Lever 	}
444bf7ca707SChuck Lever 	trace_xprt_release_cong(xprt, task);
44549e9a890SChuck Lever }
44612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
44749e9a890SChuck Lever 
448587bc725SOlga Kornievskaia void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
4491da177e4SLinus Torvalds {
450bd79bc57STrond Myklebust 	if (xprt->snd_task != task)
451bd79bc57STrond Myklebust 		return;
452b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
45349e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
454b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
4591da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds static int
46275891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	if (req->rq_cong)
4651da177e4SLinus Torvalds 		return 1;
466bf7ca707SChuck Lever 	trace_xprt_get_cong(xprt, req->rq_task);
46775891f50STrond Myklebust 	if (RPCXPRT_CONGESTED(xprt)) {
46875891f50STrond Myklebust 		xprt_set_congestion_window_wait(xprt);
4691da177e4SLinus Torvalds 		return 0;
47075891f50STrond Myklebust 	}
4711da177e4SLinus Torvalds 	req->rq_cong = 1;
4721da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4731da177e4SLinus Torvalds 	return 1;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4781da177e4SLinus Torvalds  * that has been sleeping due to congestion
4791da177e4SLinus Torvalds  */
4801da177e4SLinus Torvalds static void
4811da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds 	if (!req->rq_cong)
4841da177e4SLinus Torvalds 		return;
4851da177e4SLinus Torvalds 	req->rq_cong = 0;
4861da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
48775891f50STrond Myklebust 	xprt_test_and_clear_congestion_window_wait(xprt);
488bf7ca707SChuck Lever 	trace_xprt_put_cong(xprt, req->rq_task);
48949e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
49246c0ee8bSChuck Lever /**
49375891f50STrond Myklebust  * xprt_request_get_cong - Request congestion control credits
49475891f50STrond Myklebust  * @xprt: pointer to transport
49575891f50STrond Myklebust  * @req: pointer to RPC request
49675891f50STrond Myklebust  *
49775891f50STrond Myklebust  * Useful for transports that require congestion control.
49875891f50STrond Myklebust  */
49975891f50STrond Myklebust bool
50075891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
50175891f50STrond Myklebust {
50275891f50STrond Myklebust 	bool ret = false;
50375891f50STrond Myklebust 
50475891f50STrond Myklebust 	if (req->rq_cong)
50575891f50STrond Myklebust 		return true;
506b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
50775891f50STrond Myklebust 	ret = __xprt_get_cong(xprt, req) != 0;
508b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
50975891f50STrond Myklebust 	return ret;
51075891f50STrond Myklebust }
51175891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong);
51275891f50STrond Myklebust 
51375891f50STrond Myklebust /**
514a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
515a58dd398SChuck Lever  * @task: RPC request that recently completed
516a58dd398SChuck Lever  *
517a58dd398SChuck Lever  * Useful for transports that require congestion control.
518a58dd398SChuck Lever  */
519a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
520a58dd398SChuck Lever {
521a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
522a4f0835cSTrond Myklebust 
523a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
524a58dd398SChuck Lever }
52512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
526a58dd398SChuck Lever 
5278593e010SChuck Lever static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
5288593e010SChuck Lever {
5298593e010SChuck Lever 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
5308593e010SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5318593e010SChuck Lever }
5328593e010SChuck Lever 
53375891f50STrond Myklebust /*
53475891f50STrond Myklebust  * Clear the congestion window wait flag and wake up the next
53575891f50STrond Myklebust  * entry on xprt->sending
53675891f50STrond Myklebust  */
53775891f50STrond Myklebust static void
53875891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
53975891f50STrond Myklebust {
54075891f50STrond Myklebust 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
541b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
54275891f50STrond Myklebust 		__xprt_lock_write_next_cong(xprt);
543b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
54475891f50STrond Myklebust 	}
54575891f50STrond Myklebust }
54675891f50STrond Myklebust 
547a58dd398SChuck Lever /**
54846c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
5496a24dfb6STrond Myklebust  * @xprt: pointer to xprt
55046c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
55146c0ee8bSChuck Lever  * @result: result code of completed RPC request
55246c0ee8bSChuck Lever  *
5534f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
5544f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
5554f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
5564f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
5574f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
5584f4cf5adSChuck Lever  *
5594f4cf5adSChuck Lever  *	-	a reply is received and
5604f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
5614f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
5621da177e4SLinus Torvalds  */
5636a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
5641da177e4SLinus Torvalds {
56546c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
56646c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
5691da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
5701da177e4SLinus Torvalds 		 * the result gets rounded properly. */
5711da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
5721da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
5731da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
57449e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5751da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
5761da177e4SLinus Torvalds 		cwnd >>= 1;
5771da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
5781da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
5811da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
5821da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
58346c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
5841da177e4SLinus Torvalds }
58512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
5861da177e4SLinus Torvalds 
58744fbac22SChuck Lever /**
58844fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
58944fbac22SChuck Lever  * @xprt: transport with waiting tasks
59044fbac22SChuck Lever  * @status: result code to plant in each task before waking it
59144fbac22SChuck Lever  *
59244fbac22SChuck Lever  */
59344fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
59444fbac22SChuck Lever {
59544fbac22SChuck Lever 	if (status < 0)
59644fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
59744fbac22SChuck Lever 	else
59844fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
59944fbac22SChuck Lever }
60012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
60144fbac22SChuck Lever 
602c7b2cae8SChuck Lever /**
603c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
604c544577dSTrond Myklebust  * @xprt: transport
605a9a6b52eSTrond Myklebust  *
606a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
607a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
608a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
609c7b2cae8SChuck Lever  */
610c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
611c7b2cae8SChuck Lever {
612c544577dSTrond Myklebust 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
613c7b2cae8SChuck Lever }
61412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
615c7b2cae8SChuck Lever 
616c544577dSTrond Myklebust static bool
617c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt)
618c544577dSTrond Myklebust {
619c544577dSTrond Myklebust 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
620c544577dSTrond Myklebust 		__xprt_lock_write_next(xprt);
621c544577dSTrond Myklebust 		dprintk("RPC:       write space: waking waiting task on "
622c544577dSTrond Myklebust 				"xprt %p\n", xprt);
623c544577dSTrond Myklebust 		return true;
624c544577dSTrond Myklebust 	}
625c544577dSTrond Myklebust 	return false;
626c544577dSTrond Myklebust }
627c544577dSTrond Myklebust 
628c7b2cae8SChuck Lever /**
629c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
630c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
631c7b2cae8SChuck Lever  *
632c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
633c7b2cae8SChuck Lever  */
634c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt)
635c7b2cae8SChuck Lever {
636c544577dSTrond Myklebust 	bool ret;
637c544577dSTrond Myklebust 
638c544577dSTrond Myklebust 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
639c544577dSTrond Myklebust 		return false;
640b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
641c544577dSTrond Myklebust 	ret = xprt_clear_write_space_locked(xprt);
642b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
643c544577dSTrond Myklebust 	return ret;
644c7b2cae8SChuck Lever }
64512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
646c7b2cae8SChuck Lever 
647da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
648da953063STrond Myklebust {
649da953063STrond Myklebust 	s64 delta = ktime_to_ns(ktime_get() - abstime);
650da953063STrond Myklebust 	return likely(delta >= 0) ?
651da953063STrond Myklebust 		jiffies - nsecs_to_jiffies(delta) :
652da953063STrond Myklebust 		jiffies + nsecs_to_jiffies(-delta);
653da953063STrond Myklebust }
654da953063STrond Myklebust 
655da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
6561da177e4SLinus Torvalds {
657ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
658da953063STrond Myklebust 	unsigned long majortimeo = req->rq_timeout;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	if (to->to_exponential)
661da953063STrond Myklebust 		majortimeo <<= to->to_retries;
6621da177e4SLinus Torvalds 	else
663da953063STrond Myklebust 		majortimeo += to->to_increment * to->to_retries;
664da953063STrond Myklebust 	if (majortimeo > to->to_maxval || majortimeo == 0)
665da953063STrond Myklebust 		majortimeo = to->to_maxval;
666da953063STrond Myklebust 	return majortimeo;
667da953063STrond Myklebust }
668da953063STrond Myklebust 
669da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req)
670da953063STrond Myklebust {
671da953063STrond Myklebust 	req->rq_majortimeo += xprt_calc_majortimeo(req);
672da953063STrond Myklebust }
673da953063STrond Myklebust 
6747de62bc0SOlga Kornievskaia static void xprt_reset_minortimeo(struct rpc_rqst *req)
6757de62bc0SOlga Kornievskaia {
6767de62bc0SOlga Kornievskaia 	req->rq_minortimeo += req->rq_timeout;
6777de62bc0SOlga Kornievskaia }
6787de62bc0SOlga Kornievskaia 
679da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
680da953063STrond Myklebust {
681da953063STrond Myklebust 	unsigned long time_init;
682da953063STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
683da953063STrond Myklebust 
684da953063STrond Myklebust 	if (likely(xprt && xprt_connected(xprt)))
685da953063STrond Myklebust 		time_init = jiffies;
686da953063STrond Myklebust 	else
687da953063STrond Myklebust 		time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
688da953063STrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
689da953063STrond Myklebust 	req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
6907de62bc0SOlga Kornievskaia 	req->rq_minortimeo = time_init + req->rq_timeout;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6939903cd1cSChuck Lever /**
6949903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
6959903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
6969903cd1cSChuck Lever  *
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
701ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
7021da177e4SLinus Torvalds 	int status = 0;
7031da177e4SLinus Torvalds 
70409252177SChris Dion 	if (time_before(jiffies, req->rq_majortimeo)) {
7057de62bc0SOlga Kornievskaia 		if (time_before(jiffies, req->rq_minortimeo))
7067de62bc0SOlga Kornievskaia 			return status;
7071da177e4SLinus Torvalds 		if (to->to_exponential)
7081da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
7091da177e4SLinus Torvalds 		else
7101da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
7111da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
7121da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
7131da177e4SLinus Torvalds 		req->rq_retries++;
7141da177e4SLinus Torvalds 	} else {
7151da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
7161da177e4SLinus Torvalds 		req->rq_retries = 0;
7171da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
7181da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
719b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
7201da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
721b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
7221da177e4SLinus Torvalds 		status = -ETIMEDOUT;
7231da177e4SLinus Torvalds 	}
7247de62bc0SOlga Kornievskaia 	xprt_reset_minortimeo(req);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
7271da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
7281da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 	return status;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
73365f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
7341da177e4SLinus Torvalds {
73565f27f38SDavid Howells 	struct rpc_xprt *xprt =
73665f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
737a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
7381da177e4SLinus Torvalds 
739911813d7SChuck Lever 	trace_xprt_disconnect_auto(xprt);
74066af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7414876cc77STrond Myklebust 	xprt->ops->close(xprt);
7421da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
74379234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
744a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7479903cd1cSChuck Lever /**
74862da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
7499903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
7509903cd1cSChuck Lever  *
7511da177e4SLinus Torvalds  */
75262da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
7531da177e4SLinus Torvalds {
754911813d7SChuck Lever 	trace_xprt_disconnect_done(xprt);
755b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
7561da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
757c544577dSTrond Myklebust 	xprt_clear_write_space_locked(xprt);
7588593e010SChuck Lever 	xprt_clear_congestion_window_wait_locked(xprt);
75927adc785STrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
760b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
7611da177e4SLinus Torvalds }
76262da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
7631da177e4SLinus Torvalds 
76466af1e55STrond Myklebust /**
76566af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
76666af1e55STrond Myklebust  * @xprt: transport to disconnect
76766af1e55STrond Myklebust  *
76866af1e55STrond Myklebust  */
76966af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
77066af1e55STrond Myklebust {
771911813d7SChuck Lever 	trace_xprt_disconnect_force(xprt);
772911813d7SChuck Lever 
77366af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
774b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
77566af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
77666af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
77766af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
77840a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7790445f92cSTrond Myklebust 	else if (xprt->snd_task)
7800445f92cSTrond Myklebust 		rpc_wake_up_queued_task_set_status(&xprt->pending,
7810445f92cSTrond Myklebust 				xprt->snd_task, -ENOTCONN);
782b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
78366af1e55STrond Myklebust }
784e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
78566af1e55STrond Myklebust 
7867f3a1d1eSTrond Myklebust static unsigned int
7877f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
7887f3a1d1eSTrond Myklebust {
7897f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
7907f3a1d1eSTrond Myklebust }
7917f3a1d1eSTrond Myklebust 
7927f3a1d1eSTrond Myklebust static bool
7937f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
7947f3a1d1eSTrond Myklebust {
7957f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
7967f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
7977f3a1d1eSTrond Myklebust 
7987f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
7997f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
8007f3a1d1eSTrond Myklebust }
8017f3a1d1eSTrond Myklebust 
8027c1d71cfSTrond Myklebust /**
8037c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
8047c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
8057c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
8067c1d71cfSTrond Myklebust  *
8077c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
8087c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
8097c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
8107c1d71cfSTrond Myklebust  * a batch of RPC requests.
8117c1d71cfSTrond Myklebust  *
8127c1d71cfSTrond Myklebust  */
8137c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
8147c1d71cfSTrond Myklebust {
8157c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
816b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
8177c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
8187c1d71cfSTrond Myklebust 		goto out;
8192c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
8207c1d71cfSTrond Myklebust 		goto out;
8217c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
8227c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
8237c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
82440a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
8252a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
8267c1d71cfSTrond Myklebust out:
827b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
8287c1d71cfSTrond Myklebust }
8297c1d71cfSTrond Myklebust 
830ad3331acSTrond Myklebust static bool
831ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
832ad3331acSTrond Myklebust {
833ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
834ad3331acSTrond Myklebust }
835ad3331acSTrond Myklebust 
836ad3331acSTrond Myklebust static void
837ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
838ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
839ad3331acSTrond Myklebust {
84080d3c45fSDave Wysochanski 	xprt->last_used = jiffies;
84195f7691dSTrond Myklebust 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
842ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
843ad3331acSTrond Myklebust }
844ad3331acSTrond Myklebust 
8451da177e4SLinus Torvalds static void
846ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
8471da177e4SLinus Torvalds {
848ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
8491da177e4SLinus Torvalds 
85095f7691dSTrond Myklebust 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
851b5e92419STrond Myklebust 		return;
852ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
853ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
8542226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
8551da177e4SLinus Torvalds 		return;
856b5e92419STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
859*a4ae3081SChuck Lever #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
860*a4ae3081SChuck Lever static void xprt_inject_disconnect(struct rpc_xprt *xprt)
861*a4ae3081SChuck Lever {
862*a4ae3081SChuck Lever 	if (!fail_sunrpc.ignore_client_disconnect &&
863*a4ae3081SChuck Lever 	    should_fail(&fail_sunrpc.attr, 1))
864*a4ae3081SChuck Lever 		xprt->ops->inject_disconnect(xprt);
865*a4ae3081SChuck Lever }
866*a4ae3081SChuck Lever #else
867*a4ae3081SChuck Lever static inline void xprt_inject_disconnect(struct rpc_xprt *xprt)
868*a4ae3081SChuck Lever {
869*a4ae3081SChuck Lever }
870*a4ae3081SChuck Lever #endif
871*a4ae3081SChuck Lever 
872718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
873718ba5b8STrond Myklebust 		struct rpc_task *task,
874718ba5b8STrond Myklebust 		void *cookie)
875718ba5b8STrond Myklebust {
876718ba5b8STrond Myklebust 	bool ret = false;
877718ba5b8STrond Myklebust 
878b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
879718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
880718ba5b8STrond Myklebust 		goto out;
881718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
882718ba5b8STrond Myklebust 		goto out;
883718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
884718ba5b8STrond Myklebust 	ret = true;
885718ba5b8STrond Myklebust out:
886b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
887718ba5b8STrond Myklebust 	return ret;
888718ba5b8STrond Myklebust }
889718ba5b8STrond Myklebust 
890718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
891718ba5b8STrond Myklebust {
892b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
893718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
894718ba5b8STrond Myklebust 		goto out;
895718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
896718ba5b8STrond Myklebust 		goto out;
897718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
898718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
899ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
900718ba5b8STrond Myklebust out:
901b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
90279234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
903718ba5b8STrond Myklebust }
904718ba5b8STrond Myklebust 
9059903cd1cSChuck Lever /**
9069903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
9079903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
9081da177e4SLinus Torvalds  *
9091da177e4SLinus Torvalds  */
9101da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
9111da177e4SLinus Torvalds {
912ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
9131da177e4SLinus Torvalds 
914db0a86c4SChuck Lever 	trace_xprt_connect(xprt);
9151da177e4SLinus Torvalds 
916ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
91701d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
9181da177e4SLinus Torvalds 		return;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
9211da177e4SLinus Torvalds 		return;
922feb8ca37STrond Myklebust 
923911813d7SChuck Lever 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
924911813d7SChuck Lever 		trace_xprt_disconnect_cleanup(xprt);
925feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
926911813d7SChuck Lever 	}
927feb8ca37STrond Myklebust 
928718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
9292c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
9306b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
9319e910bffSTrond Myklebust 				xprt_request_timeout(task->tk_rqstp));
9320b9e7943STrond Myklebust 
9330b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
9340b9e7943STrond Myklebust 			return;
9350b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
9360b9e7943STrond Myklebust 			return;
9370a9a4304STrond Myklebust 		/* Race breaker */
9380a9a4304STrond Myklebust 		if (!xprt_connected(xprt)) {
939262ca07dSChuck Lever 			xprt->stat.connect_start = jiffies;
9401b092092STrond Myklebust 			xprt->ops->connect(xprt, task);
9410a9a4304STrond Myklebust 		} else {
9420a9a4304STrond Myklebust 			xprt_clear_connecting(xprt);
9430a9a4304STrond Myklebust 			task->tk_status = 0;
9440a9a4304STrond Myklebust 			rpc_wake_up_queued_task(&xprt->pending, task);
9450a9a4304STrond Myklebust 		}
9461da177e4SLinus Torvalds 	}
947718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
950675dd90aSChuck Lever /**
951675dd90aSChuck Lever  * xprt_reconnect_delay - compute the wait before scheduling a connect
952675dd90aSChuck Lever  * @xprt: transport instance
953675dd90aSChuck Lever  *
954675dd90aSChuck Lever  */
955675dd90aSChuck Lever unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
956675dd90aSChuck Lever {
957675dd90aSChuck Lever 	unsigned long start, now = jiffies;
958675dd90aSChuck Lever 
959675dd90aSChuck Lever 	start = xprt->stat.connect_start + xprt->reestablish_timeout;
960675dd90aSChuck Lever 	if (time_after(start, now))
961675dd90aSChuck Lever 		return start - now;
962675dd90aSChuck Lever 	return 0;
963675dd90aSChuck Lever }
964675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
965675dd90aSChuck Lever 
966675dd90aSChuck Lever /**
967675dd90aSChuck Lever  * xprt_reconnect_backoff - compute the new re-establish timeout
968675dd90aSChuck Lever  * @xprt: transport instance
969675dd90aSChuck Lever  * @init_to: initial reestablish timeout
970675dd90aSChuck Lever  *
971675dd90aSChuck Lever  */
972675dd90aSChuck Lever void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
973675dd90aSChuck Lever {
974675dd90aSChuck Lever 	xprt->reestablish_timeout <<= 1;
975675dd90aSChuck Lever 	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
976675dd90aSChuck Lever 		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
977675dd90aSChuck Lever 	if (xprt->reestablish_timeout < init_to)
978675dd90aSChuck Lever 		xprt->reestablish_timeout = init_to;
979675dd90aSChuck Lever }
980675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
981675dd90aSChuck Lever 
98295f7691dSTrond Myklebust enum xprt_xid_rb_cmp {
98395f7691dSTrond Myklebust 	XID_RB_EQUAL,
98495f7691dSTrond Myklebust 	XID_RB_LEFT,
98595f7691dSTrond Myklebust 	XID_RB_RIGHT,
98695f7691dSTrond Myklebust };
98795f7691dSTrond Myklebust static enum xprt_xid_rb_cmp
98895f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2)
98995f7691dSTrond Myklebust {
99095f7691dSTrond Myklebust 	if (xid1 == xid2)
99195f7691dSTrond Myklebust 		return XID_RB_EQUAL;
99295f7691dSTrond Myklebust 	if ((__force u32)xid1 < (__force u32)xid2)
99395f7691dSTrond Myklebust 		return XID_RB_LEFT;
99495f7691dSTrond Myklebust 	return XID_RB_RIGHT;
99595f7691dSTrond Myklebust }
99695f7691dSTrond Myklebust 
99795f7691dSTrond Myklebust static struct rpc_rqst *
99895f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
99995f7691dSTrond Myklebust {
100095f7691dSTrond Myklebust 	struct rb_node *n = xprt->recv_queue.rb_node;
100195f7691dSTrond Myklebust 	struct rpc_rqst *req;
100295f7691dSTrond Myklebust 
100395f7691dSTrond Myklebust 	while (n != NULL) {
100495f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
100595f7691dSTrond Myklebust 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
100695f7691dSTrond Myklebust 		case XID_RB_LEFT:
100795f7691dSTrond Myklebust 			n = n->rb_left;
100895f7691dSTrond Myklebust 			break;
100995f7691dSTrond Myklebust 		case XID_RB_RIGHT:
101095f7691dSTrond Myklebust 			n = n->rb_right;
101195f7691dSTrond Myklebust 			break;
101295f7691dSTrond Myklebust 		case XID_RB_EQUAL:
101395f7691dSTrond Myklebust 			return req;
101495f7691dSTrond Myklebust 		}
101595f7691dSTrond Myklebust 	}
101695f7691dSTrond Myklebust 	return NULL;
101795f7691dSTrond Myklebust }
101895f7691dSTrond Myklebust 
101995f7691dSTrond Myklebust static void
102095f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
102195f7691dSTrond Myklebust {
102295f7691dSTrond Myklebust 	struct rb_node **p = &xprt->recv_queue.rb_node;
102395f7691dSTrond Myklebust 	struct rb_node *n = NULL;
102495f7691dSTrond Myklebust 	struct rpc_rqst *req;
102595f7691dSTrond Myklebust 
102695f7691dSTrond Myklebust 	while (*p != NULL) {
102795f7691dSTrond Myklebust 		n = *p;
102895f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
102995f7691dSTrond Myklebust 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
103095f7691dSTrond Myklebust 		case XID_RB_LEFT:
103195f7691dSTrond Myklebust 			p = &n->rb_left;
103295f7691dSTrond Myklebust 			break;
103395f7691dSTrond Myklebust 		case XID_RB_RIGHT:
103495f7691dSTrond Myklebust 			p = &n->rb_right;
103595f7691dSTrond Myklebust 			break;
103695f7691dSTrond Myklebust 		case XID_RB_EQUAL:
103795f7691dSTrond Myklebust 			WARN_ON_ONCE(new != req);
103895f7691dSTrond Myklebust 			return;
103995f7691dSTrond Myklebust 		}
104095f7691dSTrond Myklebust 	}
104195f7691dSTrond Myklebust 	rb_link_node(&new->rq_recv, n, p);
104295f7691dSTrond Myklebust 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
104395f7691dSTrond Myklebust }
104495f7691dSTrond Myklebust 
104595f7691dSTrond Myklebust static void
104695f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
104795f7691dSTrond Myklebust {
104895f7691dSTrond Myklebust 	rb_erase(&req->rq_recv, &xprt->recv_queue);
104995f7691dSTrond Myklebust }
105095f7691dSTrond Myklebust 
10519903cd1cSChuck Lever /**
10529903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
10539903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
10549903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
10559903cd1cSChuck Lever  *
105675c84151STrond Myklebust  * Caller holds xprt->queue_lock.
10571da177e4SLinus Torvalds  */
1058d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
10591da177e4SLinus Torvalds {
10608f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
10611da177e4SLinus Torvalds 
106295f7691dSTrond Myklebust 	entry = xprt_request_rb_find(xprt, xid);
106395f7691dSTrond Myklebust 	if (entry != NULL) {
10643705ad64SJeff Layton 		trace_xprt_lookup_rqst(xprt, xid, 0);
10650b87a46bSChuck Lever 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1066262ca07dSChuck Lever 		return entry;
10673705ad64SJeff Layton 	}
106846121cf7SChuck Lever 
106946121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
107046121cf7SChuck Lever 			ntohl(xid));
10713705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
1072262ca07dSChuck Lever 	xprt->stat.bad_xids++;
1073262ca07dSChuck Lever 	return NULL;
10741da177e4SLinus Torvalds }
107512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
10761da177e4SLinus Torvalds 
1077cf9946cdSTrond Myklebust static bool
1078cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
1079cf9946cdSTrond Myklebust {
1080cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
1081cf9946cdSTrond Myklebust }
1082cf9946cdSTrond Myklebust 
1083729749bbSTrond Myklebust /**
1084729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
1085729749bbSTrond Myklebust  * @req: Request to pin
1086729749bbSTrond Myklebust  *
1087729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
10881f7d1c73SChuck Lever  * so should be holding xprt->queue_lock.
1089729749bbSTrond Myklebust  */
1090729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
1091729749bbSTrond Myklebust {
1092cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
1093729749bbSTrond Myklebust }
10949590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
1095729749bbSTrond Myklebust 
1096729749bbSTrond Myklebust /**
1097729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
1098729749bbSTrond Myklebust  * @req: Request to pin
1099729749bbSTrond Myklebust  *
11001f7d1c73SChuck Lever  * Caller should be holding xprt->queue_lock.
1101729749bbSTrond Myklebust  */
1102729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
1103729749bbSTrond Myklebust {
1104cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1105cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
1106cf9946cdSTrond Myklebust 		return;
1107cf9946cdSTrond Myklebust 	}
1108cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
1109cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
1110729749bbSTrond Myklebust }
11119590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1112729749bbSTrond Myklebust 
1113729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
1114729749bbSTrond Myklebust {
1115cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1116729749bbSTrond Myklebust }
1117729749bbSTrond Myklebust 
1118edc81dcdSTrond Myklebust static bool
1119edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
1120edc81dcdSTrond Myklebust {
1121edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1122edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1123edc81dcdSTrond Myklebust }
1124edc81dcdSTrond Myklebust 
1125edc81dcdSTrond Myklebust static bool
1126edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1127edc81dcdSTrond Myklebust {
1128edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1129edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1130edc81dcdSTrond Myklebust }
1131edc81dcdSTrond Myklebust 
1132edc81dcdSTrond Myklebust /**
1133edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
1134edc81dcdSTrond Myklebust  * @task: RPC task
1135edc81dcdSTrond Myklebust  *
1136edc81dcdSTrond Myklebust  */
1137edc81dcdSTrond Myklebust void
1138edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
1139edc81dcdSTrond Myklebust {
1140edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1141edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1142edc81dcdSTrond Myklebust 
1143edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
1144edc81dcdSTrond Myklebust 		return;
114575369089STrond Myklebust 
114675369089STrond Myklebust 	xprt_request_prepare(task->tk_rqstp);
1147edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
1148edc81dcdSTrond Myklebust 
1149edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
1150edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1151edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
1152edc81dcdSTrond Myklebust 
1153edc81dcdSTrond Myklebust 	/* Add request to the receive list */
115495f7691dSTrond Myklebust 	xprt_request_rb_insert(xprt, req);
1155edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1156edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
1157edc81dcdSTrond Myklebust 
1158edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
1159edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
1160edc81dcdSTrond Myklebust }
1161edc81dcdSTrond Myklebust 
1162edc81dcdSTrond Myklebust /**
1163edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1164edc81dcdSTrond Myklebust  * @task: RPC task
1165edc81dcdSTrond Myklebust  *
1166edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
1167edc81dcdSTrond Myklebust  */
1168edc81dcdSTrond Myklebust static void
1169edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
1170edc81dcdSTrond Myklebust {
117195f7691dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
117295f7691dSTrond Myklebust 
1173edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
117495f7691dSTrond Myklebust 		xprt_request_rb_remove(req->rq_xprt, req);
1175edc81dcdSTrond Myklebust }
1176edc81dcdSTrond Myklebust 
1177ecd465eeSChuck Lever /**
1178ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
1179ecd465eeSChuck Lever  * @task: RPC request that recently completed
1180ecd465eeSChuck Lever  *
118175c84151STrond Myklebust  * Caller holds xprt->queue_lock.
1182ecd465eeSChuck Lever  */
1183ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
11841da177e4SLinus Torvalds {
11851570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
11861570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
118795c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1188d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
11891570c1e4SChuck Lever 
11901da177e4SLinus Torvalds 	if (timer) {
11911da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
1192ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
11931570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds }
1196ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
11971da177e4SLinus Torvalds 
11981570c1e4SChuck Lever /**
11991570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
12001570c1e4SChuck Lever  * @task: RPC request that recently completed
12011570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
12021570c1e4SChuck Lever  *
120375c84151STrond Myklebust  * Caller holds xprt->queue_lock.
12041570c1e4SChuck Lever  */
12051570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
12061570c1e4SChuck Lever {
12071570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
1208fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12091da177e4SLinus Torvalds 
1210fda13939STrond Myklebust 	xprt->stat.recvs++;
1211ef759a2eSChuck Lever 
12121e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1213dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1214dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
121543ac3f29STrond Myklebust 	smp_wmb();
1216dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1217edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1218fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
12191da177e4SLinus Torvalds }
122012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
12211da177e4SLinus Torvalds 
122246c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
12231da177e4SLinus Torvalds {
12241da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
12251da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
12261da177e4SLinus Torvalds 
12275d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
12285d00837bSTrond Myklebust 		return;
122946c0ee8bSChuck Lever 
123082476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1231dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
123246c0ee8bSChuck Lever 		if (xprt->ops->timer)
12336a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
12345d00837bSTrond Myklebust 	} else
12355d00837bSTrond Myklebust 		task->tk_status = 0;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12389903cd1cSChuck Lever /**
12398ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_def - wait for reply
12408ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
12418ba6a92dSTrond Myklebust  *
12428ba6a92dSTrond Myklebust  * Set a request's retransmit timeout based on the transport's
12438ba6a92dSTrond Myklebust  * default timeout parameters.  Used by transports that don't adjust
12448ba6a92dSTrond Myklebust  * the retransmit timeout based on round-trip time estimation,
12458ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
12468ba6a92dSTrond Myklebust  */
12478ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task)
12488ba6a92dSTrond Myklebust {
12498ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12508ba6a92dSTrond Myklebust 
12516b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12529e910bffSTrond Myklebust 			xprt_request_timeout(req));
12538ba6a92dSTrond Myklebust }
12548ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
12558ba6a92dSTrond Myklebust 
12568ba6a92dSTrond Myklebust /**
12578ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
12588ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
12598ba6a92dSTrond Myklebust  *
12608ba6a92dSTrond Myklebust  * Set a request's retransmit timeout using the RTT estimator,
12618ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
12628ba6a92dSTrond Myklebust  */
12638ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
12648ba6a92dSTrond Myklebust {
12658ba6a92dSTrond Myklebust 	int timer = task->tk_msg.rpc_proc->p_timer;
12668ba6a92dSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
12678ba6a92dSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
12688ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12698ba6a92dSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
12706b2e6856STrond Myklebust 	unsigned long timeout;
12718ba6a92dSTrond Myklebust 
12726b2e6856STrond Myklebust 	timeout = rpc_calc_rto(rtt, timer);
12736b2e6856STrond Myklebust 	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
12746b2e6856STrond Myklebust 	if (timeout > max_timeout || timeout == 0)
12756b2e6856STrond Myklebust 		timeout = max_timeout;
12766b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12776b2e6856STrond Myklebust 			jiffies + timeout);
12788ba6a92dSTrond Myklebust }
12798ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
12808ba6a92dSTrond Myklebust 
12818ba6a92dSTrond Myklebust /**
12827f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
12837f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
12847f3a1d1eSTrond Myklebust  *
12857f3a1d1eSTrond Myklebust  */
12867f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
12877f3a1d1eSTrond Myklebust {
12887f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
12897f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12907f3a1d1eSTrond Myklebust 
12917f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
12927f3a1d1eSTrond Myklebust 		return;
12937f3a1d1eSTrond Myklebust 	/*
12947f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
12957f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
12967f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
12977f3a1d1eSTrond Myklebust 	 */
12987f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
12997f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
13008ba6a92dSTrond Myklebust 		xprt->ops->wait_for_reply_request(task);
13017f3a1d1eSTrond Myklebust 		/*
13027f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
13037f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
13047f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
13057f3a1d1eSTrond Myklebust 		 */
13067f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
13077f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
13087f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
13097f3a1d1eSTrond Myklebust 	}
13107f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
13117f3a1d1eSTrond Myklebust }
13127f3a1d1eSTrond Myklebust 
1313944b0429STrond Myklebust static bool
1314944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1315944b0429STrond Myklebust {
1316762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1317944b0429STrond Myklebust }
1318944b0429STrond Myklebust 
1319944b0429STrond Myklebust /**
1320944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1321944b0429STrond Myklebust  * @task: pointer to rpc_task
1322944b0429STrond Myklebust  *
1323944b0429STrond Myklebust  * Add a task to the transmission queue.
1324944b0429STrond Myklebust  */
1325944b0429STrond Myklebust void
1326944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1327944b0429STrond Myklebust {
1328918f3c1fSTrond Myklebust 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1329944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1330944b0429STrond Myklebust 
1331944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1332e66721f0STrond Myklebust 		req->rq_bytes_sent = 0;
1333944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
133475891f50STrond Myklebust 		/*
133575891f50STrond Myklebust 		 * Requests that carry congestion control credits are added
133675891f50STrond Myklebust 		 * to the head of the list to avoid starvation issues.
133775891f50STrond Myklebust 		 */
133875891f50STrond Myklebust 		if (req->rq_cong) {
133975891f50STrond Myklebust 			xprt_clear_congestion_window_wait(xprt);
134075891f50STrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
134175891f50STrond Myklebust 				if (pos->rq_cong)
134275891f50STrond Myklebust 					continue;
134375891f50STrond Myklebust 				/* Note: req is added _before_ pos */
134475891f50STrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
134575891f50STrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
134675891f50STrond Myklebust 				goto out;
134775891f50STrond Myklebust 			}
134886aeee0eSTrond Myklebust 		} else if (RPC_IS_SWAPPER(task)) {
134986aeee0eSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
135086aeee0eSTrond Myklebust 				if (pos->rq_cong || pos->rq_bytes_sent)
135186aeee0eSTrond Myklebust 					continue;
135286aeee0eSTrond Myklebust 				if (RPC_IS_SWAPPER(pos->rq_task))
135386aeee0eSTrond Myklebust 					continue;
135486aeee0eSTrond Myklebust 				/* Note: req is added _before_ pos */
135586aeee0eSTrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
135686aeee0eSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
135786aeee0eSTrond Myklebust 				goto out;
135886aeee0eSTrond Myklebust 			}
1359deaa5c96SChuck Lever 		} else if (!req->rq_seqno) {
1360918f3c1fSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1361918f3c1fSTrond Myklebust 				if (pos->rq_task->tk_owner != task->tk_owner)
1362918f3c1fSTrond Myklebust 					continue;
1363918f3c1fSTrond Myklebust 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1364918f3c1fSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit);
1365918f3c1fSTrond Myklebust 				goto out;
1366918f3c1fSTrond Myklebust 			}
136775891f50STrond Myklebust 		}
1368944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1369918f3c1fSTrond Myklebust 		INIT_LIST_HEAD(&req->rq_xmit2);
1370918f3c1fSTrond Myklebust out:
1371d737e5d4STrond Myklebust 		atomic_long_inc(&xprt->xmit_queuelen);
1372944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1373944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1374944b0429STrond Myklebust 	}
1375944b0429STrond Myklebust }
1376944b0429STrond Myklebust 
1377944b0429STrond Myklebust /**
1378944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1379944b0429STrond Myklebust  * @task: pointer to rpc_task
1380944b0429STrond Myklebust  *
1381944b0429STrond Myklebust  * Remove a task from the transmission queue
1382944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1383944b0429STrond Myklebust  */
1384944b0429STrond Myklebust static void
1385944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1386944b0429STrond Myklebust {
1387918f3c1fSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1388918f3c1fSTrond Myklebust 
1389918f3c1fSTrond Myklebust 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1390918f3c1fSTrond Myklebust 		return;
1391918f3c1fSTrond Myklebust 	if (!list_empty(&req->rq_xmit)) {
1392918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit);
1393918f3c1fSTrond Myklebust 		if (!list_empty(&req->rq_xmit2)) {
1394918f3c1fSTrond Myklebust 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1395918f3c1fSTrond Myklebust 					struct rpc_rqst, rq_xmit2);
1396918f3c1fSTrond Myklebust 			list_del(&req->rq_xmit2);
1397918f3c1fSTrond Myklebust 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1398918f3c1fSTrond Myklebust 		}
1399918f3c1fSTrond Myklebust 	} else
1400918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit2);
1401d737e5d4STrond Myklebust 	atomic_long_dec(&req->rq_xprt->xmit_queuelen);
1402944b0429STrond Myklebust }
1403944b0429STrond Myklebust 
1404944b0429STrond Myklebust /**
1405944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1406944b0429STrond Myklebust  * @task: pointer to rpc_task
1407944b0429STrond Myklebust  *
1408944b0429STrond Myklebust  * Remove a task from the transmission queue
1409944b0429STrond Myklebust  */
1410944b0429STrond Myklebust static void
1411944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1412944b0429STrond Myklebust {
1413944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1414944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1415944b0429STrond Myklebust 
1416944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1417944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1418944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1419944b0429STrond Myklebust }
1420944b0429STrond Myklebust 
14217f3a1d1eSTrond Myklebust /**
1422cc204d01STrond Myklebust  * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1423cc204d01STrond Myklebust  * @task: pointer to rpc_task
1424cc204d01STrond Myklebust  *
1425cc204d01STrond Myklebust  * Remove a task from the transmit and receive queues, and ensure that
1426cc204d01STrond Myklebust  * it is not pinned by the receive work item.
1427cc204d01STrond Myklebust  */
1428cc204d01STrond Myklebust void
1429cc204d01STrond Myklebust xprt_request_dequeue_xprt(struct rpc_task *task)
1430cc204d01STrond Myklebust {
1431cc204d01STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
1432cc204d01STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1433cc204d01STrond Myklebust 
1434cc204d01STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1435cc204d01STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1436cc204d01STrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1437cc204d01STrond Myklebust 		spin_lock(&xprt->queue_lock);
1438cc204d01STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1439cc204d01STrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1440cc204d01STrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1441cc204d01STrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1442cc204d01STrond Myklebust 			spin_unlock(&xprt->queue_lock);
1443cc204d01STrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1444cc204d01STrond Myklebust 			spin_lock(&xprt->queue_lock);
1445cc204d01STrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1446cc204d01STrond Myklebust 		}
1447cc204d01STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1448cc204d01STrond Myklebust 	}
1449cc204d01STrond Myklebust }
1450cc204d01STrond Myklebust 
1451cc204d01STrond Myklebust /**
14529d96acbcSTrond Myklebust  * xprt_request_prepare - prepare an encoded request for transport
14539d96acbcSTrond Myklebust  * @req: pointer to rpc_rqst
14549d96acbcSTrond Myklebust  *
14559d96acbcSTrond Myklebust  * Calls into the transport layer to do whatever is needed to prepare
14569d96acbcSTrond Myklebust  * the request for transmission or receive.
14579d96acbcSTrond Myklebust  */
14589d96acbcSTrond Myklebust void
14599d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req)
14609d96acbcSTrond Myklebust {
14619d96acbcSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
14629d96acbcSTrond Myklebust 
14639d96acbcSTrond Myklebust 	if (xprt->ops->prepare_request)
14649d96acbcSTrond Myklebust 		xprt->ops->prepare_request(req);
14659d96acbcSTrond Myklebust }
14669d96acbcSTrond Myklebust 
14679d96acbcSTrond Myklebust /**
1468762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1469762e4e67STrond Myklebust  * @task: pointer to rpc_task
1470762e4e67STrond Myklebust  *
1471762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1472762e4e67STrond Myklebust  */
1473762e4e67STrond Myklebust bool
1474762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1475762e4e67STrond Myklebust {
1476762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1477762e4e67STrond Myklebust }
1478762e4e67STrond Myklebust 
1479762e4e67STrond Myklebust /**
14809903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
14819903cd1cSChuck Lever  * @task: RPC task about to send a request
14829903cd1cSChuck Lever  *
14831da177e4SLinus Torvalds  */
148490051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
14871da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
14881da177e4SLinus Torvalds 
14895f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
14905f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1491944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
14925f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
14935f2f6bd9STrond Myklebust 					task, 0);
14945f2f6bd9STrond Myklebust 		return false;
14955f2f6bd9STrond Myklebust 
14968a19a0b6STrond Myklebust 	}
14975f2f6bd9STrond Myklebust 	return true;
14981da177e4SLinus Torvalds }
14991da177e4SLinus Torvalds 
1500e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
15015e5ce5beSTrond Myklebust {
15027638e0bfSChuck Lever 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
15037638e0bfSChuck Lever 
15047638e0bfSChuck Lever 	xprt_inject_disconnect(xprt);
15057638e0bfSChuck Lever 	xprt_release_write(xprt, task);
15065e5ce5beSTrond Myklebust }
15075e5ce5beSTrond Myklebust 
15089903cd1cSChuck Lever /**
150989f90fe1STrond Myklebust  * xprt_request_transmit - send an RPC request on a transport
151089f90fe1STrond Myklebust  * @req: pointer to request to transmit
151189f90fe1STrond Myklebust  * @snd_task: RPC task that owns the transport lock
15129903cd1cSChuck Lever  *
151389f90fe1STrond Myklebust  * This performs the transmission of a single request.
151489f90fe1STrond Myklebust  * Note that if the request is not the same as snd_task, then it
151589f90fe1STrond Myklebust  * does need to be pinned.
151689f90fe1STrond Myklebust  * Returns '0' on success.
15179903cd1cSChuck Lever  */
151889f90fe1STrond Myklebust static int
151989f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
15201da177e4SLinus Torvalds {
15211da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
152289f90fe1STrond Myklebust 	struct rpc_task *task = req->rq_task;
152390d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1524dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1525ff699ea8SChuck Lever 	int status;
15261da177e4SLinus Torvalds 
1527edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
152889f90fe1STrond Myklebust 		if (xprt_request_data_received(task)) {
152989f90fe1STrond Myklebust 			status = 0;
1530944b0429STrond Myklebust 			goto out_dequeue;
153189f90fe1STrond Myklebust 		}
15323021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1533edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
153489f90fe1STrond Myklebust 			status = -EBADMSG;
1535944b0429STrond Myklebust 			goto out_dequeue;
15363021a5bbSTrond Myklebust 		}
1537ae67bd38STrond Myklebust 		if (RPC_SIGNALLED(task)) {
1538ae67bd38STrond Myklebust 			status = -ERESTARTSYS;
1539ae67bd38STrond Myklebust 			goto out_dequeue;
1540ae67bd38STrond Myklebust 		}
15411da177e4SLinus Torvalds 	}
15421da177e4SLinus Torvalds 
1543dcbbeda8STrond Myklebust 	/*
1544dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1545dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1546dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1547dcbbeda8STrond Myklebust 	 */
1548dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1549dcbbeda8STrond Myklebust 
1550c509f15aSChuck Lever 	trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
155190d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1552adfa7144STrond Myklebust 	status = xprt->ops->send_request(req);
1553c8485e4dSTrond Myklebust 	if (status != 0) {
1554dcbbeda8STrond Myklebust 		req->rq_ntrans--;
15550c77668dSChuck Lever 		trace_xprt_transmit(req, status);
155689f90fe1STrond Myklebust 		return status;
1557c8485e4dSTrond Myklebust 	}
15587ebbbc6eSTrond Myklebust 
1559e936a597SChuck Lever 	if (is_retrans) {
1560dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1561e936a597SChuck Lever 		trace_xprt_retransmit(req);
1562e936a597SChuck Lever 	}
1563dcbbeda8STrond Myklebust 
15644a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1565c8485e4dSTrond Myklebust 
1566468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1567b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
1568262ca07dSChuck Lever 
1569262ca07dSChuck Lever 	xprt->stat.sends++;
1570262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1571262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
157215a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
157315a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1574b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
157590d91b0cSTrond Myklebust 
157690d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1577944b0429STrond Myklebust out_dequeue:
15780c77668dSChuck Lever 	trace_xprt_transmit(req, status);
1579944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
158089f90fe1STrond Myklebust 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
158189f90fe1STrond Myklebust 	return status;
158289f90fe1STrond Myklebust }
158389f90fe1STrond Myklebust 
158489f90fe1STrond Myklebust /**
158589f90fe1STrond Myklebust  * xprt_transmit - send an RPC request on a transport
158689f90fe1STrond Myklebust  * @task: controlling RPC task
158789f90fe1STrond Myklebust  *
158889f90fe1STrond Myklebust  * Attempts to drain the transmit queue. On exit, either the transport
158989f90fe1STrond Myklebust  * signalled an error that needs to be handled before transmission can
159089f90fe1STrond Myklebust  * resume, or @task finished transmitting, and detected that it already
159189f90fe1STrond Myklebust  * received a reply.
159289f90fe1STrond Myklebust  */
159389f90fe1STrond Myklebust void
159489f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task)
159589f90fe1STrond Myklebust {
159689f90fe1STrond Myklebust 	struct rpc_rqst *next, *req = task->tk_rqstp;
159789f90fe1STrond Myklebust 	struct rpc_xprt	*xprt = req->rq_xprt;
15986f9f1728SChuck Lever 	int counter, status;
159989f90fe1STrond Myklebust 
160089f90fe1STrond Myklebust 	spin_lock(&xprt->queue_lock);
16016f9f1728SChuck Lever 	counter = 0;
160289f90fe1STrond Myklebust 	while (!list_empty(&xprt->xmit_queue)) {
16036f9f1728SChuck Lever 		if (++counter == 20)
16046f9f1728SChuck Lever 			break;
160589f90fe1STrond Myklebust 		next = list_first_entry(&xprt->xmit_queue,
160689f90fe1STrond Myklebust 				struct rpc_rqst, rq_xmit);
160789f90fe1STrond Myklebust 		xprt_pin_rqst(next);
160889f90fe1STrond Myklebust 		spin_unlock(&xprt->queue_lock);
160989f90fe1STrond Myklebust 		status = xprt_request_transmit(next, task);
161089f90fe1STrond Myklebust 		if (status == -EBADMSG && next != req)
161189f90fe1STrond Myklebust 			status = 0;
161289f90fe1STrond Myklebust 		spin_lock(&xprt->queue_lock);
161389f90fe1STrond Myklebust 		xprt_unpin_rqst(next);
161489f90fe1STrond Myklebust 		if (status == 0) {
161589f90fe1STrond Myklebust 			if (!xprt_request_data_received(task) ||
161689f90fe1STrond Myklebust 			    test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
161789f90fe1STrond Myklebust 				continue;
1618c544577dSTrond Myklebust 		} else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
161989f90fe1STrond Myklebust 			task->tk_status = status;
162089f90fe1STrond Myklebust 		break;
162189f90fe1STrond Myklebust 	}
162289f90fe1STrond Myklebust 	spin_unlock(&xprt->queue_lock);
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
1625e86be3a0STrond Myklebust static void xprt_complete_request_init(struct rpc_task *task)
1626e86be3a0STrond Myklebust {
1627e86be3a0STrond Myklebust 	if (task->tk_rqstp)
1628e86be3a0STrond Myklebust 		xprt_request_init(task);
1629e86be3a0STrond Myklebust }
1630e86be3a0STrond Myklebust 
1631e86be3a0STrond Myklebust void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1632ba60eb25STrond Myklebust {
1633ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1634e86be3a0STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
1635ba60eb25STrond Myklebust }
1636e86be3a0STrond Myklebust EXPORT_SYMBOL_GPL(xprt_add_backlog);
1637ba60eb25STrond Myklebust 
1638e877a88dSNeilBrown static bool __xprt_set_rq(struct rpc_task *task, void *data)
1639ba60eb25STrond Myklebust {
1640e877a88dSNeilBrown 	struct rpc_rqst *req = data;
1641e877a88dSNeilBrown 
1642e877a88dSNeilBrown 	if (task->tk_rqstp == NULL) {
1643e877a88dSNeilBrown 		memset(req, 0, sizeof(*req));	/* mark unused */
1644e877a88dSNeilBrown 		task->tk_rqstp = req;
1645e877a88dSNeilBrown 		return true;
1646e877a88dSNeilBrown 	}
1647e877a88dSNeilBrown 	return false;
1648e877a88dSNeilBrown }
1649e877a88dSNeilBrown 
1650e86be3a0STrond Myklebust bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
1651e877a88dSNeilBrown {
1652e877a88dSNeilBrown 	if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
1653ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1654e877a88dSNeilBrown 		return false;
1655e877a88dSNeilBrown 	}
1656e877a88dSNeilBrown 	return true;
1657ba60eb25STrond Myklebust }
1658e86be3a0STrond Myklebust EXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
1659ba60eb25STrond Myklebust 
1660ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1661ba60eb25STrond Myklebust {
1662ba60eb25STrond Myklebust 	bool ret = false;
1663ba60eb25STrond Myklebust 
1664ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1665ba60eb25STrond Myklebust 		goto out;
1666ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1667ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1668e86be3a0STrond Myklebust 		xprt_add_backlog(xprt, task);
1669ba60eb25STrond Myklebust 		ret = true;
1670ba60eb25STrond Myklebust 	}
1671ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1672ba60eb25STrond Myklebust out:
1673ba60eb25STrond Myklebust 	return ret;
1674ba60eb25STrond Myklebust }
1675ba60eb25STrond Myklebust 
167692ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1677d9ba131dSTrond Myklebust {
1678d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1679d9ba131dSTrond Myklebust 
1680ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1681d9ba131dSTrond Myklebust 		goto out;
1682ff699ea8SChuck Lever 	++xprt->num_reqs;
168392ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
168492ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
168592ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1686d9ba131dSTrond Myklebust 	if (req != NULL)
1687d9ba131dSTrond Myklebust 		goto out;
1688ff699ea8SChuck Lever 	--xprt->num_reqs;
1689d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1690d9ba131dSTrond Myklebust out:
1691d9ba131dSTrond Myklebust 	return req;
1692d9ba131dSTrond Myklebust }
1693d9ba131dSTrond Myklebust 
1694d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1695d9ba131dSTrond Myklebust {
1696ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1697ff699ea8SChuck Lever 		--xprt->num_reqs;
1698d9ba131dSTrond Myklebust 		kfree(req);
1699d9ba131dSTrond Myklebust 		return true;
1700d9ba131dSTrond Myklebust 	}
1701d9ba131dSTrond Myklebust 	return false;
1702d9ba131dSTrond Myklebust }
1703d9ba131dSTrond Myklebust 
1704f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
17051da177e4SLinus Torvalds {
1706d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
17071da177e4SLinus Torvalds 
1708f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
17091da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1710d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1711d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1712d9ba131dSTrond Myklebust 		goto out_init_req;
1713d9ba131dSTrond Myklebust 	}
171492ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1715d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1716d9ba131dSTrond Myklebust 		goto out_init_req;
1717d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1718d9ba131dSTrond Myklebust 	case -ENOMEM:
1719d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1720d9ba131dSTrond Myklebust 				"failed! Retrying\n");
17211afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1722d9ba131dSTrond Myklebust 		break;
1723d9ba131dSTrond Myklebust 	case -EAGAIN:
1724ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1725d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1726df561f66SGustavo A. R. Silva 		fallthrough;
17271afeaf5cSTrond Myklebust 	default:
1728d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
17291afeaf5cSTrond Myklebust 	}
1730f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1731d9ba131dSTrond Myklebust 	return;
1732d9ba131dSTrond Myklebust out_init_req:
1733ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1734ff699ea8SChuck Lever 				     xprt->num_reqs);
173537ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
173637ac86c3SChuck Lever 
1737d9ba131dSTrond Myklebust 	task->tk_status = 0;
17381da177e4SLinus Torvalds 	task->tk_rqstp = req;
17391da177e4SLinus Torvalds }
1740f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1741f39c1bfbSTrond Myklebust 
1742a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1743ee5ebe85STrond Myklebust {
1744ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1745e877a88dSNeilBrown 	if (!xprt_wake_up_backlog(xprt, req) &&
1746e877a88dSNeilBrown 	    !xprt_dynamic_free_slot(xprt, req)) {
1747c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1748ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1749c25573b5STrond Myklebust 	}
1750ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1751ee5ebe85STrond Myklebust }
1752a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1753ee5ebe85STrond Myklebust 
175421de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
175521de0a95STrond Myklebust {
175621de0a95STrond Myklebust 	struct rpc_rqst *req;
175721de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
175821de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
175921de0a95STrond Myklebust 		list_del(&req->rq_list);
176021de0a95STrond Myklebust 		kfree(req);
176121de0a95STrond Myklebust 	}
176221de0a95STrond Myklebust }
176321de0a95STrond Myklebust 
1764572caba4SOlga Kornievskaia static DEFINE_IDA(rpc_xprt_ids);
1765572caba4SOlga Kornievskaia 
1766572caba4SOlga Kornievskaia void xprt_cleanup_ids(void)
1767572caba4SOlga Kornievskaia {
1768572caba4SOlga Kornievskaia 	ida_destroy(&rpc_xprt_ids);
1769572caba4SOlga Kornievskaia }
1770572caba4SOlga Kornievskaia 
1771572caba4SOlga Kornievskaia static int xprt_alloc_id(struct rpc_xprt *xprt)
1772572caba4SOlga Kornievskaia {
1773572caba4SOlga Kornievskaia 	int id;
1774572caba4SOlga Kornievskaia 
1775572caba4SOlga Kornievskaia 	id = ida_simple_get(&rpc_xprt_ids, 0, 0, GFP_KERNEL);
1776572caba4SOlga Kornievskaia 	if (id < 0)
1777572caba4SOlga Kornievskaia 		return id;
1778572caba4SOlga Kornievskaia 
1779572caba4SOlga Kornievskaia 	xprt->id = id;
1780572caba4SOlga Kornievskaia 	return 0;
1781572caba4SOlga Kornievskaia }
1782572caba4SOlga Kornievskaia 
1783572caba4SOlga Kornievskaia static void xprt_free_id(struct rpc_xprt *xprt)
1784572caba4SOlga Kornievskaia {
1785572caba4SOlga Kornievskaia 	ida_simple_remove(&rpc_xprt_ids, xprt->id);
1786572caba4SOlga Kornievskaia }
1787572caba4SOlga Kornievskaia 
1788d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1789d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1790d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1791bd1722d4SPavel Emelyanov {
1792bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
179321de0a95STrond Myklebust 	struct rpc_rqst *req;
179421de0a95STrond Myklebust 	int i;
1795bd1722d4SPavel Emelyanov 
1796bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1797bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1798bd1722d4SPavel Emelyanov 		goto out;
1799bd1722d4SPavel Emelyanov 
1800572caba4SOlga Kornievskaia 	xprt_alloc_id(xprt);
180121de0a95STrond Myklebust 	xprt_init(xprt, net);
180221de0a95STrond Myklebust 
180321de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
180421de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
180521de0a95STrond Myklebust 		if (!req)
18068313164cSwangweidong 			goto out_free;
180721de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
180821de0a95STrond Myklebust 	}
1809d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1810d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1811d9ba131dSTrond Myklebust 	else
181221de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1813d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1814ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1815bd1722d4SPavel Emelyanov 
1816bd1722d4SPavel Emelyanov 	return xprt;
1817bd1722d4SPavel Emelyanov 
1818bd1722d4SPavel Emelyanov out_free:
181921de0a95STrond Myklebust 	xprt_free(xprt);
1820bd1722d4SPavel Emelyanov out:
1821bd1722d4SPavel Emelyanov 	return NULL;
1822bd1722d4SPavel Emelyanov }
1823bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1824bd1722d4SPavel Emelyanov 
1825e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1826e204e621SPavel Emelyanov {
182737aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
182821de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1829572caba4SOlga Kornievskaia 	xprt_free_id(xprt);
1830587bc725SOlga Kornievskaia 	rpc_sysfs_xprt_destroy(xprt);
1831fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1832e204e621SPavel Emelyanov }
1833e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1834e204e621SPavel Emelyanov 
1835902c5887STrond Myklebust static void
1836902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1837902c5887STrond Myklebust {
1838902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1839902c5887STrond Myklebust }
1840902c5887STrond Myklebust 
18419dc6edcfSTrond Myklebust static __be32
18429dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
18439dc6edcfSTrond Myklebust {
18449dc6edcfSTrond Myklebust 	__be32 xid;
18459dc6edcfSTrond Myklebust 
18469dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
18479dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
18489dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
18499dc6edcfSTrond Myklebust 	return xid;
18509dc6edcfSTrond Myklebust }
18519dc6edcfSTrond Myklebust 
18529dc6edcfSTrond Myklebust static void
18539dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
18549dc6edcfSTrond Myklebust {
18559dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
18569dc6edcfSTrond Myklebust }
18579dc6edcfSTrond Myklebust 
18589dc6edcfSTrond Myklebust static void
18599dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
18609dc6edcfSTrond Myklebust {
18619dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
18629dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
18639dc6edcfSTrond Myklebust 
18649dc6edcfSTrond Myklebust 	req->rq_task	= task;
18659dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
18669dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
18679dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1868902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
18699dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
18709dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
18719dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
18729dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
187371700bb9STrond Myklebust 	req->rq_snd_buf.bvec = NULL;
187471700bb9STrond Myklebust 	req->rq_rcv_buf.bvec = NULL;
18759dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
1876da953063STrond Myklebust 	xprt_init_majortimeo(task, req);
187709d2ba0cSChuck Lever 
187809d2ba0cSChuck Lever 	trace_xprt_reserve(req);
18799dc6edcfSTrond Myklebust }
18809dc6edcfSTrond Myklebust 
18819dc6edcfSTrond Myklebust static void
18829dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
18839dc6edcfSTrond Myklebust {
18849dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
18859dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
18869dc6edcfSTrond Myklebust 		xprt_request_init(task);
18879dc6edcfSTrond Myklebust }
18889dc6edcfSTrond Myklebust 
18899903cd1cSChuck Lever /**
18909903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
18919903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
18929903cd1cSChuck Lever  *
1893ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1894ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
18959903cd1cSChuck Lever  * backlog queue.
18969903cd1cSChuck Lever  */
18979903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
18981da177e4SLinus Torvalds {
1899fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
19001da177e4SLinus Torvalds 
190143cedbf0STrond Myklebust 	task->tk_status = 0;
190243cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
190343cedbf0STrond Myklebust 		return;
190443cedbf0STrond Myklebust 
190543cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1906ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
19079dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1908ba60eb25STrond Myklebust }
1909ba60eb25STrond Myklebust 
1910ba60eb25STrond Myklebust /**
1911ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1912ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1913ba60eb25STrond Myklebust  *
1914ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1915ba60eb25STrond Myklebust  * backlog queue.
1916ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1917ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1918ba60eb25STrond Myklebust  */
1919ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1920ba60eb25STrond Myklebust {
1921fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1922ba60eb25STrond Myklebust 
1923ba60eb25STrond Myklebust 	task->tk_status = 0;
1924e86be3a0STrond Myklebust 	if (task->tk_rqstp != NULL)
1925ba60eb25STrond Myklebust 		return;
1926ba60eb25STrond Myklebust 
1927ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
19289dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19319903cd1cSChuck Lever /**
19329903cd1cSChuck Lever  * xprt_release - release an RPC request slot
19339903cd1cSChuck Lever  * @task: task which is finished with the slot
19349903cd1cSChuck Lever  *
19351da177e4SLinus Torvalds  */
19369903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
19371da177e4SLinus Torvalds {
193855ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
193987ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
19401da177e4SLinus Torvalds 
194187ed5003STrond Myklebust 	if (req == NULL) {
194287ed5003STrond Myklebust 		if (task->tk_client) {
1943fb43d172STrond Myklebust 			xprt = task->tk_xprt;
194487ed5003STrond Myklebust 			xprt_release_write(xprt, task);
194587ed5003STrond Myklebust 		}
19461da177e4SLinus Torvalds 		return;
194787ed5003STrond Myklebust 	}
194855ae1aabSRicardo Labiaga 
194955ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
1950cc204d01STrond Myklebust 	xprt_request_dequeue_xprt(task);
1951b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
195249e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1953a58dd398SChuck Lever 	if (xprt->ops->release_request)
1954a58dd398SChuck Lever 		xprt->ops->release_request(task);
1955ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
1956b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
1957ee5ebe85STrond Myklebust 	if (req->rq_buffer)
19583435c74aSChuck Lever 		xprt->ops->buf_free(task);
19599d96acbcSTrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
19600472e476STrond Myklebust 	xdr_free_bvec(&req->rq_snd_buf);
1961a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1962a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
1963ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1964ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
196555ae1aabSRicardo Labiaga 
1966e877a88dSNeilBrown 	task->tk_rqstp = NULL;
1967ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1968a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1969ee5ebe85STrond Myklebust 	else
1970c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
1973902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1974902c5887STrond Myklebust void
1975902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1976902c5887STrond Myklebust {
1977902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1978902c5887STrond Myklebust 
1979902c5887STrond Myklebust 	task->tk_rqstp = req;
1980902c5887STrond Myklebust 	req->rq_task = task;
1981902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1982902c5887STrond Myklebust 	/*
1983902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1984902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1985902c5887STrond Myklebust 	 */
1986902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1987902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1988902c5887STrond Myklebust }
1989902c5887STrond Myklebust #endif
1990902c5887STrond Myklebust 
199121de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1992c2866763SChuck Lever {
199330c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1994c2866763SChuck Lever 
1995c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1996c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
199775c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1998c2866763SChuck Lever 
1999c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
200095f7691dSTrond Myklebust 	xprt->recv_queue = RB_ROOT;
2001944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
20029e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2003f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
2004f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
20059e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
200680b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
2007f9acac1aSRicardo Labiaga 
2008c2866763SChuck Lever 	xprt->last_used = jiffies;
2009c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
2010a509050bSChuck Lever 	xprt->bind_index = 0;
2011c2866763SChuck Lever 
2012c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
2013c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
201479c99152STrond Myklebust 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
2015c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
2016c2866763SChuck Lever 
2017c2866763SChuck Lever 	xprt_init_xid(xprt);
2018c2866763SChuck Lever 
201921de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
20208d9266ffSTrond Myklebust }
20218d9266ffSTrond Myklebust 
20228d9266ffSTrond Myklebust /**
20238d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
20248d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
20258d9266ffSTrond Myklebust  *
20268d9266ffSTrond Myklebust  */
20278d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
20288d9266ffSTrond Myklebust {
20298d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
20309bccd264STrond Myklebust 	const struct xprt_class *t;
20318d9266ffSTrond Myklebust 
20329bccd264STrond Myklebust 	t = xprt_class_find_by_ident(args->ident);
20339bccd264STrond Myklebust 	if (!t) {
20343c45ddf8SChuck Lever 		dprintk("RPC: transport (%d) not supported\n", args->ident);
20358d9266ffSTrond Myklebust 		return ERR_PTR(-EIO);
20369bccd264STrond Myklebust 	}
20378d9266ffSTrond Myklebust 
20388d9266ffSTrond Myklebust 	xprt = t->setup(args);
20399bccd264STrond Myklebust 	xprt_class_release(t);
20409bccd264STrond Myklebust 
2041911813d7SChuck Lever 	if (IS_ERR(xprt))
204221de0a95STrond Myklebust 		goto out;
204333d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
204433d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
204521de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
204621de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
2047502980e8SAnna Schumaker 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
204821de0a95STrond Myklebust 	else
2049ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
20504e0038b6STrond Myklebust 
20514e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
20524e0038b6STrond Myklebust 		xprt_destroy(xprt);
20534e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
20544e0038b6STrond Myklebust 	}
20554e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
20564e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
20574e0038b6STrond Myklebust 		xprt_destroy(xprt);
20584e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
20594e0038b6STrond Myklebust 	}
20604e0038b6STrond Myklebust 
20613f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
2062388f0c77SJeff Layton 
2063911813d7SChuck Lever 	trace_xprt_create(xprt);
206421de0a95STrond Myklebust out:
2065c2866763SChuck Lever 	return xprt;
2066c2866763SChuck Lever }
2067c2866763SChuck Lever 
2068528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
2069528fd354STrond Myklebust {
2070528fd354STrond Myklebust 	struct rpc_xprt *xprt =
2071528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
2072528fd354STrond Myklebust 
2073911813d7SChuck Lever 	trace_xprt_destroy(xprt);
2074911813d7SChuck Lever 
2075528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
2076528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
2077528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
2078528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
2079528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
2080528fd354STrond Myklebust 	kfree(xprt->servername);
2081528fd354STrond Myklebust 	/*
2082669996adSTrond Myklebust 	 * Destroy any existing back channel
2083669996adSTrond Myklebust 	 */
2084669996adSTrond Myklebust 	xprt_destroy_backchannel(xprt, UINT_MAX);
2085669996adSTrond Myklebust 
2086669996adSTrond Myklebust 	/*
2087528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
2088528fd354STrond Myklebust 	 */
2089528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
2090528fd354STrond Myklebust }
2091528fd354STrond Myklebust 
20929903cd1cSChuck Lever /**
20939903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
2094a8de240aSTrond Myklebust  * @xprt: transport to destroy
20959903cd1cSChuck Lever  *
20961da177e4SLinus Torvalds  */
2097a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
20981da177e4SLinus Torvalds {
2099528fd354STrond Myklebust 	/*
2100528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
2101528fd354STrond Myklebust 	 */
210279234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
210379234c3dSTrond Myklebust 
21040065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
2105c8541ecdSChuck Lever 
2106c8541ecdSChuck Lever 	/*
2107528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
2108528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
2109c8541ecdSChuck Lever 	 */
2110528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2111528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
21126b6ca86bSTrond Myklebust }
21131da177e4SLinus Torvalds 
211430c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
211530c5116bSTrond Myklebust {
211630c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
211730c5116bSTrond Myklebust }
211830c5116bSTrond Myklebust 
211930c5116bSTrond Myklebust /**
212030c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
212130c5116bSTrond Myklebust  * @xprt: pointer to the transport
212230c5116bSTrond Myklebust  *
212330c5116bSTrond Myklebust  */
212430c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
212530c5116bSTrond Myklebust {
212630c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
212730c5116bSTrond Myklebust 		return xprt;
212830c5116bSTrond Myklebust 	return NULL;
212930c5116bSTrond Myklebust }
213030c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
213130c5116bSTrond Myklebust 
21326b6ca86bSTrond Myklebust /**
21336b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
21346b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
21356b6ca86bSTrond Myklebust  *
21366b6ca86bSTrond Myklebust  */
21376b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
21386b6ca86bSTrond Myklebust {
213930c5116bSTrond Myklebust 	if (xprt != NULL)
214030c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
21416b6ca86bSTrond Myklebust }
21425d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
2143