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
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, if a reply is expected,
16 * it installs a timer that is run after the packet's timeout has
18 * - When a packet arrives, the data_ready handler walks the list of
19 * pending requests for that transport. If a matching XID is found, the
20 * caller is woken up, and the timer removed.
21 * - When no reply arrives within the timeout interval, the timer is
22 * fired by the kernel and runs xprt_timer(). It either adjusts the
23 * timeout values (minor timeout) or wakes up the caller with a status
25 * - When the caller receives a notification from RPC that a reply arrived,
26 * it should release the RPC slot, and process the reply.
27 * If the call timed out, it may choose to retry the operation by
28 * adjusting the initial timeout value, and simply calling rpc_call
31 * Support for async RPC is done through a set of RPC-specific scheduling
32 * primitives that `transparently' work for processes as well as async
33 * tasks that rely on callbacks.
35 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
37 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
40 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/interrupt.h>
44 #include <linux/workqueue.h>
45 #include <linux/net.h>
46 #include <linux/ktime.h>
48 #include <linux/sunrpc/clnt.h>
49 #include <linux/sunrpc/metrics.h>
50 #include <linux/sunrpc/bc_xprt.h>
51 #include <linux/rcupdate.h>
53 #include <trace/events/sunrpc.h>
61 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
62 # define RPCDBG_FACILITY RPCDBG_XPRT
68 static void xprt_init(struct rpc_xprt *xprt, struct net *net);
69 static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
70 static void xprt_connect_status(struct rpc_task *task);
71 static void xprt_destroy(struct rpc_xprt *xprt);
73 static DEFINE_SPINLOCK(xprt_list_lock);
74 static LIST_HEAD(xprt_list);
77 * xprt_register_transport - register a transport implementation
78 * @transport: transport to register
80 * If a transport implementation is loaded as a kernel module, it can
81 * call this interface to make itself known to the RPC client.
84 * 0: transport successfully registered
85 * -EEXIST: transport already registered
86 * -EINVAL: transport module being unloaded
88 int xprt_register_transport(struct xprt_class *transport)
94 spin_lock(&xprt_list_lock);
95 list_for_each_entry(t, &xprt_list, list) {
96 /* don't register the same transport class twice */
97 if (t->ident == transport->ident)
101 list_add_tail(&transport->list, &xprt_list);
102 printk(KERN_INFO "RPC: Registered %s transport module.\n",
107 spin_unlock(&xprt_list_lock);
110 EXPORT_SYMBOL_GPL(xprt_register_transport);
113 * xprt_unregister_transport - unregister a transport implementation
114 * @transport: transport to unregister
117 * 0: transport successfully unregistered
118 * -ENOENT: transport never registered
120 int xprt_unregister_transport(struct xprt_class *transport)
122 struct xprt_class *t;
126 spin_lock(&xprt_list_lock);
127 list_for_each_entry(t, &xprt_list, list) {
128 if (t == transport) {
130 "RPC: Unregistered %s transport module.\n",
132 list_del_init(&transport->list);
139 spin_unlock(&xprt_list_lock);
142 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
145 * xprt_load_transport - load a transport implementation
146 * @transport_name: transport to load
149 * 0: transport successfully loaded
150 * -ENOENT: transport module not available
152 int xprt_load_transport(const char *transport_name)
154 struct xprt_class *t;
158 spin_lock(&xprt_list_lock);
159 list_for_each_entry(t, &xprt_list, list) {
160 if (strcmp(t->name, transport_name) == 0) {
161 spin_unlock(&xprt_list_lock);
165 spin_unlock(&xprt_list_lock);
166 result = request_module("xprt%s", transport_name);
170 EXPORT_SYMBOL_GPL(xprt_load_transport);
172 static void xprt_clear_locked(struct rpc_xprt *xprt)
174 xprt->snd_task = NULL;
175 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
176 smp_mb__before_atomic();
177 clear_bit(XPRT_LOCKED, &xprt->state);
178 smp_mb__after_atomic();
180 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
184 * xprt_reserve_xprt - serialize write access to transports
185 * @task: task that is requesting access to the transport
186 * @xprt: pointer to the target transport
188 * This prevents mixing the payload of separate requests, and prevents
189 * transport connects from colliding with writes. No congestion control
192 int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
194 struct rpc_rqst *req = task->tk_rqstp;
196 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
197 if (task == xprt->snd_task)
201 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
203 xprt->snd_task = task;
208 xprt_clear_locked(xprt);
210 dprintk("RPC: %5u failed to lock transport %p\n",
212 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
213 task->tk_status = -EAGAIN;
214 rpc_sleep_on(&xprt->sending, task, NULL);
217 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
220 xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
222 return test_bit(XPRT_CWND_WAIT, &xprt->state);
226 xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
228 if (!list_empty(&xprt->xmit_queue)) {
229 /* Peek at head of queue to see if it can make progress */
230 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
234 set_bit(XPRT_CWND_WAIT, &xprt->state);
238 xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
240 if (!RPCXPRT_CONGESTED(xprt))
241 clear_bit(XPRT_CWND_WAIT, &xprt->state);
245 * xprt_reserve_xprt_cong - serialize write access to transports
246 * @task: task that is requesting access to the transport
248 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
249 * integrated into the decision of whether a request is allowed to be
250 * woken up and given access to the transport.
251 * Note that the lock is only granted if we know there are free slots.
253 int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
255 struct rpc_rqst *req = task->tk_rqstp;
257 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
258 if (task == xprt->snd_task)
263 xprt->snd_task = task;
266 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
268 if (!xprt_need_congestion_window_wait(xprt)) {
269 xprt->snd_task = task;
273 xprt_clear_locked(xprt);
275 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
276 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
277 task->tk_status = -EAGAIN;
278 rpc_sleep_on(&xprt->sending, task, NULL);
281 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
283 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
287 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
289 spin_lock_bh(&xprt->transport_lock);
290 retval = xprt->ops->reserve_xprt(xprt, task);
291 spin_unlock_bh(&xprt->transport_lock);
295 static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
297 struct rpc_xprt *xprt = data;
299 xprt->snd_task = task;
303 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
305 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
307 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
309 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
310 __xprt_lock_write_func, xprt))
313 xprt_clear_locked(xprt);
316 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
318 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
320 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
322 if (xprt_need_congestion_window_wait(xprt))
324 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
325 __xprt_lock_write_func, xprt))
328 xprt_clear_locked(xprt);
332 * xprt_release_xprt - allow other requests to use a transport
333 * @xprt: transport with other tasks potentially waiting
334 * @task: task that is releasing access to the transport
336 * Note that "task" can be NULL. No congestion control is provided.
338 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
340 if (xprt->snd_task == task) {
341 xprt_clear_locked(xprt);
342 __xprt_lock_write_next(xprt);
345 EXPORT_SYMBOL_GPL(xprt_release_xprt);
348 * xprt_release_xprt_cong - allow other requests to use a transport
349 * @xprt: transport with other tasks potentially waiting
350 * @task: task that is releasing access to the transport
352 * Note that "task" can be NULL. Another task is awoken to use the
353 * transport if the transport's congestion window allows it.
355 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
357 if (xprt->snd_task == task) {
358 xprt_clear_locked(xprt);
359 __xprt_lock_write_next_cong(xprt);
362 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
364 static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
366 if (xprt->snd_task != task)
368 spin_lock_bh(&xprt->transport_lock);
369 xprt->ops->release_xprt(xprt, task);
370 spin_unlock_bh(&xprt->transport_lock);
374 * Van Jacobson congestion avoidance. Check if the congestion window
375 * overflowed. Put the task to sleep if this is the case.
378 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
382 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
383 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
384 if (RPCXPRT_CONGESTED(xprt)) {
385 xprt_set_congestion_window_wait(xprt);
389 xprt->cong += RPC_CWNDSCALE;
394 * Adjust the congestion window, and wake up the next task
395 * that has been sleeping due to congestion
398 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
403 xprt->cong -= RPC_CWNDSCALE;
404 xprt_test_and_clear_congestion_window_wait(xprt);
405 __xprt_lock_write_next_cong(xprt);
409 * xprt_request_get_cong - Request congestion control credits
410 * @xprt: pointer to transport
411 * @req: pointer to RPC request
413 * Useful for transports that require congestion control.
416 xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
422 spin_lock_bh(&xprt->transport_lock);
423 ret = __xprt_get_cong(xprt, req) != 0;
424 spin_unlock_bh(&xprt->transport_lock);
427 EXPORT_SYMBOL_GPL(xprt_request_get_cong);
430 * xprt_release_rqst_cong - housekeeping when request is complete
431 * @task: RPC request that recently completed
433 * Useful for transports that require congestion control.
435 void xprt_release_rqst_cong(struct rpc_task *task)
437 struct rpc_rqst *req = task->tk_rqstp;
439 __xprt_put_cong(req->rq_xprt, req);
441 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
444 * Clear the congestion window wait flag and wake up the next
445 * entry on xprt->sending
448 xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
450 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
451 spin_lock_bh(&xprt->transport_lock);
452 __xprt_lock_write_next_cong(xprt);
453 spin_unlock_bh(&xprt->transport_lock);
458 * xprt_adjust_cwnd - adjust transport congestion window
459 * @xprt: pointer to xprt
460 * @task: recently completed RPC request used to adjust window
461 * @result: result code of completed RPC request
463 * The transport code maintains an estimate on the maximum number of out-
464 * standing RPC requests, using a smoothed version of the congestion
465 * avoidance implemented in 44BSD. This is basically the Van Jacobson
466 * congestion algorithm: If a retransmit occurs, the congestion window is
467 * halved; otherwise, it is incremented by 1/cwnd when
469 * - a reply is received and
470 * - a full number of requests are outstanding and
471 * - the congestion window hasn't been updated recently.
473 void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
475 struct rpc_rqst *req = task->tk_rqstp;
476 unsigned long cwnd = xprt->cwnd;
478 if (result >= 0 && cwnd <= xprt->cong) {
479 /* The (cwnd >> 1) term makes sure
480 * the result gets rounded properly. */
481 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
482 if (cwnd > RPC_MAXCWND(xprt))
483 cwnd = RPC_MAXCWND(xprt);
484 __xprt_lock_write_next_cong(xprt);
485 } else if (result == -ETIMEDOUT) {
487 if (cwnd < RPC_CWNDSCALE)
488 cwnd = RPC_CWNDSCALE;
490 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
491 xprt->cong, xprt->cwnd, cwnd);
493 __xprt_put_cong(xprt, req);
495 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
498 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
499 * @xprt: transport with waiting tasks
500 * @status: result code to plant in each task before waking it
503 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
506 rpc_wake_up_status(&xprt->pending, status);
508 rpc_wake_up(&xprt->pending);
510 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
513 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
516 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
517 * we don't in general want to force a socket disconnection due to
518 * an incomplete RPC call transmission.
520 void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
522 set_bit(XPRT_WRITE_SPACE, &xprt->state);
524 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
527 xprt_clear_write_space_locked(struct rpc_xprt *xprt)
529 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
530 __xprt_lock_write_next(xprt);
531 dprintk("RPC: write space: waking waiting task on "
539 * xprt_write_space - wake the task waiting for transport output buffer space
540 * @xprt: transport with waiting tasks
542 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
544 bool xprt_write_space(struct rpc_xprt *xprt)
548 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
550 spin_lock_bh(&xprt->transport_lock);
551 ret = xprt_clear_write_space_locked(xprt);
552 spin_unlock_bh(&xprt->transport_lock);
555 EXPORT_SYMBOL_GPL(xprt_write_space);
558 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
559 * @task: task whose timeout is to be set
561 * Set a request's retransmit timeout based on the transport's
562 * default timeout parameters. Used by transports that don't adjust
563 * the retransmit timeout based on round-trip time estimation.
565 void xprt_set_retrans_timeout_def(struct rpc_task *task)
567 task->tk_timeout = task->tk_rqstp->rq_timeout;
569 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
572 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
573 * @task: task whose timeout is to be set
575 * Set a request's retransmit timeout using the RTT estimator.
577 void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
579 int timer = task->tk_msg.rpc_proc->p_timer;
580 struct rpc_clnt *clnt = task->tk_client;
581 struct rpc_rtt *rtt = clnt->cl_rtt;
582 struct rpc_rqst *req = task->tk_rqstp;
583 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
585 task->tk_timeout = rpc_calc_rto(rtt, timer);
586 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
587 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
588 task->tk_timeout = max_timeout;
590 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
592 static void xprt_reset_majortimeo(struct rpc_rqst *req)
594 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
596 req->rq_majortimeo = req->rq_timeout;
597 if (to->to_exponential)
598 req->rq_majortimeo <<= to->to_retries;
600 req->rq_majortimeo += to->to_increment * to->to_retries;
601 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
602 req->rq_majortimeo = to->to_maxval;
603 req->rq_majortimeo += jiffies;
607 * xprt_adjust_timeout - adjust timeout values for next retransmit
608 * @req: RPC request containing parameters to use for the adjustment
611 int xprt_adjust_timeout(struct rpc_rqst *req)
613 struct rpc_xprt *xprt = req->rq_xprt;
614 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
617 if (time_before(jiffies, req->rq_majortimeo)) {
618 if (to->to_exponential)
619 req->rq_timeout <<= 1;
621 req->rq_timeout += to->to_increment;
622 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
623 req->rq_timeout = to->to_maxval;
626 req->rq_timeout = to->to_initval;
628 xprt_reset_majortimeo(req);
629 /* Reset the RTT counters == "slow start" */
630 spin_lock_bh(&xprt->transport_lock);
631 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
632 spin_unlock_bh(&xprt->transport_lock);
636 if (req->rq_timeout == 0) {
637 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
638 req->rq_timeout = 5 * HZ;
643 static void xprt_autoclose(struct work_struct *work)
645 struct rpc_xprt *xprt =
646 container_of(work, struct rpc_xprt, task_cleanup);
648 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
649 xprt->ops->close(xprt);
650 xprt_release_write(xprt, NULL);
651 wake_up_bit(&xprt->state, XPRT_LOCKED);
655 * xprt_disconnect_done - mark a transport as disconnected
656 * @xprt: transport to flag for disconnect
659 void xprt_disconnect_done(struct rpc_xprt *xprt)
661 dprintk("RPC: disconnected transport %p\n", xprt);
662 spin_lock_bh(&xprt->transport_lock);
663 xprt_clear_connected(xprt);
664 xprt_clear_write_space_locked(xprt);
665 xprt_wake_pending_tasks(xprt, -EAGAIN);
666 spin_unlock_bh(&xprt->transport_lock);
668 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
671 * xprt_force_disconnect - force a transport to disconnect
672 * @xprt: transport to disconnect
675 void xprt_force_disconnect(struct rpc_xprt *xprt)
677 /* Don't race with the test_bit() in xprt_clear_locked() */
678 spin_lock_bh(&xprt->transport_lock);
679 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
680 /* Try to schedule an autoclose RPC call */
681 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
682 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
683 xprt_wake_pending_tasks(xprt, -EAGAIN);
684 spin_unlock_bh(&xprt->transport_lock);
686 EXPORT_SYMBOL_GPL(xprt_force_disconnect);
689 xprt_connect_cookie(struct rpc_xprt *xprt)
691 return READ_ONCE(xprt->connect_cookie);
695 xprt_request_retransmit_after_disconnect(struct rpc_task *task)
697 struct rpc_rqst *req = task->tk_rqstp;
698 struct rpc_xprt *xprt = req->rq_xprt;
700 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
701 !xprt_connected(xprt);
705 * xprt_conditional_disconnect - force a transport to disconnect
706 * @xprt: transport to disconnect
707 * @cookie: 'connection cookie'
709 * This attempts to break the connection if and only if 'cookie' matches
710 * the current transport 'connection cookie'. It ensures that we don't
711 * try to break the connection more than once when we need to retransmit
712 * a batch of RPC requests.
715 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
717 /* Don't race with the test_bit() in xprt_clear_locked() */
718 spin_lock_bh(&xprt->transport_lock);
719 if (cookie != xprt->connect_cookie)
721 if (test_bit(XPRT_CLOSING, &xprt->state))
723 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
724 /* Try to schedule an autoclose RPC call */
725 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
726 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
727 xprt_wake_pending_tasks(xprt, -EAGAIN);
729 spin_unlock_bh(&xprt->transport_lock);
733 xprt_has_timer(const struct rpc_xprt *xprt)
735 return xprt->idle_timeout != 0;
739 xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
740 __must_hold(&xprt->transport_lock)
742 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
743 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
747 xprt_init_autodisconnect(struct timer_list *t)
749 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
751 spin_lock(&xprt->transport_lock);
752 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
754 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
755 xprt->last_used = jiffies;
756 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
758 spin_unlock(&xprt->transport_lock);
759 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
762 spin_unlock(&xprt->transport_lock);
765 bool xprt_lock_connect(struct rpc_xprt *xprt,
766 struct rpc_task *task,
771 spin_lock_bh(&xprt->transport_lock);
772 if (!test_bit(XPRT_LOCKED, &xprt->state))
774 if (xprt->snd_task != task)
776 xprt->snd_task = cookie;
779 spin_unlock_bh(&xprt->transport_lock);
783 void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
785 spin_lock_bh(&xprt->transport_lock);
786 if (xprt->snd_task != cookie)
788 if (!test_bit(XPRT_LOCKED, &xprt->state))
790 xprt->snd_task =NULL;
791 xprt->ops->release_xprt(xprt, NULL);
792 xprt_schedule_autodisconnect(xprt);
794 spin_unlock_bh(&xprt->transport_lock);
795 wake_up_bit(&xprt->state, XPRT_LOCKED);
799 * xprt_connect - schedule a transport connect operation
800 * @task: RPC task that is requesting the connect
803 void xprt_connect(struct rpc_task *task)
805 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
807 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
808 xprt, (xprt_connected(xprt) ? "is" : "is not"));
810 if (!xprt_bound(xprt)) {
811 task->tk_status = -EAGAIN;
814 if (!xprt_lock_write(xprt, task))
817 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
818 xprt->ops->close(xprt);
820 if (!xprt_connected(xprt)) {
821 task->tk_timeout = task->tk_rqstp->rq_timeout;
822 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
823 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
825 if (test_bit(XPRT_CLOSING, &xprt->state))
827 if (xprt_test_and_set_connecting(xprt))
829 xprt->stat.connect_start = jiffies;
830 xprt->ops->connect(xprt, task);
832 xprt_release_write(xprt, task);
835 static void xprt_connect_status(struct rpc_task *task)
837 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
839 if (task->tk_status == 0) {
840 xprt->stat.connect_count++;
841 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
842 dprintk("RPC: %5u xprt_connect_status: connection established\n",
847 switch (task->tk_status) {
855 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
858 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
859 "out\n", task->tk_pid);
862 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
863 "server %s\n", task->tk_pid, -task->tk_status,
865 task->tk_status = -EIO;
869 enum xprt_xid_rb_cmp {
874 static enum xprt_xid_rb_cmp
875 xprt_xid_cmp(__be32 xid1, __be32 xid2)
879 if ((__force u32)xid1 < (__force u32)xid2)
884 static struct rpc_rqst *
885 xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
887 struct rb_node *n = xprt->recv_queue.rb_node;
888 struct rpc_rqst *req;
891 req = rb_entry(n, struct rpc_rqst, rq_recv);
892 switch (xprt_xid_cmp(xid, req->rq_xid)) {
907 xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
909 struct rb_node **p = &xprt->recv_queue.rb_node;
910 struct rb_node *n = NULL;
911 struct rpc_rqst *req;
915 req = rb_entry(n, struct rpc_rqst, rq_recv);
916 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
924 WARN_ON_ONCE(new != req);
928 rb_link_node(&new->rq_recv, n, p);
929 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
933 xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
935 rb_erase(&req->rq_recv, &xprt->recv_queue);
939 * xprt_lookup_rqst - find an RPC request corresponding to an XID
940 * @xprt: transport on which the original request was transmitted
941 * @xid: RPC XID of incoming reply
943 * Caller holds xprt->queue_lock.
945 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
947 struct rpc_rqst *entry;
949 entry = xprt_request_rb_find(xprt, xid);
951 trace_xprt_lookup_rqst(xprt, xid, 0);
952 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
956 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
958 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
959 xprt->stat.bad_xids++;
962 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
965 xprt_is_pinned_rqst(struct rpc_rqst *req)
967 return atomic_read(&req->rq_pin) != 0;
971 * xprt_pin_rqst - Pin a request on the transport receive list
972 * @req: Request to pin
974 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
975 * so should be holding the xprt receive lock.
977 void xprt_pin_rqst(struct rpc_rqst *req)
979 atomic_inc(&req->rq_pin);
981 EXPORT_SYMBOL_GPL(xprt_pin_rqst);
984 * xprt_unpin_rqst - Unpin a request on the transport receive list
985 * @req: Request to pin
987 * Caller should be holding the xprt receive lock.
989 void xprt_unpin_rqst(struct rpc_rqst *req)
991 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
992 atomic_dec(&req->rq_pin);
995 if (atomic_dec_and_test(&req->rq_pin))
996 wake_up_var(&req->rq_pin);
998 EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1000 static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
1002 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1006 xprt_request_data_received(struct rpc_task *task)
1008 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1009 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1013 xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1015 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1016 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1020 * xprt_request_enqueue_receive - Add an request to the receive queue
1025 xprt_request_enqueue_receive(struct rpc_task *task)
1027 struct rpc_rqst *req = task->tk_rqstp;
1028 struct rpc_xprt *xprt = req->rq_xprt;
1030 if (!xprt_request_need_enqueue_receive(task, req))
1032 spin_lock(&xprt->queue_lock);
1034 /* Update the softirq receive buffer */
1035 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1036 sizeof(req->rq_private_buf));
1038 /* Add request to the receive list */
1039 xprt_request_rb_insert(xprt, req);
1040 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1041 spin_unlock(&xprt->queue_lock);
1043 xprt_reset_majortimeo(req);
1044 /* Turn off autodisconnect */
1045 del_singleshot_timer_sync(&xprt->timer);
1049 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1052 * Caller must hold xprt->queue_lock.
1055 xprt_request_dequeue_receive_locked(struct rpc_task *task)
1057 struct rpc_rqst *req = task->tk_rqstp;
1059 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1060 xprt_request_rb_remove(req->rq_xprt, req);
1064 * xprt_update_rtt - Update RPC RTT statistics
1065 * @task: RPC request that recently completed
1067 * Caller holds xprt->queue_lock.
1069 void xprt_update_rtt(struct rpc_task *task)
1071 struct rpc_rqst *req = task->tk_rqstp;
1072 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
1073 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1074 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1077 if (req->rq_ntrans == 1)
1078 rpc_update_rtt(rtt, timer, m);
1079 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1082 EXPORT_SYMBOL_GPL(xprt_update_rtt);
1085 * xprt_complete_rqst - called when reply processing is complete
1086 * @task: RPC request that recently completed
1087 * @copied: actual number of bytes received from the transport
1089 * Caller holds xprt->queue_lock.
1091 void xprt_complete_rqst(struct rpc_task *task, int copied)
1093 struct rpc_rqst *req = task->tk_rqstp;
1094 struct rpc_xprt *xprt = req->rq_xprt;
1096 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1097 task->tk_pid, ntohl(req->rq_xid), copied);
1098 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
1102 req->rq_private_buf.len = copied;
1103 /* Ensure all writes are done before we update */
1104 /* req->rq_reply_bytes_recvd */
1106 req->rq_reply_bytes_recvd = copied;
1107 xprt_request_dequeue_receive_locked(task);
1108 rpc_wake_up_queued_task(&xprt->pending, task);
1110 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1112 static void xprt_timer(struct rpc_task *task)
1114 struct rpc_rqst *req = task->tk_rqstp;
1115 struct rpc_xprt *xprt = req->rq_xprt;
1117 if (task->tk_status != -ETIMEDOUT)
1120 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1121 if (!req->rq_reply_bytes_recvd) {
1122 if (xprt->ops->timer)
1123 xprt->ops->timer(xprt, task);
1125 task->tk_status = 0;
1129 * xprt_request_wait_receive - wait for the reply to an RPC request
1130 * @task: RPC task about to send a request
1133 void xprt_request_wait_receive(struct rpc_task *task)
1135 struct rpc_rqst *req = task->tk_rqstp;
1136 struct rpc_xprt *xprt = req->rq_xprt;
1138 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1141 * Sleep on the pending queue if we're expecting a reply.
1142 * The spinlock ensures atomicity between the test of
1143 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1145 spin_lock(&xprt->queue_lock);
1146 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1147 xprt->ops->set_retrans_timeout(task);
1148 rpc_sleep_on(&xprt->pending, task, xprt_timer);
1150 * Send an extra queue wakeup call if the
1151 * connection was dropped in case the call to
1152 * rpc_sleep_on() raced.
1154 if (xprt_request_retransmit_after_disconnect(task))
1155 rpc_wake_up_queued_task_set_status(&xprt->pending,
1158 spin_unlock(&xprt->queue_lock);
1162 xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1164 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1168 * xprt_request_enqueue_transmit - queue a task for transmission
1169 * @task: pointer to rpc_task
1171 * Add a task to the transmission queue.
1174 xprt_request_enqueue_transmit(struct rpc_task *task)
1176 struct rpc_rqst *pos, *req = task->tk_rqstp;
1177 struct rpc_xprt *xprt = req->rq_xprt;
1179 if (xprt_request_need_enqueue_transmit(task, req)) {
1180 spin_lock(&xprt->queue_lock);
1182 * Requests that carry congestion control credits are added
1183 * to the head of the list to avoid starvation issues.
1186 xprt_clear_congestion_window_wait(xprt);
1187 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1190 /* Note: req is added _before_ pos */
1191 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1192 INIT_LIST_HEAD(&req->rq_xmit2);
1195 } else if (RPC_IS_SWAPPER(task)) {
1196 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1197 if (pos->rq_cong || pos->rq_bytes_sent)
1199 if (RPC_IS_SWAPPER(pos->rq_task))
1201 /* Note: req is added _before_ pos */
1202 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1203 INIT_LIST_HEAD(&req->rq_xmit2);
1207 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1208 if (pos->rq_task->tk_owner != task->tk_owner)
1210 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1211 INIT_LIST_HEAD(&req->rq_xmit);
1215 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1216 INIT_LIST_HEAD(&req->rq_xmit2);
1218 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1219 spin_unlock(&xprt->queue_lock);
1224 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1225 * @task: pointer to rpc_task
1227 * Remove a task from the transmission queue
1228 * Caller must hold xprt->queue_lock
1231 xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1233 struct rpc_rqst *req = task->tk_rqstp;
1235 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1237 if (!list_empty(&req->rq_xmit)) {
1238 list_del(&req->rq_xmit);
1239 if (!list_empty(&req->rq_xmit2)) {
1240 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1241 struct rpc_rqst, rq_xmit2);
1242 list_del(&req->rq_xmit2);
1243 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1246 list_del(&req->rq_xmit2);
1250 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1251 * @task: pointer to rpc_task
1253 * Remove a task from the transmission queue
1256 xprt_request_dequeue_transmit(struct rpc_task *task)
1258 struct rpc_rqst *req = task->tk_rqstp;
1259 struct rpc_xprt *xprt = req->rq_xprt;
1261 spin_lock(&xprt->queue_lock);
1262 xprt_request_dequeue_transmit_locked(task);
1263 spin_unlock(&xprt->queue_lock);
1267 * xprt_request_need_retransmit - Test if a task needs retransmission
1268 * @task: pointer to rpc_task
1270 * Test for whether a connection breakage requires the task to retransmit
1273 xprt_request_need_retransmit(struct rpc_task *task)
1275 return xprt_request_retransmit_after_disconnect(task);
1279 * xprt_prepare_transmit - reserve the transport before sending a request
1280 * @task: RPC task about to send a request
1283 bool xprt_prepare_transmit(struct rpc_task *task)
1285 struct rpc_rqst *req = task->tk_rqstp;
1286 struct rpc_xprt *xprt = req->rq_xprt;
1288 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1290 if (!xprt_lock_write(xprt, task)) {
1291 /* Race breaker: someone may have transmitted us */
1292 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1293 rpc_wake_up_queued_task_set_status(&xprt->sending,
1301 void xprt_end_transmit(struct rpc_task *task)
1303 xprt_release_write(task->tk_rqstp->rq_xprt, task);
1307 * xprt_request_transmit - send an RPC request on a transport
1308 * @req: pointer to request to transmit
1309 * @snd_task: RPC task that owns the transport lock
1311 * This performs the transmission of a single request.
1312 * Note that if the request is not the same as snd_task, then it
1313 * does need to be pinned.
1314 * Returns '0' on success.
1317 xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
1319 struct rpc_xprt *xprt = req->rq_xprt;
1320 struct rpc_task *task = req->rq_task;
1321 unsigned int connect_cookie;
1322 int is_retrans = RPC_WAS_SENT(task);
1325 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1327 if (!req->rq_bytes_sent) {
1328 if (xprt_request_data_received(task)) {
1332 /* Verify that our message lies in the RPCSEC_GSS window */
1333 if (rpcauth_xmit_need_reencode(task)) {
1340 * Update req->rq_ntrans before transmitting to avoid races with
1341 * xprt_update_rtt(), which needs to know that it is recording a
1342 * reply to the first transmission.
1346 connect_cookie = xprt->connect_cookie;
1347 status = xprt->ops->send_request(req);
1348 trace_xprt_transmit(xprt, req->rq_xid, status);
1355 task->tk_client->cl_stats->rpcretrans++;
1357 xprt_inject_disconnect(xprt);
1359 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
1360 task->tk_flags |= RPC_TASK_SENT;
1361 spin_lock_bh(&xprt->transport_lock);
1364 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1365 xprt->stat.bklog_u += xprt->backlog.qlen;
1366 xprt->stat.sending_u += xprt->sending.qlen;
1367 xprt->stat.pending_u += xprt->pending.qlen;
1368 spin_unlock_bh(&xprt->transport_lock);
1370 req->rq_connect_cookie = connect_cookie;
1372 xprt_request_dequeue_transmit(task);
1373 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1378 * xprt_transmit - send an RPC request on a transport
1379 * @task: controlling RPC task
1381 * Attempts to drain the transmit queue. On exit, either the transport
1382 * signalled an error that needs to be handled before transmission can
1383 * resume, or @task finished transmitting, and detected that it already
1387 xprt_transmit(struct rpc_task *task)
1389 struct rpc_rqst *next, *req = task->tk_rqstp;
1390 struct rpc_xprt *xprt = req->rq_xprt;
1393 spin_lock(&xprt->queue_lock);
1394 while (!list_empty(&xprt->xmit_queue)) {
1395 next = list_first_entry(&xprt->xmit_queue,
1396 struct rpc_rqst, rq_xmit);
1397 xprt_pin_rqst(next);
1398 spin_unlock(&xprt->queue_lock);
1399 status = xprt_request_transmit(next, task);
1400 if (status == -EBADMSG && next != req)
1403 spin_lock(&xprt->queue_lock);
1404 xprt_unpin_rqst(next);
1406 if (!xprt_request_data_received(task) ||
1407 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1409 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1410 task->tk_status = status;
1413 spin_unlock(&xprt->queue_lock);
1416 static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1418 set_bit(XPRT_CONGESTED, &xprt->state);
1419 rpc_sleep_on(&xprt->backlog, task, NULL);
1422 static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1424 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1425 clear_bit(XPRT_CONGESTED, &xprt->state);
1428 static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1432 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1434 spin_lock(&xprt->reserve_lock);
1435 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1436 rpc_sleep_on(&xprt->backlog, task, NULL);
1439 spin_unlock(&xprt->reserve_lock);
1444 static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1446 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1448 if (xprt->num_reqs >= xprt->max_reqs)
1451 spin_unlock(&xprt->reserve_lock);
1452 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1453 spin_lock(&xprt->reserve_lock);
1457 req = ERR_PTR(-ENOMEM);
1462 static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1464 if (xprt->num_reqs > xprt->min_reqs) {
1472 void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1474 struct rpc_rqst *req;
1476 spin_lock(&xprt->reserve_lock);
1477 if (!list_empty(&xprt->free)) {
1478 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1479 list_del(&req->rq_list);
1482 req = xprt_dynamic_alloc_slot(xprt);
1485 switch (PTR_ERR(req)) {
1487 dprintk("RPC: dynamic allocation of request slot "
1488 "failed! Retrying\n");
1489 task->tk_status = -ENOMEM;
1492 xprt_add_backlog(xprt, task);
1493 dprintk("RPC: waiting for request slot\n");
1496 task->tk_status = -EAGAIN;
1498 spin_unlock(&xprt->reserve_lock);
1501 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1503 spin_unlock(&xprt->reserve_lock);
1505 task->tk_status = 0;
1506 task->tk_rqstp = req;
1508 EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1510 void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1512 spin_lock(&xprt->reserve_lock);
1513 if (!xprt_dynamic_free_slot(xprt, req)) {
1514 memset(req, 0, sizeof(*req)); /* mark unused */
1515 list_add(&req->rq_list, &xprt->free);
1517 xprt_wake_up_backlog(xprt);
1518 spin_unlock(&xprt->reserve_lock);
1520 EXPORT_SYMBOL_GPL(xprt_free_slot);
1522 static void xprt_free_all_slots(struct rpc_xprt *xprt)
1524 struct rpc_rqst *req;
1525 while (!list_empty(&xprt->free)) {
1526 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1527 list_del(&req->rq_list);
1532 struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1533 unsigned int num_prealloc,
1534 unsigned int max_alloc)
1536 struct rpc_xprt *xprt;
1537 struct rpc_rqst *req;
1540 xprt = kzalloc(size, GFP_KERNEL);
1544 xprt_init(xprt, net);
1546 for (i = 0; i < num_prealloc; i++) {
1547 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1550 list_add(&req->rq_list, &xprt->free);
1552 if (max_alloc > num_prealloc)
1553 xprt->max_reqs = max_alloc;
1555 xprt->max_reqs = num_prealloc;
1556 xprt->min_reqs = num_prealloc;
1557 xprt->num_reqs = num_prealloc;
1566 EXPORT_SYMBOL_GPL(xprt_alloc);
1568 void xprt_free(struct rpc_xprt *xprt)
1570 put_net(xprt->xprt_net);
1571 xprt_free_all_slots(xprt);
1572 kfree_rcu(xprt, rcu);
1574 EXPORT_SYMBOL_GPL(xprt_free);
1577 xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1579 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1583 xprt_alloc_xid(struct rpc_xprt *xprt)
1587 spin_lock(&xprt->reserve_lock);
1588 xid = (__force __be32)xprt->xid++;
1589 spin_unlock(&xprt->reserve_lock);
1594 xprt_init_xid(struct rpc_xprt *xprt)
1596 xprt->xid = prandom_u32();
1600 xprt_request_init(struct rpc_task *task)
1602 struct rpc_xprt *xprt = task->tk_xprt;
1603 struct rpc_rqst *req = task->tk_rqstp;
1605 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1606 req->rq_task = task;
1607 req->rq_xprt = xprt;
1608 req->rq_buffer = NULL;
1609 req->rq_xid = xprt_alloc_xid(xprt);
1610 xprt_init_connect_cookie(req, xprt);
1611 req->rq_bytes_sent = 0;
1612 req->rq_snd_buf.len = 0;
1613 req->rq_snd_buf.buflen = 0;
1614 req->rq_rcv_buf.len = 0;
1615 req->rq_rcv_buf.buflen = 0;
1616 req->rq_release_snd_buf = NULL;
1617 xprt_reset_majortimeo(req);
1618 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1619 req, ntohl(req->rq_xid));
1623 xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1625 xprt->ops->alloc_slot(xprt, task);
1626 if (task->tk_rqstp != NULL)
1627 xprt_request_init(task);
1631 * xprt_reserve - allocate an RPC request slot
1632 * @task: RPC task requesting a slot allocation
1634 * If the transport is marked as being congested, or if no more
1635 * slots are available, place the task on the transport's
1638 void xprt_reserve(struct rpc_task *task)
1640 struct rpc_xprt *xprt = task->tk_xprt;
1642 task->tk_status = 0;
1643 if (task->tk_rqstp != NULL)
1646 task->tk_timeout = 0;
1647 task->tk_status = -EAGAIN;
1648 if (!xprt_throttle_congested(xprt, task))
1649 xprt_do_reserve(xprt, task);
1653 * xprt_retry_reserve - allocate an RPC request slot
1654 * @task: RPC task requesting a slot allocation
1656 * If no more slots are available, place the task on the transport's
1658 * Note that the only difference with xprt_reserve is that we now
1659 * ignore the value of the XPRT_CONGESTED flag.
1661 void xprt_retry_reserve(struct rpc_task *task)
1663 struct rpc_xprt *xprt = task->tk_xprt;
1665 task->tk_status = 0;
1666 if (task->tk_rqstp != NULL)
1669 task->tk_timeout = 0;
1670 task->tk_status = -EAGAIN;
1671 xprt_do_reserve(xprt, task);
1675 xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1677 struct rpc_xprt *xprt = req->rq_xprt;
1679 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1680 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1681 xprt_is_pinned_rqst(req)) {
1682 spin_lock(&xprt->queue_lock);
1683 xprt_request_dequeue_transmit_locked(task);
1684 xprt_request_dequeue_receive_locked(task);
1685 while (xprt_is_pinned_rqst(req)) {
1686 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1687 spin_unlock(&xprt->queue_lock);
1688 xprt_wait_on_pinned_rqst(req);
1689 spin_lock(&xprt->queue_lock);
1690 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1692 spin_unlock(&xprt->queue_lock);
1697 * xprt_release - release an RPC request slot
1698 * @task: task which is finished with the slot
1701 void xprt_release(struct rpc_task *task)
1703 struct rpc_xprt *xprt;
1704 struct rpc_rqst *req = task->tk_rqstp;
1707 if (task->tk_client) {
1708 xprt = task->tk_xprt;
1709 xprt_release_write(xprt, task);
1714 xprt = req->rq_xprt;
1715 if (task->tk_ops->rpc_count_stats != NULL)
1716 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1717 else if (task->tk_client)
1718 rpc_count_iostats(task, task->tk_client->cl_metrics);
1719 xprt_request_dequeue_all(task, req);
1720 spin_lock_bh(&xprt->transport_lock);
1721 xprt->ops->release_xprt(xprt, task);
1722 if (xprt->ops->release_request)
1723 xprt->ops->release_request(task);
1724 xprt->last_used = jiffies;
1725 xprt_schedule_autodisconnect(xprt);
1726 spin_unlock_bh(&xprt->transport_lock);
1728 xprt->ops->buf_free(task);
1729 xprt_inject_disconnect(xprt);
1730 if (req->rq_cred != NULL)
1731 put_rpccred(req->rq_cred);
1732 task->tk_rqstp = NULL;
1733 if (req->rq_release_snd_buf)
1734 req->rq_release_snd_buf(req);
1736 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1737 if (likely(!bc_prealloc(req)))
1738 xprt->ops->free_slot(xprt, req);
1740 xprt_free_bc_request(req);
1743 #ifdef CONFIG_SUNRPC_BACKCHANNEL
1745 xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1747 struct xdr_buf *xbufp = &req->rq_snd_buf;
1749 task->tk_rqstp = req;
1750 req->rq_task = task;
1751 xprt_init_connect_cookie(req, req->rq_xprt);
1753 * Set up the xdr_buf length.
1754 * This also indicates that the buffer is XDR encoded already.
1756 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1757 xbufp->tail[0].iov_len;
1758 req->rq_bytes_sent = 0;
1762 static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1764 kref_init(&xprt->kref);
1766 spin_lock_init(&xprt->transport_lock);
1767 spin_lock_init(&xprt->reserve_lock);
1768 spin_lock_init(&xprt->queue_lock);
1770 INIT_LIST_HEAD(&xprt->free);
1771 xprt->recv_queue = RB_ROOT;
1772 INIT_LIST_HEAD(&xprt->xmit_queue);
1773 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1774 spin_lock_init(&xprt->bc_pa_lock);
1775 INIT_LIST_HEAD(&xprt->bc_pa_list);
1776 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1777 INIT_LIST_HEAD(&xprt->xprt_switch);
1779 xprt->last_used = jiffies;
1780 xprt->cwnd = RPC_INITCWND;
1781 xprt->bind_index = 0;
1783 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1784 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1785 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1786 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1788 xprt_init_xid(xprt);
1790 xprt->xprt_net = get_net(net);
1794 * xprt_create_transport - create an RPC transport
1795 * @args: rpc transport creation arguments
1798 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1800 struct rpc_xprt *xprt;
1801 struct xprt_class *t;
1803 spin_lock(&xprt_list_lock);
1804 list_for_each_entry(t, &xprt_list, list) {
1805 if (t->ident == args->ident) {
1806 spin_unlock(&xprt_list_lock);
1810 spin_unlock(&xprt_list_lock);
1811 dprintk("RPC: transport (%d) not supported\n", args->ident);
1812 return ERR_PTR(-EIO);
1815 xprt = t->setup(args);
1817 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1821 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1822 xprt->idle_timeout = 0;
1823 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1824 if (xprt_has_timer(xprt))
1825 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
1827 timer_setup(&xprt->timer, NULL, 0);
1829 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1831 return ERR_PTR(-EINVAL);
1833 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1834 if (xprt->servername == NULL) {
1836 return ERR_PTR(-ENOMEM);
1839 rpc_xprt_debugfs_register(xprt);
1841 dprintk("RPC: created transport %p with %u slots\n", xprt,
1847 static void xprt_destroy_cb(struct work_struct *work)
1849 struct rpc_xprt *xprt =
1850 container_of(work, struct rpc_xprt, task_cleanup);
1852 rpc_xprt_debugfs_unregister(xprt);
1853 rpc_destroy_wait_queue(&xprt->binding);
1854 rpc_destroy_wait_queue(&xprt->pending);
1855 rpc_destroy_wait_queue(&xprt->sending);
1856 rpc_destroy_wait_queue(&xprt->backlog);
1857 kfree(xprt->servername);
1859 * Tear down transport state and free the rpc_xprt
1861 xprt->ops->destroy(xprt);
1865 * xprt_destroy - destroy an RPC transport, killing off all requests.
1866 * @xprt: transport to destroy
1869 static void xprt_destroy(struct rpc_xprt *xprt)
1871 dprintk("RPC: destroying transport %p\n", xprt);
1874 * Exclude transport connect/disconnect handlers and autoclose
1876 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1878 del_timer_sync(&xprt->timer);
1881 * Destroy sockets etc from the system workqueue so they can
1882 * safely flush receive work running on rpciod.
1884 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1885 schedule_work(&xprt->task_cleanup);
1888 static void xprt_destroy_kref(struct kref *kref)
1890 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1894 * xprt_get - return a reference to an RPC transport.
1895 * @xprt: pointer to the transport
1898 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1900 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1904 EXPORT_SYMBOL_GPL(xprt_get);
1907 * xprt_put - release a reference to an RPC transport.
1908 * @xprt: pointer to the transport
1911 void xprt_put(struct rpc_xprt *xprt)
1914 kref_put(&xprt->kref, xprt_destroy_kref);
1916 EXPORT_SYMBOL_GPL(xprt_put);