xref: /openbmc/linux/net/sunrpc/xprt.c (revision 9e910bff)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/net/sunrpc/xprt.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This is a generic RPC call interface supporting congestion avoidance,
51da177e4SLinus Torvalds  *  and asynchronous calls.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  The interface works like this:
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  -	When a process places a call, it allocates a request slot if
101da177e4SLinus Torvalds  *	one is available. Otherwise, it sleeps on the backlog queue
111da177e4SLinus Torvalds  *	(xprt_reserve).
121da177e4SLinus Torvalds  *  -	Next, the caller puts together the RPC message, stuffs it into
1355aa4f58SChuck Lever  *	the request struct, and calls xprt_transmit().
1455aa4f58SChuck Lever  *  -	xprt_transmit sends the message and installs the caller on the
1555ae1aabSRicardo Labiaga  *	transport's wait list. At the same time, if a reply is expected,
1655ae1aabSRicardo Labiaga  *	it installs a timer that is run after the packet's timeout has
1755ae1aabSRicardo Labiaga  *	expired.
181da177e4SLinus Torvalds  *  -	When a packet arrives, the data_ready handler walks the list of
1955aa4f58SChuck Lever  *	pending requests for that transport. If a matching XID is found, the
201da177e4SLinus Torvalds  *	caller is woken up, and the timer removed.
211da177e4SLinus Torvalds  *  -	When no reply arrives within the timeout interval, the timer is
221da177e4SLinus Torvalds  *	fired by the kernel and runs xprt_timer(). It either adjusts the
231da177e4SLinus Torvalds  *	timeout values (minor timeout) or wakes up the caller with a status
241da177e4SLinus Torvalds  *	of -ETIMEDOUT.
251da177e4SLinus Torvalds  *  -	When the caller receives a notification from RPC that a reply arrived,
261da177e4SLinus Torvalds  *	it should release the RPC slot, and process the reply.
271da177e4SLinus Torvalds  *	If the call timed out, it may choose to retry the operation by
281da177e4SLinus Torvalds  *	adjusting the initial timeout value, and simply calling rpc_call
291da177e4SLinus Torvalds  *	again.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Support for async RPC is done through a set of RPC-specific scheduling
321da177e4SLinus Torvalds  *  primitives that `transparently' work for processes as well as async
331da177e4SLinus Torvalds  *  tasks that rely on callbacks.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
3655aa4f58SChuck Lever  *
3755aa4f58SChuck Lever  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
40a246b010SChuck Lever #include <linux/module.h>
41a246b010SChuck Lever 
421da177e4SLinus Torvalds #include <linux/types.h>
43a246b010SChuck Lever #include <linux/interrupt.h>
441da177e4SLinus Torvalds #include <linux/workqueue.h>
45bf3fcf89SChuck Lever #include <linux/net.h>
46ff839970SChuck Lever #include <linux/ktime.h>
471da177e4SLinus Torvalds 
48a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h>
50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
51fda1bfefSTrond Myklebust #include <linux/rcupdate.h>
52a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
531da177e4SLinus Torvalds 
543705ad64SJeff Layton #include <trace/events/sunrpc.h>
553705ad64SJeff Layton 
5655ae1aabSRicardo Labiaga #include "sunrpc.h"
5755ae1aabSRicardo Labiaga 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds  * Local variables
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds 
62f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
631da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
641da177e4SLinus Torvalds #endif
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * Local functions
681da177e4SLinus Torvalds  */
6921de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
7037ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
714e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
721da177e4SLinus Torvalds 
735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7581c098afS\"Talpey, Thomas\ 
769e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
779e910bffSTrond Myklebust {
789e910bffSTrond Myklebust 	unsigned long timeout = jiffies + req->rq_timeout;
799e910bffSTrond Myklebust 
809e910bffSTrond Myklebust 	if (time_before(timeout, req->rq_majortimeo))
819e910bffSTrond Myklebust 		return timeout;
829e910bffSTrond Myklebust 	return req->rq_majortimeo;
839e910bffSTrond Myklebust }
849e910bffSTrond Myklebust 
8512a80469SChuck Lever /**
8681c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
8781c098afS\"Talpey, Thomas\  * @transport: transport to register
8881c098afS\"Talpey, Thomas\  *
8981c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9081c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9181c098afS\"Talpey, Thomas\  *
9281c098afS\"Talpey, Thomas\  * Returns:
9381c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
9481c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
9581c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
9681c098afS\"Talpey, Thomas\  */
9781c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
9881c098afS\"Talpey, Thomas\ {
9981c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10081c098afS\"Talpey, Thomas\ 	int result;
10181c098afS\"Talpey, Thomas\ 
10281c098afS\"Talpey, Thomas\ 	result = -EEXIST;
10381c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
10481c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
10581c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1064fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
10781c098afS\"Talpey, Thomas\ 			goto out;
10881c098afS\"Talpey, Thomas\ 	}
10981c098afS\"Talpey, Thomas\ 
11081c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11181c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11281c098afS\"Talpey, Thomas\ 	       transport->name);
11381c098afS\"Talpey, Thomas\ 	result = 0;
11481c098afS\"Talpey, Thomas\ 
11581c098afS\"Talpey, Thomas\ out:
11681c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
11781c098afS\"Talpey, Thomas\ 	return result;
11881c098afS\"Talpey, Thomas\ }
11981c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12081c098afS\"Talpey, Thomas\ 
12181c098afS\"Talpey, Thomas\ /**
12281c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
12365b6e42cSRandy Dunlap  * @transport: transport to unregister
12481c098afS\"Talpey, Thomas\  *
12581c098afS\"Talpey, Thomas\  * Returns:
12681c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
12781c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
12881c098afS\"Talpey, Thomas\  */
12981c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13081c098afS\"Talpey, Thomas\ {
13181c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13281c098afS\"Talpey, Thomas\ 	int result;
13381c098afS\"Talpey, Thomas\ 
13481c098afS\"Talpey, Thomas\ 	result = 0;
13581c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
13681c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
13781c098afS\"Talpey, Thomas\ 		if (t == transport) {
13881c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
13981c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14081c098afS\"Talpey, Thomas\ 				transport->name);
14181c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14281c098afS\"Talpey, Thomas\ 			goto out;
14381c098afS\"Talpey, Thomas\ 		}
14481c098afS\"Talpey, Thomas\ 	}
14581c098afS\"Talpey, Thomas\ 	result = -ENOENT;
14681c098afS\"Talpey, Thomas\ 
14781c098afS\"Talpey, Thomas\ out:
14881c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
14981c098afS\"Talpey, Thomas\ 	return result;
15081c098afS\"Talpey, Thomas\ }
15181c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15281c098afS\"Talpey, Thomas\ 
15381c098afS\"Talpey, Thomas\ /**
154441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
155441e3e24STom Talpey  * @transport_name: transport to load
156441e3e24STom Talpey  *
157441e3e24STom Talpey  * Returns:
158441e3e24STom Talpey  * 0:		transport successfully loaded
159441e3e24STom Talpey  * -ENOENT:	transport module not available
160441e3e24STom Talpey  */
161441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
162441e3e24STom Talpey {
163441e3e24STom Talpey 	struct xprt_class *t;
164441e3e24STom Talpey 	int result;
165441e3e24STom Talpey 
166441e3e24STom Talpey 	result = 0;
167441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
168441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
169441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
170441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
171441e3e24STom Talpey 			goto out;
172441e3e24STom Talpey 		}
173441e3e24STom Talpey 	}
174441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
175ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
176441e3e24STom Talpey out:
177441e3e24STom Talpey 	return result;
178441e3e24STom Talpey }
179441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
180441e3e24STom Talpey 
181c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
182c544577dSTrond Myklebust {
183c544577dSTrond Myklebust 	xprt->snd_task = NULL;
184c544577dSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
185c544577dSTrond Myklebust 		smp_mb__before_atomic();
186c544577dSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
187c544577dSTrond Myklebust 		smp_mb__after_atomic();
188c544577dSTrond Myklebust 	} else
189c544577dSTrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
190c544577dSTrond Myklebust }
191c544577dSTrond Myklebust 
192441e3e24STom Talpey /**
19312a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
19412a80469SChuck Lever  * @task: task that is requesting access to the transport
195177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
19612a80469SChuck Lever  *
19712a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
19812a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
19912a80469SChuck Lever  * is provided.
2001da177e4SLinus Torvalds  */
20143cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
2021da177e4SLinus Torvalds {
20312a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
20412a80469SChuck Lever 
20512a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
20612a80469SChuck Lever 		if (task == xprt->snd_task)
20712a80469SChuck Lever 			return 1;
20812a80469SChuck Lever 		goto out_sleep;
20912a80469SChuck Lever 	}
210c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
211c544577dSTrond Myklebust 		goto out_unlock;
21212a80469SChuck Lever 	xprt->snd_task = task;
2134d4a76f3Sj223yang@asset.uwaterloo.ca 
21412a80469SChuck Lever 	return 1;
21512a80469SChuck Lever 
216c544577dSTrond Myklebust out_unlock:
217c544577dSTrond Myklebust 	xprt_clear_locked(xprt);
21812a80469SChuck Lever out_sleep:
21946121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
22012a80469SChuck Lever 			task->tk_pid, xprt);
22112a80469SChuck Lever 	task->tk_status = -EAGAIN;
2226b2e6856STrond Myklebust 	if  (RPC_IS_SOFT(task))
2236b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2249e910bffSTrond Myklebust 				xprt_request_timeout(req));
2256b2e6856STrond Myklebust 	else
22679c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
22712a80469SChuck Lever 	return 0;
22812a80469SChuck Lever }
22912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
23012a80469SChuck Lever 
23175891f50STrond Myklebust static bool
23275891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
23375891f50STrond Myklebust {
23475891f50STrond Myklebust 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
23575891f50STrond Myklebust }
23675891f50STrond Myklebust 
23775891f50STrond Myklebust static void
23875891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
23975891f50STrond Myklebust {
24075891f50STrond Myklebust 	if (!list_empty(&xprt->xmit_queue)) {
24175891f50STrond Myklebust 		/* Peek at head of queue to see if it can make progress */
24275891f50STrond Myklebust 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
24375891f50STrond Myklebust 					rq_xmit)->rq_cong)
24475891f50STrond Myklebust 			return;
24575891f50STrond Myklebust 	}
24675891f50STrond Myklebust 	set_bit(XPRT_CWND_WAIT, &xprt->state);
24775891f50STrond Myklebust }
24875891f50STrond Myklebust 
24975891f50STrond Myklebust static void
25075891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
25175891f50STrond Myklebust {
25275891f50STrond Myklebust 	if (!RPCXPRT_CONGESTED(xprt))
25375891f50STrond Myklebust 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
25475891f50STrond Myklebust }
25575891f50STrond Myklebust 
25612a80469SChuck Lever /*
25712a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
25812a80469SChuck Lever  * @task: task that is requesting access to the transport
25912a80469SChuck Lever  *
26012a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
26112a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
26212a80469SChuck Lever  * woken up and given access to the transport.
26375891f50STrond Myklebust  * Note that the lock is only granted if we know there are free slots.
26412a80469SChuck Lever  */
26543cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
26612a80469SChuck Lever {
2671da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
2681da177e4SLinus Torvalds 
2692226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2701da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2711da177e4SLinus Torvalds 			return 1;
2721da177e4SLinus Torvalds 		goto out_sleep;
2731da177e4SLinus Torvalds 	}
27443cedbf0STrond Myklebust 	if (req == NULL) {
27543cedbf0STrond Myklebust 		xprt->snd_task = task;
27643cedbf0STrond Myklebust 		return 1;
27743cedbf0STrond Myklebust 	}
278c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
279c544577dSTrond Myklebust 		goto out_unlock;
28075891f50STrond Myklebust 	if (!xprt_need_congestion_window_wait(xprt)) {
2811da177e4SLinus Torvalds 		xprt->snd_task = task;
2821da177e4SLinus Torvalds 		return 1;
2831da177e4SLinus Torvalds 	}
284c544577dSTrond Myklebust out_unlock:
285632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2861da177e4SLinus Torvalds out_sleep:
28746121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2881da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
2896b2e6856STrond Myklebust 	if (RPC_IS_SOFT(task))
2906b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2919e910bffSTrond Myklebust 				xprt_request_timeout(req));
2926b2e6856STrond Myklebust 	else
29379c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds }
29612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2971da177e4SLinus Torvalds 
29812a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	int retval;
3011da177e4SLinus Torvalds 
302bd79bc57STrond Myklebust 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
303bd79bc57STrond Myklebust 		return 1;
3044a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
30543cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
3064a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3071da177e4SLinus Torvalds 	return retval;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
310961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
3111da177e4SLinus Torvalds {
312961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
31349e9a890SChuck Lever 
31449e9a890SChuck Lever 	xprt->snd_task = task;
315961a828dSTrond Myklebust 	return true;
316961a828dSTrond Myklebust }
317961a828dSTrond Myklebust 
318961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
319961a828dSTrond Myklebust {
320961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
32149e9a890SChuck Lever 		return;
322c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
323c544577dSTrond Myklebust 		goto out_unlock;
324f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
325f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
326961a828dSTrond Myklebust 		return;
327c544577dSTrond Myklebust out_unlock:
328632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
32949e9a890SChuck Lever }
33049e9a890SChuck Lever 
331961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
332961a828dSTrond Myklebust {
333961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
334961a828dSTrond Myklebust 		return;
335c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
336c544577dSTrond Myklebust 		goto out_unlock;
33775891f50STrond Myklebust 	if (xprt_need_congestion_window_wait(xprt))
338961a828dSTrond Myklebust 		goto out_unlock;
339f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
34075891f50STrond Myklebust 				__xprt_lock_write_func, xprt))
341961a828dSTrond Myklebust 		return;
3421da177e4SLinus Torvalds out_unlock:
343632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
34649e9a890SChuck Lever /**
34749e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
34849e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
34949e9a890SChuck Lever  * @task: task that is releasing access to the transport
35049e9a890SChuck Lever  *
35149e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3521da177e4SLinus Torvalds  */
35349e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
356632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3571da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds }
36012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3611da177e4SLinus Torvalds 
36249e9a890SChuck Lever /**
36349e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
36449e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
36549e9a890SChuck Lever  * @task: task that is releasing access to the transport
36649e9a890SChuck Lever  *
36749e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
36849e9a890SChuck Lever  * transport if the transport's congestion window allows it.
36949e9a890SChuck Lever  */
37049e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
37149e9a890SChuck Lever {
37249e9a890SChuck Lever 	if (xprt->snd_task == task) {
373632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
37449e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
37549e9a890SChuck Lever 	}
37649e9a890SChuck Lever }
37712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
37849e9a890SChuck Lever 
37949e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3801da177e4SLinus Torvalds {
381bd79bc57STrond Myklebust 	if (xprt->snd_task != task)
382bd79bc57STrond Myklebust 		return;
3834a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
38449e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3854a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /*
3891da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3901da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds static int
39375891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	if (req->rq_cong)
3961da177e4SLinus Torvalds 		return 1;
39746121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
39875891f50STrond Myklebust 			req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
39975891f50STrond Myklebust 	if (RPCXPRT_CONGESTED(xprt)) {
40075891f50STrond Myklebust 		xprt_set_congestion_window_wait(xprt);
4011da177e4SLinus Torvalds 		return 0;
40275891f50STrond Myklebust 	}
4031da177e4SLinus Torvalds 	req->rq_cong = 1;
4041da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4051da177e4SLinus Torvalds 	return 1;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds /*
4091da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4101da177e4SLinus Torvalds  * that has been sleeping due to congestion
4111da177e4SLinus Torvalds  */
4121da177e4SLinus Torvalds static void
4131da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	if (!req->rq_cong)
4161da177e4SLinus Torvalds 		return;
4171da177e4SLinus Torvalds 	req->rq_cong = 0;
4181da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
41975891f50STrond Myklebust 	xprt_test_and_clear_congestion_window_wait(xprt);
42049e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
42346c0ee8bSChuck Lever /**
42475891f50STrond Myklebust  * xprt_request_get_cong - Request congestion control credits
42575891f50STrond Myklebust  * @xprt: pointer to transport
42675891f50STrond Myklebust  * @req: pointer to RPC request
42775891f50STrond Myklebust  *
42875891f50STrond Myklebust  * Useful for transports that require congestion control.
42975891f50STrond Myklebust  */
43075891f50STrond Myklebust bool
43175891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
43275891f50STrond Myklebust {
43375891f50STrond Myklebust 	bool ret = false;
43475891f50STrond Myklebust 
43575891f50STrond Myklebust 	if (req->rq_cong)
43675891f50STrond Myklebust 		return true;
43775891f50STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
43875891f50STrond Myklebust 	ret = __xprt_get_cong(xprt, req) != 0;
43975891f50STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
44075891f50STrond Myklebust 	return ret;
44175891f50STrond Myklebust }
44275891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong);
44375891f50STrond Myklebust 
44475891f50STrond Myklebust /**
445a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
446a58dd398SChuck Lever  * @task: RPC request that recently completed
447a58dd398SChuck Lever  *
448a58dd398SChuck Lever  * Useful for transports that require congestion control.
449a58dd398SChuck Lever  */
450a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
451a58dd398SChuck Lever {
452a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
453a4f0835cSTrond Myklebust 
454a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
455a58dd398SChuck Lever }
45612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
457a58dd398SChuck Lever 
45875891f50STrond Myklebust /*
45975891f50STrond Myklebust  * Clear the congestion window wait flag and wake up the next
46075891f50STrond Myklebust  * entry on xprt->sending
46175891f50STrond Myklebust  */
46275891f50STrond Myklebust static void
46375891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
46475891f50STrond Myklebust {
46575891f50STrond Myklebust 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
46675891f50STrond Myklebust 		spin_lock_bh(&xprt->transport_lock);
46775891f50STrond Myklebust 		__xprt_lock_write_next_cong(xprt);
46875891f50STrond Myklebust 		spin_unlock_bh(&xprt->transport_lock);
46975891f50STrond Myklebust 	}
47075891f50STrond Myklebust }
47175891f50STrond Myklebust 
472a58dd398SChuck Lever /**
47346c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4746a24dfb6STrond Myklebust  * @xprt: pointer to xprt
47546c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
47646c0ee8bSChuck Lever  * @result: result code of completed RPC request
47746c0ee8bSChuck Lever  *
4784f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4794f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4804f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4814f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4824f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4834f4cf5adSChuck Lever  *
4844f4cf5adSChuck Lever  *	-	a reply is received and
4854f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4864f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4871da177e4SLinus Torvalds  */
4886a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4891da177e4SLinus Torvalds {
49046c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
49146c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4941da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4951da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4961da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4971da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4981da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
49949e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5001da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
5011da177e4SLinus Torvalds 		cwnd >>= 1;
5021da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
5031da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
5061da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
5071da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
50846c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
5091da177e4SLinus Torvalds }
51012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
5111da177e4SLinus Torvalds 
51244fbac22SChuck Lever /**
51344fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
51444fbac22SChuck Lever  * @xprt: transport with waiting tasks
51544fbac22SChuck Lever  * @status: result code to plant in each task before waking it
51644fbac22SChuck Lever  *
51744fbac22SChuck Lever  */
51844fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
51944fbac22SChuck Lever {
52044fbac22SChuck Lever 	if (status < 0)
52144fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
52244fbac22SChuck Lever 	else
52344fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
52444fbac22SChuck Lever }
52512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
52644fbac22SChuck Lever 
527c7b2cae8SChuck Lever /**
528c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
529c544577dSTrond Myklebust  * @xprt: transport
530a9a6b52eSTrond Myklebust  *
531a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
532a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
533a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
534c7b2cae8SChuck Lever  */
535c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
536c7b2cae8SChuck Lever {
537c544577dSTrond Myklebust 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
538c7b2cae8SChuck Lever }
53912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
540c7b2cae8SChuck Lever 
541c544577dSTrond Myklebust static bool
542c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt)
543c544577dSTrond Myklebust {
544c544577dSTrond Myklebust 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
545c544577dSTrond Myklebust 		__xprt_lock_write_next(xprt);
546c544577dSTrond Myklebust 		dprintk("RPC:       write space: waking waiting task on "
547c544577dSTrond Myklebust 				"xprt %p\n", xprt);
548c544577dSTrond Myklebust 		return true;
549c544577dSTrond Myklebust 	}
550c544577dSTrond Myklebust 	return false;
551c544577dSTrond Myklebust }
552c544577dSTrond Myklebust 
553c7b2cae8SChuck Lever /**
554c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
555c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
556c7b2cae8SChuck Lever  *
557c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
558c7b2cae8SChuck Lever  */
559c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt)
560c7b2cae8SChuck Lever {
561c544577dSTrond Myklebust 	bool ret;
562c544577dSTrond Myklebust 
563c544577dSTrond Myklebust 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
564c544577dSTrond Myklebust 		return false;
565c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
566c544577dSTrond Myklebust 	ret = xprt_clear_write_space_locked(xprt);
567c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
568c544577dSTrond Myklebust 	return ret;
569c7b2cae8SChuck Lever }
57012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
571c7b2cae8SChuck Lever 
5721da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5731da177e4SLinus Torvalds {
574ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5771da177e4SLinus Torvalds 	if (to->to_exponential)
5781da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5791da177e4SLinus Torvalds 	else
5801da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
5811da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
5821da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
5831da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5869903cd1cSChuck Lever /**
5879903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
5889903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
5899903cd1cSChuck Lever  *
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
594ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5951da177e4SLinus Torvalds 	int status = 0;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
5981da177e4SLinus Torvalds 		if (to->to_exponential)
5991da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
6001da177e4SLinus Torvalds 		else
6011da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
6021da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
6031da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
6041da177e4SLinus Torvalds 		req->rq_retries++;
6051da177e4SLinus Torvalds 	} else {
6061da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
6071da177e4SLinus Torvalds 		req->rq_retries = 0;
6081da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
6091da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
6104a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
6111da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
6124a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
6131da177e4SLinus Torvalds 		status = -ETIMEDOUT;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
6171da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6181da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 	return status;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
62365f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6241da177e4SLinus Torvalds {
62565f27f38SDavid Howells 	struct rpc_xprt *xprt =
62665f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
627a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
6281da177e4SLinus Torvalds 
62966af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6304876cc77STrond Myklebust 	xprt->ops->close(xprt);
6311da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
63279234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
633a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6369903cd1cSChuck Lever /**
63762da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6389903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6399903cd1cSChuck Lever  *
6401da177e4SLinus Torvalds  */
64162da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6444a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6451da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
646c544577dSTrond Myklebust 	xprt_clear_write_space_locked(xprt);
64727adc785STrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
6484a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6491da177e4SLinus Torvalds }
65062da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6511da177e4SLinus Torvalds 
65266af1e55STrond Myklebust /**
65366af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
65466af1e55STrond Myklebust  * @xprt: transport to disconnect
65566af1e55STrond Myklebust  *
65666af1e55STrond Myklebust  */
65766af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
65866af1e55STrond Myklebust {
65966af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
66066af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
66166af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
66266af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
66366af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
66440a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6650445f92cSTrond Myklebust 	else if (xprt->snd_task)
6660445f92cSTrond Myklebust 		rpc_wake_up_queued_task_set_status(&xprt->pending,
6670445f92cSTrond Myklebust 				xprt->snd_task, -ENOTCONN);
66866af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
66966af1e55STrond Myklebust }
670e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
67166af1e55STrond Myklebust 
6727f3a1d1eSTrond Myklebust static unsigned int
6737f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
6747f3a1d1eSTrond Myklebust {
6757f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
6767f3a1d1eSTrond Myklebust }
6777f3a1d1eSTrond Myklebust 
6787f3a1d1eSTrond Myklebust static bool
6797f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
6807f3a1d1eSTrond Myklebust {
6817f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6827f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
6837f3a1d1eSTrond Myklebust 
6847f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
6857f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
6867f3a1d1eSTrond Myklebust }
6877f3a1d1eSTrond Myklebust 
6887c1d71cfSTrond Myklebust /**
6897c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
6907c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
6917c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
6927c1d71cfSTrond Myklebust  *
6937c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
6947c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
6957c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
6967c1d71cfSTrond Myklebust  * a batch of RPC requests.
6977c1d71cfSTrond Myklebust  *
6987c1d71cfSTrond Myklebust  */
6997c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
7007c1d71cfSTrond Myklebust {
7017c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
7027c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
7037c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
7047c1d71cfSTrond Myklebust 		goto out;
7052c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
7067c1d71cfSTrond Myklebust 		goto out;
7077c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
7087c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
7097c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
71040a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7112a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
7127c1d71cfSTrond Myklebust out:
7137c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
7147c1d71cfSTrond Myklebust }
7157c1d71cfSTrond Myklebust 
716ad3331acSTrond Myklebust static bool
717ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
718ad3331acSTrond Myklebust {
719ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
720ad3331acSTrond Myklebust }
721ad3331acSTrond Myklebust 
722ad3331acSTrond Myklebust static void
723ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
724ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
725ad3331acSTrond Myklebust {
72695f7691dSTrond Myklebust 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
727ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
728ad3331acSTrond Myklebust }
729ad3331acSTrond Myklebust 
7301da177e4SLinus Torvalds static void
731ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
7321da177e4SLinus Torvalds {
733ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
7341da177e4SLinus Torvalds 
7354a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
73695f7691dSTrond Myklebust 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
7371da177e4SLinus Torvalds 		goto out_abort;
738ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
739ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
7402226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
7411da177e4SLinus Torvalds 		goto out_abort;
7424a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
74340a5f1b1STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7441da177e4SLinus Torvalds 	return;
7451da177e4SLinus Torvalds out_abort:
7464a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
749718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
750718ba5b8STrond Myklebust 		struct rpc_task *task,
751718ba5b8STrond Myklebust 		void *cookie)
752718ba5b8STrond Myklebust {
753718ba5b8STrond Myklebust 	bool ret = false;
754718ba5b8STrond Myklebust 
755718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
756718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
757718ba5b8STrond Myklebust 		goto out;
758718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
759718ba5b8STrond Myklebust 		goto out;
760718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
761718ba5b8STrond Myklebust 	ret = true;
762718ba5b8STrond Myklebust out:
763718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
764718ba5b8STrond Myklebust 	return ret;
765718ba5b8STrond Myklebust }
766718ba5b8STrond Myklebust 
767718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
768718ba5b8STrond Myklebust {
769718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
770718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
771718ba5b8STrond Myklebust 		goto out;
772718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
773718ba5b8STrond Myklebust 		goto out;
774718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
775718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
776ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
777718ba5b8STrond Myklebust out:
778718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
77979234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
780718ba5b8STrond Myklebust }
781718ba5b8STrond Myklebust 
7829903cd1cSChuck Lever /**
7839903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
7849903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
7851da177e4SLinus Torvalds  *
7861da177e4SLinus Torvalds  */
7871da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
7881da177e4SLinus Torvalds {
789ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7901da177e4SLinus Torvalds 
79146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7921da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7931da177e4SLinus Torvalds 
794ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
79501d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7961da177e4SLinus Torvalds 		return;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7991da177e4SLinus Torvalds 		return;
800feb8ca37STrond Myklebust 
801feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
802feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
803feb8ca37STrond Myklebust 
804718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
8052c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
8066b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
8079e910bffSTrond Myklebust 				xprt_request_timeout(task->tk_rqstp));
8080b9e7943STrond Myklebust 
8090b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
8100b9e7943STrond Myklebust 			return;
8110b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
8120b9e7943STrond Myklebust 			return;
8130a9a4304STrond Myklebust 		/* Race breaker */
8140a9a4304STrond Myklebust 		if (!xprt_connected(xprt)) {
815262ca07dSChuck Lever 			xprt->stat.connect_start = jiffies;
8161b092092STrond Myklebust 			xprt->ops->connect(xprt, task);
8170a9a4304STrond Myklebust 		} else {
8180a9a4304STrond Myklebust 			xprt_clear_connecting(xprt);
8190a9a4304STrond Myklebust 			task->tk_status = 0;
8200a9a4304STrond Myklebust 			rpc_wake_up_queued_task(&xprt->pending, task);
8210a9a4304STrond Myklebust 		}
8221da177e4SLinus Torvalds 	}
823718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
82695f7691dSTrond Myklebust enum xprt_xid_rb_cmp {
82795f7691dSTrond Myklebust 	XID_RB_EQUAL,
82895f7691dSTrond Myklebust 	XID_RB_LEFT,
82995f7691dSTrond Myklebust 	XID_RB_RIGHT,
83095f7691dSTrond Myklebust };
83195f7691dSTrond Myklebust static enum xprt_xid_rb_cmp
83295f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2)
83395f7691dSTrond Myklebust {
83495f7691dSTrond Myklebust 	if (xid1 == xid2)
83595f7691dSTrond Myklebust 		return XID_RB_EQUAL;
83695f7691dSTrond Myklebust 	if ((__force u32)xid1 < (__force u32)xid2)
83795f7691dSTrond Myklebust 		return XID_RB_LEFT;
83895f7691dSTrond Myklebust 	return XID_RB_RIGHT;
83995f7691dSTrond Myklebust }
84095f7691dSTrond Myklebust 
84195f7691dSTrond Myklebust static struct rpc_rqst *
84295f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
84395f7691dSTrond Myklebust {
84495f7691dSTrond Myklebust 	struct rb_node *n = xprt->recv_queue.rb_node;
84595f7691dSTrond Myklebust 	struct rpc_rqst *req;
84695f7691dSTrond Myklebust 
84795f7691dSTrond Myklebust 	while (n != NULL) {
84895f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
84995f7691dSTrond Myklebust 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
85095f7691dSTrond Myklebust 		case XID_RB_LEFT:
85195f7691dSTrond Myklebust 			n = n->rb_left;
85295f7691dSTrond Myklebust 			break;
85395f7691dSTrond Myklebust 		case XID_RB_RIGHT:
85495f7691dSTrond Myklebust 			n = n->rb_right;
85595f7691dSTrond Myklebust 			break;
85695f7691dSTrond Myklebust 		case XID_RB_EQUAL:
85795f7691dSTrond Myklebust 			return req;
85895f7691dSTrond Myklebust 		}
85995f7691dSTrond Myklebust 	}
86095f7691dSTrond Myklebust 	return NULL;
86195f7691dSTrond Myklebust }
86295f7691dSTrond Myklebust 
86395f7691dSTrond Myklebust static void
86495f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
86595f7691dSTrond Myklebust {
86695f7691dSTrond Myklebust 	struct rb_node **p = &xprt->recv_queue.rb_node;
86795f7691dSTrond Myklebust 	struct rb_node *n = NULL;
86895f7691dSTrond Myklebust 	struct rpc_rqst *req;
86995f7691dSTrond Myklebust 
87095f7691dSTrond Myklebust 	while (*p != NULL) {
87195f7691dSTrond Myklebust 		n = *p;
87295f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
87395f7691dSTrond Myklebust 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
87495f7691dSTrond Myklebust 		case XID_RB_LEFT:
87595f7691dSTrond Myklebust 			p = &n->rb_left;
87695f7691dSTrond Myklebust 			break;
87795f7691dSTrond Myklebust 		case XID_RB_RIGHT:
87895f7691dSTrond Myklebust 			p = &n->rb_right;
87995f7691dSTrond Myklebust 			break;
88095f7691dSTrond Myklebust 		case XID_RB_EQUAL:
88195f7691dSTrond Myklebust 			WARN_ON_ONCE(new != req);
88295f7691dSTrond Myklebust 			return;
88395f7691dSTrond Myklebust 		}
88495f7691dSTrond Myklebust 	}
88595f7691dSTrond Myklebust 	rb_link_node(&new->rq_recv, n, p);
88695f7691dSTrond Myklebust 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
88795f7691dSTrond Myklebust }
88895f7691dSTrond Myklebust 
88995f7691dSTrond Myklebust static void
89095f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
89195f7691dSTrond Myklebust {
89295f7691dSTrond Myklebust 	rb_erase(&req->rq_recv, &xprt->recv_queue);
89395f7691dSTrond Myklebust }
89495f7691dSTrond Myklebust 
8959903cd1cSChuck Lever /**
8969903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
8979903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
8989903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
8999903cd1cSChuck Lever  *
90075c84151STrond Myklebust  * Caller holds xprt->queue_lock.
9011da177e4SLinus Torvalds  */
902d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
9031da177e4SLinus Torvalds {
9048f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
9051da177e4SLinus Torvalds 
90695f7691dSTrond Myklebust 	entry = xprt_request_rb_find(xprt, xid);
90795f7691dSTrond Myklebust 	if (entry != NULL) {
9083705ad64SJeff Layton 		trace_xprt_lookup_rqst(xprt, xid, 0);
9090b87a46bSChuck Lever 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
910262ca07dSChuck Lever 		return entry;
9113705ad64SJeff Layton 	}
91246121cf7SChuck Lever 
91346121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
91446121cf7SChuck Lever 			ntohl(xid));
9153705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
916262ca07dSChuck Lever 	xprt->stat.bad_xids++;
917262ca07dSChuck Lever 	return NULL;
9181da177e4SLinus Torvalds }
91912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
9201da177e4SLinus Torvalds 
921cf9946cdSTrond Myklebust static bool
922cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
923cf9946cdSTrond Myklebust {
924cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
925cf9946cdSTrond Myklebust }
926cf9946cdSTrond Myklebust 
927729749bbSTrond Myklebust /**
928729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
929729749bbSTrond Myklebust  * @req: Request to pin
930729749bbSTrond Myklebust  *
931729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
932cf9946cdSTrond Myklebust  * so should be holding the xprt receive lock.
933729749bbSTrond Myklebust  */
934729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
935729749bbSTrond Myklebust {
936cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
937729749bbSTrond Myklebust }
9389590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
939729749bbSTrond Myklebust 
940729749bbSTrond Myklebust /**
941729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
942729749bbSTrond Myklebust  * @req: Request to pin
943729749bbSTrond Myklebust  *
944cf9946cdSTrond Myklebust  * Caller should be holding the xprt receive lock.
945729749bbSTrond Myklebust  */
946729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
947729749bbSTrond Myklebust {
948cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
949cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
950cf9946cdSTrond Myklebust 		return;
951cf9946cdSTrond Myklebust 	}
952cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
953cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
954729749bbSTrond Myklebust }
9559590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
956729749bbSTrond Myklebust 
957729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
958729749bbSTrond Myklebust {
959cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
960729749bbSTrond Myklebust }
961729749bbSTrond Myklebust 
962edc81dcdSTrond Myklebust static bool
963edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
964edc81dcdSTrond Myklebust {
965edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
966edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
967edc81dcdSTrond Myklebust }
968edc81dcdSTrond Myklebust 
969edc81dcdSTrond Myklebust static bool
970edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
971edc81dcdSTrond Myklebust {
972edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
973edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
974edc81dcdSTrond Myklebust }
975edc81dcdSTrond Myklebust 
976edc81dcdSTrond Myklebust /**
977edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
978edc81dcdSTrond Myklebust  * @task: RPC task
979edc81dcdSTrond Myklebust  *
980edc81dcdSTrond Myklebust  */
981edc81dcdSTrond Myklebust void
982edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
983edc81dcdSTrond Myklebust {
984edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
985edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
986edc81dcdSTrond Myklebust 
987edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
988edc81dcdSTrond Myklebust 		return;
989edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
990edc81dcdSTrond Myklebust 
991edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
992edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
993edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
994edc81dcdSTrond Myklebust 
995edc81dcdSTrond Myklebust 	/* Add request to the receive list */
99695f7691dSTrond Myklebust 	xprt_request_rb_insert(xprt, req);
997edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
998edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
999edc81dcdSTrond Myklebust 
1000edc81dcdSTrond Myklebust 	xprt_reset_majortimeo(req);
1001edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
1002edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
1003edc81dcdSTrond Myklebust }
1004edc81dcdSTrond Myklebust 
1005edc81dcdSTrond Myklebust /**
1006edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1007edc81dcdSTrond Myklebust  * @task: RPC task
1008edc81dcdSTrond Myklebust  *
1009edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
1010edc81dcdSTrond Myklebust  */
1011edc81dcdSTrond Myklebust static void
1012edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
1013edc81dcdSTrond Myklebust {
101495f7691dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
101595f7691dSTrond Myklebust 
1016edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
101795f7691dSTrond Myklebust 		xprt_request_rb_remove(req->rq_xprt, req);
1018edc81dcdSTrond Myklebust }
1019edc81dcdSTrond Myklebust 
1020ecd465eeSChuck Lever /**
1021ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
1022ecd465eeSChuck Lever  * @task: RPC request that recently completed
1023ecd465eeSChuck Lever  *
102475c84151STrond Myklebust  * Caller holds xprt->queue_lock.
1025ecd465eeSChuck Lever  */
1026ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
10271da177e4SLinus Torvalds {
10281570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
10291570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
103095c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1031d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
10321570c1e4SChuck Lever 
10331da177e4SLinus Torvalds 	if (timer) {
10341da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
1035ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
10361570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds }
1039ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
10401da177e4SLinus Torvalds 
10411570c1e4SChuck Lever /**
10421570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
10431570c1e4SChuck Lever  * @task: RPC request that recently completed
10441570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
10451570c1e4SChuck Lever  *
104675c84151STrond Myklebust  * Caller holds xprt->queue_lock.
10471570c1e4SChuck Lever  */
10481570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
10491570c1e4SChuck Lever {
10501570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
1051fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
10521da177e4SLinus Torvalds 
10531570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
10541570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
10553705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
10561da177e4SLinus Torvalds 
1057fda13939STrond Myklebust 	xprt->stat.recvs++;
1058ef759a2eSChuck Lever 
10591e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1060dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1061dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
106243ac3f29STrond Myklebust 	smp_wmb();
1063dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1064edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1065fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
10661da177e4SLinus Torvalds }
106712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
10681da177e4SLinus Torvalds 
106946c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
10721da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
10731da177e4SLinus Torvalds 
10745d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
10755d00837bSTrond Myklebust 		return;
107646c0ee8bSChuck Lever 
107782476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1078dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
107946c0ee8bSChuck Lever 		if (xprt->ops->timer)
10806a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
10815d00837bSTrond Myklebust 	} else
10825d00837bSTrond Myklebust 		task->tk_status = 0;
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
10859903cd1cSChuck Lever /**
10868ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_def - wait for reply
10878ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
10888ba6a92dSTrond Myklebust  *
10898ba6a92dSTrond Myklebust  * Set a request's retransmit timeout based on the transport's
10908ba6a92dSTrond Myklebust  * default timeout parameters.  Used by transports that don't adjust
10918ba6a92dSTrond Myklebust  * the retransmit timeout based on round-trip time estimation,
10928ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
10938ba6a92dSTrond Myklebust  */
10948ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task)
10958ba6a92dSTrond Myklebust {
10968ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
10978ba6a92dSTrond Myklebust 
10986b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
10999e910bffSTrond Myklebust 			xprt_request_timeout(req));
11008ba6a92dSTrond Myklebust }
11018ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
11028ba6a92dSTrond Myklebust 
11038ba6a92dSTrond Myklebust /**
11048ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
11058ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
11068ba6a92dSTrond Myklebust  *
11078ba6a92dSTrond Myklebust  * Set a request's retransmit timeout using the RTT estimator,
11088ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
11098ba6a92dSTrond Myklebust  */
11108ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
11118ba6a92dSTrond Myklebust {
11128ba6a92dSTrond Myklebust 	int timer = task->tk_msg.rpc_proc->p_timer;
11138ba6a92dSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
11148ba6a92dSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
11158ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11168ba6a92dSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
11176b2e6856STrond Myklebust 	unsigned long timeout;
11188ba6a92dSTrond Myklebust 
11196b2e6856STrond Myklebust 	timeout = rpc_calc_rto(rtt, timer);
11206b2e6856STrond Myklebust 	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
11216b2e6856STrond Myklebust 	if (timeout > max_timeout || timeout == 0)
11226b2e6856STrond Myklebust 		timeout = max_timeout;
11236b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
11246b2e6856STrond Myklebust 			jiffies + timeout);
11258ba6a92dSTrond Myklebust }
11268ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
11278ba6a92dSTrond Myklebust 
11288ba6a92dSTrond Myklebust /**
11297f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
11307f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
11317f3a1d1eSTrond Myklebust  *
11327f3a1d1eSTrond Myklebust  */
11337f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
11347f3a1d1eSTrond Myklebust {
11357f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11367f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
11377f3a1d1eSTrond Myklebust 
11387f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
11397f3a1d1eSTrond Myklebust 		return;
11407f3a1d1eSTrond Myklebust 	/*
11417f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
11427f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
11437f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
11447f3a1d1eSTrond Myklebust 	 */
11457f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
11467f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
11478ba6a92dSTrond Myklebust 		xprt->ops->wait_for_reply_request(task);
11487f3a1d1eSTrond Myklebust 		/*
11497f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
11507f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
11517f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
11527f3a1d1eSTrond Myklebust 		 */
11537f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
11547f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
11557f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
11567f3a1d1eSTrond Myklebust 	}
11577f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
11587f3a1d1eSTrond Myklebust }
11597f3a1d1eSTrond Myklebust 
1160944b0429STrond Myklebust static bool
1161944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1162944b0429STrond Myklebust {
1163762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1164944b0429STrond Myklebust }
1165944b0429STrond Myklebust 
1166944b0429STrond Myklebust /**
1167944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1168944b0429STrond Myklebust  * @task: pointer to rpc_task
1169944b0429STrond Myklebust  *
1170944b0429STrond Myklebust  * Add a task to the transmission queue.
1171944b0429STrond Myklebust  */
1172944b0429STrond Myklebust void
1173944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1174944b0429STrond Myklebust {
1175918f3c1fSTrond Myklebust 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1176944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1177944b0429STrond Myklebust 
1178944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1179e66721f0STrond Myklebust 		req->rq_bytes_sent = 0;
1180944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
118175891f50STrond Myklebust 		/*
118275891f50STrond Myklebust 		 * Requests that carry congestion control credits are added
118375891f50STrond Myklebust 		 * to the head of the list to avoid starvation issues.
118475891f50STrond Myklebust 		 */
118575891f50STrond Myklebust 		if (req->rq_cong) {
118675891f50STrond Myklebust 			xprt_clear_congestion_window_wait(xprt);
118775891f50STrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
118875891f50STrond Myklebust 				if (pos->rq_cong)
118975891f50STrond Myklebust 					continue;
119075891f50STrond Myklebust 				/* Note: req is added _before_ pos */
119175891f50STrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
119275891f50STrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
11930c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 1);
119475891f50STrond Myklebust 				goto out;
119575891f50STrond Myklebust 			}
119686aeee0eSTrond Myklebust 		} else if (RPC_IS_SWAPPER(task)) {
119786aeee0eSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
119886aeee0eSTrond Myklebust 				if (pos->rq_cong || pos->rq_bytes_sent)
119986aeee0eSTrond Myklebust 					continue;
120086aeee0eSTrond Myklebust 				if (RPC_IS_SWAPPER(pos->rq_task))
120186aeee0eSTrond Myklebust 					continue;
120286aeee0eSTrond Myklebust 				/* Note: req is added _before_ pos */
120386aeee0eSTrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
120486aeee0eSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
12050c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 2);
120686aeee0eSTrond Myklebust 				goto out;
120786aeee0eSTrond Myklebust 			}
1208deaa5c96SChuck Lever 		} else if (!req->rq_seqno) {
1209918f3c1fSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1210918f3c1fSTrond Myklebust 				if (pos->rq_task->tk_owner != task->tk_owner)
1211918f3c1fSTrond Myklebust 					continue;
1212918f3c1fSTrond Myklebust 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1213918f3c1fSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit);
12140c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 3);
1215918f3c1fSTrond Myklebust 				goto out;
1216918f3c1fSTrond Myklebust 			}
121775891f50STrond Myklebust 		}
1218944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1219918f3c1fSTrond Myklebust 		INIT_LIST_HEAD(&req->rq_xmit2);
12200c77668dSChuck Lever 		trace_xprt_enq_xmit(task, 4);
1221918f3c1fSTrond Myklebust out:
1222944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1223944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1224944b0429STrond Myklebust 	}
1225944b0429STrond Myklebust }
1226944b0429STrond Myklebust 
1227944b0429STrond Myklebust /**
1228944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1229944b0429STrond Myklebust  * @task: pointer to rpc_task
1230944b0429STrond Myklebust  *
1231944b0429STrond Myklebust  * Remove a task from the transmission queue
1232944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1233944b0429STrond Myklebust  */
1234944b0429STrond Myklebust static void
1235944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1236944b0429STrond Myklebust {
1237918f3c1fSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1238918f3c1fSTrond Myklebust 
1239918f3c1fSTrond Myklebust 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1240918f3c1fSTrond Myklebust 		return;
1241918f3c1fSTrond Myklebust 	if (!list_empty(&req->rq_xmit)) {
1242918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit);
1243918f3c1fSTrond Myklebust 		if (!list_empty(&req->rq_xmit2)) {
1244918f3c1fSTrond Myklebust 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1245918f3c1fSTrond Myklebust 					struct rpc_rqst, rq_xmit2);
1246918f3c1fSTrond Myklebust 			list_del(&req->rq_xmit2);
1247918f3c1fSTrond Myklebust 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1248918f3c1fSTrond Myklebust 		}
1249918f3c1fSTrond Myklebust 	} else
1250918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit2);
1251944b0429STrond Myklebust }
1252944b0429STrond Myklebust 
1253944b0429STrond Myklebust /**
1254944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1255944b0429STrond Myklebust  * @task: pointer to rpc_task
1256944b0429STrond Myklebust  *
1257944b0429STrond Myklebust  * Remove a task from the transmission queue
1258944b0429STrond Myklebust  */
1259944b0429STrond Myklebust static void
1260944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1261944b0429STrond Myklebust {
1262944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1263944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1264944b0429STrond Myklebust 
1265944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1266944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1267944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1268944b0429STrond Myklebust }
1269944b0429STrond Myklebust 
12707f3a1d1eSTrond Myklebust /**
12719d96acbcSTrond Myklebust  * xprt_request_prepare - prepare an encoded request for transport
12729d96acbcSTrond Myklebust  * @req: pointer to rpc_rqst
12739d96acbcSTrond Myklebust  *
12749d96acbcSTrond Myklebust  * Calls into the transport layer to do whatever is needed to prepare
12759d96acbcSTrond Myklebust  * the request for transmission or receive.
12769d96acbcSTrond Myklebust  */
12779d96acbcSTrond Myklebust void
12789d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req)
12799d96acbcSTrond Myklebust {
12809d96acbcSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
12819d96acbcSTrond Myklebust 
12829d96acbcSTrond Myklebust 	if (xprt->ops->prepare_request)
12839d96acbcSTrond Myklebust 		xprt->ops->prepare_request(req);
12849d96acbcSTrond Myklebust }
12859d96acbcSTrond Myklebust 
12869d96acbcSTrond Myklebust /**
1287762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1288762e4e67STrond Myklebust  * @task: pointer to rpc_task
1289762e4e67STrond Myklebust  *
1290762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1291762e4e67STrond Myklebust  */
1292762e4e67STrond Myklebust bool
1293762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1294762e4e67STrond Myklebust {
1295762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1296762e4e67STrond Myklebust }
1297762e4e67STrond Myklebust 
1298762e4e67STrond Myklebust /**
12999903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
13009903cd1cSChuck Lever  * @task: RPC task about to send a request
13019903cd1cSChuck Lever  *
13021da177e4SLinus Torvalds  */
130390051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
13041da177e4SLinus Torvalds {
13051da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
13061da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
13071da177e4SLinus Torvalds 
130846121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
13091da177e4SLinus Torvalds 
13105f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
13115f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1312944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
13135f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
13145f2f6bd9STrond Myklebust 					task, 0);
13155f2f6bd9STrond Myklebust 		return false;
13165f2f6bd9STrond Myklebust 
13178a19a0b6STrond Myklebust 	}
13185f2f6bd9STrond Myklebust 	return true;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
1321e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
13225e5ce5beSTrond Myklebust {
1323343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
13245e5ce5beSTrond Myklebust }
13255e5ce5beSTrond Myklebust 
13269903cd1cSChuck Lever /**
132789f90fe1STrond Myklebust  * xprt_request_transmit - send an RPC request on a transport
132889f90fe1STrond Myklebust  * @req: pointer to request to transmit
132989f90fe1STrond Myklebust  * @snd_task: RPC task that owns the transport lock
13309903cd1cSChuck Lever  *
133189f90fe1STrond Myklebust  * This performs the transmission of a single request.
133289f90fe1STrond Myklebust  * Note that if the request is not the same as snd_task, then it
133389f90fe1STrond Myklebust  * does need to be pinned.
133489f90fe1STrond Myklebust  * Returns '0' on success.
13359903cd1cSChuck Lever  */
133689f90fe1STrond Myklebust static int
133789f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
134089f90fe1STrond Myklebust 	struct rpc_task *task = req->rq_task;
134190d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1342dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1343ff699ea8SChuck Lever 	int status;
13441da177e4SLinus Torvalds 
1345edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
134689f90fe1STrond Myklebust 		if (xprt_request_data_received(task)) {
134789f90fe1STrond Myklebust 			status = 0;
1348944b0429STrond Myklebust 			goto out_dequeue;
134989f90fe1STrond Myklebust 		}
13503021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1351edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
135289f90fe1STrond Myklebust 			status = -EBADMSG;
1353944b0429STrond Myklebust 			goto out_dequeue;
13543021a5bbSTrond Myklebust 		}
1355a79f194aSTrond Myklebust 		if (task->tk_ops->rpc_call_prepare_transmit) {
1356a79f194aSTrond Myklebust 			task->tk_ops->rpc_call_prepare_transmit(task,
1357a79f194aSTrond Myklebust 					task->tk_calldata);
1358a79f194aSTrond Myklebust 			status = task->tk_status;
1359a79f194aSTrond Myklebust 			if (status < 0)
1360a79f194aSTrond Myklebust 				goto out_dequeue;
1361a79f194aSTrond Myklebust 		}
1362ae67bd38STrond Myklebust 		if (RPC_SIGNALLED(task)) {
1363ae67bd38STrond Myklebust 			status = -ERESTARTSYS;
1364ae67bd38STrond Myklebust 			goto out_dequeue;
1365ae67bd38STrond Myklebust 		}
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 
1368dcbbeda8STrond Myklebust 	/*
1369dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1370dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1371dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1372dcbbeda8STrond Myklebust 	 */
1373dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1374dcbbeda8STrond Myklebust 
137590d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1376adfa7144STrond Myklebust 	status = xprt->ops->send_request(req);
1377c8485e4dSTrond Myklebust 	if (status != 0) {
1378dcbbeda8STrond Myklebust 		req->rq_ntrans--;
13790c77668dSChuck Lever 		trace_xprt_transmit(req, status);
138089f90fe1STrond Myklebust 		return status;
1381c8485e4dSTrond Myklebust 	}
13827ebbbc6eSTrond Myklebust 
1383dcbbeda8STrond Myklebust 	if (is_retrans)
1384dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1385dcbbeda8STrond Myklebust 
13864a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1387c8485e4dSTrond Myklebust 
1388468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1389fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
1390262ca07dSChuck Lever 
1391262ca07dSChuck Lever 	xprt->stat.sends++;
1392262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1393262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
139415a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
139515a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1396fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
139790d91b0cSTrond Myklebust 
139890d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1399944b0429STrond Myklebust out_dequeue:
14000c77668dSChuck Lever 	trace_xprt_transmit(req, status);
1401944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
140289f90fe1STrond Myklebust 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
140389f90fe1STrond Myklebust 	return status;
140489f90fe1STrond Myklebust }
140589f90fe1STrond Myklebust 
140689f90fe1STrond Myklebust /**
140789f90fe1STrond Myklebust  * xprt_transmit - send an RPC request on a transport
140889f90fe1STrond Myklebust  * @task: controlling RPC task
140989f90fe1STrond Myklebust  *
141089f90fe1STrond Myklebust  * Attempts to drain the transmit queue. On exit, either the transport
141189f90fe1STrond Myklebust  * signalled an error that needs to be handled before transmission can
141289f90fe1STrond Myklebust  * resume, or @task finished transmitting, and detected that it already
141389f90fe1STrond Myklebust  * received a reply.
141489f90fe1STrond Myklebust  */
141589f90fe1STrond Myklebust void
141689f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task)
141789f90fe1STrond Myklebust {
141889f90fe1STrond Myklebust 	struct rpc_rqst *next, *req = task->tk_rqstp;
141989f90fe1STrond Myklebust 	struct rpc_xprt	*xprt = req->rq_xprt;
142089f90fe1STrond Myklebust 	int status;
142189f90fe1STrond Myklebust 
142289f90fe1STrond Myklebust 	spin_lock(&xprt->queue_lock);
142389f90fe1STrond Myklebust 	while (!list_empty(&xprt->xmit_queue)) {
142489f90fe1STrond Myklebust 		next = list_first_entry(&xprt->xmit_queue,
142589f90fe1STrond Myklebust 				struct rpc_rqst, rq_xmit);
142689f90fe1STrond Myklebust 		xprt_pin_rqst(next);
142789f90fe1STrond Myklebust 		spin_unlock(&xprt->queue_lock);
142889f90fe1STrond Myklebust 		status = xprt_request_transmit(next, task);
142989f90fe1STrond Myklebust 		if (status == -EBADMSG && next != req)
143089f90fe1STrond Myklebust 			status = 0;
143189f90fe1STrond Myklebust 		cond_resched();
143289f90fe1STrond Myklebust 		spin_lock(&xprt->queue_lock);
143389f90fe1STrond Myklebust 		xprt_unpin_rqst(next);
143489f90fe1STrond Myklebust 		if (status == 0) {
143589f90fe1STrond Myklebust 			if (!xprt_request_data_received(task) ||
143689f90fe1STrond Myklebust 			    test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
143789f90fe1STrond Myklebust 				continue;
1438c544577dSTrond Myklebust 		} else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
143989f90fe1STrond Myklebust 			task->tk_status = status;
144089f90fe1STrond Myklebust 		break;
144189f90fe1STrond Myklebust 	}
144289f90fe1STrond Myklebust 	spin_unlock(&xprt->queue_lock);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
1445ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1446ba60eb25STrond Myklebust {
1447ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1448ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1449ba60eb25STrond Myklebust }
1450ba60eb25STrond Myklebust 
1451ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1452ba60eb25STrond Myklebust {
1453ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1454ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1455ba60eb25STrond Myklebust }
1456ba60eb25STrond Myklebust 
1457ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1458ba60eb25STrond Myklebust {
1459ba60eb25STrond Myklebust 	bool ret = false;
1460ba60eb25STrond Myklebust 
1461ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1462ba60eb25STrond Myklebust 		goto out;
1463ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1464ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1465ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1466ba60eb25STrond Myklebust 		ret = true;
1467ba60eb25STrond Myklebust 	}
1468ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1469ba60eb25STrond Myklebust out:
1470ba60eb25STrond Myklebust 	return ret;
1471ba60eb25STrond Myklebust }
1472ba60eb25STrond Myklebust 
147392ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1474d9ba131dSTrond Myklebust {
1475d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1476d9ba131dSTrond Myklebust 
1477ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1478d9ba131dSTrond Myklebust 		goto out;
1479ff699ea8SChuck Lever 	++xprt->num_reqs;
148092ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
148192ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
148292ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1483d9ba131dSTrond Myklebust 	if (req != NULL)
1484d9ba131dSTrond Myklebust 		goto out;
1485ff699ea8SChuck Lever 	--xprt->num_reqs;
1486d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1487d9ba131dSTrond Myklebust out:
1488d9ba131dSTrond Myklebust 	return req;
1489d9ba131dSTrond Myklebust }
1490d9ba131dSTrond Myklebust 
1491d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1492d9ba131dSTrond Myklebust {
1493ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1494ff699ea8SChuck Lever 		--xprt->num_reqs;
1495d9ba131dSTrond Myklebust 		kfree(req);
1496d9ba131dSTrond Myklebust 		return true;
1497d9ba131dSTrond Myklebust 	}
1498d9ba131dSTrond Myklebust 	return false;
1499d9ba131dSTrond Myklebust }
1500d9ba131dSTrond Myklebust 
1501f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
15021da177e4SLinus Torvalds {
1503d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
15041da177e4SLinus Torvalds 
1505f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
15061da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1507d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1508d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1509d9ba131dSTrond Myklebust 		goto out_init_req;
1510d9ba131dSTrond Myklebust 	}
151192ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1512d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1513d9ba131dSTrond Myklebust 		goto out_init_req;
1514d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1515d9ba131dSTrond Myklebust 	case -ENOMEM:
1516d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1517d9ba131dSTrond Myklebust 				"failed! Retrying\n");
15181afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1519d9ba131dSTrond Myklebust 		break;
1520d9ba131dSTrond Myklebust 	case -EAGAIN:
1521ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1522d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1523e9d47639SGustavo A. R. Silva 		/* fall through */
15241afeaf5cSTrond Myklebust 	default:
1525d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
15261afeaf5cSTrond Myklebust 	}
1527f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1528d9ba131dSTrond Myklebust 	return;
1529d9ba131dSTrond Myklebust out_init_req:
1530ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1531ff699ea8SChuck Lever 				     xprt->num_reqs);
153237ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
153337ac86c3SChuck Lever 
1534d9ba131dSTrond Myklebust 	task->tk_status = 0;
15351da177e4SLinus Torvalds 	task->tk_rqstp = req;
15361da177e4SLinus Torvalds }
1537f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1538f39c1bfbSTrond Myklebust 
1539a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1540ee5ebe85STrond Myklebust {
1541ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1542c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1543c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1544ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1545c25573b5STrond Myklebust 	}
1546ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1547ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1548ee5ebe85STrond Myklebust }
1549a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1550ee5ebe85STrond Myklebust 
155121de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
155221de0a95STrond Myklebust {
155321de0a95STrond Myklebust 	struct rpc_rqst *req;
155421de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
155521de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
155621de0a95STrond Myklebust 		list_del(&req->rq_list);
155721de0a95STrond Myklebust 		kfree(req);
155821de0a95STrond Myklebust 	}
155921de0a95STrond Myklebust }
156021de0a95STrond Myklebust 
1561d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1562d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1563d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1564bd1722d4SPavel Emelyanov {
1565bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
156621de0a95STrond Myklebust 	struct rpc_rqst *req;
156721de0a95STrond Myklebust 	int i;
1568bd1722d4SPavel Emelyanov 
1569bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1570bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1571bd1722d4SPavel Emelyanov 		goto out;
1572bd1722d4SPavel Emelyanov 
157321de0a95STrond Myklebust 	xprt_init(xprt, net);
157421de0a95STrond Myklebust 
157521de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
157621de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
157721de0a95STrond Myklebust 		if (!req)
15788313164cSwangweidong 			goto out_free;
157921de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
158021de0a95STrond Myklebust 	}
1581d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1582d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1583d9ba131dSTrond Myklebust 	else
158421de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1585d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1586ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1587bd1722d4SPavel Emelyanov 
1588bd1722d4SPavel Emelyanov 	return xprt;
1589bd1722d4SPavel Emelyanov 
1590bd1722d4SPavel Emelyanov out_free:
159121de0a95STrond Myklebust 	xprt_free(xprt);
1592bd1722d4SPavel Emelyanov out:
1593bd1722d4SPavel Emelyanov 	return NULL;
1594bd1722d4SPavel Emelyanov }
1595bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1596bd1722d4SPavel Emelyanov 
1597e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1598e204e621SPavel Emelyanov {
159937aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
160021de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1601fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1602e204e621SPavel Emelyanov }
1603e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1604e204e621SPavel Emelyanov 
1605902c5887STrond Myklebust static void
1606902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1607902c5887STrond Myklebust {
1608902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1609902c5887STrond Myklebust }
1610902c5887STrond Myklebust 
16119dc6edcfSTrond Myklebust static __be32
16129dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
16139dc6edcfSTrond Myklebust {
16149dc6edcfSTrond Myklebust 	__be32 xid;
16159dc6edcfSTrond Myklebust 
16169dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
16179dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
16189dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
16199dc6edcfSTrond Myklebust 	return xid;
16209dc6edcfSTrond Myklebust }
16219dc6edcfSTrond Myklebust 
16229dc6edcfSTrond Myklebust static void
16239dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
16249dc6edcfSTrond Myklebust {
16259dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
16269dc6edcfSTrond Myklebust }
16279dc6edcfSTrond Myklebust 
16289dc6edcfSTrond Myklebust static void
16299dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
16309dc6edcfSTrond Myklebust {
16319dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
16329dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
16339dc6edcfSTrond Myklebust 
16349dc6edcfSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
16359dc6edcfSTrond Myklebust 	req->rq_task	= task;
16369dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
16379dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
16389dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1639902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
16409dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
16419dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
16429dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
16439dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
164471700bb9STrond Myklebust 	req->rq_snd_buf.bvec = NULL;
164571700bb9STrond Myklebust 	req->rq_rcv_buf.bvec = NULL;
16469dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
16479dc6edcfSTrond Myklebust 	xprt_reset_majortimeo(req);
16489dc6edcfSTrond Myklebust 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
16499dc6edcfSTrond Myklebust 			req, ntohl(req->rq_xid));
16509dc6edcfSTrond Myklebust }
16519dc6edcfSTrond Myklebust 
16529dc6edcfSTrond Myklebust static void
16539dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
16549dc6edcfSTrond Myklebust {
16559dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
16569dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
16579dc6edcfSTrond Myklebust 		xprt_request_init(task);
16589dc6edcfSTrond Myklebust }
16599dc6edcfSTrond Myklebust 
16609903cd1cSChuck Lever /**
16619903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
16629903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
16639903cd1cSChuck Lever  *
1664ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1665ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
16669903cd1cSChuck Lever  * backlog queue.
16679903cd1cSChuck Lever  */
16689903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
16691da177e4SLinus Torvalds {
1670fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
16711da177e4SLinus Torvalds 
167243cedbf0STrond Myklebust 	task->tk_status = 0;
167343cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
167443cedbf0STrond Myklebust 		return;
167543cedbf0STrond Myklebust 
167643cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1677ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
16789dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1679ba60eb25STrond Myklebust }
1680ba60eb25STrond Myklebust 
1681ba60eb25STrond Myklebust /**
1682ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1683ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1684ba60eb25STrond Myklebust  *
1685ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1686ba60eb25STrond Myklebust  * backlog queue.
1687ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1688ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1689ba60eb25STrond Myklebust  */
1690ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1691ba60eb25STrond Myklebust {
1692fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1693ba60eb25STrond Myklebust 
1694ba60eb25STrond Myklebust 	task->tk_status = 0;
1695ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1696ba60eb25STrond Myklebust 		return;
1697ba60eb25STrond Myklebust 
1698ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
16999dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
1702edc81dcdSTrond Myklebust static void
1703edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1704edc81dcdSTrond Myklebust {
1705edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1706edc81dcdSTrond Myklebust 
1707944b0429STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1708944b0429STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1709edc81dcdSTrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1710edc81dcdSTrond Myklebust 		spin_lock(&xprt->queue_lock);
1711944b0429STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1712edc81dcdSTrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1713edc81dcdSTrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1714edc81dcdSTrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1715edc81dcdSTrond Myklebust 			spin_unlock(&xprt->queue_lock);
1716edc81dcdSTrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1717edc81dcdSTrond Myklebust 			spin_lock(&xprt->queue_lock);
1718edc81dcdSTrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1719edc81dcdSTrond Myklebust 		}
1720edc81dcdSTrond Myklebust 		spin_unlock(&xprt->queue_lock);
1721edc81dcdSTrond Myklebust 	}
1722edc81dcdSTrond Myklebust }
1723edc81dcdSTrond Myklebust 
17249903cd1cSChuck Lever /**
17259903cd1cSChuck Lever  * xprt_release - release an RPC request slot
17269903cd1cSChuck Lever  * @task: task which is finished with the slot
17279903cd1cSChuck Lever  *
17281da177e4SLinus Torvalds  */
17299903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
17301da177e4SLinus Torvalds {
173155ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
173287ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
17331da177e4SLinus Torvalds 
173487ed5003STrond Myklebust 	if (req == NULL) {
173587ed5003STrond Myklebust 		if (task->tk_client) {
1736fb43d172STrond Myklebust 			xprt = task->tk_xprt;
173787ed5003STrond Myklebust 			xprt_release_write(xprt, task);
173887ed5003STrond Myklebust 		}
17391da177e4SLinus Torvalds 		return;
174087ed5003STrond Myklebust 	}
174155ae1aabSRicardo Labiaga 
174255ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
17430a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
17440a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
17450a702195SWeston Andros Adamson 	else if (task->tk_client)
17460a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
1747edc81dcdSTrond Myklebust 	xprt_request_dequeue_all(task, req);
17484a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
174949e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1750a58dd398SChuck Lever 	if (xprt->ops->release_request)
1751a58dd398SChuck Lever 		xprt->ops->release_request(task);
17521da177e4SLinus Torvalds 	xprt->last_used = jiffies;
1753ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
17544a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1755ee5ebe85STrond Myklebust 	if (req->rq_buffer)
17563435c74aSChuck Lever 		xprt->ops->buf_free(task);
17574a068258SChuck Lever 	xprt_inject_disconnect(xprt);
17589d96acbcSTrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
17590472e476STrond Myklebust 	xdr_free_bvec(&req->rq_snd_buf);
1760a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1761a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
17621da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1763ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1764ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
176555ae1aabSRicardo Labiaga 
176646121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1767ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1768a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1769ee5ebe85STrond Myklebust 	else
1770c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds 
1773902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1774902c5887STrond Myklebust void
1775902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1776902c5887STrond Myklebust {
1777902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1778902c5887STrond Myklebust 
1779902c5887STrond Myklebust 	task->tk_rqstp = req;
1780902c5887STrond Myklebust 	req->rq_task = task;
1781902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1782902c5887STrond Myklebust 	/*
1783902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1784902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1785902c5887STrond Myklebust 	 */
1786902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1787902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1788902c5887STrond Myklebust }
1789902c5887STrond Myklebust #endif
1790902c5887STrond Myklebust 
179121de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1792c2866763SChuck Lever {
179330c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1794c2866763SChuck Lever 
1795c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1796c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
179775c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1798c2866763SChuck Lever 
1799c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
180095f7691dSTrond Myklebust 	xprt->recv_queue = RB_ROOT;
1801944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
18029e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1803f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1804f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
18059e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
180680b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1807f9acac1aSRicardo Labiaga 
1808c2866763SChuck Lever 	xprt->last_used = jiffies;
1809c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1810a509050bSChuck Lever 	xprt->bind_index = 0;
1811c2866763SChuck Lever 
1812c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1813c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
181479c99152STrond Myklebust 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1815c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1816c2866763SChuck Lever 
1817c2866763SChuck Lever 	xprt_init_xid(xprt);
1818c2866763SChuck Lever 
181921de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
18208d9266ffSTrond Myklebust }
18218d9266ffSTrond Myklebust 
18228d9266ffSTrond Myklebust /**
18238d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
18248d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
18258d9266ffSTrond Myklebust  *
18268d9266ffSTrond Myklebust  */
18278d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
18288d9266ffSTrond Myklebust {
18298d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
18308d9266ffSTrond Myklebust 	struct xprt_class *t;
18318d9266ffSTrond Myklebust 
18328d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
18338d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
18348d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
18358d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
18368d9266ffSTrond Myklebust 			goto found;
18378d9266ffSTrond Myklebust 		}
18388d9266ffSTrond Myklebust 	}
18398d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
18403c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
18418d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
18428d9266ffSTrond Myklebust 
18438d9266ffSTrond Myklebust found:
18448d9266ffSTrond Myklebust 	xprt = t->setup(args);
18458d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
18468d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
18478d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
184821de0a95STrond Myklebust 		goto out;
18498d9266ffSTrond Myklebust 	}
185033d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
185133d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
185221de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
185321de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
185443123581STrond Myklebust 		timer_setup(&xprt->timer,
185543123581STrond Myklebust 				xprt_init_autodisconnect,
185643123581STrond Myklebust 				TIMER_DEFERRABLE);
185721de0a95STrond Myklebust 	else
1858ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
18594e0038b6STrond Myklebust 
18604e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
18614e0038b6STrond Myklebust 		xprt_destroy(xprt);
18624e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
18634e0038b6STrond Myklebust 	}
18644e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
18654e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
18664e0038b6STrond Myklebust 		xprt_destroy(xprt);
18674e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
18684e0038b6STrond Myklebust 	}
18694e0038b6STrond Myklebust 
18703f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1871388f0c77SJeff Layton 
1872c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1873c2866763SChuck Lever 			xprt->max_reqs);
187421de0a95STrond Myklebust out:
1875c2866763SChuck Lever 	return xprt;
1876c2866763SChuck Lever }
1877c2866763SChuck Lever 
1878528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
1879528fd354STrond Myklebust {
1880528fd354STrond Myklebust 	struct rpc_xprt *xprt =
1881528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
1882528fd354STrond Myklebust 
1883528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
1884528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1885528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1886528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1887528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1888528fd354STrond Myklebust 	kfree(xprt->servername);
1889528fd354STrond Myklebust 	/*
1890528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
1891528fd354STrond Myklebust 	 */
1892528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
1893528fd354STrond Myklebust }
1894528fd354STrond Myklebust 
18959903cd1cSChuck Lever /**
18969903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1897a8de240aSTrond Myklebust  * @xprt: transport to destroy
18989903cd1cSChuck Lever  *
18991da177e4SLinus Torvalds  */
1900a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
19011da177e4SLinus Torvalds {
19021da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
190379234c3dSTrond Myklebust 
1904528fd354STrond Myklebust 	/*
1905528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
1906528fd354STrond Myklebust 	 */
190779234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
190879234c3dSTrond Myklebust 
19090065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1910c8541ecdSChuck Lever 
1911c8541ecdSChuck Lever 	/*
1912528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
1913528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
1914c8541ecdSChuck Lever 	 */
1915528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1916528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
19176b6ca86bSTrond Myklebust }
19181da177e4SLinus Torvalds 
191930c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
192030c5116bSTrond Myklebust {
192130c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
192230c5116bSTrond Myklebust }
192330c5116bSTrond Myklebust 
192430c5116bSTrond Myklebust /**
192530c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
192630c5116bSTrond Myklebust  * @xprt: pointer to the transport
192730c5116bSTrond Myklebust  *
192830c5116bSTrond Myklebust  */
192930c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
193030c5116bSTrond Myklebust {
193130c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
193230c5116bSTrond Myklebust 		return xprt;
193330c5116bSTrond Myklebust 	return NULL;
193430c5116bSTrond Myklebust }
193530c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
193630c5116bSTrond Myklebust 
19376b6ca86bSTrond Myklebust /**
19386b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
19396b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
19406b6ca86bSTrond Myklebust  *
19416b6ca86bSTrond Myklebust  */
19426b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
19436b6ca86bSTrond Myklebust {
194430c5116bSTrond Myklebust 	if (xprt != NULL)
194530c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
19466b6ca86bSTrond Myklebust }
19475d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
1948