xref: /openbmc/linux/net/sunrpc/xprt.c (revision b5e92419)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/net/sunrpc/xprt.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This is a generic RPC call interface supporting congestion avoidance,
61da177e4SLinus Torvalds  *  and asynchronous calls.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  The interface works like this:
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  -	When a process places a call, it allocates a request slot if
111da177e4SLinus Torvalds  *	one is available. Otherwise, it sleeps on the backlog queue
121da177e4SLinus Torvalds  *	(xprt_reserve).
131da177e4SLinus Torvalds  *  -	Next, the caller puts together the RPC message, stuffs it into
1455aa4f58SChuck Lever  *	the request struct, and calls xprt_transmit().
1555aa4f58SChuck Lever  *  -	xprt_transmit sends the message and installs the caller on the
1655ae1aabSRicardo Labiaga  *	transport's wait list. At the same time, if a reply is expected,
1755ae1aabSRicardo Labiaga  *	it installs a timer that is run after the packet's timeout has
1855ae1aabSRicardo Labiaga  *	expired.
191da177e4SLinus Torvalds  *  -	When a packet arrives, the data_ready handler walks the list of
2055aa4f58SChuck Lever  *	pending requests for that transport. If a matching XID is found, the
211da177e4SLinus Torvalds  *	caller is woken up, and the timer removed.
221da177e4SLinus Torvalds  *  -	When no reply arrives within the timeout interval, the timer is
231da177e4SLinus Torvalds  *	fired by the kernel and runs xprt_timer(). It either adjusts the
241da177e4SLinus Torvalds  *	timeout values (minor timeout) or wakes up the caller with a status
251da177e4SLinus Torvalds  *	of -ETIMEDOUT.
261da177e4SLinus Torvalds  *  -	When the caller receives a notification from RPC that a reply arrived,
271da177e4SLinus Torvalds  *	it should release the RPC slot, and process the reply.
281da177e4SLinus Torvalds  *	If the call timed out, it may choose to retry the operation by
291da177e4SLinus Torvalds  *	adjusting the initial timeout value, and simply calling rpc_call
301da177e4SLinus Torvalds  *	again.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  *  Support for async RPC is done through a set of RPC-specific scheduling
331da177e4SLinus Torvalds  *  primitives that `transparently' work for processes as well as async
341da177e4SLinus Torvalds  *  tasks that rely on callbacks.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
3755aa4f58SChuck Lever  *
3855aa4f58SChuck Lever  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
41a246b010SChuck Lever #include <linux/module.h>
42a246b010SChuck Lever 
431da177e4SLinus Torvalds #include <linux/types.h>
44a246b010SChuck Lever #include <linux/interrupt.h>
451da177e4SLinus Torvalds #include <linux/workqueue.h>
46bf3fcf89SChuck Lever #include <linux/net.h>
47ff839970SChuck Lever #include <linux/ktime.h>
481da177e4SLinus Torvalds 
49a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
5011c556b3SChuck Lever #include <linux/sunrpc/metrics.h>
51c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
52fda1bfefSTrond Myklebust #include <linux/rcupdate.h>
53a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
541da177e4SLinus Torvalds 
553705ad64SJeff Layton #include <trace/events/sunrpc.h>
563705ad64SJeff Layton 
5755ae1aabSRicardo Labiaga #include "sunrpc.h"
5855ae1aabSRicardo Labiaga 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * Local variables
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds 
63f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
641da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Local functions
691da177e4SLinus Torvalds  */
7021de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
7137ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
724e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
731da177e4SLinus Torvalds 
745ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7581c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7681c098afS\"Talpey, Thomas\ 
779e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
789e910bffSTrond Myklebust {
799e910bffSTrond Myklebust 	unsigned long timeout = jiffies + req->rq_timeout;
809e910bffSTrond Myklebust 
819e910bffSTrond Myklebust 	if (time_before(timeout, req->rq_majortimeo))
829e910bffSTrond Myklebust 		return timeout;
839e910bffSTrond Myklebust 	return req->rq_majortimeo;
849e910bffSTrond Myklebust }
859e910bffSTrond Myklebust 
8612a80469SChuck Lever /**
8781c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
8881c098afS\"Talpey, Thomas\  * @transport: transport to register
8981c098afS\"Talpey, Thomas\  *
9081c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9181c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9281c098afS\"Talpey, Thomas\  *
9381c098afS\"Talpey, Thomas\  * Returns:
9481c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
9581c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
9681c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
9781c098afS\"Talpey, Thomas\  */
9881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
9981c098afS\"Talpey, Thomas\ {
10081c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10181c098afS\"Talpey, Thomas\ 	int result;
10281c098afS\"Talpey, Thomas\ 
10381c098afS\"Talpey, Thomas\ 	result = -EEXIST;
10481c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
10581c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
10681c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1074fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
10881c098afS\"Talpey, Thomas\ 			goto out;
10981c098afS\"Talpey, Thomas\ 	}
11081c098afS\"Talpey, Thomas\ 
11181c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11281c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11381c098afS\"Talpey, Thomas\ 	       transport->name);
11481c098afS\"Talpey, Thomas\ 	result = 0;
11581c098afS\"Talpey, Thomas\ 
11681c098afS\"Talpey, Thomas\ out:
11781c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
11881c098afS\"Talpey, Thomas\ 	return result;
11981c098afS\"Talpey, Thomas\ }
12081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12181c098afS\"Talpey, Thomas\ 
12281c098afS\"Talpey, Thomas\ /**
12381c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
12465b6e42cSRandy Dunlap  * @transport: transport to unregister
12581c098afS\"Talpey, Thomas\  *
12681c098afS\"Talpey, Thomas\  * Returns:
12781c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
12881c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
12981c098afS\"Talpey, Thomas\  */
13081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13181c098afS\"Talpey, Thomas\ {
13281c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13381c098afS\"Talpey, Thomas\ 	int result;
13481c098afS\"Talpey, Thomas\ 
13581c098afS\"Talpey, Thomas\ 	result = 0;
13681c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
13781c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
13881c098afS\"Talpey, Thomas\ 		if (t == transport) {
13981c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
14081c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14181c098afS\"Talpey, Thomas\ 				transport->name);
14281c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14381c098afS\"Talpey, Thomas\ 			goto out;
14481c098afS\"Talpey, Thomas\ 		}
14581c098afS\"Talpey, Thomas\ 	}
14681c098afS\"Talpey, Thomas\ 	result = -ENOENT;
14781c098afS\"Talpey, Thomas\ 
14881c098afS\"Talpey, Thomas\ out:
14981c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
15081c098afS\"Talpey, Thomas\ 	return result;
15181c098afS\"Talpey, Thomas\ }
15281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15381c098afS\"Talpey, Thomas\ 
15481c098afS\"Talpey, Thomas\ /**
155441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
156441e3e24STom Talpey  * @transport_name: transport to load
157441e3e24STom Talpey  *
158441e3e24STom Talpey  * Returns:
159441e3e24STom Talpey  * 0:		transport successfully loaded
160441e3e24STom Talpey  * -ENOENT:	transport module not available
161441e3e24STom Talpey  */
162441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
163441e3e24STom Talpey {
164441e3e24STom Talpey 	struct xprt_class *t;
165441e3e24STom Talpey 	int result;
166441e3e24STom Talpey 
167441e3e24STom Talpey 	result = 0;
168441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
169441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
170441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
171441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
172441e3e24STom Talpey 			goto out;
173441e3e24STom Talpey 		}
174441e3e24STom Talpey 	}
175441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
176ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
177441e3e24STom Talpey out:
178441e3e24STom Talpey 	return result;
179441e3e24STom Talpey }
180441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
181441e3e24STom Talpey 
182c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
183c544577dSTrond Myklebust {
184c544577dSTrond Myklebust 	xprt->snd_task = NULL;
185c544577dSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
186c544577dSTrond Myklebust 		smp_mb__before_atomic();
187c544577dSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
188c544577dSTrond Myklebust 		smp_mb__after_atomic();
189c544577dSTrond Myklebust 	} else
190c544577dSTrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
191c544577dSTrond Myklebust }
192c544577dSTrond Myklebust 
193441e3e24STom Talpey /**
19412a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
19512a80469SChuck Lever  * @task: task that is requesting access to the transport
196177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
19712a80469SChuck Lever  *
19812a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
19912a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
20012a80469SChuck Lever  * is provided.
2011da177e4SLinus Torvalds  */
20243cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
2031da177e4SLinus Torvalds {
20412a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
20512a80469SChuck Lever 
20612a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
20712a80469SChuck Lever 		if (task == xprt->snd_task)
20812a80469SChuck Lever 			return 1;
20912a80469SChuck Lever 		goto out_sleep;
21012a80469SChuck Lever 	}
211c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
212c544577dSTrond Myklebust 		goto out_unlock;
21312a80469SChuck Lever 	xprt->snd_task = task;
2144d4a76f3Sj223yang@asset.uwaterloo.ca 
21512a80469SChuck Lever 	return 1;
21612a80469SChuck Lever 
217c544577dSTrond Myklebust out_unlock:
218c544577dSTrond Myklebust 	xprt_clear_locked(xprt);
21912a80469SChuck Lever out_sleep:
22046121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
22112a80469SChuck Lever 			task->tk_pid, xprt);
22212a80469SChuck Lever 	task->tk_status = -EAGAIN;
2236b2e6856STrond Myklebust 	if  (RPC_IS_SOFT(task))
2246b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2259e910bffSTrond Myklebust 				xprt_request_timeout(req));
2266b2e6856STrond Myklebust 	else
22779c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
22812a80469SChuck Lever 	return 0;
22912a80469SChuck Lever }
23012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
23112a80469SChuck Lever 
23275891f50STrond Myklebust static bool
23375891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
23475891f50STrond Myklebust {
23575891f50STrond Myklebust 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
23675891f50STrond Myklebust }
23775891f50STrond Myklebust 
23875891f50STrond Myklebust static void
23975891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
24075891f50STrond Myklebust {
24175891f50STrond Myklebust 	if (!list_empty(&xprt->xmit_queue)) {
24275891f50STrond Myklebust 		/* Peek at head of queue to see if it can make progress */
24375891f50STrond Myklebust 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
24475891f50STrond Myklebust 					rq_xmit)->rq_cong)
24575891f50STrond Myklebust 			return;
24675891f50STrond Myklebust 	}
24775891f50STrond Myklebust 	set_bit(XPRT_CWND_WAIT, &xprt->state);
24875891f50STrond Myklebust }
24975891f50STrond Myklebust 
25075891f50STrond Myklebust static void
25175891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
25275891f50STrond Myklebust {
25375891f50STrond Myklebust 	if (!RPCXPRT_CONGESTED(xprt))
25475891f50STrond Myklebust 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
25575891f50STrond Myklebust }
25675891f50STrond Myklebust 
25712a80469SChuck Lever /*
25812a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
25912a80469SChuck Lever  * @task: task that is requesting access to the transport
26012a80469SChuck Lever  *
26112a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
26212a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
26312a80469SChuck Lever  * woken up and given access to the transport.
26475891f50STrond Myklebust  * Note that the lock is only granted if we know there are free slots.
26512a80469SChuck Lever  */
26643cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
26712a80469SChuck Lever {
2681da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
2691da177e4SLinus Torvalds 
2702226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2711da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2721da177e4SLinus Torvalds 			return 1;
2731da177e4SLinus Torvalds 		goto out_sleep;
2741da177e4SLinus Torvalds 	}
27543cedbf0STrond Myklebust 	if (req == NULL) {
27643cedbf0STrond Myklebust 		xprt->snd_task = task;
27743cedbf0STrond Myklebust 		return 1;
27843cedbf0STrond Myklebust 	}
279c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
280c544577dSTrond Myklebust 		goto out_unlock;
28175891f50STrond Myklebust 	if (!xprt_need_congestion_window_wait(xprt)) {
2821da177e4SLinus Torvalds 		xprt->snd_task = task;
2831da177e4SLinus Torvalds 		return 1;
2841da177e4SLinus Torvalds 	}
285c544577dSTrond Myklebust out_unlock:
286632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2871da177e4SLinus Torvalds out_sleep:
28846121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2891da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
2906b2e6856STrond Myklebust 	if (RPC_IS_SOFT(task))
2916b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2929e910bffSTrond Myklebust 				xprt_request_timeout(req));
2936b2e6856STrond Myklebust 	else
29479c99152STrond Myklebust 		rpc_sleep_on(&xprt->sending, task, NULL);
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
29712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2981da177e4SLinus Torvalds 
29912a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	int retval;
3021da177e4SLinus Torvalds 
303bd79bc57STrond Myklebust 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
304bd79bc57STrond Myklebust 		return 1;
305b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
30643cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
307b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
3081da177e4SLinus Torvalds 	return retval;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
311961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
3121da177e4SLinus Torvalds {
313961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
31449e9a890SChuck Lever 
31549e9a890SChuck Lever 	xprt->snd_task = task;
316961a828dSTrond Myklebust 	return true;
317961a828dSTrond Myklebust }
318961a828dSTrond Myklebust 
319961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
320961a828dSTrond Myklebust {
321961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
32249e9a890SChuck Lever 		return;
323c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
324c544577dSTrond Myklebust 		goto out_unlock;
325f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
326f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
327961a828dSTrond Myklebust 		return;
328c544577dSTrond Myklebust out_unlock:
329632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
33049e9a890SChuck Lever }
33149e9a890SChuck Lever 
332961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
333961a828dSTrond Myklebust {
334961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
335961a828dSTrond Myklebust 		return;
336c544577dSTrond Myklebust 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
337c544577dSTrond Myklebust 		goto out_unlock;
33875891f50STrond Myklebust 	if (xprt_need_congestion_window_wait(xprt))
339961a828dSTrond Myklebust 		goto out_unlock;
340f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
34175891f50STrond Myklebust 				__xprt_lock_write_func, xprt))
342961a828dSTrond Myklebust 		return;
3431da177e4SLinus Torvalds out_unlock:
344632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
34749e9a890SChuck Lever /**
34849e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
34949e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
35049e9a890SChuck Lever  * @task: task that is releasing access to the transport
35149e9a890SChuck Lever  *
35249e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3531da177e4SLinus Torvalds  */
35449e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
357632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3581da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds }
36112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3621da177e4SLinus Torvalds 
36349e9a890SChuck Lever /**
36449e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
36549e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
36649e9a890SChuck Lever  * @task: task that is releasing access to the transport
36749e9a890SChuck Lever  *
36849e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
36949e9a890SChuck Lever  * transport if the transport's congestion window allows it.
37049e9a890SChuck Lever  */
37149e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
37249e9a890SChuck Lever {
37349e9a890SChuck Lever 	if (xprt->snd_task == task) {
374632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
37549e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
37649e9a890SChuck Lever 	}
37749e9a890SChuck Lever }
37812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
37949e9a890SChuck Lever 
38049e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3811da177e4SLinus Torvalds {
382bd79bc57STrond Myklebust 	if (xprt->snd_task != task)
383bd79bc57STrond Myklebust 		return;
384b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
38549e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
386b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
3901da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3911da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3921da177e4SLinus Torvalds  */
3931da177e4SLinus Torvalds static int
39475891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	if (req->rq_cong)
3971da177e4SLinus Torvalds 		return 1;
39846121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
39975891f50STrond Myklebust 			req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
40075891f50STrond Myklebust 	if (RPCXPRT_CONGESTED(xprt)) {
40175891f50STrond Myklebust 		xprt_set_congestion_window_wait(xprt);
4021da177e4SLinus Torvalds 		return 0;
40375891f50STrond Myklebust 	}
4041da177e4SLinus Torvalds 	req->rq_cong = 1;
4051da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4061da177e4SLinus Torvalds 	return 1;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /*
4101da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4111da177e4SLinus Torvalds  * that has been sleeping due to congestion
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds static void
4141da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	if (!req->rq_cong)
4171da177e4SLinus Torvalds 		return;
4181da177e4SLinus Torvalds 	req->rq_cong = 0;
4191da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
42075891f50STrond Myklebust 	xprt_test_and_clear_congestion_window_wait(xprt);
42149e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
42446c0ee8bSChuck Lever /**
42575891f50STrond Myklebust  * xprt_request_get_cong - Request congestion control credits
42675891f50STrond Myklebust  * @xprt: pointer to transport
42775891f50STrond Myklebust  * @req: pointer to RPC request
42875891f50STrond Myklebust  *
42975891f50STrond Myklebust  * Useful for transports that require congestion control.
43075891f50STrond Myklebust  */
43175891f50STrond Myklebust bool
43275891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
43375891f50STrond Myklebust {
43475891f50STrond Myklebust 	bool ret = false;
43575891f50STrond Myklebust 
43675891f50STrond Myklebust 	if (req->rq_cong)
43775891f50STrond Myklebust 		return true;
438b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
43975891f50STrond Myklebust 	ret = __xprt_get_cong(xprt, req) != 0;
440b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
44175891f50STrond Myklebust 	return ret;
44275891f50STrond Myklebust }
44375891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong);
44475891f50STrond Myklebust 
44575891f50STrond Myklebust /**
446a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
447a58dd398SChuck Lever  * @task: RPC request that recently completed
448a58dd398SChuck Lever  *
449a58dd398SChuck Lever  * Useful for transports that require congestion control.
450a58dd398SChuck Lever  */
451a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
452a58dd398SChuck Lever {
453a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
454a4f0835cSTrond Myklebust 
455a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
456a58dd398SChuck Lever }
45712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
458a58dd398SChuck Lever 
45975891f50STrond Myklebust /*
46075891f50STrond Myklebust  * Clear the congestion window wait flag and wake up the next
46175891f50STrond Myklebust  * entry on xprt->sending
46275891f50STrond Myklebust  */
46375891f50STrond Myklebust static void
46475891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
46575891f50STrond Myklebust {
46675891f50STrond Myklebust 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
467b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
46875891f50STrond Myklebust 		__xprt_lock_write_next_cong(xprt);
469b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
47075891f50STrond Myklebust 	}
47175891f50STrond Myklebust }
47275891f50STrond Myklebust 
473a58dd398SChuck Lever /**
47446c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4756a24dfb6STrond Myklebust  * @xprt: pointer to xprt
47646c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
47746c0ee8bSChuck Lever  * @result: result code of completed RPC request
47846c0ee8bSChuck Lever  *
4794f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4804f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4814f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4824f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4834f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4844f4cf5adSChuck Lever  *
4854f4cf5adSChuck Lever  *	-	a reply is received and
4864f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4874f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4881da177e4SLinus Torvalds  */
4896a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4901da177e4SLinus Torvalds {
49146c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
49246c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4951da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4961da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4971da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4981da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4991da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
50049e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
5011da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
5021da177e4SLinus Torvalds 		cwnd >>= 1;
5031da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
5041da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
5071da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
5081da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
50946c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
5101da177e4SLinus Torvalds }
51112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
5121da177e4SLinus Torvalds 
51344fbac22SChuck Lever /**
51444fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
51544fbac22SChuck Lever  * @xprt: transport with waiting tasks
51644fbac22SChuck Lever  * @status: result code to plant in each task before waking it
51744fbac22SChuck Lever  *
51844fbac22SChuck Lever  */
51944fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
52044fbac22SChuck Lever {
52144fbac22SChuck Lever 	if (status < 0)
52244fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
52344fbac22SChuck Lever 	else
52444fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
52544fbac22SChuck Lever }
52612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
52744fbac22SChuck Lever 
528c7b2cae8SChuck Lever /**
529c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
530c544577dSTrond Myklebust  * @xprt: transport
531a9a6b52eSTrond Myklebust  *
532a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
533a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
534a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
535c7b2cae8SChuck Lever  */
536c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
537c7b2cae8SChuck Lever {
538c544577dSTrond Myklebust 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
539c7b2cae8SChuck Lever }
54012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
541c7b2cae8SChuck Lever 
542c544577dSTrond Myklebust static bool
543c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt)
544c544577dSTrond Myklebust {
545c544577dSTrond Myklebust 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
546c544577dSTrond Myklebust 		__xprt_lock_write_next(xprt);
547c544577dSTrond Myklebust 		dprintk("RPC:       write space: waking waiting task on "
548c544577dSTrond Myklebust 				"xprt %p\n", xprt);
549c544577dSTrond Myklebust 		return true;
550c544577dSTrond Myklebust 	}
551c544577dSTrond Myklebust 	return false;
552c544577dSTrond Myklebust }
553c544577dSTrond Myklebust 
554c7b2cae8SChuck Lever /**
555c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
556c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
557c7b2cae8SChuck Lever  *
558c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
559c7b2cae8SChuck Lever  */
560c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt)
561c7b2cae8SChuck Lever {
562c544577dSTrond Myklebust 	bool ret;
563c544577dSTrond Myklebust 
564c544577dSTrond Myklebust 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
565c544577dSTrond Myklebust 		return false;
566b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
567c544577dSTrond Myklebust 	ret = xprt_clear_write_space_locked(xprt);
568b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
569c544577dSTrond Myklebust 	return ret;
570c7b2cae8SChuck Lever }
57112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
572c7b2cae8SChuck Lever 
573da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
574da953063STrond Myklebust {
575da953063STrond Myklebust 	s64 delta = ktime_to_ns(ktime_get() - abstime);
576da953063STrond Myklebust 	return likely(delta >= 0) ?
577da953063STrond Myklebust 		jiffies - nsecs_to_jiffies(delta) :
578da953063STrond Myklebust 		jiffies + nsecs_to_jiffies(-delta);
579da953063STrond Myklebust }
580da953063STrond Myklebust 
581da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
5821da177e4SLinus Torvalds {
583ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
584da953063STrond Myklebust 	unsigned long majortimeo = req->rq_timeout;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (to->to_exponential)
587da953063STrond Myklebust 		majortimeo <<= to->to_retries;
5881da177e4SLinus Torvalds 	else
589da953063STrond Myklebust 		majortimeo += to->to_increment * to->to_retries;
590da953063STrond Myklebust 	if (majortimeo > to->to_maxval || majortimeo == 0)
591da953063STrond Myklebust 		majortimeo = to->to_maxval;
592da953063STrond Myklebust 	return majortimeo;
593da953063STrond Myklebust }
594da953063STrond Myklebust 
595da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req)
596da953063STrond Myklebust {
597da953063STrond Myklebust 	req->rq_majortimeo += xprt_calc_majortimeo(req);
598da953063STrond Myklebust }
599da953063STrond Myklebust 
600da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
601da953063STrond Myklebust {
602da953063STrond Myklebust 	unsigned long time_init;
603da953063STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
604da953063STrond Myklebust 
605da953063STrond Myklebust 	if (likely(xprt && xprt_connected(xprt)))
606da953063STrond Myklebust 		time_init = jiffies;
607da953063STrond Myklebust 	else
608da953063STrond Myklebust 		time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
609da953063STrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
610da953063STrond Myklebust 	req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6139903cd1cSChuck Lever /**
6149903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
6159903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
6169903cd1cSChuck Lever  *
6171da177e4SLinus Torvalds  */
6181da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
621ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
6221da177e4SLinus Torvalds 	int status = 0;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
6251da177e4SLinus Torvalds 		if (to->to_exponential)
6261da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
6271da177e4SLinus Torvalds 		else
6281da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
6291da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
6301da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
6311da177e4SLinus Torvalds 		req->rq_retries++;
6321da177e4SLinus Torvalds 	} else {
6331da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
6341da177e4SLinus Torvalds 		req->rq_retries = 0;
6351da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
6361da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
637b5e92419STrond Myklebust 		spin_lock(&xprt->transport_lock);
6381da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
639b5e92419STrond Myklebust 		spin_unlock(&xprt->transport_lock);
6401da177e4SLinus Torvalds 		status = -ETIMEDOUT;
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
6441da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6451da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 	return status;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
65065f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6511da177e4SLinus Torvalds {
65265f27f38SDavid Howells 	struct rpc_xprt *xprt =
65365f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
654a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
6551da177e4SLinus Torvalds 
65666af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6574876cc77STrond Myklebust 	xprt->ops->close(xprt);
6581da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
65979234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
660a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6639903cd1cSChuck Lever /**
66462da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6659903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6669903cd1cSChuck Lever  *
6671da177e4SLinus Torvalds  */
66862da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
671b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
6721da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
673c544577dSTrond Myklebust 	xprt_clear_write_space_locked(xprt);
67427adc785STrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
675b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
6761da177e4SLinus Torvalds }
67762da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6781da177e4SLinus Torvalds 
67966af1e55STrond Myklebust /**
68066af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
68166af1e55STrond Myklebust  * @xprt: transport to disconnect
68266af1e55STrond Myklebust  *
68366af1e55STrond Myklebust  */
68466af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
68566af1e55STrond Myklebust {
68666af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
687b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
68866af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
68966af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
69066af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
69140a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6920445f92cSTrond Myklebust 	else if (xprt->snd_task)
6930445f92cSTrond Myklebust 		rpc_wake_up_queued_task_set_status(&xprt->pending,
6940445f92cSTrond Myklebust 				xprt->snd_task, -ENOTCONN);
695b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
69666af1e55STrond Myklebust }
697e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
69866af1e55STrond Myklebust 
6997f3a1d1eSTrond Myklebust static unsigned int
7007f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
7017f3a1d1eSTrond Myklebust {
7027f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
7037f3a1d1eSTrond Myklebust }
7047f3a1d1eSTrond Myklebust 
7057f3a1d1eSTrond Myklebust static bool
7067f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
7077f3a1d1eSTrond Myklebust {
7087f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
7097f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
7107f3a1d1eSTrond Myklebust 
7117f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
7127f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
7137f3a1d1eSTrond Myklebust }
7147f3a1d1eSTrond Myklebust 
7157c1d71cfSTrond Myklebust /**
7167c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
7177c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
7187c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
7197c1d71cfSTrond Myklebust  *
7207c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
7217c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
7227c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
7237c1d71cfSTrond Myklebust  * a batch of RPC requests.
7247c1d71cfSTrond Myklebust  *
7257c1d71cfSTrond Myklebust  */
7267c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
7277c1d71cfSTrond Myklebust {
7287c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
729b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
7307c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
7317c1d71cfSTrond Myklebust 		goto out;
7322c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
7337c1d71cfSTrond Myklebust 		goto out;
7347c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
7357c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
7367c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
73740a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7382a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
7397c1d71cfSTrond Myklebust out:
740b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
7417c1d71cfSTrond Myklebust }
7427c1d71cfSTrond Myklebust 
743ad3331acSTrond Myklebust static bool
744ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
745ad3331acSTrond Myklebust {
746ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
747ad3331acSTrond Myklebust }
748ad3331acSTrond Myklebust 
749ad3331acSTrond Myklebust static void
750ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
751ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
752ad3331acSTrond Myklebust {
75395f7691dSTrond Myklebust 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
754ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
755ad3331acSTrond Myklebust }
756ad3331acSTrond Myklebust 
7571da177e4SLinus Torvalds static void
758ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
7591da177e4SLinus Torvalds {
760ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
7611da177e4SLinus Torvalds 
76295f7691dSTrond Myklebust 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
763b5e92419STrond Myklebust 		return;
764ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
765ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
7662226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
7671da177e4SLinus Torvalds 		return;
768b5e92419STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
772718ba5b8STrond Myklebust 		struct rpc_task *task,
773718ba5b8STrond Myklebust 		void *cookie)
774718ba5b8STrond Myklebust {
775718ba5b8STrond Myklebust 	bool ret = false;
776718ba5b8STrond Myklebust 
777b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
778718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
779718ba5b8STrond Myklebust 		goto out;
780718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
781718ba5b8STrond Myklebust 		goto out;
782718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
783718ba5b8STrond Myklebust 	ret = true;
784718ba5b8STrond Myklebust out:
785b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
786718ba5b8STrond Myklebust 	return ret;
787718ba5b8STrond Myklebust }
788718ba5b8STrond Myklebust 
789718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
790718ba5b8STrond Myklebust {
791b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
792718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
793718ba5b8STrond Myklebust 		goto out;
794718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
795718ba5b8STrond Myklebust 		goto out;
796718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
797718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
798ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
799718ba5b8STrond Myklebust out:
800b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
80179234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
802718ba5b8STrond Myklebust }
803718ba5b8STrond Myklebust 
8049903cd1cSChuck Lever /**
8059903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
8069903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
8071da177e4SLinus Torvalds  *
8081da177e4SLinus Torvalds  */
8091da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
8101da177e4SLinus Torvalds {
811ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
8121da177e4SLinus Torvalds 
81346121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
8141da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
8151da177e4SLinus Torvalds 
816ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
81701d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
8181da177e4SLinus Torvalds 		return;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
8211da177e4SLinus Torvalds 		return;
822feb8ca37STrond Myklebust 
823feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
824feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
825feb8ca37STrond Myklebust 
826718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
8272c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
8286b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
8299e910bffSTrond Myklebust 				xprt_request_timeout(task->tk_rqstp));
8300b9e7943STrond Myklebust 
8310b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
8320b9e7943STrond Myklebust 			return;
8330b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
8340b9e7943STrond Myklebust 			return;
8350a9a4304STrond Myklebust 		/* Race breaker */
8360a9a4304STrond Myklebust 		if (!xprt_connected(xprt)) {
837262ca07dSChuck Lever 			xprt->stat.connect_start = jiffies;
8381b092092STrond Myklebust 			xprt->ops->connect(xprt, task);
8390a9a4304STrond Myklebust 		} else {
8400a9a4304STrond Myklebust 			xprt_clear_connecting(xprt);
8410a9a4304STrond Myklebust 			task->tk_status = 0;
8420a9a4304STrond Myklebust 			rpc_wake_up_queued_task(&xprt->pending, task);
8430a9a4304STrond Myklebust 		}
8441da177e4SLinus Torvalds 	}
845718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
84895f7691dSTrond Myklebust enum xprt_xid_rb_cmp {
84995f7691dSTrond Myklebust 	XID_RB_EQUAL,
85095f7691dSTrond Myklebust 	XID_RB_LEFT,
85195f7691dSTrond Myklebust 	XID_RB_RIGHT,
85295f7691dSTrond Myklebust };
85395f7691dSTrond Myklebust static enum xprt_xid_rb_cmp
85495f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2)
85595f7691dSTrond Myklebust {
85695f7691dSTrond Myklebust 	if (xid1 == xid2)
85795f7691dSTrond Myklebust 		return XID_RB_EQUAL;
85895f7691dSTrond Myklebust 	if ((__force u32)xid1 < (__force u32)xid2)
85995f7691dSTrond Myklebust 		return XID_RB_LEFT;
86095f7691dSTrond Myklebust 	return XID_RB_RIGHT;
86195f7691dSTrond Myklebust }
86295f7691dSTrond Myklebust 
86395f7691dSTrond Myklebust static struct rpc_rqst *
86495f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
86595f7691dSTrond Myklebust {
86695f7691dSTrond Myklebust 	struct rb_node *n = xprt->recv_queue.rb_node;
86795f7691dSTrond Myklebust 	struct rpc_rqst *req;
86895f7691dSTrond Myklebust 
86995f7691dSTrond Myklebust 	while (n != NULL) {
87095f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
87195f7691dSTrond Myklebust 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
87295f7691dSTrond Myklebust 		case XID_RB_LEFT:
87395f7691dSTrond Myklebust 			n = n->rb_left;
87495f7691dSTrond Myklebust 			break;
87595f7691dSTrond Myklebust 		case XID_RB_RIGHT:
87695f7691dSTrond Myklebust 			n = n->rb_right;
87795f7691dSTrond Myklebust 			break;
87895f7691dSTrond Myklebust 		case XID_RB_EQUAL:
87995f7691dSTrond Myklebust 			return req;
88095f7691dSTrond Myklebust 		}
88195f7691dSTrond Myklebust 	}
88295f7691dSTrond Myklebust 	return NULL;
88395f7691dSTrond Myklebust }
88495f7691dSTrond Myklebust 
88595f7691dSTrond Myklebust static void
88695f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
88795f7691dSTrond Myklebust {
88895f7691dSTrond Myklebust 	struct rb_node **p = &xprt->recv_queue.rb_node;
88995f7691dSTrond Myklebust 	struct rb_node *n = NULL;
89095f7691dSTrond Myklebust 	struct rpc_rqst *req;
89195f7691dSTrond Myklebust 
89295f7691dSTrond Myklebust 	while (*p != NULL) {
89395f7691dSTrond Myklebust 		n = *p;
89495f7691dSTrond Myklebust 		req = rb_entry(n, struct rpc_rqst, rq_recv);
89595f7691dSTrond Myklebust 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
89695f7691dSTrond Myklebust 		case XID_RB_LEFT:
89795f7691dSTrond Myklebust 			p = &n->rb_left;
89895f7691dSTrond Myklebust 			break;
89995f7691dSTrond Myklebust 		case XID_RB_RIGHT:
90095f7691dSTrond Myklebust 			p = &n->rb_right;
90195f7691dSTrond Myklebust 			break;
90295f7691dSTrond Myklebust 		case XID_RB_EQUAL:
90395f7691dSTrond Myklebust 			WARN_ON_ONCE(new != req);
90495f7691dSTrond Myklebust 			return;
90595f7691dSTrond Myklebust 		}
90695f7691dSTrond Myklebust 	}
90795f7691dSTrond Myklebust 	rb_link_node(&new->rq_recv, n, p);
90895f7691dSTrond Myklebust 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
90995f7691dSTrond Myklebust }
91095f7691dSTrond Myklebust 
91195f7691dSTrond Myklebust static void
91295f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
91395f7691dSTrond Myklebust {
91495f7691dSTrond Myklebust 	rb_erase(&req->rq_recv, &xprt->recv_queue);
91595f7691dSTrond Myklebust }
91695f7691dSTrond Myklebust 
9179903cd1cSChuck Lever /**
9189903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
9199903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
9209903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
9219903cd1cSChuck Lever  *
92275c84151STrond Myklebust  * Caller holds xprt->queue_lock.
9231da177e4SLinus Torvalds  */
924d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
9251da177e4SLinus Torvalds {
9268f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
9271da177e4SLinus Torvalds 
92895f7691dSTrond Myklebust 	entry = xprt_request_rb_find(xprt, xid);
92995f7691dSTrond Myklebust 	if (entry != NULL) {
9303705ad64SJeff Layton 		trace_xprt_lookup_rqst(xprt, xid, 0);
9310b87a46bSChuck Lever 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
932262ca07dSChuck Lever 		return entry;
9333705ad64SJeff Layton 	}
93446121cf7SChuck Lever 
93546121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
93646121cf7SChuck Lever 			ntohl(xid));
9373705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
938262ca07dSChuck Lever 	xprt->stat.bad_xids++;
939262ca07dSChuck Lever 	return NULL;
9401da177e4SLinus Torvalds }
94112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
9421da177e4SLinus Torvalds 
943cf9946cdSTrond Myklebust static bool
944cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
945cf9946cdSTrond Myklebust {
946cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
947cf9946cdSTrond Myklebust }
948cf9946cdSTrond Myklebust 
949729749bbSTrond Myklebust /**
950729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
951729749bbSTrond Myklebust  * @req: Request to pin
952729749bbSTrond Myklebust  *
953729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
9541f7d1c73SChuck Lever  * so should be holding xprt->queue_lock.
955729749bbSTrond Myklebust  */
956729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
957729749bbSTrond Myklebust {
958cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
959729749bbSTrond Myklebust }
9609590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
961729749bbSTrond Myklebust 
962729749bbSTrond Myklebust /**
963729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
964729749bbSTrond Myklebust  * @req: Request to pin
965729749bbSTrond Myklebust  *
9661f7d1c73SChuck Lever  * Caller should be holding xprt->queue_lock.
967729749bbSTrond Myklebust  */
968729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
969729749bbSTrond Myklebust {
970cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
971cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
972cf9946cdSTrond Myklebust 		return;
973cf9946cdSTrond Myklebust 	}
974cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
975cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
976729749bbSTrond Myklebust }
9779590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
978729749bbSTrond Myklebust 
979729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
980729749bbSTrond Myklebust {
981cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
982729749bbSTrond Myklebust }
983729749bbSTrond Myklebust 
984edc81dcdSTrond Myklebust static bool
985edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
986edc81dcdSTrond Myklebust {
987edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
988edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
989edc81dcdSTrond Myklebust }
990edc81dcdSTrond Myklebust 
991edc81dcdSTrond Myklebust static bool
992edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
993edc81dcdSTrond Myklebust {
994edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
995edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
996edc81dcdSTrond Myklebust }
997edc81dcdSTrond Myklebust 
998edc81dcdSTrond Myklebust /**
999edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
1000edc81dcdSTrond Myklebust  * @task: RPC task
1001edc81dcdSTrond Myklebust  *
1002edc81dcdSTrond Myklebust  */
1003edc81dcdSTrond Myklebust void
1004edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
1005edc81dcdSTrond Myklebust {
1006edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1007edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1008edc81dcdSTrond Myklebust 
1009edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
1010edc81dcdSTrond Myklebust 		return;
1011edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
1012edc81dcdSTrond Myklebust 
1013edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
1014edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1015edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
1016edc81dcdSTrond Myklebust 
1017edc81dcdSTrond Myklebust 	/* Add request to the receive list */
101895f7691dSTrond Myklebust 	xprt_request_rb_insert(xprt, req);
1019edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1020edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
1021edc81dcdSTrond Myklebust 
1022edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
1023edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
1024edc81dcdSTrond Myklebust }
1025edc81dcdSTrond Myklebust 
1026edc81dcdSTrond Myklebust /**
1027edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1028edc81dcdSTrond Myklebust  * @task: RPC task
1029edc81dcdSTrond Myklebust  *
1030edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
1031edc81dcdSTrond Myklebust  */
1032edc81dcdSTrond Myklebust static void
1033edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
1034edc81dcdSTrond Myklebust {
103595f7691dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
103695f7691dSTrond Myklebust 
1037edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
103895f7691dSTrond Myklebust 		xprt_request_rb_remove(req->rq_xprt, req);
1039edc81dcdSTrond Myklebust }
1040edc81dcdSTrond Myklebust 
1041ecd465eeSChuck Lever /**
1042ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
1043ecd465eeSChuck Lever  * @task: RPC request that recently completed
1044ecd465eeSChuck Lever  *
104575c84151STrond Myklebust  * Caller holds xprt->queue_lock.
1046ecd465eeSChuck Lever  */
1047ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
10481da177e4SLinus Torvalds {
10491570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
10501570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
105195c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1052d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
10531570c1e4SChuck Lever 
10541da177e4SLinus Torvalds 	if (timer) {
10551da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
1056ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
10571570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds }
1060ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
10611da177e4SLinus Torvalds 
10621570c1e4SChuck Lever /**
10631570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
10641570c1e4SChuck Lever  * @task: RPC request that recently completed
10651570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
10661570c1e4SChuck Lever  *
106775c84151STrond Myklebust  * Caller holds xprt->queue_lock.
10681570c1e4SChuck Lever  */
10691570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
10701570c1e4SChuck Lever {
10711570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
1072fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
10731da177e4SLinus Torvalds 
10741570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
10751570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
10763705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
10771da177e4SLinus Torvalds 
1078fda13939STrond Myklebust 	xprt->stat.recvs++;
1079ef759a2eSChuck Lever 
10801e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1081dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1082dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
108343ac3f29STrond Myklebust 	smp_wmb();
1084dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1085edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1086fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
10871da177e4SLinus Torvalds }
108812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
10891da177e4SLinus Torvalds 
109046c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
10931da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
10941da177e4SLinus Torvalds 
10955d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
10965d00837bSTrond Myklebust 		return;
109746c0ee8bSChuck Lever 
109882476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1099dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
110046c0ee8bSChuck Lever 		if (xprt->ops->timer)
11016a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
11025d00837bSTrond Myklebust 	} else
11035d00837bSTrond Myklebust 		task->tk_status = 0;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11069903cd1cSChuck Lever /**
11078ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_def - wait for reply
11088ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
11098ba6a92dSTrond Myklebust  *
11108ba6a92dSTrond Myklebust  * Set a request's retransmit timeout based on the transport's
11118ba6a92dSTrond Myklebust  * default timeout parameters.  Used by transports that don't adjust
11128ba6a92dSTrond Myklebust  * the retransmit timeout based on round-trip time estimation,
11138ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
11148ba6a92dSTrond Myklebust  */
11158ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task)
11168ba6a92dSTrond Myklebust {
11178ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11188ba6a92dSTrond Myklebust 
11196b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
11209e910bffSTrond Myklebust 			xprt_request_timeout(req));
11218ba6a92dSTrond Myklebust }
11228ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
11238ba6a92dSTrond Myklebust 
11248ba6a92dSTrond Myklebust /**
11258ba6a92dSTrond Myklebust  * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
11268ba6a92dSTrond Myklebust  * @task: pointer to rpc_task
11278ba6a92dSTrond Myklebust  *
11288ba6a92dSTrond Myklebust  * Set a request's retransmit timeout using the RTT estimator,
11298ba6a92dSTrond Myklebust  * and put the task to sleep on the pending queue.
11308ba6a92dSTrond Myklebust  */
11318ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
11328ba6a92dSTrond Myklebust {
11338ba6a92dSTrond Myklebust 	int timer = task->tk_msg.rpc_proc->p_timer;
11348ba6a92dSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
11358ba6a92dSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
11368ba6a92dSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11378ba6a92dSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
11386b2e6856STrond Myklebust 	unsigned long timeout;
11398ba6a92dSTrond Myklebust 
11406b2e6856STrond Myklebust 	timeout = rpc_calc_rto(rtt, timer);
11416b2e6856STrond Myklebust 	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
11426b2e6856STrond Myklebust 	if (timeout > max_timeout || timeout == 0)
11436b2e6856STrond Myklebust 		timeout = max_timeout;
11446b2e6856STrond Myklebust 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
11456b2e6856STrond Myklebust 			jiffies + timeout);
11468ba6a92dSTrond Myklebust }
11478ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
11488ba6a92dSTrond Myklebust 
11498ba6a92dSTrond Myklebust /**
11507f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
11517f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
11527f3a1d1eSTrond Myklebust  *
11537f3a1d1eSTrond Myklebust  */
11547f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
11557f3a1d1eSTrond Myklebust {
11567f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
11577f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
11587f3a1d1eSTrond Myklebust 
11597f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
11607f3a1d1eSTrond Myklebust 		return;
11617f3a1d1eSTrond Myklebust 	/*
11627f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
11637f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
11647f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
11657f3a1d1eSTrond Myklebust 	 */
11667f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
11677f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
11688ba6a92dSTrond Myklebust 		xprt->ops->wait_for_reply_request(task);
11697f3a1d1eSTrond Myklebust 		/*
11707f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
11717f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
11727f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
11737f3a1d1eSTrond Myklebust 		 */
11747f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
11757f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
11767f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
11777f3a1d1eSTrond Myklebust 	}
11787f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
11797f3a1d1eSTrond Myklebust }
11807f3a1d1eSTrond Myklebust 
1181944b0429STrond Myklebust static bool
1182944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1183944b0429STrond Myklebust {
1184762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1185944b0429STrond Myklebust }
1186944b0429STrond Myklebust 
1187944b0429STrond Myklebust /**
1188944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1189944b0429STrond Myklebust  * @task: pointer to rpc_task
1190944b0429STrond Myklebust  *
1191944b0429STrond Myklebust  * Add a task to the transmission queue.
1192944b0429STrond Myklebust  */
1193944b0429STrond Myklebust void
1194944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1195944b0429STrond Myklebust {
1196918f3c1fSTrond Myklebust 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1197944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1198944b0429STrond Myklebust 
1199944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1200e66721f0STrond Myklebust 		req->rq_bytes_sent = 0;
1201944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
120275891f50STrond Myklebust 		/*
120375891f50STrond Myklebust 		 * Requests that carry congestion control credits are added
120475891f50STrond Myklebust 		 * to the head of the list to avoid starvation issues.
120575891f50STrond Myklebust 		 */
120675891f50STrond Myklebust 		if (req->rq_cong) {
120775891f50STrond Myklebust 			xprt_clear_congestion_window_wait(xprt);
120875891f50STrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
120975891f50STrond Myklebust 				if (pos->rq_cong)
121075891f50STrond Myklebust 					continue;
121175891f50STrond Myklebust 				/* Note: req is added _before_ pos */
121275891f50STrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
121375891f50STrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
12140c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 1);
121575891f50STrond Myklebust 				goto out;
121675891f50STrond Myklebust 			}
121786aeee0eSTrond Myklebust 		} else if (RPC_IS_SWAPPER(task)) {
121886aeee0eSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
121986aeee0eSTrond Myklebust 				if (pos->rq_cong || pos->rq_bytes_sent)
122086aeee0eSTrond Myklebust 					continue;
122186aeee0eSTrond Myklebust 				if (RPC_IS_SWAPPER(pos->rq_task))
122286aeee0eSTrond Myklebust 					continue;
122386aeee0eSTrond Myklebust 				/* Note: req is added _before_ pos */
122486aeee0eSTrond Myklebust 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
122586aeee0eSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit2);
12260c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 2);
122786aeee0eSTrond Myklebust 				goto out;
122886aeee0eSTrond Myklebust 			}
1229deaa5c96SChuck Lever 		} else if (!req->rq_seqno) {
1230918f3c1fSTrond Myklebust 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1231918f3c1fSTrond Myklebust 				if (pos->rq_task->tk_owner != task->tk_owner)
1232918f3c1fSTrond Myklebust 					continue;
1233918f3c1fSTrond Myklebust 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1234918f3c1fSTrond Myklebust 				INIT_LIST_HEAD(&req->rq_xmit);
12350c77668dSChuck Lever 				trace_xprt_enq_xmit(task, 3);
1236918f3c1fSTrond Myklebust 				goto out;
1237918f3c1fSTrond Myklebust 			}
123875891f50STrond Myklebust 		}
1239944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1240918f3c1fSTrond Myklebust 		INIT_LIST_HEAD(&req->rq_xmit2);
12410c77668dSChuck Lever 		trace_xprt_enq_xmit(task, 4);
1242918f3c1fSTrond Myklebust out:
1243944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1244944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1245944b0429STrond Myklebust 	}
1246944b0429STrond Myklebust }
1247944b0429STrond Myklebust 
1248944b0429STrond Myklebust /**
1249944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1250944b0429STrond Myklebust  * @task: pointer to rpc_task
1251944b0429STrond Myklebust  *
1252944b0429STrond Myklebust  * Remove a task from the transmission queue
1253944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1254944b0429STrond Myklebust  */
1255944b0429STrond Myklebust static void
1256944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1257944b0429STrond Myklebust {
1258918f3c1fSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1259918f3c1fSTrond Myklebust 
1260918f3c1fSTrond Myklebust 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1261918f3c1fSTrond Myklebust 		return;
1262918f3c1fSTrond Myklebust 	if (!list_empty(&req->rq_xmit)) {
1263918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit);
1264918f3c1fSTrond Myklebust 		if (!list_empty(&req->rq_xmit2)) {
1265918f3c1fSTrond Myklebust 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1266918f3c1fSTrond Myklebust 					struct rpc_rqst, rq_xmit2);
1267918f3c1fSTrond Myklebust 			list_del(&req->rq_xmit2);
1268918f3c1fSTrond Myklebust 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1269918f3c1fSTrond Myklebust 		}
1270918f3c1fSTrond Myklebust 	} else
1271918f3c1fSTrond Myklebust 		list_del(&req->rq_xmit2);
1272944b0429STrond Myklebust }
1273944b0429STrond Myklebust 
1274944b0429STrond Myklebust /**
1275944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1276944b0429STrond Myklebust  * @task: pointer to rpc_task
1277944b0429STrond Myklebust  *
1278944b0429STrond Myklebust  * Remove a task from the transmission queue
1279944b0429STrond Myklebust  */
1280944b0429STrond Myklebust static void
1281944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1282944b0429STrond Myklebust {
1283944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1284944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1285944b0429STrond Myklebust 
1286944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1287944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1288944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1289944b0429STrond Myklebust }
1290944b0429STrond Myklebust 
12917f3a1d1eSTrond Myklebust /**
12929d96acbcSTrond Myklebust  * xprt_request_prepare - prepare an encoded request for transport
12939d96acbcSTrond Myklebust  * @req: pointer to rpc_rqst
12949d96acbcSTrond Myklebust  *
12959d96acbcSTrond Myklebust  * Calls into the transport layer to do whatever is needed to prepare
12969d96acbcSTrond Myklebust  * the request for transmission or receive.
12979d96acbcSTrond Myklebust  */
12989d96acbcSTrond Myklebust void
12999d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req)
13009d96acbcSTrond Myklebust {
13019d96acbcSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
13029d96acbcSTrond Myklebust 
13039d96acbcSTrond Myklebust 	if (xprt->ops->prepare_request)
13049d96acbcSTrond Myklebust 		xprt->ops->prepare_request(req);
13059d96acbcSTrond Myklebust }
13069d96acbcSTrond Myklebust 
13079d96acbcSTrond Myklebust /**
1308762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1309762e4e67STrond Myklebust  * @task: pointer to rpc_task
1310762e4e67STrond Myklebust  *
1311762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1312762e4e67STrond Myklebust  */
1313762e4e67STrond Myklebust bool
1314762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1315762e4e67STrond Myklebust {
1316762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1317762e4e67STrond Myklebust }
1318762e4e67STrond Myklebust 
1319762e4e67STrond Myklebust /**
13209903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
13219903cd1cSChuck Lever  * @task: RPC task about to send a request
13229903cd1cSChuck Lever  *
13231da177e4SLinus Torvalds  */
132490051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
13271da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
13281da177e4SLinus Torvalds 
132946121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
13301da177e4SLinus Torvalds 
13315f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
13325f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1333944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
13345f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
13355f2f6bd9STrond Myklebust 					task, 0);
13365f2f6bd9STrond Myklebust 		return false;
13375f2f6bd9STrond Myklebust 
13388a19a0b6STrond Myklebust 	}
13395f2f6bd9STrond Myklebust 	return true;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
13435e5ce5beSTrond Myklebust {
1344343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
13455e5ce5beSTrond Myklebust }
13465e5ce5beSTrond Myklebust 
13479903cd1cSChuck Lever /**
134889f90fe1STrond Myklebust  * xprt_request_transmit - send an RPC request on a transport
134989f90fe1STrond Myklebust  * @req: pointer to request to transmit
135089f90fe1STrond Myklebust  * @snd_task: RPC task that owns the transport lock
13519903cd1cSChuck Lever  *
135289f90fe1STrond Myklebust  * This performs the transmission of a single request.
135389f90fe1STrond Myklebust  * Note that if the request is not the same as snd_task, then it
135489f90fe1STrond Myklebust  * does need to be pinned.
135589f90fe1STrond Myklebust  * Returns '0' on success.
13569903cd1cSChuck Lever  */
135789f90fe1STrond Myklebust static int
135889f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
136189f90fe1STrond Myklebust 	struct rpc_task *task = req->rq_task;
136290d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1363dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1364ff699ea8SChuck Lever 	int status;
13651da177e4SLinus Torvalds 
1366edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
136789f90fe1STrond Myklebust 		if (xprt_request_data_received(task)) {
136889f90fe1STrond Myklebust 			status = 0;
1369944b0429STrond Myklebust 			goto out_dequeue;
137089f90fe1STrond Myklebust 		}
13713021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1372edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
137389f90fe1STrond Myklebust 			status = -EBADMSG;
1374944b0429STrond Myklebust 			goto out_dequeue;
13753021a5bbSTrond Myklebust 		}
1376a79f194aSTrond Myklebust 		if (task->tk_ops->rpc_call_prepare_transmit) {
1377a79f194aSTrond Myklebust 			task->tk_ops->rpc_call_prepare_transmit(task,
1378a79f194aSTrond Myklebust 					task->tk_calldata);
1379a79f194aSTrond Myklebust 			status = task->tk_status;
1380a79f194aSTrond Myklebust 			if (status < 0)
1381a79f194aSTrond Myklebust 				goto out_dequeue;
1382a79f194aSTrond Myklebust 		}
1383ae67bd38STrond Myklebust 		if (RPC_SIGNALLED(task)) {
1384ae67bd38STrond Myklebust 			status = -ERESTARTSYS;
1385ae67bd38STrond Myklebust 			goto out_dequeue;
1386ae67bd38STrond Myklebust 		}
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 
1389dcbbeda8STrond Myklebust 	/*
1390dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1391dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1392dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1393dcbbeda8STrond Myklebust 	 */
1394dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1395dcbbeda8STrond Myklebust 
139690d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1397adfa7144STrond Myklebust 	status = xprt->ops->send_request(req);
1398c8485e4dSTrond Myklebust 	if (status != 0) {
1399dcbbeda8STrond Myklebust 		req->rq_ntrans--;
14000c77668dSChuck Lever 		trace_xprt_transmit(req, status);
140189f90fe1STrond Myklebust 		return status;
1402c8485e4dSTrond Myklebust 	}
14037ebbbc6eSTrond Myklebust 
1404dcbbeda8STrond Myklebust 	if (is_retrans)
1405dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1406dcbbeda8STrond Myklebust 
14074a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1408c8485e4dSTrond Myklebust 
1409468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1410b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
1411262ca07dSChuck Lever 
1412262ca07dSChuck Lever 	xprt->stat.sends++;
1413262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1414262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
141515a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
141615a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1417b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
141890d91b0cSTrond Myklebust 
141990d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1420944b0429STrond Myklebust out_dequeue:
14210c77668dSChuck Lever 	trace_xprt_transmit(req, status);
1422944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
142389f90fe1STrond Myklebust 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
142489f90fe1STrond Myklebust 	return status;
142589f90fe1STrond Myklebust }
142689f90fe1STrond Myklebust 
142789f90fe1STrond Myklebust /**
142889f90fe1STrond Myklebust  * xprt_transmit - send an RPC request on a transport
142989f90fe1STrond Myklebust  * @task: controlling RPC task
143089f90fe1STrond Myklebust  *
143189f90fe1STrond Myklebust  * Attempts to drain the transmit queue. On exit, either the transport
143289f90fe1STrond Myklebust  * signalled an error that needs to be handled before transmission can
143389f90fe1STrond Myklebust  * resume, or @task finished transmitting, and detected that it already
143489f90fe1STrond Myklebust  * received a reply.
143589f90fe1STrond Myklebust  */
143689f90fe1STrond Myklebust void
143789f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task)
143889f90fe1STrond Myklebust {
143989f90fe1STrond Myklebust 	struct rpc_rqst *next, *req = task->tk_rqstp;
144089f90fe1STrond Myklebust 	struct rpc_xprt	*xprt = req->rq_xprt;
144189f90fe1STrond Myklebust 	int status;
144289f90fe1STrond Myklebust 
144389f90fe1STrond Myklebust 	spin_lock(&xprt->queue_lock);
144489f90fe1STrond Myklebust 	while (!list_empty(&xprt->xmit_queue)) {
144589f90fe1STrond Myklebust 		next = list_first_entry(&xprt->xmit_queue,
144689f90fe1STrond Myklebust 				struct rpc_rqst, rq_xmit);
144789f90fe1STrond Myklebust 		xprt_pin_rqst(next);
144889f90fe1STrond Myklebust 		spin_unlock(&xprt->queue_lock);
144989f90fe1STrond Myklebust 		status = xprt_request_transmit(next, task);
145089f90fe1STrond Myklebust 		if (status == -EBADMSG && next != req)
145189f90fe1STrond Myklebust 			status = 0;
145289f90fe1STrond Myklebust 		cond_resched();
145389f90fe1STrond Myklebust 		spin_lock(&xprt->queue_lock);
145489f90fe1STrond Myklebust 		xprt_unpin_rqst(next);
145589f90fe1STrond Myklebust 		if (status == 0) {
145689f90fe1STrond Myklebust 			if (!xprt_request_data_received(task) ||
145789f90fe1STrond Myklebust 			    test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
145889f90fe1STrond Myklebust 				continue;
1459c544577dSTrond Myklebust 		} else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
146089f90fe1STrond Myklebust 			task->tk_status = status;
146189f90fe1STrond Myklebust 		break;
146289f90fe1STrond Myklebust 	}
146389f90fe1STrond Myklebust 	spin_unlock(&xprt->queue_lock);
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
1466ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1467ba60eb25STrond Myklebust {
1468ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1469ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1470ba60eb25STrond Myklebust }
1471ba60eb25STrond Myklebust 
1472ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1473ba60eb25STrond Myklebust {
1474ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1475ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1476ba60eb25STrond Myklebust }
1477ba60eb25STrond Myklebust 
1478ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1479ba60eb25STrond Myklebust {
1480ba60eb25STrond Myklebust 	bool ret = false;
1481ba60eb25STrond Myklebust 
1482ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1483ba60eb25STrond Myklebust 		goto out;
1484ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1485ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1486ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1487ba60eb25STrond Myklebust 		ret = true;
1488ba60eb25STrond Myklebust 	}
1489ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1490ba60eb25STrond Myklebust out:
1491ba60eb25STrond Myklebust 	return ret;
1492ba60eb25STrond Myklebust }
1493ba60eb25STrond Myklebust 
149492ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1495d9ba131dSTrond Myklebust {
1496d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1497d9ba131dSTrond Myklebust 
1498ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1499d9ba131dSTrond Myklebust 		goto out;
1500ff699ea8SChuck Lever 	++xprt->num_reqs;
150192ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
150292ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
150392ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1504d9ba131dSTrond Myklebust 	if (req != NULL)
1505d9ba131dSTrond Myklebust 		goto out;
1506ff699ea8SChuck Lever 	--xprt->num_reqs;
1507d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1508d9ba131dSTrond Myklebust out:
1509d9ba131dSTrond Myklebust 	return req;
1510d9ba131dSTrond Myklebust }
1511d9ba131dSTrond Myklebust 
1512d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1513d9ba131dSTrond Myklebust {
1514ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1515ff699ea8SChuck Lever 		--xprt->num_reqs;
1516d9ba131dSTrond Myklebust 		kfree(req);
1517d9ba131dSTrond Myklebust 		return true;
1518d9ba131dSTrond Myklebust 	}
1519d9ba131dSTrond Myklebust 	return false;
1520d9ba131dSTrond Myklebust }
1521d9ba131dSTrond Myklebust 
1522f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
15231da177e4SLinus Torvalds {
1524d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
15251da177e4SLinus Torvalds 
1526f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
15271da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1528d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1529d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1530d9ba131dSTrond Myklebust 		goto out_init_req;
1531d9ba131dSTrond Myklebust 	}
153292ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1533d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1534d9ba131dSTrond Myklebust 		goto out_init_req;
1535d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1536d9ba131dSTrond Myklebust 	case -ENOMEM:
1537d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1538d9ba131dSTrond Myklebust 				"failed! Retrying\n");
15391afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1540d9ba131dSTrond Myklebust 		break;
1541d9ba131dSTrond Myklebust 	case -EAGAIN:
1542ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1543d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1544e9d47639SGustavo A. R. Silva 		/* fall through */
15451afeaf5cSTrond Myklebust 	default:
1546d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
15471afeaf5cSTrond Myklebust 	}
1548f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1549d9ba131dSTrond Myklebust 	return;
1550d9ba131dSTrond Myklebust out_init_req:
1551ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1552ff699ea8SChuck Lever 				     xprt->num_reqs);
155337ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
155437ac86c3SChuck Lever 
1555d9ba131dSTrond Myklebust 	task->tk_status = 0;
15561da177e4SLinus Torvalds 	task->tk_rqstp = req;
15571da177e4SLinus Torvalds }
1558f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1559f39c1bfbSTrond Myklebust 
1560a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1561ee5ebe85STrond Myklebust {
1562ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1563c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1564c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1565ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1566c25573b5STrond Myklebust 	}
1567ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1568ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1569ee5ebe85STrond Myklebust }
1570a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1571ee5ebe85STrond Myklebust 
157221de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
157321de0a95STrond Myklebust {
157421de0a95STrond Myklebust 	struct rpc_rqst *req;
157521de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
157621de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
157721de0a95STrond Myklebust 		list_del(&req->rq_list);
157821de0a95STrond Myklebust 		kfree(req);
157921de0a95STrond Myklebust 	}
158021de0a95STrond Myklebust }
158121de0a95STrond Myklebust 
1582d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1583d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1584d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1585bd1722d4SPavel Emelyanov {
1586bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
158721de0a95STrond Myklebust 	struct rpc_rqst *req;
158821de0a95STrond Myklebust 	int i;
1589bd1722d4SPavel Emelyanov 
1590bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1591bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1592bd1722d4SPavel Emelyanov 		goto out;
1593bd1722d4SPavel Emelyanov 
159421de0a95STrond Myklebust 	xprt_init(xprt, net);
159521de0a95STrond Myklebust 
159621de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
159721de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
159821de0a95STrond Myklebust 		if (!req)
15998313164cSwangweidong 			goto out_free;
160021de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
160121de0a95STrond Myklebust 	}
1602d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1603d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1604d9ba131dSTrond Myklebust 	else
160521de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1606d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1607ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1608bd1722d4SPavel Emelyanov 
1609bd1722d4SPavel Emelyanov 	return xprt;
1610bd1722d4SPavel Emelyanov 
1611bd1722d4SPavel Emelyanov out_free:
161221de0a95STrond Myklebust 	xprt_free(xprt);
1613bd1722d4SPavel Emelyanov out:
1614bd1722d4SPavel Emelyanov 	return NULL;
1615bd1722d4SPavel Emelyanov }
1616bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1617bd1722d4SPavel Emelyanov 
1618e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1619e204e621SPavel Emelyanov {
162037aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
162121de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1622fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1623e204e621SPavel Emelyanov }
1624e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1625e204e621SPavel Emelyanov 
1626902c5887STrond Myklebust static void
1627902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1628902c5887STrond Myklebust {
1629902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1630902c5887STrond Myklebust }
1631902c5887STrond Myklebust 
16329dc6edcfSTrond Myklebust static __be32
16339dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
16349dc6edcfSTrond Myklebust {
16359dc6edcfSTrond Myklebust 	__be32 xid;
16369dc6edcfSTrond Myklebust 
16379dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
16389dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
16399dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
16409dc6edcfSTrond Myklebust 	return xid;
16419dc6edcfSTrond Myklebust }
16429dc6edcfSTrond Myklebust 
16439dc6edcfSTrond Myklebust static void
16449dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
16459dc6edcfSTrond Myklebust {
16469dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
16479dc6edcfSTrond Myklebust }
16489dc6edcfSTrond Myklebust 
16499dc6edcfSTrond Myklebust static void
16509dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
16519dc6edcfSTrond Myklebust {
16529dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
16539dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
16549dc6edcfSTrond Myklebust 
16559dc6edcfSTrond Myklebust 	req->rq_task	= task;
16569dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
16579dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
16589dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1659902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
16609dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
16619dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
16629dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
16639dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
166471700bb9STrond Myklebust 	req->rq_snd_buf.bvec = NULL;
166571700bb9STrond Myklebust 	req->rq_rcv_buf.bvec = NULL;
16669dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
1667da953063STrond Myklebust 	xprt_init_majortimeo(task, req);
16689dc6edcfSTrond Myklebust 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
16699dc6edcfSTrond Myklebust 			req, ntohl(req->rq_xid));
16709dc6edcfSTrond Myklebust }
16719dc6edcfSTrond Myklebust 
16729dc6edcfSTrond Myklebust static void
16739dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
16749dc6edcfSTrond Myklebust {
16759dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
16769dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
16779dc6edcfSTrond Myklebust 		xprt_request_init(task);
16789dc6edcfSTrond Myklebust }
16799dc6edcfSTrond Myklebust 
16809903cd1cSChuck Lever /**
16819903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
16829903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
16839903cd1cSChuck Lever  *
1684ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1685ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
16869903cd1cSChuck Lever  * backlog queue.
16879903cd1cSChuck Lever  */
16889903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
16891da177e4SLinus Torvalds {
1690fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
16911da177e4SLinus Torvalds 
169243cedbf0STrond Myklebust 	task->tk_status = 0;
169343cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
169443cedbf0STrond Myklebust 		return;
169543cedbf0STrond Myklebust 
169643cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1697ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
16989dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1699ba60eb25STrond Myklebust }
1700ba60eb25STrond Myklebust 
1701ba60eb25STrond Myklebust /**
1702ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1703ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1704ba60eb25STrond Myklebust  *
1705ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1706ba60eb25STrond Myklebust  * backlog queue.
1707ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1708ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1709ba60eb25STrond Myklebust  */
1710ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1711ba60eb25STrond Myklebust {
1712fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1713ba60eb25STrond Myklebust 
1714ba60eb25STrond Myklebust 	task->tk_status = 0;
1715ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1716ba60eb25STrond Myklebust 		return;
1717ba60eb25STrond Myklebust 
1718ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
17199dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
1722edc81dcdSTrond Myklebust static void
1723edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1724edc81dcdSTrond Myklebust {
1725edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1726edc81dcdSTrond Myklebust 
1727944b0429STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1728944b0429STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1729edc81dcdSTrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1730edc81dcdSTrond Myklebust 		spin_lock(&xprt->queue_lock);
1731944b0429STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1732edc81dcdSTrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1733edc81dcdSTrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1734edc81dcdSTrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1735edc81dcdSTrond Myklebust 			spin_unlock(&xprt->queue_lock);
1736edc81dcdSTrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1737edc81dcdSTrond Myklebust 			spin_lock(&xprt->queue_lock);
1738edc81dcdSTrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1739edc81dcdSTrond Myklebust 		}
1740edc81dcdSTrond Myklebust 		spin_unlock(&xprt->queue_lock);
1741edc81dcdSTrond Myklebust 	}
1742edc81dcdSTrond Myklebust }
1743edc81dcdSTrond Myklebust 
17449903cd1cSChuck Lever /**
17459903cd1cSChuck Lever  * xprt_release - release an RPC request slot
17469903cd1cSChuck Lever  * @task: task which is finished with the slot
17479903cd1cSChuck Lever  *
17481da177e4SLinus Torvalds  */
17499903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
17501da177e4SLinus Torvalds {
175155ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
175287ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
17531da177e4SLinus Torvalds 
175487ed5003STrond Myklebust 	if (req == NULL) {
175587ed5003STrond Myklebust 		if (task->tk_client) {
1756fb43d172STrond Myklebust 			xprt = task->tk_xprt;
175787ed5003STrond Myklebust 			xprt_release_write(xprt, task);
175887ed5003STrond Myklebust 		}
17591da177e4SLinus Torvalds 		return;
176087ed5003STrond Myklebust 	}
176155ae1aabSRicardo Labiaga 
176255ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
17630a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
17640a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
17650a702195SWeston Andros Adamson 	else if (task->tk_client)
17660a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
1767edc81dcdSTrond Myklebust 	xprt_request_dequeue_all(task, req);
1768b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
176949e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1770a58dd398SChuck Lever 	if (xprt->ops->release_request)
1771a58dd398SChuck Lever 		xprt->ops->release_request(task);
17721da177e4SLinus Torvalds 	xprt->last_used = jiffies;
1773ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
1774b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
1775ee5ebe85STrond Myklebust 	if (req->rq_buffer)
17763435c74aSChuck Lever 		xprt->ops->buf_free(task);
17774a068258SChuck Lever 	xprt_inject_disconnect(xprt);
17789d96acbcSTrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
17790472e476STrond Myklebust 	xdr_free_bvec(&req->rq_snd_buf);
1780a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1781a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
17821da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1783ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1784ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
178555ae1aabSRicardo Labiaga 
178646121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1787ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1788a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1789ee5ebe85STrond Myklebust 	else
1790c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
1793902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1794902c5887STrond Myklebust void
1795902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1796902c5887STrond Myklebust {
1797902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1798902c5887STrond Myklebust 
1799902c5887STrond Myklebust 	task->tk_rqstp = req;
1800902c5887STrond Myklebust 	req->rq_task = task;
1801902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1802902c5887STrond Myklebust 	/*
1803902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1804902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1805902c5887STrond Myklebust 	 */
1806902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1807902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1808902c5887STrond Myklebust }
1809902c5887STrond Myklebust #endif
1810902c5887STrond Myklebust 
181121de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1812c2866763SChuck Lever {
181330c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1814c2866763SChuck Lever 
1815c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1816c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
181775c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1818c2866763SChuck Lever 
1819c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
182095f7691dSTrond Myklebust 	xprt->recv_queue = RB_ROOT;
1821944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
18229e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1823f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1824f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
18259e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
182680b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1827f9acac1aSRicardo Labiaga 
1828c2866763SChuck Lever 	xprt->last_used = jiffies;
1829c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1830a509050bSChuck Lever 	xprt->bind_index = 0;
1831c2866763SChuck Lever 
1832c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1833c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
183479c99152STrond Myklebust 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1835c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1836c2866763SChuck Lever 
1837c2866763SChuck Lever 	xprt_init_xid(xprt);
1838c2866763SChuck Lever 
183921de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
18408d9266ffSTrond Myklebust }
18418d9266ffSTrond Myklebust 
18428d9266ffSTrond Myklebust /**
18438d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
18448d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
18458d9266ffSTrond Myklebust  *
18468d9266ffSTrond Myklebust  */
18478d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
18488d9266ffSTrond Myklebust {
18498d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
18508d9266ffSTrond Myklebust 	struct xprt_class *t;
18518d9266ffSTrond Myklebust 
18528d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
18538d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
18548d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
18558d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
18568d9266ffSTrond Myklebust 			goto found;
18578d9266ffSTrond Myklebust 		}
18588d9266ffSTrond Myklebust 	}
18598d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
18603c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
18618d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
18628d9266ffSTrond Myklebust 
18638d9266ffSTrond Myklebust found:
18648d9266ffSTrond Myklebust 	xprt = t->setup(args);
18658d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
18668d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
18678d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
186821de0a95STrond Myklebust 		goto out;
18698d9266ffSTrond Myklebust 	}
187033d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
187133d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
187221de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
187321de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
1874502980e8SAnna Schumaker 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
187521de0a95STrond Myklebust 	else
1876ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
18774e0038b6STrond Myklebust 
18784e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
18794e0038b6STrond Myklebust 		xprt_destroy(xprt);
18804e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
18814e0038b6STrond Myklebust 	}
18824e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
18834e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
18844e0038b6STrond Myklebust 		xprt_destroy(xprt);
18854e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
18864e0038b6STrond Myklebust 	}
18874e0038b6STrond Myklebust 
18883f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1889388f0c77SJeff Layton 
1890c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1891c2866763SChuck Lever 			xprt->max_reqs);
189221de0a95STrond Myklebust out:
1893c2866763SChuck Lever 	return xprt;
1894c2866763SChuck Lever }
1895c2866763SChuck Lever 
1896528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
1897528fd354STrond Myklebust {
1898528fd354STrond Myklebust 	struct rpc_xprt *xprt =
1899528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
1900528fd354STrond Myklebust 
1901528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
1902528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1903528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1904528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1905528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1906528fd354STrond Myklebust 	kfree(xprt->servername);
1907528fd354STrond Myklebust 	/*
1908528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
1909528fd354STrond Myklebust 	 */
1910528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
1911528fd354STrond Myklebust }
1912528fd354STrond Myklebust 
19139903cd1cSChuck Lever /**
19149903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1915a8de240aSTrond Myklebust  * @xprt: transport to destroy
19169903cd1cSChuck Lever  *
19171da177e4SLinus Torvalds  */
1918a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
192179234c3dSTrond Myklebust 
1922528fd354STrond Myklebust 	/*
1923528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
1924528fd354STrond Myklebust 	 */
192579234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
192679234c3dSTrond Myklebust 
19270065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1928c8541ecdSChuck Lever 
1929c8541ecdSChuck Lever 	/*
1930528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
1931528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
1932c8541ecdSChuck Lever 	 */
1933528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1934528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
19356b6ca86bSTrond Myklebust }
19361da177e4SLinus Torvalds 
193730c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
193830c5116bSTrond Myklebust {
193930c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
194030c5116bSTrond Myklebust }
194130c5116bSTrond Myklebust 
194230c5116bSTrond Myklebust /**
194330c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
194430c5116bSTrond Myklebust  * @xprt: pointer to the transport
194530c5116bSTrond Myklebust  *
194630c5116bSTrond Myklebust  */
194730c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
194830c5116bSTrond Myklebust {
194930c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
195030c5116bSTrond Myklebust 		return xprt;
195130c5116bSTrond Myklebust 	return NULL;
195230c5116bSTrond Myklebust }
195330c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
195430c5116bSTrond Myklebust 
19556b6ca86bSTrond Myklebust /**
19566b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
19576b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
19586b6ca86bSTrond Myklebust  *
19596b6ca86bSTrond Myklebust  */
19606b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
19616b6ca86bSTrond Myklebust {
196230c5116bSTrond Myklebust 	if (xprt != NULL)
196330c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
19646b6ca86bSTrond Myklebust }
19655d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
1966