1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 61da177e4SLinus Torvalds * and asynchronous calls. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * The interface works like this: 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 111da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 121da177e4SLinus Torvalds * (xprt_reserve). 131da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1455aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1555aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1655ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1755ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1855ae1aabSRicardo Labiaga * expired. 191da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 2055aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 211da177e4SLinus Torvalds * caller is woken up, and the timer removed. 221da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 231da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 241da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 251da177e4SLinus Torvalds * of -ETIMEDOUT. 261da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 271da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 281da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 291da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 301da177e4SLinus Torvalds * again. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 331da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 341da177e4SLinus Torvalds * tasks that rely on callbacks. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3755aa4f58SChuck Lever * 3855aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41a246b010SChuck Lever #include <linux/module.h> 42a246b010SChuck Lever 431da177e4SLinus Torvalds #include <linux/types.h> 44a246b010SChuck Lever #include <linux/interrupt.h> 451da177e4SLinus Torvalds #include <linux/workqueue.h> 46bf3fcf89SChuck Lever #include <linux/net.h> 47ff839970SChuck Lever #include <linux/ktime.h> 481da177e4SLinus Torvalds 49a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 5011c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 51c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 52fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 53a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 541da177e4SLinus Torvalds 553705ad64SJeff Layton #include <trace/events/sunrpc.h> 563705ad64SJeff Layton 5755ae1aabSRicardo Labiaga #include "sunrpc.h" 5855ae1aabSRicardo Labiaga 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * Local variables 611da177e4SLinus Torvalds */ 621da177e4SLinus Torvalds 63f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 641da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 651da177e4SLinus Torvalds #endif 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * Local functions 691da177e4SLinus Torvalds */ 7021de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 7137ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 724e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 731da177e4SLinus Torvalds 745ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7581c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7681c098afS\"Talpey, Thomas\ 779e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req) 789e910bffSTrond Myklebust { 799e910bffSTrond Myklebust unsigned long timeout = jiffies + req->rq_timeout; 809e910bffSTrond Myklebust 819e910bffSTrond Myklebust if (time_before(timeout, req->rq_majortimeo)) 829e910bffSTrond Myklebust return timeout; 839e910bffSTrond Myklebust return req->rq_majortimeo; 849e910bffSTrond Myklebust } 859e910bffSTrond Myklebust 8612a80469SChuck Lever /** 8781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 8881c098afS\"Talpey, Thomas\ * @transport: transport to register 8981c098afS\"Talpey, Thomas\ * 9081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9281c098afS\"Talpey, Thomas\ * 9381c098afS\"Talpey, Thomas\ * Returns: 9481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 9581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 9681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 9781c098afS\"Talpey, Thomas\ */ 9881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 9981c098afS\"Talpey, Thomas\ { 10081c098afS\"Talpey, Thomas\ struct xprt_class *t; 10181c098afS\"Talpey, Thomas\ int result; 10281c098afS\"Talpey, Thomas\ 10381c098afS\"Talpey, Thomas\ result = -EEXIST; 10481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 10581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 10681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1074fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 10881c098afS\"Talpey, Thomas\ goto out; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ 11181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11381c098afS\"Talpey, Thomas\ transport->name); 11481c098afS\"Talpey, Thomas\ result = 0; 11581c098afS\"Talpey, Thomas\ 11681c098afS\"Talpey, Thomas\ out: 11781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 11881c098afS\"Talpey, Thomas\ return result; 11981c098afS\"Talpey, Thomas\ } 12081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12181c098afS\"Talpey, Thomas\ 12281c098afS\"Talpey, Thomas\ /** 12381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 12465b6e42cSRandy Dunlap * @transport: transport to unregister 12581c098afS\"Talpey, Thomas\ * 12681c098afS\"Talpey, Thomas\ * Returns: 12781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 12881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 12981c098afS\"Talpey, Thomas\ */ 13081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13181c098afS\"Talpey, Thomas\ { 13281c098afS\"Talpey, Thomas\ struct xprt_class *t; 13381c098afS\"Talpey, Thomas\ int result; 13481c098afS\"Talpey, Thomas\ 13581c098afS\"Talpey, Thomas\ result = 0; 13681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 13781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 13881c098afS\"Talpey, Thomas\ if (t == transport) { 13981c098afS\"Talpey, Thomas\ printk(KERN_INFO 14081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14181c098afS\"Talpey, Thomas\ transport->name); 14281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14381c098afS\"Talpey, Thomas\ goto out; 14481c098afS\"Talpey, Thomas\ } 14581c098afS\"Talpey, Thomas\ } 14681c098afS\"Talpey, Thomas\ result = -ENOENT; 14781c098afS\"Talpey, Thomas\ 14881c098afS\"Talpey, Thomas\ out: 14981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15081c098afS\"Talpey, Thomas\ return result; 15181c098afS\"Talpey, Thomas\ } 15281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15381c098afS\"Talpey, Thomas\ 15481c098afS\"Talpey, Thomas\ /** 155441e3e24STom Talpey * xprt_load_transport - load a transport implementation 156441e3e24STom Talpey * @transport_name: transport to load 157441e3e24STom Talpey * 158441e3e24STom Talpey * Returns: 159441e3e24STom Talpey * 0: transport successfully loaded 160441e3e24STom Talpey * -ENOENT: transport module not available 161441e3e24STom Talpey */ 162441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 163441e3e24STom Talpey { 164441e3e24STom Talpey struct xprt_class *t; 165441e3e24STom Talpey int result; 166441e3e24STom Talpey 167441e3e24STom Talpey result = 0; 168441e3e24STom Talpey spin_lock(&xprt_list_lock); 169441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 170441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 171441e3e24STom Talpey spin_unlock(&xprt_list_lock); 172441e3e24STom Talpey goto out; 173441e3e24STom Talpey } 174441e3e24STom Talpey } 175441e3e24STom Talpey spin_unlock(&xprt_list_lock); 176ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 177441e3e24STom Talpey out: 178441e3e24STom Talpey return result; 179441e3e24STom Talpey } 180441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 181441e3e24STom Talpey 182c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 183c544577dSTrond Myklebust { 184c544577dSTrond Myklebust xprt->snd_task = NULL; 185c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 186c544577dSTrond Myklebust smp_mb__before_atomic(); 187c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 188c544577dSTrond Myklebust smp_mb__after_atomic(); 189c544577dSTrond Myklebust } else 190c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 191c544577dSTrond Myklebust } 192c544577dSTrond Myklebust 193441e3e24STom Talpey /** 19412a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 19512a80469SChuck Lever * @task: task that is requesting access to the transport 196177c27bfSRandy Dunlap * @xprt: pointer to the target transport 19712a80469SChuck Lever * 19812a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 19912a80469SChuck Lever * transport connects from colliding with writes. No congestion control 20012a80469SChuck Lever * is provided. 2011da177e4SLinus Torvalds */ 20243cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 2031da177e4SLinus Torvalds { 20412a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 20512a80469SChuck Lever 20612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 20712a80469SChuck Lever if (task == xprt->snd_task) 208bf7ca707SChuck Lever goto out_locked; 20912a80469SChuck Lever goto out_sleep; 21012a80469SChuck Lever } 211c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 212c544577dSTrond Myklebust goto out_unlock; 21312a80469SChuck Lever xprt->snd_task = task; 2144d4a76f3Sj223yang@asset.uwaterloo.ca 215bf7ca707SChuck Lever out_locked: 216bf7ca707SChuck Lever trace_xprt_reserve_xprt(xprt, task); 21712a80469SChuck Lever return 1; 21812a80469SChuck Lever 219c544577dSTrond Myklebust out_unlock: 220c544577dSTrond Myklebust xprt_clear_locked(xprt); 22112a80469SChuck Lever out_sleep: 22212a80469SChuck Lever task->tk_status = -EAGAIN; 2236b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2246b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2259e910bffSTrond Myklebust xprt_request_timeout(req)); 2266b2e6856STrond Myklebust else 22779c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 22812a80469SChuck Lever return 0; 22912a80469SChuck Lever } 23012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 23112a80469SChuck Lever 23275891f50STrond Myklebust static bool 23375891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 23475891f50STrond Myklebust { 23575891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 23675891f50STrond Myklebust } 23775891f50STrond Myklebust 23875891f50STrond Myklebust static void 23975891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 24075891f50STrond Myklebust { 24175891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 24275891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 24375891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 24475891f50STrond Myklebust rq_xmit)->rq_cong) 24575891f50STrond Myklebust return; 24675891f50STrond Myklebust } 24775891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 24875891f50STrond Myklebust } 24975891f50STrond Myklebust 25075891f50STrond Myklebust static void 25175891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 25275891f50STrond Myklebust { 25375891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 25475891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 25575891f50STrond Myklebust } 25675891f50STrond Myklebust 25712a80469SChuck Lever /* 25812a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 25912a80469SChuck Lever * @task: task that is requesting access to the transport 26012a80469SChuck Lever * 26112a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 26212a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 26312a80469SChuck Lever * woken up and given access to the transport. 26475891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 26512a80469SChuck Lever */ 26643cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 26712a80469SChuck Lever { 2681da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 2691da177e4SLinus Torvalds 2702226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2711da177e4SLinus Torvalds if (task == xprt->snd_task) 272bf7ca707SChuck Lever goto out_locked; 2731da177e4SLinus Torvalds goto out_sleep; 2741da177e4SLinus Torvalds } 27543cedbf0STrond Myklebust if (req == NULL) { 27643cedbf0STrond Myklebust xprt->snd_task = task; 277bf7ca707SChuck Lever goto out_locked; 27843cedbf0STrond Myklebust } 279c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 280c544577dSTrond Myklebust goto out_unlock; 28175891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 2821da177e4SLinus Torvalds xprt->snd_task = task; 283bf7ca707SChuck Lever goto out_locked; 2841da177e4SLinus Torvalds } 285c544577dSTrond Myklebust out_unlock: 286632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2871da177e4SLinus Torvalds out_sleep: 2881da177e4SLinus Torvalds task->tk_status = -EAGAIN; 2896b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2906b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2919e910bffSTrond Myklebust xprt_request_timeout(req)); 2926b2e6856STrond Myklebust else 29379c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 2941da177e4SLinus Torvalds return 0; 295bf7ca707SChuck Lever out_locked: 296bf7ca707SChuck Lever trace_xprt_reserve_cong(xprt, task); 297bf7ca707SChuck Lever return 1; 2981da177e4SLinus Torvalds } 29912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 3001da177e4SLinus Torvalds 30112a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds int retval; 3041da177e4SLinus Torvalds 305bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 306bd79bc57STrond Myklebust return 1; 307b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 30843cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 309b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3101da177e4SLinus Torvalds return retval; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 3141da177e4SLinus Torvalds { 315961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 31649e9a890SChuck Lever 31749e9a890SChuck Lever xprt->snd_task = task; 318961a828dSTrond Myklebust return true; 319961a828dSTrond Myklebust } 320961a828dSTrond Myklebust 321961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 322961a828dSTrond Myklebust { 323961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 32449e9a890SChuck Lever return; 325c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 326c544577dSTrond Myklebust goto out_unlock; 327f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 328f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 329961a828dSTrond Myklebust return; 330c544577dSTrond Myklebust out_unlock: 331632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 33249e9a890SChuck Lever } 33349e9a890SChuck Lever 334961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 335961a828dSTrond Myklebust { 336961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 337961a828dSTrond Myklebust return; 338c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 339c544577dSTrond Myklebust goto out_unlock; 34075891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 341961a828dSTrond Myklebust goto out_unlock; 342f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 34375891f50STrond Myklebust __xprt_lock_write_func, xprt)) 344961a828dSTrond Myklebust return; 3451da177e4SLinus Torvalds out_unlock: 346632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 34949e9a890SChuck Lever /** 35049e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 35149e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 35249e9a890SChuck Lever * @task: task that is releasing access to the transport 35349e9a890SChuck Lever * 35449e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3551da177e4SLinus Torvalds */ 35649e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds if (xprt->snd_task == task) { 359632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3601da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3611da177e4SLinus Torvalds } 362bf7ca707SChuck Lever trace_xprt_release_xprt(xprt, task); 3631da177e4SLinus Torvalds } 36412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3651da177e4SLinus Torvalds 36649e9a890SChuck Lever /** 36749e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 36849e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 36949e9a890SChuck Lever * @task: task that is releasing access to the transport 37049e9a890SChuck Lever * 37149e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 37249e9a890SChuck Lever * transport if the transport's congestion window allows it. 37349e9a890SChuck Lever */ 37449e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 37549e9a890SChuck Lever { 37649e9a890SChuck Lever if (xprt->snd_task == task) { 377632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 37849e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 37949e9a890SChuck Lever } 380bf7ca707SChuck Lever trace_xprt_release_cong(xprt, task); 38149e9a890SChuck Lever } 38212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 38349e9a890SChuck Lever 38449e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3851da177e4SLinus Torvalds { 386bd79bc57STrond Myklebust if (xprt->snd_task != task) 387bd79bc57STrond Myklebust return; 388b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 38949e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 390b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3951da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds static int 39875891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds if (req->rq_cong) 4011da177e4SLinus Torvalds return 1; 402bf7ca707SChuck Lever trace_xprt_get_cong(xprt, req->rq_task); 40375891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 40475891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 4051da177e4SLinus Torvalds return 0; 40675891f50STrond Myklebust } 4071da177e4SLinus Torvalds req->rq_cong = 1; 4081da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4091da177e4SLinus Torvalds return 1; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* 4131da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4141da177e4SLinus Torvalds * that has been sleeping due to congestion 4151da177e4SLinus Torvalds */ 4161da177e4SLinus Torvalds static void 4171da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds if (!req->rq_cong) 4201da177e4SLinus Torvalds return; 4211da177e4SLinus Torvalds req->rq_cong = 0; 4221da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 42375891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 424bf7ca707SChuck Lever trace_xprt_put_cong(xprt, req->rq_task); 42549e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 42846c0ee8bSChuck Lever /** 42975891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 43075891f50STrond Myklebust * @xprt: pointer to transport 43175891f50STrond Myklebust * @req: pointer to RPC request 43275891f50STrond Myklebust * 43375891f50STrond Myklebust * Useful for transports that require congestion control. 43475891f50STrond Myklebust */ 43575891f50STrond Myklebust bool 43675891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 43775891f50STrond Myklebust { 43875891f50STrond Myklebust bool ret = false; 43975891f50STrond Myklebust 44075891f50STrond Myklebust if (req->rq_cong) 44175891f50STrond Myklebust return true; 442b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 44375891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 444b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 44575891f50STrond Myklebust return ret; 44675891f50STrond Myklebust } 44775891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 44875891f50STrond Myklebust 44975891f50STrond Myklebust /** 450a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 451a58dd398SChuck Lever * @task: RPC request that recently completed 452a58dd398SChuck Lever * 453a58dd398SChuck Lever * Useful for transports that require congestion control. 454a58dd398SChuck Lever */ 455a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 456a58dd398SChuck Lever { 457a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 458a4f0835cSTrond Myklebust 459a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 460a58dd398SChuck Lever } 46112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 462a58dd398SChuck Lever 4638593e010SChuck Lever static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt) 4648593e010SChuck Lever { 4658593e010SChuck Lever if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) 4668593e010SChuck Lever __xprt_lock_write_next_cong(xprt); 4678593e010SChuck Lever } 4688593e010SChuck Lever 46975891f50STrond Myklebust /* 47075891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 47175891f50STrond Myklebust * entry on xprt->sending 47275891f50STrond Myklebust */ 47375891f50STrond Myklebust static void 47475891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 47575891f50STrond Myklebust { 47675891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 477b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 47875891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 479b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 48075891f50STrond Myklebust } 48175891f50STrond Myklebust } 48275891f50STrond Myklebust 483a58dd398SChuck Lever /** 48446c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4856a24dfb6STrond Myklebust * @xprt: pointer to xprt 48646c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 48746c0ee8bSChuck Lever * @result: result code of completed RPC request 48846c0ee8bSChuck Lever * 4894f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4904f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4914f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4924f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4934f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4944f4cf5adSChuck Lever * 4954f4cf5adSChuck Lever * - a reply is received and 4964f4cf5adSChuck Lever * - a full number of requests are outstanding and 4974f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4981da177e4SLinus Torvalds */ 4996a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 5001da177e4SLinus Torvalds { 50146c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 50246c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 5051da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 5061da177e4SLinus Torvalds * the result gets rounded properly. */ 5071da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 5081da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 5091da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 51049e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 5111da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 5121da177e4SLinus Torvalds cwnd >>= 1; 5131da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 5141da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 5171da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 5181da177e4SLinus Torvalds xprt->cwnd = cwnd; 51946c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 5201da177e4SLinus Torvalds } 52112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 5221da177e4SLinus Torvalds 52344fbac22SChuck Lever /** 52444fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 52544fbac22SChuck Lever * @xprt: transport with waiting tasks 52644fbac22SChuck Lever * @status: result code to plant in each task before waking it 52744fbac22SChuck Lever * 52844fbac22SChuck Lever */ 52944fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 53044fbac22SChuck Lever { 53144fbac22SChuck Lever if (status < 0) 53244fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 53344fbac22SChuck Lever else 53444fbac22SChuck Lever rpc_wake_up(&xprt->pending); 53544fbac22SChuck Lever } 53612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 53744fbac22SChuck Lever 538c7b2cae8SChuck Lever /** 539c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 540c544577dSTrond Myklebust * @xprt: transport 541a9a6b52eSTrond Myklebust * 542a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 543a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 544a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 545c7b2cae8SChuck Lever */ 546c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 547c7b2cae8SChuck Lever { 548c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 549c7b2cae8SChuck Lever } 55012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 551c7b2cae8SChuck Lever 552c544577dSTrond Myklebust static bool 553c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 554c544577dSTrond Myklebust { 555c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 556c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 557c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 558c544577dSTrond Myklebust "xprt %p\n", xprt); 559c544577dSTrond Myklebust return true; 560c544577dSTrond Myklebust } 561c544577dSTrond Myklebust return false; 562c544577dSTrond Myklebust } 563c544577dSTrond Myklebust 564c7b2cae8SChuck Lever /** 565c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 566c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 567c7b2cae8SChuck Lever * 568c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 569c7b2cae8SChuck Lever */ 570c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 571c7b2cae8SChuck Lever { 572c544577dSTrond Myklebust bool ret; 573c544577dSTrond Myklebust 574c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 575c544577dSTrond Myklebust return false; 576b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 577c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 578b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 579c544577dSTrond Myklebust return ret; 580c7b2cae8SChuck Lever } 58112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 582c7b2cae8SChuck Lever 583da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime) 584da953063STrond Myklebust { 585da953063STrond Myklebust s64 delta = ktime_to_ns(ktime_get() - abstime); 586da953063STrond Myklebust return likely(delta >= 0) ? 587da953063STrond Myklebust jiffies - nsecs_to_jiffies(delta) : 588da953063STrond Myklebust jiffies + nsecs_to_jiffies(-delta); 589da953063STrond Myklebust } 590da953063STrond Myklebust 591da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req) 5921da177e4SLinus Torvalds { 593ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 594da953063STrond Myklebust unsigned long majortimeo = req->rq_timeout; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (to->to_exponential) 597da953063STrond Myklebust majortimeo <<= to->to_retries; 5981da177e4SLinus Torvalds else 599da953063STrond Myklebust majortimeo += to->to_increment * to->to_retries; 600da953063STrond Myklebust if (majortimeo > to->to_maxval || majortimeo == 0) 601da953063STrond Myklebust majortimeo = to->to_maxval; 602da953063STrond Myklebust return majortimeo; 603da953063STrond Myklebust } 604da953063STrond Myklebust 605da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req) 606da953063STrond Myklebust { 607da953063STrond Myklebust req->rq_majortimeo += xprt_calc_majortimeo(req); 608da953063STrond Myklebust } 609da953063STrond Myklebust 610da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req) 611da953063STrond Myklebust { 612da953063STrond Myklebust unsigned long time_init; 613da953063STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 614da953063STrond Myklebust 615da953063STrond Myklebust if (likely(xprt && xprt_connected(xprt))) 616da953063STrond Myklebust time_init = jiffies; 617da953063STrond Myklebust else 618da953063STrond Myklebust time_init = xprt_abs_ktime_to_jiffies(task->tk_start); 619da953063STrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 620da953063STrond Myklebust req->rq_majortimeo = time_init + xprt_calc_majortimeo(req); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6239903cd1cSChuck Lever /** 6249903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 6259903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 6269903cd1cSChuck Lever * 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 631ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 6321da177e4SLinus Torvalds int status = 0; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 6351da177e4SLinus Torvalds if (to->to_exponential) 6361da177e4SLinus Torvalds req->rq_timeout <<= 1; 6371da177e4SLinus Torvalds else 6381da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 6391da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 6401da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 6411da177e4SLinus Torvalds req->rq_retries++; 6421da177e4SLinus Torvalds } else { 6431da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 6441da177e4SLinus Torvalds req->rq_retries = 0; 6451da177e4SLinus Torvalds xprt_reset_majortimeo(req); 6461da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 647b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 6481da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 649b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 6501da177e4SLinus Torvalds status = -ETIMEDOUT; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6541da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6551da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds return status; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 66065f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6611da177e4SLinus Torvalds { 66265f27f38SDavid Howells struct rpc_xprt *xprt = 66365f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 664a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 6651da177e4SLinus Torvalds 666911813d7SChuck Lever trace_xprt_disconnect_auto(xprt); 66766af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6684876cc77STrond Myklebust xprt->ops->close(xprt); 6691da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 67079234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 671a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6749903cd1cSChuck Lever /** 67562da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6769903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6779903cd1cSChuck Lever * 6781da177e4SLinus Torvalds */ 67962da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6801da177e4SLinus Torvalds { 681911813d7SChuck Lever trace_xprt_disconnect_done(xprt); 682b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 6831da177e4SLinus Torvalds xprt_clear_connected(xprt); 684c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 6858593e010SChuck Lever xprt_clear_congestion_window_wait_locked(xprt); 68627adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 687b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 6881da177e4SLinus Torvalds } 68962da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6901da177e4SLinus Torvalds 69166af1e55STrond Myklebust /** 69266af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 69366af1e55STrond Myklebust * @xprt: transport to disconnect 69466af1e55STrond Myklebust * 69566af1e55STrond Myklebust */ 69666af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 69766af1e55STrond Myklebust { 698911813d7SChuck Lever trace_xprt_disconnect_force(xprt); 699911813d7SChuck Lever 70066af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 701b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 70266af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 70366af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 70466af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 70540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7060445f92cSTrond Myklebust else if (xprt->snd_task) 7070445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 7080445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 709b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 71066af1e55STrond Myklebust } 711e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 71266af1e55STrond Myklebust 7137f3a1d1eSTrond Myklebust static unsigned int 7147f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 7157f3a1d1eSTrond Myklebust { 7167f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 7177f3a1d1eSTrond Myklebust } 7187f3a1d1eSTrond Myklebust 7197f3a1d1eSTrond Myklebust static bool 7207f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 7217f3a1d1eSTrond Myklebust { 7227f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 7237f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 7247f3a1d1eSTrond Myklebust 7257f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7267f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7277f3a1d1eSTrond Myklebust } 7287f3a1d1eSTrond Myklebust 7297c1d71cfSTrond Myklebust /** 7307c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 7317c1d71cfSTrond Myklebust * @xprt: transport to disconnect 7327c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 7337c1d71cfSTrond Myklebust * 7347c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 7357c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 7367c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 7377c1d71cfSTrond Myklebust * a batch of RPC requests. 7387c1d71cfSTrond Myklebust * 7397c1d71cfSTrond Myklebust */ 7407c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 7417c1d71cfSTrond Myklebust { 7427c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 743b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7447c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 7457c1d71cfSTrond Myklebust goto out; 7462c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 7477c1d71cfSTrond Myklebust goto out; 7487c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 7497c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 7507c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 75140a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7522a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 7537c1d71cfSTrond Myklebust out: 754b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7557c1d71cfSTrond Myklebust } 7567c1d71cfSTrond Myklebust 757ad3331acSTrond Myklebust static bool 758ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 759ad3331acSTrond Myklebust { 760ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 761ad3331acSTrond Myklebust } 762ad3331acSTrond Myklebust 763ad3331acSTrond Myklebust static void 764ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 765ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 766ad3331acSTrond Myklebust { 76780d3c45fSDave Wysochanski xprt->last_used = jiffies; 76895f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 769ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 770ad3331acSTrond Myklebust } 771ad3331acSTrond Myklebust 7721da177e4SLinus Torvalds static void 773ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 7741da177e4SLinus Torvalds { 775ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 7761da177e4SLinus Torvalds 77795f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 778b5e92419STrond Myklebust return; 779ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 780ad3331acSTrond Myklebust xprt->last_used = jiffies; 7812226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7821da177e4SLinus Torvalds return; 783b5e92419STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 786718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 787718ba5b8STrond Myklebust struct rpc_task *task, 788718ba5b8STrond Myklebust void *cookie) 789718ba5b8STrond Myklebust { 790718ba5b8STrond Myklebust bool ret = false; 791718ba5b8STrond Myklebust 792b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 793718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 794718ba5b8STrond Myklebust goto out; 795718ba5b8STrond Myklebust if (xprt->snd_task != task) 796718ba5b8STrond Myklebust goto out; 797718ba5b8STrond Myklebust xprt->snd_task = cookie; 798718ba5b8STrond Myklebust ret = true; 799718ba5b8STrond Myklebust out: 800b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 801718ba5b8STrond Myklebust return ret; 802718ba5b8STrond Myklebust } 803718ba5b8STrond Myklebust 804718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 805718ba5b8STrond Myklebust { 806b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 807718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 808718ba5b8STrond Myklebust goto out; 809718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 810718ba5b8STrond Myklebust goto out; 811718ba5b8STrond Myklebust xprt->snd_task =NULL; 812718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 813ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 814718ba5b8STrond Myklebust out: 815b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 81679234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 817718ba5b8STrond Myklebust } 818718ba5b8STrond Myklebust 8199903cd1cSChuck Lever /** 8209903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8219903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8221da177e4SLinus Torvalds * 8231da177e4SLinus Torvalds */ 8241da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8251da177e4SLinus Torvalds { 826ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8271da177e4SLinus Torvalds 82846121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 8291da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 8301da177e4SLinus Torvalds 831ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 83201d37c42STrond Myklebust task->tk_status = -EAGAIN; 8331da177e4SLinus Torvalds return; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 8361da177e4SLinus Torvalds return; 837feb8ca37STrond Myklebust 838911813d7SChuck Lever if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 839911813d7SChuck Lever trace_xprt_disconnect_cleanup(xprt); 840feb8ca37STrond Myklebust xprt->ops->close(xprt); 841911813d7SChuck Lever } 842feb8ca37STrond Myklebust 843718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 8442c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 8456b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->pending, task, NULL, 8469e910bffSTrond Myklebust xprt_request_timeout(task->tk_rqstp)); 8470b9e7943STrond Myklebust 8480b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 8490b9e7943STrond Myklebust return; 8500b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 8510b9e7943STrond Myklebust return; 8520a9a4304STrond Myklebust /* Race breaker */ 8530a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 854262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 8551b092092STrond Myklebust xprt->ops->connect(xprt, task); 8560a9a4304STrond Myklebust } else { 8570a9a4304STrond Myklebust xprt_clear_connecting(xprt); 8580a9a4304STrond Myklebust task->tk_status = 0; 8590a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8600a9a4304STrond Myklebust } 8611da177e4SLinus Torvalds } 862718ba5b8STrond Myklebust xprt_release_write(xprt, task); 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 865675dd90aSChuck Lever /** 866675dd90aSChuck Lever * xprt_reconnect_delay - compute the wait before scheduling a connect 867675dd90aSChuck Lever * @xprt: transport instance 868675dd90aSChuck Lever * 869675dd90aSChuck Lever */ 870675dd90aSChuck Lever unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt) 871675dd90aSChuck Lever { 872675dd90aSChuck Lever unsigned long start, now = jiffies; 873675dd90aSChuck Lever 874675dd90aSChuck Lever start = xprt->stat.connect_start + xprt->reestablish_timeout; 875675dd90aSChuck Lever if (time_after(start, now)) 876675dd90aSChuck Lever return start - now; 877675dd90aSChuck Lever return 0; 878675dd90aSChuck Lever } 879675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_delay); 880675dd90aSChuck Lever 881675dd90aSChuck Lever /** 882675dd90aSChuck Lever * xprt_reconnect_backoff - compute the new re-establish timeout 883675dd90aSChuck Lever * @xprt: transport instance 884675dd90aSChuck Lever * @init_to: initial reestablish timeout 885675dd90aSChuck Lever * 886675dd90aSChuck Lever */ 887675dd90aSChuck Lever void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to) 888675dd90aSChuck Lever { 889675dd90aSChuck Lever xprt->reestablish_timeout <<= 1; 890675dd90aSChuck Lever if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 891675dd90aSChuck Lever xprt->reestablish_timeout = xprt->max_reconnect_timeout; 892675dd90aSChuck Lever if (xprt->reestablish_timeout < init_to) 893675dd90aSChuck Lever xprt->reestablish_timeout = init_to; 894675dd90aSChuck Lever } 895675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_backoff); 896675dd90aSChuck Lever 89795f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 89895f7691dSTrond Myklebust XID_RB_EQUAL, 89995f7691dSTrond Myklebust XID_RB_LEFT, 90095f7691dSTrond Myklebust XID_RB_RIGHT, 90195f7691dSTrond Myklebust }; 90295f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 90395f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 90495f7691dSTrond Myklebust { 90595f7691dSTrond Myklebust if (xid1 == xid2) 90695f7691dSTrond Myklebust return XID_RB_EQUAL; 90795f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 90895f7691dSTrond Myklebust return XID_RB_LEFT; 90995f7691dSTrond Myklebust return XID_RB_RIGHT; 91095f7691dSTrond Myklebust } 91195f7691dSTrond Myklebust 91295f7691dSTrond Myklebust static struct rpc_rqst * 91395f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 91495f7691dSTrond Myklebust { 91595f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 91695f7691dSTrond Myklebust struct rpc_rqst *req; 91795f7691dSTrond Myklebust 91895f7691dSTrond Myklebust while (n != NULL) { 91995f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 92095f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 92195f7691dSTrond Myklebust case XID_RB_LEFT: 92295f7691dSTrond Myklebust n = n->rb_left; 92395f7691dSTrond Myklebust break; 92495f7691dSTrond Myklebust case XID_RB_RIGHT: 92595f7691dSTrond Myklebust n = n->rb_right; 92695f7691dSTrond Myklebust break; 92795f7691dSTrond Myklebust case XID_RB_EQUAL: 92895f7691dSTrond Myklebust return req; 92995f7691dSTrond Myklebust } 93095f7691dSTrond Myklebust } 93195f7691dSTrond Myklebust return NULL; 93295f7691dSTrond Myklebust } 93395f7691dSTrond Myklebust 93495f7691dSTrond Myklebust static void 93595f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 93695f7691dSTrond Myklebust { 93795f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 93895f7691dSTrond Myklebust struct rb_node *n = NULL; 93995f7691dSTrond Myklebust struct rpc_rqst *req; 94095f7691dSTrond Myklebust 94195f7691dSTrond Myklebust while (*p != NULL) { 94295f7691dSTrond Myklebust n = *p; 94395f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 94495f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 94595f7691dSTrond Myklebust case XID_RB_LEFT: 94695f7691dSTrond Myklebust p = &n->rb_left; 94795f7691dSTrond Myklebust break; 94895f7691dSTrond Myklebust case XID_RB_RIGHT: 94995f7691dSTrond Myklebust p = &n->rb_right; 95095f7691dSTrond Myklebust break; 95195f7691dSTrond Myklebust case XID_RB_EQUAL: 95295f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 95395f7691dSTrond Myklebust return; 95495f7691dSTrond Myklebust } 95595f7691dSTrond Myklebust } 95695f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 95795f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 95895f7691dSTrond Myklebust } 95995f7691dSTrond Myklebust 96095f7691dSTrond Myklebust static void 96195f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 96295f7691dSTrond Myklebust { 96395f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 96495f7691dSTrond Myklebust } 96595f7691dSTrond Myklebust 9669903cd1cSChuck Lever /** 9679903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 9689903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 9699903cd1cSChuck Lever * @xid: RPC XID of incoming reply 9709903cd1cSChuck Lever * 97175c84151STrond Myklebust * Caller holds xprt->queue_lock. 9721da177e4SLinus Torvalds */ 973d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 9741da177e4SLinus Torvalds { 9758f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 9761da177e4SLinus Torvalds 97795f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 97895f7691dSTrond Myklebust if (entry != NULL) { 9793705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 9800b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 981262ca07dSChuck Lever return entry; 9823705ad64SJeff Layton } 98346121cf7SChuck Lever 98446121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 98546121cf7SChuck Lever ntohl(xid)); 9863705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 987262ca07dSChuck Lever xprt->stat.bad_xids++; 988262ca07dSChuck Lever return NULL; 9891da177e4SLinus Torvalds } 99012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 9911da177e4SLinus Torvalds 992cf9946cdSTrond Myklebust static bool 993cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 994cf9946cdSTrond Myklebust { 995cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 996cf9946cdSTrond Myklebust } 997cf9946cdSTrond Myklebust 998729749bbSTrond Myklebust /** 999729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 1000729749bbSTrond Myklebust * @req: Request to pin 1001729749bbSTrond Myklebust * 1002729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 10031f7d1c73SChuck Lever * so should be holding xprt->queue_lock. 1004729749bbSTrond Myklebust */ 1005729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 1006729749bbSTrond Myklebust { 1007cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 1008729749bbSTrond Myklebust } 10099590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 1010729749bbSTrond Myklebust 1011729749bbSTrond Myklebust /** 1012729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 1013729749bbSTrond Myklebust * @req: Request to pin 1014729749bbSTrond Myklebust * 10151f7d1c73SChuck Lever * Caller should be holding xprt->queue_lock. 1016729749bbSTrond Myklebust */ 1017729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 1018729749bbSTrond Myklebust { 1019cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 1020cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 1021cf9946cdSTrond Myklebust return; 1022cf9946cdSTrond Myklebust } 1023cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 1024cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 1025729749bbSTrond Myklebust } 10269590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 1027729749bbSTrond Myklebust 1028729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 1029729749bbSTrond Myklebust { 1030cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 1031729749bbSTrond Myklebust } 1032729749bbSTrond Myklebust 1033edc81dcdSTrond Myklebust static bool 1034edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 1035edc81dcdSTrond Myklebust { 1036edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1037edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 1038edc81dcdSTrond Myklebust } 1039edc81dcdSTrond Myklebust 1040edc81dcdSTrond Myklebust static bool 1041edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 1042edc81dcdSTrond Myklebust { 1043edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1044edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 1045edc81dcdSTrond Myklebust } 1046edc81dcdSTrond Myklebust 1047edc81dcdSTrond Myklebust /** 1048edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 1049edc81dcdSTrond Myklebust * @task: RPC task 1050edc81dcdSTrond Myklebust * 1051edc81dcdSTrond Myklebust */ 1052edc81dcdSTrond Myklebust void 1053edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1054edc81dcdSTrond Myklebust { 1055edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1056edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1057edc81dcdSTrond Myklebust 1058edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1059edc81dcdSTrond Myklebust return; 106075369089STrond Myklebust 106175369089STrond Myklebust xprt_request_prepare(task->tk_rqstp); 1062edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1063edc81dcdSTrond Myklebust 1064edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1065edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1066edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1067edc81dcdSTrond Myklebust 1068edc81dcdSTrond Myklebust /* Add request to the receive list */ 106995f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1070edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1071edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1072edc81dcdSTrond Myklebust 1073edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1074edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1075edc81dcdSTrond Myklebust } 1076edc81dcdSTrond Myklebust 1077edc81dcdSTrond Myklebust /** 1078edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1079edc81dcdSTrond Myklebust * @task: RPC task 1080edc81dcdSTrond Myklebust * 1081edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1082edc81dcdSTrond Myklebust */ 1083edc81dcdSTrond Myklebust static void 1084edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1085edc81dcdSTrond Myklebust { 108695f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 108795f7691dSTrond Myklebust 1088edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 108995f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1090edc81dcdSTrond Myklebust } 1091edc81dcdSTrond Myklebust 1092ecd465eeSChuck Lever /** 1093ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1094ecd465eeSChuck Lever * @task: RPC request that recently completed 1095ecd465eeSChuck Lever * 109675c84151STrond Myklebust * Caller holds xprt->queue_lock. 1097ecd465eeSChuck Lever */ 1098ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 10991da177e4SLinus Torvalds { 11001570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 11011570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 110295c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1103d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 11041570c1e4SChuck Lever 11051da177e4SLinus Torvalds if (timer) { 11061da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1107ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 11081570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds } 1111ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 11121da177e4SLinus Torvalds 11131570c1e4SChuck Lever /** 11141570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 11151570c1e4SChuck Lever * @task: RPC request that recently completed 11161570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 11171570c1e4SChuck Lever * 111875c84151STrond Myklebust * Caller holds xprt->queue_lock. 11191570c1e4SChuck Lever */ 11201570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 11211570c1e4SChuck Lever { 11221570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1123fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11241da177e4SLinus Torvalds 11253705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 11261da177e4SLinus Torvalds 1127fda13939STrond Myklebust xprt->stat.recvs++; 1128ef759a2eSChuck Lever 11291e799b67STrond Myklebust req->rq_private_buf.len = copied; 1130dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1131dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 113243ac3f29STrond Myklebust smp_wmb(); 1133dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1134edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1135fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 11361da177e4SLinus Torvalds } 113712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 11381da177e4SLinus Torvalds 113946c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 11401da177e4SLinus Torvalds { 11411da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 11421da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 11431da177e4SLinus Torvalds 11445d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 11455d00837bSTrond Myklebust return; 114646c0ee8bSChuck Lever 114782476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1148dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 114946c0ee8bSChuck Lever if (xprt->ops->timer) 11506a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 11515d00837bSTrond Myklebust } else 11525d00837bSTrond Myklebust task->tk_status = 0; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11559903cd1cSChuck Lever /** 11568ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_def - wait for reply 11578ba6a92dSTrond Myklebust * @task: pointer to rpc_task 11588ba6a92dSTrond Myklebust * 11598ba6a92dSTrond Myklebust * Set a request's retransmit timeout based on the transport's 11608ba6a92dSTrond Myklebust * default timeout parameters. Used by transports that don't adjust 11618ba6a92dSTrond Myklebust * the retransmit timeout based on round-trip time estimation, 11628ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 11638ba6a92dSTrond Myklebust */ 11648ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task) 11658ba6a92dSTrond Myklebust { 11668ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11678ba6a92dSTrond Myklebust 11686b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 11699e910bffSTrond Myklebust xprt_request_timeout(req)); 11708ba6a92dSTrond Myklebust } 11718ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def); 11728ba6a92dSTrond Myklebust 11738ba6a92dSTrond Myklebust /** 11748ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator 11758ba6a92dSTrond Myklebust * @task: pointer to rpc_task 11768ba6a92dSTrond Myklebust * 11778ba6a92dSTrond Myklebust * Set a request's retransmit timeout using the RTT estimator, 11788ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 11798ba6a92dSTrond Myklebust */ 11808ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task) 11818ba6a92dSTrond Myklebust { 11828ba6a92dSTrond Myklebust int timer = task->tk_msg.rpc_proc->p_timer; 11838ba6a92dSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 11848ba6a92dSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 11858ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11868ba6a92dSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 11876b2e6856STrond Myklebust unsigned long timeout; 11888ba6a92dSTrond Myklebust 11896b2e6856STrond Myklebust timeout = rpc_calc_rto(rtt, timer); 11906b2e6856STrond Myklebust timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 11916b2e6856STrond Myklebust if (timeout > max_timeout || timeout == 0) 11926b2e6856STrond Myklebust timeout = max_timeout; 11936b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 11946b2e6856STrond Myklebust jiffies + timeout); 11958ba6a92dSTrond Myklebust } 11968ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt); 11978ba6a92dSTrond Myklebust 11988ba6a92dSTrond Myklebust /** 11997f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 12007f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 12017f3a1d1eSTrond Myklebust * 12027f3a1d1eSTrond Myklebust */ 12037f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 12047f3a1d1eSTrond Myklebust { 12057f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12067f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12077f3a1d1eSTrond Myklebust 12087f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 12097f3a1d1eSTrond Myklebust return; 12107f3a1d1eSTrond Myklebust /* 12117f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 12127f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 12137f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 12147f3a1d1eSTrond Myklebust */ 12157f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 12167f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 12178ba6a92dSTrond Myklebust xprt->ops->wait_for_reply_request(task); 12187f3a1d1eSTrond Myklebust /* 12197f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 12207f3a1d1eSTrond Myklebust * connection was dropped in case the call to 12217f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 12227f3a1d1eSTrond Myklebust */ 12237f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 12247f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 12257f3a1d1eSTrond Myklebust task, -ENOTCONN); 12267f3a1d1eSTrond Myklebust } 12277f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 12287f3a1d1eSTrond Myklebust } 12297f3a1d1eSTrond Myklebust 1230944b0429STrond Myklebust static bool 1231944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1232944b0429STrond Myklebust { 1233762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1234944b0429STrond Myklebust } 1235944b0429STrond Myklebust 1236944b0429STrond Myklebust /** 1237944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1238944b0429STrond Myklebust * @task: pointer to rpc_task 1239944b0429STrond Myklebust * 1240944b0429STrond Myklebust * Add a task to the transmission queue. 1241944b0429STrond Myklebust */ 1242944b0429STrond Myklebust void 1243944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1244944b0429STrond Myklebust { 1245918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1246944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1247944b0429STrond Myklebust 1248944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1249e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1250944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 125175891f50STrond Myklebust /* 125275891f50STrond Myklebust * Requests that carry congestion control credits are added 125375891f50STrond Myklebust * to the head of the list to avoid starvation issues. 125475891f50STrond Myklebust */ 125575891f50STrond Myklebust if (req->rq_cong) { 125675891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 125775891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 125875891f50STrond Myklebust if (pos->rq_cong) 125975891f50STrond Myklebust continue; 126075891f50STrond Myklebust /* Note: req is added _before_ pos */ 126175891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 126275891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12630c77668dSChuck Lever trace_xprt_enq_xmit(task, 1); 126475891f50STrond Myklebust goto out; 126575891f50STrond Myklebust } 126686aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 126786aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 126886aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 126986aeee0eSTrond Myklebust continue; 127086aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 127186aeee0eSTrond Myklebust continue; 127286aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 127386aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 127486aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12750c77668dSChuck Lever trace_xprt_enq_xmit(task, 2); 127686aeee0eSTrond Myklebust goto out; 127786aeee0eSTrond Myklebust } 1278deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1279918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1280918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1281918f3c1fSTrond Myklebust continue; 1282918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1283918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 12840c77668dSChuck Lever trace_xprt_enq_xmit(task, 3); 1285918f3c1fSTrond Myklebust goto out; 1286918f3c1fSTrond Myklebust } 128775891f50STrond Myklebust } 1288944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1289918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12900c77668dSChuck Lever trace_xprt_enq_xmit(task, 4); 1291918f3c1fSTrond Myklebust out: 1292944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1293944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1294944b0429STrond Myklebust } 1295944b0429STrond Myklebust } 1296944b0429STrond Myklebust 1297944b0429STrond Myklebust /** 1298944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1299944b0429STrond Myklebust * @task: pointer to rpc_task 1300944b0429STrond Myklebust * 1301944b0429STrond Myklebust * Remove a task from the transmission queue 1302944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1303944b0429STrond Myklebust */ 1304944b0429STrond Myklebust static void 1305944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1306944b0429STrond Myklebust { 1307918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1308918f3c1fSTrond Myklebust 1309918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1310918f3c1fSTrond Myklebust return; 1311918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1312918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1313918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1314918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1315918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1316918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1317918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1318918f3c1fSTrond Myklebust } 1319918f3c1fSTrond Myklebust } else 1320918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1321944b0429STrond Myklebust } 1322944b0429STrond Myklebust 1323944b0429STrond Myklebust /** 1324944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1325944b0429STrond Myklebust * @task: pointer to rpc_task 1326944b0429STrond Myklebust * 1327944b0429STrond Myklebust * Remove a task from the transmission queue 1328944b0429STrond Myklebust */ 1329944b0429STrond Myklebust static void 1330944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1331944b0429STrond Myklebust { 1332944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1333944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1334944b0429STrond Myklebust 1335944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1336944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1337944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1338944b0429STrond Myklebust } 1339944b0429STrond Myklebust 13407f3a1d1eSTrond Myklebust /** 1341cc204d01STrond Myklebust * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue 1342cc204d01STrond Myklebust * @task: pointer to rpc_task 1343cc204d01STrond Myklebust * 1344cc204d01STrond Myklebust * Remove a task from the transmit and receive queues, and ensure that 1345cc204d01STrond Myklebust * it is not pinned by the receive work item. 1346cc204d01STrond Myklebust */ 1347cc204d01STrond Myklebust void 1348cc204d01STrond Myklebust xprt_request_dequeue_xprt(struct rpc_task *task) 1349cc204d01STrond Myklebust { 1350cc204d01STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1351cc204d01STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1352cc204d01STrond Myklebust 1353cc204d01STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1354cc204d01STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1355cc204d01STrond Myklebust xprt_is_pinned_rqst(req)) { 1356cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1357cc204d01STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1358cc204d01STrond Myklebust xprt_request_dequeue_receive_locked(task); 1359cc204d01STrond Myklebust while (xprt_is_pinned_rqst(req)) { 1360cc204d01STrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1361cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1362cc204d01STrond Myklebust xprt_wait_on_pinned_rqst(req); 1363cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1364cc204d01STrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1365cc204d01STrond Myklebust } 1366cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1367cc204d01STrond Myklebust } 1368cc204d01STrond Myklebust } 1369cc204d01STrond Myklebust 1370cc204d01STrond Myklebust /** 13719d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 13729d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 13739d96acbcSTrond Myklebust * 13749d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 13759d96acbcSTrond Myklebust * the request for transmission or receive. 13769d96acbcSTrond Myklebust */ 13779d96acbcSTrond Myklebust void 13789d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 13799d96acbcSTrond Myklebust { 13809d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 13819d96acbcSTrond Myklebust 13829d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 13839d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 13849d96acbcSTrond Myklebust } 13859d96acbcSTrond Myklebust 13869d96acbcSTrond Myklebust /** 1387762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1388762e4e67STrond Myklebust * @task: pointer to rpc_task 1389762e4e67STrond Myklebust * 1390762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1391762e4e67STrond Myklebust */ 1392762e4e67STrond Myklebust bool 1393762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1394762e4e67STrond Myklebust { 1395762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1396762e4e67STrond Myklebust } 1397762e4e67STrond Myklebust 1398762e4e67STrond Myklebust /** 13999903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 14009903cd1cSChuck Lever * @task: RPC task about to send a request 14019903cd1cSChuck Lever * 14021da177e4SLinus Torvalds */ 140390051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 14041da177e4SLinus Torvalds { 14051da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 14061da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 14071da177e4SLinus Torvalds 140846121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 14091da177e4SLinus Torvalds 14105f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 14115f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1412944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 14135f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 14145f2f6bd9STrond Myklebust task, 0); 14155f2f6bd9STrond Myklebust return false; 14165f2f6bd9STrond Myklebust 14178a19a0b6STrond Myklebust } 14185f2f6bd9STrond Myklebust return true; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 1421e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 14225e5ce5beSTrond Myklebust { 1423343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 14245e5ce5beSTrond Myklebust } 14255e5ce5beSTrond Myklebust 14269903cd1cSChuck Lever /** 142789f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 142889f90fe1STrond Myklebust * @req: pointer to request to transmit 142989f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 14309903cd1cSChuck Lever * 143189f90fe1STrond Myklebust * This performs the transmission of a single request. 143289f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 143389f90fe1STrond Myklebust * does need to be pinned. 143489f90fe1STrond Myklebust * Returns '0' on success. 14359903cd1cSChuck Lever */ 143689f90fe1STrond Myklebust static int 143789f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 144089f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 144190d91b0cSTrond Myklebust unsigned int connect_cookie; 1442dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1443ff699ea8SChuck Lever int status; 14441da177e4SLinus Torvalds 1445edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 144689f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 144789f90fe1STrond Myklebust status = 0; 1448944b0429STrond Myklebust goto out_dequeue; 144989f90fe1STrond Myklebust } 14503021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1451edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 145289f90fe1STrond Myklebust status = -EBADMSG; 1453944b0429STrond Myklebust goto out_dequeue; 14543021a5bbSTrond Myklebust } 1455ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) { 1456ae67bd38STrond Myklebust status = -ERESTARTSYS; 1457ae67bd38STrond Myklebust goto out_dequeue; 1458ae67bd38STrond Myklebust } 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 1461dcbbeda8STrond Myklebust /* 1462dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1463dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1464dcbbeda8STrond Myklebust * reply to the first transmission. 1465dcbbeda8STrond Myklebust */ 1466dcbbeda8STrond Myklebust req->rq_ntrans++; 1467dcbbeda8STrond Myklebust 1468c509f15aSChuck Lever trace_rpc_xdr_sendto(task, &req->rq_snd_buf); 146990d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1470adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1471c8485e4dSTrond Myklebust if (status != 0) { 1472dcbbeda8STrond Myklebust req->rq_ntrans--; 14730c77668dSChuck Lever trace_xprt_transmit(req, status); 147489f90fe1STrond Myklebust return status; 1475c8485e4dSTrond Myklebust } 14767ebbbc6eSTrond Myklebust 1477dcbbeda8STrond Myklebust if (is_retrans) 1478dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1479dcbbeda8STrond Myklebust 14804a068258SChuck Lever xprt_inject_disconnect(xprt); 1481c8485e4dSTrond Myklebust 1482468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1483b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 1484262ca07dSChuck Lever 1485262ca07dSChuck Lever xprt->stat.sends++; 1486262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1487262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 148815a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 148915a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1490b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 149190d91b0cSTrond Myklebust 149290d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1493944b0429STrond Myklebust out_dequeue: 14940c77668dSChuck Lever trace_xprt_transmit(req, status); 1495944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 149689f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 149789f90fe1STrond Myklebust return status; 149889f90fe1STrond Myklebust } 149989f90fe1STrond Myklebust 150089f90fe1STrond Myklebust /** 150189f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 150289f90fe1STrond Myklebust * @task: controlling RPC task 150389f90fe1STrond Myklebust * 150489f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 150589f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 150689f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 150789f90fe1STrond Myklebust * received a reply. 150889f90fe1STrond Myklebust */ 150989f90fe1STrond Myklebust void 151089f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 151189f90fe1STrond Myklebust { 151289f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 151389f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 151489f90fe1STrond Myklebust int status; 151589f90fe1STrond Myklebust 151689f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 151789f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 151889f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 151989f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 152089f90fe1STrond Myklebust xprt_pin_rqst(next); 152189f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 152289f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 152389f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 152489f90fe1STrond Myklebust status = 0; 152589f90fe1STrond Myklebust cond_resched(); 152689f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 152789f90fe1STrond Myklebust xprt_unpin_rqst(next); 152889f90fe1STrond Myklebust if (status == 0) { 152989f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 153089f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 153189f90fe1STrond Myklebust continue; 1532c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 153389f90fe1STrond Myklebust task->tk_status = status; 153489f90fe1STrond Myklebust break; 153589f90fe1STrond Myklebust } 153689f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 1539ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1540ba60eb25STrond Myklebust { 1541ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1542ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1543ba60eb25STrond Myklebust } 1544ba60eb25STrond Myklebust 1545ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1546ba60eb25STrond Myklebust { 1547ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1548ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1549ba60eb25STrond Myklebust } 1550ba60eb25STrond Myklebust 1551ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1552ba60eb25STrond Myklebust { 1553ba60eb25STrond Myklebust bool ret = false; 1554ba60eb25STrond Myklebust 1555ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1556ba60eb25STrond Myklebust goto out; 1557ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1558ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1559ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1560ba60eb25STrond Myklebust ret = true; 1561ba60eb25STrond Myklebust } 1562ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1563ba60eb25STrond Myklebust out: 1564ba60eb25STrond Myklebust return ret; 1565ba60eb25STrond Myklebust } 1566ba60eb25STrond Myklebust 156792ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1568d9ba131dSTrond Myklebust { 1569d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1570d9ba131dSTrond Myklebust 1571ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1572d9ba131dSTrond Myklebust goto out; 1573ff699ea8SChuck Lever ++xprt->num_reqs; 157492ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 157592ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 157692ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1577d9ba131dSTrond Myklebust if (req != NULL) 1578d9ba131dSTrond Myklebust goto out; 1579ff699ea8SChuck Lever --xprt->num_reqs; 1580d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1581d9ba131dSTrond Myklebust out: 1582d9ba131dSTrond Myklebust return req; 1583d9ba131dSTrond Myklebust } 1584d9ba131dSTrond Myklebust 1585d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1586d9ba131dSTrond Myklebust { 1587ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1588ff699ea8SChuck Lever --xprt->num_reqs; 1589d9ba131dSTrond Myklebust kfree(req); 1590d9ba131dSTrond Myklebust return true; 1591d9ba131dSTrond Myklebust } 1592d9ba131dSTrond Myklebust return false; 1593d9ba131dSTrond Myklebust } 1594d9ba131dSTrond Myklebust 1595f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 15961da177e4SLinus Torvalds { 1597d9ba131dSTrond Myklebust struct rpc_rqst *req; 15981da177e4SLinus Torvalds 1599f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 16001da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1601d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1602d9ba131dSTrond Myklebust list_del(&req->rq_list); 1603d9ba131dSTrond Myklebust goto out_init_req; 1604d9ba131dSTrond Myklebust } 160592ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1606d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1607d9ba131dSTrond Myklebust goto out_init_req; 1608d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1609d9ba131dSTrond Myklebust case -ENOMEM: 1610d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1611d9ba131dSTrond Myklebust "failed! Retrying\n"); 16121afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1613d9ba131dSTrond Myklebust break; 1614d9ba131dSTrond Myklebust case -EAGAIN: 1615ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1616d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1617e9d47639SGustavo A. R. Silva /* fall through */ 16181afeaf5cSTrond Myklebust default: 1619d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 16201afeaf5cSTrond Myklebust } 1621f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1622d9ba131dSTrond Myklebust return; 1623d9ba131dSTrond Myklebust out_init_req: 1624ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1625ff699ea8SChuck Lever xprt->num_reqs); 162637ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 162737ac86c3SChuck Lever 1628d9ba131dSTrond Myklebust task->tk_status = 0; 16291da177e4SLinus Torvalds task->tk_rqstp = req; 16301da177e4SLinus Torvalds } 1631f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1632f39c1bfbSTrond Myklebust 1633a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1634ee5ebe85STrond Myklebust { 1635ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1636c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1637c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1638ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1639c25573b5STrond Myklebust } 1640ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1641ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1642ee5ebe85STrond Myklebust } 1643a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1644ee5ebe85STrond Myklebust 164521de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 164621de0a95STrond Myklebust { 164721de0a95STrond Myklebust struct rpc_rqst *req; 164821de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 164921de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 165021de0a95STrond Myklebust list_del(&req->rq_list); 165121de0a95STrond Myklebust kfree(req); 165221de0a95STrond Myklebust } 165321de0a95STrond Myklebust } 165421de0a95STrond Myklebust 1655d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1656d9ba131dSTrond Myklebust unsigned int num_prealloc, 1657d9ba131dSTrond Myklebust unsigned int max_alloc) 1658bd1722d4SPavel Emelyanov { 1659bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 166021de0a95STrond Myklebust struct rpc_rqst *req; 166121de0a95STrond Myklebust int i; 1662bd1722d4SPavel Emelyanov 1663bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1664bd1722d4SPavel Emelyanov if (xprt == NULL) 1665bd1722d4SPavel Emelyanov goto out; 1666bd1722d4SPavel Emelyanov 166721de0a95STrond Myklebust xprt_init(xprt, net); 166821de0a95STrond Myklebust 166921de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 167021de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 167121de0a95STrond Myklebust if (!req) 16728313164cSwangweidong goto out_free; 167321de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 167421de0a95STrond Myklebust } 1675d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1676d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1677d9ba131dSTrond Myklebust else 167821de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1679d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1680ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1681bd1722d4SPavel Emelyanov 1682bd1722d4SPavel Emelyanov return xprt; 1683bd1722d4SPavel Emelyanov 1684bd1722d4SPavel Emelyanov out_free: 168521de0a95STrond Myklebust xprt_free(xprt); 1686bd1722d4SPavel Emelyanov out: 1687bd1722d4SPavel Emelyanov return NULL; 1688bd1722d4SPavel Emelyanov } 1689bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1690bd1722d4SPavel Emelyanov 1691e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1692e204e621SPavel Emelyanov { 169337aa2133SPavel Emelyanov put_net(xprt->xprt_net); 169421de0a95STrond Myklebust xprt_free_all_slots(xprt); 1695fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1696e204e621SPavel Emelyanov } 1697e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1698e204e621SPavel Emelyanov 1699902c5887STrond Myklebust static void 1700902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1701902c5887STrond Myklebust { 1702902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1703902c5887STrond Myklebust } 1704902c5887STrond Myklebust 17059dc6edcfSTrond Myklebust static __be32 17069dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 17079dc6edcfSTrond Myklebust { 17089dc6edcfSTrond Myklebust __be32 xid; 17099dc6edcfSTrond Myklebust 17109dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 17119dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 17129dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 17139dc6edcfSTrond Myklebust return xid; 17149dc6edcfSTrond Myklebust } 17159dc6edcfSTrond Myklebust 17169dc6edcfSTrond Myklebust static void 17179dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 17189dc6edcfSTrond Myklebust { 17199dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 17209dc6edcfSTrond Myklebust } 17219dc6edcfSTrond Myklebust 17229dc6edcfSTrond Myklebust static void 17239dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 17249dc6edcfSTrond Myklebust { 17259dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 17269dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17279dc6edcfSTrond Myklebust 17289dc6edcfSTrond Myklebust req->rq_task = task; 17299dc6edcfSTrond Myklebust req->rq_xprt = xprt; 17309dc6edcfSTrond Myklebust req->rq_buffer = NULL; 17319dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1732902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 17339dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 17349dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 17359dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 17369dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 173771700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 173871700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 17399dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 1740da953063STrond Myklebust xprt_init_majortimeo(task, req); 17419dc6edcfSTrond Myklebust dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 17429dc6edcfSTrond Myklebust req, ntohl(req->rq_xid)); 17439dc6edcfSTrond Myklebust } 17449dc6edcfSTrond Myklebust 17459dc6edcfSTrond Myklebust static void 17469dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 17479dc6edcfSTrond Myklebust { 17489dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 17499dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 17509dc6edcfSTrond Myklebust xprt_request_init(task); 17519dc6edcfSTrond Myklebust } 17529dc6edcfSTrond Myklebust 17539903cd1cSChuck Lever /** 17549903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 17559903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 17569903cd1cSChuck Lever * 1757ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1758ba60eb25STrond Myklebust * slots are available, place the task on the transport's 17599903cd1cSChuck Lever * backlog queue. 17609903cd1cSChuck Lever */ 17619903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 17621da177e4SLinus Torvalds { 1763fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 17641da177e4SLinus Torvalds 176543cedbf0STrond Myklebust task->tk_status = 0; 176643cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 176743cedbf0STrond Myklebust return; 176843cedbf0STrond Myklebust 176943cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1770ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 17719dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1772ba60eb25STrond Myklebust } 1773ba60eb25STrond Myklebust 1774ba60eb25STrond Myklebust /** 1775ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1776ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1777ba60eb25STrond Myklebust * 1778ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1779ba60eb25STrond Myklebust * backlog queue. 1780ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1781ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1782ba60eb25STrond Myklebust */ 1783ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1784ba60eb25STrond Myklebust { 1785fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1786ba60eb25STrond Myklebust 1787ba60eb25STrond Myklebust task->tk_status = 0; 1788ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1789ba60eb25STrond Myklebust return; 1790ba60eb25STrond Myklebust 1791ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 17929dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 17959903cd1cSChuck Lever /** 17969903cd1cSChuck Lever * xprt_release - release an RPC request slot 17979903cd1cSChuck Lever * @task: task which is finished with the slot 17989903cd1cSChuck Lever * 17991da177e4SLinus Torvalds */ 18009903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 18011da177e4SLinus Torvalds { 180255ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 180387ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 18041da177e4SLinus Torvalds 180587ed5003STrond Myklebust if (req == NULL) { 180687ed5003STrond Myklebust if (task->tk_client) { 1807fb43d172STrond Myklebust xprt = task->tk_xprt; 180887ed5003STrond Myklebust xprt_release_write(xprt, task); 180987ed5003STrond Myklebust } 18101da177e4SLinus Torvalds return; 181187ed5003STrond Myklebust } 181255ae1aabSRicardo Labiaga 181355ae1aabSRicardo Labiaga xprt = req->rq_xprt; 1814cc204d01STrond Myklebust xprt_request_dequeue_xprt(task); 1815b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 181649e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1817a58dd398SChuck Lever if (xprt->ops->release_request) 1818a58dd398SChuck Lever xprt->ops->release_request(task); 1819ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 1820b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 1821ee5ebe85STrond Myklebust if (req->rq_buffer) 18223435c74aSChuck Lever xprt->ops->buf_free(task); 18234a068258SChuck Lever xprt_inject_disconnect(xprt); 18249d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 18250472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1826a17c2153STrond Myklebust if (req->rq_cred != NULL) 1827a17c2153STrond Myklebust put_rpccred(req->rq_cred); 18281da177e4SLinus Torvalds task->tk_rqstp = NULL; 1829ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1830ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 183155ae1aabSRicardo Labiaga 183246121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1833ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1834a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1835ee5ebe85STrond Myklebust else 1836c9acb42eSTrond Myklebust xprt_free_bc_request(req); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 1839902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1840902c5887STrond Myklebust void 1841902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1842902c5887STrond Myklebust { 1843902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1844902c5887STrond Myklebust 1845902c5887STrond Myklebust task->tk_rqstp = req; 1846902c5887STrond Myklebust req->rq_task = task; 1847902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1848902c5887STrond Myklebust /* 1849902c5887STrond Myklebust * Set up the xdr_buf length. 1850902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1851902c5887STrond Myklebust */ 1852902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1853902c5887STrond Myklebust xbufp->tail[0].iov_len; 1854902c5887STrond Myklebust } 1855902c5887STrond Myklebust #endif 1856902c5887STrond Myklebust 185721de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1858c2866763SChuck Lever { 185930c5116bSTrond Myklebust kref_init(&xprt->kref); 1860c2866763SChuck Lever 1861c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1862c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 186375c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1864c2866763SChuck Lever 1865c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 186695f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1867944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 18689e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1869f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1870f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 18719e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 187280b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1873f9acac1aSRicardo Labiaga 1874c2866763SChuck Lever xprt->last_used = jiffies; 1875c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1876a509050bSChuck Lever xprt->bind_index = 0; 1877c2866763SChuck Lever 1878c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1879c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 188079c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1881c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1882c2866763SChuck Lever 1883c2866763SChuck Lever xprt_init_xid(xprt); 1884c2866763SChuck Lever 188521de0a95STrond Myklebust xprt->xprt_net = get_net(net); 18868d9266ffSTrond Myklebust } 18878d9266ffSTrond Myklebust 18888d9266ffSTrond Myklebust /** 18898d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 18908d9266ffSTrond Myklebust * @args: rpc transport creation arguments 18918d9266ffSTrond Myklebust * 18928d9266ffSTrond Myklebust */ 18938d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 18948d9266ffSTrond Myklebust { 18958d9266ffSTrond Myklebust struct rpc_xprt *xprt; 18968d9266ffSTrond Myklebust struct xprt_class *t; 18978d9266ffSTrond Myklebust 18988d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 18998d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 19008d9266ffSTrond Myklebust if (t->ident == args->ident) { 19018d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 19028d9266ffSTrond Myklebust goto found; 19038d9266ffSTrond Myklebust } 19048d9266ffSTrond Myklebust } 19058d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 19063c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 19078d9266ffSTrond Myklebust return ERR_PTR(-EIO); 19088d9266ffSTrond Myklebust 19098d9266ffSTrond Myklebust found: 19108d9266ffSTrond Myklebust xprt = t->setup(args); 1911911813d7SChuck Lever if (IS_ERR(xprt)) 191221de0a95STrond Myklebust goto out; 191333d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 191433d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 191521de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 191621de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1917502980e8SAnna Schumaker timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 191821de0a95STrond Myklebust else 1919ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 19204e0038b6STrond Myklebust 19214e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 19224e0038b6STrond Myklebust xprt_destroy(xprt); 19234e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 19244e0038b6STrond Myklebust } 19254e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 19264e0038b6STrond Myklebust if (xprt->servername == NULL) { 19274e0038b6STrond Myklebust xprt_destroy(xprt); 19284e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 19294e0038b6STrond Myklebust } 19304e0038b6STrond Myklebust 19313f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1932388f0c77SJeff Layton 1933911813d7SChuck Lever trace_xprt_create(xprt); 193421de0a95STrond Myklebust out: 1935c2866763SChuck Lever return xprt; 1936c2866763SChuck Lever } 1937c2866763SChuck Lever 1938528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 1939528fd354STrond Myklebust { 1940528fd354STrond Myklebust struct rpc_xprt *xprt = 1941528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 1942528fd354STrond Myklebust 1943911813d7SChuck Lever trace_xprt_destroy(xprt); 1944911813d7SChuck Lever 1945528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 1946528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1947528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1948528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1949528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1950528fd354STrond Myklebust kfree(xprt->servername); 1951528fd354STrond Myklebust /* 1952669996adSTrond Myklebust * Destroy any existing back channel 1953669996adSTrond Myklebust */ 1954669996adSTrond Myklebust xprt_destroy_backchannel(xprt, UINT_MAX); 1955669996adSTrond Myklebust 1956669996adSTrond Myklebust /* 1957528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 1958528fd354STrond Myklebust */ 1959528fd354STrond Myklebust xprt->ops->destroy(xprt); 1960528fd354STrond Myklebust } 1961528fd354STrond Myklebust 19629903cd1cSChuck Lever /** 19639903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1964a8de240aSTrond Myklebust * @xprt: transport to destroy 19659903cd1cSChuck Lever * 19661da177e4SLinus Torvalds */ 1967a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 19681da177e4SLinus Torvalds { 1969528fd354STrond Myklebust /* 1970528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 1971528fd354STrond Myklebust */ 197279234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 197379234c3dSTrond Myklebust 19740065db32STrond Myklebust del_timer_sync(&xprt->timer); 1975c8541ecdSChuck Lever 1976c8541ecdSChuck Lever /* 1977528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 1978528fd354STrond Myklebust * safely flush receive work running on rpciod. 1979c8541ecdSChuck Lever */ 1980528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 1981528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 19826b6ca86bSTrond Myklebust } 19831da177e4SLinus Torvalds 198430c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 198530c5116bSTrond Myklebust { 198630c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 198730c5116bSTrond Myklebust } 198830c5116bSTrond Myklebust 198930c5116bSTrond Myklebust /** 199030c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 199130c5116bSTrond Myklebust * @xprt: pointer to the transport 199230c5116bSTrond Myklebust * 199330c5116bSTrond Myklebust */ 199430c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 199530c5116bSTrond Myklebust { 199630c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 199730c5116bSTrond Myklebust return xprt; 199830c5116bSTrond Myklebust return NULL; 199930c5116bSTrond Myklebust } 200030c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 200130c5116bSTrond Myklebust 20026b6ca86bSTrond Myklebust /** 20036b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 20046b6ca86bSTrond Myklebust * @xprt: pointer to the transport 20056b6ca86bSTrond Myklebust * 20066b6ca86bSTrond Myklebust */ 20076b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 20086b6ca86bSTrond Myklebust { 200930c5116bSTrond Myklebust if (xprt != NULL) 201030c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 20116b6ca86bSTrond Myklebust } 20125d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 2013