1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/xprt.c
5 * This is a generic RPC call interface supporting congestion avoidance,
6 * and asynchronous calls.
8 * The interface works like this:
10 * - When a process places a call, it allocates a request slot if
11 * one is available. Otherwise, it sleeps on the backlog queue
13 * - Next, the caller puts together the RPC message, stuffs it into
14 * the request struct, and calls xprt_transmit().
15 * - xprt_transmit sends the message and installs the caller on the
16 * transport's wait list. At the same time, if a reply is expected,
17 * it installs a timer that is run after the packet's timeout has
19 * - When a packet arrives, the data_ready handler walks the list of
20 * pending requests for that transport. If a matching XID is found, the
21 * caller is woken up, and the timer removed.
22 * - When no reply arrives within the timeout interval, the timer is
23 * fired by the kernel and runs xprt_timer(). It either adjusts the
24 * timeout values (minor timeout) or wakes up the caller with a status
26 * - When the caller receives a notification from RPC that a reply arrived,
27 * it should release the RPC slot, and process the reply.
28 * If the call timed out, it may choose to retry the operation by
29 * adjusting the initial timeout value, and simply calling rpc_call
32 * Support for async RPC is done through a set of RPC-specific scheduling
33 * primitives that `transparently' work for processes as well as async
34 * tasks that rely on callbacks.
36 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
38 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
41 #include <linux/module.h>
43 #include <linux/types.h>
44 #include <linux/interrupt.h>
45 #include <linux/workqueue.h>
46 #include <linux/net.h>
47 #include <linux/ktime.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/metrics.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 #include <linux/rcupdate.h>
53 #include <linux/sched/mm.h>
55 #include <trace/events/sunrpc.h>
63 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
64 # define RPCDBG_FACILITY RPCDBG_XPRT
70 static void xprt_init(struct rpc_xprt *xprt, struct net *net);
71 static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
72 static void xprt_destroy(struct rpc_xprt *xprt);
74 static DEFINE_SPINLOCK(xprt_list_lock);
75 static LIST_HEAD(xprt_list);
77 static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
79 unsigned long timeout = jiffies + req->rq_timeout;
81 if (time_before(timeout, req->rq_majortimeo))
83 return req->rq_majortimeo;
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.
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;
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)
111 list_add_tail(&transport->list, &xprt_list);
112 printk(KERN_INFO "RPC: Registered %s transport module.\n",
117 spin_unlock(&xprt_list_lock);
120 EXPORT_SYMBOL_GPL(xprt_register_transport);
123 * xprt_unregister_transport - unregister a transport implementation
124 * @transport: transport to unregister
127 * 0: transport successfully unregistered
128 * -ENOENT: transport never registered
130 int xprt_unregister_transport(struct xprt_class *transport)
132 struct xprt_class *t;
136 spin_lock(&xprt_list_lock);
137 list_for_each_entry(t, &xprt_list, list) {
138 if (t == transport) {
140 "RPC: Unregistered %s transport module.\n",
142 list_del_init(&transport->list);
149 spin_unlock(&xprt_list_lock);
152 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
155 xprt_class_release(const struct xprt_class *t)
157 module_put(t->owner);
160 static const struct xprt_class *
161 xprt_class_find_by_netid_locked(const char *netid)
163 const struct xprt_class *t;
166 list_for_each_entry(t, &xprt_list, list) {
167 for (i = 0; t->netid[i][0] != '\0'; i++) {
168 if (strcmp(t->netid[i], netid) != 0)
170 if (!try_module_get(t->owner))
178 static const struct xprt_class *
179 xprt_class_find_by_netid(const char *netid)
181 const struct xprt_class *t;
183 spin_lock(&xprt_list_lock);
184 t = xprt_class_find_by_netid_locked(netid);
186 spin_unlock(&xprt_list_lock);
187 request_module("rpc%s", netid);
188 spin_lock(&xprt_list_lock);
189 t = xprt_class_find_by_netid_locked(netid);
191 spin_unlock(&xprt_list_lock);
196 * xprt_load_transport - load a transport implementation
197 * @netid: transport to load
200 * 0: transport successfully loaded
201 * -ENOENT: transport module not available
203 int xprt_load_transport(const char *netid)
205 const struct xprt_class *t;
207 t = xprt_class_find_by_netid(netid);
210 xprt_class_release(t);
213 EXPORT_SYMBOL_GPL(xprt_load_transport);
215 static void xprt_clear_locked(struct rpc_xprt *xprt)
217 xprt->snd_task = NULL;
218 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
219 smp_mb__before_atomic();
220 clear_bit(XPRT_LOCKED, &xprt->state);
221 smp_mb__after_atomic();
223 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
227 * xprt_reserve_xprt - serialize write access to transports
228 * @task: task that is requesting access to the transport
229 * @xprt: pointer to the target transport
231 * This prevents mixing the payload of separate requests, and prevents
232 * transport connects from colliding with writes. No congestion control
235 int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
237 struct rpc_rqst *req = task->tk_rqstp;
239 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
240 if (task == xprt->snd_task)
244 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
246 xprt->snd_task = task;
251 xprt_clear_locked(xprt);
253 dprintk("RPC: %5u failed to lock transport %p\n",
255 task->tk_status = -EAGAIN;
256 if (RPC_IS_SOFT(task))
257 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
258 xprt_request_timeout(req));
260 rpc_sleep_on(&xprt->sending, task, NULL);
263 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
266 xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
268 return test_bit(XPRT_CWND_WAIT, &xprt->state);
272 xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
274 if (!list_empty(&xprt->xmit_queue)) {
275 /* Peek at head of queue to see if it can make progress */
276 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
280 set_bit(XPRT_CWND_WAIT, &xprt->state);
284 xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
286 if (!RPCXPRT_CONGESTED(xprt))
287 clear_bit(XPRT_CWND_WAIT, &xprt->state);
291 * xprt_reserve_xprt_cong - serialize write access to transports
292 * @task: task that is requesting access to the transport
294 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
295 * integrated into the decision of whether a request is allowed to be
296 * woken up and given access to the transport.
297 * Note that the lock is only granted if we know there are free slots.
299 int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
301 struct rpc_rqst *req = task->tk_rqstp;
303 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
304 if (task == xprt->snd_task)
309 xprt->snd_task = task;
312 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
314 if (!xprt_need_congestion_window_wait(xprt)) {
315 xprt->snd_task = task;
319 xprt_clear_locked(xprt);
321 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
322 task->tk_status = -EAGAIN;
323 if (RPC_IS_SOFT(task))
324 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
325 xprt_request_timeout(req));
327 rpc_sleep_on(&xprt->sending, task, NULL);
330 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
332 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
336 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
338 spin_lock(&xprt->transport_lock);
339 retval = xprt->ops->reserve_xprt(xprt, task);
340 spin_unlock(&xprt->transport_lock);
344 static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
346 struct rpc_xprt *xprt = data;
348 xprt->snd_task = task;
352 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
354 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
356 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
358 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
359 __xprt_lock_write_func, xprt))
362 xprt_clear_locked(xprt);
365 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
367 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
369 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
371 if (xprt_need_congestion_window_wait(xprt))
373 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
374 __xprt_lock_write_func, xprt))
377 xprt_clear_locked(xprt);
381 * xprt_release_xprt - allow other requests to use a transport
382 * @xprt: transport with other tasks potentially waiting
383 * @task: task that is releasing access to the transport
385 * Note that "task" can be NULL. No congestion control is provided.
387 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
389 if (xprt->snd_task == task) {
390 xprt_clear_locked(xprt);
391 __xprt_lock_write_next(xprt);
394 EXPORT_SYMBOL_GPL(xprt_release_xprt);
397 * xprt_release_xprt_cong - allow other requests to use a transport
398 * @xprt: transport with other tasks potentially waiting
399 * @task: task that is releasing access to the transport
401 * Note that "task" can be NULL. Another task is awoken to use the
402 * transport if the transport's congestion window allows it.
404 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
406 if (xprt->snd_task == task) {
407 xprt_clear_locked(xprt);
408 __xprt_lock_write_next_cong(xprt);
411 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
413 static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
415 if (xprt->snd_task != task)
417 spin_lock(&xprt->transport_lock);
418 xprt->ops->release_xprt(xprt, task);
419 spin_unlock(&xprt->transport_lock);
423 * Van Jacobson congestion avoidance. Check if the congestion window
424 * overflowed. Put the task to sleep if this is the case.
427 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
431 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
432 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
433 if (RPCXPRT_CONGESTED(xprt)) {
434 xprt_set_congestion_window_wait(xprt);
438 xprt->cong += RPC_CWNDSCALE;
443 * Adjust the congestion window, and wake up the next task
444 * that has been sleeping due to congestion
447 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
452 xprt->cong -= RPC_CWNDSCALE;
453 xprt_test_and_clear_congestion_window_wait(xprt);
454 __xprt_lock_write_next_cong(xprt);
458 * xprt_request_get_cong - Request congestion control credits
459 * @xprt: pointer to transport
460 * @req: pointer to RPC request
462 * Useful for transports that require congestion control.
465 xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
471 spin_lock(&xprt->transport_lock);
472 ret = __xprt_get_cong(xprt, req) != 0;
473 spin_unlock(&xprt->transport_lock);
476 EXPORT_SYMBOL_GPL(xprt_request_get_cong);
479 * xprt_release_rqst_cong - housekeeping when request is complete
480 * @task: RPC request that recently completed
482 * Useful for transports that require congestion control.
484 void xprt_release_rqst_cong(struct rpc_task *task)
486 struct rpc_rqst *req = task->tk_rqstp;
488 __xprt_put_cong(req->rq_xprt, req);
490 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
492 static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
494 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
495 __xprt_lock_write_next_cong(xprt);
499 * Clear the congestion window wait flag and wake up the next
500 * entry on xprt->sending
503 xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
505 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
506 spin_lock(&xprt->transport_lock);
507 __xprt_lock_write_next_cong(xprt);
508 spin_unlock(&xprt->transport_lock);
513 * xprt_adjust_cwnd - adjust transport congestion window
514 * @xprt: pointer to xprt
515 * @task: recently completed RPC request used to adjust window
516 * @result: result code of completed RPC request
518 * The transport code maintains an estimate on the maximum number of out-
519 * standing RPC requests, using a smoothed version of the congestion
520 * avoidance implemented in 44BSD. This is basically the Van Jacobson
521 * congestion algorithm: If a retransmit occurs, the congestion window is
522 * halved; otherwise, it is incremented by 1/cwnd when
524 * - a reply is received and
525 * - a full number of requests are outstanding and
526 * - the congestion window hasn't been updated recently.
528 void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
530 struct rpc_rqst *req = task->tk_rqstp;
531 unsigned long cwnd = xprt->cwnd;
533 if (result >= 0 && cwnd <= xprt->cong) {
534 /* The (cwnd >> 1) term makes sure
535 * the result gets rounded properly. */
536 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
537 if (cwnd > RPC_MAXCWND(xprt))
538 cwnd = RPC_MAXCWND(xprt);
539 __xprt_lock_write_next_cong(xprt);
540 } else if (result == -ETIMEDOUT) {
542 if (cwnd < RPC_CWNDSCALE)
543 cwnd = RPC_CWNDSCALE;
545 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
546 xprt->cong, xprt->cwnd, cwnd);
548 __xprt_put_cong(xprt, req);
550 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
553 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
554 * @xprt: transport with waiting tasks
555 * @status: result code to plant in each task before waking it
558 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
561 rpc_wake_up_status(&xprt->pending, status);
563 rpc_wake_up(&xprt->pending);
565 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
568 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
571 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
572 * we don't in general want to force a socket disconnection due to
573 * an incomplete RPC call transmission.
575 void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
577 set_bit(XPRT_WRITE_SPACE, &xprt->state);
579 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
582 xprt_clear_write_space_locked(struct rpc_xprt *xprt)
584 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
585 __xprt_lock_write_next(xprt);
586 dprintk("RPC: write space: waking waiting task on "
594 * xprt_write_space - wake the task waiting for transport output buffer space
595 * @xprt: transport with waiting tasks
597 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
599 bool xprt_write_space(struct rpc_xprt *xprt)
603 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
605 spin_lock(&xprt->transport_lock);
606 ret = xprt_clear_write_space_locked(xprt);
607 spin_unlock(&xprt->transport_lock);
610 EXPORT_SYMBOL_GPL(xprt_write_space);
612 static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
614 s64 delta = ktime_to_ns(ktime_get() - abstime);
615 return likely(delta >= 0) ?
616 jiffies - nsecs_to_jiffies(delta) :
617 jiffies + nsecs_to_jiffies(-delta);
620 static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
622 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
623 unsigned long majortimeo = req->rq_timeout;
625 if (to->to_exponential)
626 majortimeo <<= to->to_retries;
628 majortimeo += to->to_increment * to->to_retries;
629 if (majortimeo > to->to_maxval || majortimeo == 0)
630 majortimeo = to->to_maxval;
634 static void xprt_reset_majortimeo(struct rpc_rqst *req)
636 req->rq_majortimeo += xprt_calc_majortimeo(req);
639 static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
641 unsigned long time_init;
642 struct rpc_xprt *xprt = req->rq_xprt;
644 if (likely(xprt && xprt_connected(xprt)))
647 time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
648 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
649 req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
653 * xprt_adjust_timeout - adjust timeout values for next retransmit
654 * @req: RPC request containing parameters to use for the adjustment
657 int xprt_adjust_timeout(struct rpc_rqst *req)
659 struct rpc_xprt *xprt = req->rq_xprt;
660 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
663 if (time_before(jiffies, req->rq_majortimeo)) {
664 if (to->to_exponential)
665 req->rq_timeout <<= 1;
667 req->rq_timeout += to->to_increment;
668 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
669 req->rq_timeout = to->to_maxval;
672 req->rq_timeout = to->to_initval;
674 xprt_reset_majortimeo(req);
675 /* Reset the RTT counters == "slow start" */
676 spin_lock(&xprt->transport_lock);
677 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
678 spin_unlock(&xprt->transport_lock);
682 if (req->rq_timeout == 0) {
683 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
684 req->rq_timeout = 5 * HZ;
689 static void xprt_autoclose(struct work_struct *work)
691 struct rpc_xprt *xprt =
692 container_of(work, struct rpc_xprt, task_cleanup);
693 unsigned int pflags = memalloc_nofs_save();
695 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
696 xprt->ops->close(xprt);
697 xprt_release_write(xprt, NULL);
698 wake_up_bit(&xprt->state, XPRT_LOCKED);
699 memalloc_nofs_restore(pflags);
703 * xprt_disconnect_done - mark a transport as disconnected
704 * @xprt: transport to flag for disconnect
707 void xprt_disconnect_done(struct rpc_xprt *xprt)
709 dprintk("RPC: disconnected transport %p\n", xprt);
710 spin_lock(&xprt->transport_lock);
711 xprt_clear_connected(xprt);
712 xprt_clear_write_space_locked(xprt);
713 xprt_clear_congestion_window_wait_locked(xprt);
714 xprt_wake_pending_tasks(xprt, -ENOTCONN);
715 spin_unlock(&xprt->transport_lock);
717 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
720 * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
721 * @xprt: transport to disconnect
723 static void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
725 if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
727 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
728 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
729 else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
730 rpc_wake_up_queued_task_set_status(&xprt->pending,
731 xprt->snd_task, -ENOTCONN);
735 * xprt_force_disconnect - force a transport to disconnect
736 * @xprt: transport to disconnect
739 void xprt_force_disconnect(struct rpc_xprt *xprt)
741 /* Don't race with the test_bit() in xprt_clear_locked() */
742 spin_lock(&xprt->transport_lock);
743 xprt_schedule_autoclose_locked(xprt);
744 spin_unlock(&xprt->transport_lock);
746 EXPORT_SYMBOL_GPL(xprt_force_disconnect);
749 xprt_connect_cookie(struct rpc_xprt *xprt)
751 return READ_ONCE(xprt->connect_cookie);
755 xprt_request_retransmit_after_disconnect(struct rpc_task *task)
757 struct rpc_rqst *req = task->tk_rqstp;
758 struct rpc_xprt *xprt = req->rq_xprt;
760 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
761 !xprt_connected(xprt);
765 * xprt_conditional_disconnect - force a transport to disconnect
766 * @xprt: transport to disconnect
767 * @cookie: 'connection cookie'
769 * This attempts to break the connection if and only if 'cookie' matches
770 * the current transport 'connection cookie'. It ensures that we don't
771 * try to break the connection more than once when we need to retransmit
772 * a batch of RPC requests.
775 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
777 /* Don't race with the test_bit() in xprt_clear_locked() */
778 spin_lock(&xprt->transport_lock);
779 if (cookie != xprt->connect_cookie)
781 if (test_bit(XPRT_CLOSING, &xprt->state))
783 xprt_schedule_autoclose_locked(xprt);
785 spin_unlock(&xprt->transport_lock);
789 xprt_has_timer(const struct rpc_xprt *xprt)
791 return xprt->idle_timeout != 0;
795 xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
796 __must_hold(&xprt->transport_lock)
798 xprt->last_used = jiffies;
799 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
800 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
804 xprt_init_autodisconnect(struct timer_list *t)
806 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
808 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
810 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
811 xprt->last_used = jiffies;
812 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
814 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
817 bool xprt_lock_connect(struct rpc_xprt *xprt,
818 struct rpc_task *task,
823 spin_lock(&xprt->transport_lock);
824 if (!test_bit(XPRT_LOCKED, &xprt->state))
826 if (xprt->snd_task != task)
828 set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
829 xprt->snd_task = cookie;
832 spin_unlock(&xprt->transport_lock);
836 void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
838 spin_lock(&xprt->transport_lock);
839 if (xprt->snd_task != cookie)
841 if (!test_bit(XPRT_LOCKED, &xprt->state))
843 xprt->snd_task =NULL;
844 clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
845 xprt->ops->release_xprt(xprt, NULL);
846 xprt_schedule_autodisconnect(xprt);
848 spin_unlock(&xprt->transport_lock);
849 wake_up_bit(&xprt->state, XPRT_LOCKED);
853 * xprt_connect - schedule a transport connect operation
854 * @task: RPC task that is requesting the connect
857 void xprt_connect(struct rpc_task *task)
859 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
861 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
862 xprt, (xprt_connected(xprt) ? "is" : "is not"));
864 if (!xprt_bound(xprt)) {
865 task->tk_status = -EAGAIN;
868 if (!xprt_lock_write(xprt, task))
871 if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
872 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
873 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
874 xprt_request_timeout(task->tk_rqstp));
876 if (test_bit(XPRT_CLOSING, &xprt->state))
878 if (xprt_test_and_set_connecting(xprt))
881 if (!xprt_connected(xprt)) {
882 xprt->stat.connect_start = jiffies;
883 xprt->ops->connect(xprt, task);
885 xprt_clear_connecting(xprt);
887 rpc_wake_up_queued_task(&xprt->pending, task);
890 xprt_release_write(xprt, task);
894 * xprt_reconnect_delay - compute the wait before scheduling a connect
895 * @xprt: transport instance
898 unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
900 unsigned long start, now = jiffies;
902 start = xprt->stat.connect_start + xprt->reestablish_timeout;
903 if (time_after(start, now))
907 EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
910 * xprt_reconnect_backoff - compute the new re-establish timeout
911 * @xprt: transport instance
912 * @init_to: initial reestablish timeout
915 void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
917 xprt->reestablish_timeout <<= 1;
918 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
919 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
920 if (xprt->reestablish_timeout < init_to)
921 xprt->reestablish_timeout = init_to;
923 EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
925 enum xprt_xid_rb_cmp {
930 static enum xprt_xid_rb_cmp
931 xprt_xid_cmp(__be32 xid1, __be32 xid2)
935 if ((__force u32)xid1 < (__force u32)xid2)
940 static struct rpc_rqst *
941 xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
943 struct rb_node *n = xprt->recv_queue.rb_node;
944 struct rpc_rqst *req;
947 req = rb_entry(n, struct rpc_rqst, rq_recv);
948 switch (xprt_xid_cmp(xid, req->rq_xid)) {
963 xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
965 struct rb_node **p = &xprt->recv_queue.rb_node;
966 struct rb_node *n = NULL;
967 struct rpc_rqst *req;
971 req = rb_entry(n, struct rpc_rqst, rq_recv);
972 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
980 WARN_ON_ONCE(new != req);
984 rb_link_node(&new->rq_recv, n, p);
985 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
989 xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
991 rb_erase(&req->rq_recv, &xprt->recv_queue);
995 * xprt_lookup_rqst - find an RPC request corresponding to an XID
996 * @xprt: transport on which the original request was transmitted
997 * @xid: RPC XID of incoming reply
999 * Caller holds xprt->queue_lock.
1001 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1003 struct rpc_rqst *entry;
1005 entry = xprt_request_rb_find(xprt, xid);
1006 if (entry != NULL) {
1007 trace_xprt_lookup_rqst(xprt, xid, 0);
1008 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1012 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
1014 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
1015 xprt->stat.bad_xids++;
1018 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1021 xprt_is_pinned_rqst(struct rpc_rqst *req)
1023 return atomic_read(&req->rq_pin) != 0;
1027 * xprt_pin_rqst - Pin a request on the transport receive list
1028 * @req: Request to pin
1030 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
1031 * so should be holding xprt->queue_lock.
1033 void xprt_pin_rqst(struct rpc_rqst *req)
1035 atomic_inc(&req->rq_pin);
1037 EXPORT_SYMBOL_GPL(xprt_pin_rqst);
1040 * xprt_unpin_rqst - Unpin a request on the transport receive list
1041 * @req: Request to pin
1043 * Caller should be holding xprt->queue_lock.
1045 void xprt_unpin_rqst(struct rpc_rqst *req)
1047 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1048 atomic_dec(&req->rq_pin);
1051 if (atomic_dec_and_test(&req->rq_pin))
1052 wake_up_var(&req->rq_pin);
1054 EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1056 static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
1058 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1062 xprt_request_data_received(struct rpc_task *task)
1064 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1065 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1069 xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1071 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1072 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1076 * xprt_request_enqueue_receive - Add an request to the receive queue
1081 xprt_request_enqueue_receive(struct rpc_task *task)
1083 struct rpc_rqst *req = task->tk_rqstp;
1084 struct rpc_xprt *xprt = req->rq_xprt;
1086 if (!xprt_request_need_enqueue_receive(task, req))
1089 xprt_request_prepare(task->tk_rqstp);
1090 spin_lock(&xprt->queue_lock);
1092 /* Update the softirq receive buffer */
1093 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1094 sizeof(req->rq_private_buf));
1096 /* Add request to the receive list */
1097 xprt_request_rb_insert(xprt, req);
1098 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1099 spin_unlock(&xprt->queue_lock);
1101 /* Turn off autodisconnect */
1102 del_singleshot_timer_sync(&xprt->timer);
1106 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1109 * Caller must hold xprt->queue_lock.
1112 xprt_request_dequeue_receive_locked(struct rpc_task *task)
1114 struct rpc_rqst *req = task->tk_rqstp;
1116 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1117 xprt_request_rb_remove(req->rq_xprt, req);
1121 * xprt_update_rtt - Update RPC RTT statistics
1122 * @task: RPC request that recently completed
1124 * Caller holds xprt->queue_lock.
1126 void xprt_update_rtt(struct rpc_task *task)
1128 struct rpc_rqst *req = task->tk_rqstp;
1129 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
1130 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1131 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1134 if (req->rq_ntrans == 1)
1135 rpc_update_rtt(rtt, timer, m);
1136 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1139 EXPORT_SYMBOL_GPL(xprt_update_rtt);
1142 * xprt_complete_rqst - called when reply processing is complete
1143 * @task: RPC request that recently completed
1144 * @copied: actual number of bytes received from the transport
1146 * Caller holds xprt->queue_lock.
1148 void xprt_complete_rqst(struct rpc_task *task, int copied)
1150 struct rpc_rqst *req = task->tk_rqstp;
1151 struct rpc_xprt *xprt = req->rq_xprt;
1153 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1154 task->tk_pid, ntohl(req->rq_xid), copied);
1155 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
1159 req->rq_private_buf.len = copied;
1160 /* Ensure all writes are done before we update */
1161 /* req->rq_reply_bytes_recvd */
1163 req->rq_reply_bytes_recvd = copied;
1164 xprt_request_dequeue_receive_locked(task);
1165 rpc_wake_up_queued_task(&xprt->pending, task);
1167 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1169 static void xprt_timer(struct rpc_task *task)
1171 struct rpc_rqst *req = task->tk_rqstp;
1172 struct rpc_xprt *xprt = req->rq_xprt;
1174 if (task->tk_status != -ETIMEDOUT)
1177 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1178 if (!req->rq_reply_bytes_recvd) {
1179 if (xprt->ops->timer)
1180 xprt->ops->timer(xprt, task);
1182 task->tk_status = 0;
1186 * xprt_wait_for_reply_request_def - wait for reply
1187 * @task: pointer to rpc_task
1189 * Set a request's retransmit timeout based on the transport's
1190 * default timeout parameters. Used by transports that don't adjust
1191 * the retransmit timeout based on round-trip time estimation,
1192 * and put the task to sleep on the pending queue.
1194 void xprt_wait_for_reply_request_def(struct rpc_task *task)
1196 struct rpc_rqst *req = task->tk_rqstp;
1198 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1199 xprt_request_timeout(req));
1201 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1204 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1205 * @task: pointer to rpc_task
1207 * Set a request's retransmit timeout using the RTT estimator,
1208 * and put the task to sleep on the pending queue.
1210 void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1212 int timer = task->tk_msg.rpc_proc->p_timer;
1213 struct rpc_clnt *clnt = task->tk_client;
1214 struct rpc_rtt *rtt = clnt->cl_rtt;
1215 struct rpc_rqst *req = task->tk_rqstp;
1216 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
1217 unsigned long timeout;
1219 timeout = rpc_calc_rto(rtt, timer);
1220 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1221 if (timeout > max_timeout || timeout == 0)
1222 timeout = max_timeout;
1223 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1226 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1229 * xprt_request_wait_receive - wait for the reply to an RPC request
1230 * @task: RPC task about to send a request
1233 void xprt_request_wait_receive(struct rpc_task *task)
1235 struct rpc_rqst *req = task->tk_rqstp;
1236 struct rpc_xprt *xprt = req->rq_xprt;
1238 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1241 * Sleep on the pending queue if we're expecting a reply.
1242 * The spinlock ensures atomicity between the test of
1243 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1245 spin_lock(&xprt->queue_lock);
1246 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1247 xprt->ops->wait_for_reply_request(task);
1249 * Send an extra queue wakeup call if the
1250 * connection was dropped in case the call to
1251 * rpc_sleep_on() raced.
1253 if (xprt_request_retransmit_after_disconnect(task))
1254 rpc_wake_up_queued_task_set_status(&xprt->pending,
1257 spin_unlock(&xprt->queue_lock);
1261 xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1263 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1267 * xprt_request_enqueue_transmit - queue a task for transmission
1268 * @task: pointer to rpc_task
1270 * Add a task to the transmission queue.
1273 xprt_request_enqueue_transmit(struct rpc_task *task)
1275 struct rpc_rqst *pos, *req = task->tk_rqstp;
1276 struct rpc_xprt *xprt = req->rq_xprt;
1278 if (xprt_request_need_enqueue_transmit(task, req)) {
1279 req->rq_bytes_sent = 0;
1280 spin_lock(&xprt->queue_lock);
1282 * Requests that carry congestion control credits are added
1283 * to the head of the list to avoid starvation issues.
1286 xprt_clear_congestion_window_wait(xprt);
1287 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1290 /* Note: req is added _before_ pos */
1291 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1292 INIT_LIST_HEAD(&req->rq_xmit2);
1293 trace_xprt_enq_xmit(task, 1);
1296 } else if (RPC_IS_SWAPPER(task)) {
1297 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1298 if (pos->rq_cong || pos->rq_bytes_sent)
1300 if (RPC_IS_SWAPPER(pos->rq_task))
1302 /* Note: req is added _before_ pos */
1303 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1304 INIT_LIST_HEAD(&req->rq_xmit2);
1305 trace_xprt_enq_xmit(task, 2);
1308 } else if (!req->rq_seqno) {
1309 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1310 if (pos->rq_task->tk_owner != task->tk_owner)
1312 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1313 INIT_LIST_HEAD(&req->rq_xmit);
1314 trace_xprt_enq_xmit(task, 3);
1318 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1319 INIT_LIST_HEAD(&req->rq_xmit2);
1320 trace_xprt_enq_xmit(task, 4);
1322 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1323 spin_unlock(&xprt->queue_lock);
1328 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1329 * @task: pointer to rpc_task
1331 * Remove a task from the transmission queue
1332 * Caller must hold xprt->queue_lock
1335 xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1337 struct rpc_rqst *req = task->tk_rqstp;
1339 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1341 if (!list_empty(&req->rq_xmit)) {
1342 list_del(&req->rq_xmit);
1343 if (!list_empty(&req->rq_xmit2)) {
1344 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1345 struct rpc_rqst, rq_xmit2);
1346 list_del(&req->rq_xmit2);
1347 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1350 list_del(&req->rq_xmit2);
1354 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1355 * @task: pointer to rpc_task
1357 * Remove a task from the transmission queue
1360 xprt_request_dequeue_transmit(struct rpc_task *task)
1362 struct rpc_rqst *req = task->tk_rqstp;
1363 struct rpc_xprt *xprt = req->rq_xprt;
1365 spin_lock(&xprt->queue_lock);
1366 xprt_request_dequeue_transmit_locked(task);
1367 spin_unlock(&xprt->queue_lock);
1371 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1372 * @task: pointer to rpc_task
1374 * Remove a task from the transmit and receive queues, and ensure that
1375 * it is not pinned by the receive work item.
1378 xprt_request_dequeue_xprt(struct rpc_task *task)
1380 struct rpc_rqst *req = task->tk_rqstp;
1381 struct rpc_xprt *xprt = req->rq_xprt;
1383 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1384 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1385 xprt_is_pinned_rqst(req)) {
1386 spin_lock(&xprt->queue_lock);
1387 xprt_request_dequeue_transmit_locked(task);
1388 xprt_request_dequeue_receive_locked(task);
1389 while (xprt_is_pinned_rqst(req)) {
1390 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1391 spin_unlock(&xprt->queue_lock);
1392 xprt_wait_on_pinned_rqst(req);
1393 spin_lock(&xprt->queue_lock);
1394 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1396 spin_unlock(&xprt->queue_lock);
1401 * xprt_request_prepare - prepare an encoded request for transport
1402 * @req: pointer to rpc_rqst
1404 * Calls into the transport layer to do whatever is needed to prepare
1405 * the request for transmission or receive.
1408 xprt_request_prepare(struct rpc_rqst *req)
1410 struct rpc_xprt *xprt = req->rq_xprt;
1412 if (xprt->ops->prepare_request)
1413 xprt->ops->prepare_request(req);
1417 * xprt_request_need_retransmit - Test if a task needs retransmission
1418 * @task: pointer to rpc_task
1420 * Test for whether a connection breakage requires the task to retransmit
1423 xprt_request_need_retransmit(struct rpc_task *task)
1425 return xprt_request_retransmit_after_disconnect(task);
1429 * xprt_prepare_transmit - reserve the transport before sending a request
1430 * @task: RPC task about to send a request
1433 bool xprt_prepare_transmit(struct rpc_task *task)
1435 struct rpc_rqst *req = task->tk_rqstp;
1436 struct rpc_xprt *xprt = req->rq_xprt;
1438 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1440 if (!xprt_lock_write(xprt, task)) {
1441 /* Race breaker: someone may have transmitted us */
1442 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1443 rpc_wake_up_queued_task_set_status(&xprt->sending,
1451 void xprt_end_transmit(struct rpc_task *task)
1453 xprt_release_write(task->tk_rqstp->rq_xprt, task);
1457 * xprt_request_transmit - send an RPC request on a transport
1458 * @req: pointer to request to transmit
1459 * @snd_task: RPC task that owns the transport lock
1461 * This performs the transmission of a single request.
1462 * Note that if the request is not the same as snd_task, then it
1463 * does need to be pinned.
1464 * Returns '0' on success.
1467 xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
1469 struct rpc_xprt *xprt = req->rq_xprt;
1470 struct rpc_task *task = req->rq_task;
1471 unsigned int connect_cookie;
1472 int is_retrans = RPC_WAS_SENT(task);
1475 if (!req->rq_bytes_sent) {
1476 if (xprt_request_data_received(task)) {
1480 /* Verify that our message lies in the RPCSEC_GSS window */
1481 if (rpcauth_xmit_need_reencode(task)) {
1485 if (RPC_SIGNALLED(task)) {
1486 status = -ERESTARTSYS;
1492 * Update req->rq_ntrans before transmitting to avoid races with
1493 * xprt_update_rtt(), which needs to know that it is recording a
1494 * reply to the first transmission.
1498 connect_cookie = xprt->connect_cookie;
1499 status = xprt->ops->send_request(req);
1502 trace_xprt_transmit(req, status);
1507 task->tk_client->cl_stats->rpcretrans++;
1509 xprt_inject_disconnect(xprt);
1511 task->tk_flags |= RPC_TASK_SENT;
1512 spin_lock(&xprt->transport_lock);
1515 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1516 xprt->stat.bklog_u += xprt->backlog.qlen;
1517 xprt->stat.sending_u += xprt->sending.qlen;
1518 xprt->stat.pending_u += xprt->pending.qlen;
1519 spin_unlock(&xprt->transport_lock);
1521 req->rq_connect_cookie = connect_cookie;
1523 trace_xprt_transmit(req, status);
1524 xprt_request_dequeue_transmit(task);
1525 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1530 * xprt_transmit - send an RPC request on a transport
1531 * @task: controlling RPC task
1533 * Attempts to drain the transmit queue. On exit, either the transport
1534 * signalled an error that needs to be handled before transmission can
1535 * resume, or @task finished transmitting, and detected that it already
1539 xprt_transmit(struct rpc_task *task)
1541 struct rpc_rqst *next, *req = task->tk_rqstp;
1542 struct rpc_xprt *xprt = req->rq_xprt;
1545 spin_lock(&xprt->queue_lock);
1547 next = list_first_entry_or_null(&xprt->xmit_queue,
1548 struct rpc_rqst, rq_xmit);
1551 xprt_pin_rqst(next);
1552 spin_unlock(&xprt->queue_lock);
1553 status = xprt_request_transmit(next, task);
1554 if (status == -EBADMSG && next != req)
1556 spin_lock(&xprt->queue_lock);
1557 xprt_unpin_rqst(next);
1559 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1560 task->tk_status = status;
1563 /* Was @task transmitted, and has it received a reply? */
1564 if (xprt_request_data_received(task) &&
1565 !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1567 cond_resched_lock(&xprt->queue_lock);
1569 spin_unlock(&xprt->queue_lock);
1572 static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1574 set_bit(XPRT_CONGESTED, &xprt->state);
1575 rpc_sleep_on(&xprt->backlog, task, NULL);
1578 static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1580 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1581 clear_bit(XPRT_CONGESTED, &xprt->state);
1584 static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1588 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1590 spin_lock(&xprt->reserve_lock);
1591 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1592 rpc_sleep_on(&xprt->backlog, task, NULL);
1595 spin_unlock(&xprt->reserve_lock);
1600 static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1602 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1604 if (xprt->num_reqs >= xprt->max_reqs)
1607 spin_unlock(&xprt->reserve_lock);
1608 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1609 spin_lock(&xprt->reserve_lock);
1613 req = ERR_PTR(-ENOMEM);
1618 static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1620 if (xprt->num_reqs > xprt->min_reqs) {
1628 void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1630 struct rpc_rqst *req;
1632 spin_lock(&xprt->reserve_lock);
1633 if (!list_empty(&xprt->free)) {
1634 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1635 list_del(&req->rq_list);
1638 req = xprt_dynamic_alloc_slot(xprt);
1641 switch (PTR_ERR(req)) {
1643 dprintk("RPC: dynamic allocation of request slot "
1644 "failed! Retrying\n");
1645 task->tk_status = -ENOMEM;
1648 xprt_add_backlog(xprt, task);
1649 dprintk("RPC: waiting for request slot\n");
1652 task->tk_status = -EAGAIN;
1654 spin_unlock(&xprt->reserve_lock);
1657 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1659 spin_unlock(&xprt->reserve_lock);
1661 task->tk_status = 0;
1662 task->tk_rqstp = req;
1664 EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1666 void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1668 spin_lock(&xprt->reserve_lock);
1669 if (!xprt_dynamic_free_slot(xprt, req)) {
1670 memset(req, 0, sizeof(*req)); /* mark unused */
1671 list_add(&req->rq_list, &xprt->free);
1673 xprt_wake_up_backlog(xprt);
1674 spin_unlock(&xprt->reserve_lock);
1676 EXPORT_SYMBOL_GPL(xprt_free_slot);
1678 static void xprt_free_all_slots(struct rpc_xprt *xprt)
1680 struct rpc_rqst *req;
1681 while (!list_empty(&xprt->free)) {
1682 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1683 list_del(&req->rq_list);
1688 struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1689 unsigned int num_prealloc,
1690 unsigned int max_alloc)
1692 struct rpc_xprt *xprt;
1693 struct rpc_rqst *req;
1696 xprt = kzalloc(size, GFP_KERNEL);
1700 xprt_init(xprt, net);
1702 for (i = 0; i < num_prealloc; i++) {
1703 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1706 list_add(&req->rq_list, &xprt->free);
1708 if (max_alloc > num_prealloc)
1709 xprt->max_reqs = max_alloc;
1711 xprt->max_reqs = num_prealloc;
1712 xprt->min_reqs = num_prealloc;
1713 xprt->num_reqs = num_prealloc;
1722 EXPORT_SYMBOL_GPL(xprt_alloc);
1724 void xprt_free(struct rpc_xprt *xprt)
1726 put_net(xprt->xprt_net);
1727 xprt_free_all_slots(xprt);
1728 kfree_rcu(xprt, rcu);
1730 EXPORT_SYMBOL_GPL(xprt_free);
1733 xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1735 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1739 xprt_alloc_xid(struct rpc_xprt *xprt)
1743 spin_lock(&xprt->reserve_lock);
1744 xid = (__force __be32)xprt->xid++;
1745 spin_unlock(&xprt->reserve_lock);
1750 xprt_init_xid(struct rpc_xprt *xprt)
1752 xprt->xid = prandom_u32();
1756 xprt_request_init(struct rpc_task *task)
1758 struct rpc_xprt *xprt = task->tk_xprt;
1759 struct rpc_rqst *req = task->tk_rqstp;
1761 req->rq_task = task;
1762 req->rq_xprt = xprt;
1763 req->rq_buffer = NULL;
1764 req->rq_xid = xprt_alloc_xid(xprt);
1765 xprt_init_connect_cookie(req, xprt);
1766 req->rq_snd_buf.len = 0;
1767 req->rq_snd_buf.buflen = 0;
1768 req->rq_rcv_buf.len = 0;
1769 req->rq_rcv_buf.buflen = 0;
1770 req->rq_snd_buf.bvec = NULL;
1771 req->rq_rcv_buf.bvec = NULL;
1772 req->rq_release_snd_buf = NULL;
1773 xprt_init_majortimeo(task, req);
1774 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1775 req, ntohl(req->rq_xid));
1779 xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1781 xprt->ops->alloc_slot(xprt, task);
1782 if (task->tk_rqstp != NULL)
1783 xprt_request_init(task);
1787 * xprt_reserve - allocate an RPC request slot
1788 * @task: RPC task requesting a slot allocation
1790 * If the transport is marked as being congested, or if no more
1791 * slots are available, place the task on the transport's
1794 void xprt_reserve(struct rpc_task *task)
1796 struct rpc_xprt *xprt = task->tk_xprt;
1798 task->tk_status = 0;
1799 if (task->tk_rqstp != NULL)
1802 task->tk_status = -EAGAIN;
1803 if (!xprt_throttle_congested(xprt, task))
1804 xprt_do_reserve(xprt, task);
1808 * xprt_retry_reserve - allocate an RPC request slot
1809 * @task: RPC task requesting a slot allocation
1811 * If no more slots are available, place the task on the transport's
1813 * Note that the only difference with xprt_reserve is that we now
1814 * ignore the value of the XPRT_CONGESTED flag.
1816 void xprt_retry_reserve(struct rpc_task *task)
1818 struct rpc_xprt *xprt = task->tk_xprt;
1820 task->tk_status = 0;
1821 if (task->tk_rqstp != NULL)
1824 task->tk_status = -EAGAIN;
1825 xprt_do_reserve(xprt, task);
1829 * xprt_release - release an RPC request slot
1830 * @task: task which is finished with the slot
1833 void xprt_release(struct rpc_task *task)
1835 struct rpc_xprt *xprt;
1836 struct rpc_rqst *req = task->tk_rqstp;
1839 if (task->tk_client) {
1840 xprt = task->tk_xprt;
1841 xprt_release_write(xprt, task);
1846 xprt = req->rq_xprt;
1847 xprt_request_dequeue_xprt(task);
1848 spin_lock(&xprt->transport_lock);
1849 xprt->ops->release_xprt(xprt, task);
1850 if (xprt->ops->release_request)
1851 xprt->ops->release_request(task);
1852 xprt_schedule_autodisconnect(xprt);
1853 spin_unlock(&xprt->transport_lock);
1855 xprt->ops->buf_free(task);
1856 xprt_inject_disconnect(xprt);
1857 xdr_free_bvec(&req->rq_rcv_buf);
1858 xdr_free_bvec(&req->rq_snd_buf);
1859 if (req->rq_cred != NULL)
1860 put_rpccred(req->rq_cred);
1861 task->tk_rqstp = NULL;
1862 if (req->rq_release_snd_buf)
1863 req->rq_release_snd_buf(req);
1865 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1866 if (likely(!bc_prealloc(req)))
1867 xprt->ops->free_slot(xprt, req);
1869 xprt_free_bc_request(req);
1872 #ifdef CONFIG_SUNRPC_BACKCHANNEL
1874 xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1876 struct xdr_buf *xbufp = &req->rq_snd_buf;
1878 task->tk_rqstp = req;
1879 req->rq_task = task;
1880 xprt_init_connect_cookie(req, req->rq_xprt);
1882 * Set up the xdr_buf length.
1883 * This also indicates that the buffer is XDR encoded already.
1885 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1886 xbufp->tail[0].iov_len;
1890 static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1892 kref_init(&xprt->kref);
1894 spin_lock_init(&xprt->transport_lock);
1895 spin_lock_init(&xprt->reserve_lock);
1896 spin_lock_init(&xprt->queue_lock);
1898 INIT_LIST_HEAD(&xprt->free);
1899 xprt->recv_queue = RB_ROOT;
1900 INIT_LIST_HEAD(&xprt->xmit_queue);
1901 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1902 spin_lock_init(&xprt->bc_pa_lock);
1903 INIT_LIST_HEAD(&xprt->bc_pa_list);
1904 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1905 INIT_LIST_HEAD(&xprt->xprt_switch);
1907 xprt->last_used = jiffies;
1908 xprt->cwnd = RPC_INITCWND;
1909 xprt->bind_index = 0;
1911 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1912 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1913 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1914 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1916 xprt_init_xid(xprt);
1918 xprt->xprt_net = get_net(net);
1922 * xprt_create_transport - create an RPC transport
1923 * @args: rpc transport creation arguments
1926 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1928 struct rpc_xprt *xprt;
1929 struct xprt_class *t;
1931 spin_lock(&xprt_list_lock);
1932 list_for_each_entry(t, &xprt_list, list) {
1933 if (t->ident == args->ident) {
1934 spin_unlock(&xprt_list_lock);
1938 spin_unlock(&xprt_list_lock);
1939 dprintk("RPC: transport (%d) not supported\n", args->ident);
1940 return ERR_PTR(-EIO);
1943 xprt = t->setup(args);
1945 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1949 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1950 xprt->idle_timeout = 0;
1951 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1952 if (xprt_has_timer(xprt))
1953 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
1955 timer_setup(&xprt->timer, NULL, 0);
1957 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1959 return ERR_PTR(-EINVAL);
1961 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1962 if (xprt->servername == NULL) {
1964 return ERR_PTR(-ENOMEM);
1967 rpc_xprt_debugfs_register(xprt);
1969 dprintk("RPC: created transport %p with %u slots\n", xprt,
1975 static void xprt_destroy_cb(struct work_struct *work)
1977 struct rpc_xprt *xprt =
1978 container_of(work, struct rpc_xprt, task_cleanup);
1980 rpc_xprt_debugfs_unregister(xprt);
1981 rpc_destroy_wait_queue(&xprt->binding);
1982 rpc_destroy_wait_queue(&xprt->pending);
1983 rpc_destroy_wait_queue(&xprt->sending);
1984 rpc_destroy_wait_queue(&xprt->backlog);
1985 kfree(xprt->servername);
1987 * Destroy any existing back channel
1989 xprt_destroy_backchannel(xprt, UINT_MAX);
1992 * Tear down transport state and free the rpc_xprt
1994 xprt->ops->destroy(xprt);
1998 * xprt_destroy - destroy an RPC transport, killing off all requests.
1999 * @xprt: transport to destroy
2002 static void xprt_destroy(struct rpc_xprt *xprt)
2004 dprintk("RPC: destroying transport %p\n", xprt);
2007 * Exclude transport connect/disconnect handlers and autoclose
2009 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
2012 * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
2013 * is cleared. We use ->transport_lock to ensure the mod_timer()
2014 * can only run *before* del_time_sync(), never after.
2016 spin_lock(&xprt->transport_lock);
2017 del_timer_sync(&xprt->timer);
2018 spin_unlock(&xprt->transport_lock);
2021 * Destroy sockets etc from the system workqueue so they can
2022 * safely flush receive work running on rpciod.
2024 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2025 schedule_work(&xprt->task_cleanup);
2028 static void xprt_destroy_kref(struct kref *kref)
2030 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
2034 * xprt_get - return a reference to an RPC transport.
2035 * @xprt: pointer to the transport
2038 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
2040 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
2044 EXPORT_SYMBOL_GPL(xprt_get);
2047 * xprt_put - release a reference to an RPC transport.
2048 * @xprt: pointer to the transport
2051 void xprt_put(struct rpc_xprt *xprt)
2054 kref_put(&xprt->kref, xprt_destroy_kref);
2056 EXPORT_SYMBOL_GPL(xprt_put);