xref: /openbmc/linux/net/sunrpc/xprt.c (revision 961a828d)
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>
511da177e4SLinus Torvalds 
5255ae1aabSRicardo Labiaga #include "sunrpc.h"
5355ae1aabSRicardo Labiaga 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * Local variables
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #ifdef RPC_DEBUG
591da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
601da177e4SLinus Torvalds #endif
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * Local functions
641da177e4SLinus Torvalds  */
6521de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
661da177e4SLinus Torvalds static void	xprt_request_init(struct rpc_task *, struct rpc_xprt *);
671da177e4SLinus Torvalds static void	xprt_connect_status(struct rpc_task *task);
681da177e4SLinus Torvalds static int      __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
691da177e4SLinus Torvalds 
705ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7181c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7281c098afS\"Talpey, Thomas\ 
73555ee3afSChuck Lever /*
74555ee3afSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
75555ee3afSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
76555ee3afSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
77555ee3afSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
78555ee3afSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
79555ee3afSChuck Lever  *
80555ee3afSChuck Lever  *	-	a reply is received and
81555ee3afSChuck Lever  *	-	a full number of requests are outstanding and
82555ee3afSChuck Lever  *	-	the congestion window hasn't been updated recently.
83555ee3afSChuck Lever  */
84555ee3afSChuck Lever #define RPC_CWNDSHIFT		(8U)
85555ee3afSChuck Lever #define RPC_CWNDSCALE		(1U << RPC_CWNDSHIFT)
86555ee3afSChuck Lever #define RPC_INITCWND		RPC_CWNDSCALE
87555ee3afSChuck Lever #define RPC_MAXCWND(xprt)	((xprt)->max_reqs << RPC_CWNDSHIFT)
88555ee3afSChuck Lever 
89555ee3afSChuck Lever #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
901da177e4SLinus Torvalds 
9112a80469SChuck Lever /**
9281c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
9381c098afS\"Talpey, Thomas\  * @transport: transport to register
9481c098afS\"Talpey, Thomas\  *
9581c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9681c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9781c098afS\"Talpey, Thomas\  *
9881c098afS\"Talpey, Thomas\  * Returns:
9981c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
10081c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
10181c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
10281c098afS\"Talpey, Thomas\  */
10381c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
10481c098afS\"Talpey, Thomas\ {
10581c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10681c098afS\"Talpey, Thomas\ 	int result;
10781c098afS\"Talpey, Thomas\ 
10881c098afS\"Talpey, Thomas\ 	result = -EEXIST;
10981c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
11081c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
11181c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1124fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
11381c098afS\"Talpey, Thomas\ 			goto out;
11481c098afS\"Talpey, Thomas\ 	}
11581c098afS\"Talpey, Thomas\ 
11681c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11781c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11881c098afS\"Talpey, Thomas\ 	       transport->name);
11981c098afS\"Talpey, Thomas\ 	result = 0;
12081c098afS\"Talpey, Thomas\ 
12181c098afS\"Talpey, Thomas\ out:
12281c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
12381c098afS\"Talpey, Thomas\ 	return result;
12481c098afS\"Talpey, Thomas\ }
12581c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12681c098afS\"Talpey, Thomas\ 
12781c098afS\"Talpey, Thomas\ /**
12881c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
12965b6e42cSRandy Dunlap  * @transport: transport to unregister
13081c098afS\"Talpey, Thomas\  *
13181c098afS\"Talpey, Thomas\  * Returns:
13281c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
13381c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
13481c098afS\"Talpey, Thomas\  */
13581c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13681c098afS\"Talpey, Thomas\ {
13781c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13881c098afS\"Talpey, Thomas\ 	int result;
13981c098afS\"Talpey, Thomas\ 
14081c098afS\"Talpey, Thomas\ 	result = 0;
14181c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
14281c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
14381c098afS\"Talpey, Thomas\ 		if (t == transport) {
14481c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
14581c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14681c098afS\"Talpey, Thomas\ 				transport->name);
14781c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14881c098afS\"Talpey, Thomas\ 			goto out;
14981c098afS\"Talpey, Thomas\ 		}
15081c098afS\"Talpey, Thomas\ 	}
15181c098afS\"Talpey, Thomas\ 	result = -ENOENT;
15281c098afS\"Talpey, Thomas\ 
15381c098afS\"Talpey, Thomas\ out:
15481c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
15581c098afS\"Talpey, Thomas\ 	return result;
15681c098afS\"Talpey, Thomas\ }
15781c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15881c098afS\"Talpey, Thomas\ 
15981c098afS\"Talpey, Thomas\ /**
160441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
161441e3e24STom Talpey  * @transport_name: transport to load
162441e3e24STom Talpey  *
163441e3e24STom Talpey  * Returns:
164441e3e24STom Talpey  * 0:		transport successfully loaded
165441e3e24STom Talpey  * -ENOENT:	transport module not available
166441e3e24STom Talpey  */
167441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
168441e3e24STom Talpey {
169441e3e24STom Talpey 	struct xprt_class *t;
170441e3e24STom Talpey 	int result;
171441e3e24STom Talpey 
172441e3e24STom Talpey 	result = 0;
173441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
174441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
175441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
176441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
177441e3e24STom Talpey 			goto out;
178441e3e24STom Talpey 		}
179441e3e24STom Talpey 	}
180441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
181ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
182441e3e24STom Talpey out:
183441e3e24STom Talpey 	return result;
184441e3e24STom Talpey }
185441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
186441e3e24STom Talpey 
187441e3e24STom Talpey /**
18812a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
18912a80469SChuck Lever  * @task: task that is requesting access to the transport
190177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
19112a80469SChuck Lever  *
19212a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
19312a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
19412a80469SChuck Lever  * is provided.
1951da177e4SLinus Torvalds  */
19643cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1971da177e4SLinus Torvalds {
19812a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
19934006ceeSTrond Myklebust 	int priority;
20012a80469SChuck Lever 
20112a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
20212a80469SChuck Lever 		if (task == xprt->snd_task)
20312a80469SChuck Lever 			return 1;
20412a80469SChuck Lever 		goto out_sleep;
20512a80469SChuck Lever 	}
20612a80469SChuck Lever 	xprt->snd_task = task;
20743cedbf0STrond Myklebust 	if (req != NULL) {
20812a80469SChuck Lever 		req->rq_bytes_sent = 0;
20912a80469SChuck Lever 		req->rq_ntrans++;
21043cedbf0STrond Myklebust 	}
2114d4a76f3Sj223yang@asset.uwaterloo.ca 
21212a80469SChuck Lever 	return 1;
21312a80469SChuck Lever 
21412a80469SChuck Lever out_sleep:
21546121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
21612a80469SChuck Lever 			task->tk_pid, xprt);
21712a80469SChuck Lever 	task->tk_timeout = 0;
21812a80469SChuck Lever 	task->tk_status = -EAGAIN;
21934006ceeSTrond Myklebust 	if (req == NULL)
22034006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
22134006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
22234006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
22312a80469SChuck Lever 	else
22434006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
22534006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
22612a80469SChuck Lever 	return 0;
22712a80469SChuck Lever }
22812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
22912a80469SChuck Lever 
230632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
231632e3bdcSTrond Myklebust {
232632e3bdcSTrond Myklebust 	xprt->snd_task = NULL;
233632e3bdcSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
234632e3bdcSTrond Myklebust 		smp_mb__before_clear_bit();
235632e3bdcSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
236632e3bdcSTrond Myklebust 		smp_mb__after_clear_bit();
237632e3bdcSTrond Myklebust 	} else
238c1384c9cSTrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
239632e3bdcSTrond Myklebust }
240632e3bdcSTrond Myklebust 
24112a80469SChuck Lever /*
24212a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
24312a80469SChuck Lever  * @task: task that is requesting access to the transport
24412a80469SChuck Lever  *
24512a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
24612a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
24712a80469SChuck Lever  * woken up and given access to the transport.
24812a80469SChuck Lever  */
24943cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
25012a80469SChuck Lever {
2511da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
25234006ceeSTrond Myklebust 	int priority;
2531da177e4SLinus Torvalds 
2542226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2551da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2561da177e4SLinus Torvalds 			return 1;
2571da177e4SLinus Torvalds 		goto out_sleep;
2581da177e4SLinus Torvalds 	}
25943cedbf0STrond Myklebust 	if (req == NULL) {
26043cedbf0STrond Myklebust 		xprt->snd_task = task;
26143cedbf0STrond Myklebust 		return 1;
26243cedbf0STrond Myklebust 	}
26312a80469SChuck Lever 	if (__xprt_get_cong(xprt, task)) {
2641da177e4SLinus Torvalds 		xprt->snd_task = task;
2651da177e4SLinus Torvalds 		req->rq_bytes_sent = 0;
2661da177e4SLinus Torvalds 		req->rq_ntrans++;
2671da177e4SLinus Torvalds 		return 1;
2681da177e4SLinus Torvalds 	}
269632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2701da177e4SLinus Torvalds out_sleep:
27146121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2721da177e4SLinus Torvalds 	task->tk_timeout = 0;
2731da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
27434006ceeSTrond Myklebust 	if (req == NULL)
27534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
27634006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
27734006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
2781da177e4SLinus Torvalds 	else
27934006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
28034006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
2811da177e4SLinus Torvalds 	return 0;
2821da177e4SLinus Torvalds }
28312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2841da177e4SLinus Torvalds 
28512a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	int retval;
2881da177e4SLinus Torvalds 
2894a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
29043cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2914a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2921da177e4SLinus Torvalds 	return retval;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
295961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2961da177e4SLinus Torvalds {
297961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
29849e9a890SChuck Lever 	struct rpc_rqst *req;
29949e9a890SChuck Lever 
30049e9a890SChuck Lever 	req = task->tk_rqstp;
30149e9a890SChuck Lever 	xprt->snd_task = task;
30249e9a890SChuck Lever 	if (req) {
30349e9a890SChuck Lever 		req->rq_bytes_sent = 0;
30449e9a890SChuck Lever 		req->rq_ntrans++;
30549e9a890SChuck Lever 	}
306961a828dSTrond Myklebust 	return true;
307961a828dSTrond Myklebust }
308961a828dSTrond Myklebust 
309961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
310961a828dSTrond Myklebust {
311961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
31249e9a890SChuck Lever 		return;
31349e9a890SChuck Lever 
314961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt))
315961a828dSTrond Myklebust 		return;
316632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
31749e9a890SChuck Lever }
31849e9a890SChuck Lever 
319961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
32049e9a890SChuck Lever {
321961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
32243cedbf0STrond Myklebust 	struct rpc_rqst *req;
3231da177e4SLinus Torvalds 
32443cedbf0STrond Myklebust 	req = task->tk_rqstp;
32543cedbf0STrond Myklebust 	if (req == NULL) {
3261da177e4SLinus Torvalds 		xprt->snd_task = task;
327961a828dSTrond Myklebust 		return true;
32843cedbf0STrond Myklebust 	}
32943cedbf0STrond Myklebust 	if (__xprt_get_cong(xprt, task)) {
33043cedbf0STrond Myklebust 		xprt->snd_task = task;
3311da177e4SLinus Torvalds 		req->rq_bytes_sent = 0;
3321da177e4SLinus Torvalds 		req->rq_ntrans++;
333961a828dSTrond Myklebust 		return true;
3341da177e4SLinus Torvalds 	}
335961a828dSTrond Myklebust 	return false;
336961a828dSTrond Myklebust }
337961a828dSTrond Myklebust 
338961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
339961a828dSTrond Myklebust {
340961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
341961a828dSTrond Myklebust 		return;
342961a828dSTrond Myklebust 	if (RPCXPRT_CONGESTED(xprt))
343961a828dSTrond Myklebust 		goto out_unlock;
344961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt))
345961a828dSTrond Myklebust 		return;
3461da177e4SLinus Torvalds out_unlock:
347632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
35049e9a890SChuck Lever /**
35149e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
35249e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
35349e9a890SChuck Lever  * @task: task that is releasing access to the transport
35449e9a890SChuck Lever  *
35549e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3561da177e4SLinus Torvalds  */
35749e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
360632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3611da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds }
36412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3651da177e4SLinus Torvalds 
36649e9a890SChuck Lever /**
36749e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
36849e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
36949e9a890SChuck Lever  * @task: task that is releasing access to the transport
37049e9a890SChuck Lever  *
37149e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
37249e9a890SChuck Lever  * transport if the transport's congestion window allows it.
37349e9a890SChuck Lever  */
37449e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
37549e9a890SChuck Lever {
37649e9a890SChuck Lever 	if (xprt->snd_task == task) {
377632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
37849e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
37949e9a890SChuck Lever 	}
38049e9a890SChuck Lever }
38112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
38249e9a890SChuck Lever 
38349e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3841da177e4SLinus Torvalds {
3854a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
38649e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3874a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3921da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3931da177e4SLinus Torvalds  */
3941da177e4SLinus Torvalds static int
3951da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (req->rq_cong)
4001da177e4SLinus Torvalds 		return 1;
40146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
4021da177e4SLinus Torvalds 			task->tk_pid, xprt->cong, xprt->cwnd);
4031da177e4SLinus Torvalds 	if (RPCXPRT_CONGESTED(xprt))
4041da177e4SLinus Torvalds 		return 0;
4051da177e4SLinus Torvalds 	req->rq_cong = 1;
4061da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4071da177e4SLinus Torvalds 	return 1;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4121da177e4SLinus Torvalds  * that has been sleeping due to congestion
4131da177e4SLinus Torvalds  */
4141da177e4SLinus Torvalds static void
4151da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	if (!req->rq_cong)
4181da177e4SLinus Torvalds 		return;
4191da177e4SLinus Torvalds 	req->rq_cong = 0;
4201da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
42149e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
42446c0ee8bSChuck Lever /**
425a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
426a58dd398SChuck Lever  * @task: RPC request that recently completed
427a58dd398SChuck Lever  *
428a58dd398SChuck Lever  * Useful for transports that require congestion control.
429a58dd398SChuck Lever  */
430a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
431a58dd398SChuck Lever {
432a58dd398SChuck Lever 	__xprt_put_cong(task->tk_xprt, task->tk_rqstp);
433a58dd398SChuck Lever }
43412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
435a58dd398SChuck Lever 
436a58dd398SChuck Lever /**
43746c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
43846c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
43946c0ee8bSChuck Lever  * @result: result code of completed RPC request
44046c0ee8bSChuck Lever  *
4411da177e4SLinus Torvalds  * We use a time-smoothed congestion estimator to avoid heavy oscillation.
4421da177e4SLinus Torvalds  */
44346c0ee8bSChuck Lever void xprt_adjust_cwnd(struct rpc_task *task, int result)
4441da177e4SLinus Torvalds {
44546c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
44646c0ee8bSChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
44746c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4501da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4511da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4521da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4531da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4541da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
45549e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
4561da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
4571da177e4SLinus Torvalds 		cwnd >>= 1;
4581da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
4591da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
4621da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
4631da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
46446c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
4651da177e4SLinus Torvalds }
46612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
4671da177e4SLinus Torvalds 
46844fbac22SChuck Lever /**
46944fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
47044fbac22SChuck Lever  * @xprt: transport with waiting tasks
47144fbac22SChuck Lever  * @status: result code to plant in each task before waking it
47244fbac22SChuck Lever  *
47344fbac22SChuck Lever  */
47444fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
47544fbac22SChuck Lever {
47644fbac22SChuck Lever 	if (status < 0)
47744fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
47844fbac22SChuck Lever 	else
47944fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
48044fbac22SChuck Lever }
48112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
48244fbac22SChuck Lever 
483c7b2cae8SChuck Lever /**
484c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
485c7b2cae8SChuck Lever  * @task: task to be put to sleep
4860b80ae42SRandy Dunlap  * @action: function pointer to be executed after wait
487c7b2cae8SChuck Lever  */
488b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
489c7b2cae8SChuck Lever {
490c7b2cae8SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
491c7b2cae8SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
492c7b2cae8SChuck Lever 
493c7b2cae8SChuck Lever 	task->tk_timeout = req->rq_timeout;
494b6ddf64fSTrond Myklebust 	rpc_sleep_on(&xprt->pending, task, action);
495c7b2cae8SChuck Lever }
49612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
497c7b2cae8SChuck Lever 
498c7b2cae8SChuck Lever /**
499c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
500c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
501c7b2cae8SChuck Lever  *
502c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
503c7b2cae8SChuck Lever  */
504c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt)
505c7b2cae8SChuck Lever {
506c7b2cae8SChuck Lever 	if (unlikely(xprt->shutdown))
507c7b2cae8SChuck Lever 		return;
508c7b2cae8SChuck Lever 
509c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
510c7b2cae8SChuck Lever 	if (xprt->snd_task) {
51146121cf7SChuck Lever 		dprintk("RPC:       write space: waking waiting task on "
51246121cf7SChuck Lever 				"xprt %p\n", xprt);
513fda13939STrond Myklebust 		rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
514c7b2cae8SChuck Lever 	}
515c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
516c7b2cae8SChuck Lever }
51712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
518c7b2cae8SChuck Lever 
519fe3aca29SChuck Lever /**
520fe3aca29SChuck Lever  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
521fe3aca29SChuck Lever  * @task: task whose timeout is to be set
522fe3aca29SChuck Lever  *
523fe3aca29SChuck Lever  * Set a request's retransmit timeout based on the transport's
524fe3aca29SChuck Lever  * default timeout parameters.  Used by transports that don't adjust
525fe3aca29SChuck Lever  * the retransmit timeout based on round-trip time estimation.
526fe3aca29SChuck Lever  */
527fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task)
528fe3aca29SChuck Lever {
529fe3aca29SChuck Lever 	task->tk_timeout = task->tk_rqstp->rq_timeout;
530fe3aca29SChuck Lever }
53112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
532fe3aca29SChuck Lever 
533fe3aca29SChuck Lever /*
534fe3aca29SChuck Lever  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
535fe3aca29SChuck Lever  * @task: task whose timeout is to be set
536fe3aca29SChuck Lever  *
537fe3aca29SChuck Lever  * Set a request's retransmit timeout using the RTT estimator.
538fe3aca29SChuck Lever  */
539fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
540fe3aca29SChuck Lever {
541fe3aca29SChuck Lever 	int timer = task->tk_msg.rpc_proc->p_timer;
542ba7392bbSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
543ba7392bbSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
544fe3aca29SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
545ba7392bbSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
546fe3aca29SChuck Lever 
547fe3aca29SChuck Lever 	task->tk_timeout = rpc_calc_rto(rtt, timer);
548fe3aca29SChuck Lever 	task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
549fe3aca29SChuck Lever 	if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
550fe3aca29SChuck Lever 		task->tk_timeout = max_timeout;
551fe3aca29SChuck Lever }
55212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
553fe3aca29SChuck Lever 
5541da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5551da177e4SLinus Torvalds {
556ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5591da177e4SLinus Torvalds 	if (to->to_exponential)
5601da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5611da177e4SLinus Torvalds 	else
5621da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
5631da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
5641da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
5651da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5689903cd1cSChuck Lever /**
5699903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
5709903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
5719903cd1cSChuck Lever  *
5721da177e4SLinus Torvalds  */
5731da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
576ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5771da177e4SLinus Torvalds 	int status = 0;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
5801da177e4SLinus Torvalds 		if (to->to_exponential)
5811da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
5821da177e4SLinus Torvalds 		else
5831da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
5841da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
5851da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
5861da177e4SLinus Torvalds 		req->rq_retries++;
5871da177e4SLinus Torvalds 	} else {
5881da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
5891da177e4SLinus Torvalds 		req->rq_retries = 0;
5901da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
5911da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
5924a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
5931da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
5944a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
5951da177e4SLinus Torvalds 		status = -ETIMEDOUT;
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
5991da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6001da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	return status;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
60565f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6061da177e4SLinus Torvalds {
60765f27f38SDavid Howells 	struct rpc_xprt *xprt =
60865f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
6091da177e4SLinus Torvalds 
610a246b010SChuck Lever 	xprt->ops->close(xprt);
61166af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6121da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6159903cd1cSChuck Lever /**
61662da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6179903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6189903cd1cSChuck Lever  *
6191da177e4SLinus Torvalds  */
62062da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6234a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6241da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
6252a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6264a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6271da177e4SLinus Torvalds }
62862da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6291da177e4SLinus Torvalds 
63066af1e55STrond Myklebust /**
63166af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
63266af1e55STrond Myklebust  * @xprt: transport to disconnect
63366af1e55STrond Myklebust  *
63466af1e55STrond Myklebust  */
63566af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
63666af1e55STrond Myklebust {
63766af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
63866af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
63966af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
64066af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
64166af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
64266af1e55STrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
6432a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
64466af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
64566af1e55STrond Myklebust }
64666af1e55STrond Myklebust 
6477c1d71cfSTrond Myklebust /**
6487c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
6497c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
6507c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
6517c1d71cfSTrond Myklebust  *
6527c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
6537c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
6547c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
6557c1d71cfSTrond Myklebust  * a batch of RPC requests.
6567c1d71cfSTrond Myklebust  *
6577c1d71cfSTrond Myklebust  */
6587c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
6597c1d71cfSTrond Myklebust {
6607c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
6617c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
6627c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
6637c1d71cfSTrond Myklebust 		goto out;
6647c1d71cfSTrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
6657c1d71cfSTrond Myklebust 		goto out;
6667c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
6677c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
6687c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
6697c1d71cfSTrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
6702a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6717c1d71cfSTrond Myklebust out:
6727c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
6737c1d71cfSTrond Myklebust }
6747c1d71cfSTrond Myklebust 
6751da177e4SLinus Torvalds static void
6761da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	struct rpc_xprt *xprt = (struct rpc_xprt *)data;
6791da177e4SLinus Torvalds 
6804a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
6811da177e4SLinus Torvalds 	if (!list_empty(&xprt->recv) || xprt->shutdown)
6821da177e4SLinus Torvalds 		goto out_abort;
6832226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
6841da177e4SLinus Torvalds 		goto out_abort;
6854a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
686f75e6745STrond Myklebust 	set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
687c1384c9cSTrond Myklebust 	queue_work(rpciod_workqueue, &xprt->task_cleanup);
6881da177e4SLinus Torvalds 	return;
6891da177e4SLinus Torvalds out_abort:
6904a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6939903cd1cSChuck Lever /**
6949903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
6959903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
7011da177e4SLinus Torvalds 
70246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7031da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7041da177e4SLinus Torvalds 
705ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
70601d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7071da177e4SLinus Torvalds 		return;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7101da177e4SLinus Torvalds 		return;
711feb8ca37STrond Myklebust 
712feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
713feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
714feb8ca37STrond Myklebust 
7151da177e4SLinus Torvalds 	if (xprt_connected(xprt))
716a246b010SChuck Lever 		xprt_release_write(xprt, task);
717a246b010SChuck Lever 	else {
7181da177e4SLinus Torvalds 		if (task->tk_rqstp)
7191da177e4SLinus Torvalds 			task->tk_rqstp->rq_bytes_sent = 0;
7201da177e4SLinus Torvalds 
721a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
7225d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
7230b9e7943STrond Myklebust 
7240b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
7250b9e7943STrond Myklebust 			return;
7260b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
7270b9e7943STrond Myklebust 			return;
728262ca07dSChuck Lever 		xprt->stat.connect_start = jiffies;
729a246b010SChuck Lever 		xprt->ops->connect(task);
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7339903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
7361da177e4SLinus Torvalds 
737cd983ef8SChuck Lever 	if (task->tk_status == 0) {
738262ca07dSChuck Lever 		xprt->stat.connect_count++;
739262ca07dSChuck Lever 		xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
74046121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connection established\n",
7411da177e4SLinus Torvalds 				task->tk_pid);
7421da177e4SLinus Torvalds 		return;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	switch (task->tk_status) {
7462a491991STrond Myklebust 	case -EAGAIN:
7472a491991STrond Myklebust 		dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
74823475d66SChuck Lever 		break;
7491da177e4SLinus Torvalds 	case -ETIMEDOUT:
75046121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
75146121cf7SChuck Lever 				"out\n", task->tk_pid);
7521da177e4SLinus Torvalds 		break;
7531da177e4SLinus Torvalds 	default:
75446121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
75546121cf7SChuck Lever 				"server %s\n", task->tk_pid, -task->tk_status,
75646121cf7SChuck Lever 				task->tk_client->cl_server);
7571da177e4SLinus Torvalds 		xprt_release_write(xprt, task);
75823475d66SChuck Lever 		task->tk_status = -EIO;
75923475d66SChuck Lever 	}
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7629903cd1cSChuck Lever /**
7639903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
7649903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
7659903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
7669903cd1cSChuck Lever  *
7671da177e4SLinus Torvalds  */
768d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
7691da177e4SLinus Torvalds {
7708f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
7711da177e4SLinus Torvalds 
7728f3a6de3SPavel Emelyanov 	list_for_each_entry(entry, &xprt->recv, rq_list)
773262ca07dSChuck Lever 		if (entry->rq_xid == xid)
774262ca07dSChuck Lever 			return entry;
77546121cf7SChuck Lever 
77646121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
77746121cf7SChuck Lever 			ntohl(xid));
778262ca07dSChuck Lever 	xprt->stat.bad_xids++;
779262ca07dSChuck Lever 	return NULL;
7801da177e4SLinus Torvalds }
78112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
7821da177e4SLinus Torvalds 
783bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task)
7841da177e4SLinus Torvalds {
7851570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
7861570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
7871da177e4SLinus Torvalds 	unsigned timer = task->tk_msg.rpc_proc->p_timer;
788d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
7891570c1e4SChuck Lever 
7901da177e4SLinus Torvalds 	if (timer) {
7911da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
792ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
7931570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971570c1e4SChuck Lever /**
7981570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
7991570c1e4SChuck Lever  * @task: RPC request that recently completed
8001570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
8011570c1e4SChuck Lever  *
8021570c1e4SChuck Lever  * Caller holds transport lock.
8031570c1e4SChuck Lever  */
8041570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
8051570c1e4SChuck Lever {
8061570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
807fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
8081da177e4SLinus Torvalds 
8091570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
8101570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
8111da177e4SLinus Torvalds 
812fda13939STrond Myklebust 	xprt->stat.recvs++;
813d60dbb20STrond Myklebust 	req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
814bbc72ceaSChuck Lever 	if (xprt->ops->timer != NULL)
815bbc72ceaSChuck Lever 		xprt_update_rtt(task);
816ef759a2eSChuck Lever 
8171da177e4SLinus Torvalds 	list_del_init(&req->rq_list);
8181e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
819dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
820dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
82143ac3f29STrond Myklebust 	smp_wmb();
822dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
823fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
8241da177e4SLinus Torvalds }
82512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
8261da177e4SLinus Torvalds 
82746c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
8301da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
8311da177e4SLinus Torvalds 
8325d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
8335d00837bSTrond Myklebust 		return;
83446121cf7SChuck Lever 	dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
83546c0ee8bSChuck Lever 
8365d00837bSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
837dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
83846c0ee8bSChuck Lever 		if (xprt->ops->timer)
83946c0ee8bSChuck Lever 			xprt->ops->timer(task);
8405d00837bSTrond Myklebust 	} else
8415d00837bSTrond Myklebust 		task->tk_status = 0;
8425d00837bSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8454cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt)
8464cfc7e60SRahul Iyer {
8474cfc7e60SRahul Iyer 	return xprt->idle_timeout != 0;
8484cfc7e60SRahul Iyer }
8494cfc7e60SRahul Iyer 
8509903cd1cSChuck Lever /**
8519903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
8529903cd1cSChuck Lever  * @task: RPC task about to send a request
8539903cd1cSChuck Lever  *
8541da177e4SLinus Torvalds  */
8559903cd1cSChuck Lever int xprt_prepare_transmit(struct rpc_task *task)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
8581da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
8591da177e4SLinus Torvalds 	int err = 0;
8601da177e4SLinus Torvalds 
86146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
8621da177e4SLinus Torvalds 
8634a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
864dd2b63d0SRicardo Labiaga 	if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
865dd2b63d0SRicardo Labiaga 		err = req->rq_reply_bytes_recvd;
8661da177e4SLinus Torvalds 		goto out_unlock;
8671da177e4SLinus Torvalds 	}
86843cedbf0STrond Myklebust 	if (!xprt->ops->reserve_xprt(xprt, task))
8691da177e4SLinus Torvalds 		err = -EAGAIN;
8701da177e4SLinus Torvalds out_unlock:
8714a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
8721da177e4SLinus Torvalds 	return err;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
875e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
8765e5ce5beSTrond Myklebust {
877343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
8785e5ce5beSTrond Myklebust }
8795e5ce5beSTrond Myklebust 
8809903cd1cSChuck Lever /**
8819903cd1cSChuck Lever  * xprt_transmit - send an RPC request on a transport
8829903cd1cSChuck Lever  * @task: controlling RPC task
8839903cd1cSChuck Lever  *
8849903cd1cSChuck Lever  * We have to copy the iovec because sendmsg fiddles with its contents.
8859903cd1cSChuck Lever  */
8869903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
8891da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
890a246b010SChuck Lever 	int status;
8911da177e4SLinus Torvalds 
89246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
8931da177e4SLinus Torvalds 
894dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
89555ae1aabSRicardo Labiaga 		if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
89655ae1aabSRicardo Labiaga 			/*
89755ae1aabSRicardo Labiaga 			 * Add to the list only if we're expecting a reply
89855ae1aabSRicardo Labiaga 			 */
8994a0f8c04SChuck Lever 			spin_lock_bh(&xprt->transport_lock);
9001da177e4SLinus Torvalds 			/* Update the softirq receive buffer */
9011da177e4SLinus Torvalds 			memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
9021da177e4SLinus Torvalds 					sizeof(req->rq_private_buf));
9031da177e4SLinus Torvalds 			/* Add request to the receive list */
9041da177e4SLinus Torvalds 			list_add_tail(&req->rq_list, &xprt->recv);
9054a0f8c04SChuck Lever 			spin_unlock_bh(&xprt->transport_lock);
9061da177e4SLinus Torvalds 			xprt_reset_majortimeo(req);
9070f9dc2b1STrond Myklebust 			/* Turn off autodisconnect */
9080f9dc2b1STrond Myklebust 			del_singleshot_timer_sync(&xprt->timer);
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 	} else if (!req->rq_bytes_sent)
9111da177e4SLinus Torvalds 		return;
9121da177e4SLinus Torvalds 
9137c1d71cfSTrond Myklebust 	req->rq_connect_cookie = xprt->connect_cookie;
914ff839970SChuck Lever 	req->rq_xtime = ktime_get();
915a246b010SChuck Lever 	status = xprt->ops->send_request(task);
916c8485e4dSTrond Myklebust 	if (status != 0) {
917c8485e4dSTrond Myklebust 		task->tk_status = status;
918c8485e4dSTrond Myklebust 		return;
919c8485e4dSTrond Myklebust 	}
920c8485e4dSTrond Myklebust 
92146121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
922468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
923fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
924262ca07dSChuck Lever 
925fe3aca29SChuck Lever 	xprt->ops->set_retrans_timeout(task);
926262ca07dSChuck Lever 
927262ca07dSChuck Lever 	xprt->stat.sends++;
928262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
929262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
930262ca07dSChuck Lever 
931fe3aca29SChuck Lever 	/* Don't race with disconnect */
932fe3aca29SChuck Lever 	if (!xprt_connected(xprt))
933fe3aca29SChuck Lever 		task->tk_status = -ENOTCONN;
934dd2b63d0SRicardo Labiaga 	else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
93555ae1aabSRicardo Labiaga 		/*
93655ae1aabSRicardo Labiaga 		 * Sleep on the pending queue since
93755ae1aabSRicardo Labiaga 		 * we're expecting a reply.
93855ae1aabSRicardo Labiaga 		 */
9395d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_timer);
94055ae1aabSRicardo Labiaga 	}
941fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
945d9ba131dSTrond Myklebust {
946d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
947d9ba131dSTrond Myklebust 
948d9ba131dSTrond Myklebust 	if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
949d9ba131dSTrond Myklebust 		goto out;
950d9ba131dSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
951d9ba131dSTrond Myklebust 	if (req != NULL)
952d9ba131dSTrond Myklebust 		goto out;
953d9ba131dSTrond Myklebust 	atomic_dec(&xprt->num_reqs);
954d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
955d9ba131dSTrond Myklebust out:
956d9ba131dSTrond Myklebust 	return req;
957d9ba131dSTrond Myklebust }
958d9ba131dSTrond Myklebust 
959d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
960d9ba131dSTrond Myklebust {
961d9ba131dSTrond Myklebust 	if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
962d9ba131dSTrond Myklebust 		kfree(req);
963d9ba131dSTrond Myklebust 		return true;
964d9ba131dSTrond Myklebust 	}
965d9ba131dSTrond Myklebust 	return false;
966d9ba131dSTrond Myklebust }
967d9ba131dSTrond Myklebust 
968ee5ebe85STrond Myklebust static void xprt_alloc_slot(struct rpc_task *task)
9691da177e4SLinus Torvalds {
9701da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
971d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
974d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
975d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
976d9ba131dSTrond Myklebust 		goto out_init_req;
977d9ba131dSTrond Myklebust 	}
978d9ba131dSTrond Myklebust 	req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT);
979d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
980d9ba131dSTrond Myklebust 		goto out_init_req;
981d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
982d9ba131dSTrond Myklebust 	case -ENOMEM:
983d9ba131dSTrond Myklebust 		rpc_delay(task, HZ >> 2);
984d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
985d9ba131dSTrond Myklebust 				"failed! Retrying\n");
986d9ba131dSTrond Myklebust 		break;
987d9ba131dSTrond Myklebust 	case -EAGAIN:
988d9ba131dSTrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
989d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
990d9ba131dSTrond Myklebust 	}
991d9ba131dSTrond Myklebust 	task->tk_status = -EAGAIN;
992d9ba131dSTrond Myklebust 	return;
993d9ba131dSTrond Myklebust out_init_req:
994d9ba131dSTrond Myklebust 	task->tk_status = 0;
9951da177e4SLinus Torvalds 	task->tk_rqstp = req;
9961da177e4SLinus Torvalds 	xprt_request_init(task, xprt);
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
999ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1000ee5ebe85STrond Myklebust {
1001ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1002c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1003c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1004ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1005c25573b5STrond Myklebust 	}
1006ee5ebe85STrond Myklebust 	rpc_wake_up_next(&xprt->backlog);
1007ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1008ee5ebe85STrond Myklebust }
1009ee5ebe85STrond Myklebust 
101021de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
101121de0a95STrond Myklebust {
101221de0a95STrond Myklebust 	struct rpc_rqst *req;
101321de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
101421de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
101521de0a95STrond Myklebust 		list_del(&req->rq_list);
101621de0a95STrond Myklebust 		kfree(req);
101721de0a95STrond Myklebust 	}
101821de0a95STrond Myklebust }
101921de0a95STrond Myklebust 
1020d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1021d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1022d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1023bd1722d4SPavel Emelyanov {
1024bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
102521de0a95STrond Myklebust 	struct rpc_rqst *req;
102621de0a95STrond Myklebust 	int i;
1027bd1722d4SPavel Emelyanov 
1028bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1029bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1030bd1722d4SPavel Emelyanov 		goto out;
1031bd1722d4SPavel Emelyanov 
103221de0a95STrond Myklebust 	xprt_init(xprt, net);
103321de0a95STrond Myklebust 
103421de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
103521de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
103621de0a95STrond Myklebust 		if (!req)
103721de0a95STrond Myklebust 			break;
103821de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
103921de0a95STrond Myklebust 	}
104021de0a95STrond Myklebust 	if (i < num_prealloc)
1041bd1722d4SPavel Emelyanov 		goto out_free;
1042d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1043d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1044d9ba131dSTrond Myklebust 	else
104521de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1046d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1047d9ba131dSTrond Myklebust 	atomic_set(&xprt->num_reqs, num_prealloc);
1048bd1722d4SPavel Emelyanov 
1049bd1722d4SPavel Emelyanov 	return xprt;
1050bd1722d4SPavel Emelyanov 
1051bd1722d4SPavel Emelyanov out_free:
105221de0a95STrond Myklebust 	xprt_free(xprt);
1053bd1722d4SPavel Emelyanov out:
1054bd1722d4SPavel Emelyanov 	return NULL;
1055bd1722d4SPavel Emelyanov }
1056bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1057bd1722d4SPavel Emelyanov 
1058e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1059e204e621SPavel Emelyanov {
106037aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
106121de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1062e204e621SPavel Emelyanov 	kfree(xprt);
1063e204e621SPavel Emelyanov }
1064e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1065e204e621SPavel Emelyanov 
10669903cd1cSChuck Lever /**
10679903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
10689903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
10699903cd1cSChuck Lever  *
10709903cd1cSChuck Lever  * If no more slots are available, place the task on the transport's
10719903cd1cSChuck Lever  * backlog queue.
10729903cd1cSChuck Lever  */
10739903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
10761da177e4SLinus Torvalds 
107743cedbf0STrond Myklebust 	task->tk_status = 0;
107843cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
107943cedbf0STrond Myklebust 		return;
108043cedbf0STrond Myklebust 
108143cedbf0STrond Myklebust 	/* Note: grabbing the xprt_lock_write() here is not strictly needed,
108243cedbf0STrond Myklebust 	 * but ensures that we throttle new slot allocation if the transport
108343cedbf0STrond Myklebust 	 * is congested (e.g. if reconnecting or if we're out of socket
108443cedbf0STrond Myklebust 	 * write buffer space).
108543cedbf0STrond Myklebust 	 */
108643cedbf0STrond Myklebust 	task->tk_timeout = 0;
108743cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
108843cedbf0STrond Myklebust 	if (!xprt_lock_write(xprt, task))
108943cedbf0STrond Myklebust 		return;
109043cedbf0STrond Myklebust 
10915dc07727SChuck Lever 	spin_lock(&xprt->reserve_lock);
1092ee5ebe85STrond Myklebust 	xprt_alloc_slot(task);
10935dc07727SChuck Lever 	spin_unlock(&xprt->reserve_lock);
109443cedbf0STrond Myklebust 	xprt_release_write(xprt, task);
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
1097d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
10981da177e4SLinus Torvalds {
10990eae88f3SEric Dumazet 	return (__force __be32)xprt->xid++;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt)
11031da177e4SLinus Torvalds {
1104bf3fcf89SChuck Lever 	xprt->xid = net_random();
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11079903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
11101da177e4SLinus Torvalds 
1111d9ba131dSTrond Myklebust 	INIT_LIST_HEAD(&req->rq_list);
1112ba7392bbSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
11131da177e4SLinus Torvalds 	req->rq_task	= task;
11141da177e4SLinus Torvalds 	req->rq_xprt    = xprt;
111502107148SChuck Lever 	req->rq_buffer  = NULL;
11161da177e4SLinus Torvalds 	req->rq_xid     = xprt_alloc_xid(xprt);
1117ead5e1c2SJ. Bruce Fields 	req->rq_release_snd_buf = NULL;
1118da45828eSTrond Myklebust 	xprt_reset_majortimeo(req);
111946121cf7SChuck Lever 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
11201da177e4SLinus Torvalds 			req, ntohl(req->rq_xid));
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds 
11239903cd1cSChuck Lever /**
11249903cd1cSChuck Lever  * xprt_release - release an RPC request slot
11259903cd1cSChuck Lever  * @task: task which is finished with the slot
11269903cd1cSChuck Lever  *
11271da177e4SLinus Torvalds  */
11289903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
11291da177e4SLinus Torvalds {
113055ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
11311da177e4SLinus Torvalds 	struct rpc_rqst	*req;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	if (!(req = task->tk_rqstp))
11341da177e4SLinus Torvalds 		return;
113555ae1aabSRicardo Labiaga 
113655ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
113711c556b3SChuck Lever 	rpc_count_iostats(task);
11384a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
113949e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1140a58dd398SChuck Lever 	if (xprt->ops->release_request)
1141a58dd398SChuck Lever 		xprt->ops->release_request(task);
11421da177e4SLinus Torvalds 	if (!list_empty(&req->rq_list))
11431da177e4SLinus Torvalds 		list_del(&req->rq_list);
11441da177e4SLinus Torvalds 	xprt->last_used = jiffies;
11454cfc7e60SRahul Iyer 	if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
1146a246b010SChuck Lever 		mod_timer(&xprt->timer,
114703bf4b70SChuck Lever 				xprt->last_used + xprt->idle_timeout);
11484a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1149ee5ebe85STrond Myklebust 	if (req->rq_buffer)
1150c5a4dd8bSChuck Lever 		xprt->ops->buf_free(req->rq_buffer);
1151a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1152a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
11531da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1154ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1155ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
115655ae1aabSRicardo Labiaga 
115746121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1158ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1159ee5ebe85STrond Myklebust 		xprt_free_slot(xprt, req);
1160ee5ebe85STrond Myklebust 	else
1161c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
116421de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1165c2866763SChuck Lever {
116621de0a95STrond Myklebust 	atomic_set(&xprt->count, 1);
1167c2866763SChuck Lever 
1168c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1169c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
1170c2866763SChuck Lever 
1171c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
1172c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->recv);
11739e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1174f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1175f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
11769e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1177f9acac1aSRicardo Labiaga 
1178c2866763SChuck Lever 	xprt->last_used = jiffies;
1179c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1180a509050bSChuck Lever 	xprt->bind_index = 0;
1181c2866763SChuck Lever 
1182c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1183c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
118434006ceeSTrond Myklebust 	rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1185c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1186c2866763SChuck Lever 
1187c2866763SChuck Lever 	xprt_init_xid(xprt);
1188c2866763SChuck Lever 
118921de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
11908d9266ffSTrond Myklebust }
11918d9266ffSTrond Myklebust 
11928d9266ffSTrond Myklebust /**
11938d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
11948d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
11958d9266ffSTrond Myklebust  *
11968d9266ffSTrond Myklebust  */
11978d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
11988d9266ffSTrond Myklebust {
11998d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
12008d9266ffSTrond Myklebust 	struct xprt_class *t;
12018d9266ffSTrond Myklebust 
12028d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
12038d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
12048d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
12058d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
12068d9266ffSTrond Myklebust 			goto found;
12078d9266ffSTrond Myklebust 		}
12088d9266ffSTrond Myklebust 	}
12098d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
12108d9266ffSTrond Myklebust 	printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
12118d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
12128d9266ffSTrond Myklebust 
12138d9266ffSTrond Myklebust found:
12148d9266ffSTrond Myklebust 	xprt = t->setup(args);
12158d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
12168d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
12178d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
121821de0a95STrond Myklebust 		goto out;
12198d9266ffSTrond Myklebust 	}
122021de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
122121de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
122221de0a95STrond Myklebust 		setup_timer(&xprt->timer, xprt_init_autodisconnect,
122321de0a95STrond Myklebust 			    (unsigned long)xprt);
122421de0a95STrond Myklebust 	else
122521de0a95STrond Myklebust 		init_timer(&xprt->timer);
1226c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1227c2866763SChuck Lever 			xprt->max_reqs);
122821de0a95STrond Myklebust out:
1229c2866763SChuck Lever 	return xprt;
1230c2866763SChuck Lever }
1231c2866763SChuck Lever 
12329903cd1cSChuck Lever /**
12339903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1234a8de240aSTrond Myklebust  * @xprt: transport to destroy
12359903cd1cSChuck Lever  *
12361da177e4SLinus Torvalds  */
1237a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
12381da177e4SLinus Torvalds {
12391da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
12400065db32STrond Myklebust 	xprt->shutdown = 1;
12410065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1242c8541ecdSChuck Lever 
1243f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1244f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1245f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1246f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1247c3ae62aeSJ. Bruce Fields 	cancel_work_sync(&xprt->task_cleanup);
1248c8541ecdSChuck Lever 	/*
1249c8541ecdSChuck Lever 	 * Tear down transport state and free the rpc_xprt
1250c8541ecdSChuck Lever 	 */
1251a246b010SChuck Lever 	xprt->ops->destroy(xprt);
12526b6ca86bSTrond Myklebust }
12531da177e4SLinus Torvalds 
12546b6ca86bSTrond Myklebust /**
12556b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
12566b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
12576b6ca86bSTrond Myklebust  *
12586b6ca86bSTrond Myklebust  */
12596b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
12606b6ca86bSTrond Myklebust {
1261a8de240aSTrond Myklebust 	if (atomic_dec_and_test(&xprt->count))
1262a8de240aSTrond Myklebust 		xprt_destroy(xprt);
12636b6ca86bSTrond Myklebust }
12646b6ca86bSTrond Myklebust 
12656b6ca86bSTrond Myklebust /**
12666b6ca86bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
12676b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
12686b6ca86bSTrond Myklebust  *
12696b6ca86bSTrond Myklebust  */
12706b6ca86bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
12716b6ca86bSTrond Myklebust {
1272a8de240aSTrond Myklebust 	if (atomic_inc_not_zero(&xprt->count))
12736b6ca86bSTrond Myklebust 		return xprt;
1274a8de240aSTrond Myklebust 	return NULL;
12751da177e4SLinus Torvalds }
1276