xref: /openbmc/linux/net/sunrpc/xprt.c (revision ef3f5434)
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;
19492551948STrond Myklebust 	if (req != NULL)
19512a80469SChuck Lever 		req->rq_ntrans++;
1964d4a76f3Sj223yang@asset.uwaterloo.ca 
19712a80469SChuck Lever 	return 1;
19812a80469SChuck Lever 
19912a80469SChuck Lever out_sleep:
20046121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
20112a80469SChuck Lever 			task->tk_pid, xprt);
20212a80469SChuck Lever 	task->tk_timeout = 0;
20312a80469SChuck Lever 	task->tk_status = -EAGAIN;
20434006ceeSTrond Myklebust 	if (req == NULL)
20534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
20634006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
20734006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
20812a80469SChuck Lever 	else
20934006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
21034006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
21112a80469SChuck Lever 	return 0;
21212a80469SChuck Lever }
21312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
21412a80469SChuck Lever 
215632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
216632e3bdcSTrond Myklebust {
217632e3bdcSTrond Myklebust 	xprt->snd_task = NULL;
218d19751e7STrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
2194e857c58SPeter Zijlstra 		smp_mb__before_atomic();
220632e3bdcSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
2214e857c58SPeter Zijlstra 		smp_mb__after_atomic();
222632e3bdcSTrond Myklebust 	} else
22340a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
224632e3bdcSTrond Myklebust }
225632e3bdcSTrond Myklebust 
22612a80469SChuck Lever /*
22712a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
22812a80469SChuck Lever  * @task: task that is requesting access to the transport
22912a80469SChuck Lever  *
23012a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
23112a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
23212a80469SChuck Lever  * woken up and given access to the transport.
23312a80469SChuck Lever  */
23443cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
23512a80469SChuck Lever {
2361da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
23734006ceeSTrond Myklebust 	int priority;
2381da177e4SLinus Torvalds 
2392226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2401da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2411da177e4SLinus Torvalds 			return 1;
2421da177e4SLinus Torvalds 		goto out_sleep;
2431da177e4SLinus Torvalds 	}
24443cedbf0STrond Myklebust 	if (req == NULL) {
24543cedbf0STrond Myklebust 		xprt->snd_task = task;
24643cedbf0STrond Myklebust 		return 1;
24743cedbf0STrond Myklebust 	}
24812a80469SChuck Lever 	if (__xprt_get_cong(xprt, task)) {
2491da177e4SLinus Torvalds 		xprt->snd_task = task;
2501da177e4SLinus Torvalds 		req->rq_ntrans++;
2511da177e4SLinus Torvalds 		return 1;
2521da177e4SLinus Torvalds 	}
253632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2541da177e4SLinus Torvalds out_sleep:
25529807318SNeil Brown 	if (req)
25629807318SNeil Brown 		__xprt_put_cong(xprt, req);
25746121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2581da177e4SLinus Torvalds 	task->tk_timeout = 0;
2591da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
26034006ceeSTrond Myklebust 	if (req == NULL)
26134006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
26234006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
26334006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
2641da177e4SLinus Torvalds 	else
26534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
26634006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
2671da177e4SLinus Torvalds 	return 0;
2681da177e4SLinus Torvalds }
26912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2701da177e4SLinus Torvalds 
27112a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	int retval;
2741da177e4SLinus Torvalds 
2754a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
27643cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2774a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2781da177e4SLinus Torvalds 	return retval;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
281961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2821da177e4SLinus Torvalds {
283961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
28449e9a890SChuck Lever 	struct rpc_rqst *req;
28549e9a890SChuck Lever 
28649e9a890SChuck Lever 	req = task->tk_rqstp;
28749e9a890SChuck Lever 	xprt->snd_task = task;
28892551948STrond Myklebust 	if (req)
28949e9a890SChuck Lever 		req->rq_ntrans++;
290961a828dSTrond Myklebust 	return true;
291961a828dSTrond Myklebust }
292961a828dSTrond Myklebust 
293961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
294961a828dSTrond Myklebust {
295961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
29649e9a890SChuck Lever 		return;
29749e9a890SChuck Lever 
298f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
299f1dc237cSTrond Myklebust 				__xprt_lock_write_func, xprt))
300961a828dSTrond Myklebust 		return;
301632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
30249e9a890SChuck Lever }
30349e9a890SChuck Lever 
304961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
30549e9a890SChuck Lever {
306961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
30743cedbf0STrond Myklebust 	struct rpc_rqst *req;
3081da177e4SLinus Torvalds 
30943cedbf0STrond Myklebust 	req = task->tk_rqstp;
31043cedbf0STrond Myklebust 	if (req == NULL) {
3111da177e4SLinus Torvalds 		xprt->snd_task = task;
312961a828dSTrond Myklebust 		return true;
31343cedbf0STrond Myklebust 	}
31443cedbf0STrond Myklebust 	if (__xprt_get_cong(xprt, task)) {
31543cedbf0STrond Myklebust 		xprt->snd_task = task;
3161da177e4SLinus Torvalds 		req->rq_ntrans++;
317961a828dSTrond Myklebust 		return true;
3181da177e4SLinus Torvalds 	}
319961a828dSTrond Myklebust 	return false;
320961a828dSTrond Myklebust }
321961a828dSTrond Myklebust 
322961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
323961a828dSTrond Myklebust {
324961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
325961a828dSTrond Myklebust 		return;
326961a828dSTrond Myklebust 	if (RPCXPRT_CONGESTED(xprt))
327961a828dSTrond Myklebust 		goto out_unlock;
328f1dc237cSTrond Myklebust 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
329f1dc237cSTrond Myklebust 				__xprt_lock_write_cong_func, xprt))
330961a828dSTrond Myklebust 		return;
3311da177e4SLinus Torvalds out_unlock:
332632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3350695314eSTrond Myklebust static void xprt_task_clear_bytes_sent(struct rpc_task *task)
3360695314eSTrond Myklebust {
3370695314eSTrond Myklebust 	if (task != NULL) {
3380695314eSTrond Myklebust 		struct rpc_rqst *req = task->tk_rqstp;
3390695314eSTrond Myklebust 		if (req != NULL)
3400695314eSTrond Myklebust 			req->rq_bytes_sent = 0;
3410695314eSTrond Myklebust 	}
3420695314eSTrond Myklebust }
3430695314eSTrond Myklebust 
34449e9a890SChuck Lever /**
34549e9a890SChuck Lever  * xprt_release_xprt - 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.  No congestion control is provided.
3501da177e4SLinus Torvalds  */
35149e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
3540695314eSTrond Myklebust 		xprt_task_clear_bytes_sent(task);
355632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3561da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds }
35912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3601da177e4SLinus Torvalds 
36149e9a890SChuck Lever /**
36249e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
36349e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
36449e9a890SChuck Lever  * @task: task that is releasing access to the transport
36549e9a890SChuck Lever  *
36649e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
36749e9a890SChuck Lever  * transport if the transport's congestion window allows it.
36849e9a890SChuck Lever  */
36949e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
37049e9a890SChuck Lever {
37149e9a890SChuck Lever 	if (xprt->snd_task == task) {
3720695314eSTrond Myklebust 		xprt_task_clear_bytes_sent(task);
373632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
37449e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
37549e9a890SChuck Lever 	}
37649e9a890SChuck Lever }
37712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
37849e9a890SChuck Lever 
37949e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3801da177e4SLinus Torvalds {
3814a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
38249e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3834a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /*
3871da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3881da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds static int
3911da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if (req->rq_cong)
3961da177e4SLinus Torvalds 		return 1;
39746121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
3981da177e4SLinus Torvalds 			task->tk_pid, xprt->cong, xprt->cwnd);
3991da177e4SLinus Torvalds 	if (RPCXPRT_CONGESTED(xprt))
4001da177e4SLinus Torvalds 		return 0;
4011da177e4SLinus Torvalds 	req->rq_cong = 1;
4021da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4031da177e4SLinus Torvalds 	return 1;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4081da177e4SLinus Torvalds  * that has been sleeping due to congestion
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static void
4111da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	if (!req->rq_cong)
4141da177e4SLinus Torvalds 		return;
4151da177e4SLinus Torvalds 	req->rq_cong = 0;
4161da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
41749e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
42046c0ee8bSChuck Lever /**
421a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
422a58dd398SChuck Lever  * @task: RPC request that recently completed
423a58dd398SChuck Lever  *
424a58dd398SChuck Lever  * Useful for transports that require congestion control.
425a58dd398SChuck Lever  */
426a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
427a58dd398SChuck Lever {
428a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
429a4f0835cSTrond Myklebust 
430a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
431a58dd398SChuck Lever }
43212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
433a58dd398SChuck Lever 
434a58dd398SChuck Lever /**
43546c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4366a24dfb6STrond Myklebust  * @xprt: pointer to xprt
43746c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
43846c0ee8bSChuck Lever  * @result: result code of completed RPC request
43946c0ee8bSChuck Lever  *
4404f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4414f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4424f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4434f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4444f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4454f4cf5adSChuck Lever  *
4464f4cf5adSChuck Lever  *	-	a reply is received and
4474f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4484f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4491da177e4SLinus Torvalds  */
4506a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4511da177e4SLinus Torvalds {
45246c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
45346c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4561da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4571da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4581da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4591da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4601da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
46149e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
4621da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
4631da177e4SLinus Torvalds 		cwnd >>= 1;
4641da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
4651da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
4681da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
4691da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
47046c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
4711da177e4SLinus Torvalds }
47212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
4731da177e4SLinus Torvalds 
47444fbac22SChuck Lever /**
47544fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
47644fbac22SChuck Lever  * @xprt: transport with waiting tasks
47744fbac22SChuck Lever  * @status: result code to plant in each task before waking it
47844fbac22SChuck Lever  *
47944fbac22SChuck Lever  */
48044fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
48144fbac22SChuck Lever {
48244fbac22SChuck Lever 	if (status < 0)
48344fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
48444fbac22SChuck Lever 	else
48544fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
48644fbac22SChuck Lever }
48712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
48844fbac22SChuck Lever 
489c7b2cae8SChuck Lever /**
490c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
491c7b2cae8SChuck Lever  * @task: task to be put to sleep
4920b80ae42SRandy Dunlap  * @action: function pointer to be executed after wait
493a9a6b52eSTrond Myklebust  *
494a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
495a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
496a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
497c7b2cae8SChuck Lever  */
498b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
499c7b2cae8SChuck Lever {
500c7b2cae8SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
501c7b2cae8SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
502c7b2cae8SChuck Lever 
503a9a6b52eSTrond Myklebust 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
504b6ddf64fSTrond Myklebust 	rpc_sleep_on(&xprt->pending, task, action);
505c7b2cae8SChuck Lever }
50612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
507c7b2cae8SChuck Lever 
508c7b2cae8SChuck Lever /**
509c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
510c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
511c7b2cae8SChuck Lever  *
512c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
513c7b2cae8SChuck Lever  */
514c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt)
515c7b2cae8SChuck Lever {
516c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
517c7b2cae8SChuck Lever 	if (xprt->snd_task) {
51846121cf7SChuck Lever 		dprintk("RPC:       write space: waking waiting task on "
51946121cf7SChuck Lever 				"xprt %p\n", xprt);
5202275cde4STrond Myklebust 		rpc_wake_up_queued_task_on_wq(xprtiod_workqueue,
5212275cde4STrond Myklebust 				&xprt->pending, xprt->snd_task);
522c7b2cae8SChuck Lever 	}
523c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
524c7b2cae8SChuck Lever }
52512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
526c7b2cae8SChuck Lever 
527fe3aca29SChuck Lever /**
528fe3aca29SChuck Lever  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
529fe3aca29SChuck Lever  * @task: task whose timeout is to be set
530fe3aca29SChuck Lever  *
531fe3aca29SChuck Lever  * Set a request's retransmit timeout based on the transport's
532fe3aca29SChuck Lever  * default timeout parameters.  Used by transports that don't adjust
533fe3aca29SChuck Lever  * the retransmit timeout based on round-trip time estimation.
534fe3aca29SChuck Lever  */
535fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task)
536fe3aca29SChuck Lever {
537fe3aca29SChuck Lever 	task->tk_timeout = task->tk_rqstp->rq_timeout;
538fe3aca29SChuck Lever }
53912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
540fe3aca29SChuck Lever 
5412c53040fSBen Hutchings /**
542fe3aca29SChuck Lever  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
543fe3aca29SChuck Lever  * @task: task whose timeout is to be set
544fe3aca29SChuck Lever  *
545fe3aca29SChuck Lever  * Set a request's retransmit timeout using the RTT estimator.
546fe3aca29SChuck Lever  */
547fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
548fe3aca29SChuck Lever {
549fe3aca29SChuck Lever 	int timer = task->tk_msg.rpc_proc->p_timer;
550ba7392bbSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
551ba7392bbSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
552fe3aca29SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
553ba7392bbSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
554fe3aca29SChuck Lever 
555fe3aca29SChuck Lever 	task->tk_timeout = rpc_calc_rto(rtt, timer);
556fe3aca29SChuck Lever 	task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
557fe3aca29SChuck Lever 	if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
558fe3aca29SChuck Lever 		task->tk_timeout = max_timeout;
559fe3aca29SChuck Lever }
56012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
561fe3aca29SChuck Lever 
5621da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5631da177e4SLinus Torvalds {
564ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5671da177e4SLinus Torvalds 	if (to->to_exponential)
5681da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5691da177e4SLinus Torvalds 	else
5701da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
5711da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
5721da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
5731da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5769903cd1cSChuck Lever /**
5779903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
5789903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
5799903cd1cSChuck Lever  *
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
584ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5851da177e4SLinus Torvalds 	int status = 0;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
5881da177e4SLinus Torvalds 		if (to->to_exponential)
5891da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
5901da177e4SLinus Torvalds 		else
5911da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
5921da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
5931da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
5941da177e4SLinus Torvalds 		req->rq_retries++;
5951da177e4SLinus Torvalds 	} else {
5961da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
5971da177e4SLinus Torvalds 		req->rq_retries = 0;
5981da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
5991da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
6004a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
6011da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
6024a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
6031da177e4SLinus Torvalds 		status = -ETIMEDOUT;
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
6071da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6081da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	return status;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
61365f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6141da177e4SLinus Torvalds {
61565f27f38SDavid Howells 	struct rpc_xprt *xprt =
61665f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
6171da177e4SLinus Torvalds 
61866af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6194876cc77STrond Myklebust 	xprt->ops->close(xprt);
6201da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
62179234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6249903cd1cSChuck Lever /**
62562da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6269903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6279903cd1cSChuck Lever  *
6281da177e4SLinus Torvalds  */
62962da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6324a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6331da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
6342a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6354a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6361da177e4SLinus Torvalds }
63762da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6381da177e4SLinus Torvalds 
63966af1e55STrond Myklebust /**
64066af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
64166af1e55STrond Myklebust  * @xprt: transport to disconnect
64266af1e55STrond Myklebust  *
64366af1e55STrond Myklebust  */
64466af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
64566af1e55STrond Myklebust {
64666af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
64766af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
64866af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
64966af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
65066af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
65140a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6522a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
65366af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
65466af1e55STrond Myklebust }
655e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect);
65666af1e55STrond Myklebust 
6577f3a1d1eSTrond Myklebust static unsigned int
6587f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt)
6597f3a1d1eSTrond Myklebust {
6607f3a1d1eSTrond Myklebust 	return READ_ONCE(xprt->connect_cookie);
6617f3a1d1eSTrond Myklebust }
6627f3a1d1eSTrond Myklebust 
6637f3a1d1eSTrond Myklebust static bool
6647f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task)
6657f3a1d1eSTrond Myklebust {
6667f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6677f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
6687f3a1d1eSTrond Myklebust 
6697f3a1d1eSTrond Myklebust 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
6707f3a1d1eSTrond Myklebust 		!xprt_connected(xprt);
6717f3a1d1eSTrond Myklebust }
6727f3a1d1eSTrond Myklebust 
6737c1d71cfSTrond Myklebust /**
6747c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
6757c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
6767c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
6777c1d71cfSTrond Myklebust  *
6787c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
6797c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
6807c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
6817c1d71cfSTrond Myklebust  * a batch of RPC requests.
6827c1d71cfSTrond Myklebust  *
6837c1d71cfSTrond Myklebust  */
6847c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
6857c1d71cfSTrond Myklebust {
6867c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
6877c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
6887c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
6897c1d71cfSTrond Myklebust 		goto out;
6902c2ee6d2SNeilBrown 	if (test_bit(XPRT_CLOSING, &xprt->state))
6917c1d71cfSTrond Myklebust 		goto out;
6927c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
6937c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
6947c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
69540a5f1b1STrond Myklebust 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
6962a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6977c1d71cfSTrond Myklebust out:
6987c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
6997c1d71cfSTrond Myklebust }
7007c1d71cfSTrond Myklebust 
701ad3331acSTrond Myklebust static bool
702ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt)
703ad3331acSTrond Myklebust {
704ad3331acSTrond Myklebust 	return xprt->idle_timeout != 0;
705ad3331acSTrond Myklebust }
706ad3331acSTrond Myklebust 
707ad3331acSTrond Myklebust static void
708ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
709ad3331acSTrond Myklebust 	__must_hold(&xprt->transport_lock)
710ad3331acSTrond Myklebust {
711ef3f5434STrond Myklebust 	if (list_empty(&xprt->recv_queue) && xprt_has_timer(xprt))
712ad3331acSTrond Myklebust 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
713ad3331acSTrond Myklebust }
714ad3331acSTrond Myklebust 
7151da177e4SLinus Torvalds static void
716ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t)
7171da177e4SLinus Torvalds {
718ff861c4dSKees Cook 	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
7191da177e4SLinus Torvalds 
7204a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
721ef3f5434STrond Myklebust 	if (!list_empty(&xprt->recv_queue))
7221da177e4SLinus Torvalds 		goto out_abort;
723ad3331acSTrond Myklebust 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
724ad3331acSTrond Myklebust 	xprt->last_used = jiffies;
7252226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
7261da177e4SLinus Torvalds 		goto out_abort;
7274a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
72840a5f1b1STrond Myklebust 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7291da177e4SLinus Torvalds 	return;
7301da177e4SLinus Torvalds out_abort:
7314a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
735718ba5b8STrond Myklebust 		struct rpc_task *task,
736718ba5b8STrond Myklebust 		void *cookie)
737718ba5b8STrond Myklebust {
738718ba5b8STrond Myklebust 	bool ret = false;
739718ba5b8STrond Myklebust 
740718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
741718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
742718ba5b8STrond Myklebust 		goto out;
743718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
744718ba5b8STrond Myklebust 		goto out;
7450695314eSTrond Myklebust 	xprt_task_clear_bytes_sent(task);
746718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
747718ba5b8STrond Myklebust 	ret = true;
748718ba5b8STrond Myklebust out:
749718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
750718ba5b8STrond Myklebust 	return ret;
751718ba5b8STrond Myklebust }
752718ba5b8STrond Myklebust 
753718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
754718ba5b8STrond Myklebust {
755718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
756718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
757718ba5b8STrond Myklebust 		goto out;
758718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
759718ba5b8STrond Myklebust 		goto out;
760718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
761718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
762ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
763718ba5b8STrond Myklebust out:
764718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
76579234c3dSTrond Myklebust 	wake_up_bit(&xprt->state, XPRT_LOCKED);
766718ba5b8STrond Myklebust }
767718ba5b8STrond Myklebust 
7689903cd1cSChuck Lever /**
7699903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
7709903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
7711da177e4SLinus Torvalds  *
7721da177e4SLinus Torvalds  */
7731da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
7741da177e4SLinus Torvalds {
775ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7761da177e4SLinus Torvalds 
77746121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7781da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7791da177e4SLinus Torvalds 
780ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
78101d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7821da177e4SLinus Torvalds 		return;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7851da177e4SLinus Torvalds 		return;
786feb8ca37STrond Myklebust 
787feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
788feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
789feb8ca37STrond Myklebust 
790718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
7911da177e4SLinus Torvalds 		task->tk_rqstp->rq_bytes_sent = 0;
792a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
7932c2ee6d2SNeilBrown 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
7945d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
7950b9e7943STrond Myklebust 
7960b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
7970b9e7943STrond Myklebust 			return;
7980b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
7990b9e7943STrond Myklebust 			return;
800262ca07dSChuck Lever 		xprt->stat.connect_start = jiffies;
8011b092092STrond Myklebust 		xprt->ops->connect(xprt, task);
8021da177e4SLinus Torvalds 	}
803718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8069903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task)
8071da177e4SLinus Torvalds {
808ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
8091da177e4SLinus Torvalds 
810cd983ef8SChuck Lever 	if (task->tk_status == 0) {
811262ca07dSChuck Lever 		xprt->stat.connect_count++;
812262ca07dSChuck Lever 		xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
81346121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connection established\n",
8141da177e4SLinus Torvalds 				task->tk_pid);
8151da177e4SLinus Torvalds 		return;
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	switch (task->tk_status) {
8190fe8d04eSTrond Myklebust 	case -ECONNREFUSED:
8200fe8d04eSTrond Myklebust 	case -ECONNRESET:
8210fe8d04eSTrond Myklebust 	case -ECONNABORTED:
8220fe8d04eSTrond Myklebust 	case -ENETUNREACH:
8230fe8d04eSTrond Myklebust 	case -EHOSTUNREACH:
8242fc193cfSTrond Myklebust 	case -EPIPE:
8252a491991STrond Myklebust 	case -EAGAIN:
8262a491991STrond Myklebust 		dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
82723475d66SChuck Lever 		break;
8281da177e4SLinus Torvalds 	case -ETIMEDOUT:
82946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
83046121cf7SChuck Lever 				"out\n", task->tk_pid);
8311da177e4SLinus Torvalds 		break;
8321da177e4SLinus Torvalds 	default:
83346121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
83446121cf7SChuck Lever 				"server %s\n", task->tk_pid, -task->tk_status,
8354e0038b6STrond Myklebust 				xprt->servername);
83623475d66SChuck Lever 		task->tk_status = -EIO;
83723475d66SChuck Lever 	}
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8409903cd1cSChuck Lever /**
8419903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
8429903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
8439903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
8449903cd1cSChuck Lever  *
84575c84151STrond Myklebust  * Caller holds xprt->queue_lock.
8461da177e4SLinus Torvalds  */
847d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
8481da177e4SLinus Torvalds {
8498f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
8501da177e4SLinus Torvalds 
851ef3f5434STrond Myklebust 	list_for_each_entry(entry, &xprt->recv_queue, rq_recv)
8523705ad64SJeff Layton 		if (entry->rq_xid == xid) {
8533705ad64SJeff Layton 			trace_xprt_lookup_rqst(xprt, xid, 0);
8540b87a46bSChuck Lever 			entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
855262ca07dSChuck Lever 			return entry;
8563705ad64SJeff Layton 		}
85746121cf7SChuck Lever 
85846121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
85946121cf7SChuck Lever 			ntohl(xid));
8603705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
861262ca07dSChuck Lever 	xprt->stat.bad_xids++;
862262ca07dSChuck Lever 	return NULL;
8631da177e4SLinus Torvalds }
86412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
8651da177e4SLinus Torvalds 
866cf9946cdSTrond Myklebust static bool
867cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req)
868cf9946cdSTrond Myklebust {
869cf9946cdSTrond Myklebust 	return atomic_read(&req->rq_pin) != 0;
870cf9946cdSTrond Myklebust }
871cf9946cdSTrond Myklebust 
872729749bbSTrond Myklebust /**
873729749bbSTrond Myklebust  * xprt_pin_rqst - Pin a request on the transport receive list
874729749bbSTrond Myklebust  * @req: Request to pin
875729749bbSTrond Myklebust  *
876729749bbSTrond Myklebust  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
877cf9946cdSTrond Myklebust  * so should be holding the xprt receive lock.
878729749bbSTrond Myklebust  */
879729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req)
880729749bbSTrond Myklebust {
881cf9946cdSTrond Myklebust 	atomic_inc(&req->rq_pin);
882729749bbSTrond Myklebust }
8839590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst);
884729749bbSTrond Myklebust 
885729749bbSTrond Myklebust /**
886729749bbSTrond Myklebust  * xprt_unpin_rqst - Unpin a request on the transport receive list
887729749bbSTrond Myklebust  * @req: Request to pin
888729749bbSTrond Myklebust  *
889cf9946cdSTrond Myklebust  * Caller should be holding the xprt receive lock.
890729749bbSTrond Myklebust  */
891729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req)
892729749bbSTrond Myklebust {
893cf9946cdSTrond Myklebust 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
894cf9946cdSTrond Myklebust 		atomic_dec(&req->rq_pin);
895cf9946cdSTrond Myklebust 		return;
896cf9946cdSTrond Myklebust 	}
897cf9946cdSTrond Myklebust 	if (atomic_dec_and_test(&req->rq_pin))
898cf9946cdSTrond Myklebust 		wake_up_var(&req->rq_pin);
899729749bbSTrond Myklebust }
9009590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
901729749bbSTrond Myklebust 
902729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
903729749bbSTrond Myklebust {
904cf9946cdSTrond Myklebust 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
905729749bbSTrond Myklebust }
906729749bbSTrond Myklebust 
907edc81dcdSTrond Myklebust static bool
908edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task)
909edc81dcdSTrond Myklebust {
910edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
911edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
912edc81dcdSTrond Myklebust }
913edc81dcdSTrond Myklebust 
914edc81dcdSTrond Myklebust static bool
915edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
916edc81dcdSTrond Myklebust {
917edc81dcdSTrond Myklebust 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
918edc81dcdSTrond Myklebust 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
919edc81dcdSTrond Myklebust }
920edc81dcdSTrond Myklebust 
921edc81dcdSTrond Myklebust /**
922edc81dcdSTrond Myklebust  * xprt_request_enqueue_receive - Add an request to the receive queue
923edc81dcdSTrond Myklebust  * @task: RPC task
924edc81dcdSTrond Myklebust  *
925edc81dcdSTrond Myklebust  */
926edc81dcdSTrond Myklebust void
927edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task)
928edc81dcdSTrond Myklebust {
929edc81dcdSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
930edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
931edc81dcdSTrond Myklebust 
932edc81dcdSTrond Myklebust 	if (!xprt_request_need_enqueue_receive(task, req))
933edc81dcdSTrond Myklebust 		return;
934edc81dcdSTrond Myklebust 	spin_lock(&xprt->queue_lock);
935edc81dcdSTrond Myklebust 
936edc81dcdSTrond Myklebust 	/* Update the softirq receive buffer */
937edc81dcdSTrond Myklebust 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
938edc81dcdSTrond Myklebust 			sizeof(req->rq_private_buf));
939edc81dcdSTrond Myklebust 
940edc81dcdSTrond Myklebust 	/* Add request to the receive list */
941ef3f5434STrond Myklebust 	list_add_tail(&req->rq_recv, &xprt->recv_queue);
942edc81dcdSTrond Myklebust 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
943edc81dcdSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
944edc81dcdSTrond Myklebust 
945edc81dcdSTrond Myklebust 	xprt_reset_majortimeo(req);
946edc81dcdSTrond Myklebust 	/* Turn off autodisconnect */
947edc81dcdSTrond Myklebust 	del_singleshot_timer_sync(&xprt->timer);
948edc81dcdSTrond Myklebust }
949edc81dcdSTrond Myklebust 
950edc81dcdSTrond Myklebust /**
951edc81dcdSTrond Myklebust  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
952edc81dcdSTrond Myklebust  * @task: RPC task
953edc81dcdSTrond Myklebust  *
954edc81dcdSTrond Myklebust  * Caller must hold xprt->queue_lock.
955edc81dcdSTrond Myklebust  */
956edc81dcdSTrond Myklebust static void
957edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task)
958edc81dcdSTrond Myklebust {
959edc81dcdSTrond Myklebust 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
960ef3f5434STrond Myklebust 		list_del(&task->tk_rqstp->rq_recv);
961edc81dcdSTrond Myklebust }
962edc81dcdSTrond Myklebust 
963ecd465eeSChuck Lever /**
964ecd465eeSChuck Lever  * xprt_update_rtt - Update RPC RTT statistics
965ecd465eeSChuck Lever  * @task: RPC request that recently completed
966ecd465eeSChuck Lever  *
96775c84151STrond Myklebust  * Caller holds xprt->queue_lock.
968ecd465eeSChuck Lever  */
969ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task)
9701da177e4SLinus Torvalds {
9711570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
9721570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
97395c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
974d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
9751570c1e4SChuck Lever 
9761da177e4SLinus Torvalds 	if (timer) {
9771da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
978ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
9791570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds }
982ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt);
9831da177e4SLinus Torvalds 
9841570c1e4SChuck Lever /**
9851570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
9861570c1e4SChuck Lever  * @task: RPC request that recently completed
9871570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
9881570c1e4SChuck Lever  *
98975c84151STrond Myklebust  * Caller holds xprt->queue_lock.
9901570c1e4SChuck Lever  */
9911570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
9921570c1e4SChuck Lever {
9931570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
994fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
9951da177e4SLinus Torvalds 
9961570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
9971570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
9983705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
9991da177e4SLinus Torvalds 
1000fda13939STrond Myklebust 	xprt->stat.recvs++;
1001ef759a2eSChuck Lever 
10021e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
1003dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
1004dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
100543ac3f29STrond Myklebust 	smp_wmb();
1006dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
1007edc81dcdSTrond Myklebust 	xprt_request_dequeue_receive_locked(task);
1008fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
10091da177e4SLinus Torvalds }
101012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
10111da177e4SLinus Torvalds 
101246c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
10131da177e4SLinus Torvalds {
10141da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
10151da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
10161da177e4SLinus Torvalds 
10175d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
10185d00837bSTrond Myklebust 		return;
101946c0ee8bSChuck Lever 
102082476d9fSChuck Lever 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1021dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
102246c0ee8bSChuck Lever 		if (xprt->ops->timer)
10236a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
10245d00837bSTrond Myklebust 	} else
10255d00837bSTrond Myklebust 		task->tk_status = 0;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10289903cd1cSChuck Lever /**
10297f3a1d1eSTrond Myklebust  * xprt_request_wait_receive - wait for the reply to an RPC request
10307f3a1d1eSTrond Myklebust  * @task: RPC task about to send a request
10317f3a1d1eSTrond Myklebust  *
10327f3a1d1eSTrond Myklebust  */
10337f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task)
10347f3a1d1eSTrond Myklebust {
10357f3a1d1eSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
10367f3a1d1eSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
10377f3a1d1eSTrond Myklebust 
10387f3a1d1eSTrond Myklebust 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
10397f3a1d1eSTrond Myklebust 		return;
10407f3a1d1eSTrond Myklebust 	/*
10417f3a1d1eSTrond Myklebust 	 * Sleep on the pending queue if we're expecting a reply.
10427f3a1d1eSTrond Myklebust 	 * The spinlock ensures atomicity between the test of
10437f3a1d1eSTrond Myklebust 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
10447f3a1d1eSTrond Myklebust 	 */
10457f3a1d1eSTrond Myklebust 	spin_lock(&xprt->queue_lock);
10467f3a1d1eSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
10477f3a1d1eSTrond Myklebust 		xprt->ops->set_retrans_timeout(task);
10487f3a1d1eSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_timer);
10497f3a1d1eSTrond Myklebust 		/*
10507f3a1d1eSTrond Myklebust 		 * Send an extra queue wakeup call if the
10517f3a1d1eSTrond Myklebust 		 * connection was dropped in case the call to
10527f3a1d1eSTrond Myklebust 		 * rpc_sleep_on() raced.
10537f3a1d1eSTrond Myklebust 		 */
10547f3a1d1eSTrond Myklebust 		if (xprt_request_retransmit_after_disconnect(task))
10557f3a1d1eSTrond Myklebust 			rpc_wake_up_queued_task_set_status(&xprt->pending,
10567f3a1d1eSTrond Myklebust 					task, -ENOTCONN);
10577f3a1d1eSTrond Myklebust 	}
10587f3a1d1eSTrond Myklebust 	spin_unlock(&xprt->queue_lock);
10597f3a1d1eSTrond Myklebust }
10607f3a1d1eSTrond Myklebust 
10617f3a1d1eSTrond Myklebust /**
10629903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
10639903cd1cSChuck Lever  * @task: RPC task about to send a request
10649903cd1cSChuck Lever  *
10651da177e4SLinus Torvalds  */
106690051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
10691da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
107090051ea7STrond Myklebust 	bool ret = false;
10711da177e4SLinus Torvalds 
107246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
10731da177e4SLinus Torvalds 
10744a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
10758a19a0b6STrond Myklebust 	if (!req->rq_bytes_sent) {
10768a19a0b6STrond Myklebust 		if (req->rq_reply_bytes_recvd) {
107790051ea7STrond Myklebust 			task->tk_status = req->rq_reply_bytes_recvd;
10781da177e4SLinus Torvalds 			goto out_unlock;
10791da177e4SLinus Torvalds 		}
10807f3a1d1eSTrond Myklebust 		if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
10817f3a1d1eSTrond Myklebust 		    !xprt_request_retransmit_after_disconnect(task)) {
10828a19a0b6STrond Myklebust 			xprt->ops->set_retrans_timeout(task);
10838a19a0b6STrond Myklebust 			rpc_sleep_on(&xprt->pending, task, xprt_timer);
10848a19a0b6STrond Myklebust 			goto out_unlock;
10858a19a0b6STrond Myklebust 		}
10868a19a0b6STrond Myklebust 	}
108790051ea7STrond Myklebust 	if (!xprt->ops->reserve_xprt(xprt, task)) {
108890051ea7STrond Myklebust 		task->tk_status = -EAGAIN;
108990051ea7STrond Myklebust 		goto out_unlock;
109090051ea7STrond Myklebust 	}
109190051ea7STrond Myklebust 	ret = true;
10921da177e4SLinus Torvalds out_unlock:
10934a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
109490051ea7STrond Myklebust 	return ret;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
1097e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
10985e5ce5beSTrond Myklebust {
1099343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
11005e5ce5beSTrond Myklebust }
11015e5ce5beSTrond Myklebust 
11029903cd1cSChuck Lever /**
11039903cd1cSChuck Lever  * xprt_transmit - send an RPC request on a transport
11049903cd1cSChuck Lever  * @task: controlling RPC task
11059903cd1cSChuck Lever  *
11069903cd1cSChuck Lever  * We have to copy the iovec because sendmsg fiddles with its contents.
11079903cd1cSChuck Lever  */
11089903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
11111da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
111290d91b0cSTrond Myklebust 	unsigned int connect_cookie;
1113ff699ea8SChuck Lever 	int status;
11141da177e4SLinus Torvalds 
111546121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
11161da177e4SLinus Torvalds 
1117edc81dcdSTrond Myklebust 	if (!req->rq_bytes_sent) {
1118edc81dcdSTrond Myklebust 		if (xprt_request_data_received(task))
1119edc81dcdSTrond Myklebust 			return;
11203021a5bbSTrond Myklebust 		/* Verify that our message lies in the RPCSEC_GSS window */
1121edc81dcdSTrond Myklebust 		if (rpcauth_xmit_need_reencode(task)) {
11223021a5bbSTrond Myklebust 			task->tk_status = -EBADMSG;
11233021a5bbSTrond Myklebust 			return;
11243021a5bbSTrond Myklebust 		}
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 
112790d91b0cSTrond Myklebust 	connect_cookie = xprt->connect_cookie;
1128a246b010SChuck Lever 	status = xprt->ops->send_request(task);
11293705ad64SJeff Layton 	trace_xprt_transmit(xprt, req->rq_xid, status);
1130c8485e4dSTrond Myklebust 	if (status != 0) {
1131c8485e4dSTrond Myklebust 		task->tk_status = status;
1132c8485e4dSTrond Myklebust 		return;
1133c8485e4dSTrond Myklebust 	}
11347ebbbc6eSTrond Myklebust 
11354a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1136c8485e4dSTrond Myklebust 
113746121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
11387ebbbc6eSTrond Myklebust 	clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1139468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
1140fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
1141262ca07dSChuck Lever 
1142262ca07dSChuck Lever 	xprt->stat.sends++;
1143262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1144262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
114515a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
114615a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
1147fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
114890d91b0cSTrond Myklebust 
114990d91b0cSTrond Myklebust 	req->rq_connect_cookie = connect_cookie;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
1152ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1153ba60eb25STrond Myklebust {
1154ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1155ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1156ba60eb25STrond Myklebust }
1157ba60eb25STrond Myklebust 
1158ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1159ba60eb25STrond Myklebust {
1160ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1161ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1162ba60eb25STrond Myklebust }
1163ba60eb25STrond Myklebust 
1164ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1165ba60eb25STrond Myklebust {
1166ba60eb25STrond Myklebust 	bool ret = false;
1167ba60eb25STrond Myklebust 
1168ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1169ba60eb25STrond Myklebust 		goto out;
1170ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1171ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1172ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1173ba60eb25STrond Myklebust 		ret = true;
1174ba60eb25STrond Myklebust 	}
1175ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1176ba60eb25STrond Myklebust out:
1177ba60eb25STrond Myklebust 	return ret;
1178ba60eb25STrond Myklebust }
1179ba60eb25STrond Myklebust 
118092ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1181d9ba131dSTrond Myklebust {
1182d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1183d9ba131dSTrond Myklebust 
1184ff699ea8SChuck Lever 	if (xprt->num_reqs >= xprt->max_reqs)
1185d9ba131dSTrond Myklebust 		goto out;
1186ff699ea8SChuck Lever 	++xprt->num_reqs;
118792ea011fSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
118892ea011fSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
118992ea011fSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
1190d9ba131dSTrond Myklebust 	if (req != NULL)
1191d9ba131dSTrond Myklebust 		goto out;
1192ff699ea8SChuck Lever 	--xprt->num_reqs;
1193d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1194d9ba131dSTrond Myklebust out:
1195d9ba131dSTrond Myklebust 	return req;
1196d9ba131dSTrond Myklebust }
1197d9ba131dSTrond Myklebust 
1198d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1199d9ba131dSTrond Myklebust {
1200ff699ea8SChuck Lever 	if (xprt->num_reqs > xprt->min_reqs) {
1201ff699ea8SChuck Lever 		--xprt->num_reqs;
1202d9ba131dSTrond Myklebust 		kfree(req);
1203d9ba131dSTrond Myklebust 		return true;
1204d9ba131dSTrond Myklebust 	}
1205d9ba131dSTrond Myklebust 	return false;
1206d9ba131dSTrond Myklebust }
1207d9ba131dSTrond Myklebust 
1208f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
12091da177e4SLinus Torvalds {
1210d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
12111da177e4SLinus Torvalds 
1212f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
12131da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1214d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1215d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1216d9ba131dSTrond Myklebust 		goto out_init_req;
1217d9ba131dSTrond Myklebust 	}
121892ea011fSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt);
1219d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1220d9ba131dSTrond Myklebust 		goto out_init_req;
1221d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1222d9ba131dSTrond Myklebust 	case -ENOMEM:
1223d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1224d9ba131dSTrond Myklebust 				"failed! Retrying\n");
12251afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1226d9ba131dSTrond Myklebust 		break;
1227d9ba131dSTrond Myklebust 	case -EAGAIN:
1228ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1229d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
1230e9d47639SGustavo A. R. Silva 		/* fall through */
12311afeaf5cSTrond Myklebust 	default:
1232d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
12331afeaf5cSTrond Myklebust 	}
1234f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1235d9ba131dSTrond Myklebust 	return;
1236d9ba131dSTrond Myklebust out_init_req:
1237ff699ea8SChuck Lever 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1238ff699ea8SChuck Lever 				     xprt->num_reqs);
123937ac86c3SChuck Lever 	spin_unlock(&xprt->reserve_lock);
124037ac86c3SChuck Lever 
1241d9ba131dSTrond Myklebust 	task->tk_status = 0;
12421da177e4SLinus Torvalds 	task->tk_rqstp = req;
12431da177e4SLinus Torvalds }
1244f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1245f39c1bfbSTrond Myklebust 
1246f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1247f39c1bfbSTrond Myklebust {
1248f39c1bfbSTrond Myklebust 	/* Note: grabbing the xprt_lock_write() ensures that we throttle
1249f39c1bfbSTrond Myklebust 	 * new slot allocation if the transport is congested (i.e. when
1250f39c1bfbSTrond Myklebust 	 * reconnecting a stream transport or when out of socket write
1251f39c1bfbSTrond Myklebust 	 * buffer space).
1252f39c1bfbSTrond Myklebust 	 */
1253f39c1bfbSTrond Myklebust 	if (xprt_lock_write(xprt, task)) {
1254f39c1bfbSTrond Myklebust 		xprt_alloc_slot(xprt, task);
1255f39c1bfbSTrond Myklebust 		xprt_release_write(xprt, task);
1256f39c1bfbSTrond Myklebust 	}
1257f39c1bfbSTrond Myklebust }
1258f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
12591da177e4SLinus Torvalds 
1260a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1261ee5ebe85STrond Myklebust {
1262ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1263c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1264c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1265ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1266c25573b5STrond Myklebust 	}
1267ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1268ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1269ee5ebe85STrond Myklebust }
1270a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot);
1271ee5ebe85STrond Myklebust 
127221de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
127321de0a95STrond Myklebust {
127421de0a95STrond Myklebust 	struct rpc_rqst *req;
127521de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
127621de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
127721de0a95STrond Myklebust 		list_del(&req->rq_list);
127821de0a95STrond Myklebust 		kfree(req);
127921de0a95STrond Myklebust 	}
128021de0a95STrond Myklebust }
128121de0a95STrond Myklebust 
1282d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1283d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1284d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1285bd1722d4SPavel Emelyanov {
1286bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
128721de0a95STrond Myklebust 	struct rpc_rqst *req;
128821de0a95STrond Myklebust 	int i;
1289bd1722d4SPavel Emelyanov 
1290bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1291bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1292bd1722d4SPavel Emelyanov 		goto out;
1293bd1722d4SPavel Emelyanov 
129421de0a95STrond Myklebust 	xprt_init(xprt, net);
129521de0a95STrond Myklebust 
129621de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
129721de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
129821de0a95STrond Myklebust 		if (!req)
12998313164cSwangweidong 			goto out_free;
130021de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
130121de0a95STrond Myklebust 	}
1302d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1303d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1304d9ba131dSTrond Myklebust 	else
130521de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1306d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1307ff699ea8SChuck Lever 	xprt->num_reqs = num_prealloc;
1308bd1722d4SPavel Emelyanov 
1309bd1722d4SPavel Emelyanov 	return xprt;
1310bd1722d4SPavel Emelyanov 
1311bd1722d4SPavel Emelyanov out_free:
131221de0a95STrond Myklebust 	xprt_free(xprt);
1313bd1722d4SPavel Emelyanov out:
1314bd1722d4SPavel Emelyanov 	return NULL;
1315bd1722d4SPavel Emelyanov }
1316bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1317bd1722d4SPavel Emelyanov 
1318e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1319e204e621SPavel Emelyanov {
132037aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
132121de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1322fda1bfefSTrond Myklebust 	kfree_rcu(xprt, rcu);
1323e204e621SPavel Emelyanov }
1324e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1325e204e621SPavel Emelyanov 
13269dc6edcfSTrond Myklebust static __be32
13279dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt)
13289dc6edcfSTrond Myklebust {
13299dc6edcfSTrond Myklebust 	__be32 xid;
13309dc6edcfSTrond Myklebust 
13319dc6edcfSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
13329dc6edcfSTrond Myklebust 	xid = (__force __be32)xprt->xid++;
13339dc6edcfSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
13349dc6edcfSTrond Myklebust 	return xid;
13359dc6edcfSTrond Myklebust }
13369dc6edcfSTrond Myklebust 
13379dc6edcfSTrond Myklebust static void
13389dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt)
13399dc6edcfSTrond Myklebust {
13409dc6edcfSTrond Myklebust 	xprt->xid = prandom_u32();
13419dc6edcfSTrond Myklebust }
13429dc6edcfSTrond Myklebust 
13439dc6edcfSTrond Myklebust static void
13449dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task)
13459dc6edcfSTrond Myklebust {
13469dc6edcfSTrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
13479dc6edcfSTrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
13489dc6edcfSTrond Myklebust 
13499dc6edcfSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
13509dc6edcfSTrond Myklebust 	req->rq_task	= task;
13519dc6edcfSTrond Myklebust 	req->rq_xprt    = xprt;
13529dc6edcfSTrond Myklebust 	req->rq_buffer  = NULL;
13539dc6edcfSTrond Myklebust 	req->rq_xid	= xprt_alloc_xid(xprt);
13547f3a1d1eSTrond Myklebust 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
13559dc6edcfSTrond Myklebust 	req->rq_bytes_sent = 0;
13569dc6edcfSTrond Myklebust 	req->rq_snd_buf.len = 0;
13579dc6edcfSTrond Myklebust 	req->rq_snd_buf.buflen = 0;
13589dc6edcfSTrond Myklebust 	req->rq_rcv_buf.len = 0;
13599dc6edcfSTrond Myklebust 	req->rq_rcv_buf.buflen = 0;
13609dc6edcfSTrond Myklebust 	req->rq_release_snd_buf = NULL;
13619dc6edcfSTrond Myklebust 	xprt_reset_majortimeo(req);
13629dc6edcfSTrond Myklebust 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
13639dc6edcfSTrond Myklebust 			req, ntohl(req->rq_xid));
13649dc6edcfSTrond Myklebust }
13659dc6edcfSTrond Myklebust 
13669dc6edcfSTrond Myklebust static void
13679dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
13689dc6edcfSTrond Myklebust {
13699dc6edcfSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
13709dc6edcfSTrond Myklebust 	if (task->tk_rqstp != NULL)
13719dc6edcfSTrond Myklebust 		xprt_request_init(task);
13729dc6edcfSTrond Myklebust }
13739dc6edcfSTrond Myklebust 
13749903cd1cSChuck Lever /**
13759903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
13769903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
13779903cd1cSChuck Lever  *
1378ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1379ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
13809903cd1cSChuck Lever  * backlog queue.
13819903cd1cSChuck Lever  */
13829903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
13831da177e4SLinus Torvalds {
1384fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
13851da177e4SLinus Torvalds 
138643cedbf0STrond Myklebust 	task->tk_status = 0;
138743cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
138843cedbf0STrond Myklebust 		return;
138943cedbf0STrond Myklebust 
139043cedbf0STrond Myklebust 	task->tk_timeout = 0;
139143cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
1392ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
13939dc6edcfSTrond Myklebust 		xprt_do_reserve(xprt, task);
1394ba60eb25STrond Myklebust }
1395ba60eb25STrond Myklebust 
1396ba60eb25STrond Myklebust /**
1397ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1398ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1399ba60eb25STrond Myklebust  *
1400ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1401ba60eb25STrond Myklebust  * backlog queue.
1402ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1403ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1404ba60eb25STrond Myklebust  */
1405ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1406ba60eb25STrond Myklebust {
1407fb43d172STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1408ba60eb25STrond Myklebust 
1409ba60eb25STrond Myklebust 	task->tk_status = 0;
1410ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1411ba60eb25STrond Myklebust 		return;
1412ba60eb25STrond Myklebust 
1413ba60eb25STrond Myklebust 	task->tk_timeout = 0;
1414ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
14159dc6edcfSTrond Myklebust 	xprt_do_reserve(xprt, task);
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
1418edc81dcdSTrond Myklebust static void
1419edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1420edc81dcdSTrond Myklebust {
1421edc81dcdSTrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
1422edc81dcdSTrond Myklebust 
1423edc81dcdSTrond Myklebust 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1424edc81dcdSTrond Myklebust 	    xprt_is_pinned_rqst(req)) {
1425edc81dcdSTrond Myklebust 		spin_lock(&xprt->queue_lock);
1426edc81dcdSTrond Myklebust 		xprt_request_dequeue_receive_locked(task);
1427edc81dcdSTrond Myklebust 		while (xprt_is_pinned_rqst(req)) {
1428edc81dcdSTrond Myklebust 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1429edc81dcdSTrond Myklebust 			spin_unlock(&xprt->queue_lock);
1430edc81dcdSTrond Myklebust 			xprt_wait_on_pinned_rqst(req);
1431edc81dcdSTrond Myklebust 			spin_lock(&xprt->queue_lock);
1432edc81dcdSTrond Myklebust 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1433edc81dcdSTrond Myklebust 		}
1434edc81dcdSTrond Myklebust 		spin_unlock(&xprt->queue_lock);
1435edc81dcdSTrond Myklebust 	}
1436edc81dcdSTrond Myklebust }
1437edc81dcdSTrond Myklebust 
14389903cd1cSChuck Lever /**
14399903cd1cSChuck Lever  * xprt_release - release an RPC request slot
14409903cd1cSChuck Lever  * @task: task which is finished with the slot
14419903cd1cSChuck Lever  *
14421da177e4SLinus Torvalds  */
14439903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
14441da177e4SLinus Torvalds {
144555ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
144687ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
14471da177e4SLinus Torvalds 
144887ed5003STrond Myklebust 	if (req == NULL) {
144987ed5003STrond Myklebust 		if (task->tk_client) {
1450fb43d172STrond Myklebust 			xprt = task->tk_xprt;
145187ed5003STrond Myklebust 			if (xprt->snd_task == task)
145287ed5003STrond Myklebust 				xprt_release_write(xprt, task);
145387ed5003STrond Myklebust 		}
14541da177e4SLinus Torvalds 		return;
145587ed5003STrond Myklebust 	}
145655ae1aabSRicardo Labiaga 
145755ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
14580a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
14590a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
14600a702195SWeston Andros Adamson 	else if (task->tk_client)
14610a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
1462edc81dcdSTrond Myklebust 	xprt_request_dequeue_all(task, req);
14634a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
146449e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1465a58dd398SChuck Lever 	if (xprt->ops->release_request)
1466a58dd398SChuck Lever 		xprt->ops->release_request(task);
14671da177e4SLinus Torvalds 	xprt->last_used = jiffies;
1468ad3331acSTrond Myklebust 	xprt_schedule_autodisconnect(xprt);
14694a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1470ee5ebe85STrond Myklebust 	if (req->rq_buffer)
14713435c74aSChuck Lever 		xprt->ops->buf_free(task);
14724a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1473a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1474a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
14751da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1476ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1477ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
147855ae1aabSRicardo Labiaga 
147946121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1480ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1481a9cde23aSChuck Lever 		xprt->ops->free_slot(xprt, req);
1482ee5ebe85STrond Myklebust 	else
1483c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
148621de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1487c2866763SChuck Lever {
148830c5116bSTrond Myklebust 	kref_init(&xprt->kref);
1489c2866763SChuck Lever 
1490c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1491c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
149275c84151STrond Myklebust 	spin_lock_init(&xprt->queue_lock);
1493c2866763SChuck Lever 
1494c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
1495ef3f5434STrond Myklebust 	INIT_LIST_HEAD(&xprt->recv_queue);
14969e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1497f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1498f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
14999e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
150080b14d5eSTrond Myklebust 	INIT_LIST_HEAD(&xprt->xprt_switch);
1501f9acac1aSRicardo Labiaga 
1502c2866763SChuck Lever 	xprt->last_used = jiffies;
1503c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1504a509050bSChuck Lever 	xprt->bind_index = 0;
1505c2866763SChuck Lever 
1506c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1507c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
150834006ceeSTrond Myklebust 	rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1509c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1510c2866763SChuck Lever 
1511c2866763SChuck Lever 	xprt_init_xid(xprt);
1512c2866763SChuck Lever 
151321de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
15148d9266ffSTrond Myklebust }
15158d9266ffSTrond Myklebust 
15168d9266ffSTrond Myklebust /**
15178d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
15188d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
15198d9266ffSTrond Myklebust  *
15208d9266ffSTrond Myklebust  */
15218d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
15228d9266ffSTrond Myklebust {
15238d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
15248d9266ffSTrond Myklebust 	struct xprt_class *t;
15258d9266ffSTrond Myklebust 
15268d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
15278d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
15288d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
15298d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
15308d9266ffSTrond Myklebust 			goto found;
15318d9266ffSTrond Myklebust 		}
15328d9266ffSTrond Myklebust 	}
15338d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
15343c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
15358d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
15368d9266ffSTrond Myklebust 
15378d9266ffSTrond Myklebust found:
15388d9266ffSTrond Myklebust 	xprt = t->setup(args);
15398d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
15408d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
15418d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
154221de0a95STrond Myklebust 		goto out;
15438d9266ffSTrond Myklebust 	}
154433d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
154533d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
154621de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
154721de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
1548ff861c4dSKees Cook 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
154921de0a95STrond Myklebust 	else
1550ff861c4dSKees Cook 		timer_setup(&xprt->timer, NULL, 0);
15514e0038b6STrond Myklebust 
15524e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
15534e0038b6STrond Myklebust 		xprt_destroy(xprt);
15544e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
15554e0038b6STrond Myklebust 	}
15564e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
15574e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
15584e0038b6STrond Myklebust 		xprt_destroy(xprt);
15594e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
15604e0038b6STrond Myklebust 	}
15614e0038b6STrond Myklebust 
15623f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1563388f0c77SJeff Layton 
1564c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1565c2866763SChuck Lever 			xprt->max_reqs);
156621de0a95STrond Myklebust out:
1567c2866763SChuck Lever 	return xprt;
1568c2866763SChuck Lever }
1569c2866763SChuck Lever 
1570528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work)
1571528fd354STrond Myklebust {
1572528fd354STrond Myklebust 	struct rpc_xprt *xprt =
1573528fd354STrond Myklebust 		container_of(work, struct rpc_xprt, task_cleanup);
1574528fd354STrond Myklebust 
1575528fd354STrond Myklebust 	rpc_xprt_debugfs_unregister(xprt);
1576528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1577528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1578528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1579528fd354STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1580528fd354STrond Myklebust 	kfree(xprt->servername);
1581528fd354STrond Myklebust 	/*
1582528fd354STrond Myklebust 	 * Tear down transport state and free the rpc_xprt
1583528fd354STrond Myklebust 	 */
1584528fd354STrond Myklebust 	xprt->ops->destroy(xprt);
1585528fd354STrond Myklebust }
1586528fd354STrond Myklebust 
15879903cd1cSChuck Lever /**
15889903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1589a8de240aSTrond Myklebust  * @xprt: transport to destroy
15909903cd1cSChuck Lever  *
15911da177e4SLinus Torvalds  */
1592a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
159579234c3dSTrond Myklebust 
1596528fd354STrond Myklebust 	/*
1597528fd354STrond Myklebust 	 * Exclude transport connect/disconnect handlers and autoclose
1598528fd354STrond Myklebust 	 */
159979234c3dSTrond Myklebust 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
160079234c3dSTrond Myklebust 
16010065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1602c8541ecdSChuck Lever 
1603c8541ecdSChuck Lever 	/*
1604528fd354STrond Myklebust 	 * Destroy sockets etc from the system workqueue so they can
1605528fd354STrond Myklebust 	 * safely flush receive work running on rpciod.
1606c8541ecdSChuck Lever 	 */
1607528fd354STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1608528fd354STrond Myklebust 	schedule_work(&xprt->task_cleanup);
16096b6ca86bSTrond Myklebust }
16101da177e4SLinus Torvalds 
161130c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref)
161230c5116bSTrond Myklebust {
161330c5116bSTrond Myklebust 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
161430c5116bSTrond Myklebust }
161530c5116bSTrond Myklebust 
161630c5116bSTrond Myklebust /**
161730c5116bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
161830c5116bSTrond Myklebust  * @xprt: pointer to the transport
161930c5116bSTrond Myklebust  *
162030c5116bSTrond Myklebust  */
162130c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
162230c5116bSTrond Myklebust {
162330c5116bSTrond Myklebust 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
162430c5116bSTrond Myklebust 		return xprt;
162530c5116bSTrond Myklebust 	return NULL;
162630c5116bSTrond Myklebust }
162730c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get);
162830c5116bSTrond Myklebust 
16296b6ca86bSTrond Myklebust /**
16306b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
16316b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
16326b6ca86bSTrond Myklebust  *
16336b6ca86bSTrond Myklebust  */
16346b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
16356b6ca86bSTrond Myklebust {
163630c5116bSTrond Myklebust 	if (xprt != NULL)
163730c5116bSTrond Myklebust 		kref_put(&xprt->kref, xprt_destroy_kref);
16386b6ca86bSTrond Myklebust }
16395d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put);
1640