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_destroy(struct rpc_xprt *xprt);
72 static DEFINE_SPINLOCK(xprt_list_lock);
73 static LIST_HEAD(xprt_list);
76 * xprt_register_transport - register a transport implementation
77 * @transport: transport to register
79 * If a transport implementation is loaded as a kernel module, it can
80 * call this interface to make itself known to the RPC client.
83 * 0: transport successfully registered
84 * -EEXIST: transport already registered
85 * -EINVAL: transport module being unloaded
87 int xprt_register_transport(struct xprt_class *transport)
93 spin_lock(&xprt_list_lock);
94 list_for_each_entry(t, &xprt_list, list) {
95 /* don't register the same transport class twice */
96 if (t->ident == transport->ident)
100 list_add_tail(&transport->list, &xprt_list);
101 printk(KERN_INFO "RPC: Registered %s transport module.\n",
106 spin_unlock(&xprt_list_lock);
109 EXPORT_SYMBOL_GPL(xprt_register_transport);
112 * xprt_unregister_transport - unregister a transport implementation
113 * @transport: transport to unregister
116 * 0: transport successfully unregistered
117 * -ENOENT: transport never registered
119 int xprt_unregister_transport(struct xprt_class *transport)
121 struct xprt_class *t;
125 spin_lock(&xprt_list_lock);
126 list_for_each_entry(t, &xprt_list, list) {
127 if (t == transport) {
129 "RPC: Unregistered %s transport module.\n",
131 list_del_init(&transport->list);
138 spin_unlock(&xprt_list_lock);
141 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
144 * xprt_load_transport - load a transport implementation
145 * @transport_name: transport to load
148 * 0: transport successfully loaded
149 * -ENOENT: transport module not available
151 int xprt_load_transport(const char *transport_name)
153 struct xprt_class *t;
157 spin_lock(&xprt_list_lock);
158 list_for_each_entry(t, &xprt_list, list) {
159 if (strcmp(t->name, transport_name) == 0) {
160 spin_unlock(&xprt_list_lock);
164 spin_unlock(&xprt_list_lock);
165 result = request_module("xprt%s", transport_name);
169 EXPORT_SYMBOL_GPL(xprt_load_transport);
171 static void xprt_clear_locked(struct rpc_xprt *xprt)
173 xprt->snd_task = NULL;
174 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
175 smp_mb__before_atomic();
176 clear_bit(XPRT_LOCKED, &xprt->state);
177 smp_mb__after_atomic();
179 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
183 * xprt_reserve_xprt - serialize write access to transports
184 * @task: task that is requesting access to the transport
185 * @xprt: pointer to the target transport
187 * This prevents mixing the payload of separate requests, and prevents
188 * transport connects from colliding with writes. No congestion control
191 int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
193 struct rpc_rqst *req = task->tk_rqstp;
195 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
196 if (task == xprt->snd_task)
200 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
202 xprt->snd_task = task;
207 xprt_clear_locked(xprt);
209 dprintk("RPC: %5u failed to lock transport %p\n",
211 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
212 task->tk_status = -EAGAIN;
213 rpc_sleep_on(&xprt->sending, task, NULL);
216 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
219 xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
221 return test_bit(XPRT_CWND_WAIT, &xprt->state);
225 xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
227 if (!list_empty(&xprt->xmit_queue)) {
228 /* Peek at head of queue to see if it can make progress */
229 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
233 set_bit(XPRT_CWND_WAIT, &xprt->state);
237 xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
239 if (!RPCXPRT_CONGESTED(xprt))
240 clear_bit(XPRT_CWND_WAIT, &xprt->state);
244 * xprt_reserve_xprt_cong - serialize write access to transports
245 * @task: task that is requesting access to the transport
247 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
248 * integrated into the decision of whether a request is allowed to be
249 * woken up and given access to the transport.
250 * Note that the lock is only granted if we know there are free slots.
252 int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
254 struct rpc_rqst *req = task->tk_rqstp;
256 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
257 if (task == xprt->snd_task)
262 xprt->snd_task = task;
265 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
267 if (!xprt_need_congestion_window_wait(xprt)) {
268 xprt->snd_task = task;
272 xprt_clear_locked(xprt);
274 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
275 task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
276 task->tk_status = -EAGAIN;
277 rpc_sleep_on(&xprt->sending, task, NULL);
280 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
282 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
286 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
288 spin_lock_bh(&xprt->transport_lock);
289 retval = xprt->ops->reserve_xprt(xprt, task);
290 spin_unlock_bh(&xprt->transport_lock);
294 static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
296 struct rpc_xprt *xprt = data;
298 xprt->snd_task = task;
302 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
304 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
306 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
308 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
309 __xprt_lock_write_func, xprt))
312 xprt_clear_locked(xprt);
315 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
317 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
319 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
321 if (xprt_need_congestion_window_wait(xprt))
323 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
324 __xprt_lock_write_func, xprt))
327 xprt_clear_locked(xprt);
331 * xprt_release_xprt - allow other requests to use a transport
332 * @xprt: transport with other tasks potentially waiting
333 * @task: task that is releasing access to the transport
335 * Note that "task" can be NULL. No congestion control is provided.
337 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
339 if (xprt->snd_task == task) {
340 xprt_clear_locked(xprt);
341 __xprt_lock_write_next(xprt);
344 EXPORT_SYMBOL_GPL(xprt_release_xprt);
347 * xprt_release_xprt_cong - allow other requests to use a transport
348 * @xprt: transport with other tasks potentially waiting
349 * @task: task that is releasing access to the transport
351 * Note that "task" can be NULL. Another task is awoken to use the
352 * transport if the transport's congestion window allows it.
354 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
356 if (xprt->snd_task == task) {
357 xprt_clear_locked(xprt);
358 __xprt_lock_write_next_cong(xprt);
361 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
363 static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
365 if (xprt->snd_task != task)
367 spin_lock_bh(&xprt->transport_lock);
368 xprt->ops->release_xprt(xprt, task);
369 spin_unlock_bh(&xprt->transport_lock);
373 * Van Jacobson congestion avoidance. Check if the congestion window
374 * overflowed. Put the task to sleep if this is the case.
377 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
381 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
382 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
383 if (RPCXPRT_CONGESTED(xprt)) {
384 xprt_set_congestion_window_wait(xprt);
388 xprt->cong += RPC_CWNDSCALE;
393 * Adjust the congestion window, and wake up the next task
394 * that has been sleeping due to congestion
397 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
402 xprt->cong -= RPC_CWNDSCALE;
403 xprt_test_and_clear_congestion_window_wait(xprt);
404 __xprt_lock_write_next_cong(xprt);
408 * xprt_request_get_cong - Request congestion control credits
409 * @xprt: pointer to transport
410 * @req: pointer to RPC request
412 * Useful for transports that require congestion control.
415 xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
421 spin_lock_bh(&xprt->transport_lock);
422 ret = __xprt_get_cong(xprt, req) != 0;
423 spin_unlock_bh(&xprt->transport_lock);
426 EXPORT_SYMBOL_GPL(xprt_request_get_cong);
429 * xprt_release_rqst_cong - housekeeping when request is complete
430 * @task: RPC request that recently completed
432 * Useful for transports that require congestion control.
434 void xprt_release_rqst_cong(struct rpc_task *task)
436 struct rpc_rqst *req = task->tk_rqstp;
438 __xprt_put_cong(req->rq_xprt, req);
440 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
443 * Clear the congestion window wait flag and wake up the next
444 * entry on xprt->sending
447 xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
449 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
450 spin_lock_bh(&xprt->transport_lock);
451 __xprt_lock_write_next_cong(xprt);
452 spin_unlock_bh(&xprt->transport_lock);
457 * xprt_adjust_cwnd - adjust transport congestion window
458 * @xprt: pointer to xprt
459 * @task: recently completed RPC request used to adjust window
460 * @result: result code of completed RPC request
462 * The transport code maintains an estimate on the maximum number of out-
463 * standing RPC requests, using a smoothed version of the congestion
464 * avoidance implemented in 44BSD. This is basically the Van Jacobson
465 * congestion algorithm: If a retransmit occurs, the congestion window is
466 * halved; otherwise, it is incremented by 1/cwnd when
468 * - a reply is received and
469 * - a full number of requests are outstanding and
470 * - the congestion window hasn't been updated recently.
472 void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
474 struct rpc_rqst *req = task->tk_rqstp;
475 unsigned long cwnd = xprt->cwnd;
477 if (result >= 0 && cwnd <= xprt->cong) {
478 /* The (cwnd >> 1) term makes sure
479 * the result gets rounded properly. */
480 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
481 if (cwnd > RPC_MAXCWND(xprt))
482 cwnd = RPC_MAXCWND(xprt);
483 __xprt_lock_write_next_cong(xprt);
484 } else if (result == -ETIMEDOUT) {
486 if (cwnd < RPC_CWNDSCALE)
487 cwnd = RPC_CWNDSCALE;
489 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
490 xprt->cong, xprt->cwnd, cwnd);
492 __xprt_put_cong(xprt, req);
494 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
497 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
498 * @xprt: transport with waiting tasks
499 * @status: result code to plant in each task before waking it
502 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
505 rpc_wake_up_status(&xprt->pending, status);
507 rpc_wake_up(&xprt->pending);
509 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
512 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
515 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
516 * we don't in general want to force a socket disconnection due to
517 * an incomplete RPC call transmission.
519 void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
521 set_bit(XPRT_WRITE_SPACE, &xprt->state);
523 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
526 xprt_clear_write_space_locked(struct rpc_xprt *xprt)
528 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
529 __xprt_lock_write_next(xprt);
530 dprintk("RPC: write space: waking waiting task on "
538 * xprt_write_space - wake the task waiting for transport output buffer space
539 * @xprt: transport with waiting tasks
541 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
543 bool xprt_write_space(struct rpc_xprt *xprt)
547 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
549 spin_lock_bh(&xprt->transport_lock);
550 ret = xprt_clear_write_space_locked(xprt);
551 spin_unlock_bh(&xprt->transport_lock);
554 EXPORT_SYMBOL_GPL(xprt_write_space);
557 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
558 * @task: task whose timeout is to be set
560 * Set a request's retransmit timeout based on the transport's
561 * default timeout parameters. Used by transports that don't adjust
562 * the retransmit timeout based on round-trip time estimation.
564 void xprt_set_retrans_timeout_def(struct rpc_task *task)
566 task->tk_timeout = task->tk_rqstp->rq_timeout;
568 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
571 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
572 * @task: task whose timeout is to be set
574 * Set a request's retransmit timeout using the RTT estimator.
576 void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
578 int timer = task->tk_msg.rpc_proc->p_timer;
579 struct rpc_clnt *clnt = task->tk_client;
580 struct rpc_rtt *rtt = clnt->cl_rtt;
581 struct rpc_rqst *req = task->tk_rqstp;
582 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
584 task->tk_timeout = rpc_calc_rto(rtt, timer);
585 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
586 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
587 task->tk_timeout = max_timeout;
589 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
591 static void xprt_reset_majortimeo(struct rpc_rqst *req)
593 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
595 req->rq_majortimeo = req->rq_timeout;
596 if (to->to_exponential)
597 req->rq_majortimeo <<= to->to_retries;
599 req->rq_majortimeo += to->to_increment * to->to_retries;
600 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
601 req->rq_majortimeo = to->to_maxval;
602 req->rq_majortimeo += jiffies;
606 * xprt_adjust_timeout - adjust timeout values for next retransmit
607 * @req: RPC request containing parameters to use for the adjustment
610 int xprt_adjust_timeout(struct rpc_rqst *req)
612 struct rpc_xprt *xprt = req->rq_xprt;
613 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
616 if (time_before(jiffies, req->rq_majortimeo)) {
617 if (to->to_exponential)
618 req->rq_timeout <<= 1;
620 req->rq_timeout += to->to_increment;
621 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
622 req->rq_timeout = to->to_maxval;
625 req->rq_timeout = to->to_initval;
627 xprt_reset_majortimeo(req);
628 /* Reset the RTT counters == "slow start" */
629 spin_lock_bh(&xprt->transport_lock);
630 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
631 spin_unlock_bh(&xprt->transport_lock);
635 if (req->rq_timeout == 0) {
636 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
637 req->rq_timeout = 5 * HZ;
642 static void xprt_autoclose(struct work_struct *work)
644 struct rpc_xprt *xprt =
645 container_of(work, struct rpc_xprt, task_cleanup);
647 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
648 xprt->ops->close(xprt);
649 xprt_release_write(xprt, NULL);
650 wake_up_bit(&xprt->state, XPRT_LOCKED);
654 * xprt_disconnect_done - mark a transport as disconnected
655 * @xprt: transport to flag for disconnect
658 void xprt_disconnect_done(struct rpc_xprt *xprt)
660 dprintk("RPC: disconnected transport %p\n", xprt);
661 spin_lock_bh(&xprt->transport_lock);
662 xprt_clear_connected(xprt);
663 xprt_clear_write_space_locked(xprt);
664 xprt_wake_pending_tasks(xprt, -EAGAIN);
665 spin_unlock_bh(&xprt->transport_lock);
667 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
670 * xprt_force_disconnect - force a transport to disconnect
671 * @xprt: transport to disconnect
674 void xprt_force_disconnect(struct rpc_xprt *xprt)
676 /* Don't race with the test_bit() in xprt_clear_locked() */
677 spin_lock_bh(&xprt->transport_lock);
678 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
679 /* Try to schedule an autoclose RPC call */
680 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
681 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
682 else if (xprt->snd_task)
683 rpc_wake_up_queued_task_set_status(&xprt->pending,
684 xprt->snd_task, -ENOTCONN);
685 spin_unlock_bh(&xprt->transport_lock);
687 EXPORT_SYMBOL_GPL(xprt_force_disconnect);
690 xprt_connect_cookie(struct rpc_xprt *xprt)
692 return READ_ONCE(xprt->connect_cookie);
696 xprt_request_retransmit_after_disconnect(struct rpc_task *task)
698 struct rpc_rqst *req = task->tk_rqstp;
699 struct rpc_xprt *xprt = req->rq_xprt;
701 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
702 !xprt_connected(xprt);
706 * xprt_conditional_disconnect - force a transport to disconnect
707 * @xprt: transport to disconnect
708 * @cookie: 'connection cookie'
710 * This attempts to break the connection if and only if 'cookie' matches
711 * the current transport 'connection cookie'. It ensures that we don't
712 * try to break the connection more than once when we need to retransmit
713 * a batch of RPC requests.
716 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
718 /* Don't race with the test_bit() in xprt_clear_locked() */
719 spin_lock_bh(&xprt->transport_lock);
720 if (cookie != xprt->connect_cookie)
722 if (test_bit(XPRT_CLOSING, &xprt->state))
724 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
725 /* Try to schedule an autoclose RPC call */
726 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
727 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
728 xprt_wake_pending_tasks(xprt, -EAGAIN);
730 spin_unlock_bh(&xprt->transport_lock);
734 xprt_has_timer(const struct rpc_xprt *xprt)
736 return xprt->idle_timeout != 0;
740 xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
741 __must_hold(&xprt->transport_lock)
743 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
744 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
748 xprt_init_autodisconnect(struct timer_list *t)
750 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
752 spin_lock(&xprt->transport_lock);
753 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
755 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
756 xprt->last_used = jiffies;
757 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
759 spin_unlock(&xprt->transport_lock);
760 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
763 spin_unlock(&xprt->transport_lock);
766 bool xprt_lock_connect(struct rpc_xprt *xprt,
767 struct rpc_task *task,
772 spin_lock_bh(&xprt->transport_lock);
773 if (!test_bit(XPRT_LOCKED, &xprt->state))
775 if (xprt->snd_task != task)
777 xprt->snd_task = cookie;
780 spin_unlock_bh(&xprt->transport_lock);
784 void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
786 spin_lock_bh(&xprt->transport_lock);
787 if (xprt->snd_task != cookie)
789 if (!test_bit(XPRT_LOCKED, &xprt->state))
791 xprt->snd_task =NULL;
792 xprt->ops->release_xprt(xprt, NULL);
793 xprt_schedule_autodisconnect(xprt);
795 spin_unlock_bh(&xprt->transport_lock);
796 wake_up_bit(&xprt->state, XPRT_LOCKED);
800 * xprt_connect - schedule a transport connect operation
801 * @task: RPC task that is requesting the connect
804 void xprt_connect(struct rpc_task *task)
806 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
808 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
809 xprt, (xprt_connected(xprt) ? "is" : "is not"));
811 if (!xprt_bound(xprt)) {
812 task->tk_status = -EAGAIN;
815 if (!xprt_lock_write(xprt, task))
818 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
819 xprt->ops->close(xprt);
821 if (!xprt_connected(xprt)) {
822 task->tk_timeout = task->tk_rqstp->rq_timeout;
823 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
824 rpc_sleep_on(&xprt->pending, task, NULL);
826 if (test_bit(XPRT_CLOSING, &xprt->state))
828 if (xprt_test_and_set_connecting(xprt))
831 if (!xprt_connected(xprt)) {
832 xprt->stat.connect_start = jiffies;
833 xprt->ops->connect(xprt, task);
835 xprt_clear_connecting(xprt);
837 rpc_wake_up_queued_task(&xprt->pending, task);
840 xprt_release_write(xprt, task);
843 enum xprt_xid_rb_cmp {
848 static enum xprt_xid_rb_cmp
849 xprt_xid_cmp(__be32 xid1, __be32 xid2)
853 if ((__force u32)xid1 < (__force u32)xid2)
858 static struct rpc_rqst *
859 xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
861 struct rb_node *n = xprt->recv_queue.rb_node;
862 struct rpc_rqst *req;
865 req = rb_entry(n, struct rpc_rqst, rq_recv);
866 switch (xprt_xid_cmp(xid, req->rq_xid)) {
881 xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
883 struct rb_node **p = &xprt->recv_queue.rb_node;
884 struct rb_node *n = NULL;
885 struct rpc_rqst *req;
889 req = rb_entry(n, struct rpc_rqst, rq_recv);
890 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
898 WARN_ON_ONCE(new != req);
902 rb_link_node(&new->rq_recv, n, p);
903 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
907 xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
909 rb_erase(&req->rq_recv, &xprt->recv_queue);
913 * xprt_lookup_rqst - find an RPC request corresponding to an XID
914 * @xprt: transport on which the original request was transmitted
915 * @xid: RPC XID of incoming reply
917 * Caller holds xprt->queue_lock.
919 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
921 struct rpc_rqst *entry;
923 entry = xprt_request_rb_find(xprt, xid);
925 trace_xprt_lookup_rqst(xprt, xid, 0);
926 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
930 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
932 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
933 xprt->stat.bad_xids++;
936 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
939 xprt_is_pinned_rqst(struct rpc_rqst *req)
941 return atomic_read(&req->rq_pin) != 0;
945 * xprt_pin_rqst - Pin a request on the transport receive list
946 * @req: Request to pin
948 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
949 * so should be holding the xprt receive lock.
951 void xprt_pin_rqst(struct rpc_rqst *req)
953 atomic_inc(&req->rq_pin);
955 EXPORT_SYMBOL_GPL(xprt_pin_rqst);
958 * xprt_unpin_rqst - Unpin a request on the transport receive list
959 * @req: Request to pin
961 * Caller should be holding the xprt receive lock.
963 void xprt_unpin_rqst(struct rpc_rqst *req)
965 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
966 atomic_dec(&req->rq_pin);
969 if (atomic_dec_and_test(&req->rq_pin))
970 wake_up_var(&req->rq_pin);
972 EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
974 static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
976 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
980 xprt_request_data_received(struct rpc_task *task)
982 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
983 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
987 xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
989 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
990 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
994 * xprt_request_enqueue_receive - Add an request to the receive queue
999 xprt_request_enqueue_receive(struct rpc_task *task)
1001 struct rpc_rqst *req = task->tk_rqstp;
1002 struct rpc_xprt *xprt = req->rq_xprt;
1004 if (!xprt_request_need_enqueue_receive(task, req))
1006 spin_lock(&xprt->queue_lock);
1008 /* Update the softirq receive buffer */
1009 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1010 sizeof(req->rq_private_buf));
1012 /* Add request to the receive list */
1013 xprt_request_rb_insert(xprt, req);
1014 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1015 spin_unlock(&xprt->queue_lock);
1017 xprt_reset_majortimeo(req);
1018 /* Turn off autodisconnect */
1019 del_singleshot_timer_sync(&xprt->timer);
1023 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1026 * Caller must hold xprt->queue_lock.
1029 xprt_request_dequeue_receive_locked(struct rpc_task *task)
1031 struct rpc_rqst *req = task->tk_rqstp;
1033 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1034 xprt_request_rb_remove(req->rq_xprt, req);
1038 * xprt_update_rtt - Update RPC RTT statistics
1039 * @task: RPC request that recently completed
1041 * Caller holds xprt->queue_lock.
1043 void xprt_update_rtt(struct rpc_task *task)
1045 struct rpc_rqst *req = task->tk_rqstp;
1046 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
1047 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1048 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1051 if (req->rq_ntrans == 1)
1052 rpc_update_rtt(rtt, timer, m);
1053 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1056 EXPORT_SYMBOL_GPL(xprt_update_rtt);
1059 * xprt_complete_rqst - called when reply processing is complete
1060 * @task: RPC request that recently completed
1061 * @copied: actual number of bytes received from the transport
1063 * Caller holds xprt->queue_lock.
1065 void xprt_complete_rqst(struct rpc_task *task, int copied)
1067 struct rpc_rqst *req = task->tk_rqstp;
1068 struct rpc_xprt *xprt = req->rq_xprt;
1070 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1071 task->tk_pid, ntohl(req->rq_xid), copied);
1072 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
1076 req->rq_private_buf.len = copied;
1077 /* Ensure all writes are done before we update */
1078 /* req->rq_reply_bytes_recvd */
1080 req->rq_reply_bytes_recvd = copied;
1081 xprt_request_dequeue_receive_locked(task);
1082 rpc_wake_up_queued_task(&xprt->pending, task);
1084 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1086 static void xprt_timer(struct rpc_task *task)
1088 struct rpc_rqst *req = task->tk_rqstp;
1089 struct rpc_xprt *xprt = req->rq_xprt;
1091 if (task->tk_status != -ETIMEDOUT)
1094 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1095 if (!req->rq_reply_bytes_recvd) {
1096 if (xprt->ops->timer)
1097 xprt->ops->timer(xprt, task);
1099 task->tk_status = 0;
1103 * xprt_request_wait_receive - wait for the reply to an RPC request
1104 * @task: RPC task about to send a request
1107 void xprt_request_wait_receive(struct rpc_task *task)
1109 struct rpc_rqst *req = task->tk_rqstp;
1110 struct rpc_xprt *xprt = req->rq_xprt;
1112 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1115 * Sleep on the pending queue if we're expecting a reply.
1116 * The spinlock ensures atomicity between the test of
1117 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1119 spin_lock(&xprt->queue_lock);
1120 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1121 xprt->ops->set_retrans_timeout(task);
1122 rpc_sleep_on(&xprt->pending, task, xprt_timer);
1124 * Send an extra queue wakeup call if the
1125 * connection was dropped in case the call to
1126 * rpc_sleep_on() raced.
1128 if (xprt_request_retransmit_after_disconnect(task))
1129 rpc_wake_up_queued_task_set_status(&xprt->pending,
1132 spin_unlock(&xprt->queue_lock);
1136 xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1138 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1142 * xprt_request_enqueue_transmit - queue a task for transmission
1143 * @task: pointer to rpc_task
1145 * Add a task to the transmission queue.
1148 xprt_request_enqueue_transmit(struct rpc_task *task)
1150 struct rpc_rqst *pos, *req = task->tk_rqstp;
1151 struct rpc_xprt *xprt = req->rq_xprt;
1153 if (xprt_request_need_enqueue_transmit(task, req)) {
1154 req->rq_bytes_sent = 0;
1155 spin_lock(&xprt->queue_lock);
1157 * Requests that carry congestion control credits are added
1158 * to the head of the list to avoid starvation issues.
1161 xprt_clear_congestion_window_wait(xprt);
1162 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1165 /* Note: req is added _before_ pos */
1166 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1167 INIT_LIST_HEAD(&req->rq_xmit2);
1170 } else if (RPC_IS_SWAPPER(task)) {
1171 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1172 if (pos->rq_cong || pos->rq_bytes_sent)
1174 if (RPC_IS_SWAPPER(pos->rq_task))
1176 /* Note: req is added _before_ pos */
1177 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1178 INIT_LIST_HEAD(&req->rq_xmit2);
1181 } else if (!req->rq_seqno) {
1182 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1183 if (pos->rq_task->tk_owner != task->tk_owner)
1185 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1186 INIT_LIST_HEAD(&req->rq_xmit);
1190 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1191 INIT_LIST_HEAD(&req->rq_xmit2);
1193 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1194 spin_unlock(&xprt->queue_lock);
1199 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1200 * @task: pointer to rpc_task
1202 * Remove a task from the transmission queue
1203 * Caller must hold xprt->queue_lock
1206 xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1208 struct rpc_rqst *req = task->tk_rqstp;
1210 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1212 if (!list_empty(&req->rq_xmit)) {
1213 list_del(&req->rq_xmit);
1214 if (!list_empty(&req->rq_xmit2)) {
1215 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1216 struct rpc_rqst, rq_xmit2);
1217 list_del(&req->rq_xmit2);
1218 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1221 list_del(&req->rq_xmit2);
1225 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1226 * @task: pointer to rpc_task
1228 * Remove a task from the transmission queue
1231 xprt_request_dequeue_transmit(struct rpc_task *task)
1233 struct rpc_rqst *req = task->tk_rqstp;
1234 struct rpc_xprt *xprt = req->rq_xprt;
1236 spin_lock(&xprt->queue_lock);
1237 xprt_request_dequeue_transmit_locked(task);
1238 spin_unlock(&xprt->queue_lock);
1242 * xprt_request_prepare - prepare an encoded request for transport
1243 * @req: pointer to rpc_rqst
1245 * Calls into the transport layer to do whatever is needed to prepare
1246 * the request for transmission or receive.
1249 xprt_request_prepare(struct rpc_rqst *req)
1251 struct rpc_xprt *xprt = req->rq_xprt;
1253 if (xprt->ops->prepare_request)
1254 xprt->ops->prepare_request(req);
1258 * xprt_request_need_retransmit - Test if a task needs retransmission
1259 * @task: pointer to rpc_task
1261 * Test for whether a connection breakage requires the task to retransmit
1264 xprt_request_need_retransmit(struct rpc_task *task)
1266 return xprt_request_retransmit_after_disconnect(task);
1270 * xprt_prepare_transmit - reserve the transport before sending a request
1271 * @task: RPC task about to send a request
1274 bool xprt_prepare_transmit(struct rpc_task *task)
1276 struct rpc_rqst *req = task->tk_rqstp;
1277 struct rpc_xprt *xprt = req->rq_xprt;
1279 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1281 if (!xprt_lock_write(xprt, task)) {
1282 /* Race breaker: someone may have transmitted us */
1283 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1284 rpc_wake_up_queued_task_set_status(&xprt->sending,
1292 void xprt_end_transmit(struct rpc_task *task)
1294 xprt_release_write(task->tk_rqstp->rq_xprt, task);
1298 * xprt_request_transmit - send an RPC request on a transport
1299 * @req: pointer to request to transmit
1300 * @snd_task: RPC task that owns the transport lock
1302 * This performs the transmission of a single request.
1303 * Note that if the request is not the same as snd_task, then it
1304 * does need to be pinned.
1305 * Returns '0' on success.
1308 xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
1310 struct rpc_xprt *xprt = req->rq_xprt;
1311 struct rpc_task *task = req->rq_task;
1312 unsigned int connect_cookie;
1313 int is_retrans = RPC_WAS_SENT(task);
1316 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1318 if (!req->rq_bytes_sent) {
1319 if (xprt_request_data_received(task)) {
1323 /* Verify that our message lies in the RPCSEC_GSS window */
1324 if (rpcauth_xmit_need_reencode(task)) {
1331 * Update req->rq_ntrans before transmitting to avoid races with
1332 * xprt_update_rtt(), which needs to know that it is recording a
1333 * reply to the first transmission.
1337 connect_cookie = xprt->connect_cookie;
1338 status = xprt->ops->send_request(req);
1339 trace_xprt_transmit(xprt, req->rq_xid, status);
1346 task->tk_client->cl_stats->rpcretrans++;
1348 xprt_inject_disconnect(xprt);
1350 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
1351 task->tk_flags |= RPC_TASK_SENT;
1352 spin_lock_bh(&xprt->transport_lock);
1355 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1356 xprt->stat.bklog_u += xprt->backlog.qlen;
1357 xprt->stat.sending_u += xprt->sending.qlen;
1358 xprt->stat.pending_u += xprt->pending.qlen;
1359 spin_unlock_bh(&xprt->transport_lock);
1361 req->rq_connect_cookie = connect_cookie;
1363 xprt_request_dequeue_transmit(task);
1364 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1369 * xprt_transmit - send an RPC request on a transport
1370 * @task: controlling RPC task
1372 * Attempts to drain the transmit queue. On exit, either the transport
1373 * signalled an error that needs to be handled before transmission can
1374 * resume, or @task finished transmitting, and detected that it already
1378 xprt_transmit(struct rpc_task *task)
1380 struct rpc_rqst *next, *req = task->tk_rqstp;
1381 struct rpc_xprt *xprt = req->rq_xprt;
1384 spin_lock(&xprt->queue_lock);
1385 while (!list_empty(&xprt->xmit_queue)) {
1386 next = list_first_entry(&xprt->xmit_queue,
1387 struct rpc_rqst, rq_xmit);
1388 xprt_pin_rqst(next);
1389 spin_unlock(&xprt->queue_lock);
1390 status = xprt_request_transmit(next, task);
1391 if (status == -EBADMSG && next != req)
1394 spin_lock(&xprt->queue_lock);
1395 xprt_unpin_rqst(next);
1397 if (!xprt_request_data_received(task) ||
1398 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1400 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1401 task->tk_status = status;
1404 spin_unlock(&xprt->queue_lock);
1407 static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1409 set_bit(XPRT_CONGESTED, &xprt->state);
1410 rpc_sleep_on(&xprt->backlog, task, NULL);
1413 static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1415 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1416 clear_bit(XPRT_CONGESTED, &xprt->state);
1419 static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1423 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1425 spin_lock(&xprt->reserve_lock);
1426 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1427 rpc_sleep_on(&xprt->backlog, task, NULL);
1430 spin_unlock(&xprt->reserve_lock);
1435 static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1437 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1439 if (xprt->num_reqs >= xprt->max_reqs)
1442 spin_unlock(&xprt->reserve_lock);
1443 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1444 spin_lock(&xprt->reserve_lock);
1448 req = ERR_PTR(-ENOMEM);
1453 static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1455 if (xprt->num_reqs > xprt->min_reqs) {
1463 void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1465 struct rpc_rqst *req;
1467 spin_lock(&xprt->reserve_lock);
1468 if (!list_empty(&xprt->free)) {
1469 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1470 list_del(&req->rq_list);
1473 req = xprt_dynamic_alloc_slot(xprt);
1476 switch (PTR_ERR(req)) {
1478 dprintk("RPC: dynamic allocation of request slot "
1479 "failed! Retrying\n");
1480 task->tk_status = -ENOMEM;
1483 xprt_add_backlog(xprt, task);
1484 dprintk("RPC: waiting for request slot\n");
1487 task->tk_status = -EAGAIN;
1489 spin_unlock(&xprt->reserve_lock);
1492 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1494 spin_unlock(&xprt->reserve_lock);
1496 task->tk_status = 0;
1497 task->tk_rqstp = req;
1499 EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1501 void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1503 spin_lock(&xprt->reserve_lock);
1504 if (!xprt_dynamic_free_slot(xprt, req)) {
1505 memset(req, 0, sizeof(*req)); /* mark unused */
1506 list_add(&req->rq_list, &xprt->free);
1508 xprt_wake_up_backlog(xprt);
1509 spin_unlock(&xprt->reserve_lock);
1511 EXPORT_SYMBOL_GPL(xprt_free_slot);
1513 static void xprt_free_all_slots(struct rpc_xprt *xprt)
1515 struct rpc_rqst *req;
1516 while (!list_empty(&xprt->free)) {
1517 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1518 list_del(&req->rq_list);
1523 struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1524 unsigned int num_prealloc,
1525 unsigned int max_alloc)
1527 struct rpc_xprt *xprt;
1528 struct rpc_rqst *req;
1531 xprt = kzalloc(size, GFP_KERNEL);
1535 xprt_init(xprt, net);
1537 for (i = 0; i < num_prealloc; i++) {
1538 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1541 list_add(&req->rq_list, &xprt->free);
1543 if (max_alloc > num_prealloc)
1544 xprt->max_reqs = max_alloc;
1546 xprt->max_reqs = num_prealloc;
1547 xprt->min_reqs = num_prealloc;
1548 xprt->num_reqs = num_prealloc;
1557 EXPORT_SYMBOL_GPL(xprt_alloc);
1559 void xprt_free(struct rpc_xprt *xprt)
1561 put_net(xprt->xprt_net);
1562 xprt_free_all_slots(xprt);
1563 kfree_rcu(xprt, rcu);
1565 EXPORT_SYMBOL_GPL(xprt_free);
1568 xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1570 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1574 xprt_alloc_xid(struct rpc_xprt *xprt)
1578 spin_lock(&xprt->reserve_lock);
1579 xid = (__force __be32)xprt->xid++;
1580 spin_unlock(&xprt->reserve_lock);
1585 xprt_init_xid(struct rpc_xprt *xprt)
1587 xprt->xid = prandom_u32();
1591 xprt_request_init(struct rpc_task *task)
1593 struct rpc_xprt *xprt = task->tk_xprt;
1594 struct rpc_rqst *req = task->tk_rqstp;
1596 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1597 req->rq_task = task;
1598 req->rq_xprt = xprt;
1599 req->rq_buffer = NULL;
1600 req->rq_xid = xprt_alloc_xid(xprt);
1601 xprt_init_connect_cookie(req, xprt);
1602 req->rq_bytes_sent = 0;
1603 req->rq_snd_buf.len = 0;
1604 req->rq_snd_buf.buflen = 0;
1605 req->rq_rcv_buf.len = 0;
1606 req->rq_rcv_buf.buflen = 0;
1607 req->rq_snd_buf.bvec = NULL;
1608 req->rq_rcv_buf.bvec = NULL;
1609 req->rq_release_snd_buf = NULL;
1610 xprt_reset_majortimeo(req);
1611 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1612 req, ntohl(req->rq_xid));
1616 xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1618 xprt->ops->alloc_slot(xprt, task);
1619 if (task->tk_rqstp != NULL)
1620 xprt_request_init(task);
1624 * xprt_reserve - allocate an RPC request slot
1625 * @task: RPC task requesting a slot allocation
1627 * If the transport is marked as being congested, or if no more
1628 * slots are available, place the task on the transport's
1631 void xprt_reserve(struct rpc_task *task)
1633 struct rpc_xprt *xprt = task->tk_xprt;
1635 task->tk_status = 0;
1636 if (task->tk_rqstp != NULL)
1639 task->tk_timeout = 0;
1640 task->tk_status = -EAGAIN;
1641 if (!xprt_throttle_congested(xprt, task))
1642 xprt_do_reserve(xprt, task);
1646 * xprt_retry_reserve - allocate an RPC request slot
1647 * @task: RPC task requesting a slot allocation
1649 * If no more slots are available, place the task on the transport's
1651 * Note that the only difference with xprt_reserve is that we now
1652 * ignore the value of the XPRT_CONGESTED flag.
1654 void xprt_retry_reserve(struct rpc_task *task)
1656 struct rpc_xprt *xprt = task->tk_xprt;
1658 task->tk_status = 0;
1659 if (task->tk_rqstp != NULL)
1662 task->tk_timeout = 0;
1663 task->tk_status = -EAGAIN;
1664 xprt_do_reserve(xprt, task);
1668 xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1670 struct rpc_xprt *xprt = req->rq_xprt;
1672 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1673 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1674 xprt_is_pinned_rqst(req)) {
1675 spin_lock(&xprt->queue_lock);
1676 xprt_request_dequeue_transmit_locked(task);
1677 xprt_request_dequeue_receive_locked(task);
1678 while (xprt_is_pinned_rqst(req)) {
1679 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1680 spin_unlock(&xprt->queue_lock);
1681 xprt_wait_on_pinned_rqst(req);
1682 spin_lock(&xprt->queue_lock);
1683 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1685 spin_unlock(&xprt->queue_lock);
1690 * xprt_release - release an RPC request slot
1691 * @task: task which is finished with the slot
1694 void xprt_release(struct rpc_task *task)
1696 struct rpc_xprt *xprt;
1697 struct rpc_rqst *req = task->tk_rqstp;
1700 if (task->tk_client) {
1701 xprt = task->tk_xprt;
1702 xprt_release_write(xprt, task);
1707 xprt = req->rq_xprt;
1708 if (task->tk_ops->rpc_count_stats != NULL)
1709 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1710 else if (task->tk_client)
1711 rpc_count_iostats(task, task->tk_client->cl_metrics);
1712 xprt_request_dequeue_all(task, req);
1713 spin_lock_bh(&xprt->transport_lock);
1714 xprt->ops->release_xprt(xprt, task);
1715 if (xprt->ops->release_request)
1716 xprt->ops->release_request(task);
1717 xprt->last_used = jiffies;
1718 xprt_schedule_autodisconnect(xprt);
1719 spin_unlock_bh(&xprt->transport_lock);
1721 xprt->ops->buf_free(task);
1722 xprt_inject_disconnect(xprt);
1723 xdr_free_bvec(&req->rq_rcv_buf);
1724 if (req->rq_cred != NULL)
1725 put_rpccred(req->rq_cred);
1726 task->tk_rqstp = NULL;
1727 if (req->rq_release_snd_buf)
1728 req->rq_release_snd_buf(req);
1730 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1731 if (likely(!bc_prealloc(req)))
1732 xprt->ops->free_slot(xprt, req);
1734 xprt_free_bc_request(req);
1737 #ifdef CONFIG_SUNRPC_BACKCHANNEL
1739 xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1741 struct xdr_buf *xbufp = &req->rq_snd_buf;
1743 task->tk_rqstp = req;
1744 req->rq_task = task;
1745 xprt_init_connect_cookie(req, req->rq_xprt);
1747 * Set up the xdr_buf length.
1748 * This also indicates that the buffer is XDR encoded already.
1750 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1751 xbufp->tail[0].iov_len;
1752 req->rq_bytes_sent = 0;
1756 static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1758 kref_init(&xprt->kref);
1760 spin_lock_init(&xprt->transport_lock);
1761 spin_lock_init(&xprt->reserve_lock);
1762 spin_lock_init(&xprt->queue_lock);
1764 INIT_LIST_HEAD(&xprt->free);
1765 xprt->recv_queue = RB_ROOT;
1766 INIT_LIST_HEAD(&xprt->xmit_queue);
1767 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1768 spin_lock_init(&xprt->bc_pa_lock);
1769 INIT_LIST_HEAD(&xprt->bc_pa_list);
1770 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1771 INIT_LIST_HEAD(&xprt->xprt_switch);
1773 xprt->last_used = jiffies;
1774 xprt->cwnd = RPC_INITCWND;
1775 xprt->bind_index = 0;
1777 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1778 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1779 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1780 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1782 xprt_init_xid(xprt);
1784 xprt->xprt_net = get_net(net);
1788 * xprt_create_transport - create an RPC transport
1789 * @args: rpc transport creation arguments
1792 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1794 struct rpc_xprt *xprt;
1795 struct xprt_class *t;
1797 spin_lock(&xprt_list_lock);
1798 list_for_each_entry(t, &xprt_list, list) {
1799 if (t->ident == args->ident) {
1800 spin_unlock(&xprt_list_lock);
1804 spin_unlock(&xprt_list_lock);
1805 dprintk("RPC: transport (%d) not supported\n", args->ident);
1806 return ERR_PTR(-EIO);
1809 xprt = t->setup(args);
1811 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1815 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1816 xprt->idle_timeout = 0;
1817 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1818 if (xprt_has_timer(xprt))
1819 timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
1821 timer_setup(&xprt->timer, NULL, 0);
1823 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1825 return ERR_PTR(-EINVAL);
1827 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1828 if (xprt->servername == NULL) {
1830 return ERR_PTR(-ENOMEM);
1833 rpc_xprt_debugfs_register(xprt);
1835 dprintk("RPC: created transport %p with %u slots\n", xprt,
1841 static void xprt_destroy_cb(struct work_struct *work)
1843 struct rpc_xprt *xprt =
1844 container_of(work, struct rpc_xprt, task_cleanup);
1846 rpc_xprt_debugfs_unregister(xprt);
1847 rpc_destroy_wait_queue(&xprt->binding);
1848 rpc_destroy_wait_queue(&xprt->pending);
1849 rpc_destroy_wait_queue(&xprt->sending);
1850 rpc_destroy_wait_queue(&xprt->backlog);
1851 kfree(xprt->servername);
1853 * Tear down transport state and free the rpc_xprt
1855 xprt->ops->destroy(xprt);
1859 * xprt_destroy - destroy an RPC transport, killing off all requests.
1860 * @xprt: transport to destroy
1863 static void xprt_destroy(struct rpc_xprt *xprt)
1865 dprintk("RPC: destroying transport %p\n", xprt);
1868 * Exclude transport connect/disconnect handlers and autoclose
1870 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1872 del_timer_sync(&xprt->timer);
1875 * Destroy sockets etc from the system workqueue so they can
1876 * safely flush receive work running on rpciod.
1878 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1879 schedule_work(&xprt->task_cleanup);
1882 static void xprt_destroy_kref(struct kref *kref)
1884 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1888 * xprt_get - return a reference to an RPC transport.
1889 * @xprt: pointer to the transport
1892 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1894 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1898 EXPORT_SYMBOL_GPL(xprt_get);
1901 * xprt_put - release a reference to an RPC transport.
1902 * @xprt: pointer to the transport
1905 void xprt_put(struct rpc_xprt *xprt)
1908 kref_put(&xprt->kref, xprt_destroy_kref);
1910 EXPORT_SYMBOL_GPL(xprt_put);