blob: 8bc0d5acf0da3d76d0c8591c13a9ec34402604fe [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>
44#include <linux/random.h>
45
Chuck Levera246b012005-08-11 16:25:23 -040046#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Local variables
50 */
51
52#ifdef RPC_DEBUG
53# undef RPC_DEBUG_DATA
54# 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:
111 dprintk("RPC: %4d failed to lock transport %p\n",
112 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 Lever55aa4f52005-08-11 16:25:47 -0400161 dprintk("RPC: %4d 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;
284 dprintk("RPC: %4d xprt_cwnd_limited cong = %ld cwnd = %ld\n",
285 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 }
343 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
344 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) {
390 dprintk("RPC: write space: waking waiting task on xprt %p\n",
391 xprt);
392 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
413 *
414 * 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++;
462 pprintk("RPC: %lu retrans\n", jiffies);
463 } else {
464 req->rq_timeout = to->to_initval;
465 req->rq_retries = 0;
466 xprt_reset_majortimeo(req);
467 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400468 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400470 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 pprintk("RPC: %lu timeout\n", jiffies);
472 status = -ETIMEDOUT;
473 }
474
475 if (req->rq_timeout == 0) {
476 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
477 req->rq_timeout = 5 * HZ;
478 }
479 return status;
480}
481
Chuck Lever55aa4f52005-08-11 16:25:47 -0400482static void xprt_autoclose(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
485
486 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400487 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 xprt_release_write(xprt, NULL);
489}
490
Chuck Lever9903cd12005-08-11 16:25:26 -0400491/**
492 * xprt_disconnect - mark a transport as disconnected
493 * @xprt: transport to flag for disconnect
494 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Chuck Levera246b012005-08-11 16:25:23 -0400496void xprt_disconnect(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400499 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 xprt_clear_connected(xprt);
Chuck Lever44fbac22005-08-11 16:25:44 -0400501 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400502 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static void
506xprt_init_autodisconnect(unsigned long data)
507{
508 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
509
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400510 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!list_empty(&xprt->recv) || xprt->shutdown)
512 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400513 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400515 spin_unlock(&xprt->transport_lock);
Chuck Lever2226feb2005-08-11 16:25:38 -0400516 if (xprt_connecting(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xprt_release_write(xprt, NULL);
518 else
519 schedule_work(&xprt->task_cleanup);
520 return;
521out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400522 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Chuck Lever9903cd12005-08-11 16:25:26 -0400525/**
526 * xprt_connect - schedule a transport connect operation
527 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
529 */
530void xprt_connect(struct rpc_task *task)
531{
532 struct rpc_xprt *xprt = task->tk_xprt;
533
534 dprintk("RPC: %4d xprt_connect xprt %p %s connected\n", task->tk_pid,
535 xprt, (xprt_connected(xprt) ? "is" : "is not"));
536
537 if (xprt->shutdown) {
538 task->tk_status = -EIO;
539 return;
540 }
541 if (!xprt->addr.sin_port) {
542 task->tk_status = -EIO;
543 return;
544 }
545 if (!xprt_lock_write(xprt, task))
546 return;
547 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400548 xprt_release_write(xprt, task);
549 else {
550 if (task->tk_rqstp)
551 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Chuck Lever03bf4b72005-08-25 16:25:55 -0700553 task->tk_timeout = xprt->connect_timeout;
Chuck Levera246b012005-08-11 16:25:23 -0400554 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
555 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Chuck Lever9903cd12005-08-11 16:25:26 -0400560static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct rpc_xprt *xprt = task->tk_xprt;
563
564 if (task->tk_status >= 0) {
565 dprintk("RPC: %4d xprt_connect_status: connection established\n",
566 task->tk_pid);
567 return;
568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (task->tk_status) {
571 case -ECONNREFUSED:
572 case -ECONNRESET:
Chuck Lever23475d62005-08-11 16:25:08 -0400573 dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n",
574 task->tk_pid, task->tk_client->cl_server);
575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 case -ENOTCONN:
Chuck Lever23475d62005-08-11 16:25:08 -0400577 dprintk("RPC: %4d xprt_connect_status: connection broken\n",
578 task->tk_pid);
579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 case -ETIMEDOUT:
Chuck Lever23475d62005-08-11 16:25:08 -0400581 dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 task->tk_pid);
583 break;
584 default:
Chuck Lever23475d62005-08-11 16:25:08 -0400585 dprintk("RPC: %4d xprt_connect_status: error %d connecting to server %s\n",
586 task->tk_pid, -task->tk_status, task->tk_client->cl_server);
587 xprt_release_write(xprt, task);
588 task->tk_status = -EIO;
589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Chuck Lever23475d62005-08-11 16:25:08 -0400591
592 /* if soft mounted, just cause this RPC to fail */
593 if (RPC_IS_SOFT(task)) {
594 xprt_release_write(xprt, task);
595 task->tk_status = -EIO;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Chuck Lever9903cd12005-08-11 16:25:26 -0400599/**
600 * xprt_lookup_rqst - find an RPC request corresponding to an XID
601 * @xprt: transport on which the original request was transmitted
602 * @xid: RPC XID of incoming reply
603 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Chuck Levera246b012005-08-11 16:25:23 -0400605struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct list_head *pos;
608 struct rpc_rqst *req = NULL;
609
610 list_for_each(pos, &xprt->recv) {
611 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
612 if (entry->rq_xid == xid) {
613 req = entry;
614 break;
615 }
616 }
617 return req;
618}
619
Chuck Lever9903cd12005-08-11 16:25:26 -0400620/**
Chuck Lever1570c1e2005-08-25 16:25:52 -0700621 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
622 * @task: RPC request that recently completed
Chuck Lever9903cd12005-08-11 16:25:26 -0400623 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Chuck Lever1570c1e2005-08-25 16:25:52 -0700625void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700627 struct rpc_rqst *req = task->tk_rqstp;
628 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
629 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Chuck Lever1570c1e2005-08-25 16:25:52 -0700631 if (timer) {
632 if (req->rq_ntrans == 1)
633 rpc_update_rtt(rtt, timer,
634 (long)jiffies - req->rq_xtime);
635 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Chuck Lever1570c1e2005-08-25 16:25:52 -0700639/**
640 * xprt_complete_rqst - called when reply processing is complete
641 * @task: RPC request that recently completed
642 * @copied: actual number of bytes received from the transport
643 *
644 * Caller holds transport lock.
645 */
646void xprt_complete_rqst(struct rpc_task *task, int copied)
647{
648 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Chuck Lever1570c1e2005-08-25 16:25:52 -0700650 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
651 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 list_del_init(&req->rq_list);
654 req->rq_received = req->rq_private_buf.len = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Chuck Lever46c0ee82005-08-25 16:25:52 -0700658static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700660 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct rpc_xprt *xprt = req->rq_xprt;
662
Chuck Lever46c0ee82005-08-25 16:25:52 -0700663 dprintk("RPC: %4d xprt_timer\n", task->tk_pid);
664
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400665 spin_lock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700666 if (!req->rq_received) {
667 if (xprt->ops->timer)
668 xprt->ops->timer(task);
669 task->tk_status = -ETIMEDOUT;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 task->tk_timeout = 0;
672 rpc_wake_up_task(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400673 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Chuck Lever9903cd12005-08-11 16:25:26 -0400676/**
677 * xprt_prepare_transmit - reserve the transport before sending a request
678 * @task: RPC task about to send a request
679 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400681int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct rpc_rqst *req = task->tk_rqstp;
684 struct rpc_xprt *xprt = req->rq_xprt;
685 int err = 0;
686
687 dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid);
688
689 if (xprt->shutdown)
690 return -EIO;
691
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400692 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (req->rq_received && !req->rq_bytes_sent) {
694 err = req->rq_received;
695 goto out_unlock;
696 }
Chuck Lever12a80462005-08-25 16:25:51 -0700697 if (!xprt->ops->reserve_xprt(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 err = -EAGAIN;
699 goto out_unlock;
700 }
701
702 if (!xprt_connected(xprt)) {
703 err = -ENOTCONN;
704 goto out_unlock;
705 }
706out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400707 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return err;
709}
710
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700711void
712xprt_abort_transmit(struct rpc_task *task)
713{
714 struct rpc_xprt *xprt = task->tk_xprt;
715
716 xprt_release_write(xprt, task);
717}
718
Chuck Lever9903cd12005-08-11 16:25:26 -0400719/**
720 * xprt_transmit - send an RPC request on a transport
721 * @task: controlling RPC task
722 *
723 * We have to copy the iovec because sendmsg fiddles with its contents.
724 */
725void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct rpc_rqst *req = task->tk_rqstp;
728 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400729 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 dprintk("RPC: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 smp_rmb();
734 if (!req->rq_received) {
735 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400736 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Update the softirq receive buffer */
738 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
739 sizeof(req->rq_private_buf));
740 /* Add request to the receive list */
741 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400742 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000744 /* Turn off autodisconnect */
745 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 } else if (!req->rq_bytes_sent)
748 return;
749
Chuck Levera246b012005-08-11 16:25:23 -0400750 status = xprt->ops->send_request(task);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700751 if (status == 0) {
752 dprintk("RPC: %4d xmit complete\n", task->tk_pid);
753 spin_lock_bh(&xprt->transport_lock);
754 xprt->ops->set_retrans_timeout(task);
755 /* Don't race with disconnect */
756 if (!xprt_connected(xprt))
757 task->tk_status = -ENOTCONN;
758 else if (!req->rq_received)
759 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
Chuck Lever49e9a892005-08-25 16:25:51 -0700760 xprt->ops->release_xprt(xprt, task);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700761 spin_unlock_bh(&xprt->transport_lock);
762 return;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* Note: at this point, task->tk_sleeping has not yet been set,
766 * hence there is no danger of the waking up task being put on
767 * schedq, and being picked up by a parallel run of rpciod().
768 */
769 task->tk_status = status;
770
771 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
Chuck Levera246b012005-08-11 16:25:23 -0400774 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case -ENOTCONN:
776 return;
777 default:
Chuck Lever43118c22005-08-25 16:25:49 -0700778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 xprt_release_write(xprt, task);
781 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Chuck Lever9903cd12005-08-11 16:25:26 -0400784static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct rpc_xprt *xprt = task->tk_xprt;
787
788 task->tk_status = 0;
789 if (task->tk_rqstp)
790 return;
791 if (!list_empty(&xprt->free)) {
792 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
793 list_del_init(&req->rq_list);
794 task->tk_rqstp = req;
795 xprt_request_init(task, xprt);
796 return;
797 }
798 dprintk("RPC: waiting for request slot\n");
799 task->tk_status = -EAGAIN;
800 task->tk_timeout = 0;
801 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
802}
803
Chuck Lever9903cd12005-08-11 16:25:26 -0400804/**
805 * xprt_reserve - allocate an RPC request slot
806 * @task: RPC task requesting a slot allocation
807 *
808 * If no more slots are available, place the task on the transport's
809 * backlog queue.
810 */
811void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct rpc_xprt *xprt = task->tk_xprt;
814
815 task->tk_status = -EIO;
816 if (!xprt->shutdown) {
Chuck Lever5dc07722005-08-11 16:25:35 -0400817 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 do_xprt_reserve(task);
Chuck Lever5dc07722005-08-11 16:25:35 -0400819 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt)
824{
825 return xprt->xid++;
826}
827
828static inline void xprt_init_xid(struct rpc_xprt *xprt)
829{
830 get_random_bytes(&xprt->xid, sizeof(xprt->xid));
831}
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;
841 req->rq_bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400843 req->rq_release_snd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid,
845 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 Lever4a0f8c02005-08-11 16:25:32 -0400860 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700861 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -0700862 if (xprt->ops->release_request)
863 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!list_empty(&req->rq_list))
865 list_del(&req->rq_list);
866 xprt->last_used = jiffies;
867 if (list_empty(&xprt->recv) && !xprt->shutdown)
Chuck Levera246b012005-08-11 16:25:23 -0400868 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -0700869 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400870 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever02107142006-01-03 09:55:49 +0100871 xprt->ops->buf_free(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400873 if (req->rq_release_snd_buf)
874 req->rq_release_snd_buf(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 memset(req, 0, sizeof(*req)); /* mark unused */
876
877 dprintk("RPC: %4d release request %p\n", task->tk_pid, req);
878
Chuck Lever5dc07722005-08-11 16:25:35 -0400879 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 list_add(&req->rq_list, &xprt->free);
Chuck Lever555ee3a2005-08-25 16:25:54 -0700881 rpc_wake_up_next(&xprt->backlog);
Chuck Lever5dc07722005-08-11 16:25:35 -0400882 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Chuck Lever9903cd12005-08-11 16:25:26 -0400885/**
886 * xprt_set_timeout - set constant RPC timeout
887 * @to: RPC timeout parameters to set up
888 * @retr: number of retries
889 * @incr: amount of increase after each retry
890 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400892void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 to->to_initval =
895 to->to_increment = incr;
Chuck Levereab5c082005-08-11 16:25:14 -0400896 to->to_maxval = to->to_initval + (incr * retr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 to->to_retries = retr;
898 to->to_exponential = 0;
899}
900
Chuck Lever9903cd12005-08-11 16:25:26 -0400901static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Chuck Levera246b012005-08-11 16:25:23 -0400903 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct rpc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct rpc_rqst *req;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL)
908 return ERR_PTR(-ENOMEM);
909 memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 xprt->addr = *ap;
Chuck Levera246b012005-08-11 16:25:23 -0400912
913 switch (proto) {
914 case IPPROTO_UDP:
915 result = xs_setup_udp(xprt, to);
916 break;
917 case IPPROTO_TCP:
918 result = xs_setup_tcp(xprt, to);
919 break;
920 default:
921 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
922 proto);
923 result = -EIO;
924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Chuck Levera246b012005-08-11 16:25:23 -0400926 if (result) {
927 kfree(xprt);
928 return ERR_PTR(result);
929 }
930
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400931 spin_lock_init(&xprt->transport_lock);
Chuck Lever5dc07722005-08-11 16:25:35 -0400932 spin_lock_init(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 INIT_LIST_HEAD(&xprt->free);
935 INIT_LIST_HEAD(&xprt->recv);
Chuck Lever55aa4f52005-08-11 16:25:47 -0400936 INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 init_timer(&xprt->timer);
938 xprt->timer.function = xprt_init_autodisconnect;
939 xprt->timer.data = (unsigned long) xprt;
940 xprt->last_used = jiffies;
Chuck Lever555ee3a2005-08-25 16:25:54 -0700941 xprt->cwnd = RPC_INITCWND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
944 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
945 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
946 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
947
948 /* initialize free list */
Chuck Levera246b012005-08-11 16:25:23 -0400949 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 list_add(&req->rq_list, &xprt->free);
951
952 xprt_init_xid(xprt);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 dprintk("RPC: created transport %p with %u slots\n", xprt,
955 xprt->max_reqs);
956
957 return xprt;
958}
959
Chuck Lever9903cd12005-08-11 16:25:26 -0400960/**
961 * xprt_create_proto - create an RPC client transport
962 * @proto: requested transport protocol
963 * @sap: remote peer's address
964 * @to: timeout parameters for new transport
965 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400967struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct rpc_xprt *xprt;
970
971 xprt = xprt_setup(proto, sap, to);
972 if (IS_ERR(xprt))
973 dprintk("RPC: xprt_create_proto failed\n");
974 else
975 dprintk("RPC: xprt_create_proto created xprt %p\n", xprt);
976 return xprt;
977}
978
Chuck Lever9903cd12005-08-11 16:25:26 -0400979static void xprt_shutdown(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 xprt->shutdown = 1;
982 rpc_wake_up(&xprt->sending);
983 rpc_wake_up(&xprt->resend);
Chuck Lever44fbac22005-08-11 16:25:44 -0400984 xprt_wake_pending_tasks(xprt, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 rpc_wake_up(&xprt->backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 del_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Chuck Lever9903cd12005-08-11 16:25:26 -0400989/**
990 * xprt_destroy - destroy an RPC transport, killing off all requests.
991 * @xprt: transport to destroy
992 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400994int xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 dprintk("RPC: destroying transport %p\n", xprt);
997 xprt_shutdown(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400998 xprt->ops->destroy(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 kfree(xprt);
1000
1001 return 0;
1002}