xref: /openbmc/linux/net/sunrpc/xprt.c (revision 2c53040f)
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 *);
694e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
701da177e4SLinus Torvalds 
715ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7281c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7381c098afS\"Talpey, Thomas\ 
74555ee3afSChuck Lever /*
75555ee3afSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
76555ee3afSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
77555ee3afSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
78555ee3afSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
79555ee3afSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
80555ee3afSChuck Lever  *
81555ee3afSChuck Lever  *	-	a reply is received and
82555ee3afSChuck Lever  *	-	a full number of requests are outstanding and
83555ee3afSChuck Lever  *	-	the congestion window hasn't been updated recently.
84555ee3afSChuck Lever  */
85555ee3afSChuck Lever #define RPC_CWNDSHIFT		(8U)
86555ee3afSChuck Lever #define RPC_CWNDSCALE		(1U << RPC_CWNDSHIFT)
87555ee3afSChuck Lever #define RPC_INITCWND		RPC_CWNDSCALE
88555ee3afSChuck Lever #define RPC_MAXCWND(xprt)	((xprt)->max_reqs << RPC_CWNDSHIFT)
89555ee3afSChuck Lever 
90555ee3afSChuck Lever #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
911da177e4SLinus Torvalds 
9212a80469SChuck Lever /**
9381c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
9481c098afS\"Talpey, Thomas\  * @transport: transport to register
9581c098afS\"Talpey, Thomas\  *
9681c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
9781c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
9881c098afS\"Talpey, Thomas\  *
9981c098afS\"Talpey, Thomas\  * Returns:
10081c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
10181c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
10281c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
10381c098afS\"Talpey, Thomas\  */
10481c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
10581c098afS\"Talpey, Thomas\ {
10681c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
10781c098afS\"Talpey, Thomas\ 	int result;
10881c098afS\"Talpey, Thomas\ 
10981c098afS\"Talpey, Thomas\ 	result = -EEXIST;
11081c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
11181c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
11281c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
1134fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
11481c098afS\"Talpey, Thomas\ 			goto out;
11581c098afS\"Talpey, Thomas\ 	}
11681c098afS\"Talpey, Thomas\ 
11781c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
11881c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
11981c098afS\"Talpey, Thomas\ 	       transport->name);
12081c098afS\"Talpey, Thomas\ 	result = 0;
12181c098afS\"Talpey, Thomas\ 
12281c098afS\"Talpey, Thomas\ out:
12381c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
12481c098afS\"Talpey, Thomas\ 	return result;
12581c098afS\"Talpey, Thomas\ }
12681c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
12781c098afS\"Talpey, Thomas\ 
12881c098afS\"Talpey, Thomas\ /**
12981c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
13065b6e42cSRandy Dunlap  * @transport: transport to unregister
13181c098afS\"Talpey, Thomas\  *
13281c098afS\"Talpey, Thomas\  * Returns:
13381c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
13481c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
13581c098afS\"Talpey, Thomas\  */
13681c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
13781c098afS\"Talpey, Thomas\ {
13881c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
13981c098afS\"Talpey, Thomas\ 	int result;
14081c098afS\"Talpey, Thomas\ 
14181c098afS\"Talpey, Thomas\ 	result = 0;
14281c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
14381c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
14481c098afS\"Talpey, Thomas\ 		if (t == transport) {
14581c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
14681c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
14781c098afS\"Talpey, Thomas\ 				transport->name);
14881c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
14981c098afS\"Talpey, Thomas\ 			goto out;
15081c098afS\"Talpey, Thomas\ 		}
15181c098afS\"Talpey, Thomas\ 	}
15281c098afS\"Talpey, Thomas\ 	result = -ENOENT;
15381c098afS\"Talpey, Thomas\ 
15481c098afS\"Talpey, Thomas\ out:
15581c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
15681c098afS\"Talpey, Thomas\ 	return result;
15781c098afS\"Talpey, Thomas\ }
15881c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
15981c098afS\"Talpey, Thomas\ 
16081c098afS\"Talpey, Thomas\ /**
161441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
162441e3e24STom Talpey  * @transport_name: transport to load
163441e3e24STom Talpey  *
164441e3e24STom Talpey  * Returns:
165441e3e24STom Talpey  * 0:		transport successfully loaded
166441e3e24STom Talpey  * -ENOENT:	transport module not available
167441e3e24STom Talpey  */
168441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
169441e3e24STom Talpey {
170441e3e24STom Talpey 	struct xprt_class *t;
171441e3e24STom Talpey 	int result;
172441e3e24STom Talpey 
173441e3e24STom Talpey 	result = 0;
174441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
175441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
176441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
177441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
178441e3e24STom Talpey 			goto out;
179441e3e24STom Talpey 		}
180441e3e24STom Talpey 	}
181441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
182ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
183441e3e24STom Talpey out:
184441e3e24STom Talpey 	return result;
185441e3e24STom Talpey }
186441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
187441e3e24STom Talpey 
188441e3e24STom Talpey /**
18912a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
19012a80469SChuck Lever  * @task: task that is requesting access to the transport
191177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
19212a80469SChuck Lever  *
19312a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
19412a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
19512a80469SChuck Lever  * is provided.
1961da177e4SLinus Torvalds  */
19743cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1981da177e4SLinus Torvalds {
19912a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
20034006ceeSTrond Myklebust 	int priority;
20112a80469SChuck Lever 
20212a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
20312a80469SChuck Lever 		if (task == xprt->snd_task)
20412a80469SChuck Lever 			return 1;
20512a80469SChuck Lever 		goto out_sleep;
20612a80469SChuck Lever 	}
20712a80469SChuck Lever 	xprt->snd_task = task;
20843cedbf0STrond Myklebust 	if (req != NULL) {
20912a80469SChuck Lever 		req->rq_bytes_sent = 0;
21012a80469SChuck Lever 		req->rq_ntrans++;
21143cedbf0STrond Myklebust 	}
2124d4a76f3Sj223yang@asset.uwaterloo.ca 
21312a80469SChuck Lever 	return 1;
21412a80469SChuck Lever 
21512a80469SChuck Lever out_sleep:
21646121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
21712a80469SChuck Lever 			task->tk_pid, xprt);
21812a80469SChuck Lever 	task->tk_timeout = 0;
21912a80469SChuck Lever 	task->tk_status = -EAGAIN;
22034006ceeSTrond Myklebust 	if (req == NULL)
22134006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
22234006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
22334006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
22412a80469SChuck Lever 	else
22534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
22634006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
22712a80469SChuck Lever 	return 0;
22812a80469SChuck Lever }
22912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
23012a80469SChuck Lever 
231632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
232632e3bdcSTrond Myklebust {
233632e3bdcSTrond Myklebust 	xprt->snd_task = NULL;
234632e3bdcSTrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
235632e3bdcSTrond Myklebust 		smp_mb__before_clear_bit();
236632e3bdcSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
237632e3bdcSTrond Myklebust 		smp_mb__after_clear_bit();
238632e3bdcSTrond Myklebust 	} else
239c1384c9cSTrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
240632e3bdcSTrond Myklebust }
241632e3bdcSTrond Myklebust 
24212a80469SChuck Lever /*
24312a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
24412a80469SChuck Lever  * @task: task that is requesting access to the transport
24512a80469SChuck Lever  *
24612a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
24712a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
24812a80469SChuck Lever  * woken up and given access to the transport.
24912a80469SChuck Lever  */
25043cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
25112a80469SChuck Lever {
2521da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
25334006ceeSTrond Myklebust 	int priority;
2541da177e4SLinus Torvalds 
2552226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2561da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2571da177e4SLinus Torvalds 			return 1;
2581da177e4SLinus Torvalds 		goto out_sleep;
2591da177e4SLinus Torvalds 	}
26043cedbf0STrond Myklebust 	if (req == NULL) {
26143cedbf0STrond Myklebust 		xprt->snd_task = task;
26243cedbf0STrond Myklebust 		return 1;
26343cedbf0STrond Myklebust 	}
26412a80469SChuck Lever 	if (__xprt_get_cong(xprt, task)) {
2651da177e4SLinus Torvalds 		xprt->snd_task = task;
2661da177e4SLinus Torvalds 		req->rq_bytes_sent = 0;
2671da177e4SLinus Torvalds 		req->rq_ntrans++;
2681da177e4SLinus Torvalds 		return 1;
2691da177e4SLinus Torvalds 	}
270632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2711da177e4SLinus Torvalds out_sleep:
27246121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2731da177e4SLinus Torvalds 	task->tk_timeout = 0;
2741da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
27534006ceeSTrond Myklebust 	if (req == NULL)
27634006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
27734006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
27834006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
2791da177e4SLinus Torvalds 	else
28034006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
28134006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
2821da177e4SLinus Torvalds 	return 0;
2831da177e4SLinus Torvalds }
28412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2851da177e4SLinus Torvalds 
28612a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	int retval;
2891da177e4SLinus Torvalds 
2904a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
29143cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2924a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2931da177e4SLinus Torvalds 	return retval;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
296961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2971da177e4SLinus Torvalds {
298961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
29949e9a890SChuck Lever 	struct rpc_rqst *req;
30049e9a890SChuck Lever 
30149e9a890SChuck Lever 	req = task->tk_rqstp;
30249e9a890SChuck Lever 	xprt->snd_task = task;
30349e9a890SChuck Lever 	if (req) {
30449e9a890SChuck Lever 		req->rq_bytes_sent = 0;
30549e9a890SChuck Lever 		req->rq_ntrans++;
30649e9a890SChuck Lever 	}
307961a828dSTrond Myklebust 	return true;
308961a828dSTrond Myklebust }
309961a828dSTrond Myklebust 
310961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
311961a828dSTrond Myklebust {
312961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
31349e9a890SChuck Lever 		return;
31449e9a890SChuck Lever 
315961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt))
316961a828dSTrond Myklebust 		return;
317632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
31849e9a890SChuck Lever }
31949e9a890SChuck Lever 
320961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
32149e9a890SChuck Lever {
322961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
32343cedbf0STrond Myklebust 	struct rpc_rqst *req;
3241da177e4SLinus Torvalds 
32543cedbf0STrond Myklebust 	req = task->tk_rqstp;
32643cedbf0STrond Myklebust 	if (req == NULL) {
3271da177e4SLinus Torvalds 		xprt->snd_task = task;
328961a828dSTrond Myklebust 		return true;
32943cedbf0STrond Myklebust 	}
33043cedbf0STrond Myklebust 	if (__xprt_get_cong(xprt, task)) {
33143cedbf0STrond Myklebust 		xprt->snd_task = task;
3321da177e4SLinus Torvalds 		req->rq_bytes_sent = 0;
3331da177e4SLinus Torvalds 		req->rq_ntrans++;
334961a828dSTrond Myklebust 		return true;
3351da177e4SLinus Torvalds 	}
336961a828dSTrond Myklebust 	return false;
337961a828dSTrond Myklebust }
338961a828dSTrond Myklebust 
339961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
340961a828dSTrond Myklebust {
341961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
342961a828dSTrond Myklebust 		return;
343961a828dSTrond Myklebust 	if (RPCXPRT_CONGESTED(xprt))
344961a828dSTrond Myklebust 		goto out_unlock;
345961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt))
346961a828dSTrond Myklebust 		return;
3471da177e4SLinus Torvalds out_unlock:
348632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
35149e9a890SChuck Lever /**
35249e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
35349e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
35449e9a890SChuck Lever  * @task: task that is releasing access to the transport
35549e9a890SChuck Lever  *
35649e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3571da177e4SLinus Torvalds  */
35849e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
361632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3621da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds }
36512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3661da177e4SLinus Torvalds 
36749e9a890SChuck Lever /**
36849e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
36949e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
37049e9a890SChuck Lever  * @task: task that is releasing access to the transport
37149e9a890SChuck Lever  *
37249e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
37349e9a890SChuck Lever  * transport if the transport's congestion window allows it.
37449e9a890SChuck Lever  */
37549e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
37649e9a890SChuck Lever {
37749e9a890SChuck Lever 	if (xprt->snd_task == task) {
378632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
37949e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
38049e9a890SChuck Lever 	}
38149e9a890SChuck Lever }
38212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
38349e9a890SChuck Lever 
38449e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3851da177e4SLinus Torvalds {
3864a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
38749e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3884a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3931da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds static int
3961da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	if (req->rq_cong)
4011da177e4SLinus Torvalds 		return 1;
40246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
4031da177e4SLinus Torvalds 			task->tk_pid, xprt->cong, xprt->cwnd);
4041da177e4SLinus Torvalds 	if (RPCXPRT_CONGESTED(xprt))
4051da177e4SLinus Torvalds 		return 0;
4061da177e4SLinus Torvalds 	req->rq_cong = 1;
4071da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
4081da177e4SLinus Torvalds 	return 1;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4131da177e4SLinus Torvalds  * that has been sleeping due to congestion
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static void
4161da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	if (!req->rq_cong)
4191da177e4SLinus Torvalds 		return;
4201da177e4SLinus Torvalds 	req->rq_cong = 0;
4211da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
42249e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
42546c0ee8bSChuck Lever /**
426a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
427a58dd398SChuck Lever  * @task: RPC request that recently completed
428a58dd398SChuck Lever  *
429a58dd398SChuck Lever  * Useful for transports that require congestion control.
430a58dd398SChuck Lever  */
431a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
432a58dd398SChuck Lever {
433a58dd398SChuck Lever 	__xprt_put_cong(task->tk_xprt, task->tk_rqstp);
434a58dd398SChuck Lever }
43512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
436a58dd398SChuck Lever 
437a58dd398SChuck Lever /**
43846c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
43946c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
44046c0ee8bSChuck Lever  * @result: result code of completed RPC request
44146c0ee8bSChuck Lever  *
4421da177e4SLinus Torvalds  * We use a time-smoothed congestion estimator to avoid heavy oscillation.
4431da177e4SLinus Torvalds  */
44446c0ee8bSChuck Lever void xprt_adjust_cwnd(struct rpc_task *task, int result)
4451da177e4SLinus Torvalds {
44646c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
44746c0ee8bSChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
44846c0ee8bSChuck Lever 	unsigned long cwnd = xprt->cwnd;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (result >= 0 && cwnd <= xprt->cong) {
4511da177e4SLinus Torvalds 		/* The (cwnd >> 1) term makes sure
4521da177e4SLinus Torvalds 		 * the result gets rounded properly. */
4531da177e4SLinus Torvalds 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
4541da177e4SLinus Torvalds 		if (cwnd > RPC_MAXCWND(xprt))
4551da177e4SLinus Torvalds 			cwnd = RPC_MAXCWND(xprt);
45649e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
4571da177e4SLinus Torvalds 	} else if (result == -ETIMEDOUT) {
4581da177e4SLinus Torvalds 		cwnd >>= 1;
4591da177e4SLinus Torvalds 		if (cwnd < RPC_CWNDSCALE)
4601da177e4SLinus Torvalds 			cwnd = RPC_CWNDSCALE;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
4631da177e4SLinus Torvalds 			xprt->cong, xprt->cwnd, cwnd);
4641da177e4SLinus Torvalds 	xprt->cwnd = cwnd;
46546c0ee8bSChuck Lever 	__xprt_put_cong(xprt, req);
4661da177e4SLinus Torvalds }
46712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
4681da177e4SLinus Torvalds 
46944fbac22SChuck Lever /**
47044fbac22SChuck Lever  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
47144fbac22SChuck Lever  * @xprt: transport with waiting tasks
47244fbac22SChuck Lever  * @status: result code to plant in each task before waking it
47344fbac22SChuck Lever  *
47444fbac22SChuck Lever  */
47544fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
47644fbac22SChuck Lever {
47744fbac22SChuck Lever 	if (status < 0)
47844fbac22SChuck Lever 		rpc_wake_up_status(&xprt->pending, status);
47944fbac22SChuck Lever 	else
48044fbac22SChuck Lever 		rpc_wake_up(&xprt->pending);
48144fbac22SChuck Lever }
48212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
48344fbac22SChuck Lever 
484c7b2cae8SChuck Lever /**
485c7b2cae8SChuck Lever  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
486c7b2cae8SChuck Lever  * @task: task to be put to sleep
4870b80ae42SRandy Dunlap  * @action: function pointer to be executed after wait
488c7b2cae8SChuck Lever  */
489b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
490c7b2cae8SChuck Lever {
491c7b2cae8SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
492c7b2cae8SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
493c7b2cae8SChuck Lever 
494c7b2cae8SChuck Lever 	task->tk_timeout = req->rq_timeout;
495b6ddf64fSTrond Myklebust 	rpc_sleep_on(&xprt->pending, task, action);
496c7b2cae8SChuck Lever }
49712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
498c7b2cae8SChuck Lever 
499c7b2cae8SChuck Lever /**
500c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
501c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
502c7b2cae8SChuck Lever  *
503c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
504c7b2cae8SChuck Lever  */
505c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt)
506c7b2cae8SChuck Lever {
507c7b2cae8SChuck Lever 	if (unlikely(xprt->shutdown))
508c7b2cae8SChuck Lever 		return;
509c7b2cae8SChuck Lever 
510c7b2cae8SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
511c7b2cae8SChuck Lever 	if (xprt->snd_task) {
51246121cf7SChuck Lever 		dprintk("RPC:       write space: waking waiting task on "
51346121cf7SChuck Lever 				"xprt %p\n", xprt);
514fda13939STrond Myklebust 		rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
515c7b2cae8SChuck Lever 	}
516c7b2cae8SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
517c7b2cae8SChuck Lever }
51812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space);
519c7b2cae8SChuck Lever 
520fe3aca29SChuck Lever /**
521fe3aca29SChuck Lever  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
522fe3aca29SChuck Lever  * @task: task whose timeout is to be set
523fe3aca29SChuck Lever  *
524fe3aca29SChuck Lever  * Set a request's retransmit timeout based on the transport's
525fe3aca29SChuck Lever  * default timeout parameters.  Used by transports that don't adjust
526fe3aca29SChuck Lever  * the retransmit timeout based on round-trip time estimation.
527fe3aca29SChuck Lever  */
528fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task)
529fe3aca29SChuck Lever {
530fe3aca29SChuck Lever 	task->tk_timeout = task->tk_rqstp->rq_timeout;
531fe3aca29SChuck Lever }
53212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
533fe3aca29SChuck Lever 
5342c53040fSBen Hutchings /**
535fe3aca29SChuck Lever  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
536fe3aca29SChuck Lever  * @task: task whose timeout is to be set
537fe3aca29SChuck Lever  *
538fe3aca29SChuck Lever  * Set a request's retransmit timeout using the RTT estimator.
539fe3aca29SChuck Lever  */
540fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
541fe3aca29SChuck Lever {
542fe3aca29SChuck Lever 	int timer = task->tk_msg.rpc_proc->p_timer;
543ba7392bbSTrond Myklebust 	struct rpc_clnt *clnt = task->tk_client;
544ba7392bbSTrond Myklebust 	struct rpc_rtt *rtt = clnt->cl_rtt;
545fe3aca29SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
546ba7392bbSTrond Myklebust 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
547fe3aca29SChuck Lever 
548fe3aca29SChuck Lever 	task->tk_timeout = rpc_calc_rto(rtt, timer);
549fe3aca29SChuck Lever 	task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
550fe3aca29SChuck Lever 	if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
551fe3aca29SChuck Lever 		task->tk_timeout = max_timeout;
552fe3aca29SChuck Lever }
55312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
554fe3aca29SChuck Lever 
5551da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req)
5561da177e4SLinus Torvalds {
557ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	req->rq_majortimeo = req->rq_timeout;
5601da177e4SLinus Torvalds 	if (to->to_exponential)
5611da177e4SLinus Torvalds 		req->rq_majortimeo <<= to->to_retries;
5621da177e4SLinus Torvalds 	else
5631da177e4SLinus Torvalds 		req->rq_majortimeo += to->to_increment * to->to_retries;
5641da177e4SLinus Torvalds 	if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
5651da177e4SLinus Torvalds 		req->rq_majortimeo = to->to_maxval;
5661da177e4SLinus Torvalds 	req->rq_majortimeo += jiffies;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5699903cd1cSChuck Lever /**
5709903cd1cSChuck Lever  * xprt_adjust_timeout - adjust timeout values for next retransmit
5719903cd1cSChuck Lever  * @req: RPC request containing parameters to use for the adjustment
5729903cd1cSChuck Lever  *
5731da177e4SLinus Torvalds  */
5741da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
577ba7392bbSTrond Myklebust 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
5781da177e4SLinus Torvalds 	int status = 0;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	if (time_before(jiffies, req->rq_majortimeo)) {
5811da177e4SLinus Torvalds 		if (to->to_exponential)
5821da177e4SLinus Torvalds 			req->rq_timeout <<= 1;
5831da177e4SLinus Torvalds 		else
5841da177e4SLinus Torvalds 			req->rq_timeout += to->to_increment;
5851da177e4SLinus Torvalds 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
5861da177e4SLinus Torvalds 			req->rq_timeout = to->to_maxval;
5871da177e4SLinus Torvalds 		req->rq_retries++;
5881da177e4SLinus Torvalds 	} else {
5891da177e4SLinus Torvalds 		req->rq_timeout = to->to_initval;
5901da177e4SLinus Torvalds 		req->rq_retries = 0;
5911da177e4SLinus Torvalds 		xprt_reset_majortimeo(req);
5921da177e4SLinus Torvalds 		/* Reset the RTT counters == "slow start" */
5934a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
5941da177e4SLinus Torvalds 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
5954a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
5961da177e4SLinus Torvalds 		status = -ETIMEDOUT;
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (req->rq_timeout == 0) {
6001da177e4SLinus Torvalds 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6011da177e4SLinus Torvalds 		req->rq_timeout = 5 * HZ;
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 	return status;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
60665f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work)
6071da177e4SLinus Torvalds {
60865f27f38SDavid Howells 	struct rpc_xprt *xprt =
60965f27f38SDavid Howells 		container_of(work, struct rpc_xprt, task_cleanup);
6101da177e4SLinus Torvalds 
611a246b010SChuck Lever 	xprt->ops->close(xprt);
61266af1e55STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
6131da177e4SLinus Torvalds 	xprt_release_write(xprt, NULL);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6169903cd1cSChuck Lever /**
61762da3b24STrond Myklebust  * xprt_disconnect_done - mark a transport as disconnected
6189903cd1cSChuck Lever  * @xprt: transport to flag for disconnect
6199903cd1cSChuck Lever  *
6201da177e4SLinus Torvalds  */
62162da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	dprintk("RPC:       disconnected transport %p\n", xprt);
6244a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
6251da177e4SLinus Torvalds 	xprt_clear_connected(xprt);
6262a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6274a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
6281da177e4SLinus Torvalds }
62962da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done);
6301da177e4SLinus Torvalds 
63166af1e55STrond Myklebust /**
63266af1e55STrond Myklebust  * xprt_force_disconnect - force a transport to disconnect
63366af1e55STrond Myklebust  * @xprt: transport to disconnect
63466af1e55STrond Myklebust  *
63566af1e55STrond Myklebust  */
63666af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt)
63766af1e55STrond Myklebust {
63866af1e55STrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
63966af1e55STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
64066af1e55STrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
64166af1e55STrond Myklebust 	/* Try to schedule an autoclose RPC call */
64266af1e55STrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
64366af1e55STrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
6442a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
64566af1e55STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
64666af1e55STrond Myklebust }
64766af1e55STrond Myklebust 
6487c1d71cfSTrond Myklebust /**
6497c1d71cfSTrond Myklebust  * xprt_conditional_disconnect - force a transport to disconnect
6507c1d71cfSTrond Myklebust  * @xprt: transport to disconnect
6517c1d71cfSTrond Myklebust  * @cookie: 'connection cookie'
6527c1d71cfSTrond Myklebust  *
6537c1d71cfSTrond Myklebust  * This attempts to break the connection if and only if 'cookie' matches
6547c1d71cfSTrond Myklebust  * the current transport 'connection cookie'. It ensures that we don't
6557c1d71cfSTrond Myklebust  * try to break the connection more than once when we need to retransmit
6567c1d71cfSTrond Myklebust  * a batch of RPC requests.
6577c1d71cfSTrond Myklebust  *
6587c1d71cfSTrond Myklebust  */
6597c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
6607c1d71cfSTrond Myklebust {
6617c1d71cfSTrond Myklebust 	/* Don't race with the test_bit() in xprt_clear_locked() */
6627c1d71cfSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
6637c1d71cfSTrond Myklebust 	if (cookie != xprt->connect_cookie)
6647c1d71cfSTrond Myklebust 		goto out;
6657c1d71cfSTrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
6667c1d71cfSTrond Myklebust 		goto out;
6677c1d71cfSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
6687c1d71cfSTrond Myklebust 	/* Try to schedule an autoclose RPC call */
6697c1d71cfSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
6707c1d71cfSTrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
6712a491991STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
6727c1d71cfSTrond Myklebust out:
6737c1d71cfSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
6747c1d71cfSTrond Myklebust }
6757c1d71cfSTrond Myklebust 
6761da177e4SLinus Torvalds static void
6771da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct rpc_xprt *xprt = (struct rpc_xprt *)data;
6801da177e4SLinus Torvalds 
6814a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
6821da177e4SLinus Torvalds 	if (!list_empty(&xprt->recv) || xprt->shutdown)
6831da177e4SLinus Torvalds 		goto out_abort;
6842226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
6851da177e4SLinus Torvalds 		goto out_abort;
6864a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
687f75e6745STrond Myklebust 	set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
688c1384c9cSTrond Myklebust 	queue_work(rpciod_workqueue, &xprt->task_cleanup);
6891da177e4SLinus Torvalds 	return;
6901da177e4SLinus Torvalds out_abort:
6914a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6949903cd1cSChuck Lever /**
6959903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
6969903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
6971da177e4SLinus Torvalds  *
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
7021da177e4SLinus Torvalds 
70346121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7041da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7051da177e4SLinus Torvalds 
706ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
70701d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7081da177e4SLinus Torvalds 		return;
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7111da177e4SLinus Torvalds 		return;
712feb8ca37STrond Myklebust 
713feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
714feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
715feb8ca37STrond Myklebust 
7161da177e4SLinus Torvalds 	if (xprt_connected(xprt))
717a246b010SChuck Lever 		xprt_release_write(xprt, task);
718a246b010SChuck Lever 	else {
7191da177e4SLinus Torvalds 		task->tk_rqstp->rq_bytes_sent = 0;
720a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
7215d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
7220b9e7943STrond Myklebust 
7230b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
7240b9e7943STrond Myklebust 			return;
7250b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
7260b9e7943STrond Myklebust 			return;
727262ca07dSChuck Lever 		xprt->stat.connect_start = jiffies;
728a246b010SChuck Lever 		xprt->ops->connect(task);
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7329903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
7351da177e4SLinus Torvalds 
736cd983ef8SChuck Lever 	if (task->tk_status == 0) {
737262ca07dSChuck Lever 		xprt->stat.connect_count++;
738262ca07dSChuck Lever 		xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
73946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connection established\n",
7401da177e4SLinus Torvalds 				task->tk_pid);
7411da177e4SLinus Torvalds 		return;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	switch (task->tk_status) {
7452a491991STrond Myklebust 	case -EAGAIN:
7462a491991STrond Myklebust 		dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
74723475d66SChuck Lever 		break;
7481da177e4SLinus Torvalds 	case -ETIMEDOUT:
74946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
75046121cf7SChuck Lever 				"out\n", task->tk_pid);
7511da177e4SLinus Torvalds 		break;
7521da177e4SLinus Torvalds 	default:
75346121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
75446121cf7SChuck Lever 				"server %s\n", task->tk_pid, -task->tk_status,
7554e0038b6STrond Myklebust 				xprt->servername);
7561da177e4SLinus Torvalds 		xprt_release_write(xprt, task);
75723475d66SChuck Lever 		task->tk_status = -EIO;
75823475d66SChuck Lever 	}
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7619903cd1cSChuck Lever /**
7629903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
7639903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
7649903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
7659903cd1cSChuck Lever  *
7661da177e4SLinus Torvalds  */
767d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
7681da177e4SLinus Torvalds {
7698f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
7701da177e4SLinus Torvalds 
7718f3a6de3SPavel Emelyanov 	list_for_each_entry(entry, &xprt->recv, rq_list)
772262ca07dSChuck Lever 		if (entry->rq_xid == xid)
773262ca07dSChuck Lever 			return entry;
77446121cf7SChuck Lever 
77546121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
77646121cf7SChuck Lever 			ntohl(xid));
777262ca07dSChuck Lever 	xprt->stat.bad_xids++;
778262ca07dSChuck Lever 	return NULL;
7791da177e4SLinus Torvalds }
78012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
7811da177e4SLinus Torvalds 
782bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task)
7831da177e4SLinus Torvalds {
7841570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
7851570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
78695c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
787d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
7881570c1e4SChuck Lever 
7891da177e4SLinus Torvalds 	if (timer) {
7901da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
791ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
7921570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961570c1e4SChuck Lever /**
7971570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
7981570c1e4SChuck Lever  * @task: RPC request that recently completed
7991570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
8001570c1e4SChuck Lever  *
8011570c1e4SChuck Lever  * Caller holds transport lock.
8021570c1e4SChuck Lever  */
8031570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
8041570c1e4SChuck Lever {
8051570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
806fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
8071da177e4SLinus Torvalds 
8081570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
8091570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
8101da177e4SLinus Torvalds 
811fda13939STrond Myklebust 	xprt->stat.recvs++;
812d60dbb20STrond Myklebust 	req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
813bbc72ceaSChuck Lever 	if (xprt->ops->timer != NULL)
814bbc72ceaSChuck Lever 		xprt_update_rtt(task);
815ef759a2eSChuck Lever 
8161da177e4SLinus Torvalds 	list_del_init(&req->rq_list);
8171e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
818dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
819dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
82043ac3f29STrond Myklebust 	smp_wmb();
821dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
822fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
8231da177e4SLinus Torvalds }
82412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
8251da177e4SLinus Torvalds 
82646c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
8291da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
8301da177e4SLinus Torvalds 
8315d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
8325d00837bSTrond Myklebust 		return;
83346121cf7SChuck Lever 	dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
83446c0ee8bSChuck Lever 
8355d00837bSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
836dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
83746c0ee8bSChuck Lever 		if (xprt->ops->timer)
83846c0ee8bSChuck Lever 			xprt->ops->timer(task);
8395d00837bSTrond Myklebust 	} else
8405d00837bSTrond Myklebust 		task->tk_status = 0;
8415d00837bSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8444cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt)
8454cfc7e60SRahul Iyer {
8464cfc7e60SRahul Iyer 	return xprt->idle_timeout != 0;
8474cfc7e60SRahul Iyer }
8484cfc7e60SRahul Iyer 
8499903cd1cSChuck Lever /**
8509903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
8519903cd1cSChuck Lever  * @task: RPC task about to send a request
8529903cd1cSChuck Lever  *
8531da177e4SLinus Torvalds  */
8549903cd1cSChuck Lever int xprt_prepare_transmit(struct rpc_task *task)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
8571da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
8581da177e4SLinus Torvalds 	int err = 0;
8591da177e4SLinus Torvalds 
86046121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
8611da177e4SLinus Torvalds 
8624a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
863dd2b63d0SRicardo Labiaga 	if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
864dd2b63d0SRicardo Labiaga 		err = req->rq_reply_bytes_recvd;
8651da177e4SLinus Torvalds 		goto out_unlock;
8661da177e4SLinus Torvalds 	}
86743cedbf0STrond Myklebust 	if (!xprt->ops->reserve_xprt(xprt, task))
8681da177e4SLinus Torvalds 		err = -EAGAIN;
8691da177e4SLinus Torvalds out_unlock:
8704a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
8711da177e4SLinus Torvalds 	return err;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
874e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
8755e5ce5beSTrond Myklebust {
876343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
8775e5ce5beSTrond Myklebust }
8785e5ce5beSTrond Myklebust 
8799903cd1cSChuck Lever /**
8809903cd1cSChuck Lever  * xprt_transmit - send an RPC request on a transport
8819903cd1cSChuck Lever  * @task: controlling RPC task
8829903cd1cSChuck Lever  *
8839903cd1cSChuck Lever  * We have to copy the iovec because sendmsg fiddles with its contents.
8849903cd1cSChuck Lever  */
8859903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
8881da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
88915a45206SAndy Adamson 	int status, numreqs;
8901da177e4SLinus Torvalds 
89146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
8921da177e4SLinus Torvalds 
893dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
89455ae1aabSRicardo Labiaga 		if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
89555ae1aabSRicardo Labiaga 			/*
89655ae1aabSRicardo Labiaga 			 * Add to the list only if we're expecting a reply
89755ae1aabSRicardo Labiaga 			 */
8984a0f8c04SChuck Lever 			spin_lock_bh(&xprt->transport_lock);
8991da177e4SLinus Torvalds 			/* Update the softirq receive buffer */
9001da177e4SLinus Torvalds 			memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
9011da177e4SLinus Torvalds 					sizeof(req->rq_private_buf));
9021da177e4SLinus Torvalds 			/* Add request to the receive list */
9031da177e4SLinus Torvalds 			list_add_tail(&req->rq_list, &xprt->recv);
9044a0f8c04SChuck Lever 			spin_unlock_bh(&xprt->transport_lock);
9051da177e4SLinus Torvalds 			xprt_reset_majortimeo(req);
9060f9dc2b1STrond Myklebust 			/* Turn off autodisconnect */
9070f9dc2b1STrond Myklebust 			del_singleshot_timer_sync(&xprt->timer);
9081da177e4SLinus Torvalds 		}
9091da177e4SLinus Torvalds 	} else if (!req->rq_bytes_sent)
9101da177e4SLinus Torvalds 		return;
9111da177e4SLinus Torvalds 
9127c1d71cfSTrond Myklebust 	req->rq_connect_cookie = xprt->connect_cookie;
913ff839970SChuck Lever 	req->rq_xtime = ktime_get();
914a246b010SChuck Lever 	status = xprt->ops->send_request(task);
915c8485e4dSTrond Myklebust 	if (status != 0) {
916c8485e4dSTrond Myklebust 		task->tk_status = status;
917c8485e4dSTrond Myklebust 		return;
918c8485e4dSTrond Myklebust 	}
919c8485e4dSTrond Myklebust 
92046121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
921468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
922fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
923262ca07dSChuck Lever 
924fe3aca29SChuck Lever 	xprt->ops->set_retrans_timeout(task);
925262ca07dSChuck Lever 
92615a45206SAndy Adamson 	numreqs = atomic_read(&xprt->num_reqs);
92715a45206SAndy Adamson 	if (numreqs > xprt->stat.max_slots)
92815a45206SAndy Adamson 		xprt->stat.max_slots = numreqs;
929262ca07dSChuck Lever 	xprt->stat.sends++;
930262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
931262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
93215a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
93315a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
934262ca07dSChuck Lever 
935fe3aca29SChuck Lever 	/* Don't race with disconnect */
936fe3aca29SChuck Lever 	if (!xprt_connected(xprt))
937fe3aca29SChuck Lever 		task->tk_status = -ENOTCONN;
938dd2b63d0SRicardo Labiaga 	else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
93955ae1aabSRicardo Labiaga 		/*
94055ae1aabSRicardo Labiaga 		 * Sleep on the pending queue since
94155ae1aabSRicardo Labiaga 		 * we're expecting a reply.
94255ae1aabSRicardo Labiaga 		 */
9435d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_timer);
94455ae1aabSRicardo Labiaga 	}
945fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
949d9ba131dSTrond Myklebust {
950d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
951d9ba131dSTrond Myklebust 
952d9ba131dSTrond Myklebust 	if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
953d9ba131dSTrond Myklebust 		goto out;
954d9ba131dSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
955d9ba131dSTrond Myklebust 	if (req != NULL)
956d9ba131dSTrond Myklebust 		goto out;
957d9ba131dSTrond Myklebust 	atomic_dec(&xprt->num_reqs);
958d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
959d9ba131dSTrond Myklebust out:
960d9ba131dSTrond Myklebust 	return req;
961d9ba131dSTrond Myklebust }
962d9ba131dSTrond Myklebust 
963d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
964d9ba131dSTrond Myklebust {
965d9ba131dSTrond Myklebust 	if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
966d9ba131dSTrond Myklebust 		kfree(req);
967d9ba131dSTrond Myklebust 		return true;
968d9ba131dSTrond Myklebust 	}
969d9ba131dSTrond Myklebust 	return false;
970d9ba131dSTrond Myklebust }
971d9ba131dSTrond Myklebust 
972ee5ebe85STrond Myklebust static void xprt_alloc_slot(struct rpc_task *task)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
975d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
978d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
979d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
980d9ba131dSTrond Myklebust 		goto out_init_req;
981d9ba131dSTrond Myklebust 	}
9826b343099SJeff Layton 	req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN);
983d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
984d9ba131dSTrond Myklebust 		goto out_init_req;
985d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
986d9ba131dSTrond Myklebust 	case -ENOMEM:
987d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
988d9ba131dSTrond Myklebust 				"failed! Retrying\n");
9891afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
990d9ba131dSTrond Myklebust 		break;
991d9ba131dSTrond Myklebust 	case -EAGAIN:
992d9ba131dSTrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
993d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
9941afeaf5cSTrond Myklebust 	default:
995d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
9961afeaf5cSTrond Myklebust 	}
997d9ba131dSTrond Myklebust 	return;
998d9ba131dSTrond Myklebust out_init_req:
999d9ba131dSTrond Myklebust 	task->tk_status = 0;
10001da177e4SLinus Torvalds 	task->tk_rqstp = req;
10011da177e4SLinus Torvalds 	xprt_request_init(task, xprt);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
1004ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1005ee5ebe85STrond Myklebust {
1006ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1007c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1008c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1009ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1010c25573b5STrond Myklebust 	}
1011ee5ebe85STrond Myklebust 	rpc_wake_up_next(&xprt->backlog);
1012ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1013ee5ebe85STrond Myklebust }
1014ee5ebe85STrond Myklebust 
101521de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
101621de0a95STrond Myklebust {
101721de0a95STrond Myklebust 	struct rpc_rqst *req;
101821de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
101921de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
102021de0a95STrond Myklebust 		list_del(&req->rq_list);
102121de0a95STrond Myklebust 		kfree(req);
102221de0a95STrond Myklebust 	}
102321de0a95STrond Myklebust }
102421de0a95STrond Myklebust 
1025d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1026d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1027d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1028bd1722d4SPavel Emelyanov {
1029bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
103021de0a95STrond Myklebust 	struct rpc_rqst *req;
103121de0a95STrond Myklebust 	int i;
1032bd1722d4SPavel Emelyanov 
1033bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1034bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1035bd1722d4SPavel Emelyanov 		goto out;
1036bd1722d4SPavel Emelyanov 
103721de0a95STrond Myklebust 	xprt_init(xprt, net);
103821de0a95STrond Myklebust 
103921de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
104021de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
104121de0a95STrond Myklebust 		if (!req)
104221de0a95STrond Myklebust 			break;
104321de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
104421de0a95STrond Myklebust 	}
104521de0a95STrond Myklebust 	if (i < num_prealloc)
1046bd1722d4SPavel Emelyanov 		goto out_free;
1047d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1048d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1049d9ba131dSTrond Myklebust 	else
105021de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1051d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1052d9ba131dSTrond Myklebust 	atomic_set(&xprt->num_reqs, num_prealloc);
1053bd1722d4SPavel Emelyanov 
1054bd1722d4SPavel Emelyanov 	return xprt;
1055bd1722d4SPavel Emelyanov 
1056bd1722d4SPavel Emelyanov out_free:
105721de0a95STrond Myklebust 	xprt_free(xprt);
1058bd1722d4SPavel Emelyanov out:
1059bd1722d4SPavel Emelyanov 	return NULL;
1060bd1722d4SPavel Emelyanov }
1061bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1062bd1722d4SPavel Emelyanov 
1063e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1064e204e621SPavel Emelyanov {
106537aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
106621de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1067e204e621SPavel Emelyanov 	kfree(xprt);
1068e204e621SPavel Emelyanov }
1069e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1070e204e621SPavel Emelyanov 
10719903cd1cSChuck Lever /**
10729903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
10739903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
10749903cd1cSChuck Lever  *
10759903cd1cSChuck Lever  * If no more slots are available, place the task on the transport's
10769903cd1cSChuck Lever  * backlog queue.
10779903cd1cSChuck Lever  */
10789903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = task->tk_xprt;
10811da177e4SLinus Torvalds 
108243cedbf0STrond Myklebust 	task->tk_status = 0;
108343cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
108443cedbf0STrond Myklebust 		return;
108543cedbf0STrond Myklebust 
108643cedbf0STrond Myklebust 	/* Note: grabbing the xprt_lock_write() here is not strictly needed,
108743cedbf0STrond Myklebust 	 * but ensures that we throttle new slot allocation if the transport
108843cedbf0STrond Myklebust 	 * is congested (e.g. if reconnecting or if we're out of socket
108943cedbf0STrond Myklebust 	 * write buffer space).
109043cedbf0STrond Myklebust 	 */
109143cedbf0STrond Myklebust 	task->tk_timeout = 0;
109243cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
109343cedbf0STrond Myklebust 	if (!xprt_lock_write(xprt, task))
109443cedbf0STrond Myklebust 		return;
109543cedbf0STrond Myklebust 
10965dc07727SChuck Lever 	spin_lock(&xprt->reserve_lock);
1097ee5ebe85STrond Myklebust 	xprt_alloc_slot(task);
10985dc07727SChuck Lever 	spin_unlock(&xprt->reserve_lock);
109943cedbf0STrond Myklebust 	xprt_release_write(xprt, task);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
1102d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
11031da177e4SLinus Torvalds {
11040eae88f3SEric Dumazet 	return (__force __be32)xprt->xid++;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt)
11081da177e4SLinus Torvalds {
1109bf3fcf89SChuck Lever 	xprt->xid = net_random();
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11129903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
11151da177e4SLinus Torvalds 
1116d9ba131dSTrond Myklebust 	INIT_LIST_HEAD(&req->rq_list);
1117ba7392bbSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
11181da177e4SLinus Torvalds 	req->rq_task	= task;
11191da177e4SLinus Torvalds 	req->rq_xprt    = xprt;
112002107148SChuck Lever 	req->rq_buffer  = NULL;
11211da177e4SLinus Torvalds 	req->rq_xid     = xprt_alloc_xid(xprt);
1122ead5e1c2SJ. Bruce Fields 	req->rq_release_snd_buf = NULL;
1123da45828eSTrond Myklebust 	xprt_reset_majortimeo(req);
112446121cf7SChuck Lever 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
11251da177e4SLinus Torvalds 			req, ntohl(req->rq_xid));
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11289903cd1cSChuck Lever /**
11299903cd1cSChuck Lever  * xprt_release - release an RPC request slot
11309903cd1cSChuck Lever  * @task: task which is finished with the slot
11319903cd1cSChuck Lever  *
11321da177e4SLinus Torvalds  */
11339903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
11341da177e4SLinus Torvalds {
113555ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
11361da177e4SLinus Torvalds 	struct rpc_rqst	*req;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	if (!(req = task->tk_rqstp))
11391da177e4SLinus Torvalds 		return;
114055ae1aabSRicardo Labiaga 
114155ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
11420a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
11430a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
11440a702195SWeston Andros Adamson 	else if (task->tk_client)
11450a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
11464a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
114749e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1148a58dd398SChuck Lever 	if (xprt->ops->release_request)
1149a58dd398SChuck Lever 		xprt->ops->release_request(task);
11501da177e4SLinus Torvalds 	if (!list_empty(&req->rq_list))
11511da177e4SLinus Torvalds 		list_del(&req->rq_list);
11521da177e4SLinus Torvalds 	xprt->last_used = jiffies;
11534cfc7e60SRahul Iyer 	if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
1154a246b010SChuck Lever 		mod_timer(&xprt->timer,
115503bf4b70SChuck Lever 				xprt->last_used + xprt->idle_timeout);
11564a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1157ee5ebe85STrond Myklebust 	if (req->rq_buffer)
1158c5a4dd8bSChuck Lever 		xprt->ops->buf_free(req->rq_buffer);
1159a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1160a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
11611da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1162ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1163ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
116455ae1aabSRicardo Labiaga 
116546121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1166ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1167ee5ebe85STrond Myklebust 		xprt_free_slot(xprt, req);
1168ee5ebe85STrond Myklebust 	else
1169c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
117221de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1173c2866763SChuck Lever {
117421de0a95STrond Myklebust 	atomic_set(&xprt->count, 1);
1175c2866763SChuck Lever 
1176c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1177c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
1178c2866763SChuck Lever 
1179c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
1180c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->recv);
11819e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1182f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1183f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
11849e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1185f9acac1aSRicardo Labiaga 
1186c2866763SChuck Lever 	xprt->last_used = jiffies;
1187c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1188a509050bSChuck Lever 	xprt->bind_index = 0;
1189c2866763SChuck Lever 
1190c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1191c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
119234006ceeSTrond Myklebust 	rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1193c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1194c2866763SChuck Lever 
1195c2866763SChuck Lever 	xprt_init_xid(xprt);
1196c2866763SChuck Lever 
119721de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
11988d9266ffSTrond Myklebust }
11998d9266ffSTrond Myklebust 
12008d9266ffSTrond Myklebust /**
12018d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
12028d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
12038d9266ffSTrond Myklebust  *
12048d9266ffSTrond Myklebust  */
12058d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
12068d9266ffSTrond Myklebust {
12078d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
12088d9266ffSTrond Myklebust 	struct xprt_class *t;
12098d9266ffSTrond Myklebust 
12108d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
12118d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
12128d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
12138d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
12148d9266ffSTrond Myklebust 			goto found;
12158d9266ffSTrond Myklebust 		}
12168d9266ffSTrond Myklebust 	}
12178d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
12188d9266ffSTrond Myklebust 	printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
12198d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
12208d9266ffSTrond Myklebust 
12218d9266ffSTrond Myklebust found:
12228d9266ffSTrond Myklebust 	xprt = t->setup(args);
12238d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
12248d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
12258d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
122621de0a95STrond Myklebust 		goto out;
12278d9266ffSTrond Myklebust 	}
122821de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
122921de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
123021de0a95STrond Myklebust 		setup_timer(&xprt->timer, xprt_init_autodisconnect,
123121de0a95STrond Myklebust 			    (unsigned long)xprt);
123221de0a95STrond Myklebust 	else
123321de0a95STrond Myklebust 		init_timer(&xprt->timer);
12344e0038b6STrond Myklebust 
12354e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
12364e0038b6STrond Myklebust 		xprt_destroy(xprt);
12374e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
12384e0038b6STrond Myklebust 	}
12394e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
12404e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
12414e0038b6STrond Myklebust 		xprt_destroy(xprt);
12424e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
12434e0038b6STrond Myklebust 	}
12444e0038b6STrond Myklebust 
1245c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1246c2866763SChuck Lever 			xprt->max_reqs);
124721de0a95STrond Myklebust out:
1248c2866763SChuck Lever 	return xprt;
1249c2866763SChuck Lever }
1250c2866763SChuck Lever 
12519903cd1cSChuck Lever /**
12529903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1253a8de240aSTrond Myklebust  * @xprt: transport to destroy
12549903cd1cSChuck Lever  *
12551da177e4SLinus Torvalds  */
1256a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
12590065db32STrond Myklebust 	xprt->shutdown = 1;
12600065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1261c8541ecdSChuck Lever 
1262f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1263f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1264f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1265f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1266c3ae62aeSJ. Bruce Fields 	cancel_work_sync(&xprt->task_cleanup);
12674e0038b6STrond Myklebust 	kfree(xprt->servername);
1268c8541ecdSChuck Lever 	/*
1269c8541ecdSChuck Lever 	 * Tear down transport state and free the rpc_xprt
1270c8541ecdSChuck Lever 	 */
1271a246b010SChuck Lever 	xprt->ops->destroy(xprt);
12726b6ca86bSTrond Myklebust }
12731da177e4SLinus Torvalds 
12746b6ca86bSTrond Myklebust /**
12756b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
12766b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
12776b6ca86bSTrond Myklebust  *
12786b6ca86bSTrond Myklebust  */
12796b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
12806b6ca86bSTrond Myklebust {
1281a8de240aSTrond Myklebust 	if (atomic_dec_and_test(&xprt->count))
1282a8de240aSTrond Myklebust 		xprt_destroy(xprt);
12836b6ca86bSTrond Myklebust }
12846b6ca86bSTrond Myklebust 
12856b6ca86bSTrond Myklebust /**
12866b6ca86bSTrond Myklebust  * xprt_get - return a reference to an RPC transport.
12876b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
12886b6ca86bSTrond Myklebust  *
12896b6ca86bSTrond Myklebust  */
12906b6ca86bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
12916b6ca86bSTrond Myklebust {
1292a8de240aSTrond Myklebust 	if (atomic_inc_not_zero(&xprt->count))
12936b6ca86bSTrond Myklebust 		return xprt;
1294a8de240aSTrond Myklebust 	return NULL;
12951da177e4SLinus Torvalds }
1296