xref: /openbmc/linux/net/sunrpc/xprt.c (revision dcbbeda8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/net/sunrpc/xprt.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This is a generic RPC call interface supporting congestion avoidance,
51da177e4SLinus Torvalds  *  and asynchronous calls.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  The interface works like this:
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  -	When a process places a call, it allocates a request slot if
101da177e4SLinus Torvalds  *	one is available. Otherwise, it sleeps on the backlog queue
111da177e4SLinus Torvalds  *	(xprt_reserve).
121da177e4SLinus Torvalds  *  -	Next, the caller puts together the RPC message, stuffs it into
1355aa4f58SChuck Lever  *	the request struct, and calls xprt_transmit().
1455aa4f58SChuck Lever  *  -	xprt_transmit sends the message and installs the caller on the
1555ae1aabSRicardo Labiaga  *	transport's wait list. At the same time, if a reply is expected,
1655ae1aabSRicardo Labiaga  *	it installs a timer that is run after the packet's timeout has
1755ae1aabSRicardo Labiaga  *	expired.
181da177e4SLinus Torvalds  *  -	When a packet arrives, the data_ready handler walks the list of
1955aa4f58SChuck Lever  *	pending requests for that transport. If a matching XID is found, the
201da177e4SLinus Torvalds  *	caller is woken up, and the timer removed.
211da177e4SLinus Torvalds  *  -	When no reply arrives within the timeout interval, the timer is
221da177e4SLinus Torvalds  *	fired by the kernel and runs xprt_timer(). It either adjusts the
231da177e4SLinus Torvalds  *	timeout values (minor timeout) or wakes up the caller with a status
241da177e4SLinus Torvalds  *	of -ETIMEDOUT.
251da177e4SLinus Torvalds  *  -	When the caller receives a notification from RPC that a reply arrived,
261da177e4SLinus Torvalds  *	it should release the RPC slot, and process the reply.
271da177e4SLinus Torvalds  *	If the call timed out, it may choose to retry the operation by
281da177e4SLinus Torvalds  *	adjusting the initial timeout value, and simply calling rpc_call
291da177e4SLinus Torvalds  *	again.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Support for async RPC is done through a set of RPC-specific scheduling
321da177e4SLinus Torvalds  *  primitives that `transparently' work for processes as well as async
331da177e4SLinus Torvalds  *  tasks that rely on callbacks.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
3655aa4f58SChuck Lever  *
3755aa4f58SChuck Lever  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
40a246b010SChuck Lever #include <linux/module.h>
41a246b010SChuck Lever 
421da177e4SLinus Torvalds #include <linux/types.h>
43a246b010SChuck Lever #include <linux/interrupt.h>
441da177e4SLinus Torvalds #include <linux/workqueue.h>
45bf3fcf89SChuck Lever #include <linux/net.h>
46ff839970SChuck Lever #include <linux/ktime.h>
471da177e4SLinus Torvalds 
48a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h>
50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
51fda1bfefSTrond Myklebust #include <linux/rcupdate.h>
521da177e4SLinus Torvalds 
533705ad64SJeff Layton #include <trace/events/sunrpc.h>
543705ad64SJeff Layton 
5555ae1aabSRicardo Labiaga #include "sunrpc.h"
5655ae1aabSRicardo Labiaga 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Local variables
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
61f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
621da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Local functions
671da177e4SLinus Torvalds  */
6821de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
6937ac86c3SChuck Lever static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
701da177e4SLinus Torvalds static void	xprt_connect_status(struct rpc_task *task);
711da177e4SLinus Torvalds static int      __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
7229807318SNeil Brown static void     __xprt_put_cong(struct rpc_xprt *, struct rpc_rqst *);
734e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
741da177e4SLinus Torvalds 
755ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7681c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7781c098afS\"Talpey, Thomas\ 
7812a80469SChuck Lever /**
7981c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
8081c098afS\"Talpey, Thomas\  * @transport: transport to register
8181c098afS\"Talpey, Thomas\  *
8281c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
8381c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
8481c098afS\"Talpey, Thomas\  *
8581c098afS\"Talpey, Thomas\  * Returns:
8681c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
8781c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
8881c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
8981c098afS\"Talpey, Thomas\  */
9081c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
9181c098afS\"Talpey, Thomas\ {
9281c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
9381c098afS\"Talpey, Thomas\ 	int result;
9481c098afS\"Talpey, Thomas\ 
9581c098afS\"Talpey, Thomas\ 	result = -EEXIST;
9681c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
9781c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
9881c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
994fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
10081c098afS\"Talpey, Thomas\ 			goto out;
10181c098afS\"Talpey, Thomas\ 	}
10281c098afS\"Talpey, Thomas\ 
10381c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
10481c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
10581c098afS\"Talpey, Thomas\ 	       transport->name);
10681c098afS\"Talpey, Thomas\ 	result = 0;
10781c098afS\"Talpey, Thomas\ 
10881c098afS\"Talpey, Thomas\ out:
10981c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
11081c098afS\"Talpey, Thomas\ 	return result;
11181c098afS\"Talpey, Thomas\ }
11281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
11381c098afS\"Talpey, Thomas\ 
11481c098afS\"Talpey, Thomas\ /**
11581c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
11665b6e42cSRandy Dunlap  * @transport: transport to unregister
11781c098afS\"Talpey, Thomas\  *
11881c098afS\"Talpey, Thomas\  * Returns:
11981c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
12081c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
12181c098afS\"Talpey, Thomas\  */
12281c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
12381c098afS\"Talpey, Thomas\ {
12481c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
12581c098afS\"Talpey, Thomas\ 	int result;
12681c098afS\"Talpey, Thomas\ 
12781c098afS\"Talpey, Thomas\ 	result = 0;
12881c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
12981c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
13081c098afS\"Talpey, Thomas\ 		if (t == transport) {
13181c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
13281c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
13381c098afS\"Talpey, Thomas\ 				transport->name);
13481c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
13581c098afS\"Talpey, Thomas\ 			goto out;
13681c098afS\"Talpey, Thomas\ 		}
13781c098afS\"Talpey, Thomas\ 	}
13881c098afS\"Talpey, Thomas\ 	result = -ENOENT;
13981c098afS\"Talpey, Thomas\ 
14081c098afS\"Talpey, Thomas\ out:
14181c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
14281c098afS\"Talpey, Thomas\ 	return result;
14381c098afS\"Talpey, Thomas\ }
14481c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
14581c098afS\"Talpey, Thomas\ 
14681c098afS\"Talpey, Thomas\ /**
147441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
148441e3e24STom Talpey  * @transport_name: transport to load
149441e3e24STom Talpey  *
150441e3e24STom Talpey  * Returns:
151441e3e24STom Talpey  * 0:		transport successfully loaded
152441e3e24STom Talpey  * -ENOENT:	transport module not available
153441e3e24STom Talpey  */
154441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
155441e3e24STom Talpey {
156441e3e24STom Talpey 	struct xprt_class *t;
157441e3e24STom Talpey 	int result;
158441e3e24STom Talpey 
159441e3e24STom Talpey 	result = 0;
160441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
161441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
162441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
163441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
164441e3e24STom Talpey 			goto out;
165441e3e24STom Talpey 		}
166441e3e24STom Talpey 	}
167441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
168ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
169441e3e24STom Talpey out:
170441e3e24STom Talpey 	return result;
171441e3e24STom Talpey }
172441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
173441e3e24STom Talpey 
174441e3e24STom Talpey /**
17512a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
17612a80469SChuck Lever  * @task: task that is requesting access to the transport
177177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
17812a80469SChuck Lever  *
17912a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
18012a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
18112a80469SChuck Lever  * is provided.
1821da177e4SLinus Torvalds  */
18343cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1841da177e4SLinus Torvalds {
18512a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
18634006ceeSTrond Myklebust 	int priority;
18712a80469SChuck Lever 
18812a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
18912a80469SChuck Lever 		if (task == xprt->snd_task)
19012a80469SChuck Lever 			return 1;
19112a80469SChuck Lever 		goto out_sleep;
19212a80469SChuck Lever 	}
19312a80469SChuck Lever 	xprt->snd_task = task;
1944d4a76f3Sj223yang@asset.uwaterloo.ca 
19512a80469SChuck Lever 	return 1;
19612a80469SChuck Lever 
19712a80469SChuck Lever out_sleep:
19846121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
19912a80469SChuck Lever 			task->tk_pid, xprt);
20012a80469SChuck Lever 	task->tk_timeout = 0;
20112a80469SChuck Lever 	task->tk_status = -EAGAIN;
20234006ceeSTrond Myklebust 	if (req == NULL)
20334006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
20434006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
20534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
20612a80469SChuck Lever 	else
20734006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
20834006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
20912a80469SChuck Lever 	return 0;
21012a80469SChuck Lever }
21112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
21212a80469SChuck Lever 
213632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
214632e3bdcSTrond Myklebust {
215632e3bdcSTrond Myklebust 	xprt->snd_task = NULL;
216d19751e7STrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
2174e857c58SPeter Zijlstra 		smp_mb__before_atomic();
218632e3bdcSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
2194e857c58SPeter Zijlstra 		smp_mb__after_atomic();
220632e3bdcSTrond Myklebust 	} else
22140a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
222632e3bdcSTrond Myklebust }
223632e3bdcSTrond Myklebust 
22412a80469SChuck Lever /*
22512a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
22612a80469SChuck Lever  * @task: task that is requesting access to the transport
22712a80469SChuck Lever  *
22812a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
22912a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
23012a80469SChuck Lever  * woken up and given access to the transport.
23112a80469SChuck Lever  */
23243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
23312a80469SChuck Lever {
2341da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
23534006ceeSTrond Myklebust 	int priority;
2361da177e4SLinus Torvalds 
2372226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2381da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2391da177e4SLinus Torvalds 			return 1;
2401da177e4SLinus Torvalds 		goto out_sleep;
2411da177e4SLinus Torvalds 	}
24243cedbf0STrond Myklebust 	if (req == NULL) {
24343cedbf0STrond Myklebust 		xprt->snd_task = task;
24443cedbf0STrond Myklebust 		return 1;
24543cedbf0STrond Myklebust 	}
24612a80469SChuck Lever 	if (__xprt_get_cong(xprt, task)) {
2471da177e4SLinus Torvalds 		xprt->snd_task = task;
2481da177e4SLinus Torvalds 		return 1;
2491da177e4SLinus Torvalds 	}
250632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2511da177e4SLinus Torvalds out_sleep:
25229807318SNeil Brown 	if (req)
25329807318SNeil Brown 		__xprt_put_cong(xprt, req);
25446121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2551da177e4SLinus Torvalds 	task->tk_timeout = 0;
2561da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
25734006ceeSTrond Myklebust 	if (req == NULL)
25834006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
25934006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
26034006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
2611da177e4SLinus Torvalds 	else
26234006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
26334006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
2641da177e4SLinus Torvalds 	return 0;
2651da177e4SLinus Torvalds }
26612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2671da177e4SLinus Torvalds 
26812a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	int retval;
2711da177e4SLinus Torvalds 
2724a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
27343cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2744a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2751da177e4SLinus Torvalds 	return retval;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
278961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2791da177e4SLinus Torvalds {
280961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
28149e9a890SChuck Lever 
28249e9a890SChuck Lever 	xprt->snd_task = task;
283961a828dSTrond Myklebust 	return true;
284961a828dSTrond Myklebust }
285961a828dSTrond Myklebust 
286961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
287961a828dSTrond Myklebust {
288961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
28949e9a890SChuck Lever 		return;
29049e9a890SChuck Lever 
291f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
292f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
293961a828dSTrond Myklebust 		return;
294632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
29549e9a890SChuck Lever }
29649e9a890SChuck Lever 
297961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
29849e9a890SChuck Lever {
299961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
30043cedbf0STrond Myklebust 	struct rpc_rqst *req;
3011da177e4SLinus Torvalds 
30243cedbf0STrond Myklebust 	req = task->tk_rqstp;
30343cedbf0STrond Myklebust 	if (req == NULL) {
3041da177e4SLinus Torvalds 		xprt->snd_task = task;
305961a828dSTrond Myklebust 		return true;
30643cedbf0STrond Myklebust 	}
30743cedbf0STrond Myklebust 	if (__xprt_get_cong(xprt, task)) {
30843cedbf0STrond Myklebust 		xprt->snd_task = task;
3091da177e4SLinus Torvalds 		req->rq_ntrans++;
310961a828dSTrond Myklebust 		return true;
3111da177e4SLinus Torvalds 	}
312961a828dSTrond Myklebust 	return false;
313961a828dSTrond Myklebust }
314961a828dSTrond Myklebust 
315961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
316961a828dSTrond Myklebust {
317961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
318961a828dSTrond Myklebust 		return;
319961a828dSTrond Myklebust 	if (RPCXPRT_CONGESTED(xprt))
320961a828dSTrond Myklebust 		goto out_unlock;
321f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
322f1dc237cSTrond Myklebust 				__xprt_lock_write_cong_func, xprt))
323961a828dSTrond Myklebust 		return;
3241da177e4SLinus Torvalds out_unlock:
325632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
32849e9a890SChuck Lever /**
32949e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
33049e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
33149e9a890SChuck Lever  * @task: task that is releasing access to the transport
33249e9a890SChuck Lever  *
33349e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3341da177e4SLinus Torvalds  */
33549e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
338632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3391da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds }
34212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3431da177e4SLinus Torvalds 
34449e9a890SChuck Lever /**
34549e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
34649e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
34749e9a890SChuck Lever  * @task: task that is releasing access to the transport
34849e9a890SChuck Lever  *
34949e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
35049e9a890SChuck Lever  * transport if the transport's congestion window allows it.
35149e9a890SChuck Lever  */
35249e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
35349e9a890SChuck Lever {
35449e9a890SChuck Lever 	if (xprt->snd_task == task) {
355632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
35649e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
35749e9a890SChuck Lever 	}
35849e9a890SChuck Lever }
35912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
36049e9a890SChuck Lever 
36149e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3621da177e4SLinus Torvalds {
3634a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
36449e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3654a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /*
3691da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3701da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3711da177e4SLinus Torvalds  */
3721da177e4SLinus Torvalds static int
3731da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	if (req->rq_cong)
3781da177e4SLinus Torvalds 		return 1;
37946121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
3801da177e4SLinus Torvalds 			task->tk_pid, xprt->cong, xprt->cwnd);
3811da177e4SLinus Torvalds 	if (RPCXPRT_CONGESTED(xprt))
3821da177e4SLinus Torvalds 		return 0;
3831da177e4SLinus Torvalds 	req->rq_cong = 1;
3841da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
3851da177e4SLinus Torvalds 	return 1;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /*
3891da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
3901da177e4SLinus Torvalds  * that has been sleeping due to congestion
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds static void
3931da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	if (!req->rq_cong)
3961da177e4SLinus Torvalds 		return;
3971da177e4SLinus Torvalds 	req->rq_cong = 0;
3981da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
39949e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
40246c0ee8bSChuck Lever /**
403a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
404a58dd398SChuck Lever  * @task: RPC request that recently completed
405a58dd398SChuck Lever  *
406a58dd398SChuck Lever  * Useful for transports that require congestion control.
407a58dd398SChuck Lever  */
408a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
409a58dd398SChuck Lever {
410a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
411a4f0835cSTrond Myklebust 
412a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
413a58dd398SChuck Lever }
41412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
415a58dd398SChuck Lever 
416a58dd398SChuck Lever /**
41746c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4186a24dfb6STrond Myklebust  * @xprt: pointer to xprt
41946c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
42046c0ee8bSChuck Lever  * @result: result code of completed RPC request
42146c0ee8bSChuck Lever  *
4224f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4234f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4244f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4254f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4264f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4274f4cf5adSChuck Lever  *
4284f4cf5adSChuck Lever  *	-	a reply is received and
4294f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4304f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4311da177e4SLinus Torvalds  */
4326a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4331da177e4SLinus Torvalds {
43446c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
43546c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4381da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4391da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4401da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4411da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4421da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
44349e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
4441da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
4451da177e4SLinus Torvalds 		cwnd >>= 1;
4461da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
4471da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
4501da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
4511da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
45246c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
4531da177e4SLinus Torvalds }
45412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
4551da177e4SLinus Torvalds 
45644fbac22SChuck Lever /**
45744fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
45844fbac22SChuck Lever  * @xprt: transport with waiting tasks
45944fbac22SChuck Lever  * @status: result code to plant in each task before waking it
46044fbac22SChuck Lever  *
46144fbac22SChuck Lever  */
46244fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
46344fbac22SChuck Lever {
46444fbac22SChuck Lever 	if (status < 0)
46544fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
46644fbac22SChuck Lever 	else
46744fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
46844fbac22SChuck Lever }
46912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
47044fbac22SChuck Lever 
471c7b2cae8SChuck Lever /**
472c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
473c7b2cae8SChuck Lever  * @task: task to be put to sleep
4740b80ae42SRandy Dunlap  * @action: function pointer to be executed after wait
475a9a6b52eSTrond Myklebust  *
476a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
477a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
478a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
479c7b2cae8SChuck Lever  */
480b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
481c7b2cae8SChuck Lever {
482c7b2cae8SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
483c7b2cae8SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
484c7b2cae8SChuck Lever 
485a9a6b52eSTrond Myklebust 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
486b6ddf64fSTrond Myklebust 	rpc_sleep_on(&xprt->pending, task, action);
487c7b2cae8SChuck Lever }
48812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
489c7b2cae8SChuck Lever 
490c7b2cae8SChuck Lever /**
491c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
492c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
493c7b2cae8SChuck Lever  *
494c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
495c7b2cae8SChuck Lever  */
496c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt)
497c7b2cae8SChuck Lever {
498c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
499c7b2cae8SChuck Lever 	if (xprt->snd_task) {
50046121cf7SChuck Lever 		dprintk("RPC:       write space: waking waiting task on "
50146121cf7SChuck Lever 				"xprt %p\n", xprt);
5022275cde4STrond Myklebust 		rpc_wake_up_queued_task_on_wq(xprtiod_workqueue,
5032275cde4STrond Myklebust 				&xprt->pending, xprt->snd_task);
504c7b2cae8SChuck Lever 	}
505c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
506c7b2cae8SChuck Lever }
50712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
508c7b2cae8SChuck Lever 
509fe3aca29SChuck Lever /**
510fe3aca29SChuck Lever  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
511fe3aca29SChuck Lever  * @task: task whose timeout is to be set
512fe3aca29SChuck Lever  *
513fe3aca29SChuck Lever  * Set a request's retransmit timeout based on the transport's
514fe3aca29SChuck Lever  * default timeout parameters.  Used by transports that don't adjust
515fe3aca29SChuck Lever  * the retransmit timeout based on round-trip time estimation.
516fe3aca29SChuck Lever  */
517fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task)
518fe3aca29SChuck Lever {
519fe3aca29SChuck Lever 	task->tk_timeout = task->tk_rqstp->rq_timeout;
520fe3aca29SChuck Lever }
52112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
522fe3aca29SChuck Lever 
5232c53040fSBen Hutchings /**
524fe3aca29SChuck Lever  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
525fe3aca29SChuck Lever  * @task: task whose timeout is to be set
526fe3aca29SChuck Lever  *
527fe3aca29SChuck Lever  * Set a request's retransmit timeout using the RTT estimator.
528fe3aca29SChuck Lever  */
529fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
530fe3aca29SChuck Lever {
531fe3aca29SChuck Lever 	int timer = task->tk_msg.rpc_proc->p_timer;
532ba7392bbSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
533ba7392bbSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
534fe3aca29SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
535ba7392bbSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
536fe3aca29SChuck Lever 
537fe3aca29SChuck Lever 	task->tk_timeout = rpc_calc_rto(rtt, timer);
538fe3aca29SChuck Lever 	task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
539fe3aca29SChuck Lever 	if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
540fe3aca29SChuck Lever 		task->tk_timeout = max_timeout;
541fe3aca29SChuck Lever }
54212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
543fe3aca29SChuck Lever 
5441da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5451da177e4SLinus Torvalds {
546ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5491da177e4SLinus Torvalds 	if (to->to_exponential)
5501da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5511da177e4SLinus Torvalds 	else
5521da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
5531da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
5541da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
5551da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5589903cd1cSChuck Lever /**
5599903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
5609903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
5619903cd1cSChuck Lever  *
5621da177e4SLinus Torvalds  */
5631da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
566ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5671da177e4SLinus Torvalds 	int status = 0;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
5701da177e4SLinus Torvalds 		if (to->to_exponential)
5711da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
5721da177e4SLinus Torvalds 		else
5731da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
5741da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
5751da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
5761da177e4SLinus Torvalds 		req->rq_retries++;
5771da177e4SLinus Torvalds 	} else {
5781da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
5791da177e4SLinus Torvalds 		req->rq_retries = 0;
5801da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
5811da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
5824a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
5831da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
5844a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
5851da177e4SLinus Torvalds 		status = -ETIMEDOUT;
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
5891da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
5901da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	return status;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
59565f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
5961da177e4SLinus Torvalds {
59765f27f38SDavid Howells 	struct rpc_xprt *xprt =
59865f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
5991da177e4SLinus Torvalds 
60066af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6014876cc77STrond Myklebust 	xprt->ops->close(xprt);
6021da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
60379234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6069903cd1cSChuck Lever /**
60762da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6089903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6099903cd1cSChuck Lever  *
6101da177e4SLinus Torvalds  */
61162da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6144a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6151da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
6162a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6174a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6181da177e4SLinus Torvalds }
61962da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6201da177e4SLinus Torvalds 
62166af1e55STrond Myklebust /**
62266af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
62366af1e55STrond Myklebust  * @xprt: transport to disconnect
62466af1e55STrond Myklebust  *
62566af1e55STrond Myklebust  */
62666af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
62766af1e55STrond Myklebust {
62866af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
62966af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
63066af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
63166af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
63266af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
63340a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6342a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
63566af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
63666af1e55STrond Myklebust }
637e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
63866af1e55STrond Myklebust 
6397f3a1d1eSTrond Myklebust static unsigned int
6407f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
6417f3a1d1eSTrond Myklebust {
6427f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
6437f3a1d1eSTrond Myklebust }
6447f3a1d1eSTrond Myklebust 
6457f3a1d1eSTrond Myklebust static bool
6467f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
6477f3a1d1eSTrond Myklebust {
6487f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6497f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
6507f3a1d1eSTrond Myklebust 
6517f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
6527f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
6537f3a1d1eSTrond Myklebust }
6547f3a1d1eSTrond Myklebust 
6557c1d71cfSTrond Myklebust /**
6567c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
6577c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
6587c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
6597c1d71cfSTrond Myklebust  *
6607c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
6617c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
6627c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
6637c1d71cfSTrond Myklebust  * a batch of RPC requests.
6647c1d71cfSTrond Myklebust  *
6657c1d71cfSTrond Myklebust  */
6667c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
6677c1d71cfSTrond Myklebust {
6687c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
6697c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
6707c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
6717c1d71cfSTrond Myklebust 		goto out;
6722c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
6737c1d71cfSTrond Myklebust 		goto out;
6747c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
6757c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
6767c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
67740a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6782a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6797c1d71cfSTrond Myklebust out:
6807c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
6817c1d71cfSTrond Myklebust }
6827c1d71cfSTrond Myklebust 
683ad3331acSTrond Myklebust static bool
684ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
685ad3331acSTrond Myklebust {
686ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
687ad3331acSTrond Myklebust }
688ad3331acSTrond Myklebust 
689ad3331acSTrond Myklebust static void
690ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
691ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
692ad3331acSTrond Myklebust {
693ef3f5434STrond Myklebust 	if (list_empty(&xprt->recv_queue) && xprt_has_timer(xprt))
694ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
695ad3331acSTrond Myklebust }
696ad3331acSTrond Myklebust 
6971da177e4SLinus Torvalds static void
698ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
6991da177e4SLinus Torvalds {
700ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
7011da177e4SLinus Torvalds 
7024a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
703ef3f5434STrond Myklebust 	if (!list_empty(&xprt->recv_queue))
7041da177e4SLinus Torvalds 		goto out_abort;
705ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
706ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
7072226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
7081da177e4SLinus Torvalds 		goto out_abort;
7094a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
71040a5f1b1STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7111da177e4SLinus Torvalds 	return;
7121da177e4SLinus Torvalds out_abort:
7134a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
716718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
717718ba5b8STrond Myklebust 		struct rpc_task *task,
718718ba5b8STrond Myklebust 		void *cookie)
719718ba5b8STrond Myklebust {
720718ba5b8STrond Myklebust 	bool ret = false;
721718ba5b8STrond Myklebust 
722718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
723718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
724718ba5b8STrond Myklebust 		goto out;
725718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
726718ba5b8STrond Myklebust 		goto out;
727718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
728718ba5b8STrond Myklebust 	ret = true;
729718ba5b8STrond Myklebust out:
730718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
731718ba5b8STrond Myklebust 	return ret;
732718ba5b8STrond Myklebust }
733718ba5b8STrond Myklebust 
734718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
735718ba5b8STrond Myklebust {
736718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
737718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
738718ba5b8STrond Myklebust 		goto out;
739718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
740718ba5b8STrond Myklebust 		goto out;
741718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
742718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
743ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
744718ba5b8STrond Myklebust out:
745718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
74679234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
747718ba5b8STrond Myklebust }
748718ba5b8STrond Myklebust 
7499903cd1cSChuck Lever /**
7509903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
7519903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
7521da177e4SLinus Torvalds  *
7531da177e4SLinus Torvalds  */
7541da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
7551da177e4SLinus Torvalds {
756ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7571da177e4SLinus Torvalds 
75846121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7591da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7601da177e4SLinus Torvalds 
761ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
76201d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7631da177e4SLinus Torvalds 		return;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7661da177e4SLinus Torvalds 		return;
767feb8ca37STrond Myklebust 
768feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
769feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
770feb8ca37STrond Myklebust 
771718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
772a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
7732c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
7745d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
7750b9e7943STrond Myklebust 
7760b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
7770b9e7943STrond Myklebust 			return;
7780b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
7790b9e7943STrond Myklebust 			return;
780262ca07dSChuck Lever 		xprt->stat.connect_start = jiffies;
7811b092092STrond Myklebust 		xprt->ops->connect(xprt, task);
7821da177e4SLinus Torvalds 	}
783718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7869903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task)
7871da177e4SLinus Torvalds {
788ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7891da177e4SLinus Torvalds 
790cd983ef8SChuck Lever 	if (task->tk_status == 0) {
791262ca07dSChuck Lever 		xprt->stat.connect_count++;
792262ca07dSChuck Lever 		xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
79346121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connection established\n",
7941da177e4SLinus Torvalds 				task->tk_pid);
7951da177e4SLinus Torvalds 		return;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	switch (task->tk_status) {
7990fe8d04eSTrond Myklebust 	case -ECONNREFUSED:
8000fe8d04eSTrond Myklebust 	case -ECONNRESET:
8010fe8d04eSTrond Myklebust 	case -ECONNABORTED:
8020fe8d04eSTrond Myklebust 	case -ENETUNREACH:
8030fe8d04eSTrond Myklebust 	case -EHOSTUNREACH:
8042fc193cfSTrond Myklebust 	case -EPIPE:
8052a491991STrond Myklebust 	case -EAGAIN:
8062a491991STrond Myklebust 		dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
80723475d66SChuck Lever 		break;
8081da177e4SLinus Torvalds 	case -ETIMEDOUT:
80946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
81046121cf7SChuck Lever 				"out\n", task->tk_pid);
8111da177e4SLinus Torvalds 		break;
8121da177e4SLinus Torvalds 	default:
81346121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
81446121cf7SChuck Lever 				"server %s\n", task->tk_pid, -task->tk_status,
8154e0038b6STrond Myklebust 				xprt->servername);
81623475d66SChuck Lever 		task->tk_status = -EIO;
81723475d66SChuck Lever 	}
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8209903cd1cSChuck Lever /**
8219903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
8229903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
8239903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
8249903cd1cSChuck Lever  *
82575c84151STrond Myklebust  * Caller holds xprt->queue_lock.
8261da177e4SLinus Torvalds  */
827d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
8281da177e4SLinus Torvalds {
8298f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
8301da177e4SLinus Torvalds 
831ef3f5434STrond Myklebust 	list_for_each_entry(entry, &xprt->recv_queue, rq_recv)
8323705ad64SJeff Layton 		if (entry->rq_xid == xid) {
8333705ad64SJeff Layton 			trace_xprt_lookup_rqst(xprt, xid, 0);
8340b87a46bSChuck Lever 			entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
835262ca07dSChuck Lever 			return entry;
8363705ad64SJeff Layton 		}
83746121cf7SChuck Lever 
83846121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
83946121cf7SChuck Lever 			ntohl(xid));
8403705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
841262ca07dSChuck Lever 	xprt->stat.bad_xids++;
842262ca07dSChuck Lever 	return NULL;
8431da177e4SLinus Torvalds }
84412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
8451da177e4SLinus Torvalds 
846cf9946cdSTrond Myklebust static bool
847cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
848cf9946cdSTrond Myklebust {
849cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
850cf9946cdSTrond Myklebust }
851cf9946cdSTrond Myklebust 
852729749bbSTrond Myklebust /**
853729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
854729749bbSTrond Myklebust  * @req: Request to pin
855729749bbSTrond Myklebust  *
856729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
857cf9946cdSTrond Myklebust  * so should be holding the xprt receive lock.
858729749bbSTrond Myklebust  */
859729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
860729749bbSTrond Myklebust {
861cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
862729749bbSTrond Myklebust }
8639590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
864729749bbSTrond Myklebust 
865729749bbSTrond Myklebust /**
866729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
867729749bbSTrond Myklebust  * @req: Request to pin
868729749bbSTrond Myklebust  *
869cf9946cdSTrond Myklebust  * Caller should be holding the xprt receive lock.
870729749bbSTrond Myklebust  */
871729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
872729749bbSTrond Myklebust {
873cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
874cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
875cf9946cdSTrond Myklebust 		return;
876cf9946cdSTrond Myklebust 	}
877cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
878cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
879729749bbSTrond Myklebust }
8809590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
881729749bbSTrond Myklebust 
882729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
883729749bbSTrond Myklebust {
884cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
885729749bbSTrond Myklebust }
886729749bbSTrond Myklebust 
887edc81dcdSTrond Myklebust static bool
888edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
889edc81dcdSTrond Myklebust {
890edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
891edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
892edc81dcdSTrond Myklebust }
893edc81dcdSTrond Myklebust 
894edc81dcdSTrond Myklebust static bool
895edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
896edc81dcdSTrond Myklebust {
897edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
898edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
899edc81dcdSTrond Myklebust }
900edc81dcdSTrond Myklebust 
901edc81dcdSTrond Myklebust /**
902edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
903edc81dcdSTrond Myklebust  * @task: RPC task
904edc81dcdSTrond Myklebust  *
905edc81dcdSTrond Myklebust  */
906edc81dcdSTrond Myklebust void
907edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
908edc81dcdSTrond Myklebust {
909edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
910edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
911edc81dcdSTrond Myklebust 
912edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
913edc81dcdSTrond Myklebust 		return;
914edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
915edc81dcdSTrond Myklebust 
916edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
917edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
918edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
919edc81dcdSTrond Myklebust 
920edc81dcdSTrond Myklebust 	/* Add request to the receive list */
921ef3f5434STrond Myklebust 	list_add_tail(&req->rq_recv, &xprt->recv_queue);
922edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
923edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
924edc81dcdSTrond Myklebust 
925edc81dcdSTrond Myklebust 	xprt_reset_majortimeo(req);
926edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
927edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
928edc81dcdSTrond Myklebust }
929edc81dcdSTrond Myklebust 
930edc81dcdSTrond Myklebust /**
931edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
932edc81dcdSTrond Myklebust  * @task: RPC task
933edc81dcdSTrond Myklebust  *
934edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
935edc81dcdSTrond Myklebust  */
936edc81dcdSTrond Myklebust static void
937edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
938edc81dcdSTrond Myklebust {
939edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
940ef3f5434STrond Myklebust 		list_del(&task->tk_rqstp->rq_recv);
941edc81dcdSTrond Myklebust }
942edc81dcdSTrond Myklebust 
943ecd465eeSChuck Lever /**
944ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
945ecd465eeSChuck Lever  * @task: RPC request that recently completed
946ecd465eeSChuck Lever  *
94775c84151STrond Myklebust  * Caller holds xprt->queue_lock.
948ecd465eeSChuck Lever  */
949ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
9501da177e4SLinus Torvalds {
9511570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
9521570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
95395c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
954d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
9551570c1e4SChuck Lever 
9561da177e4SLinus Torvalds 	if (timer) {
9571da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
958ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
9591570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds }
962ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
9631da177e4SLinus Torvalds 
9641570c1e4SChuck Lever /**
9651570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
9661570c1e4SChuck Lever  * @task: RPC request that recently completed
9671570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
9681570c1e4SChuck Lever  *
96975c84151STrond Myklebust  * Caller holds xprt->queue_lock.
9701570c1e4SChuck Lever  */
9711570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
9721570c1e4SChuck Lever {
9731570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
974fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
9751da177e4SLinus Torvalds 
9761570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
9771570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
9783705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
9791da177e4SLinus Torvalds 
980fda13939STrond Myklebust 	xprt->stat.recvs++;
981ef759a2eSChuck Lever 
9821e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
983dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
984dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
98543ac3f29STrond Myklebust 	smp_wmb();
986dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
987edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
988fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
9891da177e4SLinus Torvalds }
99012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
9911da177e4SLinus Torvalds 
99246c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
9951da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
9961da177e4SLinus Torvalds 
9975d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
9985d00837bSTrond Myklebust 		return;
99946c0ee8bSChuck Lever 
100082476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1001dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
100246c0ee8bSChuck Lever 		if (xprt->ops->timer)
10036a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
10045d00837bSTrond Myklebust 	} else
10055d00837bSTrond Myklebust 		task->tk_status = 0;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10089903cd1cSChuck Lever /**
10097f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
10107f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
10117f3a1d1eSTrond Myklebust  *
10127f3a1d1eSTrond Myklebust  */
10137f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
10147f3a1d1eSTrond Myklebust {
10157f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
10167f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
10177f3a1d1eSTrond Myklebust 
10187f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
10197f3a1d1eSTrond Myklebust 		return;
10207f3a1d1eSTrond Myklebust 	/*
10217f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
10227f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
10237f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
10247f3a1d1eSTrond Myklebust 	 */
10257f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
10267f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
10277f3a1d1eSTrond Myklebust 		xprt->ops->set_retrans_timeout(task);
10287f3a1d1eSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_timer);
10297f3a1d1eSTrond Myklebust 		/*
10307f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
10317f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
10327f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
10337f3a1d1eSTrond Myklebust 		 */
10347f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
10357f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
10367f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
10377f3a1d1eSTrond Myklebust 	}
10387f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
10397f3a1d1eSTrond Myklebust }
10407f3a1d1eSTrond Myklebust 
1041944b0429STrond Myklebust static bool
1042944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1043944b0429STrond Myklebust {
1044762e4e67STrond Myklebust 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1045944b0429STrond Myklebust }
1046944b0429STrond Myklebust 
1047944b0429STrond Myklebust /**
1048944b0429STrond Myklebust  * xprt_request_enqueue_transmit - queue a task for transmission
1049944b0429STrond Myklebust  * @task: pointer to rpc_task
1050944b0429STrond Myklebust  *
1051944b0429STrond Myklebust  * Add a task to the transmission queue.
1052944b0429STrond Myklebust  */
1053944b0429STrond Myklebust void
1054944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task)
1055944b0429STrond Myklebust {
1056944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1057944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1058944b0429STrond Myklebust 
1059944b0429STrond Myklebust 	if (xprt_request_need_enqueue_transmit(task, req)) {
1060944b0429STrond Myklebust 		spin_lock(&xprt->queue_lock);
1061944b0429STrond Myklebust 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1062944b0429STrond Myklebust 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1063944b0429STrond Myklebust 		spin_unlock(&xprt->queue_lock);
1064944b0429STrond Myklebust 	}
1065944b0429STrond Myklebust }
1066944b0429STrond Myklebust 
1067944b0429STrond Myklebust /**
1068944b0429STrond Myklebust  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1069944b0429STrond Myklebust  * @task: pointer to rpc_task
1070944b0429STrond Myklebust  *
1071944b0429STrond Myklebust  * Remove a task from the transmission queue
1072944b0429STrond Myklebust  * Caller must hold xprt->queue_lock
1073944b0429STrond Myklebust  */
1074944b0429STrond Myklebust static void
1075944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1076944b0429STrond Myklebust {
1077944b0429STrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1078944b0429STrond Myklebust 		list_del(&task->tk_rqstp->rq_xmit);
1079944b0429STrond Myklebust }
1080944b0429STrond Myklebust 
1081944b0429STrond Myklebust /**
1082944b0429STrond Myklebust  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1083944b0429STrond Myklebust  * @task: pointer to rpc_task
1084944b0429STrond Myklebust  *
1085944b0429STrond Myklebust  * Remove a task from the transmission queue
1086944b0429STrond Myklebust  */
1087944b0429STrond Myklebust static void
1088944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task)
1089944b0429STrond Myklebust {
1090944b0429STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1091944b0429STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1092944b0429STrond Myklebust 
1093944b0429STrond Myklebust 	spin_lock(&xprt->queue_lock);
1094944b0429STrond Myklebust 	xprt_request_dequeue_transmit_locked(task);
1095944b0429STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1096944b0429STrond Myklebust }
1097944b0429STrond Myklebust 
10987f3a1d1eSTrond Myklebust /**
1099762e4e67STrond Myklebust  * xprt_request_need_retransmit - Test if a task needs retransmission
1100762e4e67STrond Myklebust  * @task: pointer to rpc_task
1101762e4e67STrond Myklebust  *
1102762e4e67STrond Myklebust  * Test for whether a connection breakage requires the task to retransmit
1103762e4e67STrond Myklebust  */
1104762e4e67STrond Myklebust bool
1105762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task)
1106762e4e67STrond Myklebust {
1107762e4e67STrond Myklebust 	return xprt_request_retransmit_after_disconnect(task);
1108762e4e67STrond Myklebust }
1109762e4e67STrond Myklebust 
1110762e4e67STrond Myklebust /**
11119903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
11129903cd1cSChuck Lever  * @task: RPC task about to send a request
11139903cd1cSChuck Lever  *
11141da177e4SLinus Torvalds  */
111590051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
11181da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
11191da177e4SLinus Torvalds 
112046121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
11211da177e4SLinus Torvalds 
11225f2f6bd9STrond Myklebust 	if (!xprt_lock_write(xprt, task)) {
11235f2f6bd9STrond Myklebust 		/* Race breaker: someone may have transmitted us */
1124944b0429STrond Myklebust 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
11255f2f6bd9STrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->sending,
11265f2f6bd9STrond Myklebust 					task, 0);
11275f2f6bd9STrond Myklebust 		return false;
11285f2f6bd9STrond Myklebust 
11298a19a0b6STrond Myklebust 	}
11305f2f6bd9STrond Myklebust 	return true;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
1133e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
11345e5ce5beSTrond Myklebust {
1135343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
11365e5ce5beSTrond Myklebust }
11375e5ce5beSTrond Myklebust 
11389903cd1cSChuck Lever /**
11399903cd1cSChuck Lever  * xprt_transmit - send an RPC request on a transport
11409903cd1cSChuck Lever  * @task: controlling RPC task
11419903cd1cSChuck Lever  *
11429903cd1cSChuck Lever  * We have to copy the iovec because sendmsg fiddles with its contents.
11439903cd1cSChuck Lever  */
11449903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
11471da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
114890d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1149dcbbeda8STrond Myklebust 	int is_retrans = RPC_WAS_SENT(task);
1150ff699ea8SChuck Lever 	int status;
11511da177e4SLinus Torvalds 
115246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
11531da177e4SLinus Torvalds 
1154edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
1155edc81dcdSTrond Myklebust 		if (xprt_request_data_received(task))
1156944b0429STrond Myklebust 			goto out_dequeue;
11573021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1158edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
11593021a5bbSTrond Myklebust 			task->tk_status = -EBADMSG;
1160944b0429STrond Myklebust 			goto out_dequeue;
11613021a5bbSTrond Myklebust 		}
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
1164dcbbeda8STrond Myklebust 	/*
1165dcbbeda8STrond Myklebust 	 * Update req->rq_ntrans before transmitting to avoid races with
1166dcbbeda8STrond Myklebust 	 * xprt_update_rtt(), which needs to know that it is recording a
1167dcbbeda8STrond Myklebust 	 * reply to the first transmission.
1168dcbbeda8STrond Myklebust 	 */
1169dcbbeda8STrond Myklebust 	req->rq_ntrans++;
1170dcbbeda8STrond Myklebust 
117190d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
117250f484e2STrond Myklebust 	status = xprt->ops->send_request(req, task);
11733705ad64SJeff Layton 	trace_xprt_transmit(xprt, req->rq_xid, status);
1174c8485e4dSTrond Myklebust 	if (status != 0) {
1175dcbbeda8STrond Myklebust 		req->rq_ntrans--;
1176c8485e4dSTrond Myklebust 		task->tk_status = status;
1177c8485e4dSTrond Myklebust 		return;
1178c8485e4dSTrond Myklebust 	}
11797ebbbc6eSTrond Myklebust 
1180dcbbeda8STrond Myklebust 	if (is_retrans)
1181dcbbeda8STrond Myklebust 		task->tk_client->cl_stats->rpcretrans++;
1182dcbbeda8STrond Myklebust 
11834a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1184c8485e4dSTrond Myklebust 
118546121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
1186468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1187fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
1188262ca07dSChuck Lever 
1189262ca07dSChuck Lever 	xprt->stat.sends++;
1190262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1191262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
119215a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
119315a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1194fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
119590d91b0cSTrond Myklebust 
119690d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
1197944b0429STrond Myklebust out_dequeue:
1198944b0429STrond Myklebust 	xprt_request_dequeue_transmit(task);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
1201ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1202ba60eb25STrond Myklebust {
1203ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1204ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1205ba60eb25STrond Myklebust }
1206ba60eb25STrond Myklebust 
1207ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1208ba60eb25STrond Myklebust {
1209ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1210ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1211ba60eb25STrond Myklebust }
1212ba60eb25STrond Myklebust 
1213ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1214ba60eb25STrond Myklebust {
1215ba60eb25STrond Myklebust 	bool ret = false;
1216ba60eb25STrond Myklebust 
1217ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1218ba60eb25STrond Myklebust 		goto out;
1219ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1220ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1221ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1222ba60eb25STrond Myklebust 		ret = true;
1223ba60eb25STrond Myklebust 	}
1224ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1225ba60eb25STrond Myklebust out:
1226ba60eb25STrond Myklebust 	return ret;
1227ba60eb25STrond Myklebust }
1228ba60eb25STrond Myklebust 
122992ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1230d9ba131dSTrond Myklebust {
1231d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1232d9ba131dSTrond Myklebust 
1233ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1234d9ba131dSTrond Myklebust 		goto out;
1235ff699ea8SChuck Lever 	++xprt->num_reqs;
123692ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
123792ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
123892ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1239d9ba131dSTrond Myklebust 	if (req != NULL)
1240d9ba131dSTrond Myklebust 		goto out;
1241ff699ea8SChuck Lever 	--xprt->num_reqs;
1242d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1243d9ba131dSTrond Myklebust out:
1244d9ba131dSTrond Myklebust 	return req;
1245d9ba131dSTrond Myklebust }
1246d9ba131dSTrond Myklebust 
1247d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1248d9ba131dSTrond Myklebust {
1249ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1250ff699ea8SChuck Lever 		--xprt->num_reqs;
1251d9ba131dSTrond Myklebust 		kfree(req);
1252d9ba131dSTrond Myklebust 		return true;
1253d9ba131dSTrond Myklebust 	}
1254d9ba131dSTrond Myklebust 	return false;
1255d9ba131dSTrond Myklebust }
1256d9ba131dSTrond Myklebust 
1257f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
12581da177e4SLinus Torvalds {
1259d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
12601da177e4SLinus Torvalds 
1261f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
12621da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1263d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1264d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1265d9ba131dSTrond Myklebust 		goto out_init_req;
1266d9ba131dSTrond Myklebust 	}
126792ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1268d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1269d9ba131dSTrond Myklebust 		goto out_init_req;
1270d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1271d9ba131dSTrond Myklebust 	case -ENOMEM:
1272d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1273d9ba131dSTrond Myklebust 				"failed! Retrying\n");
12741afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1275d9ba131dSTrond Myklebust 		break;
1276d9ba131dSTrond Myklebust 	case -EAGAIN:
1277ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1278d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1279e9d47639SGustavo A. R. Silva 		/* fall through */
12801afeaf5cSTrond Myklebust 	default:
1281d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
12821afeaf5cSTrond Myklebust 	}
1283f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1284d9ba131dSTrond Myklebust 	return;
1285d9ba131dSTrond Myklebust out_init_req:
1286ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1287ff699ea8SChuck Lever 				     xprt->num_reqs);
128837ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
128937ac86c3SChuck Lever 
1290d9ba131dSTrond Myklebust 	task->tk_status = 0;
12911da177e4SLinus Torvalds 	task->tk_rqstp = req;
12921da177e4SLinus Torvalds }
1293f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1294f39c1bfbSTrond Myklebust 
1295f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1296f39c1bfbSTrond Myklebust {
1297f39c1bfbSTrond Myklebust 	/* Note: grabbing the xprt_lock_write() ensures that we throttle
1298f39c1bfbSTrond Myklebust 	 * new slot allocation if the transport is congested (i.e. when
1299f39c1bfbSTrond Myklebust 	 * reconnecting a stream transport or when out of socket write
1300f39c1bfbSTrond Myklebust 	 * buffer space).
1301f39c1bfbSTrond Myklebust 	 */
1302f39c1bfbSTrond Myklebust 	if (xprt_lock_write(xprt, task)) {
1303f39c1bfbSTrond Myklebust 		xprt_alloc_slot(xprt, task);
1304f39c1bfbSTrond Myklebust 		xprt_release_write(xprt, task);
1305f39c1bfbSTrond Myklebust 	}
1306f39c1bfbSTrond Myklebust }
1307f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
13081da177e4SLinus Torvalds 
1309a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1310ee5ebe85STrond Myklebust {
1311ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1312c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1313c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1314ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1315c25573b5STrond Myklebust 	}
1316ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1317ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1318ee5ebe85STrond Myklebust }
1319a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1320ee5ebe85STrond Myklebust 
132121de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
132221de0a95STrond Myklebust {
132321de0a95STrond Myklebust 	struct rpc_rqst *req;
132421de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
132521de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
132621de0a95STrond Myklebust 		list_del(&req->rq_list);
132721de0a95STrond Myklebust 		kfree(req);
132821de0a95STrond Myklebust 	}
132921de0a95STrond Myklebust }
133021de0a95STrond Myklebust 
1331d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1332d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1333d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1334bd1722d4SPavel Emelyanov {
1335bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
133621de0a95STrond Myklebust 	struct rpc_rqst *req;
133721de0a95STrond Myklebust 	int i;
1338bd1722d4SPavel Emelyanov 
1339bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1340bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1341bd1722d4SPavel Emelyanov 		goto out;
1342bd1722d4SPavel Emelyanov 
134321de0a95STrond Myklebust 	xprt_init(xprt, net);
134421de0a95STrond Myklebust 
134521de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
134621de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
134721de0a95STrond Myklebust 		if (!req)
13488313164cSwangweidong 			goto out_free;
134921de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
135021de0a95STrond Myklebust 	}
1351d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1352d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1353d9ba131dSTrond Myklebust 	else
135421de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1355d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1356ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1357bd1722d4SPavel Emelyanov 
1358bd1722d4SPavel Emelyanov 	return xprt;
1359bd1722d4SPavel Emelyanov 
1360bd1722d4SPavel Emelyanov out_free:
136121de0a95STrond Myklebust 	xprt_free(xprt);
1362bd1722d4SPavel Emelyanov out:
1363bd1722d4SPavel Emelyanov 	return NULL;
1364bd1722d4SPavel Emelyanov }
1365bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1366bd1722d4SPavel Emelyanov 
1367e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1368e204e621SPavel Emelyanov {
136937aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
137021de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1371fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1372e204e621SPavel Emelyanov }
1373e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1374e204e621SPavel Emelyanov 
1375902c5887STrond Myklebust static void
1376902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1377902c5887STrond Myklebust {
1378902c5887STrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1379902c5887STrond Myklebust }
1380902c5887STrond Myklebust 
13819dc6edcfSTrond Myklebust static __be32
13829dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
13839dc6edcfSTrond Myklebust {
13849dc6edcfSTrond Myklebust 	__be32 xid;
13859dc6edcfSTrond Myklebust 
13869dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
13879dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
13889dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
13899dc6edcfSTrond Myklebust 	return xid;
13909dc6edcfSTrond Myklebust }
13919dc6edcfSTrond Myklebust 
13929dc6edcfSTrond Myklebust static void
13939dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
13949dc6edcfSTrond Myklebust {
13959dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
13969dc6edcfSTrond Myklebust }
13979dc6edcfSTrond Myklebust 
13989dc6edcfSTrond Myklebust static void
13999dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
14009dc6edcfSTrond Myklebust {
14019dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
14029dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
14039dc6edcfSTrond Myklebust 
14049dc6edcfSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
14059dc6edcfSTrond Myklebust 	req->rq_task	= task;
14069dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
14079dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
14089dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
1409902c5887STrond Myklebust 	xprt_init_connect_cookie(req, xprt);
14109dc6edcfSTrond Myklebust 	req->rq_bytes_sent = 0;
14119dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
14129dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
14139dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
14149dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
14159dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
14169dc6edcfSTrond Myklebust 	xprt_reset_majortimeo(req);
14179dc6edcfSTrond Myklebust 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
14189dc6edcfSTrond Myklebust 			req, ntohl(req->rq_xid));
14199dc6edcfSTrond Myklebust }
14209dc6edcfSTrond Myklebust 
14219dc6edcfSTrond Myklebust static void
14229dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
14239dc6edcfSTrond Myklebust {
14249dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
14259dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
14269dc6edcfSTrond Myklebust 		xprt_request_init(task);
14279dc6edcfSTrond Myklebust }
14289dc6edcfSTrond Myklebust 
14299903cd1cSChuck Lever /**
14309903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
14319903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
14329903cd1cSChuck Lever  *
1433ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1434ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
14359903cd1cSChuck Lever  * backlog queue.
14369903cd1cSChuck Lever  */
14379903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
14381da177e4SLinus Torvalds {
1439fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
14401da177e4SLinus Torvalds 
144143cedbf0STrond Myklebust 	task->tk_status = 0;
144243cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
144343cedbf0STrond Myklebust 		return;
144443cedbf0STrond Myklebust 
144543cedbf0STrond Myklebust 	task->tk_timeout = 0;
144643cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1447ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
14489dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1449ba60eb25STrond Myklebust }
1450ba60eb25STrond Myklebust 
1451ba60eb25STrond Myklebust /**
1452ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1453ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1454ba60eb25STrond Myklebust  *
1455ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1456ba60eb25STrond Myklebust  * backlog queue.
1457ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1458ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1459ba60eb25STrond Myklebust  */
1460ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1461ba60eb25STrond Myklebust {
1462fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1463ba60eb25STrond Myklebust 
1464ba60eb25STrond Myklebust 	task->tk_status = 0;
1465ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1466ba60eb25STrond Myklebust 		return;
1467ba60eb25STrond Myklebust 
1468ba60eb25STrond Myklebust 	task->tk_timeout = 0;
1469ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
14709dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
1473edc81dcdSTrond Myklebust static void
1474edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1475edc81dcdSTrond Myklebust {
1476edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1477edc81dcdSTrond Myklebust 
1478944b0429STrond Myklebust 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1479944b0429STrond Myklebust 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1480edc81dcdSTrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1481edc81dcdSTrond Myklebust 		spin_lock(&xprt->queue_lock);
1482944b0429STrond Myklebust 		xprt_request_dequeue_transmit_locked(task);
1483edc81dcdSTrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1484edc81dcdSTrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1485edc81dcdSTrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1486edc81dcdSTrond Myklebust 			spin_unlock(&xprt->queue_lock);
1487edc81dcdSTrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1488edc81dcdSTrond Myklebust 			spin_lock(&xprt->queue_lock);
1489edc81dcdSTrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1490edc81dcdSTrond Myklebust 		}
1491edc81dcdSTrond Myklebust 		spin_unlock(&xprt->queue_lock);
1492edc81dcdSTrond Myklebust 	}
1493edc81dcdSTrond Myklebust }
1494edc81dcdSTrond Myklebust 
14959903cd1cSChuck Lever /**
14969903cd1cSChuck Lever  * xprt_release - release an RPC request slot
14979903cd1cSChuck Lever  * @task: task which is finished with the slot
14989903cd1cSChuck Lever  *
14991da177e4SLinus Torvalds  */
15009903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
15011da177e4SLinus Torvalds {
150255ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
150387ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
15041da177e4SLinus Torvalds 
150587ed5003STrond Myklebust 	if (req == NULL) {
150687ed5003STrond Myklebust 		if (task->tk_client) {
1507fb43d172STrond Myklebust 			xprt = task->tk_xprt;
150887ed5003STrond Myklebust 			if (xprt->snd_task == task)
150987ed5003STrond Myklebust 				xprt_release_write(xprt, task);
151087ed5003STrond Myklebust 		}
15111da177e4SLinus Torvalds 		return;
151287ed5003STrond Myklebust 	}
151355ae1aabSRicardo Labiaga 
151455ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
15150a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
15160a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
15170a702195SWeston Andros Adamson 	else if (task->tk_client)
15180a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
1519edc81dcdSTrond Myklebust 	xprt_request_dequeue_all(task, req);
15204a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
152149e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1522a58dd398SChuck Lever 	if (xprt->ops->release_request)
1523a58dd398SChuck Lever 		xprt->ops->release_request(task);
15241da177e4SLinus Torvalds 	xprt->last_used = jiffies;
1525ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
15264a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1527ee5ebe85STrond Myklebust 	if (req->rq_buffer)
15283435c74aSChuck Lever 		xprt->ops->buf_free(task);
15294a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1530a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1531a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
15321da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1533ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1534ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
153555ae1aabSRicardo Labiaga 
153646121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1537ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1538a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1539ee5ebe85STrond Myklebust 	else
1540c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
1543902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
1544902c5887STrond Myklebust void
1545902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1546902c5887STrond Myklebust {
1547902c5887STrond Myklebust 	struct xdr_buf *xbufp = &req->rq_snd_buf;
1548902c5887STrond Myklebust 
1549902c5887STrond Myklebust 	task->tk_rqstp = req;
1550902c5887STrond Myklebust 	req->rq_task = task;
1551902c5887STrond Myklebust 	xprt_init_connect_cookie(req, req->rq_xprt);
1552902c5887STrond Myklebust 	/*
1553902c5887STrond Myklebust 	 * Set up the xdr_buf length.
1554902c5887STrond Myklebust 	 * This also indicates that the buffer is XDR encoded already.
1555902c5887STrond Myklebust 	 */
1556902c5887STrond Myklebust 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1557902c5887STrond Myklebust 		xbufp->tail[0].iov_len;
1558902c5887STrond Myklebust 	req->rq_bytes_sent = 0;
1559902c5887STrond Myklebust }
1560902c5887STrond Myklebust #endif
1561902c5887STrond Myklebust 
156221de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1563c2866763SChuck Lever {
156430c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1565c2866763SChuck Lever 
1566c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1567c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
156875c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1569c2866763SChuck Lever 
1570c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
1571ef3f5434STrond Myklebust 	INIT_LIST_HEAD(&xprt->recv_queue);
1572944b0429STrond Myklebust 	INIT_LIST_HEAD(&xprt->xmit_queue);
15739e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1574f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1575f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
15769e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
157780b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1578f9acac1aSRicardo Labiaga 
1579c2866763SChuck Lever 	xprt->last_used = jiffies;
1580c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1581a509050bSChuck Lever 	xprt->bind_index = 0;
1582c2866763SChuck Lever 
1583c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1584c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
158534006ceeSTrond Myklebust 	rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1586c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1587c2866763SChuck Lever 
1588c2866763SChuck Lever 	xprt_init_xid(xprt);
1589c2866763SChuck Lever 
159021de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
15918d9266ffSTrond Myklebust }
15928d9266ffSTrond Myklebust 
15938d9266ffSTrond Myklebust /**
15948d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
15958d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
15968d9266ffSTrond Myklebust  *
15978d9266ffSTrond Myklebust  */
15988d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
15998d9266ffSTrond Myklebust {
16008d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
16018d9266ffSTrond Myklebust 	struct xprt_class *t;
16028d9266ffSTrond Myklebust 
16038d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
16048d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
16058d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
16068d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
16078d9266ffSTrond Myklebust 			goto found;
16088d9266ffSTrond Myklebust 		}
16098d9266ffSTrond Myklebust 	}
16108d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
16113c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
16128d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
16138d9266ffSTrond Myklebust 
16148d9266ffSTrond Myklebust found:
16158d9266ffSTrond Myklebust 	xprt = t->setup(args);
16168d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
16178d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
16188d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
161921de0a95STrond Myklebust 		goto out;
16208d9266ffSTrond Myklebust 	}
162133d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
162233d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
162321de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
162421de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
1625ff861c4dSKees Cook 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
162621de0a95STrond Myklebust 	else
1627ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
16284e0038b6STrond Myklebust 
16294e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
16304e0038b6STrond Myklebust 		xprt_destroy(xprt);
16314e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
16324e0038b6STrond Myklebust 	}
16334e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
16344e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
16354e0038b6STrond Myklebust 		xprt_destroy(xprt);
16364e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
16374e0038b6STrond Myklebust 	}
16384e0038b6STrond Myklebust 
16393f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1640388f0c77SJeff Layton 
1641c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1642c2866763SChuck Lever 			xprt->max_reqs);
164321de0a95STrond Myklebust out:
1644c2866763SChuck Lever 	return xprt;
1645c2866763SChuck Lever }
1646c2866763SChuck Lever 
1647528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
1648528fd354STrond Myklebust {
1649528fd354STrond Myklebust 	struct rpc_xprt *xprt =
1650528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
1651528fd354STrond Myklebust 
1652528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
1653528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1654528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1655528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1656528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1657528fd354STrond Myklebust 	kfree(xprt->servername);
1658528fd354STrond Myklebust 	/*
1659528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
1660528fd354STrond Myklebust 	 */
1661528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
1662528fd354STrond Myklebust }
1663528fd354STrond Myklebust 
16649903cd1cSChuck Lever /**
16659903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1666a8de240aSTrond Myklebust  * @xprt: transport to destroy
16679903cd1cSChuck Lever  *
16681da177e4SLinus Torvalds  */
1669a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
16701da177e4SLinus Torvalds {
16711da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
167279234c3dSTrond Myklebust 
1673528fd354STrond Myklebust 	/*
1674528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
1675528fd354STrond Myklebust 	 */
167679234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
167779234c3dSTrond Myklebust 
16780065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1679c8541ecdSChuck Lever 
1680c8541ecdSChuck Lever 	/*
1681528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
1682528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
1683c8541ecdSChuck Lever 	 */
1684528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1685528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
16866b6ca86bSTrond Myklebust }
16871da177e4SLinus Torvalds 
168830c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
168930c5116bSTrond Myklebust {
169030c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
169130c5116bSTrond Myklebust }
169230c5116bSTrond Myklebust 
169330c5116bSTrond Myklebust /**
169430c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
169530c5116bSTrond Myklebust  * @xprt: pointer to the transport
169630c5116bSTrond Myklebust  *
169730c5116bSTrond Myklebust  */
169830c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
169930c5116bSTrond Myklebust {
170030c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
170130c5116bSTrond Myklebust 		return xprt;
170230c5116bSTrond Myklebust 	return NULL;
170330c5116bSTrond Myklebust }
170430c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
170530c5116bSTrond Myklebust 
17066b6ca86bSTrond Myklebust /**
17076b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
17086b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
17096b6ca86bSTrond Myklebust  *
17106b6ca86bSTrond Myklebust  */
17116b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
17126b6ca86bSTrond Myklebust {
171330c5116bSTrond Myklebust 	if (xprt != NULL)
171430c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
17156b6ca86bSTrond Myklebust }
17165d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
1717