blob: ccd583a46ff62d27ad40c7a712b47887038d6e70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/xprt.c
3 *
4 * This is a generic RPC call interface supporting congestion avoidance,
5 * and asynchronous calls.
6 *
7 * The interface works like this:
8 *
9 * - When a process places a call, it allocates a request slot if
10 * one is available. Otherwise, it sleeps on the backlog queue
11 * (xprt_reserve).
12 * - Next, the caller puts together the RPC message, stuffs it into
Chuck Lever55aa4f52005-08-11 16:25:47 -040013 * the request struct, and calls xprt_transmit().
14 * - xprt_transmit sends the message and installs the caller on the
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040015 * transport's wait list. At the same time, if a reply is expected,
16 * it installs a timer that is run after the packet's timeout has
17 * expired.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040019 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * caller is woken up, and the timer removed.
21 * - When no reply arrives within the timeout interval, the timer is
22 * fired by the kernel and runs xprt_timer(). It either adjusts the
23 * timeout values (minor timeout) or wakes up the caller with a status
24 * of -ETIMEDOUT.
25 * - When the caller receives a notification from RPC that a reply arrived,
26 * it should release the RPC slot, and process the reply.
27 * If the call timed out, it may choose to retry the operation by
28 * adjusting the initial timeout value, and simply calling rpc_call
29 * again.
30 *
31 * Support for async RPC is done through a set of RPC-specific scheduling
32 * primitives that `transparently' work for processes as well as async
33 * tasks that rely on callbacks.
34 *
35 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
Chuck Lever55aa4f52005-08-11 16:25:47 -040036 *
37 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/module.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040043#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040045#include <linux/net.h>
Chuck Leverff839972010-05-07 13:34:47 -040046#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Chuck Levera246b012005-08-11 16:25:23 -040048#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050049#include <linux/sunrpc/metrics.h>
Trond Myklebustc9acb422010-03-19 15:36:22 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040052#include "sunrpc.h"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Local variables
56 */
57
58#ifdef RPC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070059# define RPCDBG_FACILITY RPCDBG_XPRT
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * Local functions
64 */
65static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void xprt_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
68
Jiri Slaby5ba03e82007-11-22 19:40:22 +080069static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040070static LIST_HEAD(xprt_list);
71
Chuck Lever555ee3a2005-08-25 16:25:54 -070072/*
73 * The transport code maintains an estimate on the maximum number of out-
74 * standing RPC requests, using a smoothed version of the congestion
75 * avoidance implemented in 44BSD. This is basically the Van Jacobson
76 * congestion algorithm: If a retransmit occurs, the congestion window is
77 * halved; otherwise, it is incremented by 1/cwnd when
78 *
79 * - a reply is received and
80 * - a full number of requests are outstanding and
81 * - the congestion window hasn't been updated recently.
82 */
83#define RPC_CWNDSHIFT (8U)
84#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
85#define RPC_INITCWND RPC_CWNDSCALE
86#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
87
88#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Chuck Lever12a80462005-08-25 16:25:51 -070090/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040091 * xprt_register_transport - register a transport implementation
92 * @transport: transport to register
93 *
94 * If a transport implementation is loaded as a kernel module, it can
95 * call this interface to make itself known to the RPC client.
96 *
97 * Returns:
98 * 0: transport successfully registered
99 * -EEXIST: transport already registered
100 * -EINVAL: transport module being unloaded
101 */
102int xprt_register_transport(struct xprt_class *transport)
103{
104 struct xprt_class *t;
105 int result;
106
107 result = -EEXIST;
108 spin_lock(&xprt_list_lock);
109 list_for_each_entry(t, &xprt_list, list) {
110 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400111 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400112 goto out;
113 }
114
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400115 list_add_tail(&transport->list, &xprt_list);
116 printk(KERN_INFO "RPC: Registered %s transport module.\n",
117 transport->name);
118 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400119
120out:
121 spin_unlock(&xprt_list_lock);
122 return result;
123}
124EXPORT_SYMBOL_GPL(xprt_register_transport);
125
126/**
127 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800128 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400129 *
130 * Returns:
131 * 0: transport successfully unregistered
132 * -ENOENT: transport never registered
133 */
134int xprt_unregister_transport(struct xprt_class *transport)
135{
136 struct xprt_class *t;
137 int result;
138
139 result = 0;
140 spin_lock(&xprt_list_lock);
141 list_for_each_entry(t, &xprt_list, list) {
142 if (t == transport) {
143 printk(KERN_INFO
144 "RPC: Unregistered %s transport module.\n",
145 transport->name);
146 list_del_init(&transport->list);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400147 goto out;
148 }
149 }
150 result = -ENOENT;
151
152out:
153 spin_unlock(&xprt_list_lock);
154 return result;
155}
156EXPORT_SYMBOL_GPL(xprt_unregister_transport);
157
158/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400159 * xprt_load_transport - load a transport implementation
160 * @transport_name: transport to load
161 *
162 * Returns:
163 * 0: transport successfully loaded
164 * -ENOENT: transport module not available
165 */
166int xprt_load_transport(const char *transport_name)
167{
168 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400169 int result;
170
171 result = 0;
172 spin_lock(&xprt_list_lock);
173 list_for_each_entry(t, &xprt_list, list) {
174 if (strcmp(t->name, transport_name) == 0) {
175 spin_unlock(&xprt_list_lock);
176 goto out;
177 }
178 }
179 spin_unlock(&xprt_list_lock);
Alex Riesenef7ffe8f2010-05-24 14:33:05 -0700180 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400181out:
182 return result;
183}
184EXPORT_SYMBOL_GPL(xprt_load_transport);
185
186/**
Chuck Lever12a80462005-08-25 16:25:51 -0700187 * xprt_reserve_xprt - serialize write access to transports
188 * @task: task that is requesting access to the transport
189 *
190 * This prevents mixing the payload of separate requests, and prevents
191 * transport connects from colliding with writes. No congestion control
192 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400194int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Chuck Lever12a80462005-08-25 16:25:51 -0700196 struct rpc_rqst *req = task->tk_rqstp;
197
198 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
199 if (task == xprt->snd_task)
200 return 1;
Chuck Lever12a80462005-08-25 16:25:51 -0700201 goto out_sleep;
202 }
203 xprt->snd_task = task;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400204 if (req != NULL) {
205 req->rq_bytes_sent = 0;
206 req->rq_ntrans++;
207 }
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500208
Chuck Lever12a80462005-08-25 16:25:51 -0700209 return 1;
210
211out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500212 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700213 task->tk_pid, xprt);
214 task->tk_timeout = 0;
215 task->tk_status = -EAGAIN;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400216 if (req != NULL && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500217 rpc_sleep_on(&xprt->resend, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700218 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500219 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700220 return 0;
221}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400222EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700223
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100224static void xprt_clear_locked(struct rpc_xprt *xprt)
225{
226 xprt->snd_task = NULL;
227 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
228 smp_mb__before_clear_bit();
229 clear_bit(XPRT_LOCKED, &xprt->state);
230 smp_mb__after_clear_bit();
231 } else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400232 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100233}
234
Chuck Lever12a80462005-08-25 16:25:51 -0700235/*
236 * xprt_reserve_xprt_cong - serialize write access to transports
237 * @task: task that is requesting access to the transport
238 *
239 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
240 * integrated into the decision of whether a request is allowed to be
241 * woken up and given access to the transport.
242 */
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400243int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever12a80462005-08-25 16:25:51 -0700244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct rpc_rqst *req = task->tk_rqstp;
246
Chuck Lever2226feb2005-08-11 16:25:38 -0400247 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (task == xprt->snd_task)
249 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out_sleep;
251 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400252 if (req == NULL) {
253 xprt->snd_task = task;
254 return 1;
255 }
Chuck Lever12a80462005-08-25 16:25:51 -0700256 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xprt->snd_task = task;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400258 req->rq_bytes_sent = 0;
259 req->rq_ntrans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 1;
261 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100262 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500264 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 task->tk_timeout = 0;
266 task->tk_status = -EAGAIN;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400267 if (req != NULL && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500268 rpc_sleep_on(&xprt->resend, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500270 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400273EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Chuck Lever12a80462005-08-25 16:25:51 -0700275static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int retval;
278
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400279 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400280 retval = xprt->ops->reserve_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400281 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return retval;
283}
284
Chuck Lever12a80462005-08-25 16:25:51 -0700285static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700288 struct rpc_rqst *req;
289
290 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
291 return;
292
293 task = rpc_wake_up_next(&xprt->resend);
294 if (!task) {
295 task = rpc_wake_up_next(&xprt->sending);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400296 if (task == NULL)
Chuck Lever49e9a892005-08-25 16:25:51 -0700297 goto out_unlock;
298 }
299
300 req = task->tk_rqstp;
301 xprt->snd_task = task;
302 if (req) {
303 req->rq_bytes_sent = 0;
304 req->rq_ntrans++;
305 }
306 return;
307
308out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100309 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700310}
311
312static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
313{
314 struct rpc_task *task;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400315 struct rpc_rqst *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Chuck Lever2226feb2005-08-11 16:25:38 -0400317 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700319 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto out_unlock;
321 task = rpc_wake_up_next(&xprt->resend);
322 if (!task) {
323 task = rpc_wake_up_next(&xprt->sending);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400324 if (task == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out_unlock;
326 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400327
328 req = task->tk_rqstp;
329 if (req == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xprt->snd_task = task;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400331 return;
332 }
333 if (__xprt_get_cong(xprt, task)) {
334 xprt->snd_task = task;
335 req->rq_bytes_sent = 0;
336 req->rq_ntrans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return;
338 }
339out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100340 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Chuck Lever49e9a892005-08-25 16:25:51 -0700343/**
344 * xprt_release_xprt - allow other requests to use a transport
345 * @xprt: transport with other tasks potentially waiting
346 * @task: task that is releasing access to the transport
347 *
348 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700350void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100353 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 __xprt_lock_write_next(xprt);
355 }
356}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400357EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Chuck Lever49e9a892005-08-25 16:25:51 -0700359/**
360 * xprt_release_xprt_cong - allow other requests to use a transport
361 * @xprt: transport with other tasks potentially waiting
362 * @task: task that is releasing access to the transport
363 *
364 * Note that "task" can be NULL. Another task is awoken to use the
365 * transport if the transport's congestion window allows it.
366 */
367void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
368{
369 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100370 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700371 __xprt_lock_write_next_cong(xprt);
372 }
373}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400374EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700375
376static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400378 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700379 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400380 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * Van Jacobson congestion avoidance. Check if the congestion window
385 * overflowed. Put the task to sleep if this is the case.
386 */
387static int
388__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
389{
390 struct rpc_rqst *req = task->tk_rqstp;
391
392 if (req->rq_cong)
393 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500394 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 task->tk_pid, xprt->cong, xprt->cwnd);
396 if (RPCXPRT_CONGESTED(xprt))
397 return 0;
398 req->rq_cong = 1;
399 xprt->cong += RPC_CWNDSCALE;
400 return 1;
401}
402
403/*
404 * Adjust the congestion window, and wake up the next task
405 * that has been sleeping due to congestion
406 */
407static void
408__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
409{
410 if (!req->rq_cong)
411 return;
412 req->rq_cong = 0;
413 xprt->cong -= RPC_CWNDSCALE;
Chuck Lever49e9a892005-08-25 16:25:51 -0700414 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Chuck Lever46c0ee82005-08-25 16:25:52 -0700417/**
Chuck Levera58dd392005-08-25 16:25:53 -0700418 * xprt_release_rqst_cong - housekeeping when request is complete
419 * @task: RPC request that recently completed
420 *
421 * Useful for transports that require congestion control.
422 */
423void xprt_release_rqst_cong(struct rpc_task *task)
424{
425 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
426}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400427EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700428
429/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700430 * xprt_adjust_cwnd - adjust transport congestion window
431 * @task: recently completed RPC request used to adjust window
432 * @result: result code of completed RPC request
433 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
435 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700436void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700438 struct rpc_rqst *req = task->tk_rqstp;
439 struct rpc_xprt *xprt = task->tk_xprt;
440 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (result >= 0 && cwnd <= xprt->cong) {
443 /* The (cwnd >> 1) term makes sure
444 * the result gets rounded properly. */
445 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
446 if (cwnd > RPC_MAXCWND(xprt))
447 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700448 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 } else if (result == -ETIMEDOUT) {
450 cwnd >>= 1;
451 if (cwnd < RPC_CWNDSCALE)
452 cwnd = RPC_CWNDSCALE;
453 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500454 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 xprt->cong, xprt->cwnd, cwnd);
456 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700457 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400459EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Chuck Lever44fbac22005-08-11 16:25:44 -0400461/**
462 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
463 * @xprt: transport with waiting tasks
464 * @status: result code to plant in each task before waking it
465 *
466 */
467void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
468{
469 if (status < 0)
470 rpc_wake_up_status(&xprt->pending, status);
471 else
472 rpc_wake_up(&xprt->pending);
473}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400474EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400475
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400476/**
477 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
478 * @task: task to be put to sleep
Randy Dunlap0b80ae42008-04-26 22:59:02 -0700479 * @action: function pointer to be executed after wait
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400480 */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400482{
483 struct rpc_rqst *req = task->tk_rqstp;
484 struct rpc_xprt *xprt = req->rq_xprt;
485
486 task->tk_timeout = req->rq_timeout;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400487 rpc_sleep_on(&xprt->pending, task, action);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400488}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400489EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400490
491/**
492 * xprt_write_space - wake the task waiting for transport output buffer space
493 * @xprt: transport with waiting tasks
494 *
495 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
496 */
497void xprt_write_space(struct rpc_xprt *xprt)
498{
499 if (unlikely(xprt->shutdown))
500 return;
501
502 spin_lock_bh(&xprt->transport_lock);
503 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500504 dprintk("RPC: write space: waking waiting task on "
505 "xprt %p\n", xprt);
Trond Myklebustfda13932008-02-22 16:34:12 -0500506 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400507 }
508 spin_unlock_bh(&xprt->transport_lock);
509}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400510EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400511
Chuck Leverfe3aca22005-08-25 16:25:50 -0700512/**
513 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
514 * @task: task whose timeout is to be set
515 *
516 * Set a request's retransmit timeout based on the transport's
517 * default timeout parameters. Used by transports that don't adjust
518 * the retransmit timeout based on round-trip time estimation.
519 */
520void xprt_set_retrans_timeout_def(struct rpc_task *task)
521{
522 task->tk_timeout = task->tk_rqstp->rq_timeout;
523}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400524EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700525
526/*
527 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
528 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800529 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700530 * Set a request's retransmit timeout using the RTT estimator.
531 */
532void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
533{
534 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500535 struct rpc_clnt *clnt = task->tk_client;
536 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700537 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500538 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700539
540 task->tk_timeout = rpc_calc_rto(rtt, timer);
541 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
542 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
543 task->tk_timeout = max_timeout;
544}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400545EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static void xprt_reset_majortimeo(struct rpc_rqst *req)
548{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500549 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 req->rq_majortimeo = req->rq_timeout;
552 if (to->to_exponential)
553 req->rq_majortimeo <<= to->to_retries;
554 else
555 req->rq_majortimeo += to->to_increment * to->to_retries;
556 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
557 req->rq_majortimeo = to->to_maxval;
558 req->rq_majortimeo += jiffies;
559}
560
Chuck Lever9903cd12005-08-11 16:25:26 -0400561/**
562 * xprt_adjust_timeout - adjust timeout values for next retransmit
563 * @req: RPC request containing parameters to use for the adjustment
564 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566int xprt_adjust_timeout(struct rpc_rqst *req)
567{
568 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500569 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int status = 0;
571
572 if (time_before(jiffies, req->rq_majortimeo)) {
573 if (to->to_exponential)
574 req->rq_timeout <<= 1;
575 else
576 req->rq_timeout += to->to_increment;
577 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
578 req->rq_timeout = to->to_maxval;
579 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else {
581 req->rq_timeout = to->to_initval;
582 req->rq_retries = 0;
583 xprt_reset_majortimeo(req);
584 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400585 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400587 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 status = -ETIMEDOUT;
589 }
590
591 if (req->rq_timeout == 0) {
592 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
593 req->rq_timeout = 5 * HZ;
594 }
595 return status;
596}
597
David Howells65f27f32006-11-22 14:55:48 +0000598static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
David Howells65f27f32006-11-22 14:55:48 +0000600 struct rpc_xprt *xprt =
601 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Chuck Levera246b012005-08-11 16:25:23 -0400603 xprt->ops->close(xprt);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500604 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xprt_release_write(xprt, NULL);
606}
607
Chuck Lever9903cd12005-08-11 16:25:26 -0400608/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500609 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400610 * @xprt: transport to flag for disconnect
611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500613void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Chuck Lever46121cf2007-01-31 12:14:08 -0500615 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400616 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xprt_clear_connected(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400618 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400619 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500621EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Trond Myklebust66af1e552007-11-06 10:18:36 -0500623/**
624 * xprt_force_disconnect - force a transport to disconnect
625 * @xprt: transport to disconnect
626 *
627 */
628void xprt_force_disconnect(struct rpc_xprt *xprt)
629{
630 /* Don't race with the test_bit() in xprt_clear_locked() */
631 spin_lock_bh(&xprt->transport_lock);
632 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
633 /* Try to schedule an autoclose RPC call */
634 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
635 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400636 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust66af1e552007-11-06 10:18:36 -0500637 spin_unlock_bh(&xprt->transport_lock);
638}
Trond Myklebust66af1e552007-11-06 10:18:36 -0500639
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400640/**
641 * xprt_conditional_disconnect - force a transport to disconnect
642 * @xprt: transport to disconnect
643 * @cookie: 'connection cookie'
644 *
645 * This attempts to break the connection if and only if 'cookie' matches
646 * the current transport 'connection cookie'. It ensures that we don't
647 * try to break the connection more than once when we need to retransmit
648 * a batch of RPC requests.
649 *
650 */
651void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
652{
653 /* Don't race with the test_bit() in xprt_clear_locked() */
654 spin_lock_bh(&xprt->transport_lock);
655 if (cookie != xprt->connect_cookie)
656 goto out;
657 if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
658 goto out;
659 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
660 /* Try to schedule an autoclose RPC call */
661 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
662 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400663 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400664out:
665 spin_unlock_bh(&xprt->transport_lock);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static void
669xprt_init_autodisconnect(unsigned long data)
670{
671 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
672
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400673 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!list_empty(&xprt->recv) || xprt->shutdown)
675 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400676 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400678 spin_unlock(&xprt->transport_lock);
Trond Myklebustf75e6742009-04-21 17:18:20 -0400679 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
680 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400683 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Chuck Lever9903cd12005-08-11 16:25:26 -0400686/**
687 * xprt_connect - schedule a transport connect operation
688 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
690 */
691void xprt_connect(struct rpc_task *task)
692{
693 struct rpc_xprt *xprt = task->tk_xprt;
694
Chuck Lever46121cf2007-01-31 12:14:08 -0500695 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xprt, (xprt_connected(xprt) ? "is" : "is not"));
697
Chuck Leverec739ef2006-08-22 20:06:15 -0400698 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400699 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
701 }
702 if (!xprt_lock_write(xprt, task))
703 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500704
705 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
706 xprt->ops->close(xprt);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400709 xprt_release_write(xprt, task);
710 else {
711 if (task->tk_rqstp)
712 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Trond Myklebusta8ce4a82010-04-16 16:42:12 -0400714 task->tk_timeout = task->tk_rqstp->rq_timeout;
Trond Myklebust5d008372008-02-22 16:34:17 -0500715 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400716
717 if (test_bit(XPRT_CLOSING, &xprt->state))
718 return;
719 if (xprt_test_and_set_connecting(xprt))
720 return;
Chuck Lever262ca072006-03-20 13:44:16 -0500721 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400722 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Chuck Lever9903cd12005-08-11 16:25:26 -0400726static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct rpc_xprt *xprt = task->tk_xprt;
729
Chuck Levercd983ef2008-06-11 17:56:13 -0400730 if (task->tk_status == 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500731 xprt->stat.connect_count++;
732 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500733 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 task->tk_pid);
735 return;
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 switch (task->tk_status) {
Trond Myklebust2a491992009-03-11 14:38:00 -0400739 case -EAGAIN:
740 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
Chuck Lever23475d62005-08-11 16:25:08 -0400741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500743 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
744 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500747 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
748 "server %s\n", task->tk_pid, -task->tk_status,
749 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400750 xprt_release_write(xprt, task);
751 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Chuck Lever9903cd12005-08-11 16:25:26 -0400755/**
756 * xprt_lookup_rqst - find an RPC request corresponding to an XID
757 * @xprt: transport on which the original request was transmitted
758 * @xid: RPC XID of incoming reply
759 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700761struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400763 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400765 list_for_each_entry(entry, &xprt->recv, rq_list)
Chuck Lever262ca072006-03-20 13:44:16 -0500766 if (entry->rq_xid == xid)
767 return entry;
Chuck Lever46121cf2007-01-31 12:14:08 -0500768
769 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
770 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500771 xprt->stat.bad_xids++;
772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400774EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Chuck Leverbbc72ce2010-05-07 13:34:27 -0400776static void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700778 struct rpc_rqst *req = task->tk_rqstp;
779 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
780 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400781 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Chuck Lever1570c1e2005-08-25 16:25:52 -0700783 if (timer) {
784 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -0400785 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -0700786 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700788}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Chuck Lever1570c1e2005-08-25 16:25:52 -0700790/**
791 * xprt_complete_rqst - called when reply processing is complete
792 * @task: RPC request that recently completed
793 * @copied: actual number of bytes received from the transport
794 *
795 * Caller holds transport lock.
796 */
797void xprt_complete_rqst(struct rpc_task *task, int copied)
798{
799 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -0500800 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Chuck Lever1570c1e2005-08-25 16:25:52 -0700802 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
803 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Trond Myklebustfda13932008-02-22 16:34:12 -0500805 xprt->stat.recvs++;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400806 req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
Chuck Leverbbc72ce2010-05-07 13:34:27 -0400807 if (xprt->ops->timer != NULL)
808 xprt_update_rtt(task);
Chuck Leveref759a22006-03-20 13:44:17 -0500809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 list_del_init(&req->rq_list);
Trond Myklebust1e799b62008-03-21 16:19:41 -0400811 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400812 /* Ensure all writes are done before we update */
813 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500814 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400815 req->rq_reply_bytes_recvd = copied;
Trond Myklebustfda13932008-02-22 16:34:12 -0500816 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400818EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Chuck Lever46c0ee82005-08-25 16:25:52 -0700820static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700822 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct rpc_xprt *xprt = req->rq_xprt;
824
Trond Myklebust5d008372008-02-22 16:34:17 -0500825 if (task->tk_status != -ETIMEDOUT)
826 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500827 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700828
Trond Myklebust5d008372008-02-22 16:34:17 -0500829 spin_lock_bh(&xprt->transport_lock);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400830 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -0700831 if (xprt->ops->timer)
832 xprt->ops->timer(task);
Trond Myklebust5d008372008-02-22 16:34:17 -0500833 } else
834 task->tk_status = 0;
835 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300838static inline int xprt_has_timer(struct rpc_xprt *xprt)
839{
840 return xprt->idle_timeout != 0;
841}
842
Chuck Lever9903cd12005-08-11 16:25:26 -0400843/**
844 * xprt_prepare_transmit - reserve the transport before sending a request
845 * @task: RPC task about to send a request
846 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400848int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct rpc_rqst *req = task->tk_rqstp;
851 struct rpc_xprt *xprt = req->rq_xprt;
852 int err = 0;
853
Chuck Lever46121cf2007-01-31 12:14:08 -0500854 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400856 spin_lock_bh(&xprt->transport_lock);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400857 if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
858 err = req->rq_reply_bytes_recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto out_unlock;
860 }
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400861 if (!xprt->ops->reserve_xprt(xprt, task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400864 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return err;
866}
867
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400868void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700869{
Rahul Iyer343952f2009-04-01 09:23:17 -0400870 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700871}
872
Chuck Lever9903cd12005-08-11 16:25:26 -0400873/**
874 * xprt_transmit - send an RPC request on a transport
875 * @task: controlling RPC task
876 *
877 * We have to copy the iovec because sendmsg fiddles with its contents.
878 */
879void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct rpc_rqst *req = task->tk_rqstp;
882 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400883 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Chuck Lever46121cf2007-01-31 12:14:08 -0500885 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400887 if (!req->rq_reply_bytes_recvd) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400888 if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
889 /*
890 * Add to the list only if we're expecting a reply
891 */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400892 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* Update the softirq receive buffer */
894 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
895 sizeof(req->rq_private_buf));
896 /* Add request to the receive list */
897 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400898 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000900 /* Turn off autodisconnect */
901 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 } else if (!req->rq_bytes_sent)
904 return;
905
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400906 req->rq_connect_cookie = xprt->connect_cookie;
Chuck Leverff839972010-05-07 13:34:47 -0400907 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -0400908 status = xprt->ops->send_request(task);
Trond Myklebustc8485e42009-03-11 14:37:59 -0400909 if (status != 0) {
910 task->tk_status = status;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700911 return;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Trond Myklebustc8485e42009-03-11 14:37:59 -0400914 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -0400915 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400916 spin_lock_bh(&xprt->transport_lock);
917
918 xprt->ops->set_retrans_timeout(task);
919
920 xprt->stat.sends++;
921 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
922 xprt->stat.bklog_u += xprt->backlog.qlen;
923
924 /* Don't race with disconnect */
925 if (!xprt_connected(xprt))
926 task->tk_status = -ENOTCONN;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400927 else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400928 /*
929 * Sleep on the pending queue since
930 * we're expecting a reply.
931 */
Trond Myklebustc8485e42009-03-11 14:37:59 -0400932 rpc_sleep_on(&xprt->pending, task, xprt_timer);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400933 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400934 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Trond Myklebustee5ebe82010-04-16 16:37:01 -0400937static void xprt_alloc_slot(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct rpc_xprt *xprt = task->tk_xprt;
940
941 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!list_empty(&xprt->free)) {
943 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
944 list_del_init(&req->rq_list);
945 task->tk_rqstp = req;
946 xprt_request_init(task, xprt);
947 return;
948 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500949 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 task->tk_status = -EAGAIN;
Trond Myklebust5d008372008-02-22 16:34:17 -0500951 rpc_sleep_on(&xprt->backlog, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Trond Myklebustee5ebe82010-04-16 16:37:01 -0400954static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
955{
956 memset(req, 0, sizeof(*req)); /* mark unused */
957
958 spin_lock(&xprt->reserve_lock);
959 list_add(&req->rq_list, &xprt->free);
960 rpc_wake_up_next(&xprt->backlog);
961 spin_unlock(&xprt->reserve_lock);
962}
963
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400964struct rpc_xprt *xprt_alloc(struct net *net, int size, int max_req)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400965{
966 struct rpc_xprt *xprt;
967
968 xprt = kzalloc(size, GFP_KERNEL);
969 if (xprt == NULL)
970 goto out;
Trond Myklebusta8de2402011-03-15 19:56:30 -0400971 atomic_set(&xprt->count, 1);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400972
973 xprt->max_reqs = max_req;
974 xprt->slot = kcalloc(max_req, sizeof(struct rpc_rqst), GFP_KERNEL);
975 if (xprt->slot == NULL)
976 goto out_free;
977
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400978 xprt->xprt_net = get_net(net);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400979 return xprt;
980
981out_free:
982 kfree(xprt);
983out:
984 return NULL;
985}
986EXPORT_SYMBOL_GPL(xprt_alloc);
987
Pavel Emelyanove204e622010-09-29 16:03:13 +0400988void xprt_free(struct rpc_xprt *xprt)
989{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400990 put_net(xprt->xprt_net);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400991 kfree(xprt->slot);
992 kfree(xprt);
993}
994EXPORT_SYMBOL_GPL(xprt_free);
995
Chuck Lever9903cd12005-08-11 16:25:26 -0400996/**
997 * xprt_reserve - allocate an RPC request slot
998 * @task: RPC task requesting a slot allocation
999 *
1000 * If no more slots are available, place the task on the transport's
1001 * backlog queue.
1002 */
1003void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct rpc_xprt *xprt = task->tk_xprt;
1006
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001007 task->tk_status = 0;
1008 if (task->tk_rqstp != NULL)
1009 return;
1010
1011 /* Note: grabbing the xprt_lock_write() here is not strictly needed,
1012 * but ensures that we throttle new slot allocation if the transport
1013 * is congested (e.g. if reconnecting or if we're out of socket
1014 * write buffer space).
1015 */
1016 task->tk_timeout = 0;
1017 task->tk_status = -EAGAIN;
1018 if (!xprt_lock_write(xprt, task))
1019 return;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +01001022 spin_lock(&xprt->reserve_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001023 xprt_alloc_slot(task);
Trond Myklebust0065db32006-01-03 09:55:56 +01001024 spin_unlock(&xprt->reserve_lock);
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -04001025 xprt_release_write(xprt, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001028static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001030 return (__force __be32)xprt->xid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static inline void xprt_init_xid(struct rpc_xprt *xprt)
1034{
Chuck Leverbf3fcf82006-05-25 01:40:51 -04001035 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Chuck Lever9903cd12005-08-11 16:25:26 -04001038static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct rpc_rqst *req = task->tk_rqstp;
1041
Trond Myklebustba7392b2007-12-20 16:03:55 -05001042 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 req->rq_task = task;
1044 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +01001045 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001047 req->rq_release_snd_buf = NULL;
Trond Myklebustda45828e2006-08-31 15:44:52 -04001048 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -05001049 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 req, ntohl(req->rq_xid));
1051}
1052
Chuck Lever9903cd12005-08-11 16:25:26 -04001053/**
1054 * xprt_release - release an RPC request slot
1055 * @task: task which is finished with the slot
1056 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001058void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001060 struct rpc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct rpc_rqst *req;
1062
1063 if (!(req = task->tk_rqstp))
1064 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001065
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001066 xprt = req->rq_xprt;
Chuck Lever11c556b2006-03-20 13:44:22 -05001067 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001068 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001069 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001070 if (xprt->ops->release_request)
1071 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!list_empty(&req->rq_list))
1073 list_del(&req->rq_list);
1074 xprt->last_used = jiffies;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001075 if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001076 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001077 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001078 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001079 if (req->rq_buffer)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001080 xprt->ops->buf_free(req->rq_buffer);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001081 if (req->rq_cred != NULL)
1082 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001084 if (req->rq_release_snd_buf)
1085 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001086
Chuck Lever46121cf2007-01-31 12:14:08 -05001087 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001088 if (likely(!bc_prealloc(req)))
1089 xprt_free_slot(xprt, req);
1090 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001091 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Chuck Lever9903cd12005-08-11 16:25:26 -04001094/**
Chuck Leverc2866762006-08-22 20:06:20 -04001095 * xprt_create_transport - create an RPC transport
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001096 * @args: rpc transport creation arguments
Chuck Leverc2866762006-08-22 20:06:20 -04001097 *
1098 */
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04001099struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
Chuck Leverc2866762006-08-22 20:06:20 -04001100{
Chuck Leverc2866762006-08-22 20:06:20 -04001101 struct rpc_xprt *xprt;
1102 struct rpc_rqst *req;
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001103 struct xprt_class *t;
Chuck Leverc2866762006-08-22 20:06:20 -04001104
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001105 spin_lock(&xprt_list_lock);
1106 list_for_each_entry(t, &xprt_list, list) {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001107 if (t->ident == args->ident) {
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001108 spin_unlock(&xprt_list_lock);
1109 goto found;
1110 }
Chuck Leverc2866762006-08-22 20:06:20 -04001111 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001112 spin_unlock(&xprt_list_lock);
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001113 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001114 return ERR_PTR(-EIO);
1115
1116found:
1117 xprt = t->setup(args);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001118 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001119 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -04001120 -PTR_ERR(xprt));
1121 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001122 }
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05001123 if (test_and_set_bit(XPRT_INITIALIZED, &xprt->state))
1124 /* ->setup returned a pre-initialized xprt: */
1125 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001126
1127 spin_lock_init(&xprt->transport_lock);
1128 spin_lock_init(&xprt->reserve_lock);
1129
1130 INIT_LIST_HEAD(&xprt->free);
1131 INIT_LIST_HEAD(&xprt->recv);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001132#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001133 spin_lock_init(&xprt->bc_pa_lock);
1134 INIT_LIST_HEAD(&xprt->bc_pa_list);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001135#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001136
David Howells65f27f32006-11-22 14:55:48 +00001137 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001138 if (xprt_has_timer(xprt))
1139 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1140 (unsigned long)xprt);
1141 else
1142 init_timer(&xprt->timer);
Chuck Leverc2866762006-08-22 20:06:20 -04001143 xprt->last_used = jiffies;
1144 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001145 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001146
1147 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1148 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1149 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1150 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1151 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1152
1153 /* initialize free list */
1154 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1155 list_add(&req->rq_list, &xprt->free);
1156
1157 xprt_init_xid(xprt);
1158
Chuck Lever46121cf2007-01-31 12:14:08 -05001159 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001160 xprt->max_reqs);
Chuck Leverc2866762006-08-22 20:06:20 -04001161 return xprt;
1162}
1163
Chuck Lever9903cd12005-08-11 16:25:26 -04001164/**
1165 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001166 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001167 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001169static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Chuck Lever46121cf2007-01-31 12:14:08 -05001171 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +01001172 xprt->shutdown = 1;
1173 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001174
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001175 rpc_destroy_wait_queue(&xprt->binding);
1176 rpc_destroy_wait_queue(&xprt->pending);
1177 rpc_destroy_wait_queue(&xprt->sending);
1178 rpc_destroy_wait_queue(&xprt->resend);
1179 rpc_destroy_wait_queue(&xprt->backlog);
J. Bruce Fieldsc3ae62ae2010-08-03 17:22:20 -04001180 cancel_work_sync(&xprt->task_cleanup);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001181 /*
1182 * Tear down transport state and free the rpc_xprt
1183 */
Chuck Levera246b012005-08-11 16:25:23 -04001184 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001187/**
1188 * xprt_put - release a reference to an RPC transport.
1189 * @xprt: pointer to the transport
1190 *
1191 */
1192void xprt_put(struct rpc_xprt *xprt)
1193{
Trond Myklebusta8de2402011-03-15 19:56:30 -04001194 if (atomic_dec_and_test(&xprt->count))
1195 xprt_destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001196}
1197
1198/**
1199 * xprt_get - return a reference to an RPC transport.
1200 * @xprt: pointer to the transport
1201 *
1202 */
1203struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1204{
Trond Myklebusta8de2402011-03-15 19:56:30 -04001205 if (atomic_inc_not_zero(&xprt->count))
1206 return xprt;
1207 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}