SUNRPC: Convert users of rpc_wake_up_task to use rpc_wake_up_queued_task
[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
5ba03e82 65static DEFINE_SPINLOCK(xprt_list_lock);
81c098af
TT
66static LIST_HEAD(xprt_list);
67
555ee3af
CL
68/*
69 * The transport code maintains an estimate on the maximum number of out-
70 * standing RPC requests, using a smoothed version of the congestion
71 * avoidance implemented in 44BSD. This is basically the Van Jacobson
72 * congestion algorithm: If a retransmit occurs, the congestion window is
73 * halved; otherwise, it is incremented by 1/cwnd when
74 *
75 * - a reply is received and
76 * - a full number of requests are outstanding and
77 * - the congestion window hasn't been updated recently.
78 */
79#define RPC_CWNDSHIFT (8U)
80#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
81#define RPC_INITCWND RPC_CWNDSCALE
82#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
83
84#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
1da177e4 85
81c098af
TT
86/**
87 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
89 *
90 * If a transport implementation is loaded as a kernel module, it can
91 * call this interface to make itself known to the RPC client.
92 *
93 * Returns:
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
97 */
98int xprt_register_transport(struct xprt_class *transport)
99{
100 struct xprt_class *t;
101 int result;
102
103 result = -EEXIST;
104 spin_lock(&xprt_list_lock);
105 list_for_each_entry(t, &xprt_list, list) {
106 /* don't register the same transport class twice */
4fa016eb 107 if (t->ident == transport->ident)
81c098af
TT
108 goto out;
109 }
110
111 result = -EINVAL;
112 if (try_module_get(THIS_MODULE)) {
113 list_add_tail(&transport->list, &xprt_list);
114 printk(KERN_INFO "RPC: Registered %s transport module.\n",
115 transport->name);
116 result = 0;
117 }
118
119out:
120 spin_unlock(&xprt_list_lock);
121 return result;
122}
123EXPORT_SYMBOL_GPL(xprt_register_transport);
124
125/**
126 * xprt_unregister_transport - unregister a transport implementation
65b6e42c 127 * @transport: transport to unregister
81c098af
TT
128 *
129 * Returns:
130 * 0: transport successfully unregistered
131 * -ENOENT: transport never registered
132 */
133int xprt_unregister_transport(struct xprt_class *transport)
134{
135 struct xprt_class *t;
136 int result;
137
138 result = 0;
139 spin_lock(&xprt_list_lock);
140 list_for_each_entry(t, &xprt_list, list) {
141 if (t == transport) {
142 printk(KERN_INFO
143 "RPC: Unregistered %s transport module.\n",
144 transport->name);
145 list_del_init(&transport->list);
146 module_put(THIS_MODULE);
147 goto out;
148 }
149 }
150 result = -ENOENT;
151
152out:
153 spin_unlock(&xprt_list_lock);
154 return result;
155}
156EXPORT_SYMBOL_GPL(xprt_unregister_transport);
157
12a80469
CL
158/**
159 * xprt_reserve_xprt - serialize write access to transports
160 * @task: task that is requesting access to the transport
161 *
162 * This prevents mixing the payload of separate requests, and prevents
163 * transport connects from colliding with writes. No congestion control
164 * is provided.
165 */
166int xprt_reserve_xprt(struct rpc_task *task)
167{
168 struct rpc_xprt *xprt = task->tk_xprt;
169 struct rpc_rqst *req = task->tk_rqstp;
170
171 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
172 if (task == xprt->snd_task)
173 return 1;
174 if (task == NULL)
175 return 0;
176 goto out_sleep;
177 }
178 xprt->snd_task = task;
179 if (req) {
180 req->rq_bytes_sent = 0;
181 req->rq_ntrans++;
182 }
183 return 1;
184
185out_sleep:
46121cf7 186 dprintk("RPC: %5u failed to lock transport %p\n",
12a80469
CL
187 task->tk_pid, xprt);
188 task->tk_timeout = 0;
189 task->tk_status = -EAGAIN;
190 if (req && req->rq_ntrans)
191 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
192 else
193 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
194 return 0;
195}
12444809 196EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
12a80469 197
632e3bdc
TM
198static void xprt_clear_locked(struct rpc_xprt *xprt)
199{
200 xprt->snd_task = NULL;
201 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
202 smp_mb__before_clear_bit();
203 clear_bit(XPRT_LOCKED, &xprt->state);
204 smp_mb__after_clear_bit();
205 } else
c1384c9c 206 queue_work(rpciod_workqueue, &xprt->task_cleanup);
632e3bdc
TM
207}
208
1da177e4 209/*
12a80469
CL
210 * xprt_reserve_xprt_cong - serialize write access to transports
211 * @task: task that is requesting access to the transport
212 *
213 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
214 * integrated into the decision of whether a request is allowed to be
215 * woken up and given access to the transport.
1da177e4 216 */
12a80469 217int xprt_reserve_xprt_cong(struct rpc_task *task)
1da177e4 218{
12a80469 219 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4
LT
220 struct rpc_rqst *req = task->tk_rqstp;
221
2226feb6 222 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
1da177e4
LT
223 if (task == xprt->snd_task)
224 return 1;
1da177e4
LT
225 goto out_sleep;
226 }
12a80469 227 if (__xprt_get_cong(xprt, task)) {
1da177e4
LT
228 xprt->snd_task = task;
229 if (req) {
230 req->rq_bytes_sent = 0;
231 req->rq_ntrans++;
232 }
233 return 1;
234 }
632e3bdc 235 xprt_clear_locked(xprt);
1da177e4 236out_sleep:
46121cf7 237 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
1da177e4
LT
238 task->tk_timeout = 0;
239 task->tk_status = -EAGAIN;
240 if (req && req->rq_ntrans)
241 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
242 else
243 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
244 return 0;
245}
12444809 246EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
1da177e4 247
12a80469 248static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
249{
250 int retval;
251
4a0f8c04 252 spin_lock_bh(&xprt->transport_lock);
12a80469 253 retval = xprt->ops->reserve_xprt(task);
4a0f8c04 254 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
255 return retval;
256}
257
12a80469 258static void __xprt_lock_write_next(struct rpc_xprt *xprt)
49e9a890
CL
259{
260 struct rpc_task *task;
261 struct rpc_rqst *req;
262
263 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
264 return;
265
266 task = rpc_wake_up_next(&xprt->resend);
267 if (!task) {
268 task = rpc_wake_up_next(&xprt->sending);
269 if (!task)
270 goto out_unlock;
271 }
272
273 req = task->tk_rqstp;
274 xprt->snd_task = task;
275 if (req) {
276 req->rq_bytes_sent = 0;
277 req->rq_ntrans++;
278 }
279 return;
280
281out_unlock:
632e3bdc 282 xprt_clear_locked(xprt);
49e9a890
CL
283}
284
285static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
1da177e4
LT
286{
287 struct rpc_task *task;
288
2226feb6 289 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 290 return;
49e9a890 291 if (RPCXPRT_CONGESTED(xprt))
1da177e4
LT
292 goto out_unlock;
293 task = rpc_wake_up_next(&xprt->resend);
294 if (!task) {
295 task = rpc_wake_up_next(&xprt->sending);
296 if (!task)
297 goto out_unlock;
298 }
49e9a890 299 if (__xprt_get_cong(xprt, task)) {
1da177e4
LT
300 struct rpc_rqst *req = task->tk_rqstp;
301 xprt->snd_task = task;
302 if (req) {
303 req->rq_bytes_sent = 0;
304 req->rq_ntrans++;
305 }
306 return;
307 }
308out_unlock:
632e3bdc 309 xprt_clear_locked(xprt);
1da177e4
LT
310}
311
49e9a890
CL
312/**
313 * xprt_release_xprt - allow other requests to use a transport
314 * @xprt: transport with other tasks potentially waiting
315 * @task: task that is releasing access to the transport
316 *
317 * Note that "task" can be NULL. No congestion control is provided.
1da177e4 318 */
49e9a890 319void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
320{
321 if (xprt->snd_task == task) {
632e3bdc 322 xprt_clear_locked(xprt);
1da177e4
LT
323 __xprt_lock_write_next(xprt);
324 }
325}
12444809 326EXPORT_SYMBOL_GPL(xprt_release_xprt);
1da177e4 327
49e9a890
CL
328/**
329 * xprt_release_xprt_cong - allow other requests to use a transport
330 * @xprt: transport with other tasks potentially waiting
331 * @task: task that is releasing access to the transport
332 *
333 * Note that "task" can be NULL. Another task is awoken to use the
334 * transport if the transport's congestion window allows it.
335 */
336void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
337{
338 if (xprt->snd_task == task) {
632e3bdc 339 xprt_clear_locked(xprt);
49e9a890
CL
340 __xprt_lock_write_next_cong(xprt);
341 }
342}
12444809 343EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
49e9a890
CL
344
345static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4 346{
4a0f8c04 347 spin_lock_bh(&xprt->transport_lock);
49e9a890 348 xprt->ops->release_xprt(xprt, task);
4a0f8c04 349 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
350}
351
1da177e4
LT
352/*
353 * Van Jacobson congestion avoidance. Check if the congestion window
354 * overflowed. Put the task to sleep if this is the case.
355 */
356static int
357__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
358{
359 struct rpc_rqst *req = task->tk_rqstp;
360
361 if (req->rq_cong)
362 return 1;
46121cf7 363 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
1da177e4
LT
364 task->tk_pid, xprt->cong, xprt->cwnd);
365 if (RPCXPRT_CONGESTED(xprt))
366 return 0;
367 req->rq_cong = 1;
368 xprt->cong += RPC_CWNDSCALE;
369 return 1;
370}
371
372/*
373 * Adjust the congestion window, and wake up the next task
374 * that has been sleeping due to congestion
375 */
376static void
377__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
378{
379 if (!req->rq_cong)
380 return;
381 req->rq_cong = 0;
382 xprt->cong -= RPC_CWNDSCALE;
49e9a890 383 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
384}
385
a58dd398
CL
386/**
387 * xprt_release_rqst_cong - housekeeping when request is complete
388 * @task: RPC request that recently completed
389 *
390 * Useful for transports that require congestion control.
391 */
392void xprt_release_rqst_cong(struct rpc_task *task)
393{
394 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
395}
12444809 396EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
a58dd398 397
46c0ee8b
CL
398/**
399 * xprt_adjust_cwnd - adjust transport congestion window
400 * @task: recently completed RPC request used to adjust window
401 * @result: result code of completed RPC request
402 *
1da177e4
LT
403 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
404 */
46c0ee8b 405void xprt_adjust_cwnd(struct rpc_task *task, int result)
1da177e4 406{
46c0ee8b
CL
407 struct rpc_rqst *req = task->tk_rqstp;
408 struct rpc_xprt *xprt = task->tk_xprt;
409 unsigned long cwnd = xprt->cwnd;
1da177e4 410
1da177e4
LT
411 if (result >= 0 && cwnd <= xprt->cong) {
412 /* The (cwnd >> 1) term makes sure
413 * the result gets rounded properly. */
414 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
415 if (cwnd > RPC_MAXCWND(xprt))
416 cwnd = RPC_MAXCWND(xprt);
49e9a890 417 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
418 } else if (result == -ETIMEDOUT) {
419 cwnd >>= 1;
420 if (cwnd < RPC_CWNDSCALE)
421 cwnd = RPC_CWNDSCALE;
422 }
46121cf7 423 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
1da177e4
LT
424 xprt->cong, xprt->cwnd, cwnd);
425 xprt->cwnd = cwnd;
46c0ee8b 426 __xprt_put_cong(xprt, req);
1da177e4 427}
12444809 428EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
1da177e4 429
44fbac22
CL
430/**
431 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
432 * @xprt: transport with waiting tasks
433 * @status: result code to plant in each task before waking it
434 *
435 */
436void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
437{
438 if (status < 0)
439 rpc_wake_up_status(&xprt->pending, status);
440 else
441 rpc_wake_up(&xprt->pending);
442}
12444809 443EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
44fbac22 444
c7b2cae8
CL
445/**
446 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
447 * @task: task to be put to sleep
448 *
449 */
450void xprt_wait_for_buffer_space(struct rpc_task *task)
451{
452 struct rpc_rqst *req = task->tk_rqstp;
453 struct rpc_xprt *xprt = req->rq_xprt;
454
455 task->tk_timeout = req->rq_timeout;
456 rpc_sleep_on(&xprt->pending, task, NULL, NULL);
457}
12444809 458EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
c7b2cae8
CL
459
460/**
461 * xprt_write_space - wake the task waiting for transport output buffer space
462 * @xprt: transport with waiting tasks
463 *
464 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
465 */
466void xprt_write_space(struct rpc_xprt *xprt)
467{
468 if (unlikely(xprt->shutdown))
469 return;
470
471 spin_lock_bh(&xprt->transport_lock);
472 if (xprt->snd_task) {
46121cf7
CL
473 dprintk("RPC: write space: waking waiting task on "
474 "xprt %p\n", xprt);
fda13939 475 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
c7b2cae8
CL
476 }
477 spin_unlock_bh(&xprt->transport_lock);
478}
12444809 479EXPORT_SYMBOL_GPL(xprt_write_space);
c7b2cae8 480
fe3aca29
CL
481/**
482 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
483 * @task: task whose timeout is to be set
484 *
485 * Set a request's retransmit timeout based on the transport's
486 * default timeout parameters. Used by transports that don't adjust
487 * the retransmit timeout based on round-trip time estimation.
488 */
489void xprt_set_retrans_timeout_def(struct rpc_task *task)
490{
491 task->tk_timeout = task->tk_rqstp->rq_timeout;
492}
12444809 493EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
fe3aca29
CL
494
495/*
496 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
497 * @task: task whose timeout is to be set
cca5172a 498 *
fe3aca29
CL
499 * Set a request's retransmit timeout using the RTT estimator.
500 */
501void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
502{
503 int timer = task->tk_msg.rpc_proc->p_timer;
ba7392bb
TM
504 struct rpc_clnt *clnt = task->tk_client;
505 struct rpc_rtt *rtt = clnt->cl_rtt;
fe3aca29 506 struct rpc_rqst *req = task->tk_rqstp;
ba7392bb 507 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
fe3aca29
CL
508
509 task->tk_timeout = rpc_calc_rto(rtt, timer);
510 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
511 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
512 task->tk_timeout = max_timeout;
513}
12444809 514EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
fe3aca29 515
1da177e4
LT
516static void xprt_reset_majortimeo(struct rpc_rqst *req)
517{
ba7392bb 518 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
519
520 req->rq_majortimeo = req->rq_timeout;
521 if (to->to_exponential)
522 req->rq_majortimeo <<= to->to_retries;
523 else
524 req->rq_majortimeo += to->to_increment * to->to_retries;
525 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
526 req->rq_majortimeo = to->to_maxval;
527 req->rq_majortimeo += jiffies;
528}
529
9903cd1c
CL
530/**
531 * xprt_adjust_timeout - adjust timeout values for next retransmit
532 * @req: RPC request containing parameters to use for the adjustment
533 *
1da177e4
LT
534 */
535int xprt_adjust_timeout(struct rpc_rqst *req)
536{
537 struct rpc_xprt *xprt = req->rq_xprt;
ba7392bb 538 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
539 int status = 0;
540
541 if (time_before(jiffies, req->rq_majortimeo)) {
542 if (to->to_exponential)
543 req->rq_timeout <<= 1;
544 else
545 req->rq_timeout += to->to_increment;
546 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
547 req->rq_timeout = to->to_maxval;
548 req->rq_retries++;
1da177e4
LT
549 } else {
550 req->rq_timeout = to->to_initval;
551 req->rq_retries = 0;
552 xprt_reset_majortimeo(req);
553 /* Reset the RTT counters == "slow start" */
4a0f8c04 554 spin_lock_bh(&xprt->transport_lock);
1da177e4 555 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
4a0f8c04 556 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
557 status = -ETIMEDOUT;
558 }
559
560 if (req->rq_timeout == 0) {
561 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
562 req->rq_timeout = 5 * HZ;
563 }
564 return status;
565}
566
65f27f38 567static void xprt_autoclose(struct work_struct *work)
1da177e4 568{
65f27f38
DH
569 struct rpc_xprt *xprt =
570 container_of(work, struct rpc_xprt, task_cleanup);
1da177e4 571
a246b010 572 xprt->ops->close(xprt);
66af1e55 573 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1da177e4
LT
574 xprt_release_write(xprt, NULL);
575}
576
9903cd1c 577/**
62da3b24 578 * xprt_disconnect_done - mark a transport as disconnected
9903cd1c
CL
579 * @xprt: transport to flag for disconnect
580 *
1da177e4 581 */
62da3b24 582void xprt_disconnect_done(struct rpc_xprt *xprt)
1da177e4 583{
46121cf7 584 dprintk("RPC: disconnected transport %p\n", xprt);
4a0f8c04 585 spin_lock_bh(&xprt->transport_lock);
1da177e4 586 xprt_clear_connected(xprt);
44fbac22 587 xprt_wake_pending_tasks(xprt, -ENOTCONN);
4a0f8c04 588 spin_unlock_bh(&xprt->transport_lock);
1da177e4 589}
62da3b24 590EXPORT_SYMBOL_GPL(xprt_disconnect_done);
1da177e4 591
66af1e55
TM
592/**
593 * xprt_force_disconnect - force a transport to disconnect
594 * @xprt: transport to disconnect
595 *
596 */
597void xprt_force_disconnect(struct rpc_xprt *xprt)
598{
599 /* Don't race with the test_bit() in xprt_clear_locked() */
600 spin_lock_bh(&xprt->transport_lock);
601 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
602 /* Try to schedule an autoclose RPC call */
603 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
604 queue_work(rpciod_workqueue, &xprt->task_cleanup);
fda13939 605 xprt_wake_pending_tasks(xprt, -ENOTCONN);
66af1e55
TM
606 spin_unlock_bh(&xprt->transport_lock);
607}
608EXPORT_SYMBOL_GPL(xprt_force_disconnect);
609
1da177e4
LT
610static void
611xprt_init_autodisconnect(unsigned long data)
612{
613 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
614
4a0f8c04 615 spin_lock(&xprt->transport_lock);
1da177e4
LT
616 if (!list_empty(&xprt->recv) || xprt->shutdown)
617 goto out_abort;
2226feb6 618 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 619 goto out_abort;
4a0f8c04 620 spin_unlock(&xprt->transport_lock);
2226feb6 621 if (xprt_connecting(xprt))
1da177e4
LT
622 xprt_release_write(xprt, NULL);
623 else
c1384c9c 624 queue_work(rpciod_workqueue, &xprt->task_cleanup);
1da177e4
LT
625 return;
626out_abort:
4a0f8c04 627 spin_unlock(&xprt->transport_lock);
1da177e4
LT
628}
629
9903cd1c
CL
630/**
631 * xprt_connect - schedule a transport connect operation
632 * @task: RPC task that is requesting the connect
1da177e4
LT
633 *
634 */
635void xprt_connect(struct rpc_task *task)
636{
637 struct rpc_xprt *xprt = task->tk_xprt;
638
46121cf7 639 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
1da177e4
LT
640 xprt, (xprt_connected(xprt) ? "is" : "is not"));
641
ec739ef0 642 if (!xprt_bound(xprt)) {
1da177e4
LT
643 task->tk_status = -EIO;
644 return;
645 }
646 if (!xprt_lock_write(xprt, task))
647 return;
648 if (xprt_connected(xprt))
a246b010
CL
649 xprt_release_write(xprt, task);
650 else {
651 if (task->tk_rqstp)
652 task->tk_rqstp->rq_bytes_sent = 0;
1da177e4 653
03bf4b70 654 task->tk_timeout = xprt->connect_timeout;
a246b010 655 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
262ca07d 656 xprt->stat.connect_start = jiffies;
a246b010 657 xprt->ops->connect(task);
1da177e4
LT
658 }
659 return;
1da177e4
LT
660}
661
9903cd1c 662static void xprt_connect_status(struct rpc_task *task)
1da177e4
LT
663{
664 struct rpc_xprt *xprt = task->tk_xprt;
665
666 if (task->tk_status >= 0) {
262ca07d
CL
667 xprt->stat.connect_count++;
668 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
46121cf7 669 dprintk("RPC: %5u xprt_connect_status: connection established\n",
1da177e4
LT
670 task->tk_pid);
671 return;
672 }
673
1da177e4
LT
674 switch (task->tk_status) {
675 case -ECONNREFUSED:
676 case -ECONNRESET:
46121cf7
CL
677 dprintk("RPC: %5u xprt_connect_status: server %s refused "
678 "connection\n", task->tk_pid,
679 task->tk_client->cl_server);
23475d66 680 break;
1da177e4 681 case -ENOTCONN:
46121cf7 682 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
23475d66
CL
683 task->tk_pid);
684 break;
1da177e4 685 case -ETIMEDOUT:
46121cf7
CL
686 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
687 "out\n", task->tk_pid);
1da177e4
LT
688 break;
689 default:
46121cf7
CL
690 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
691 "server %s\n", task->tk_pid, -task->tk_status,
692 task->tk_client->cl_server);
23475d66
CL
693 xprt_release_write(xprt, task);
694 task->tk_status = -EIO;
1da177e4 695 }
1da177e4
LT
696}
697
9903cd1c
CL
698/**
699 * xprt_lookup_rqst - find an RPC request corresponding to an XID
700 * @xprt: transport on which the original request was transmitted
701 * @xid: RPC XID of incoming reply
702 *
1da177e4 703 */
d8ed029d 704struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1da177e4
LT
705{
706 struct list_head *pos;
1da177e4
LT
707
708 list_for_each(pos, &xprt->recv) {
709 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
262ca07d
CL
710 if (entry->rq_xid == xid)
711 return entry;
1da177e4 712 }
46121cf7
CL
713
714 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
715 ntohl(xid));
262ca07d
CL
716 xprt->stat.bad_xids++;
717 return NULL;
1da177e4 718}
12444809 719EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1da177e4 720
1570c1e4
CL
721/**
722 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
723 * @task: RPC request that recently completed
724 *
725 */
726void xprt_update_rtt(struct rpc_task *task)
727{
728 struct rpc_rqst *req = task->tk_rqstp;
729 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
730 unsigned timer = task->tk_msg.rpc_proc->p_timer;
731
732 if (timer) {
733 if (req->rq_ntrans == 1)
734 rpc_update_rtt(rtt, timer,
735 (long)jiffies - req->rq_xtime);
736 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
737 }
738}
12444809 739EXPORT_SYMBOL_GPL(xprt_update_rtt);
1570c1e4 740
9903cd1c
CL
741/**
742 * xprt_complete_rqst - called when reply processing is complete
1570c1e4 743 * @task: RPC request that recently completed
9903cd1c
CL
744 * @copied: actual number of bytes received from the transport
745 *
1570c1e4 746 * Caller holds transport lock.
1da177e4 747 */
1570c1e4 748void xprt_complete_rqst(struct rpc_task *task, int copied)
1da177e4 749{
1570c1e4 750 struct rpc_rqst *req = task->tk_rqstp;
fda13939 751 struct rpc_xprt *xprt = req->rq_xprt;
1da177e4 752
1570c1e4
CL
753 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
754 task->tk_pid, ntohl(req->rq_xid), copied);
1da177e4 755
fda13939 756 xprt->stat.recvs++;
ef759a2e
CL
757 task->tk_rtt = (long)jiffies - req->rq_xtime;
758
1da177e4 759 list_del_init(&req->rq_list);
43ac3f29
TM
760 /* Ensure all writes are done before we update req->rq_received */
761 smp_wmb();
1da177e4 762 req->rq_received = req->rq_private_buf.len = copied;
fda13939 763 rpc_wake_up_queued_task(&xprt->pending, task);
1da177e4 764}
12444809 765EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1da177e4 766
46c0ee8b 767static void xprt_timer(struct rpc_task *task)
1da177e4 768{
46c0ee8b 769 struct rpc_rqst *req = task->tk_rqstp;
1da177e4
LT
770 struct rpc_xprt *xprt = req->rq_xprt;
771
46121cf7 772 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
1da177e4 773
46c0ee8b
CL
774 spin_lock(&xprt->transport_lock);
775 if (!req->rq_received) {
776 if (xprt->ops->timer)
777 xprt->ops->timer(task);
778 task->tk_status = -ETIMEDOUT;
779 }
4a0f8c04 780 spin_unlock(&xprt->transport_lock);
1da177e4
LT
781}
782
9903cd1c
CL
783/**
784 * xprt_prepare_transmit - reserve the transport before sending a request
785 * @task: RPC task about to send a request
786 *
1da177e4 787 */
9903cd1c 788int xprt_prepare_transmit(struct rpc_task *task)
1da177e4
LT
789{
790 struct rpc_rqst *req = task->tk_rqstp;
791 struct rpc_xprt *xprt = req->rq_xprt;
792 int err = 0;
793
46121cf7 794 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1da177e4 795
4a0f8c04 796 spin_lock_bh(&xprt->transport_lock);
1da177e4
LT
797 if (req->rq_received && !req->rq_bytes_sent) {
798 err = req->rq_received;
799 goto out_unlock;
800 }
12a80469 801 if (!xprt->ops->reserve_xprt(task)) {
1da177e4
LT
802 err = -EAGAIN;
803 goto out_unlock;
804 }
805
806 if (!xprt_connected(xprt)) {
807 err = -ENOTCONN;
808 goto out_unlock;
809 }
810out_unlock:
4a0f8c04 811 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
812 return err;
813}
814
e0ab53de 815void xprt_end_transmit(struct rpc_task *task)
5e5ce5be 816{
e0ab53de 817 xprt_release_write(task->tk_xprt, task);
5e5ce5be
TM
818}
819
9903cd1c
CL
820/**
821 * xprt_transmit - send an RPC request on a transport
822 * @task: controlling RPC task
823 *
824 * We have to copy the iovec because sendmsg fiddles with its contents.
825 */
826void xprt_transmit(struct rpc_task *task)
1da177e4 827{
1da177e4
LT
828 struct rpc_rqst *req = task->tk_rqstp;
829 struct rpc_xprt *xprt = req->rq_xprt;
a246b010 830 int status;
1da177e4 831
46121cf7 832 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1da177e4 833
1da177e4
LT
834 if (!req->rq_received) {
835 if (list_empty(&req->rq_list)) {
4a0f8c04 836 spin_lock_bh(&xprt->transport_lock);
1da177e4
LT
837 /* Update the softirq receive buffer */
838 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
839 sizeof(req->rq_private_buf));
840 /* Add request to the receive list */
841 list_add_tail(&req->rq_list, &xprt->recv);
4a0f8c04 842 spin_unlock_bh(&xprt->transport_lock);
1da177e4 843 xprt_reset_majortimeo(req);
0f9dc2b1
TM
844 /* Turn off autodisconnect */
845 del_singleshot_timer_sync(&xprt->timer);
1da177e4
LT
846 }
847 } else if (!req->rq_bytes_sent)
848 return;
849
a246b010 850 status = xprt->ops->send_request(task);
fe3aca29 851 if (status == 0) {
46121cf7 852 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
fe3aca29 853 spin_lock_bh(&xprt->transport_lock);
262ca07d 854
fe3aca29 855 xprt->ops->set_retrans_timeout(task);
262ca07d
CL
856
857 xprt->stat.sends++;
858 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
859 xprt->stat.bklog_u += xprt->backlog.qlen;
860
fe3aca29
CL
861 /* Don't race with disconnect */
862 if (!xprt_connected(xprt))
863 task->tk_status = -ENOTCONN;
864 else if (!req->rq_received)
865 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
fe3aca29
CL
866 spin_unlock_bh(&xprt->transport_lock);
867 return;
868 }
1da177e4
LT
869
870 /* Note: at this point, task->tk_sleeping has not yet been set,
871 * hence there is no danger of the waking up task being put on
872 * schedq, and being picked up by a parallel run of rpciod().
873 */
874 task->tk_status = status;
e0ab53de 875 if (status == -ECONNREFUSED)
1da177e4 876 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
1da177e4
LT
877}
878
9903cd1c 879static inline void do_xprt_reserve(struct rpc_task *task)
1da177e4
LT
880{
881 struct rpc_xprt *xprt = task->tk_xprt;
882
883 task->tk_status = 0;
884 if (task->tk_rqstp)
885 return;
886 if (!list_empty(&xprt->free)) {
887 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
888 list_del_init(&req->rq_list);
889 task->tk_rqstp = req;
890 xprt_request_init(task, xprt);
891 return;
892 }
46121cf7 893 dprintk("RPC: waiting for request slot\n");
1da177e4
LT
894 task->tk_status = -EAGAIN;
895 task->tk_timeout = 0;
896 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
897}
898
9903cd1c
CL
899/**
900 * xprt_reserve - allocate an RPC request slot
901 * @task: RPC task requesting a slot allocation
902 *
903 * If no more slots are available, place the task on the transport's
904 * backlog queue.
905 */
906void xprt_reserve(struct rpc_task *task)
1da177e4
LT
907{
908 struct rpc_xprt *xprt = task->tk_xprt;
909
910 task->tk_status = -EIO;
0065db32
TM
911 spin_lock(&xprt->reserve_lock);
912 do_xprt_reserve(task);
913 spin_unlock(&xprt->reserve_lock);
1da177e4
LT
914}
915
d8ed029d 916static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
1da177e4
LT
917{
918 return xprt->xid++;
919}
920
921static inline void xprt_init_xid(struct rpc_xprt *xprt)
922{
bf3fcf89 923 xprt->xid = net_random();
1da177e4
LT
924}
925
9903cd1c 926static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
1da177e4
LT
927{
928 struct rpc_rqst *req = task->tk_rqstp;
929
ba7392bb 930 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1da177e4
LT
931 req->rq_task = task;
932 req->rq_xprt = xprt;
02107148 933 req->rq_buffer = NULL;
1da177e4 934 req->rq_xid = xprt_alloc_xid(xprt);
ead5e1c2 935 req->rq_release_snd_buf = NULL;
da45828e 936 xprt_reset_majortimeo(req);
46121cf7 937 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1da177e4
LT
938 req, ntohl(req->rq_xid));
939}
940
9903cd1c
CL
941/**
942 * xprt_release - release an RPC request slot
943 * @task: task which is finished with the slot
944 *
1da177e4 945 */
9903cd1c 946void xprt_release(struct rpc_task *task)
1da177e4
LT
947{
948 struct rpc_xprt *xprt = task->tk_xprt;
949 struct rpc_rqst *req;
950
951 if (!(req = task->tk_rqstp))
952 return;
11c556b3 953 rpc_count_iostats(task);
4a0f8c04 954 spin_lock_bh(&xprt->transport_lock);
49e9a890 955 xprt->ops->release_xprt(xprt, task);
a58dd398
CL
956 if (xprt->ops->release_request)
957 xprt->ops->release_request(task);
1da177e4
LT
958 if (!list_empty(&req->rq_list))
959 list_del(&req->rq_list);
960 xprt->last_used = jiffies;
0065db32 961 if (list_empty(&xprt->recv))
a246b010 962 mod_timer(&xprt->timer,
03bf4b70 963 xprt->last_used + xprt->idle_timeout);
4a0f8c04 964 spin_unlock_bh(&xprt->transport_lock);
c5a4dd8b 965 xprt->ops->buf_free(req->rq_buffer);
1da177e4 966 task->tk_rqstp = NULL;
ead5e1c2
BF
967 if (req->rq_release_snd_buf)
968 req->rq_release_snd_buf(req);
1da177e4
LT
969 memset(req, 0, sizeof(*req)); /* mark unused */
970
46121cf7 971 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1da177e4 972
5dc07727 973 spin_lock(&xprt->reserve_lock);
1da177e4 974 list_add(&req->rq_list, &xprt->free);
555ee3af 975 rpc_wake_up_next(&xprt->backlog);
5dc07727 976 spin_unlock(&xprt->reserve_lock);
1da177e4
LT
977}
978
c2866763
CL
979/**
980 * xprt_create_transport - create an RPC transport
96802a09 981 * @args: rpc transport creation arguments
c2866763
CL
982 *
983 */
3c341b0b 984struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
c2866763 985{
c2866763
CL
986 struct rpc_xprt *xprt;
987 struct rpc_rqst *req;
bc25571e 988 struct xprt_class *t;
c2866763 989
bc25571e
TT
990 spin_lock(&xprt_list_lock);
991 list_for_each_entry(t, &xprt_list, list) {
4fa016eb 992 if (t->ident == args->ident) {
bc25571e
TT
993 spin_unlock(&xprt_list_lock);
994 goto found;
995 }
c2866763 996 }
bc25571e 997 spin_unlock(&xprt_list_lock);
4fa016eb 998 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
bc25571e
TT
999 return ERR_PTR(-EIO);
1000
1001found:
1002 xprt = t->setup(args);
c8541ecd 1003 if (IS_ERR(xprt)) {
46121cf7 1004 dprintk("RPC: xprt_create_transport: failed, %ld\n",
c8541ecd
CL
1005 -PTR_ERR(xprt));
1006 return xprt;
c2866763
CL
1007 }
1008
6b6ca86b 1009 kref_init(&xprt->kref);
c2866763
CL
1010 spin_lock_init(&xprt->transport_lock);
1011 spin_lock_init(&xprt->reserve_lock);
1012
1013 INIT_LIST_HEAD(&xprt->free);
1014 INIT_LIST_HEAD(&xprt->recv);
65f27f38 1015 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
b24b8a24
PE
1016 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1017 (unsigned long)xprt);
c2866763
CL
1018 xprt->last_used = jiffies;
1019 xprt->cwnd = RPC_INITCWND;
a509050b 1020 xprt->bind_index = 0;
c2866763
CL
1021
1022 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1023 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1024 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1025 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1026 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1027
1028 /* initialize free list */
1029 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1030 list_add(&req->rq_list, &xprt->free);
1031
1032 xprt_init_xid(xprt);
1033
46121cf7 1034 dprintk("RPC: created transport %p with %u slots\n", xprt,
c2866763
CL
1035 xprt->max_reqs);
1036
1037 return xprt;
1038}
1039
9903cd1c
CL
1040/**
1041 * xprt_destroy - destroy an RPC transport, killing off all requests.
6b6ca86b 1042 * @kref: kref for the transport to destroy
9903cd1c 1043 *
1da177e4 1044 */
6b6ca86b 1045static void xprt_destroy(struct kref *kref)
1da177e4 1046{
6b6ca86b
TM
1047 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
1048
46121cf7 1049 dprintk("RPC: destroying transport %p\n", xprt);
0065db32
TM
1050 xprt->shutdown = 1;
1051 del_timer_sync(&xprt->timer);
c8541ecd
CL
1052
1053 /*
1054 * Tear down transport state and free the rpc_xprt
1055 */
a246b010 1056 xprt->ops->destroy(xprt);
6b6ca86b 1057}
1da177e4 1058
6b6ca86b
TM
1059/**
1060 * xprt_put - release a reference to an RPC transport.
1061 * @xprt: pointer to the transport
1062 *
1063 */
1064void xprt_put(struct rpc_xprt *xprt)
1065{
1066 kref_put(&xprt->kref, xprt_destroy);
1067}
1068
1069/**
1070 * xprt_get - return a reference to an RPC transport.
1071 * @xprt: pointer to the transport
1072 *
1073 */
1074struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1075{
1076 kref_get(&xprt->kref);
1077 return xprt;
1da177e4 1078}
This page took 0.40501 seconds and 5 git commands to generate.