blob: bc13616e7fdc527c3aef355c86020b4bfb28d926 [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
Chuck Lever555ee3a2005-08-25 16:25:54 -070065/*
66 * The transport code maintains an estimate on the maximum number of out-
67 * standing RPC requests, using a smoothed version of the congestion
68 * avoidance implemented in 44BSD. This is basically the Van Jacobson
69 * congestion algorithm: If a retransmit occurs, the congestion window is
70 * halved; otherwise, it is incremented by 1/cwnd when
71 *
72 * - a reply is received and
73 * - a full number of requests are outstanding and
74 * - the congestion window hasn't been updated recently.
75 */
76#define RPC_CWNDSHIFT (8U)
77#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
78#define RPC_INITCWND RPC_CWNDSCALE
79#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
80
81#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Chuck Lever12a80462005-08-25 16:25:51 -070083/**
84 * xprt_reserve_xprt - serialize write access to transports
85 * @task: task that is requesting access to the transport
86 *
87 * This prevents mixing the payload of separate requests, and prevents
88 * transport connects from colliding with writes. No congestion control
89 * is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Chuck Lever12a80462005-08-25 16:25:51 -070091int xprt_reserve_xprt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Chuck Lever12a80462005-08-25 16:25:51 -070093 struct rpc_xprt *xprt = task->tk_xprt;
94 struct rpc_rqst *req = task->tk_rqstp;
95
96 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
97 if (task == xprt->snd_task)
98 return 1;
99 if (task == NULL)
100 return 0;
101 goto out_sleep;
102 }
103 xprt->snd_task = task;
104 if (req) {
105 req->rq_bytes_sent = 0;
106 req->rq_ntrans++;
107 }
108 return 1;
109
110out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500111 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700112 task->tk_pid, xprt);
113 task->tk_timeout = 0;
114 task->tk_status = -EAGAIN;
115 if (req && req->rq_ntrans)
116 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
117 else
118 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
119 return 0;
120}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400121EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700122
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100123static void xprt_clear_locked(struct rpc_xprt *xprt)
124{
125 xprt->snd_task = NULL;
126 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
127 smp_mb__before_clear_bit();
128 clear_bit(XPRT_LOCKED, &xprt->state);
129 smp_mb__after_clear_bit();
130 } else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400131 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100132}
133
Chuck Lever12a80462005-08-25 16:25:51 -0700134/*
135 * xprt_reserve_xprt_cong - serialize write access to transports
136 * @task: task that is requesting access to the transport
137 *
138 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
139 * integrated into the decision of whether a request is allowed to be
140 * woken up and given access to the transport.
141 */
142int xprt_reserve_xprt_cong(struct rpc_task *task)
143{
144 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct rpc_rqst *req = task->tk_rqstp;
146
Chuck Lever2226feb2005-08-11 16:25:38 -0400147 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (task == xprt->snd_task)
149 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto out_sleep;
151 }
Chuck Lever12a80462005-08-25 16:25:51 -0700152 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 xprt->snd_task = task;
154 if (req) {
155 req->rq_bytes_sent = 0;
156 req->rq_ntrans++;
157 }
158 return 1;
159 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100160 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500162 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 task->tk_timeout = 0;
164 task->tk_status = -EAGAIN;
165 if (req && req->rq_ntrans)
166 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
167 else
168 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
169 return 0;
170}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400171EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Chuck Lever12a80462005-08-25 16:25:51 -0700173static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int retval;
176
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400177 spin_lock_bh(&xprt->transport_lock);
Chuck Lever12a80462005-08-25 16:25:51 -0700178 retval = xprt->ops->reserve_xprt(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400179 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return retval;
181}
182
Chuck Lever12a80462005-08-25 16:25:51 -0700183static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700186 struct rpc_rqst *req;
187
188 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
189 return;
190
191 task = rpc_wake_up_next(&xprt->resend);
192 if (!task) {
193 task = rpc_wake_up_next(&xprt->sending);
194 if (!task)
195 goto out_unlock;
196 }
197
198 req = task->tk_rqstp;
199 xprt->snd_task = task;
200 if (req) {
201 req->rq_bytes_sent = 0;
202 req->rq_ntrans++;
203 }
204 return;
205
206out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100207 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700208}
209
210static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
211{
212 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Chuck Lever2226feb2005-08-11 16:25:38 -0400214 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700216 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out_unlock;
218 task = rpc_wake_up_next(&xprt->resend);
219 if (!task) {
220 task = rpc_wake_up_next(&xprt->sending);
221 if (!task)
222 goto out_unlock;
223 }
Chuck Lever49e9a892005-08-25 16:25:51 -0700224 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 struct rpc_rqst *req = task->tk_rqstp;
226 xprt->snd_task = task;
227 if (req) {
228 req->rq_bytes_sent = 0;
229 req->rq_ntrans++;
230 }
231 return;
232 }
233out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100234 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Chuck Lever49e9a892005-08-25 16:25:51 -0700237/**
238 * xprt_release_xprt - allow other requests to use a transport
239 * @xprt: transport with other tasks potentially waiting
240 * @task: task that is releasing access to the transport
241 *
242 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700244void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100247 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 __xprt_lock_write_next(xprt);
249 }
250}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400251EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Chuck Lever49e9a892005-08-25 16:25:51 -0700253/**
254 * xprt_release_xprt_cong - allow other requests to use a transport
255 * @xprt: transport with other tasks potentially waiting
256 * @task: task that is releasing access to the transport
257 *
258 * Note that "task" can be NULL. Another task is awoken to use the
259 * transport if the transport's congestion window allows it.
260 */
261void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
262{
263 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100264 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700265 __xprt_lock_write_next_cong(xprt);
266 }
267}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400268EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700269
270static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400272 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700273 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400274 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * Van Jacobson congestion avoidance. Check if the congestion window
279 * overflowed. Put the task to sleep if this is the case.
280 */
281static int
282__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
283{
284 struct rpc_rqst *req = task->tk_rqstp;
285
286 if (req->rq_cong)
287 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500288 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 task->tk_pid, xprt->cong, xprt->cwnd);
290 if (RPCXPRT_CONGESTED(xprt))
291 return 0;
292 req->rq_cong = 1;
293 xprt->cong += RPC_CWNDSCALE;
294 return 1;
295}
296
297/*
298 * Adjust the congestion window, and wake up the next task
299 * that has been sleeping due to congestion
300 */
301static void
302__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
303{
304 if (!req->rq_cong)
305 return;
306 req->rq_cong = 0;
307 xprt->cong -= RPC_CWNDSCALE;
Chuck Lever49e9a892005-08-25 16:25:51 -0700308 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Chuck Lever46c0ee82005-08-25 16:25:52 -0700311/**
Chuck Levera58dd392005-08-25 16:25:53 -0700312 * xprt_release_rqst_cong - housekeeping when request is complete
313 * @task: RPC request that recently completed
314 *
315 * Useful for transports that require congestion control.
316 */
317void xprt_release_rqst_cong(struct rpc_task *task)
318{
319 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
320}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400321EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700322
323/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700324 * xprt_adjust_cwnd - adjust transport congestion window
325 * @task: recently completed RPC request used to adjust window
326 * @result: result code of completed RPC request
327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
329 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700330void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700332 struct rpc_rqst *req = task->tk_rqstp;
333 struct rpc_xprt *xprt = task->tk_xprt;
334 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (result >= 0 && cwnd <= xprt->cong) {
337 /* The (cwnd >> 1) term makes sure
338 * the result gets rounded properly. */
339 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
340 if (cwnd > RPC_MAXCWND(xprt))
341 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700342 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else if (result == -ETIMEDOUT) {
344 cwnd >>= 1;
345 if (cwnd < RPC_CWNDSCALE)
346 cwnd = RPC_CWNDSCALE;
347 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500348 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 xprt->cong, xprt->cwnd, cwnd);
350 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700351 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400353EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Chuck Lever44fbac22005-08-11 16:25:44 -0400355/**
356 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
357 * @xprt: transport with waiting tasks
358 * @status: result code to plant in each task before waking it
359 *
360 */
361void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
362{
363 if (status < 0)
364 rpc_wake_up_status(&xprt->pending, status);
365 else
366 rpc_wake_up(&xprt->pending);
367}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400368EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400369
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400370/**
371 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
372 * @task: task to be put to sleep
373 *
374 */
375void xprt_wait_for_buffer_space(struct rpc_task *task)
376{
377 struct rpc_rqst *req = task->tk_rqstp;
378 struct rpc_xprt *xprt = req->rq_xprt;
379
380 task->tk_timeout = req->rq_timeout;
381 rpc_sleep_on(&xprt->pending, task, NULL, NULL);
382}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400383EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400384
385/**
386 * xprt_write_space - wake the task waiting for transport output buffer space
387 * @xprt: transport with waiting tasks
388 *
389 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
390 */
391void xprt_write_space(struct rpc_xprt *xprt)
392{
393 if (unlikely(xprt->shutdown))
394 return;
395
396 spin_lock_bh(&xprt->transport_lock);
397 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500398 dprintk("RPC: write space: waking waiting task on "
399 "xprt %p\n", xprt);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400400 rpc_wake_up_task(xprt->snd_task);
401 }
402 spin_unlock_bh(&xprt->transport_lock);
403}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400404EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400405
Chuck Leverfe3aca22005-08-25 16:25:50 -0700406/**
407 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
408 * @task: task whose timeout is to be set
409 *
410 * Set a request's retransmit timeout based on the transport's
411 * default timeout parameters. Used by transports that don't adjust
412 * the retransmit timeout based on round-trip time estimation.
413 */
414void xprt_set_retrans_timeout_def(struct rpc_task *task)
415{
416 task->tk_timeout = task->tk_rqstp->rq_timeout;
417}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400418EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700419
420/*
421 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
422 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800423 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700424 * Set a request's retransmit timeout using the RTT estimator.
425 */
426void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
427{
428 int timer = task->tk_msg.rpc_proc->p_timer;
429 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
430 struct rpc_rqst *req = task->tk_rqstp;
431 unsigned long max_timeout = req->rq_xprt->timeout.to_maxval;
432
433 task->tk_timeout = rpc_calc_rto(rtt, timer);
434 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
435 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
436 task->tk_timeout = max_timeout;
437}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400438EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static void xprt_reset_majortimeo(struct rpc_rqst *req)
441{
442 struct rpc_timeout *to = &req->rq_xprt->timeout;
443
444 req->rq_majortimeo = req->rq_timeout;
445 if (to->to_exponential)
446 req->rq_majortimeo <<= to->to_retries;
447 else
448 req->rq_majortimeo += to->to_increment * to->to_retries;
449 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
450 req->rq_majortimeo = to->to_maxval;
451 req->rq_majortimeo += jiffies;
452}
453
Chuck Lever9903cd12005-08-11 16:25:26 -0400454/**
455 * xprt_adjust_timeout - adjust timeout values for next retransmit
456 * @req: RPC request containing parameters to use for the adjustment
457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
459int xprt_adjust_timeout(struct rpc_rqst *req)
460{
461 struct rpc_xprt *xprt = req->rq_xprt;
462 struct rpc_timeout *to = &xprt->timeout;
463 int status = 0;
464
465 if (time_before(jiffies, req->rq_majortimeo)) {
466 if (to->to_exponential)
467 req->rq_timeout <<= 1;
468 else
469 req->rq_timeout += to->to_increment;
470 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
471 req->rq_timeout = to->to_maxval;
472 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
474 req->rq_timeout = to->to_initval;
475 req->rq_retries = 0;
476 xprt_reset_majortimeo(req);
477 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400478 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400480 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 status = -ETIMEDOUT;
482 }
483
484 if (req->rq_timeout == 0) {
485 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
486 req->rq_timeout = 5 * HZ;
487 }
488 return status;
489}
490
David Howells65f27f32006-11-22 14:55:48 +0000491static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
David Howells65f27f32006-11-22 14:55:48 +0000493 struct rpc_xprt *xprt =
494 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400497 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 xprt_release_write(xprt, NULL);
499}
500
Chuck Lever9903cd12005-08-11 16:25:26 -0400501/**
502 * xprt_disconnect - mark a transport as disconnected
503 * @xprt: transport to flag for disconnect
504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Chuck Levera246b012005-08-11 16:25:23 -0400506void xprt_disconnect(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Chuck Lever46121cf2007-01-31 12:14:08 -0500508 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400509 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 xprt_clear_connected(xprt);
Chuck Lever44fbac22005-08-11 16:25:44 -0400511 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400512 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400514EXPORT_SYMBOL_GPL(xprt_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static void
517xprt_init_autodisconnect(unsigned long data)
518{
519 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
520
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400521 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!list_empty(&xprt->recv) || xprt->shutdown)
523 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400524 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400526 spin_unlock(&xprt->transport_lock);
Chuck Lever2226feb2005-08-11 16:25:38 -0400527 if (xprt_connecting(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 xprt_release_write(xprt, NULL);
529 else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400530 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return;
532out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400533 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Chuck Lever9903cd12005-08-11 16:25:26 -0400536/**
537 * xprt_connect - schedule a transport connect operation
538 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
540 */
541void xprt_connect(struct rpc_task *task)
542{
543 struct rpc_xprt *xprt = task->tk_xprt;
544
Chuck Lever46121cf2007-01-31 12:14:08 -0500545 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 xprt, (xprt_connected(xprt) ? "is" : "is not"));
547
Chuck Leverec739ef2006-08-22 20:06:15 -0400548 if (!xprt_bound(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 task->tk_status = -EIO;
550 return;
551 }
552 if (!xprt_lock_write(xprt, task))
553 return;
554 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400555 xprt_release_write(xprt, task);
556 else {
557 if (task->tk_rqstp)
558 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Chuck Lever03bf4b72005-08-25 16:25:55 -0700560 task->tk_timeout = xprt->connect_timeout;
Chuck Levera246b012005-08-11 16:25:23 -0400561 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
Chuck Lever262ca072006-03-20 13:44:16 -0500562 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400563 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Chuck Lever9903cd12005-08-11 16:25:26 -0400568static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct rpc_xprt *xprt = task->tk_xprt;
571
572 if (task->tk_status >= 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500573 xprt->stat.connect_count++;
574 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500575 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 task->tk_pid);
577 return;
578 }
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 switch (task->tk_status) {
581 case -ECONNREFUSED:
582 case -ECONNRESET:
Chuck Lever46121cf2007-01-31 12:14:08 -0500583 dprintk("RPC: %5u xprt_connect_status: server %s refused "
584 "connection\n", task->tk_pid,
585 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 case -ENOTCONN:
Chuck Lever46121cf2007-01-31 12:14:08 -0500588 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
Chuck Lever23475d62005-08-11 16:25:08 -0400589 task->tk_pid);
590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500592 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
593 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500596 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
597 "server %s\n", task->tk_pid, -task->tk_status,
598 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400599 xprt_release_write(xprt, task);
600 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Chuck Lever9903cd12005-08-11 16:25:26 -0400604/**
605 * xprt_lookup_rqst - find an RPC request corresponding to an XID
606 * @xprt: transport on which the original request was transmitted
607 * @xid: RPC XID of incoming reply
608 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700610struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 list_for_each(pos, &xprt->recv) {
615 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
Chuck Lever262ca072006-03-20 13:44:16 -0500616 if (entry->rq_xid == xid)
617 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500619
620 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
621 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500622 xprt->stat.bad_xids++;
623 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400625EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Chuck Lever9903cd12005-08-11 16:25:26 -0400627/**
Chuck Lever1570c1e2005-08-25 16:25:52 -0700628 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
629 * @task: RPC request that recently completed
Chuck Lever9903cd12005-08-11 16:25:26 -0400630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Chuck Lever1570c1e2005-08-25 16:25:52 -0700632void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700634 struct rpc_rqst *req = task->tk_rqstp;
635 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
636 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Chuck Lever1570c1e2005-08-25 16:25:52 -0700638 if (timer) {
639 if (req->rq_ntrans == 1)
640 rpc_update_rtt(rtt, timer,
641 (long)jiffies - req->rq_xtime);
642 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700644}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400645EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Chuck Lever1570c1e2005-08-25 16:25:52 -0700647/**
648 * xprt_complete_rqst - called when reply processing is complete
649 * @task: RPC request that recently completed
650 * @copied: actual number of bytes received from the transport
651 *
652 * Caller holds transport lock.
653 */
654void xprt_complete_rqst(struct rpc_task *task, int copied)
655{
656 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Chuck Lever1570c1e2005-08-25 16:25:52 -0700658 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
659 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Chuck Lever262ca072006-03-20 13:44:16 -0500661 task->tk_xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -0500662 task->tk_rtt = (long)jiffies - req->rq_xtime;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 list_del_init(&req->rq_list);
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500665 /* Ensure all writes are done before we update req->rq_received */
666 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 req->rq_received = req->rq_private_buf.len = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400670EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Chuck Lever46c0ee82005-08-25 16:25:52 -0700672static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700674 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct rpc_xprt *xprt = req->rq_xprt;
676
Chuck Lever46121cf2007-01-31 12:14:08 -0500677 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700678
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400679 spin_lock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700680 if (!req->rq_received) {
681 if (xprt->ops->timer)
682 xprt->ops->timer(task);
683 task->tk_status = -ETIMEDOUT;
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 task->tk_timeout = 0;
686 rpc_wake_up_task(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400687 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Chuck Lever9903cd12005-08-11 16:25:26 -0400690/**
691 * xprt_prepare_transmit - reserve the transport before sending a request
692 * @task: RPC task about to send a request
693 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400695int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct rpc_rqst *req = task->tk_rqstp;
698 struct rpc_xprt *xprt = req->rq_xprt;
699 int err = 0;
700
Chuck Lever46121cf2007-01-31 12:14:08 -0500701 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400703 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (req->rq_received && !req->rq_bytes_sent) {
705 err = req->rq_received;
706 goto out_unlock;
707 }
Chuck Lever12a80462005-08-25 16:25:51 -0700708 if (!xprt->ops->reserve_xprt(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 err = -EAGAIN;
710 goto out_unlock;
711 }
712
713 if (!xprt_connected(xprt)) {
714 err = -ENOTCONN;
715 goto out_unlock;
716 }
717out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400718 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return err;
720}
721
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400722void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700723{
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400724 xprt_release_write(task->tk_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700725}
726
Chuck Lever9903cd12005-08-11 16:25:26 -0400727/**
728 * xprt_transmit - send an RPC request on a transport
729 * @task: controlling RPC task
730 *
731 * We have to copy the iovec because sendmsg fiddles with its contents.
732 */
733void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct rpc_rqst *req = task->tk_rqstp;
736 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400737 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Chuck Lever46121cf2007-01-31 12:14:08 -0500739 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!req->rq_received) {
742 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400743 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* Update the softirq receive buffer */
745 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
746 sizeof(req->rq_private_buf));
747 /* Add request to the receive list */
748 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400749 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000751 /* Turn off autodisconnect */
752 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 } else if (!req->rq_bytes_sent)
755 return;
756
Chuck Levera246b012005-08-11 16:25:23 -0400757 status = xprt->ops->send_request(task);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700758 if (status == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500759 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700760 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262ca072006-03-20 13:44:16 -0500761
Chuck Leverfe3aca22005-08-25 16:25:50 -0700762 xprt->ops->set_retrans_timeout(task);
Chuck Lever262ca072006-03-20 13:44:16 -0500763
764 xprt->stat.sends++;
765 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
766 xprt->stat.bklog_u += xprt->backlog.qlen;
767
Chuck Leverfe3aca22005-08-25 16:25:50 -0700768 /* Don't race with disconnect */
769 if (!xprt_connected(xprt))
770 task->tk_status = -ENOTCONN;
771 else if (!req->rq_received)
772 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700773 spin_unlock_bh(&xprt->transport_lock);
774 return;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* Note: at this point, task->tk_sleeping has not yet been set,
778 * hence there is no danger of the waking up task being put on
779 * schedq, and being picked up by a parallel run of rpciod().
780 */
781 task->tk_status = status;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400782 if (status == -ECONNREFUSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Chuck Lever9903cd12005-08-11 16:25:26 -0400786static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct rpc_xprt *xprt = task->tk_xprt;
789
790 task->tk_status = 0;
791 if (task->tk_rqstp)
792 return;
793 if (!list_empty(&xprt->free)) {
794 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
795 list_del_init(&req->rq_list);
796 task->tk_rqstp = req;
797 xprt_request_init(task, xprt);
798 return;
799 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500800 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 task->tk_status = -EAGAIN;
802 task->tk_timeout = 0;
803 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
804}
805
Chuck Lever9903cd12005-08-11 16:25:26 -0400806/**
807 * xprt_reserve - allocate an RPC request slot
808 * @task: RPC task requesting a slot allocation
809 *
810 * If no more slots are available, place the task on the transport's
811 * backlog queue.
812 */
813void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct rpc_xprt *xprt = task->tk_xprt;
816
817 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +0100818 spin_lock(&xprt->reserve_lock);
819 do_xprt_reserve(task);
820 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700823static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 return xprt->xid++;
826}
827
828static inline void xprt_init_xid(struct rpc_xprt *xprt)
829{
Chuck Leverbf3fcf82006-05-25 01:40:51 -0400830 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Chuck Lever9903cd12005-08-11 16:25:26 -0400833static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct rpc_rqst *req = task->tk_rqstp;
836
837 req->rq_timeout = xprt->timeout.to_initval;
838 req->rq_task = task;
839 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +0100840 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400842 req->rq_release_snd_buf = NULL;
Trond Myklebustda458282006-08-31 15:44:52 -0400843 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -0500844 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 req, ntohl(req->rq_xid));
846}
847
Chuck Lever9903cd12005-08-11 16:25:26 -0400848/**
849 * xprt_release - release an RPC request slot
850 * @task: task which is finished with the slot
851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400853void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct rpc_xprt *xprt = task->tk_xprt;
856 struct rpc_rqst *req;
857
858 if (!(req = task->tk_rqstp))
859 return;
Chuck Lever11c556b2006-03-20 13:44:22 -0500860 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400861 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700862 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -0700863 if (xprt->ops->release_request)
864 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!list_empty(&req->rq_list))
866 list_del(&req->rq_list);
867 xprt->last_used = jiffies;
Trond Myklebust0065db32006-01-03 09:55:56 +0100868 if (list_empty(&xprt->recv))
Chuck Levera246b012005-08-11 16:25:23 -0400869 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -0700870 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400871 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400872 xprt->ops->buf_free(req->rq_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400874 if (req->rq_release_snd_buf)
875 req->rq_release_snd_buf(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 memset(req, 0, sizeof(*req)); /* mark unused */
877
Chuck Lever46121cf2007-01-31 12:14:08 -0500878 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Chuck Lever5dc07722005-08-11 16:25:35 -0400880 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 list_add(&req->rq_list, &xprt->free);
Chuck Lever555ee3a2005-08-25 16:25:54 -0700882 rpc_wake_up_next(&xprt->backlog);
Chuck Lever5dc07722005-08-11 16:25:35 -0400883 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Chuck Lever9903cd12005-08-11 16:25:26 -0400886/**
887 * xprt_set_timeout - set constant RPC timeout
888 * @to: RPC timeout parameters to set up
889 * @retr: number of retries
890 * @incr: amount of increase after each retry
891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400893void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800895 to->to_initval =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 to->to_increment = incr;
Chuck Levereab5c082005-08-11 16:25:14 -0400897 to->to_maxval = to->to_initval + (incr * retr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 to->to_retries = retr;
899 to->to_exponential = 0;
900}
901
Chuck Leverc2866762006-08-22 20:06:20 -0400902/**
903 * xprt_create_transport - create an RPC transport
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200904 * @args: rpc transport creation arguments
Chuck Leverc2866762006-08-22 20:06:20 -0400905 *
906 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200907struct rpc_xprt *xprt_create_transport(struct rpc_xprtsock_create *args)
Chuck Leverc2866762006-08-22 20:06:20 -0400908{
Chuck Leverc2866762006-08-22 20:06:20 -0400909 struct rpc_xprt *xprt;
910 struct rpc_rqst *req;
911
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200912 switch (args->proto) {
Chuck Leverc2866762006-08-22 20:06:20 -0400913 case IPPROTO_UDP:
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200914 xprt = xs_setup_udp(args);
Chuck Leverc2866762006-08-22 20:06:20 -0400915 break;
916 case IPPROTO_TCP:
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200917 xprt = xs_setup_tcp(args);
Chuck Leverc2866762006-08-22 20:06:20 -0400918 break;
919 default:
920 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200921 args->proto);
Chuck Leverc2866762006-08-22 20:06:20 -0400922 return ERR_PTR(-EIO);
923 }
Chuck Leverc8541ec2006-10-17 14:44:27 -0400924 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500925 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -0400926 -PTR_ERR(xprt));
927 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -0400928 }
929
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400930 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -0400931 spin_lock_init(&xprt->transport_lock);
932 spin_lock_init(&xprt->reserve_lock);
933
934 INIT_LIST_HEAD(&xprt->free);
935 INIT_LIST_HEAD(&xprt->recv);
David Howells65f27f32006-11-22 14:55:48 +0000936 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Chuck Leverc2866762006-08-22 20:06:20 -0400937 init_timer(&xprt->timer);
938 xprt->timer.function = xprt_init_autodisconnect;
939 xprt->timer.data = (unsigned long) xprt;
940 xprt->last_used = jiffies;
941 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -0400942 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -0400943
944 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
945 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
946 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
947 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
948 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
949
950 /* initialize free list */
951 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
952 list_add(&req->rq_list, &xprt->free);
953
954 xprt_init_xid(xprt);
955
Chuck Lever46121cf2007-01-31 12:14:08 -0500956 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -0400957 xprt->max_reqs);
958
959 return xprt;
960}
961
Chuck Lever9903cd12005-08-11 16:25:26 -0400962/**
963 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400964 * @kref: kref for the transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -0400965 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400967static void xprt_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400969 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
970
Chuck Lever46121cf2007-01-31 12:14:08 -0500971 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +0100972 xprt->shutdown = 1;
973 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400974
975 /*
976 * Tear down transport state and free the rpc_xprt
977 */
Chuck Levera246b012005-08-11 16:25:23 -0400978 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400979}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400981/**
982 * xprt_put - release a reference to an RPC transport.
983 * @xprt: pointer to the transport
984 *
985 */
986void xprt_put(struct rpc_xprt *xprt)
987{
988 kref_put(&xprt->kref, xprt_destroy);
989}
990
991/**
992 * xprt_get - return a reference to an RPC transport.
993 * @xprt: pointer to the transport
994 *
995 */
996struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
997{
998 kref_get(&xprt->kref);
999 return xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}