blob: 67996bd7fbf9c9261dcd29f781f260fef951e9b1 [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
15 * transport's wait list. At the same time, it installs a timer that
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * is run after the packet's timeout has expired.
17 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040018 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * caller is woken up, and the timer removed.
20 * - When no reply arrives within the timeout interval, the timer is
21 * fired by the kernel and runs xprt_timer(). It either adjusts the
22 * timeout values (minor timeout) or wakes up the caller with a status
23 * of -ETIMEDOUT.
24 * - When the caller receives a notification from RPC that a reply arrived,
25 * it should release the RPC slot, and process the reply.
26 * If the call timed out, it may choose to retry the operation by
27 * adjusting the initial timeout value, and simply calling rpc_call
28 * again.
29 *
30 * Support for async RPC is done through a set of RPC-specific scheduling
31 * primitives that `transparently' work for processes as well as async
32 * tasks that rely on callbacks.
33 *
34 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
Chuck Lever55aa4f52005-08-11 16:25:47 -040035 *
36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Chuck Levera246b012005-08-11 16:25:23 -040039#include <linux/module.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040042#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040044#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Chuck Levera246b012005-08-11 16:25:23 -040046#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050047#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Local variables
51 */
52
53#ifdef RPC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070054# define RPCDBG_FACILITY RPCDBG_XPRT
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Local functions
59 */
60static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
61static inline void do_xprt_reserve(struct rpc_task *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void xprt_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
64
Jiri Slaby5ba03e82007-11-22 19:40:22 +080065static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040066static LIST_HEAD(xprt_list);
67
Chuck Lever555ee3a2005-08-25 16:25:54 -070068/*
69 * The transport code maintains an estimate on the maximum number of out-
70 * standing RPC requests, using a smoothed version of the congestion
71 * avoidance implemented in 44BSD. This is basically the Van Jacobson
72 * congestion algorithm: If a retransmit occurs, the congestion window is
73 * halved; otherwise, it is incremented by 1/cwnd when
74 *
75 * - a reply is received and
76 * - a full number of requests are outstanding and
77 * - the congestion window hasn't been updated recently.
78 */
79#define RPC_CWNDSHIFT (8U)
80#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
81#define RPC_INITCWND RPC_CWNDSCALE
82#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
83
84#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Chuck Lever12a80462005-08-25 16:25:51 -070086/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040087 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
89 *
90 * If a transport implementation is loaded as a kernel module, it can
91 * call this interface to make itself known to the RPC client.
92 *
93 * Returns:
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
97 */
98int xprt_register_transport(struct xprt_class *transport)
99{
100 struct xprt_class *t;
101 int result;
102
103 result = -EEXIST;
104 spin_lock(&xprt_list_lock);
105 list_for_each_entry(t, &xprt_list, list) {
106 /* don't register the same transport class twice */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400107 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400108 goto out;
109 }
110
111 result = -EINVAL;
112 if (try_module_get(THIS_MODULE)) {
113 list_add_tail(&transport->list, &xprt_list);
114 printk(KERN_INFO "RPC: Registered %s transport module.\n",
115 transport->name);
116 result = 0;
117 }
118
119out:
120 spin_unlock(&xprt_list_lock);
121 return result;
122}
123EXPORT_SYMBOL_GPL(xprt_register_transport);
124
125/**
126 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800127 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400128 *
129 * Returns:
130 * 0: transport successfully unregistered
131 * -ENOENT: transport never registered
132 */
133int xprt_unregister_transport(struct xprt_class *transport)
134{
135 struct xprt_class *t;
136 int result;
137
138 result = 0;
139 spin_lock(&xprt_list_lock);
140 list_for_each_entry(t, &xprt_list, list) {
141 if (t == transport) {
142 printk(KERN_INFO
143 "RPC: Unregistered %s transport module.\n",
144 transport->name);
145 list_del_init(&transport->list);
146 module_put(THIS_MODULE);
147 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/**
Chuck Lever12a80462005-08-25 16:25:51 -0700159 * xprt_reserve_xprt - serialize write access to transports
160 * @task: task that is requesting access to the transport
161 *
162 * This prevents mixing the payload of separate requests, and prevents
163 * transport connects from colliding with writes. No congestion control
164 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Chuck Lever12a80462005-08-25 16:25:51 -0700166int xprt_reserve_xprt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Chuck Lever12a80462005-08-25 16:25:51 -0700168 struct rpc_xprt *xprt = task->tk_xprt;
169 struct rpc_rqst *req = task->tk_rqstp;
170
171 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
172 if (task == xprt->snd_task)
173 return 1;
174 if (task == NULL)
175 return 0;
176 goto out_sleep;
177 }
178 xprt->snd_task = task;
179 if (req) {
180 req->rq_bytes_sent = 0;
181 req->rq_ntrans++;
182 }
183 return 1;
184
185out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500186 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700187 task->tk_pid, xprt);
188 task->tk_timeout = 0;
189 task->tk_status = -EAGAIN;
190 if (req && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500191 rpc_sleep_on(&xprt->resend, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700192 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500193 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700194 return 0;
195}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400196EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700197
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100198static void xprt_clear_locked(struct rpc_xprt *xprt)
199{
200 xprt->snd_task = NULL;
201 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
202 smp_mb__before_clear_bit();
203 clear_bit(XPRT_LOCKED, &xprt->state);
204 smp_mb__after_clear_bit();
205 } else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400206 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100207}
208
Chuck Lever12a80462005-08-25 16:25:51 -0700209/*
210 * xprt_reserve_xprt_cong - serialize write access to transports
211 * @task: task that is requesting access to the transport
212 *
213 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
214 * integrated into the decision of whether a request is allowed to be
215 * woken up and given access to the transport.
216 */
217int xprt_reserve_xprt_cong(struct rpc_task *task)
218{
219 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct rpc_rqst *req = task->tk_rqstp;
221
Chuck Lever2226feb2005-08-11 16:25:38 -0400222 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (task == xprt->snd_task)
224 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto out_sleep;
226 }
Chuck Lever12a80462005-08-25 16:25:51 -0700227 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xprt->snd_task = task;
229 if (req) {
230 req->rq_bytes_sent = 0;
231 req->rq_ntrans++;
232 }
233 return 1;
234 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100235 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500237 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 task->tk_timeout = 0;
239 task->tk_status = -EAGAIN;
240 if (req && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500241 rpc_sleep_on(&xprt->resend, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500243 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400246EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Chuck Lever12a80462005-08-25 16:25:51 -0700248static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int retval;
251
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400252 spin_lock_bh(&xprt->transport_lock);
Chuck Lever12a80462005-08-25 16:25:51 -0700253 retval = xprt->ops->reserve_xprt(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400254 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return retval;
256}
257
Chuck Lever12a80462005-08-25 16:25:51 -0700258static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700261 struct rpc_rqst *req;
262
263 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
264 return;
265
266 task = rpc_wake_up_next(&xprt->resend);
267 if (!task) {
268 task = rpc_wake_up_next(&xprt->sending);
269 if (!task)
270 goto out_unlock;
271 }
272
273 req = task->tk_rqstp;
274 xprt->snd_task = task;
275 if (req) {
276 req->rq_bytes_sent = 0;
277 req->rq_ntrans++;
278 }
279 return;
280
281out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100282 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700283}
284
285static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
286{
287 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Chuck Lever2226feb2005-08-11 16:25:38 -0400289 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700291 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out_unlock;
293 task = rpc_wake_up_next(&xprt->resend);
294 if (!task) {
295 task = rpc_wake_up_next(&xprt->sending);
296 if (!task)
297 goto out_unlock;
298 }
Chuck Lever49e9a892005-08-25 16:25:51 -0700299 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct rpc_rqst *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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Chuck Lever49e9a892005-08-25 16:25:51 -0700312/**
313 * xprt_release_xprt - allow other requests to use a transport
314 * @xprt: transport with other tasks potentially waiting
315 * @task: task that is releasing access to the transport
316 *
317 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700319void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100322 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 __xprt_lock_write_next(xprt);
324 }
325}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400326EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Chuck Lever49e9a892005-08-25 16:25:51 -0700328/**
329 * xprt_release_xprt_cong - allow other requests to use a transport
330 * @xprt: transport with other tasks potentially waiting
331 * @task: task that is releasing access to the transport
332 *
333 * Note that "task" can be NULL. Another task is awoken to use the
334 * transport if the transport's congestion window allows it.
335 */
336void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
337{
338 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100339 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700340 __xprt_lock_write_next_cong(xprt);
341 }
342}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400343EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700344
345static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400347 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700348 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400349 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Van Jacobson congestion avoidance. Check if the congestion window
354 * overflowed. Put the task to sleep if this is the case.
355 */
356static int
357__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
358{
359 struct rpc_rqst *req = task->tk_rqstp;
360
361 if (req->rq_cong)
362 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500363 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 task->tk_pid, xprt->cong, xprt->cwnd);
365 if (RPCXPRT_CONGESTED(xprt))
366 return 0;
367 req->rq_cong = 1;
368 xprt->cong += RPC_CWNDSCALE;
369 return 1;
370}
371
372/*
373 * Adjust the congestion window, and wake up the next task
374 * that has been sleeping due to congestion
375 */
376static void
377__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
378{
379 if (!req->rq_cong)
380 return;
381 req->rq_cong = 0;
382 xprt->cong -= RPC_CWNDSCALE;
Chuck Lever49e9a892005-08-25 16:25:51 -0700383 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Chuck Lever46c0ee82005-08-25 16:25:52 -0700386/**
Chuck Levera58dd392005-08-25 16:25:53 -0700387 * xprt_release_rqst_cong - housekeeping when request is complete
388 * @task: RPC request that recently completed
389 *
390 * Useful for transports that require congestion control.
391 */
392void xprt_release_rqst_cong(struct rpc_task *task)
393{
394 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
395}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400396EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700397
398/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700399 * xprt_adjust_cwnd - adjust transport congestion window
400 * @task: recently completed RPC request used to adjust window
401 * @result: result code of completed RPC request
402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
404 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700405void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700407 struct rpc_rqst *req = task->tk_rqstp;
408 struct rpc_xprt *xprt = task->tk_xprt;
409 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (result >= 0 && cwnd <= xprt->cong) {
412 /* The (cwnd >> 1) term makes sure
413 * the result gets rounded properly. */
414 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
415 if (cwnd > RPC_MAXCWND(xprt))
416 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700417 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } else if (result == -ETIMEDOUT) {
419 cwnd >>= 1;
420 if (cwnd < RPC_CWNDSCALE)
421 cwnd = RPC_CWNDSCALE;
422 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500423 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 xprt->cong, xprt->cwnd, cwnd);
425 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700426 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400428EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Chuck Lever44fbac22005-08-11 16:25:44 -0400430/**
431 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
432 * @xprt: transport with waiting tasks
433 * @status: result code to plant in each task before waking it
434 *
435 */
436void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
437{
438 if (status < 0)
439 rpc_wake_up_status(&xprt->pending, status);
440 else
441 rpc_wake_up(&xprt->pending);
442}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400443EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400444
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400445/**
446 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
447 * @task: task to be put to sleep
Randy Dunlap0b80ae42008-04-26 22:59:02 -0700448 * @action: function pointer to be executed after wait
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400449 */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400450void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400451{
452 struct rpc_rqst *req = task->tk_rqstp;
453 struct rpc_xprt *xprt = req->rq_xprt;
454
455 task->tk_timeout = req->rq_timeout;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400456 rpc_sleep_on(&xprt->pending, task, action);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400457}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400458EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400459
460/**
461 * xprt_write_space - wake the task waiting for transport output buffer space
462 * @xprt: transport with waiting tasks
463 *
464 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
465 */
466void xprt_write_space(struct rpc_xprt *xprt)
467{
468 if (unlikely(xprt->shutdown))
469 return;
470
471 spin_lock_bh(&xprt->transport_lock);
472 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500473 dprintk("RPC: write space: waking waiting task on "
474 "xprt %p\n", xprt);
Trond Myklebustfda13932008-02-22 16:34:12 -0500475 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400476 }
477 spin_unlock_bh(&xprt->transport_lock);
478}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400479EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400480
Chuck Leverfe3aca22005-08-25 16:25:50 -0700481/**
482 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
483 * @task: task whose timeout is to be set
484 *
485 * Set a request's retransmit timeout based on the transport's
486 * default timeout parameters. Used by transports that don't adjust
487 * the retransmit timeout based on round-trip time estimation.
488 */
489void xprt_set_retrans_timeout_def(struct rpc_task *task)
490{
491 task->tk_timeout = task->tk_rqstp->rq_timeout;
492}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400493EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700494
495/*
496 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
497 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800498 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700499 * Set a request's retransmit timeout using the RTT estimator.
500 */
501void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
502{
503 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500504 struct rpc_clnt *clnt = task->tk_client;
505 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700506 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500507 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700508
509 task->tk_timeout = rpc_calc_rto(rtt, timer);
510 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
511 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
512 task->tk_timeout = max_timeout;
513}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400514EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static void xprt_reset_majortimeo(struct rpc_rqst *req)
517{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500518 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 req->rq_majortimeo = req->rq_timeout;
521 if (to->to_exponential)
522 req->rq_majortimeo <<= to->to_retries;
523 else
524 req->rq_majortimeo += to->to_increment * to->to_retries;
525 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
526 req->rq_majortimeo = to->to_maxval;
527 req->rq_majortimeo += jiffies;
528}
529
Chuck Lever9903cd12005-08-11 16:25:26 -0400530/**
531 * xprt_adjust_timeout - adjust timeout values for next retransmit
532 * @req: RPC request containing parameters to use for the adjustment
533 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535int xprt_adjust_timeout(struct rpc_rqst *req)
536{
537 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500538 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int status = 0;
540
541 if (time_before(jiffies, req->rq_majortimeo)) {
542 if (to->to_exponential)
543 req->rq_timeout <<= 1;
544 else
545 req->rq_timeout += to->to_increment;
546 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
547 req->rq_timeout = to->to_maxval;
548 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 } else {
550 req->rq_timeout = to->to_initval;
551 req->rq_retries = 0;
552 xprt_reset_majortimeo(req);
553 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400554 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400556 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 status = -ETIMEDOUT;
558 }
559
560 if (req->rq_timeout == 0) {
561 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
562 req->rq_timeout = 5 * HZ;
563 }
564 return status;
565}
566
David Howells65f27f32006-11-22 14:55:48 +0000567static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
David Howells65f27f32006-11-22 14:55:48 +0000569 struct rpc_xprt *xprt =
570 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Chuck Levera246b012005-08-11 16:25:23 -0400572 xprt->ops->close(xprt);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500573 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xprt_release_write(xprt, NULL);
575}
576
Chuck Lever9903cd12005-08-11 16:25:26 -0400577/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500578 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400579 * @xprt: transport to flag for disconnect
580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500582void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Chuck Lever46121cf2007-01-31 12:14:08 -0500584 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400585 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 xprt_clear_connected(xprt);
Chuck Lever44fbac22005-08-11 16:25:44 -0400587 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400588 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500590EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Trond Myklebust66af1e52007-11-06 10:18:36 -0500592/**
593 * xprt_force_disconnect - force a transport to disconnect
594 * @xprt: transport to disconnect
595 *
596 */
597void xprt_force_disconnect(struct rpc_xprt *xprt)
598{
599 /* Don't race with the test_bit() in xprt_clear_locked() */
600 spin_lock_bh(&xprt->transport_lock);
601 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
602 /* Try to schedule an autoclose RPC call */
603 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
604 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebustfda13932008-02-22 16:34:12 -0500605 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500606 spin_unlock_bh(&xprt->transport_lock);
607}
Trond Myklebust66af1e52007-11-06 10:18:36 -0500608
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400609/**
610 * xprt_conditional_disconnect - force a transport to disconnect
611 * @xprt: transport to disconnect
612 * @cookie: 'connection cookie'
613 *
614 * This attempts to break the connection if and only if 'cookie' matches
615 * the current transport 'connection cookie'. It ensures that we don't
616 * try to break the connection more than once when we need to retransmit
617 * a batch of RPC requests.
618 *
619 */
620void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
621{
622 /* Don't race with the test_bit() in xprt_clear_locked() */
623 spin_lock_bh(&xprt->transport_lock);
624 if (cookie != xprt->connect_cookie)
625 goto out;
626 if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
627 goto out;
628 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
629 /* Try to schedule an autoclose RPC call */
630 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
631 queue_work(rpciod_workqueue, &xprt->task_cleanup);
632 xprt_wake_pending_tasks(xprt, -ENOTCONN);
633out:
634 spin_unlock_bh(&xprt->transport_lock);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static void
638xprt_init_autodisconnect(unsigned long data)
639{
640 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
641
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400642 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!list_empty(&xprt->recv) || xprt->shutdown)
644 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400645 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400647 spin_unlock(&xprt->transport_lock);
Chuck Lever2226feb2005-08-11 16:25:38 -0400648 if (xprt_connecting(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xprt_release_write(xprt, NULL);
650 else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400651 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return;
653out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400654 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Chuck Lever9903cd12005-08-11 16:25:26 -0400657/**
658 * xprt_connect - schedule a transport connect operation
659 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
661 */
662void xprt_connect(struct rpc_task *task)
663{
664 struct rpc_xprt *xprt = task->tk_xprt;
665
Chuck Lever46121cf2007-01-31 12:14:08 -0500666 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 xprt, (xprt_connected(xprt) ? "is" : "is not"));
668
Chuck Leverec739ef2006-08-22 20:06:15 -0400669 if (!xprt_bound(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 task->tk_status = -EIO;
671 return;
672 }
673 if (!xprt_lock_write(xprt, task))
674 return;
675 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400676 xprt_release_write(xprt, task);
677 else {
678 if (task->tk_rqstp)
679 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Chuck Lever03bf4b72005-08-25 16:25:55 -0700681 task->tk_timeout = xprt->connect_timeout;
Trond Myklebust5d008372008-02-22 16:34:17 -0500682 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Chuck Lever262ca072006-03-20 13:44:16 -0500683 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400684 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Chuck Lever9903cd12005-08-11 16:25:26 -0400689static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct rpc_xprt *xprt = task->tk_xprt;
692
Chuck Levercd983ef2008-06-11 17:56:13 -0400693 if (task->tk_status == 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500694 xprt->stat.connect_count++;
695 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500696 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 task->tk_pid);
698 return;
699 }
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 switch (task->tk_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case -ENOTCONN:
Chuck Lever46121cf2007-01-31 12:14:08 -0500703 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
Chuck Lever23475d62005-08-11 16:25:08 -0400704 task->tk_pid);
705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500707 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
708 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500711 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
712 "server %s\n", task->tk_pid, -task->tk_status,
713 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400714 xprt_release_write(xprt, task);
715 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Chuck Lever9903cd12005-08-11 16:25:26 -0400719/**
720 * xprt_lookup_rqst - find an RPC request corresponding to an XID
721 * @xprt: transport on which the original request was transmitted
722 * @xid: RPC XID of incoming reply
723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700725struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 list_for_each(pos, &xprt->recv) {
730 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
Chuck Lever262ca072006-03-20 13:44:16 -0500731 if (entry->rq_xid == xid)
732 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500734
735 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
736 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500737 xprt->stat.bad_xids++;
738 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400740EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Chuck Lever9903cd12005-08-11 16:25:26 -0400742/**
Chuck Lever1570c1e2005-08-25 16:25:52 -0700743 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
744 * @task: RPC request that recently completed
Chuck Lever9903cd12005-08-11 16:25:26 -0400745 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Chuck Lever1570c1e2005-08-25 16:25:52 -0700747void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700749 struct rpc_rqst *req = task->tk_rqstp;
750 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
751 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Chuck Lever1570c1e2005-08-25 16:25:52 -0700753 if (timer) {
754 if (req->rq_ntrans == 1)
755 rpc_update_rtt(rtt, timer,
756 (long)jiffies - req->rq_xtime);
757 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700759}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400760EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Chuck Lever1570c1e2005-08-25 16:25:52 -0700762/**
763 * xprt_complete_rqst - called when reply processing is complete
764 * @task: RPC request that recently completed
765 * @copied: actual number of bytes received from the transport
766 *
767 * Caller holds transport lock.
768 */
769void xprt_complete_rqst(struct rpc_task *task, int copied)
770{
771 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -0500772 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Chuck Lever1570c1e2005-08-25 16:25:52 -0700774 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
775 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Trond Myklebustfda13932008-02-22 16:34:12 -0500777 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -0500778 task->tk_rtt = (long)jiffies - req->rq_xtime;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 list_del_init(&req->rq_list);
Trond Myklebust1e799b62008-03-21 16:19:41 -0400781 req->rq_private_buf.len = copied;
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500782 /* Ensure all writes are done before we update req->rq_received */
783 smp_wmb();
Trond Myklebust1e799b62008-03-21 16:19:41 -0400784 req->rq_received = copied;
Trond Myklebustfda13932008-02-22 16:34:12 -0500785 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400787EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Chuck Lever46c0ee82005-08-25 16:25:52 -0700789static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700791 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct rpc_xprt *xprt = req->rq_xprt;
793
Trond Myklebust5d008372008-02-22 16:34:17 -0500794 if (task->tk_status != -ETIMEDOUT)
795 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500796 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700797
Trond Myklebust5d008372008-02-22 16:34:17 -0500798 spin_lock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700799 if (!req->rq_received) {
800 if (xprt->ops->timer)
801 xprt->ops->timer(task);
Trond Myklebust5d008372008-02-22 16:34:17 -0500802 } else
803 task->tk_status = 0;
804 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Chuck Lever9903cd12005-08-11 16:25:26 -0400807/**
808 * xprt_prepare_transmit - reserve the transport before sending a request
809 * @task: RPC task about to send a request
810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400812int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct rpc_rqst *req = task->tk_rqstp;
815 struct rpc_xprt *xprt = req->rq_xprt;
816 int err = 0;
817
Chuck Lever46121cf2007-01-31 12:14:08 -0500818 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400820 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (req->rq_received && !req->rq_bytes_sent) {
822 err = req->rq_received;
823 goto out_unlock;
824 }
Chuck Lever12a80462005-08-25 16:25:51 -0700825 if (!xprt->ops->reserve_xprt(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 err = -EAGAIN;
827 goto out_unlock;
828 }
829
830 if (!xprt_connected(xprt)) {
831 err = -ENOTCONN;
832 goto out_unlock;
833 }
834out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400835 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return err;
837}
838
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400839void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700840{
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400841 xprt_release_write(task->tk_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700842}
843
Chuck Lever9903cd12005-08-11 16:25:26 -0400844/**
845 * xprt_transmit - send an RPC request on a transport
846 * @task: controlling RPC task
847 *
848 * We have to copy the iovec because sendmsg fiddles with its contents.
849 */
850void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct rpc_rqst *req = task->tk_rqstp;
853 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400854 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Chuck Lever46121cf2007-01-31 12:14:08 -0500856 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!req->rq_received) {
859 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400860 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* Update the softirq receive buffer */
862 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
863 sizeof(req->rq_private_buf));
864 /* Add request to the receive list */
865 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400866 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000868 /* Turn off autodisconnect */
869 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 } else if (!req->rq_bytes_sent)
872 return;
873
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400874 req->rq_connect_cookie = xprt->connect_cookie;
Chuck Levera246b012005-08-11 16:25:23 -0400875 status = xprt->ops->send_request(task);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700876 if (status == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500877 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700878 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262ca072006-03-20 13:44:16 -0500879
Chuck Leverfe3aca22005-08-25 16:25:50 -0700880 xprt->ops->set_retrans_timeout(task);
Chuck Lever262ca072006-03-20 13:44:16 -0500881
882 xprt->stat.sends++;
883 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
884 xprt->stat.bklog_u += xprt->backlog.qlen;
885
Chuck Leverfe3aca22005-08-25 16:25:50 -0700886 /* Don't race with disconnect */
887 if (!xprt_connected(xprt))
888 task->tk_status = -ENOTCONN;
889 else if (!req->rq_received)
Trond Myklebust5d008372008-02-22 16:34:17 -0500890 rpc_sleep_on(&xprt->pending, task, xprt_timer);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700891 spin_unlock_bh(&xprt->transport_lock);
892 return;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Note: at this point, task->tk_sleeping has not yet been set,
896 * hence there is no danger of the waking up task being put on
897 * schedq, and being picked up by a parallel run of rpciod().
898 */
899 task->tk_status = status;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400900 if (status == -ECONNREFUSED)
Trond Myklebust5d008372008-02-22 16:34:17 -0500901 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Chuck Lever9903cd12005-08-11 16:25:26 -0400904static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct rpc_xprt *xprt = task->tk_xprt;
907
908 task->tk_status = 0;
909 if (task->tk_rqstp)
910 return;
911 if (!list_empty(&xprt->free)) {
912 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
913 list_del_init(&req->rq_list);
914 task->tk_rqstp = req;
915 xprt_request_init(task, xprt);
916 return;
917 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500918 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 task->tk_status = -EAGAIN;
920 task->tk_timeout = 0;
Trond Myklebust5d008372008-02-22 16:34:17 -0500921 rpc_sleep_on(&xprt->backlog, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Chuck Lever9903cd12005-08-11 16:25:26 -0400924/**
925 * xprt_reserve - allocate an RPC request slot
926 * @task: RPC task requesting a slot allocation
927 *
928 * If no more slots are available, place the task on the transport's
929 * backlog queue.
930 */
931void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct rpc_xprt *xprt = task->tk_xprt;
934
935 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +0100936 spin_lock(&xprt->reserve_lock);
937 do_xprt_reserve(task);
938 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700941static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 return xprt->xid++;
944}
945
946static inline void xprt_init_xid(struct rpc_xprt *xprt)
947{
Chuck Leverbf3fcf82006-05-25 01:40:51 -0400948 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Chuck Lever9903cd12005-08-11 16:25:26 -0400951static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct rpc_rqst *req = task->tk_rqstp;
954
Trond Myklebustba7392b2007-12-20 16:03:55 -0500955 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 req->rq_task = task;
957 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +0100958 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400960 req->rq_release_snd_buf = NULL;
Trond Myklebustda458282006-08-31 15:44:52 -0400961 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -0500962 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 req, ntohl(req->rq_xid));
964}
965
Chuck Lever9903cd12005-08-11 16:25:26 -0400966/**
967 * xprt_release - release an RPC request slot
968 * @task: task which is finished with the slot
969 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400971void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct rpc_xprt *xprt = task->tk_xprt;
974 struct rpc_rqst *req;
975
976 if (!(req = task->tk_rqstp))
977 return;
Chuck Lever11c556b2006-03-20 13:44:22 -0500978 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400979 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700980 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -0700981 if (xprt->ops->release_request)
982 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!list_empty(&req->rq_list))
984 list_del(&req->rq_list);
985 xprt->last_used = jiffies;
Trond Myklebust0065db32006-01-03 09:55:56 +0100986 if (list_empty(&xprt->recv))
Chuck Levera246b012005-08-11 16:25:23 -0400987 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -0700988 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400989 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400990 xprt->ops->buf_free(req->rq_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400992 if (req->rq_release_snd_buf)
993 req->rq_release_snd_buf(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 memset(req, 0, sizeof(*req)); /* mark unused */
995
Chuck Lever46121cf2007-01-31 12:14:08 -0500996 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Chuck Lever5dc07722005-08-11 16:25:35 -0400998 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_add(&req->rq_list, &xprt->free);
Chuck Lever555ee3a2005-08-25 16:25:54 -07001000 rpc_wake_up_next(&xprt->backlog);
Chuck Lever5dc07722005-08-11 16:25:35 -04001001 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Chuck Lever9903cd12005-08-11 16:25:26 -04001004/**
Chuck Leverc2866762006-08-22 20:06:20 -04001005 * xprt_create_transport - create an RPC transport
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001006 * @args: rpc transport creation arguments
Chuck Leverc2866762006-08-22 20:06:20 -04001007 *
1008 */
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04001009struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
Chuck Leverc2866762006-08-22 20:06:20 -04001010{
Chuck Leverc2866762006-08-22 20:06:20 -04001011 struct rpc_xprt *xprt;
1012 struct rpc_rqst *req;
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001013 struct xprt_class *t;
Chuck Leverc2866762006-08-22 20:06:20 -04001014
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001015 spin_lock(&xprt_list_lock);
1016 list_for_each_entry(t, &xprt_list, list) {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001017 if (t->ident == args->ident) {
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001018 spin_unlock(&xprt_list_lock);
1019 goto found;
1020 }
Chuck Leverc2866762006-08-22 20:06:20 -04001021 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001022 spin_unlock(&xprt_list_lock);
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001023 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001024 return ERR_PTR(-EIO);
1025
1026found:
1027 xprt = t->setup(args);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001028 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001029 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -04001030 -PTR_ERR(xprt));
1031 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001032 }
1033
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001034 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001035 spin_lock_init(&xprt->transport_lock);
1036 spin_lock_init(&xprt->reserve_lock);
1037
1038 INIT_LIST_HEAD(&xprt->free);
1039 INIT_LIST_HEAD(&xprt->recv);
David Howells65f27f32006-11-22 14:55:48 +00001040 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001041 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1042 (unsigned long)xprt);
Chuck Leverc2866762006-08-22 20:06:20 -04001043 xprt->last_used = jiffies;
1044 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001045 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001046
1047 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1048 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1049 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1050 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1051 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1052
1053 /* initialize free list */
1054 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1055 list_add(&req->rq_list, &xprt->free);
1056
1057 xprt_init_xid(xprt);
1058
Chuck Lever46121cf2007-01-31 12:14:08 -05001059 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001060 xprt->max_reqs);
1061
1062 return xprt;
1063}
1064
Chuck Lever9903cd12005-08-11 16:25:26 -04001065/**
1066 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001067 * @kref: kref for the transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001070static void xprt_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001072 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
1073
Chuck Lever46121cf2007-01-31 12:14:08 -05001074 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +01001075 xprt->shutdown = 1;
1076 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001077
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001078 rpc_destroy_wait_queue(&xprt->binding);
1079 rpc_destroy_wait_queue(&xprt->pending);
1080 rpc_destroy_wait_queue(&xprt->sending);
1081 rpc_destroy_wait_queue(&xprt->resend);
1082 rpc_destroy_wait_queue(&xprt->backlog);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001083 /*
1084 * Tear down transport state and free the rpc_xprt
1085 */
Chuck Levera246b012005-08-11 16:25:23 -04001086 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001089/**
1090 * xprt_put - release a reference to an RPC transport.
1091 * @xprt: pointer to the transport
1092 *
1093 */
1094void xprt_put(struct rpc_xprt *xprt)
1095{
1096 kref_put(&xprt->kref, xprt_destroy);
1097}
1098
1099/**
1100 * xprt_get - return a reference to an RPC transport.
1101 * @xprt: pointer to the transport
1102 *
1103 */
1104struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1105{
1106 kref_get(&xprt->kref);
1107 return xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}