blob: 5b05b73e4c1d3f7a52c189e05b1ba55461c3270f [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}
121
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100122static void xprt_clear_locked(struct rpc_xprt *xprt)
123{
124 xprt->snd_task = NULL;
125 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
126 smp_mb__before_clear_bit();
127 clear_bit(XPRT_LOCKED, &xprt->state);
128 smp_mb__after_clear_bit();
129 } else
130 schedule_work(&xprt->task_cleanup);
131}
132
Chuck Lever12a80462005-08-25 16:25:51 -0700133/*
134 * xprt_reserve_xprt_cong - serialize write access to transports
135 * @task: task that is requesting access to the transport
136 *
137 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
138 * integrated into the decision of whether a request is allowed to be
139 * woken up and given access to the transport.
140 */
141int xprt_reserve_xprt_cong(struct rpc_task *task)
142{
143 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct rpc_rqst *req = task->tk_rqstp;
145
Chuck Lever2226feb2005-08-11 16:25:38 -0400146 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (task == xprt->snd_task)
148 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out_sleep;
150 }
Chuck Lever12a80462005-08-25 16:25:51 -0700151 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xprt->snd_task = task;
153 if (req) {
154 req->rq_bytes_sent = 0;
155 req->rq_ntrans++;
156 }
157 return 1;
158 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100159 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500161 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 task->tk_timeout = 0;
163 task->tk_status = -EAGAIN;
164 if (req && req->rq_ntrans)
165 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
166 else
167 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
168 return 0;
169}
170
Chuck Lever12a80462005-08-25 16:25:51 -0700171static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int retval;
174
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400175 spin_lock_bh(&xprt->transport_lock);
Chuck Lever12a80462005-08-25 16:25:51 -0700176 retval = xprt->ops->reserve_xprt(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400177 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return retval;
179}
180
Chuck Lever12a80462005-08-25 16:25:51 -0700181static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700184 struct rpc_rqst *req;
185
186 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
187 return;
188
189 task = rpc_wake_up_next(&xprt->resend);
190 if (!task) {
191 task = rpc_wake_up_next(&xprt->sending);
192 if (!task)
193 goto out_unlock;
194 }
195
196 req = task->tk_rqstp;
197 xprt->snd_task = task;
198 if (req) {
199 req->rq_bytes_sent = 0;
200 req->rq_ntrans++;
201 }
202 return;
203
204out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100205 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700206}
207
208static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
209{
210 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Chuck Lever2226feb2005-08-11 16:25:38 -0400212 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700214 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto out_unlock;
216 task = rpc_wake_up_next(&xprt->resend);
217 if (!task) {
218 task = rpc_wake_up_next(&xprt->sending);
219 if (!task)
220 goto out_unlock;
221 }
Chuck Lever49e9a892005-08-25 16:25:51 -0700222 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct rpc_rqst *req = task->tk_rqstp;
224 xprt->snd_task = task;
225 if (req) {
226 req->rq_bytes_sent = 0;
227 req->rq_ntrans++;
228 }
229 return;
230 }
231out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100232 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Chuck Lever49e9a892005-08-25 16:25:51 -0700235/**
236 * xprt_release_xprt - allow other requests to use a transport
237 * @xprt: transport with other tasks potentially waiting
238 * @task: task that is releasing access to the transport
239 *
240 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700242void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100245 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 __xprt_lock_write_next(xprt);
247 }
248}
249
Chuck Lever49e9a892005-08-25 16:25:51 -0700250/**
251 * xprt_release_xprt_cong - allow other requests to use a transport
252 * @xprt: transport with other tasks potentially waiting
253 * @task: task that is releasing access to the transport
254 *
255 * Note that "task" can be NULL. Another task is awoken to use the
256 * transport if the transport's congestion window allows it.
257 */
258void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
259{
260 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100261 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700262 __xprt_lock_write_next_cong(xprt);
263 }
264}
265
266static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400268 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700269 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400270 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * Van Jacobson congestion avoidance. Check if the congestion window
275 * overflowed. Put the task to sleep if this is the case.
276 */
277static int
278__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
279{
280 struct rpc_rqst *req = task->tk_rqstp;
281
282 if (req->rq_cong)
283 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500284 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 task->tk_pid, xprt->cong, xprt->cwnd);
286 if (RPCXPRT_CONGESTED(xprt))
287 return 0;
288 req->rq_cong = 1;
289 xprt->cong += RPC_CWNDSCALE;
290 return 1;
291}
292
293/*
294 * Adjust the congestion window, and wake up the next task
295 * that has been sleeping due to congestion
296 */
297static void
298__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
299{
300 if (!req->rq_cong)
301 return;
302 req->rq_cong = 0;
303 xprt->cong -= RPC_CWNDSCALE;
Chuck Lever49e9a892005-08-25 16:25:51 -0700304 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Chuck Lever46c0ee82005-08-25 16:25:52 -0700307/**
Chuck Levera58dd392005-08-25 16:25:53 -0700308 * xprt_release_rqst_cong - housekeeping when request is complete
309 * @task: RPC request that recently completed
310 *
311 * Useful for transports that require congestion control.
312 */
313void xprt_release_rqst_cong(struct rpc_task *task)
314{
315 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
316}
317
318/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700319 * xprt_adjust_cwnd - adjust transport congestion window
320 * @task: recently completed RPC request used to adjust window
321 * @result: result code of completed RPC request
322 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
324 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700325void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700327 struct rpc_rqst *req = task->tk_rqstp;
328 struct rpc_xprt *xprt = task->tk_xprt;
329 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (result >= 0 && cwnd <= xprt->cong) {
332 /* The (cwnd >> 1) term makes sure
333 * the result gets rounded properly. */
334 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
335 if (cwnd > RPC_MAXCWND(xprt))
336 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700337 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 } else if (result == -ETIMEDOUT) {
339 cwnd >>= 1;
340 if (cwnd < RPC_CWNDSCALE)
341 cwnd = RPC_CWNDSCALE;
342 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500343 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 xprt->cong, xprt->cwnd, cwnd);
345 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700346 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Chuck Lever44fbac22005-08-11 16:25:44 -0400349/**
350 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
351 * @xprt: transport with waiting tasks
352 * @status: result code to plant in each task before waking it
353 *
354 */
355void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
356{
357 if (status < 0)
358 rpc_wake_up_status(&xprt->pending, status);
359 else
360 rpc_wake_up(&xprt->pending);
361}
362
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400363/**
364 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
365 * @task: task to be put to sleep
366 *
367 */
368void xprt_wait_for_buffer_space(struct rpc_task *task)
369{
370 struct rpc_rqst *req = task->tk_rqstp;
371 struct rpc_xprt *xprt = req->rq_xprt;
372
373 task->tk_timeout = req->rq_timeout;
374 rpc_sleep_on(&xprt->pending, task, NULL, NULL);
375}
376
377/**
378 * xprt_write_space - wake the task waiting for transport output buffer space
379 * @xprt: transport with waiting tasks
380 *
381 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
382 */
383void xprt_write_space(struct rpc_xprt *xprt)
384{
385 if (unlikely(xprt->shutdown))
386 return;
387
388 spin_lock_bh(&xprt->transport_lock);
389 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500390 dprintk("RPC: write space: waking waiting task on "
391 "xprt %p\n", xprt);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400392 rpc_wake_up_task(xprt->snd_task);
393 }
394 spin_unlock_bh(&xprt->transport_lock);
395}
396
Chuck Leverfe3aca22005-08-25 16:25:50 -0700397/**
398 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
399 * @task: task whose timeout is to be set
400 *
401 * Set a request's retransmit timeout based on the transport's
402 * default timeout parameters. Used by transports that don't adjust
403 * the retransmit timeout based on round-trip time estimation.
404 */
405void xprt_set_retrans_timeout_def(struct rpc_task *task)
406{
407 task->tk_timeout = task->tk_rqstp->rq_timeout;
408}
409
410/*
411 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
412 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800413 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700414 * Set a request's retransmit timeout using the RTT estimator.
415 */
416void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
417{
418 int timer = task->tk_msg.rpc_proc->p_timer;
419 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
420 struct rpc_rqst *req = task->tk_rqstp;
421 unsigned long max_timeout = req->rq_xprt->timeout.to_maxval;
422
423 task->tk_timeout = rpc_calc_rto(rtt, timer);
424 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
425 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
426 task->tk_timeout = max_timeout;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void xprt_reset_majortimeo(struct rpc_rqst *req)
430{
431 struct rpc_timeout *to = &req->rq_xprt->timeout;
432
433 req->rq_majortimeo = req->rq_timeout;
434 if (to->to_exponential)
435 req->rq_majortimeo <<= to->to_retries;
436 else
437 req->rq_majortimeo += to->to_increment * to->to_retries;
438 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
439 req->rq_majortimeo = to->to_maxval;
440 req->rq_majortimeo += jiffies;
441}
442
Chuck Lever9903cd12005-08-11 16:25:26 -0400443/**
444 * xprt_adjust_timeout - adjust timeout values for next retransmit
445 * @req: RPC request containing parameters to use for the adjustment
446 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448int xprt_adjust_timeout(struct rpc_rqst *req)
449{
450 struct rpc_xprt *xprt = req->rq_xprt;
451 struct rpc_timeout *to = &xprt->timeout;
452 int status = 0;
453
454 if (time_before(jiffies, req->rq_majortimeo)) {
455 if (to->to_exponential)
456 req->rq_timeout <<= 1;
457 else
458 req->rq_timeout += to->to_increment;
459 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
460 req->rq_timeout = to->to_maxval;
461 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
463 req->rq_timeout = to->to_initval;
464 req->rq_retries = 0;
465 xprt_reset_majortimeo(req);
466 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400467 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400469 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 status = -ETIMEDOUT;
471 }
472
473 if (req->rq_timeout == 0) {
474 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
475 req->rq_timeout = 5 * HZ;
476 }
477 return status;
478}
479
David Howells65f27f32006-11-22 14:55:48 +0000480static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
David Howells65f27f32006-11-22 14:55:48 +0000482 struct rpc_xprt *xprt =
483 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400486 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 xprt_release_write(xprt, NULL);
488}
489
Chuck Lever9903cd12005-08-11 16:25:26 -0400490/**
491 * xprt_disconnect - mark a transport as disconnected
492 * @xprt: transport to flag for disconnect
493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Chuck Levera246b012005-08-11 16:25:23 -0400495void xprt_disconnect(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Chuck Lever46121cf2007-01-31 12:14:08 -0500497 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400498 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 xprt_clear_connected(xprt);
Chuck Lever44fbac22005-08-11 16:25:44 -0400500 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400501 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void
505xprt_init_autodisconnect(unsigned long data)
506{
507 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
508
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400509 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!list_empty(&xprt->recv) || xprt->shutdown)
511 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400512 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400514 spin_unlock(&xprt->transport_lock);
Chuck Lever2226feb2005-08-11 16:25:38 -0400515 if (xprt_connecting(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 xprt_release_write(xprt, NULL);
517 else
518 schedule_work(&xprt->task_cleanup);
519 return;
520out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400521 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Chuck Lever9903cd12005-08-11 16:25:26 -0400524/**
525 * xprt_connect - schedule a transport connect operation
526 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
528 */
529void xprt_connect(struct rpc_task *task)
530{
531 struct rpc_xprt *xprt = task->tk_xprt;
532
Chuck Lever46121cf2007-01-31 12:14:08 -0500533 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 xprt, (xprt_connected(xprt) ? "is" : "is not"));
535
Chuck Leverec739ef2006-08-22 20:06:15 -0400536 if (!xprt_bound(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 task->tk_status = -EIO;
538 return;
539 }
540 if (!xprt_lock_write(xprt, task))
541 return;
542 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400543 xprt_release_write(xprt, task);
544 else {
545 if (task->tk_rqstp)
546 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Chuck Lever03bf4b72005-08-25 16:25:55 -0700548 task->tk_timeout = xprt->connect_timeout;
Chuck Levera246b012005-08-11 16:25:23 -0400549 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
Chuck Lever262ca072006-03-20 13:44:16 -0500550 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400551 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Chuck Lever9903cd12005-08-11 16:25:26 -0400556static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct rpc_xprt *xprt = task->tk_xprt;
559
560 if (task->tk_status >= 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500561 xprt->stat.connect_count++;
562 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500563 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 task->tk_pid);
565 return;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 switch (task->tk_status) {
569 case -ECONNREFUSED:
570 case -ECONNRESET:
Chuck Lever46121cf2007-01-31 12:14:08 -0500571 dprintk("RPC: %5u xprt_connect_status: server %s refused "
572 "connection\n", task->tk_pid,
573 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 case -ENOTCONN:
Chuck Lever46121cf2007-01-31 12:14:08 -0500576 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
Chuck Lever23475d62005-08-11 16:25:08 -0400577 task->tk_pid);
578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500580 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
581 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500584 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
585 "server %s\n", task->tk_pid, -task->tk_status,
586 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400587 xprt_release_write(xprt, task);
588 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Chuck Lever9903cd12005-08-11 16:25:26 -0400592/**
593 * xprt_lookup_rqst - find an RPC request corresponding to an XID
594 * @xprt: transport on which the original request was transmitted
595 * @xid: RPC XID of incoming reply
596 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700598struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 list_for_each(pos, &xprt->recv) {
603 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
Chuck Lever262ca072006-03-20 13:44:16 -0500604 if (entry->rq_xid == xid)
605 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500607
608 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
609 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500610 xprt->stat.bad_xids++;
611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Chuck Lever9903cd12005-08-11 16:25:26 -0400614/**
Chuck Lever1570c1e2005-08-25 16:25:52 -0700615 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
616 * @task: RPC request that recently completed
Chuck Lever9903cd12005-08-11 16:25:26 -0400617 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Chuck Lever1570c1e2005-08-25 16:25:52 -0700619void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700621 struct rpc_rqst *req = task->tk_rqstp;
622 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
623 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chuck Lever1570c1e2005-08-25 16:25:52 -0700625 if (timer) {
626 if (req->rq_ntrans == 1)
627 rpc_update_rtt(rtt, timer,
628 (long)jiffies - req->rq_xtime);
629 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Chuck Lever1570c1e2005-08-25 16:25:52 -0700633/**
634 * xprt_complete_rqst - called when reply processing is complete
635 * @task: RPC request that recently completed
636 * @copied: actual number of bytes received from the transport
637 *
638 * Caller holds transport lock.
639 */
640void xprt_complete_rqst(struct rpc_task *task, int copied)
641{
642 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Chuck Lever1570c1e2005-08-25 16:25:52 -0700644 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
645 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Chuck Lever262ca072006-03-20 13:44:16 -0500647 task->tk_xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -0500648 task->tk_rtt = (long)jiffies - req->rq_xtime;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 list_del_init(&req->rq_list);
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500651 /* Ensure all writes are done before we update req->rq_received */
652 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 req->rq_received = req->rq_private_buf.len = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Chuck Lever46c0ee82005-08-25 16:25:52 -0700657static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700659 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct rpc_xprt *xprt = req->rq_xprt;
661
Chuck Lever46121cf2007-01-31 12:14:08 -0500662 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700663
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400664 spin_lock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700665 if (!req->rq_received) {
666 if (xprt->ops->timer)
667 xprt->ops->timer(task);
668 task->tk_status = -ETIMEDOUT;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 task->tk_timeout = 0;
671 rpc_wake_up_task(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400672 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Chuck Lever9903cd12005-08-11 16:25:26 -0400675/**
676 * xprt_prepare_transmit - reserve the transport before sending a request
677 * @task: RPC task about to send a request
678 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400680int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct rpc_rqst *req = task->tk_rqstp;
683 struct rpc_xprt *xprt = req->rq_xprt;
684 int err = 0;
685
Chuck Lever46121cf2007-01-31 12:14:08 -0500686 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400688 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (req->rq_received && !req->rq_bytes_sent) {
690 err = req->rq_received;
691 goto out_unlock;
692 }
Chuck Lever12a80462005-08-25 16:25:51 -0700693 if (!xprt->ops->reserve_xprt(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 err = -EAGAIN;
695 goto out_unlock;
696 }
697
698 if (!xprt_connected(xprt)) {
699 err = -ENOTCONN;
700 goto out_unlock;
701 }
702out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400703 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return err;
705}
706
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400707void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700708{
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400709 xprt_release_write(task->tk_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700710}
711
Chuck Lever9903cd12005-08-11 16:25:26 -0400712/**
713 * xprt_transmit - send an RPC request on a transport
714 * @task: controlling RPC task
715 *
716 * We have to copy the iovec because sendmsg fiddles with its contents.
717 */
718void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct rpc_rqst *req = task->tk_rqstp;
721 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400722 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Chuck Lever46121cf2007-01-31 12:14:08 -0500724 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!req->rq_received) {
727 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400728 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Update the softirq receive buffer */
730 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
731 sizeof(req->rq_private_buf));
732 /* Add request to the receive list */
733 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400734 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000736 /* Turn off autodisconnect */
737 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 } else if (!req->rq_bytes_sent)
740 return;
741
Chuck Levera246b012005-08-11 16:25:23 -0400742 status = xprt->ops->send_request(task);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700743 if (status == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500744 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700745 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262ca072006-03-20 13:44:16 -0500746
Chuck Leverfe3aca22005-08-25 16:25:50 -0700747 xprt->ops->set_retrans_timeout(task);
Chuck Lever262ca072006-03-20 13:44:16 -0500748
749 xprt->stat.sends++;
750 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
751 xprt->stat.bklog_u += xprt->backlog.qlen;
752
Chuck Leverfe3aca22005-08-25 16:25:50 -0700753 /* Don't race with disconnect */
754 if (!xprt_connected(xprt))
755 task->tk_status = -ENOTCONN;
756 else if (!req->rq_received)
757 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700758 spin_unlock_bh(&xprt->transport_lock);
759 return;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Note: at this point, task->tk_sleeping has not yet been set,
763 * hence there is no danger of the waking up task being put on
764 * schedq, and being picked up by a parallel run of rpciod().
765 */
766 task->tk_status = status;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400767 if (status == -ECONNREFUSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Chuck Lever9903cd12005-08-11 16:25:26 -0400771static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct rpc_xprt *xprt = task->tk_xprt;
774
775 task->tk_status = 0;
776 if (task->tk_rqstp)
777 return;
778 if (!list_empty(&xprt->free)) {
779 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
780 list_del_init(&req->rq_list);
781 task->tk_rqstp = req;
782 xprt_request_init(task, xprt);
783 return;
784 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500785 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 task->tk_status = -EAGAIN;
787 task->tk_timeout = 0;
788 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
789}
790
Chuck Lever9903cd12005-08-11 16:25:26 -0400791/**
792 * xprt_reserve - allocate an RPC request slot
793 * @task: RPC task requesting a slot allocation
794 *
795 * If no more slots are available, place the task on the transport's
796 * backlog queue.
797 */
798void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 struct rpc_xprt *xprt = task->tk_xprt;
801
802 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +0100803 spin_lock(&xprt->reserve_lock);
804 do_xprt_reserve(task);
805 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700808static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 return xprt->xid++;
811}
812
813static inline void xprt_init_xid(struct rpc_xprt *xprt)
814{
Chuck Leverbf3fcf82006-05-25 01:40:51 -0400815 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Chuck Lever9903cd12005-08-11 16:25:26 -0400818static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct rpc_rqst *req = task->tk_rqstp;
821
822 req->rq_timeout = xprt->timeout.to_initval;
823 req->rq_task = task;
824 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +0100825 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400827 req->rq_release_snd_buf = NULL;
Trond Myklebustda458282006-08-31 15:44:52 -0400828 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -0500829 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 req, ntohl(req->rq_xid));
831}
832
Chuck Lever9903cd12005-08-11 16:25:26 -0400833/**
834 * xprt_release - release an RPC request slot
835 * @task: task which is finished with the slot
836 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400838void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct rpc_xprt *xprt = task->tk_xprt;
841 struct rpc_rqst *req;
842
843 if (!(req = task->tk_rqstp))
844 return;
Chuck Lever11c556b2006-03-20 13:44:22 -0500845 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400846 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700847 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -0700848 if (xprt->ops->release_request)
849 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!list_empty(&req->rq_list))
851 list_del(&req->rq_list);
852 xprt->last_used = jiffies;
Trond Myklebust0065db32006-01-03 09:55:56 +0100853 if (list_empty(&xprt->recv))
Chuck Levera246b012005-08-11 16:25:23 -0400854 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -0700855 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400856 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400857 xprt->ops->buf_free(req->rq_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400859 if (req->rq_release_snd_buf)
860 req->rq_release_snd_buf(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 memset(req, 0, sizeof(*req)); /* mark unused */
862
Chuck Lever46121cf2007-01-31 12:14:08 -0500863 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Chuck Lever5dc07722005-08-11 16:25:35 -0400865 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 list_add(&req->rq_list, &xprt->free);
Chuck Lever555ee3a2005-08-25 16:25:54 -0700867 rpc_wake_up_next(&xprt->backlog);
Chuck Lever5dc07722005-08-11 16:25:35 -0400868 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Chuck Lever9903cd12005-08-11 16:25:26 -0400871/**
872 * xprt_set_timeout - set constant RPC timeout
873 * @to: RPC timeout parameters to set up
874 * @retr: number of retries
875 * @incr: amount of increase after each retry
876 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400878void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800880 to->to_initval =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 to->to_increment = incr;
Chuck Levereab5c082005-08-11 16:25:14 -0400882 to->to_maxval = to->to_initval + (incr * retr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 to->to_retries = retr;
884 to->to_exponential = 0;
885}
886
Chuck Leverc2866762006-08-22 20:06:20 -0400887/**
888 * xprt_create_transport - create an RPC transport
889 * @proto: requested transport protocol
890 * @ap: remote peer address
891 * @size: length of address
892 * @to: timeout parameters
893 *
894 */
895struct rpc_xprt *xprt_create_transport(int proto, struct sockaddr *ap, size_t size, struct rpc_timeout *to)
896{
Chuck Leverc2866762006-08-22 20:06:20 -0400897 struct rpc_xprt *xprt;
898 struct rpc_rqst *req;
899
Chuck Leverc2866762006-08-22 20:06:20 -0400900 switch (proto) {
901 case IPPROTO_UDP:
Chuck Leverc8541ec2006-10-17 14:44:27 -0400902 xprt = xs_setup_udp(ap, size, to);
Chuck Leverc2866762006-08-22 20:06:20 -0400903 break;
904 case IPPROTO_TCP:
Chuck Leverc8541ec2006-10-17 14:44:27 -0400905 xprt = xs_setup_tcp(ap, size, to);
Chuck Leverc2866762006-08-22 20:06:20 -0400906 break;
907 default:
908 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
909 proto);
910 return ERR_PTR(-EIO);
911 }
Chuck Leverc8541ec2006-10-17 14:44:27 -0400912 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500913 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -0400914 -PTR_ERR(xprt));
915 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -0400916 }
917
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400918 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -0400919 spin_lock_init(&xprt->transport_lock);
920 spin_lock_init(&xprt->reserve_lock);
921
922 INIT_LIST_HEAD(&xprt->free);
923 INIT_LIST_HEAD(&xprt->recv);
David Howells65f27f32006-11-22 14:55:48 +0000924 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Chuck Leverc2866762006-08-22 20:06:20 -0400925 init_timer(&xprt->timer);
926 xprt->timer.function = xprt_init_autodisconnect;
927 xprt->timer.data = (unsigned long) xprt;
928 xprt->last_used = jiffies;
929 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -0400930 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -0400931
932 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
933 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
934 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
935 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
936 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
937
938 /* initialize free list */
939 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
940 list_add(&req->rq_list, &xprt->free);
941
942 xprt_init_xid(xprt);
943
Chuck Lever46121cf2007-01-31 12:14:08 -0500944 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -0400945 xprt->max_reqs);
946
947 return xprt;
948}
949
Chuck Lever9903cd12005-08-11 16:25:26 -0400950/**
951 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400952 * @kref: kref for the transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -0400953 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400955static void xprt_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400957 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
958
Chuck Lever46121cf2007-01-31 12:14:08 -0500959 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +0100960 xprt->shutdown = 1;
961 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400962
963 /*
964 * Tear down transport state and free the rpc_xprt
965 */
Chuck Levera246b012005-08-11 16:25:23 -0400966 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400969/**
970 * xprt_put - release a reference to an RPC transport.
971 * @xprt: pointer to the transport
972 *
973 */
974void xprt_put(struct rpc_xprt *xprt)
975{
976 kref_put(&xprt->kref, xprt_destroy);
977}
978
979/**
980 * xprt_get - return a reference to an RPC transport.
981 * @xprt: pointer to the transport
982 *
983 */
984struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
985{
986 kref_get(&xprt->kref);
987 return xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}