SUNRPC: add EXPORT_SYMBOL_GPL for generic transport functions
[deliverable/linux.git] / net / sunrpc / xprt.c
CommitLineData
1da177e4
LT
1/*
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
55aa4f58
CL
13 * 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
1da177e4
LT
16 * is run after the packet's timeout has expired.
17 * - When a packet arrives, the data_ready handler walks the list of
55aa4f58 18 * pending requests for that transport. If a matching XID is found, the
1da177e4
LT
19 * 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>
55aa4f58
CL
35 *
36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
1da177e4
LT
37 */
38
a246b010
CL
39#include <linux/module.h>
40
1da177e4 41#include <linux/types.h>
a246b010 42#include <linux/interrupt.h>
1da177e4 43#include <linux/workqueue.h>
bf3fcf89 44#include <linux/net.h>
1da177e4 45
a246b010 46#include <linux/sunrpc/clnt.h>
11c556b3 47#include <linux/sunrpc/metrics.h>
1da177e4
LT
48
49/*
50 * Local variables
51 */
52
53#ifdef RPC_DEBUG
1da177e4
LT
54# define RPCDBG_FACILITY RPCDBG_XPRT
55#endif
56
1da177e4
LT
57/*
58 * Local functions
59 */
60static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
61static inline void do_xprt_reserve(struct rpc_task *);
1da177e4 62static void xprt_connect_status(struct rpc_task *task);
1da177e4
LT
63static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
64
555ee3af
CL
65/*
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)
1da177e4 82
12a80469
CL
83/**
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.
90 */
91int xprt_reserve_xprt(struct rpc_task *task)
92{
93 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:
46121cf7 111 dprintk("RPC: %5u failed to lock transport %p\n",
12a80469
CL
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}
12444809 121EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
12a80469 122
632e3bdc
TM
123static 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
c1384c9c 131 queue_work(rpciod_workqueue, &xprt->task_cleanup);
632e3bdc
TM
132}
133
1da177e4 134/*
12a80469
CL
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.
1da177e4 141 */
12a80469 142int xprt_reserve_xprt_cong(struct rpc_task *task)
1da177e4 143{
12a80469 144 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4
LT
145 struct rpc_rqst *req = task->tk_rqstp;
146
2226feb6 147 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
1da177e4
LT
148 if (task == xprt->snd_task)
149 return 1;
1da177e4
LT
150 goto out_sleep;
151 }
12a80469 152 if (__xprt_get_cong(xprt, task)) {
1da177e4
LT
153 xprt->snd_task = task;
154 if (req) {
155 req->rq_bytes_sent = 0;
156 req->rq_ntrans++;
157 }
158 return 1;
159 }
632e3bdc 160 xprt_clear_locked(xprt);
1da177e4 161out_sleep:
46121cf7 162 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
1da177e4
LT
163 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}
12444809 171EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
1da177e4 172
12a80469 173static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
174{
175 int retval;
176
4a0f8c04 177 spin_lock_bh(&xprt->transport_lock);
12a80469 178 retval = xprt->ops->reserve_xprt(task);
4a0f8c04 179 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
180 return retval;
181}
182
12a80469 183static void __xprt_lock_write_next(struct rpc_xprt *xprt)
49e9a890
CL
184{
185 struct rpc_task *task;
186 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:
632e3bdc 207 xprt_clear_locked(xprt);
49e9a890
CL
208}
209
210static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
1da177e4
LT
211{
212 struct rpc_task *task;
213
2226feb6 214 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 215 return;
49e9a890 216 if (RPCXPRT_CONGESTED(xprt))
1da177e4
LT
217 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 }
49e9a890 224 if (__xprt_get_cong(xprt, task)) {
1da177e4
LT
225 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:
632e3bdc 234 xprt_clear_locked(xprt);
1da177e4
LT
235}
236
49e9a890
CL
237/**
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.
1da177e4 243 */
49e9a890 244void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
245{
246 if (xprt->snd_task == task) {
632e3bdc 247 xprt_clear_locked(xprt);
1da177e4
LT
248 __xprt_lock_write_next(xprt);
249 }
250}
12444809 251EXPORT_SYMBOL_GPL(xprt_release_xprt);
1da177e4 252
49e9a890
CL
253/**
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) {
632e3bdc 264 xprt_clear_locked(xprt);
49e9a890
CL
265 __xprt_lock_write_next_cong(xprt);
266 }
267}
12444809 268EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
49e9a890
CL
269
270static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4 271{
4a0f8c04 272 spin_lock_bh(&xprt->transport_lock);
49e9a890 273 xprt->ops->release_xprt(xprt, task);
4a0f8c04 274 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
275}
276
1da177e4
LT
277/*
278 * 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;
46121cf7 288 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
1da177e4
LT
289 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;
49e9a890 308 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
309}
310
a58dd398
CL
311/**
312 * 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}
12444809 321EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
a58dd398 322
46c0ee8b
CL
323/**
324 * 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 *
1da177e4
LT
328 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
329 */
46c0ee8b 330void xprt_adjust_cwnd(struct rpc_task *task, int result)
1da177e4 331{
46c0ee8b
CL
332 struct rpc_rqst *req = task->tk_rqstp;
333 struct rpc_xprt *xprt = task->tk_xprt;
334 unsigned long cwnd = xprt->cwnd;
1da177e4 335
1da177e4
LT
336 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);
49e9a890 342 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
343 } else if (result == -ETIMEDOUT) {
344 cwnd >>= 1;
345 if (cwnd < RPC_CWNDSCALE)
346 cwnd = RPC_CWNDSCALE;
347 }
46121cf7 348 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
1da177e4
LT
349 xprt->cong, xprt->cwnd, cwnd);
350 xprt->cwnd = cwnd;
46c0ee8b 351 __xprt_put_cong(xprt, req);
1da177e4 352}
12444809 353EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
1da177e4 354
44fbac22
CL
355/**
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}
12444809 368EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
44fbac22 369
c7b2cae8
CL
370/**
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}
12444809 383EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
c7b2cae8
CL
384
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) {
46121cf7
CL
398 dprintk("RPC: write space: waking waiting task on "
399 "xprt %p\n", xprt);
c7b2cae8
CL
400 rpc_wake_up_task(xprt->snd_task);
401 }
402 spin_unlock_bh(&xprt->transport_lock);
403}
12444809 404EXPORT_SYMBOL_GPL(xprt_write_space);
c7b2cae8 405
fe3aca29
CL
406/**
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}
12444809 418EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
fe3aca29
CL
419
420/*
421 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
422 * @task: task whose timeout is to be set
cca5172a 423 *
fe3aca29
CL
424 * 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}
12444809 438EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
fe3aca29 439
1da177e4
LT
440static 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
9903cd1c
CL
454/**
455 * xprt_adjust_timeout - adjust timeout values for next retransmit
456 * @req: RPC request containing parameters to use for the adjustment
457 *
1da177e4
LT
458 */
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++;
1da177e4
LT
473 } 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" */
4a0f8c04 478 spin_lock_bh(&xprt->transport_lock);
1da177e4 479 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
4a0f8c04 480 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
481 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
65f27f38 491static void xprt_autoclose(struct work_struct *work)
1da177e4 492{
65f27f38
DH
493 struct rpc_xprt *xprt =
494 container_of(work, struct rpc_xprt, task_cleanup);
1da177e4
LT
495
496 xprt_disconnect(xprt);
a246b010 497 xprt->ops->close(xprt);
1da177e4
LT
498 xprt_release_write(xprt, NULL);
499}
500
9903cd1c
CL
501/**
502 * xprt_disconnect - mark a transport as disconnected
503 * @xprt: transport to flag for disconnect
504 *
1da177e4 505 */
a246b010 506void xprt_disconnect(struct rpc_xprt *xprt)
1da177e4 507{
46121cf7 508 dprintk("RPC: disconnected transport %p\n", xprt);
4a0f8c04 509 spin_lock_bh(&xprt->transport_lock);
1da177e4 510 xprt_clear_connected(xprt);
44fbac22 511 xprt_wake_pending_tasks(xprt, -ENOTCONN);
4a0f8c04 512 spin_unlock_bh(&xprt->transport_lock);
1da177e4 513}
12444809 514EXPORT_SYMBOL_GPL(xprt_disconnect);
1da177e4 515
1da177e4
LT
516static void
517xprt_init_autodisconnect(unsigned long data)
518{
519 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
520
4a0f8c04 521 spin_lock(&xprt->transport_lock);
1da177e4
LT
522 if (!list_empty(&xprt->recv) || xprt->shutdown)
523 goto out_abort;
2226feb6 524 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 525 goto out_abort;
4a0f8c04 526 spin_unlock(&xprt->transport_lock);
2226feb6 527 if (xprt_connecting(xprt))
1da177e4
LT
528 xprt_release_write(xprt, NULL);
529 else
c1384c9c 530 queue_work(rpciod_workqueue, &xprt->task_cleanup);
1da177e4
LT
531 return;
532out_abort:
4a0f8c04 533 spin_unlock(&xprt->transport_lock);
1da177e4
LT
534}
535
9903cd1c
CL
536/**
537 * xprt_connect - schedule a transport connect operation
538 * @task: RPC task that is requesting the connect
1da177e4
LT
539 *
540 */
541void xprt_connect(struct rpc_task *task)
542{
543 struct rpc_xprt *xprt = task->tk_xprt;
544
46121cf7 545 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
1da177e4
LT
546 xprt, (xprt_connected(xprt) ? "is" : "is not"));
547
ec739ef0 548 if (!xprt_bound(xprt)) {
1da177e4
LT
549 task->tk_status = -EIO;
550 return;
551 }
552 if (!xprt_lock_write(xprt, task))
553 return;
554 if (xprt_connected(xprt))
a246b010
CL
555 xprt_release_write(xprt, task);
556 else {
557 if (task->tk_rqstp)
558 task->tk_rqstp->rq_bytes_sent = 0;
1da177e4 559
03bf4b70 560 task->tk_timeout = xprt->connect_timeout;
a246b010 561 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
262ca07d 562 xprt->stat.connect_start = jiffies;
a246b010 563 xprt->ops->connect(task);
1da177e4
LT
564 }
565 return;
1da177e4
LT
566}
567
9903cd1c 568static void xprt_connect_status(struct rpc_task *task)
1da177e4
LT
569{
570 struct rpc_xprt *xprt = task->tk_xprt;
571
572 if (task->tk_status >= 0) {
262ca07d
CL
573 xprt->stat.connect_count++;
574 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
46121cf7 575 dprintk("RPC: %5u xprt_connect_status: connection established\n",
1da177e4
LT
576 task->tk_pid);
577 return;
578 }
579
1da177e4
LT
580 switch (task->tk_status) {
581 case -ECONNREFUSED:
582 case -ECONNRESET:
46121cf7
CL
583 dprintk("RPC: %5u xprt_connect_status: server %s refused "
584 "connection\n", task->tk_pid,
585 task->tk_client->cl_server);
23475d66 586 break;
1da177e4 587 case -ENOTCONN:
46121cf7 588 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
23475d66
CL
589 task->tk_pid);
590 break;
1da177e4 591 case -ETIMEDOUT:
46121cf7
CL
592 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
593 "out\n", task->tk_pid);
1da177e4
LT
594 break;
595 default:
46121cf7
CL
596 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);
23475d66
CL
599 xprt_release_write(xprt, task);
600 task->tk_status = -EIO;
1da177e4 601 }
1da177e4
LT
602}
603
9903cd1c
CL
604/**
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 *
1da177e4 609 */
d8ed029d 610struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1da177e4
LT
611{
612 struct list_head *pos;
1da177e4
LT
613
614 list_for_each(pos, &xprt->recv) {
615 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
262ca07d
CL
616 if (entry->rq_xid == xid)
617 return entry;
1da177e4 618 }
46121cf7
CL
619
620 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
621 ntohl(xid));
262ca07d
CL
622 xprt->stat.bad_xids++;
623 return NULL;
1da177e4 624}
12444809 625EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1da177e4 626
1570c1e4
CL
627/**
628 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
629 * @task: RPC request that recently completed
630 *
631 */
632void xprt_update_rtt(struct rpc_task *task)
633{
634 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;
637
638 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);
643 }
644}
12444809 645EXPORT_SYMBOL_GPL(xprt_update_rtt);
1570c1e4 646
9903cd1c
CL
647/**
648 * xprt_complete_rqst - called when reply processing is complete
1570c1e4 649 * @task: RPC request that recently completed
9903cd1c
CL
650 * @copied: actual number of bytes received from the transport
651 *
1570c1e4 652 * Caller holds transport lock.
1da177e4 653 */
1570c1e4 654void xprt_complete_rqst(struct rpc_task *task, int copied)
1da177e4 655{
1570c1e4 656 struct rpc_rqst *req = task->tk_rqstp;
1da177e4 657
1570c1e4
CL
658 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
659 task->tk_pid, ntohl(req->rq_xid), copied);
1da177e4 660
262ca07d 661 task->tk_xprt->stat.recvs++;
ef759a2e
CL
662 task->tk_rtt = (long)jiffies - req->rq_xtime;
663
1da177e4 664 list_del_init(&req->rq_list);
43ac3f29
TM
665 /* Ensure all writes are done before we update req->rq_received */
666 smp_wmb();
1da177e4 667 req->rq_received = req->rq_private_buf.len = copied;
1da177e4 668 rpc_wake_up_task(task);
1da177e4 669}
12444809 670EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1da177e4 671
46c0ee8b 672static void xprt_timer(struct rpc_task *task)
1da177e4 673{
46c0ee8b 674 struct rpc_rqst *req = task->tk_rqstp;
1da177e4
LT
675 struct rpc_xprt *xprt = req->rq_xprt;
676
46121cf7 677 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
1da177e4 678
46c0ee8b
CL
679 spin_lock(&xprt->transport_lock);
680 if (!req->rq_received) {
681 if (xprt->ops->timer)
682 xprt->ops->timer(task);
683 task->tk_status = -ETIMEDOUT;
684 }
1da177e4
LT
685 task->tk_timeout = 0;
686 rpc_wake_up_task(task);
4a0f8c04 687 spin_unlock(&xprt->transport_lock);
1da177e4
LT
688}
689
9903cd1c
CL
690/**
691 * xprt_prepare_transmit - reserve the transport before sending a request
692 * @task: RPC task about to send a request
693 *
1da177e4 694 */
9903cd1c 695int xprt_prepare_transmit(struct rpc_task *task)
1da177e4
LT
696{
697 struct rpc_rqst *req = task->tk_rqstp;
698 struct rpc_xprt *xprt = req->rq_xprt;
699 int err = 0;
700
46121cf7 701 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1da177e4 702
4a0f8c04 703 spin_lock_bh(&xprt->transport_lock);
1da177e4
LT
704 if (req->rq_received && !req->rq_bytes_sent) {
705 err = req->rq_received;
706 goto out_unlock;
707 }
12a80469 708 if (!xprt->ops->reserve_xprt(task)) {
1da177e4
LT
709 err = -EAGAIN;
710 goto out_unlock;
711 }
712
713 if (!xprt_connected(xprt)) {
714 err = -ENOTCONN;
715 goto out_unlock;
716 }
717out_unlock:
4a0f8c04 718 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
719 return err;
720}
721
e0ab53de 722void xprt_end_transmit(struct rpc_task *task)
5e5ce5be 723{
e0ab53de 724 xprt_release_write(task->tk_xprt, task);
5e5ce5be
TM
725}
726
9903cd1c
CL
727/**
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)
1da177e4 734{
1da177e4
LT
735 struct rpc_rqst *req = task->tk_rqstp;
736 struct rpc_xprt *xprt = req->rq_xprt;
a246b010 737 int status;
1da177e4 738
46121cf7 739 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1da177e4 740
1da177e4
LT
741 if (!req->rq_received) {
742 if (list_empty(&req->rq_list)) {
4a0f8c04 743 spin_lock_bh(&xprt->transport_lock);
1da177e4
LT
744 /* 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);
4a0f8c04 749 spin_unlock_bh(&xprt->transport_lock);
1da177e4 750 xprt_reset_majortimeo(req);
0f9dc2b1
TM
751 /* Turn off autodisconnect */
752 del_singleshot_timer_sync(&xprt->timer);
1da177e4
LT
753 }
754 } else if (!req->rq_bytes_sent)
755 return;
756
a246b010 757 status = xprt->ops->send_request(task);
fe3aca29 758 if (status == 0) {
46121cf7 759 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
fe3aca29 760 spin_lock_bh(&xprt->transport_lock);
262ca07d 761
fe3aca29 762 xprt->ops->set_retrans_timeout(task);
262ca07d
CL
763
764 xprt->stat.sends++;
765 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
766 xprt->stat.bklog_u += xprt->backlog.qlen;
767
fe3aca29
CL
768 /* 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);
fe3aca29
CL
773 spin_unlock_bh(&xprt->transport_lock);
774 return;
775 }
1da177e4
LT
776
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;
e0ab53de 782 if (status == -ECONNREFUSED)
1da177e4 783 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
1da177e4
LT
784}
785
9903cd1c 786static inline void do_xprt_reserve(struct rpc_task *task)
1da177e4
LT
787{
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 }
46121cf7 800 dprintk("RPC: waiting for request slot\n");
1da177e4
LT
801 task->tk_status = -EAGAIN;
802 task->tk_timeout = 0;
803 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
804}
805
9903cd1c
CL
806/**
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)
1da177e4
LT
814{
815 struct rpc_xprt *xprt = task->tk_xprt;
816
817 task->tk_status = -EIO;
0065db32
TM
818 spin_lock(&xprt->reserve_lock);
819 do_xprt_reserve(task);
820 spin_unlock(&xprt->reserve_lock);
1da177e4
LT
821}
822
d8ed029d 823static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
1da177e4
LT
824{
825 return xprt->xid++;
826}
827
828static inline void xprt_init_xid(struct rpc_xprt *xprt)
829{
bf3fcf89 830 xprt->xid = net_random();
1da177e4
LT
831}
832
9903cd1c 833static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
1da177e4
LT
834{
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;
02107148 840 req->rq_buffer = NULL;
1da177e4 841 req->rq_xid = xprt_alloc_xid(xprt);
ead5e1c2 842 req->rq_release_snd_buf = NULL;
da45828e 843 xprt_reset_majortimeo(req);
46121cf7 844 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1da177e4
LT
845 req, ntohl(req->rq_xid));
846}
847
9903cd1c
CL
848/**
849 * xprt_release - release an RPC request slot
850 * @task: task which is finished with the slot
851 *
1da177e4 852 */
9903cd1c 853void xprt_release(struct rpc_task *task)
1da177e4
LT
854{
855 struct rpc_xprt *xprt = task->tk_xprt;
856 struct rpc_rqst *req;
857
858 if (!(req = task->tk_rqstp))
859 return;
11c556b3 860 rpc_count_iostats(task);
4a0f8c04 861 spin_lock_bh(&xprt->transport_lock);
49e9a890 862 xprt->ops->release_xprt(xprt, task);
a58dd398
CL
863 if (xprt->ops->release_request)
864 xprt->ops->release_request(task);
1da177e4
LT
865 if (!list_empty(&req->rq_list))
866 list_del(&req->rq_list);
867 xprt->last_used = jiffies;
0065db32 868 if (list_empty(&xprt->recv))
a246b010 869 mod_timer(&xprt->timer,
03bf4b70 870 xprt->last_used + xprt->idle_timeout);
4a0f8c04 871 spin_unlock_bh(&xprt->transport_lock);
c5a4dd8b 872 xprt->ops->buf_free(req->rq_buffer);
1da177e4 873 task->tk_rqstp = NULL;
ead5e1c2
BF
874 if (req->rq_release_snd_buf)
875 req->rq_release_snd_buf(req);
1da177e4
LT
876 memset(req, 0, sizeof(*req)); /* mark unused */
877
46121cf7 878 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1da177e4 879
5dc07727 880 spin_lock(&xprt->reserve_lock);
1da177e4 881 list_add(&req->rq_list, &xprt->free);
555ee3af 882 rpc_wake_up_next(&xprt->backlog);
5dc07727 883 spin_unlock(&xprt->reserve_lock);
1da177e4
LT
884}
885
9903cd1c
CL
886/**
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 *
1da177e4 892 */
9903cd1c 893void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
1da177e4 894{
cca5172a 895 to->to_initval =
1da177e4 896 to->to_increment = incr;
eab5c084 897 to->to_maxval = to->to_initval + (incr * retr);
1da177e4
LT
898 to->to_retries = retr;
899 to->to_exponential = 0;
900}
901
c2866763
CL
902/**
903 * xprt_create_transport - create an RPC transport
96802a09 904 * @args: rpc transport creation arguments
c2866763
CL
905 *
906 */
96802a09 907struct rpc_xprt *xprt_create_transport(struct rpc_xprtsock_create *args)
c2866763 908{
c2866763
CL
909 struct rpc_xprt *xprt;
910 struct rpc_rqst *req;
911
96802a09 912 switch (args->proto) {
c2866763 913 case IPPROTO_UDP:
96802a09 914 xprt = xs_setup_udp(args);
c2866763
CL
915 break;
916 case IPPROTO_TCP:
96802a09 917 xprt = xs_setup_tcp(args);
c2866763
CL
918 break;
919 default:
920 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
96802a09 921 args->proto);
c2866763
CL
922 return ERR_PTR(-EIO);
923 }
c8541ecd 924 if (IS_ERR(xprt)) {
46121cf7 925 dprintk("RPC: xprt_create_transport: failed, %ld\n",
c8541ecd
CL
926 -PTR_ERR(xprt));
927 return xprt;
c2866763
CL
928 }
929
6b6ca86b 930 kref_init(&xprt->kref);
c2866763
CL
931 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);
65f27f38 936 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
c2866763
CL
937 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;
a509050b 942 xprt->bind_index = 0;
c2866763
CL
943
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
46121cf7 956 dprintk("RPC: created transport %p with %u slots\n", xprt,
c2866763
CL
957 xprt->max_reqs);
958
959 return xprt;
960}
961
9903cd1c
CL
962/**
963 * xprt_destroy - destroy an RPC transport, killing off all requests.
6b6ca86b 964 * @kref: kref for the transport to destroy
9903cd1c 965 *
1da177e4 966 */
6b6ca86b 967static void xprt_destroy(struct kref *kref)
1da177e4 968{
6b6ca86b
TM
969 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
970
46121cf7 971 dprintk("RPC: destroying transport %p\n", xprt);
0065db32
TM
972 xprt->shutdown = 1;
973 del_timer_sync(&xprt->timer);
c8541ecd
CL
974
975 /*
976 * Tear down transport state and free the rpc_xprt
977 */
a246b010 978 xprt->ops->destroy(xprt);
6b6ca86b 979}
1da177e4 980
6b6ca86b
TM
981/**
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;
1da177e4 1000}
This page took 0.322784 seconds and 5 git commands to generate.