xref: /openbmc/linux/net/sunrpc/xprt.c (revision 4a068258)
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 
523705ad64SJeff Layton #include <trace/events/sunrpc.h>
533705ad64SJeff Layton 
5455ae1aabSRicardo Labiaga #include "sunrpc.h"
5555ae1aabSRicardo Labiaga 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Local variables
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds 
60f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
611da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_XPRT
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Local functions
661da177e4SLinus Torvalds  */
6721de0a95STrond Myklebust static void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
681da177e4SLinus Torvalds static void	xprt_request_init(struct rpc_task *, struct rpc_xprt *);
691da177e4SLinus Torvalds static void	xprt_connect_status(struct rpc_task *task);
701da177e4SLinus Torvalds static int      __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
714e0038b6STrond Myklebust static void	 xprt_destroy(struct rpc_xprt *xprt);
721da177e4SLinus Torvalds 
735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock);
7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list);
7581c098afS\"Talpey, Thomas\ 
7612a80469SChuck Lever /**
7781c098afS\"Talpey, Thomas\  * xprt_register_transport - register a transport implementation
7881c098afS\"Talpey, Thomas\  * @transport: transport to register
7981c098afS\"Talpey, Thomas\  *
8081c098afS\"Talpey, Thomas\  * If a transport implementation is loaded as a kernel module, it can
8181c098afS\"Talpey, Thomas\  * call this interface to make itself known to the RPC client.
8281c098afS\"Talpey, Thomas\  *
8381c098afS\"Talpey, Thomas\  * Returns:
8481c098afS\"Talpey, Thomas\  * 0:		transport successfully registered
8581c098afS\"Talpey, Thomas\  * -EEXIST:	transport already registered
8681c098afS\"Talpey, Thomas\  * -EINVAL:	transport module being unloaded
8781c098afS\"Talpey, Thomas\  */
8881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport)
8981c098afS\"Talpey, Thomas\ {
9081c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
9181c098afS\"Talpey, Thomas\ 	int result;
9281c098afS\"Talpey, Thomas\ 
9381c098afS\"Talpey, Thomas\ 	result = -EEXIST;
9481c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
9581c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
9681c098afS\"Talpey, Thomas\ 		/* don't register the same transport class twice */
974fa016ebS\"Talpey, Thomas\ 		if (t->ident == transport->ident)
9881c098afS\"Talpey, Thomas\ 			goto out;
9981c098afS\"Talpey, Thomas\ 	}
10081c098afS\"Talpey, Thomas\ 
10181c098afS\"Talpey, Thomas\ 	list_add_tail(&transport->list, &xprt_list);
10281c098afS\"Talpey, Thomas\ 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
10381c098afS\"Talpey, Thomas\ 	       transport->name);
10481c098afS\"Talpey, Thomas\ 	result = 0;
10581c098afS\"Talpey, Thomas\ 
10681c098afS\"Talpey, Thomas\ out:
10781c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
10881c098afS\"Talpey, Thomas\ 	return result;
10981c098afS\"Talpey, Thomas\ }
11081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport);
11181c098afS\"Talpey, Thomas\ 
11281c098afS\"Talpey, Thomas\ /**
11381c098afS\"Talpey, Thomas\  * xprt_unregister_transport - unregister a transport implementation
11465b6e42cSRandy Dunlap  * @transport: transport to unregister
11581c098afS\"Talpey, Thomas\  *
11681c098afS\"Talpey, Thomas\  * Returns:
11781c098afS\"Talpey, Thomas\  * 0:		transport successfully unregistered
11881c098afS\"Talpey, Thomas\  * -ENOENT:	transport never registered
11981c098afS\"Talpey, Thomas\  */
12081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport)
12181c098afS\"Talpey, Thomas\ {
12281c098afS\"Talpey, Thomas\ 	struct xprt_class *t;
12381c098afS\"Talpey, Thomas\ 	int result;
12481c098afS\"Talpey, Thomas\ 
12581c098afS\"Talpey, Thomas\ 	result = 0;
12681c098afS\"Talpey, Thomas\ 	spin_lock(&xprt_list_lock);
12781c098afS\"Talpey, Thomas\ 	list_for_each_entry(t, &xprt_list, list) {
12881c098afS\"Talpey, Thomas\ 		if (t == transport) {
12981c098afS\"Talpey, Thomas\ 			printk(KERN_INFO
13081c098afS\"Talpey, Thomas\ 				"RPC: Unregistered %s transport module.\n",
13181c098afS\"Talpey, Thomas\ 				transport->name);
13281c098afS\"Talpey, Thomas\ 			list_del_init(&transport->list);
13381c098afS\"Talpey, Thomas\ 			goto out;
13481c098afS\"Talpey, Thomas\ 		}
13581c098afS\"Talpey, Thomas\ 	}
13681c098afS\"Talpey, Thomas\ 	result = -ENOENT;
13781c098afS\"Talpey, Thomas\ 
13881c098afS\"Talpey, Thomas\ out:
13981c098afS\"Talpey, Thomas\ 	spin_unlock(&xprt_list_lock);
14081c098afS\"Talpey, Thomas\ 	return result;
14181c098afS\"Talpey, Thomas\ }
14281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport);
14381c098afS\"Talpey, Thomas\ 
14481c098afS\"Talpey, Thomas\ /**
145441e3e24STom Talpey  * xprt_load_transport - load a transport implementation
146441e3e24STom Talpey  * @transport_name: transport to load
147441e3e24STom Talpey  *
148441e3e24STom Talpey  * Returns:
149441e3e24STom Talpey  * 0:		transport successfully loaded
150441e3e24STom Talpey  * -ENOENT:	transport module not available
151441e3e24STom Talpey  */
152441e3e24STom Talpey int xprt_load_transport(const char *transport_name)
153441e3e24STom Talpey {
154441e3e24STom Talpey 	struct xprt_class *t;
155441e3e24STom Talpey 	int result;
156441e3e24STom Talpey 
157441e3e24STom Talpey 	result = 0;
158441e3e24STom Talpey 	spin_lock(&xprt_list_lock);
159441e3e24STom Talpey 	list_for_each_entry(t, &xprt_list, list) {
160441e3e24STom Talpey 		if (strcmp(t->name, transport_name) == 0) {
161441e3e24STom Talpey 			spin_unlock(&xprt_list_lock);
162441e3e24STom Talpey 			goto out;
163441e3e24STom Talpey 		}
164441e3e24STom Talpey 	}
165441e3e24STom Talpey 	spin_unlock(&xprt_list_lock);
166ef7ffe8fSAlex Riesen 	result = request_module("xprt%s", transport_name);
167441e3e24STom Talpey out:
168441e3e24STom Talpey 	return result;
169441e3e24STom Talpey }
170441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport);
171441e3e24STom Talpey 
172441e3e24STom Talpey /**
17312a80469SChuck Lever  * xprt_reserve_xprt - serialize write access to transports
17412a80469SChuck Lever  * @task: task that is requesting access to the transport
175177c27bfSRandy Dunlap  * @xprt: pointer to the target transport
17612a80469SChuck Lever  *
17712a80469SChuck Lever  * This prevents mixing the payload of separate requests, and prevents
17812a80469SChuck Lever  * transport connects from colliding with writes.  No congestion control
17912a80469SChuck Lever  * is provided.
1801da177e4SLinus Torvalds  */
18143cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1821da177e4SLinus Torvalds {
18312a80469SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
18434006ceeSTrond Myklebust 	int priority;
18512a80469SChuck Lever 
18612a80469SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
18712a80469SChuck Lever 		if (task == xprt->snd_task)
18812a80469SChuck Lever 			return 1;
18912a80469SChuck Lever 		goto out_sleep;
19012a80469SChuck Lever 	}
19112a80469SChuck Lever 	xprt->snd_task = task;
19292551948STrond Myklebust 	if (req != NULL)
19312a80469SChuck Lever 		req->rq_ntrans++;
1944d4a76f3Sj223yang@asset.uwaterloo.ca 
19512a80469SChuck Lever 	return 1;
19612a80469SChuck Lever 
19712a80469SChuck Lever out_sleep:
19846121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n",
19912a80469SChuck Lever 			task->tk_pid, xprt);
20012a80469SChuck Lever 	task->tk_timeout = 0;
20112a80469SChuck Lever 	task->tk_status = -EAGAIN;
20234006ceeSTrond Myklebust 	if (req == NULL)
20334006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
20434006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
20534006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
20612a80469SChuck Lever 	else
20734006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
20834006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
20912a80469SChuck Lever 	return 0;
21012a80469SChuck Lever }
21112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
21212a80469SChuck Lever 
213632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt)
214632e3bdcSTrond Myklebust {
215632e3bdcSTrond Myklebust 	xprt->snd_task = NULL;
216d19751e7STrond Myklebust 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
2174e857c58SPeter Zijlstra 		smp_mb__before_atomic();
218632e3bdcSTrond Myklebust 		clear_bit(XPRT_LOCKED, &xprt->state);
2194e857c58SPeter Zijlstra 		smp_mb__after_atomic();
220632e3bdcSTrond Myklebust 	} else
221c1384c9cSTrond Myklebust 		queue_work(rpciod_workqueue, &xprt->task_cleanup);
222632e3bdcSTrond Myklebust }
223632e3bdcSTrond Myklebust 
22412a80469SChuck Lever /*
22512a80469SChuck Lever  * xprt_reserve_xprt_cong - serialize write access to transports
22612a80469SChuck Lever  * @task: task that is requesting access to the transport
22712a80469SChuck Lever  *
22812a80469SChuck Lever  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
22912a80469SChuck Lever  * integrated into the decision of whether a request is allowed to be
23012a80469SChuck Lever  * woken up and given access to the transport.
23112a80469SChuck Lever  */
23243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
23312a80469SChuck Lever {
2341da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
23534006ceeSTrond Myklebust 	int priority;
2361da177e4SLinus Torvalds 
2372226feb6SChuck Lever 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2381da177e4SLinus Torvalds 		if (task == xprt->snd_task)
2391da177e4SLinus Torvalds 			return 1;
2401da177e4SLinus Torvalds 		goto out_sleep;
2411da177e4SLinus Torvalds 	}
24243cedbf0STrond Myklebust 	if (req == NULL) {
24343cedbf0STrond Myklebust 		xprt->snd_task = task;
24443cedbf0STrond Myklebust 		return 1;
24543cedbf0STrond Myklebust 	}
24612a80469SChuck Lever 	if (__xprt_get_cong(xprt, task)) {
2471da177e4SLinus Torvalds 		xprt->snd_task = task;
2481da177e4SLinus Torvalds 		req->rq_ntrans++;
2491da177e4SLinus Torvalds 		return 1;
2501da177e4SLinus Torvalds 	}
251632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
2521da177e4SLinus Torvalds out_sleep:
25346121cf7SChuck Lever 	dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
2541da177e4SLinus Torvalds 	task->tk_timeout = 0;
2551da177e4SLinus Torvalds 	task->tk_status = -EAGAIN;
25634006ceeSTrond Myklebust 	if (req == NULL)
25734006ceeSTrond Myklebust 		priority = RPC_PRIORITY_LOW;
25834006ceeSTrond Myklebust 	else if (!req->rq_ntrans)
25934006ceeSTrond Myklebust 		priority = RPC_PRIORITY_NORMAL;
2601da177e4SLinus Torvalds 	else
26134006ceeSTrond Myklebust 		priority = RPC_PRIORITY_HIGH;
26234006ceeSTrond Myklebust 	rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
2631da177e4SLinus Torvalds 	return 0;
2641da177e4SLinus Torvalds }
26512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
2661da177e4SLinus Torvalds 
26712a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	int retval;
2701da177e4SLinus Torvalds 
2714a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
27243cedbf0STrond Myklebust 	retval = xprt->ops->reserve_xprt(xprt, task);
2734a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
2741da177e4SLinus Torvalds 	return retval;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
277961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
2781da177e4SLinus Torvalds {
279961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
28049e9a890SChuck Lever 	struct rpc_rqst *req;
28149e9a890SChuck Lever 
28249e9a890SChuck Lever 	req = task->tk_rqstp;
28349e9a890SChuck Lever 	xprt->snd_task = task;
28492551948STrond Myklebust 	if (req)
28549e9a890SChuck Lever 		req->rq_ntrans++;
286961a828dSTrond Myklebust 	return true;
287961a828dSTrond Myklebust }
288961a828dSTrond Myklebust 
289961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt)
290961a828dSTrond Myklebust {
291961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
29249e9a890SChuck Lever 		return;
29349e9a890SChuck Lever 
294961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt))
295961a828dSTrond Myklebust 		return;
296632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
29749e9a890SChuck Lever }
29849e9a890SChuck Lever 
299961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
30049e9a890SChuck Lever {
301961a828dSTrond Myklebust 	struct rpc_xprt *xprt = data;
30243cedbf0STrond Myklebust 	struct rpc_rqst *req;
3031da177e4SLinus Torvalds 
30443cedbf0STrond Myklebust 	req = task->tk_rqstp;
30543cedbf0STrond Myklebust 	if (req == NULL) {
3061da177e4SLinus Torvalds 		xprt->snd_task = task;
307961a828dSTrond Myklebust 		return true;
30843cedbf0STrond Myklebust 	}
30943cedbf0STrond Myklebust 	if (__xprt_get_cong(xprt, task)) {
31043cedbf0STrond Myklebust 		xprt->snd_task = task;
3111da177e4SLinus Torvalds 		req->rq_ntrans++;
312961a828dSTrond Myklebust 		return true;
3131da177e4SLinus Torvalds 	}
314961a828dSTrond Myklebust 	return false;
315961a828dSTrond Myklebust }
316961a828dSTrond Myklebust 
317961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
318961a828dSTrond Myklebust {
319961a828dSTrond Myklebust 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
320961a828dSTrond Myklebust 		return;
321961a828dSTrond Myklebust 	if (RPCXPRT_CONGESTED(xprt))
322961a828dSTrond Myklebust 		goto out_unlock;
323961a828dSTrond Myklebust 	if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt))
324961a828dSTrond Myklebust 		return;
3251da177e4SLinus Torvalds out_unlock:
326632e3bdcSTrond Myklebust 	xprt_clear_locked(xprt);
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3290695314eSTrond Myklebust static void xprt_task_clear_bytes_sent(struct rpc_task *task)
3300695314eSTrond Myklebust {
3310695314eSTrond Myklebust 	if (task != NULL) {
3320695314eSTrond Myklebust 		struct rpc_rqst *req = task->tk_rqstp;
3330695314eSTrond Myklebust 		if (req != NULL)
3340695314eSTrond Myklebust 			req->rq_bytes_sent = 0;
3350695314eSTrond Myklebust 	}
3360695314eSTrond Myklebust }
3370695314eSTrond Myklebust 
33849e9a890SChuck Lever /**
33949e9a890SChuck Lever  * xprt_release_xprt - allow other requests to use a transport
34049e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
34149e9a890SChuck Lever  * @task: task that is releasing access to the transport
34249e9a890SChuck Lever  *
34349e9a890SChuck Lever  * Note that "task" can be NULL.  No congestion control is provided.
3441da177e4SLinus Torvalds  */
34549e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	if (xprt->snd_task == task) {
3480695314eSTrond Myklebust 		xprt_task_clear_bytes_sent(task);
349632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
3501da177e4SLinus Torvalds 		__xprt_lock_write_next(xprt);
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds }
35312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt);
3541da177e4SLinus Torvalds 
35549e9a890SChuck Lever /**
35649e9a890SChuck Lever  * xprt_release_xprt_cong - allow other requests to use a transport
35749e9a890SChuck Lever  * @xprt: transport with other tasks potentially waiting
35849e9a890SChuck Lever  * @task: task that is releasing access to the transport
35949e9a890SChuck Lever  *
36049e9a890SChuck Lever  * Note that "task" can be NULL.  Another task is awoken to use the
36149e9a890SChuck Lever  * transport if the transport's congestion window allows it.
36249e9a890SChuck Lever  */
36349e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
36449e9a890SChuck Lever {
36549e9a890SChuck Lever 	if (xprt->snd_task == task) {
3660695314eSTrond Myklebust 		xprt_task_clear_bytes_sent(task);
367632e3bdcSTrond Myklebust 		xprt_clear_locked(xprt);
36849e9a890SChuck Lever 		__xprt_lock_write_next_cong(xprt);
36949e9a890SChuck Lever 	}
37049e9a890SChuck Lever }
37112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
37249e9a890SChuck Lever 
37349e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
3741da177e4SLinus Torvalds {
3754a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
37649e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
3774a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  * Van Jacobson congestion avoidance. Check if the congestion window
3821da177e4SLinus Torvalds  * overflowed. Put the task to sleep if this is the case.
3831da177e4SLinus Torvalds  */
3841da177e4SLinus Torvalds static int
3851da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (req->rq_cong)
3901da177e4SLinus Torvalds 		return 1;
39146121cf7SChuck Lever 	dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
3921da177e4SLinus Torvalds 			task->tk_pid, xprt->cong, xprt->cwnd);
3931da177e4SLinus Torvalds 	if (RPCXPRT_CONGESTED(xprt))
3941da177e4SLinus Torvalds 		return 0;
3951da177e4SLinus Torvalds 	req->rq_cong = 1;
3961da177e4SLinus Torvalds 	xprt->cong += RPC_CWNDSCALE;
3971da177e4SLinus Torvalds 	return 1;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*
4011da177e4SLinus Torvalds  * Adjust the congestion window, and wake up the next task
4021da177e4SLinus Torvalds  * that has been sleeping due to congestion
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds static void
4051da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	if (!req->rq_cong)
4081da177e4SLinus Torvalds 		return;
4091da177e4SLinus Torvalds 	req->rq_cong = 0;
4101da177e4SLinus Torvalds 	xprt->cong -= RPC_CWNDSCALE;
41149e9a890SChuck Lever 	__xprt_lock_write_next_cong(xprt);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
41446c0ee8bSChuck Lever /**
415a58dd398SChuck Lever  * xprt_release_rqst_cong - housekeeping when request is complete
416a58dd398SChuck Lever  * @task: RPC request that recently completed
417a58dd398SChuck Lever  *
418a58dd398SChuck Lever  * Useful for transports that require congestion control.
419a58dd398SChuck Lever  */
420a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task)
421a58dd398SChuck Lever {
422a4f0835cSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
423a4f0835cSTrond Myklebust 
424a4f0835cSTrond Myklebust 	__xprt_put_cong(req->rq_xprt, req);
425a58dd398SChuck Lever }
42612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
427a58dd398SChuck Lever 
428a58dd398SChuck Lever /**
42946c0ee8bSChuck Lever  * xprt_adjust_cwnd - adjust transport congestion window
4306a24dfb6STrond Myklebust  * @xprt: pointer to xprt
43146c0ee8bSChuck Lever  * @task: recently completed RPC request used to adjust window
43246c0ee8bSChuck Lever  * @result: result code of completed RPC request
43346c0ee8bSChuck Lever  *
4344f4cf5adSChuck Lever  * The transport code maintains an estimate on the maximum number of out-
4354f4cf5adSChuck Lever  * standing RPC requests, using a smoothed version of the congestion
4364f4cf5adSChuck Lever  * avoidance implemented in 44BSD. This is basically the Van Jacobson
4374f4cf5adSChuck Lever  * congestion algorithm: If a retransmit occurs, the congestion window is
4384f4cf5adSChuck Lever  * halved; otherwise, it is incremented by 1/cwnd when
4394f4cf5adSChuck Lever  *
4404f4cf5adSChuck Lever  *	-	a reply is received and
4414f4cf5adSChuck Lever  *	-	a full number of requests are outstanding and
4424f4cf5adSChuck Lever  *	-	the congestion window hasn't been updated recently.
4431da177e4SLinus Torvalds  */
4446a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
4451da177e4SLinus Torvalds {
44646c0ee8bSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
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
487a9a6b52eSTrond Myklebust  *
488a9a6b52eSTrond Myklebust  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
489a9a6b52eSTrond Myklebust  * we don't in general want to force a socket disconnection due to
490a9a6b52eSTrond Myklebust  * an incomplete RPC call transmission.
491c7b2cae8SChuck Lever  */
492b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
493c7b2cae8SChuck Lever {
494c7b2cae8SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
495c7b2cae8SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
496c7b2cae8SChuck Lever 
497a9a6b52eSTrond Myklebust 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
498b6ddf64fSTrond Myklebust 	rpc_sleep_on(&xprt->pending, task, action);
499c7b2cae8SChuck Lever }
50012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
501c7b2cae8SChuck Lever 
502c7b2cae8SChuck Lever /**
503c7b2cae8SChuck Lever  * xprt_write_space - wake the task waiting for transport output buffer space
504c7b2cae8SChuck Lever  * @xprt: transport with waiting tasks
505c7b2cae8SChuck Lever  *
506c7b2cae8SChuck Lever  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
507c7b2cae8SChuck Lever  */
508c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt)
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);
682d19751e7STrond Myklebust 	if (!list_empty(&xprt->recv))
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);
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 
693718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt,
694718ba5b8STrond Myklebust 		struct rpc_task *task,
695718ba5b8STrond Myklebust 		void *cookie)
696718ba5b8STrond Myklebust {
697718ba5b8STrond Myklebust 	bool ret = false;
698718ba5b8STrond Myklebust 
699718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
700718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
701718ba5b8STrond Myklebust 		goto out;
702718ba5b8STrond Myklebust 	if (xprt->snd_task != task)
703718ba5b8STrond Myklebust 		goto out;
7040695314eSTrond Myklebust 	xprt_task_clear_bytes_sent(task);
705718ba5b8STrond Myklebust 	xprt->snd_task = cookie;
706718ba5b8STrond Myklebust 	ret = true;
707718ba5b8STrond Myklebust out:
708718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
709718ba5b8STrond Myklebust 	return ret;
710718ba5b8STrond Myklebust }
711718ba5b8STrond Myklebust 
712718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
713718ba5b8STrond Myklebust {
714718ba5b8STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
715718ba5b8STrond Myklebust 	if (xprt->snd_task != cookie)
716718ba5b8STrond Myklebust 		goto out;
717718ba5b8STrond Myklebust 	if (!test_bit(XPRT_LOCKED, &xprt->state))
718718ba5b8STrond Myklebust 		goto out;
719718ba5b8STrond Myklebust 	xprt->snd_task =NULL;
720718ba5b8STrond Myklebust 	xprt->ops->release_xprt(xprt, NULL);
721718ba5b8STrond Myklebust out:
722718ba5b8STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
723718ba5b8STrond Myklebust }
724718ba5b8STrond Myklebust 
7259903cd1cSChuck Lever /**
7269903cd1cSChuck Lever  * xprt_connect - schedule a transport connect operation
7279903cd1cSChuck Lever  * @task: RPC task that is requesting the connect
7281da177e4SLinus Torvalds  *
7291da177e4SLinus Torvalds  */
7301da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task)
7311da177e4SLinus Torvalds {
732ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7331da177e4SLinus Torvalds 
73446121cf7SChuck Lever 	dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
7351da177e4SLinus Torvalds 			xprt, (xprt_connected(xprt) ? "is" : "is not"));
7361da177e4SLinus Torvalds 
737ec739ef0SChuck Lever 	if (!xprt_bound(xprt)) {
73801d37c42STrond Myklebust 		task->tk_status = -EAGAIN;
7391da177e4SLinus Torvalds 		return;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 	if (!xprt_lock_write(xprt, task))
7421da177e4SLinus Torvalds 		return;
743feb8ca37STrond Myklebust 
744feb8ca37STrond Myklebust 	if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
745feb8ca37STrond Myklebust 		xprt->ops->close(xprt);
746feb8ca37STrond Myklebust 
747718ba5b8STrond Myklebust 	if (!xprt_connected(xprt)) {
7481da177e4SLinus Torvalds 		task->tk_rqstp->rq_bytes_sent = 0;
749a8ce4a8fSTrond Myklebust 		task->tk_timeout = task->tk_rqstp->rq_timeout;
7505d00837bSTrond Myklebust 		rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
7510b9e7943STrond Myklebust 
7520b9e7943STrond Myklebust 		if (test_bit(XPRT_CLOSING, &xprt->state))
7530b9e7943STrond Myklebust 			return;
7540b9e7943STrond Myklebust 		if (xprt_test_and_set_connecting(xprt))
7550b9e7943STrond Myklebust 			return;
756262ca07dSChuck Lever 		xprt->stat.connect_start = jiffies;
7571b092092STrond Myklebust 		xprt->ops->connect(xprt, task);
7581da177e4SLinus Torvalds 	}
759718ba5b8STrond Myklebust 	xprt_release_write(xprt, task);
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7629903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task)
7631da177e4SLinus Torvalds {
764ad2368d6STrond Myklebust 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
7651da177e4SLinus Torvalds 
766cd983ef8SChuck Lever 	if (task->tk_status == 0) {
767262ca07dSChuck Lever 		xprt->stat.connect_count++;
768262ca07dSChuck Lever 		xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
76946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connection established\n",
7701da177e4SLinus Torvalds 				task->tk_pid);
7711da177e4SLinus Torvalds 		return;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	switch (task->tk_status) {
7750fe8d04eSTrond Myklebust 	case -ECONNREFUSED:
7760fe8d04eSTrond Myklebust 	case -ECONNRESET:
7770fe8d04eSTrond Myklebust 	case -ECONNABORTED:
7780fe8d04eSTrond Myklebust 	case -ENETUNREACH:
7790fe8d04eSTrond Myklebust 	case -EHOSTUNREACH:
7802fc193cfSTrond Myklebust 	case -EPIPE:
7812a491991STrond Myklebust 	case -EAGAIN:
7822a491991STrond Myklebust 		dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
78323475d66SChuck Lever 		break;
7841da177e4SLinus Torvalds 	case -ETIMEDOUT:
78546121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
78646121cf7SChuck Lever 				"out\n", task->tk_pid);
7871da177e4SLinus Torvalds 		break;
7881da177e4SLinus Torvalds 	default:
78946121cf7SChuck Lever 		dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
79046121cf7SChuck Lever 				"server %s\n", task->tk_pid, -task->tk_status,
7914e0038b6STrond Myklebust 				xprt->servername);
79223475d66SChuck Lever 		task->tk_status = -EIO;
79323475d66SChuck Lever 	}
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7969903cd1cSChuck Lever /**
7979903cd1cSChuck Lever  * xprt_lookup_rqst - find an RPC request corresponding to an XID
7989903cd1cSChuck Lever  * @xprt: transport on which the original request was transmitted
7999903cd1cSChuck Lever  * @xid: RPC XID of incoming reply
8009903cd1cSChuck Lever  *
8011da177e4SLinus Torvalds  */
802d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
8031da177e4SLinus Torvalds {
8048f3a6de3SPavel Emelyanov 	struct rpc_rqst *entry;
8051da177e4SLinus Torvalds 
8068f3a6de3SPavel Emelyanov 	list_for_each_entry(entry, &xprt->recv, rq_list)
8073705ad64SJeff Layton 		if (entry->rq_xid == xid) {
8083705ad64SJeff Layton 			trace_xprt_lookup_rqst(xprt, xid, 0);
809262ca07dSChuck Lever 			return entry;
8103705ad64SJeff Layton 		}
81146121cf7SChuck Lever 
81246121cf7SChuck Lever 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
81346121cf7SChuck Lever 			ntohl(xid));
8143705ad64SJeff Layton 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
815262ca07dSChuck Lever 	xprt->stat.bad_xids++;
816262ca07dSChuck Lever 	return NULL;
8171da177e4SLinus Torvalds }
81812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
8191da177e4SLinus Torvalds 
820bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task)
8211da177e4SLinus Torvalds {
8221570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
8231570c1e4SChuck Lever 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
82495c96174SEric Dumazet 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
825d60dbb20STrond Myklebust 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
8261570c1e4SChuck Lever 
8271da177e4SLinus Torvalds 	if (timer) {
8281da177e4SLinus Torvalds 		if (req->rq_ntrans == 1)
829ff839970SChuck Lever 			rpc_update_rtt(rtt, timer, m);
8301570c1e4SChuck Lever 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
8311da177e4SLinus Torvalds 	}
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341570c1e4SChuck Lever /**
8351570c1e4SChuck Lever  * xprt_complete_rqst - called when reply processing is complete
8361570c1e4SChuck Lever  * @task: RPC request that recently completed
8371570c1e4SChuck Lever  * @copied: actual number of bytes received from the transport
8381570c1e4SChuck Lever  *
8391570c1e4SChuck Lever  * Caller holds transport lock.
8401570c1e4SChuck Lever  */
8411570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied)
8421570c1e4SChuck Lever {
8431570c1e4SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
844fda13939STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
8451da177e4SLinus Torvalds 
8461570c1e4SChuck Lever 	dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
8471570c1e4SChuck Lever 			task->tk_pid, ntohl(req->rq_xid), copied);
8483705ad64SJeff Layton 	trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
8491da177e4SLinus Torvalds 
850fda13939STrond Myklebust 	xprt->stat.recvs++;
851d60dbb20STrond Myklebust 	req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
852bbc72ceaSChuck Lever 	if (xprt->ops->timer != NULL)
853bbc72ceaSChuck Lever 		xprt_update_rtt(task);
854ef759a2eSChuck Lever 
8551da177e4SLinus Torvalds 	list_del_init(&req->rq_list);
8561e799b67STrond Myklebust 	req->rq_private_buf.len = copied;
857dd2b63d0SRicardo Labiaga 	/* Ensure all writes are done before we update */
858dd2b63d0SRicardo Labiaga 	/* req->rq_reply_bytes_recvd */
85943ac3f29STrond Myklebust 	smp_wmb();
860dd2b63d0SRicardo Labiaga 	req->rq_reply_bytes_recvd = copied;
861fda13939STrond Myklebust 	rpc_wake_up_queued_task(&xprt->pending, task);
8621da177e4SLinus Torvalds }
86312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst);
8641da177e4SLinus Torvalds 
86546c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct rpc_rqst *req = task->tk_rqstp;
8681da177e4SLinus Torvalds 	struct rpc_xprt *xprt = req->rq_xprt;
8691da177e4SLinus Torvalds 
8705d00837bSTrond Myklebust 	if (task->tk_status != -ETIMEDOUT)
8715d00837bSTrond Myklebust 		return;
87246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
87346c0ee8bSChuck Lever 
8745d00837bSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
875dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
87646c0ee8bSChuck Lever 		if (xprt->ops->timer)
8776a24dfb6STrond Myklebust 			xprt->ops->timer(xprt, task);
8785d00837bSTrond Myklebust 	} else
8795d00837bSTrond Myklebust 		task->tk_status = 0;
8805d00837bSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
8834cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt)
8844cfc7e60SRahul Iyer {
8854cfc7e60SRahul Iyer 	return xprt->idle_timeout != 0;
8864cfc7e60SRahul Iyer }
8874cfc7e60SRahul Iyer 
8889903cd1cSChuck Lever /**
8899903cd1cSChuck Lever  * xprt_prepare_transmit - reserve the transport before sending a request
8909903cd1cSChuck Lever  * @task: RPC task about to send a request
8919903cd1cSChuck Lever  *
8921da177e4SLinus Torvalds  */
89390051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
8961da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
89790051ea7STrond Myklebust 	bool ret = false;
8981da177e4SLinus Torvalds 
89946121cf7SChuck Lever 	dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
9001da177e4SLinus Torvalds 
9014a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
9028a19a0b6STrond Myklebust 	if (!req->rq_bytes_sent) {
9038a19a0b6STrond Myklebust 		if (req->rq_reply_bytes_recvd) {
90490051ea7STrond Myklebust 			task->tk_status = req->rq_reply_bytes_recvd;
9051da177e4SLinus Torvalds 			goto out_unlock;
9061da177e4SLinus Torvalds 		}
9078a19a0b6STrond Myklebust 		if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
9088a19a0b6STrond Myklebust 		    && xprt_connected(xprt)
9098a19a0b6STrond Myklebust 		    && req->rq_connect_cookie == xprt->connect_cookie) {
9108a19a0b6STrond Myklebust 			xprt->ops->set_retrans_timeout(task);
9118a19a0b6STrond Myklebust 			rpc_sleep_on(&xprt->pending, task, xprt_timer);
9128a19a0b6STrond Myklebust 			goto out_unlock;
9138a19a0b6STrond Myklebust 		}
9148a19a0b6STrond Myklebust 	}
91590051ea7STrond Myklebust 	if (!xprt->ops->reserve_xprt(xprt, task)) {
91690051ea7STrond Myklebust 		task->tk_status = -EAGAIN;
91790051ea7STrond Myklebust 		goto out_unlock;
91890051ea7STrond Myklebust 	}
91990051ea7STrond Myklebust 	ret = true;
9201da177e4SLinus Torvalds out_unlock:
9214a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
92290051ea7STrond Myklebust 	return ret;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
925e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task)
9265e5ce5beSTrond Myklebust {
927343952faSRahul Iyer 	xprt_release_write(task->tk_rqstp->rq_xprt, task);
9285e5ce5beSTrond Myklebust }
9295e5ce5beSTrond Myklebust 
9309903cd1cSChuck Lever /**
9319903cd1cSChuck Lever  * xprt_transmit - send an RPC request on a transport
9329903cd1cSChuck Lever  * @task: controlling RPC task
9339903cd1cSChuck Lever  *
9349903cd1cSChuck Lever  * We have to copy the iovec because sendmsg fiddles with its contents.
9359903cd1cSChuck Lever  */
9369903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
9391da177e4SLinus Torvalds 	struct rpc_xprt	*xprt = req->rq_xprt;
94015a45206SAndy Adamson 	int status, numreqs;
9411da177e4SLinus Torvalds 
94246121cf7SChuck Lever 	dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
9431da177e4SLinus Torvalds 
944dd2b63d0SRicardo Labiaga 	if (!req->rq_reply_bytes_recvd) {
94555ae1aabSRicardo Labiaga 		if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
94655ae1aabSRicardo Labiaga 			/*
94755ae1aabSRicardo Labiaga 			 * Add to the list only if we're expecting a reply
94855ae1aabSRicardo Labiaga 			 */
9494a0f8c04SChuck Lever 			spin_lock_bh(&xprt->transport_lock);
9501da177e4SLinus Torvalds 			/* Update the softirq receive buffer */
9511da177e4SLinus Torvalds 			memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
9521da177e4SLinus Torvalds 					sizeof(req->rq_private_buf));
9531da177e4SLinus Torvalds 			/* Add request to the receive list */
9541da177e4SLinus Torvalds 			list_add_tail(&req->rq_list, &xprt->recv);
9554a0f8c04SChuck Lever 			spin_unlock_bh(&xprt->transport_lock);
9561da177e4SLinus Torvalds 			xprt_reset_majortimeo(req);
9570f9dc2b1STrond Myklebust 			/* Turn off autodisconnect */
9580f9dc2b1STrond Myklebust 			del_singleshot_timer_sync(&xprt->timer);
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 	} else if (!req->rq_bytes_sent)
9611da177e4SLinus Torvalds 		return;
9621da177e4SLinus Torvalds 
963ff839970SChuck Lever 	req->rq_xtime = ktime_get();
964a246b010SChuck Lever 	status = xprt->ops->send_request(task);
9653705ad64SJeff Layton 	trace_xprt_transmit(xprt, req->rq_xid, status);
966c8485e4dSTrond Myklebust 	if (status != 0) {
967c8485e4dSTrond Myklebust 		task->tk_status = status;
968c8485e4dSTrond Myklebust 		return;
969c8485e4dSTrond Myklebust 	}
9704a068258SChuck Lever 	xprt_inject_disconnect(xprt);
971c8485e4dSTrond Myklebust 
97246121cf7SChuck Lever 	dprintk("RPC: %5u xmit complete\n", task->tk_pid);
973468f8613SBryan Schumaker 	task->tk_flags |= RPC_TASK_SENT;
974fe3aca29SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
975262ca07dSChuck Lever 
976fe3aca29SChuck Lever 	xprt->ops->set_retrans_timeout(task);
977262ca07dSChuck Lever 
97815a45206SAndy Adamson 	numreqs = atomic_read(&xprt->num_reqs);
97915a45206SAndy Adamson 	if (numreqs > xprt->stat.max_slots)
98015a45206SAndy Adamson 		xprt->stat.max_slots = numreqs;
981262ca07dSChuck Lever 	xprt->stat.sends++;
982262ca07dSChuck Lever 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
983262ca07dSChuck Lever 	xprt->stat.bklog_u += xprt->backlog.qlen;
98415a45206SAndy Adamson 	xprt->stat.sending_u += xprt->sending.qlen;
98515a45206SAndy Adamson 	xprt->stat.pending_u += xprt->pending.qlen;
986262ca07dSChuck Lever 
987fe3aca29SChuck Lever 	/* Don't race with disconnect */
988fe3aca29SChuck Lever 	if (!xprt_connected(xprt))
989fe3aca29SChuck Lever 		task->tk_status = -ENOTCONN;
9900a660521STrond Myklebust 	else {
99155ae1aabSRicardo Labiaga 		/*
99255ae1aabSRicardo Labiaga 		 * Sleep on the pending queue since
99355ae1aabSRicardo Labiaga 		 * we're expecting a reply.
99455ae1aabSRicardo Labiaga 		 */
9950a660521STrond Myklebust 		if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task))
9965d00837bSTrond Myklebust 			rpc_sleep_on(&xprt->pending, task, xprt_timer);
9970a660521STrond Myklebust 		req->rq_connect_cookie = xprt->connect_cookie;
99855ae1aabSRicardo Labiaga 	}
999fe3aca29SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
1002ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1003ba60eb25STrond Myklebust {
1004ba60eb25STrond Myklebust 	set_bit(XPRT_CONGESTED, &xprt->state);
1005ba60eb25STrond Myklebust 	rpc_sleep_on(&xprt->backlog, task, NULL);
1006ba60eb25STrond Myklebust }
1007ba60eb25STrond Myklebust 
1008ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1009ba60eb25STrond Myklebust {
1010ba60eb25STrond Myklebust 	if (rpc_wake_up_next(&xprt->backlog) == NULL)
1011ba60eb25STrond Myklebust 		clear_bit(XPRT_CONGESTED, &xprt->state);
1012ba60eb25STrond Myklebust }
1013ba60eb25STrond Myklebust 
1014ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1015ba60eb25STrond Myklebust {
1016ba60eb25STrond Myklebust 	bool ret = false;
1017ba60eb25STrond Myklebust 
1018ba60eb25STrond Myklebust 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1019ba60eb25STrond Myklebust 		goto out;
1020ba60eb25STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1021ba60eb25STrond Myklebust 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1022ba60eb25STrond Myklebust 		rpc_sleep_on(&xprt->backlog, task, NULL);
1023ba60eb25STrond Myklebust 		ret = true;
1024ba60eb25STrond Myklebust 	}
1025ba60eb25STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1026ba60eb25STrond Myklebust out:
1027ba60eb25STrond Myklebust 	return ret;
1028ba60eb25STrond Myklebust }
1029ba60eb25STrond Myklebust 
1030d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
1031d9ba131dSTrond Myklebust {
1032d9ba131dSTrond Myklebust 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1033d9ba131dSTrond Myklebust 
1034d9ba131dSTrond Myklebust 	if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
1035d9ba131dSTrond Myklebust 		goto out;
1036d9ba131dSTrond Myklebust 	req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
1037d9ba131dSTrond Myklebust 	if (req != NULL)
1038d9ba131dSTrond Myklebust 		goto out;
1039d9ba131dSTrond Myklebust 	atomic_dec(&xprt->num_reqs);
1040d9ba131dSTrond Myklebust 	req = ERR_PTR(-ENOMEM);
1041d9ba131dSTrond Myklebust out:
1042d9ba131dSTrond Myklebust 	return req;
1043d9ba131dSTrond Myklebust }
1044d9ba131dSTrond Myklebust 
1045d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1046d9ba131dSTrond Myklebust {
1047d9ba131dSTrond Myklebust 	if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
1048d9ba131dSTrond Myklebust 		kfree(req);
1049d9ba131dSTrond Myklebust 		return true;
1050d9ba131dSTrond Myklebust 	}
1051d9ba131dSTrond Myklebust 	return false;
1052d9ba131dSTrond Myklebust }
1053d9ba131dSTrond Myklebust 
1054f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
10551da177e4SLinus Torvalds {
1056d9ba131dSTrond Myklebust 	struct rpc_rqst *req;
10571da177e4SLinus Torvalds 
1058f39c1bfbSTrond Myklebust 	spin_lock(&xprt->reserve_lock);
10591da177e4SLinus Torvalds 	if (!list_empty(&xprt->free)) {
1060d9ba131dSTrond Myklebust 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1061d9ba131dSTrond Myklebust 		list_del(&req->rq_list);
1062d9ba131dSTrond Myklebust 		goto out_init_req;
1063d9ba131dSTrond Myklebust 	}
10646b343099SJeff Layton 	req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN);
1065d9ba131dSTrond Myklebust 	if (!IS_ERR(req))
1066d9ba131dSTrond Myklebust 		goto out_init_req;
1067d9ba131dSTrond Myklebust 	switch (PTR_ERR(req)) {
1068d9ba131dSTrond Myklebust 	case -ENOMEM:
1069d9ba131dSTrond Myklebust 		dprintk("RPC:       dynamic allocation of request slot "
1070d9ba131dSTrond Myklebust 				"failed! Retrying\n");
10711afeaf5cSTrond Myklebust 		task->tk_status = -ENOMEM;
1072d9ba131dSTrond Myklebust 		break;
1073d9ba131dSTrond Myklebust 	case -EAGAIN:
1074ba60eb25STrond Myklebust 		xprt_add_backlog(xprt, task);
1075d9ba131dSTrond Myklebust 		dprintk("RPC:       waiting for request slot\n");
10761afeaf5cSTrond Myklebust 	default:
1077d9ba131dSTrond Myklebust 		task->tk_status = -EAGAIN;
10781afeaf5cSTrond Myklebust 	}
1079f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1080d9ba131dSTrond Myklebust 	return;
1081d9ba131dSTrond Myklebust out_init_req:
1082d9ba131dSTrond Myklebust 	task->tk_status = 0;
10831da177e4SLinus Torvalds 	task->tk_rqstp = req;
10841da177e4SLinus Torvalds 	xprt_request_init(task, xprt);
1085f39c1bfbSTrond Myklebust 	spin_unlock(&xprt->reserve_lock);
10861da177e4SLinus Torvalds }
1087f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1088f39c1bfbSTrond Myklebust 
1089f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1090f39c1bfbSTrond Myklebust {
1091f39c1bfbSTrond Myklebust 	/* Note: grabbing the xprt_lock_write() ensures that we throttle
1092f39c1bfbSTrond Myklebust 	 * new slot allocation if the transport is congested (i.e. when
1093f39c1bfbSTrond Myklebust 	 * reconnecting a stream transport or when out of socket write
1094f39c1bfbSTrond Myklebust 	 * buffer space).
1095f39c1bfbSTrond Myklebust 	 */
1096f39c1bfbSTrond Myklebust 	if (xprt_lock_write(xprt, task)) {
1097f39c1bfbSTrond Myklebust 		xprt_alloc_slot(xprt, task);
1098f39c1bfbSTrond Myklebust 		xprt_release_write(xprt, task);
1099f39c1bfbSTrond Myklebust 	}
1100f39c1bfbSTrond Myklebust }
1101f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
11021da177e4SLinus Torvalds 
1103ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1104ee5ebe85STrond Myklebust {
1105ee5ebe85STrond Myklebust 	spin_lock(&xprt->reserve_lock);
1106c25573b5STrond Myklebust 	if (!xprt_dynamic_free_slot(xprt, req)) {
1107c25573b5STrond Myklebust 		memset(req, 0, sizeof(*req));	/* mark unused */
1108ee5ebe85STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
1109c25573b5STrond Myklebust 	}
1110ba60eb25STrond Myklebust 	xprt_wake_up_backlog(xprt);
1111ee5ebe85STrond Myklebust 	spin_unlock(&xprt->reserve_lock);
1112ee5ebe85STrond Myklebust }
1113ee5ebe85STrond Myklebust 
111421de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt)
111521de0a95STrond Myklebust {
111621de0a95STrond Myklebust 	struct rpc_rqst *req;
111721de0a95STrond Myklebust 	while (!list_empty(&xprt->free)) {
111821de0a95STrond Myklebust 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
111921de0a95STrond Myklebust 		list_del(&req->rq_list);
112021de0a95STrond Myklebust 		kfree(req);
112121de0a95STrond Myklebust 	}
112221de0a95STrond Myklebust }
112321de0a95STrond Myklebust 
1124d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1125d9ba131dSTrond Myklebust 		unsigned int num_prealloc,
1126d9ba131dSTrond Myklebust 		unsigned int max_alloc)
1127bd1722d4SPavel Emelyanov {
1128bd1722d4SPavel Emelyanov 	struct rpc_xprt *xprt;
112921de0a95STrond Myklebust 	struct rpc_rqst *req;
113021de0a95STrond Myklebust 	int i;
1131bd1722d4SPavel Emelyanov 
1132bd1722d4SPavel Emelyanov 	xprt = kzalloc(size, GFP_KERNEL);
1133bd1722d4SPavel Emelyanov 	if (xprt == NULL)
1134bd1722d4SPavel Emelyanov 		goto out;
1135bd1722d4SPavel Emelyanov 
113621de0a95STrond Myklebust 	xprt_init(xprt, net);
113721de0a95STrond Myklebust 
113821de0a95STrond Myklebust 	for (i = 0; i < num_prealloc; i++) {
113921de0a95STrond Myklebust 		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
114021de0a95STrond Myklebust 		if (!req)
11418313164cSwangweidong 			goto out_free;
114221de0a95STrond Myklebust 		list_add(&req->rq_list, &xprt->free);
114321de0a95STrond Myklebust 	}
1144d9ba131dSTrond Myklebust 	if (max_alloc > num_prealloc)
1145d9ba131dSTrond Myklebust 		xprt->max_reqs = max_alloc;
1146d9ba131dSTrond Myklebust 	else
114721de0a95STrond Myklebust 		xprt->max_reqs = num_prealloc;
1148d9ba131dSTrond Myklebust 	xprt->min_reqs = num_prealloc;
1149d9ba131dSTrond Myklebust 	atomic_set(&xprt->num_reqs, num_prealloc);
1150bd1722d4SPavel Emelyanov 
1151bd1722d4SPavel Emelyanov 	return xprt;
1152bd1722d4SPavel Emelyanov 
1153bd1722d4SPavel Emelyanov out_free:
115421de0a95STrond Myklebust 	xprt_free(xprt);
1155bd1722d4SPavel Emelyanov out:
1156bd1722d4SPavel Emelyanov 	return NULL;
1157bd1722d4SPavel Emelyanov }
1158bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc);
1159bd1722d4SPavel Emelyanov 
1160e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt)
1161e204e621SPavel Emelyanov {
116237aa2133SPavel Emelyanov 	put_net(xprt->xprt_net);
116321de0a95STrond Myklebust 	xprt_free_all_slots(xprt);
1164e204e621SPavel Emelyanov 	kfree(xprt);
1165e204e621SPavel Emelyanov }
1166e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free);
1167e204e621SPavel Emelyanov 
11689903cd1cSChuck Lever /**
11699903cd1cSChuck Lever  * xprt_reserve - allocate an RPC request slot
11709903cd1cSChuck Lever  * @task: RPC task requesting a slot allocation
11719903cd1cSChuck Lever  *
1172ba60eb25STrond Myklebust  * If the transport is marked as being congested, or if no more
1173ba60eb25STrond Myklebust  * slots are available, place the task on the transport's
11749903cd1cSChuck Lever  * backlog queue.
11759903cd1cSChuck Lever  */
11769903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task)
11771da177e4SLinus Torvalds {
117845bc0dceSTrond Myklebust 	struct rpc_xprt	*xprt;
11791da177e4SLinus Torvalds 
118043cedbf0STrond Myklebust 	task->tk_status = 0;
118143cedbf0STrond Myklebust 	if (task->tk_rqstp != NULL)
118243cedbf0STrond Myklebust 		return;
118343cedbf0STrond Myklebust 
118443cedbf0STrond Myklebust 	task->tk_timeout = 0;
118543cedbf0STrond Myklebust 	task->tk_status = -EAGAIN;
118645bc0dceSTrond Myklebust 	rcu_read_lock();
118745bc0dceSTrond Myklebust 	xprt = rcu_dereference(task->tk_client->cl_xprt);
1188ba60eb25STrond Myklebust 	if (!xprt_throttle_congested(xprt, task))
1189ba60eb25STrond Myklebust 		xprt->ops->alloc_slot(xprt, task);
1190ba60eb25STrond Myklebust 	rcu_read_unlock();
1191ba60eb25STrond Myklebust }
1192ba60eb25STrond Myklebust 
1193ba60eb25STrond Myklebust /**
1194ba60eb25STrond Myklebust  * xprt_retry_reserve - allocate an RPC request slot
1195ba60eb25STrond Myklebust  * @task: RPC task requesting a slot allocation
1196ba60eb25STrond Myklebust  *
1197ba60eb25STrond Myklebust  * If no more slots are available, place the task on the transport's
1198ba60eb25STrond Myklebust  * backlog queue.
1199ba60eb25STrond Myklebust  * Note that the only difference with xprt_reserve is that we now
1200ba60eb25STrond Myklebust  * ignore the value of the XPRT_CONGESTED flag.
1201ba60eb25STrond Myklebust  */
1202ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task)
1203ba60eb25STrond Myklebust {
1204ba60eb25STrond Myklebust 	struct rpc_xprt	*xprt;
1205ba60eb25STrond Myklebust 
1206ba60eb25STrond Myklebust 	task->tk_status = 0;
1207ba60eb25STrond Myklebust 	if (task->tk_rqstp != NULL)
1208ba60eb25STrond Myklebust 		return;
1209ba60eb25STrond Myklebust 
1210ba60eb25STrond Myklebust 	task->tk_timeout = 0;
1211ba60eb25STrond Myklebust 	task->tk_status = -EAGAIN;
1212ba60eb25STrond Myklebust 	rcu_read_lock();
1213ba60eb25STrond Myklebust 	xprt = rcu_dereference(task->tk_client->cl_xprt);
1214f39c1bfbSTrond Myklebust 	xprt->ops->alloc_slot(xprt, task);
121545bc0dceSTrond Myklebust 	rcu_read_unlock();
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
1218d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
12191da177e4SLinus Torvalds {
12200eae88f3SEric Dumazet 	return (__force __be32)xprt->xid++;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt)
12241da177e4SLinus Torvalds {
122563862b5bSAruna-Hewapathirane 	xprt->xid = prandom_u32();
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
12289903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	struct rpc_rqst	*req = task->tk_rqstp;
12311da177e4SLinus Torvalds 
1232d9ba131dSTrond Myklebust 	INIT_LIST_HEAD(&req->rq_list);
1233ba7392bbSTrond Myklebust 	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
12341da177e4SLinus Torvalds 	req->rq_task	= task;
12351da177e4SLinus Torvalds 	req->rq_xprt    = xprt;
123602107148SChuck Lever 	req->rq_buffer  = NULL;
12371da177e4SLinus Torvalds 	req->rq_xid     = xprt_alloc_xid(xprt);
12380a660521STrond Myklebust 	req->rq_connect_cookie = xprt->connect_cookie - 1;
123992551948STrond Myklebust 	req->rq_bytes_sent = 0;
124092551948STrond Myklebust 	req->rq_snd_buf.len = 0;
124192551948STrond Myklebust 	req->rq_snd_buf.buflen = 0;
124292551948STrond Myklebust 	req->rq_rcv_buf.len = 0;
124392551948STrond Myklebust 	req->rq_rcv_buf.buflen = 0;
1244ead5e1c2SJ. Bruce Fields 	req->rq_release_snd_buf = NULL;
1245da45828eSTrond Myklebust 	xprt_reset_majortimeo(req);
124646121cf7SChuck Lever 	dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
12471da177e4SLinus Torvalds 			req, ntohl(req->rq_xid));
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12509903cd1cSChuck Lever /**
12519903cd1cSChuck Lever  * xprt_release - release an RPC request slot
12529903cd1cSChuck Lever  * @task: task which is finished with the slot
12539903cd1cSChuck Lever  *
12541da177e4SLinus Torvalds  */
12559903cd1cSChuck Lever void xprt_release(struct rpc_task *task)
12561da177e4SLinus Torvalds {
125755ae1aabSRicardo Labiaga 	struct rpc_xprt	*xprt;
125887ed5003STrond Myklebust 	struct rpc_rqst	*req = task->tk_rqstp;
12591da177e4SLinus Torvalds 
126087ed5003STrond Myklebust 	if (req == NULL) {
126187ed5003STrond Myklebust 		if (task->tk_client) {
126287ed5003STrond Myklebust 			rcu_read_lock();
126387ed5003STrond Myklebust 			xprt = rcu_dereference(task->tk_client->cl_xprt);
126487ed5003STrond Myklebust 			if (xprt->snd_task == task)
126587ed5003STrond Myklebust 				xprt_release_write(xprt, task);
126687ed5003STrond Myklebust 			rcu_read_unlock();
126787ed5003STrond Myklebust 		}
12681da177e4SLinus Torvalds 		return;
126987ed5003STrond Myklebust 	}
127055ae1aabSRicardo Labiaga 
127155ae1aabSRicardo Labiaga 	xprt = req->rq_xprt;
12720a702195SWeston Andros Adamson 	if (task->tk_ops->rpc_count_stats != NULL)
12730a702195SWeston Andros Adamson 		task->tk_ops->rpc_count_stats(task, task->tk_calldata);
12740a702195SWeston Andros Adamson 	else if (task->tk_client)
12750a702195SWeston Andros Adamson 		rpc_count_iostats(task, task->tk_client->cl_metrics);
12764a0f8c04SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
127749e9a890SChuck Lever 	xprt->ops->release_xprt(xprt, task);
1278a58dd398SChuck Lever 	if (xprt->ops->release_request)
1279a58dd398SChuck Lever 		xprt->ops->release_request(task);
12801da177e4SLinus Torvalds 	if (!list_empty(&req->rq_list))
12811da177e4SLinus Torvalds 		list_del(&req->rq_list);
12821da177e4SLinus Torvalds 	xprt->last_used = jiffies;
12834cfc7e60SRahul Iyer 	if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
1284a246b010SChuck Lever 		mod_timer(&xprt->timer,
128503bf4b70SChuck Lever 				xprt->last_used + xprt->idle_timeout);
12864a0f8c04SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
1287ee5ebe85STrond Myklebust 	if (req->rq_buffer)
1288c5a4dd8bSChuck Lever 		xprt->ops->buf_free(req->rq_buffer);
12894a068258SChuck Lever 	xprt_inject_disconnect(xprt);
1290a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
1291a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
12921da177e4SLinus Torvalds 	task->tk_rqstp = NULL;
1293ead5e1c2SJ. Bruce Fields 	if (req->rq_release_snd_buf)
1294ead5e1c2SJ. Bruce Fields 		req->rq_release_snd_buf(req);
129555ae1aabSRicardo Labiaga 
129646121cf7SChuck Lever 	dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1297ee5ebe85STrond Myklebust 	if (likely(!bc_prealloc(req)))
1298ee5ebe85STrond Myklebust 		xprt_free_slot(xprt, req);
1299ee5ebe85STrond Myklebust 	else
1300c9acb42eSTrond Myklebust 		xprt_free_bc_request(req);
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
130321de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1304c2866763SChuck Lever {
130521de0a95STrond Myklebust 	atomic_set(&xprt->count, 1);
1306c2866763SChuck Lever 
1307c2866763SChuck Lever 	spin_lock_init(&xprt->transport_lock);
1308c2866763SChuck Lever 	spin_lock_init(&xprt->reserve_lock);
1309c2866763SChuck Lever 
1310c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->free);
1311c2866763SChuck Lever 	INIT_LIST_HEAD(&xprt->recv);
13129e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1313f9acac1aSRicardo Labiaga 	spin_lock_init(&xprt->bc_pa_lock);
1314f9acac1aSRicardo Labiaga 	INIT_LIST_HEAD(&xprt->bc_pa_list);
13159e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1316f9acac1aSRicardo Labiaga 
1317c2866763SChuck Lever 	xprt->last_used = jiffies;
1318c2866763SChuck Lever 	xprt->cwnd = RPC_INITCWND;
1319a509050bSChuck Lever 	xprt->bind_index = 0;
1320c2866763SChuck Lever 
1321c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1322c2866763SChuck Lever 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
132334006ceeSTrond Myklebust 	rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1324c2866763SChuck Lever 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1325c2866763SChuck Lever 
1326c2866763SChuck Lever 	xprt_init_xid(xprt);
1327c2866763SChuck Lever 
132821de0a95STrond Myklebust 	xprt->xprt_net = get_net(net);
13298d9266ffSTrond Myklebust }
13308d9266ffSTrond Myklebust 
13318d9266ffSTrond Myklebust /**
13328d9266ffSTrond Myklebust  * xprt_create_transport - create an RPC transport
13338d9266ffSTrond Myklebust  * @args: rpc transport creation arguments
13348d9266ffSTrond Myklebust  *
13358d9266ffSTrond Myklebust  */
13368d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
13378d9266ffSTrond Myklebust {
13388d9266ffSTrond Myklebust 	struct rpc_xprt	*xprt;
13398d9266ffSTrond Myklebust 	struct xprt_class *t;
13408d9266ffSTrond Myklebust 
13418d9266ffSTrond Myklebust 	spin_lock(&xprt_list_lock);
13428d9266ffSTrond Myklebust 	list_for_each_entry(t, &xprt_list, list) {
13438d9266ffSTrond Myklebust 		if (t->ident == args->ident) {
13448d9266ffSTrond Myklebust 			spin_unlock(&xprt_list_lock);
13458d9266ffSTrond Myklebust 			goto found;
13468d9266ffSTrond Myklebust 		}
13478d9266ffSTrond Myklebust 	}
13488d9266ffSTrond Myklebust 	spin_unlock(&xprt_list_lock);
13493c45ddf8SChuck Lever 	dprintk("RPC: transport (%d) not supported\n", args->ident);
13508d9266ffSTrond Myklebust 	return ERR_PTR(-EIO);
13518d9266ffSTrond Myklebust 
13528d9266ffSTrond Myklebust found:
13538d9266ffSTrond Myklebust 	xprt = t->setup(args);
13548d9266ffSTrond Myklebust 	if (IS_ERR(xprt)) {
13558d9266ffSTrond Myklebust 		dprintk("RPC:       xprt_create_transport: failed, %ld\n",
13568d9266ffSTrond Myklebust 				-PTR_ERR(xprt));
135721de0a95STrond Myklebust 		goto out;
13588d9266ffSTrond Myklebust 	}
135933d90ac0SJ. Bruce Fields 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
136033d90ac0SJ. Bruce Fields 		xprt->idle_timeout = 0;
136121de0a95STrond Myklebust 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
136221de0a95STrond Myklebust 	if (xprt_has_timer(xprt))
136321de0a95STrond Myklebust 		setup_timer(&xprt->timer, xprt_init_autodisconnect,
136421de0a95STrond Myklebust 			    (unsigned long)xprt);
136521de0a95STrond Myklebust 	else
136621de0a95STrond Myklebust 		init_timer(&xprt->timer);
13674e0038b6STrond Myklebust 
13684e0038b6STrond Myklebust 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
13694e0038b6STrond Myklebust 		xprt_destroy(xprt);
13704e0038b6STrond Myklebust 		return ERR_PTR(-EINVAL);
13714e0038b6STrond Myklebust 	}
13724e0038b6STrond Myklebust 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
13734e0038b6STrond Myklebust 	if (xprt->servername == NULL) {
13744e0038b6STrond Myklebust 		xprt_destroy(xprt);
13754e0038b6STrond Myklebust 		return ERR_PTR(-ENOMEM);
13764e0038b6STrond Myklebust 	}
13774e0038b6STrond Myklebust 
13783f940098SJeff Layton 	rpc_xprt_debugfs_register(xprt);
1379388f0c77SJeff Layton 
1380c2866763SChuck Lever 	dprintk("RPC:       created transport %p with %u slots\n", xprt,
1381c2866763SChuck Lever 			xprt->max_reqs);
138221de0a95STrond Myklebust out:
1383c2866763SChuck Lever 	return xprt;
1384c2866763SChuck Lever }
1385c2866763SChuck Lever 
13869903cd1cSChuck Lever /**
13879903cd1cSChuck Lever  * xprt_destroy - destroy an RPC transport, killing off all requests.
1388a8de240aSTrond Myklebust  * @xprt: transport to destroy
13899903cd1cSChuck Lever  *
13901da177e4SLinus Torvalds  */
1391a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	dprintk("RPC:       destroying transport %p\n", xprt);
13940065db32STrond Myklebust 	del_timer_sync(&xprt->timer);
1395c8541ecdSChuck Lever 
1396388f0c77SJeff Layton 	rpc_xprt_debugfs_unregister(xprt);
1397f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->binding);
1398f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->pending);
1399f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->sending);
1400f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&xprt->backlog);
1401c3ae62aeSJ. Bruce Fields 	cancel_work_sync(&xprt->task_cleanup);
14024e0038b6STrond Myklebust 	kfree(xprt->servername);
1403c8541ecdSChuck Lever 	/*
1404c8541ecdSChuck Lever 	 * Tear down transport state and free the rpc_xprt
1405c8541ecdSChuck Lever 	 */
1406a246b010SChuck Lever 	xprt->ops->destroy(xprt);
14076b6ca86bSTrond Myklebust }
14081da177e4SLinus Torvalds 
14096b6ca86bSTrond Myklebust /**
14106b6ca86bSTrond Myklebust  * xprt_put - release a reference to an RPC transport.
14116b6ca86bSTrond Myklebust  * @xprt: pointer to the transport
14126b6ca86bSTrond Myklebust  *
14136b6ca86bSTrond Myklebust  */
14146b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt)
14156b6ca86bSTrond Myklebust {
1416a8de240aSTrond Myklebust 	if (atomic_dec_and_test(&xprt->count))
1417a8de240aSTrond Myklebust 		xprt_destroy(xprt);
14186b6ca86bSTrond Myklebust }
1419