ACPI: thinkpad-acpi: preserve radio state across shutdown
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / xprt.c
blobb66be675be2841b25cc14bd055d5d181d48d7a00
1 /*
2 * linux/net/sunrpc/xprt.c
4 * This is a generic RPC call interface supporting congestion avoidance,
5 * and asynchronous calls.
7 * The interface works like this:
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
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
16 * is run after the packet's timeout has expired.
17 * - When a packet arrives, the data_ready handler walks the list of
18 * pending requests for that transport. If a matching XID is found, the
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.
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.
34 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
39 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/interrupt.h>
43 #include <linux/workqueue.h>
44 #include <linux/net.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/sunrpc/metrics.h>
50 * Local variables
53 #ifdef RPC_DEBUG
54 # define RPCDBG_FACILITY RPCDBG_XPRT
55 #endif
58 * Local functions
60 static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
61 static inline void do_xprt_reserve(struct rpc_task *);
62 static void xprt_connect_status(struct rpc_task *task);
63 static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
65 static DEFINE_SPINLOCK(xprt_list_lock);
66 static LIST_HEAD(xprt_list);
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
75 * - a reply is received and
76 * - a full number of requests are outstanding and
77 * - the congestion window hasn't been updated recently.
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)
84 #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
86 /**
87 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
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.
93 * Returns:
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
98 int xprt_register_transport(struct xprt_class *transport)
100 struct xprt_class *t;
101 int result;
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 */
107 if (t->ident == transport->ident)
108 goto out;
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;
119 out:
120 spin_unlock(&xprt_list_lock);
121 return result;
123 EXPORT_SYMBOL_GPL(xprt_register_transport);
126 * xprt_unregister_transport - unregister a transport implementation
127 * @transport: transport to unregister
129 * Returns:
130 * 0: transport successfully unregistered
131 * -ENOENT: transport never registered
133 int xprt_unregister_transport(struct xprt_class *transport)
135 struct xprt_class *t;
136 int result;
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;
150 result = -ENOENT;
152 out:
153 spin_unlock(&xprt_list_lock);
154 return result;
156 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
159 * xprt_reserve_xprt - serialize write access to transports
160 * @task: task that is requesting access to the transport
162 * This prevents mixing the payload of separate requests, and prevents
163 * transport connects from colliding with writes. No congestion control
164 * is provided.
166 int xprt_reserve_xprt(struct rpc_task *task)
168 struct rpc_xprt *xprt = task->tk_xprt;
169 struct rpc_rqst *req = task->tk_rqstp;
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;
178 xprt->snd_task = task;
179 if (req) {
180 req->rq_bytes_sent = 0;
181 req->rq_ntrans++;
183 return 1;
185 out_sleep:
186 dprintk("RPC: %5u failed to lock transport %p\n",
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);
192 else
193 rpc_sleep_on(&xprt->sending, task, NULL);
194 return 0;
196 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
198 static void xprt_clear_locked(struct rpc_xprt *xprt)
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
206 queue_work(rpciod_workqueue, &xprt->task_cleanup);
210 * xprt_reserve_xprt_cong - serialize write access to transports
211 * @task: task that is requesting access to the transport
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.
217 int xprt_reserve_xprt_cong(struct rpc_task *task)
219 struct rpc_xprt *xprt = task->tk_xprt;
220 struct rpc_rqst *req = task->tk_rqstp;
222 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
223 if (task == xprt->snd_task)
224 return 1;
225 goto out_sleep;
227 if (__xprt_get_cong(xprt, task)) {
228 xprt->snd_task = task;
229 if (req) {
230 req->rq_bytes_sent = 0;
231 req->rq_ntrans++;
233 return 1;
235 xprt_clear_locked(xprt);
236 out_sleep:
237 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
238 task->tk_timeout = 0;
239 task->tk_status = -EAGAIN;
240 if (req && req->rq_ntrans)
241 rpc_sleep_on(&xprt->resend, task, NULL);
242 else
243 rpc_sleep_on(&xprt->sending, task, NULL);
244 return 0;
246 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
248 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
250 int retval;
252 spin_lock_bh(&xprt->transport_lock);
253 retval = xprt->ops->reserve_xprt(task);
254 spin_unlock_bh(&xprt->transport_lock);
255 return retval;
258 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
260 struct rpc_task *task;
261 struct rpc_rqst *req;
263 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
264 return;
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;
273 req = task->tk_rqstp;
274 xprt->snd_task = task;
275 if (req) {
276 req->rq_bytes_sent = 0;
277 req->rq_ntrans++;
279 return;
281 out_unlock:
282 xprt_clear_locked(xprt);
285 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
287 struct rpc_task *task;
289 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
290 return;
291 if (RPCXPRT_CONGESTED(xprt))
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;
299 if (__xprt_get_cong(xprt, task)) {
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++;
306 return;
308 out_unlock:
309 xprt_clear_locked(xprt);
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
317 * Note that "task" can be NULL. No congestion control is provided.
319 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
321 if (xprt->snd_task == task) {
322 xprt_clear_locked(xprt);
323 __xprt_lock_write_next(xprt);
326 EXPORT_SYMBOL_GPL(xprt_release_xprt);
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
333 * Note that "task" can be NULL. Another task is awoken to use the
334 * transport if the transport's congestion window allows it.
336 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
338 if (xprt->snd_task == task) {
339 xprt_clear_locked(xprt);
340 __xprt_lock_write_next_cong(xprt);
343 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
345 static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
347 spin_lock_bh(&xprt->transport_lock);
348 xprt->ops->release_xprt(xprt, task);
349 spin_unlock_bh(&xprt->transport_lock);
353 * Van Jacobson congestion avoidance. Check if the congestion window
354 * overflowed. Put the task to sleep if this is the case.
356 static int
357 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
359 struct rpc_rqst *req = task->tk_rqstp;
361 if (req->rq_cong)
362 return 1;
363 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
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;
373 * Adjust the congestion window, and wake up the next task
374 * that has been sleeping due to congestion
376 static void
377 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
379 if (!req->rq_cong)
380 return;
381 req->rq_cong = 0;
382 xprt->cong -= RPC_CWNDSCALE;
383 __xprt_lock_write_next_cong(xprt);
387 * xprt_release_rqst_cong - housekeeping when request is complete
388 * @task: RPC request that recently completed
390 * Useful for transports that require congestion control.
392 void xprt_release_rqst_cong(struct rpc_task *task)
394 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
396 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
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
403 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
405 void xprt_adjust_cwnd(struct rpc_task *task, int result)
407 struct rpc_rqst *req = task->tk_rqstp;
408 struct rpc_xprt *xprt = task->tk_xprt;
409 unsigned long cwnd = xprt->cwnd;
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);
417 __xprt_lock_write_next_cong(xprt);
418 } else if (result == -ETIMEDOUT) {
419 cwnd >>= 1;
420 if (cwnd < RPC_CWNDSCALE)
421 cwnd = RPC_CWNDSCALE;
423 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
424 xprt->cong, xprt->cwnd, cwnd);
425 xprt->cwnd = cwnd;
426 __xprt_put_cong(xprt, req);
428 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
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
436 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
438 if (status < 0)
439 rpc_wake_up_status(&xprt->pending, status);
440 else
441 rpc_wake_up(&xprt->pending);
443 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
446 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
447 * @task: task to be put to sleep
448 * @action: function pointer to be executed after wait
450 void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
452 struct rpc_rqst *req = task->tk_rqstp;
453 struct rpc_xprt *xprt = req->rq_xprt;
455 task->tk_timeout = req->rq_timeout;
456 rpc_sleep_on(&xprt->pending, task, action);
458 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
461 * xprt_write_space - wake the task waiting for transport output buffer space
462 * @xprt: transport with waiting tasks
464 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
466 void xprt_write_space(struct rpc_xprt *xprt)
468 if (unlikely(xprt->shutdown))
469 return;
471 spin_lock_bh(&xprt->transport_lock);
472 if (xprt->snd_task) {
473 dprintk("RPC: write space: waking waiting task on "
474 "xprt %p\n", xprt);
475 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
477 spin_unlock_bh(&xprt->transport_lock);
479 EXPORT_SYMBOL_GPL(xprt_write_space);
482 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
483 * @task: task whose timeout is to be set
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.
489 void xprt_set_retrans_timeout_def(struct rpc_task *task)
491 task->tk_timeout = task->tk_rqstp->rq_timeout;
493 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
496 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
497 * @task: task whose timeout is to be set
499 * Set a request's retransmit timeout using the RTT estimator.
501 void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
503 int timer = task->tk_msg.rpc_proc->p_timer;
504 struct rpc_clnt *clnt = task->tk_client;
505 struct rpc_rtt *rtt = clnt->cl_rtt;
506 struct rpc_rqst *req = task->tk_rqstp;
507 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
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;
514 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
516 static void xprt_reset_majortimeo(struct rpc_rqst *req)
518 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
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;
531 * xprt_adjust_timeout - adjust timeout values for next retransmit
532 * @req: RPC request containing parameters to use for the adjustment
535 int xprt_adjust_timeout(struct rpc_rqst *req)
537 struct rpc_xprt *xprt = req->rq_xprt;
538 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
539 int status = 0;
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++;
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" */
554 spin_lock_bh(&xprt->transport_lock);
555 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
556 spin_unlock_bh(&xprt->transport_lock);
557 status = -ETIMEDOUT;
560 if (req->rq_timeout == 0) {
561 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
562 req->rq_timeout = 5 * HZ;
564 return status;
567 static void xprt_autoclose(struct work_struct *work)
569 struct rpc_xprt *xprt =
570 container_of(work, struct rpc_xprt, task_cleanup);
572 xprt->ops->close(xprt);
573 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
574 xprt_release_write(xprt, NULL);
578 * xprt_disconnect_done - mark a transport as disconnected
579 * @xprt: transport to flag for disconnect
582 void xprt_disconnect_done(struct rpc_xprt *xprt)
584 dprintk("RPC: disconnected transport %p\n", xprt);
585 spin_lock_bh(&xprt->transport_lock);
586 xprt_clear_connected(xprt);
587 xprt_wake_pending_tasks(xprt, -ENOTCONN);
588 spin_unlock_bh(&xprt->transport_lock);
590 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
593 * xprt_force_disconnect - force a transport to disconnect
594 * @xprt: transport to disconnect
597 void xprt_force_disconnect(struct rpc_xprt *xprt)
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);
605 xprt_wake_pending_tasks(xprt, -ENOTCONN);
606 spin_unlock_bh(&xprt->transport_lock);
610 * xprt_conditional_disconnect - force a transport to disconnect
611 * @xprt: transport to disconnect
612 * @cookie: 'connection cookie'
614 * This attempts to break the connection if and only if 'cookie' matches
615 * the current transport 'connection cookie'. It ensures that we don't
616 * try to break the connection more than once when we need to retransmit
617 * a batch of RPC requests.
620 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
622 /* Don't race with the test_bit() in xprt_clear_locked() */
623 spin_lock_bh(&xprt->transport_lock);
624 if (cookie != xprt->connect_cookie)
625 goto out;
626 if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
627 goto out;
628 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
629 /* Try to schedule an autoclose RPC call */
630 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
631 queue_work(rpciod_workqueue, &xprt->task_cleanup);
632 xprt_wake_pending_tasks(xprt, -ENOTCONN);
633 out:
634 spin_unlock_bh(&xprt->transport_lock);
637 static void
638 xprt_init_autodisconnect(unsigned long data)
640 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
642 spin_lock(&xprt->transport_lock);
643 if (!list_empty(&xprt->recv) || xprt->shutdown)
644 goto out_abort;
645 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
646 goto out_abort;
647 spin_unlock(&xprt->transport_lock);
648 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
649 queue_work(rpciod_workqueue, &xprt->task_cleanup);
650 return;
651 out_abort:
652 spin_unlock(&xprt->transport_lock);
656 * xprt_connect - schedule a transport connect operation
657 * @task: RPC task that is requesting the connect
660 void xprt_connect(struct rpc_task *task)
662 struct rpc_xprt *xprt = task->tk_xprt;
664 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
665 xprt, (xprt_connected(xprt) ? "is" : "is not"));
667 if (!xprt_bound(xprt)) {
668 task->tk_status = -EIO;
669 return;
671 if (!xprt_lock_write(xprt, task))
672 return;
673 if (xprt_connected(xprt))
674 xprt_release_write(xprt, task);
675 else {
676 if (task->tk_rqstp)
677 task->tk_rqstp->rq_bytes_sent = 0;
679 task->tk_timeout = xprt->connect_timeout;
680 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
681 xprt->stat.connect_start = jiffies;
682 xprt->ops->connect(task);
684 return;
687 static void xprt_connect_status(struct rpc_task *task)
689 struct rpc_xprt *xprt = task->tk_xprt;
691 if (task->tk_status == 0) {
692 xprt->stat.connect_count++;
693 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
694 dprintk("RPC: %5u xprt_connect_status: connection established\n",
695 task->tk_pid);
696 return;
699 switch (task->tk_status) {
700 case -ENOTCONN:
701 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
702 task->tk_pid);
703 break;
704 case -ETIMEDOUT:
705 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
706 "out\n", task->tk_pid);
707 break;
708 default:
709 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
710 "server %s\n", task->tk_pid, -task->tk_status,
711 task->tk_client->cl_server);
712 xprt_release_write(xprt, task);
713 task->tk_status = -EIO;
718 * xprt_lookup_rqst - find an RPC request corresponding to an XID
719 * @xprt: transport on which the original request was transmitted
720 * @xid: RPC XID of incoming reply
723 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
725 struct list_head *pos;
727 list_for_each(pos, &xprt->recv) {
728 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
729 if (entry->rq_xid == xid)
730 return entry;
733 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
734 ntohl(xid));
735 xprt->stat.bad_xids++;
736 return NULL;
738 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
741 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
742 * @task: RPC request that recently completed
745 void xprt_update_rtt(struct rpc_task *task)
747 struct rpc_rqst *req = task->tk_rqstp;
748 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
749 unsigned timer = task->tk_msg.rpc_proc->p_timer;
751 if (timer) {
752 if (req->rq_ntrans == 1)
753 rpc_update_rtt(rtt, timer,
754 (long)jiffies - req->rq_xtime);
755 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
758 EXPORT_SYMBOL_GPL(xprt_update_rtt);
761 * xprt_complete_rqst - called when reply processing is complete
762 * @task: RPC request that recently completed
763 * @copied: actual number of bytes received from the transport
765 * Caller holds transport lock.
767 void xprt_complete_rqst(struct rpc_task *task, int copied)
769 struct rpc_rqst *req = task->tk_rqstp;
770 struct rpc_xprt *xprt = req->rq_xprt;
772 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
773 task->tk_pid, ntohl(req->rq_xid), copied);
775 xprt->stat.recvs++;
776 task->tk_rtt = (long)jiffies - req->rq_xtime;
778 list_del_init(&req->rq_list);
779 req->rq_private_buf.len = copied;
780 /* Ensure all writes are done before we update req->rq_received */
781 smp_wmb();
782 req->rq_received = copied;
783 rpc_wake_up_queued_task(&xprt->pending, task);
785 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
787 static void xprt_timer(struct rpc_task *task)
789 struct rpc_rqst *req = task->tk_rqstp;
790 struct rpc_xprt *xprt = req->rq_xprt;
792 if (task->tk_status != -ETIMEDOUT)
793 return;
794 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
796 spin_lock_bh(&xprt->transport_lock);
797 if (!req->rq_received) {
798 if (xprt->ops->timer)
799 xprt->ops->timer(task);
800 } else
801 task->tk_status = 0;
802 spin_unlock_bh(&xprt->transport_lock);
806 * xprt_prepare_transmit - reserve the transport before sending a request
807 * @task: RPC task about to send a request
810 int xprt_prepare_transmit(struct rpc_task *task)
812 struct rpc_rqst *req = task->tk_rqstp;
813 struct rpc_xprt *xprt = req->rq_xprt;
814 int err = 0;
816 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
818 spin_lock_bh(&xprt->transport_lock);
819 if (req->rq_received && !req->rq_bytes_sent) {
820 err = req->rq_received;
821 goto out_unlock;
823 if (!xprt->ops->reserve_xprt(task)) {
824 err = -EAGAIN;
825 goto out_unlock;
828 if (!xprt_connected(xprt)) {
829 err = -ENOTCONN;
830 goto out_unlock;
832 out_unlock:
833 spin_unlock_bh(&xprt->transport_lock);
834 return err;
837 void xprt_end_transmit(struct rpc_task *task)
839 xprt_release_write(task->tk_xprt, task);
843 * xprt_transmit - send an RPC request on a transport
844 * @task: controlling RPC task
846 * We have to copy the iovec because sendmsg fiddles with its contents.
848 void xprt_transmit(struct rpc_task *task)
850 struct rpc_rqst *req = task->tk_rqstp;
851 struct rpc_xprt *xprt = req->rq_xprt;
852 int status;
854 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
856 if (!req->rq_received) {
857 if (list_empty(&req->rq_list)) {
858 spin_lock_bh(&xprt->transport_lock);
859 /* Update the softirq receive buffer */
860 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
861 sizeof(req->rq_private_buf));
862 /* Add request to the receive list */
863 list_add_tail(&req->rq_list, &xprt->recv);
864 spin_unlock_bh(&xprt->transport_lock);
865 xprt_reset_majortimeo(req);
866 /* Turn off autodisconnect */
867 del_singleshot_timer_sync(&xprt->timer);
869 } else if (!req->rq_bytes_sent)
870 return;
872 req->rq_connect_cookie = xprt->connect_cookie;
873 req->rq_xtime = jiffies;
874 status = xprt->ops->send_request(task);
875 if (status == 0) {
876 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
877 spin_lock_bh(&xprt->transport_lock);
879 xprt->ops->set_retrans_timeout(task);
881 xprt->stat.sends++;
882 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
883 xprt->stat.bklog_u += xprt->backlog.qlen;
885 /* Don't race with disconnect */
886 if (!xprt_connected(xprt))
887 task->tk_status = -ENOTCONN;
888 else if (!req->rq_received)
889 rpc_sleep_on(&xprt->pending, task, xprt_timer);
890 spin_unlock_bh(&xprt->transport_lock);
891 return;
894 /* Note: at this point, task->tk_sleeping has not yet been set,
895 * hence there is no danger of the waking up task being put on
896 * schedq, and being picked up by a parallel run of rpciod().
898 task->tk_status = status;
899 if (status == -ECONNREFUSED)
900 rpc_sleep_on(&xprt->sending, task, NULL);
903 static inline void do_xprt_reserve(struct rpc_task *task)
905 struct rpc_xprt *xprt = task->tk_xprt;
907 task->tk_status = 0;
908 if (task->tk_rqstp)
909 return;
910 if (!list_empty(&xprt->free)) {
911 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
912 list_del_init(&req->rq_list);
913 task->tk_rqstp = req;
914 xprt_request_init(task, xprt);
915 return;
917 dprintk("RPC: waiting for request slot\n");
918 task->tk_status = -EAGAIN;
919 task->tk_timeout = 0;
920 rpc_sleep_on(&xprt->backlog, task, NULL);
924 * xprt_reserve - allocate an RPC request slot
925 * @task: RPC task requesting a slot allocation
927 * If no more slots are available, place the task on the transport's
928 * backlog queue.
930 void xprt_reserve(struct rpc_task *task)
932 struct rpc_xprt *xprt = task->tk_xprt;
934 task->tk_status = -EIO;
935 spin_lock(&xprt->reserve_lock);
936 do_xprt_reserve(task);
937 spin_unlock(&xprt->reserve_lock);
940 static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
942 return xprt->xid++;
945 static inline void xprt_init_xid(struct rpc_xprt *xprt)
947 xprt->xid = net_random();
950 static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
952 struct rpc_rqst *req = task->tk_rqstp;
954 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
955 req->rq_task = task;
956 req->rq_xprt = xprt;
957 req->rq_buffer = NULL;
958 req->rq_xid = xprt_alloc_xid(xprt);
959 req->rq_release_snd_buf = NULL;
960 xprt_reset_majortimeo(req);
961 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
962 req, ntohl(req->rq_xid));
966 * xprt_release - release an RPC request slot
967 * @task: task which is finished with the slot
970 void xprt_release(struct rpc_task *task)
972 struct rpc_xprt *xprt = task->tk_xprt;
973 struct rpc_rqst *req;
975 if (!(req = task->tk_rqstp))
976 return;
977 rpc_count_iostats(task);
978 spin_lock_bh(&xprt->transport_lock);
979 xprt->ops->release_xprt(xprt, task);
980 if (xprt->ops->release_request)
981 xprt->ops->release_request(task);
982 if (!list_empty(&req->rq_list))
983 list_del(&req->rq_list);
984 xprt->last_used = jiffies;
985 if (list_empty(&xprt->recv))
986 mod_timer(&xprt->timer,
987 xprt->last_used + xprt->idle_timeout);
988 spin_unlock_bh(&xprt->transport_lock);
989 xprt->ops->buf_free(req->rq_buffer);
990 task->tk_rqstp = NULL;
991 if (req->rq_release_snd_buf)
992 req->rq_release_snd_buf(req);
993 memset(req, 0, sizeof(*req)); /* mark unused */
995 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
997 spin_lock(&xprt->reserve_lock);
998 list_add(&req->rq_list, &xprt->free);
999 rpc_wake_up_next(&xprt->backlog);
1000 spin_unlock(&xprt->reserve_lock);
1004 * xprt_create_transport - create an RPC transport
1005 * @args: rpc transport creation arguments
1008 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1010 struct rpc_xprt *xprt;
1011 struct rpc_rqst *req;
1012 struct xprt_class *t;
1014 spin_lock(&xprt_list_lock);
1015 list_for_each_entry(t, &xprt_list, list) {
1016 if (t->ident == args->ident) {
1017 spin_unlock(&xprt_list_lock);
1018 goto found;
1021 spin_unlock(&xprt_list_lock);
1022 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
1023 return ERR_PTR(-EIO);
1025 found:
1026 xprt = t->setup(args);
1027 if (IS_ERR(xprt)) {
1028 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1029 -PTR_ERR(xprt));
1030 return xprt;
1033 kref_init(&xprt->kref);
1034 spin_lock_init(&xprt->transport_lock);
1035 spin_lock_init(&xprt->reserve_lock);
1037 INIT_LIST_HEAD(&xprt->free);
1038 INIT_LIST_HEAD(&xprt->recv);
1039 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1040 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1041 (unsigned long)xprt);
1042 xprt->last_used = jiffies;
1043 xprt->cwnd = RPC_INITCWND;
1044 xprt->bind_index = 0;
1046 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1047 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1048 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1049 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1050 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1052 /* initialize free list */
1053 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1054 list_add(&req->rq_list, &xprt->free);
1056 xprt_init_xid(xprt);
1058 dprintk("RPC: created transport %p with %u slots\n", xprt,
1059 xprt->max_reqs);
1061 return xprt;
1065 * xprt_destroy - destroy an RPC transport, killing off all requests.
1066 * @kref: kref for the transport to destroy
1069 static void xprt_destroy(struct kref *kref)
1071 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
1073 dprintk("RPC: destroying transport %p\n", xprt);
1074 xprt->shutdown = 1;
1075 del_timer_sync(&xprt->timer);
1077 rpc_destroy_wait_queue(&xprt->binding);
1078 rpc_destroy_wait_queue(&xprt->pending);
1079 rpc_destroy_wait_queue(&xprt->sending);
1080 rpc_destroy_wait_queue(&xprt->resend);
1081 rpc_destroy_wait_queue(&xprt->backlog);
1083 * Tear down transport state and free the rpc_xprt
1085 xprt->ops->destroy(xprt);
1089 * xprt_put - release a reference to an RPC transport.
1090 * @xprt: pointer to the transport
1093 void xprt_put(struct rpc_xprt *xprt)
1095 kref_put(&xprt->kref, xprt_destroy);
1099 * xprt_get - return a reference to an RPC transport.
1100 * @xprt: pointer to the transport
1103 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1105 kref_get(&xprt->kref);
1106 return xprt;