11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 51da177e4SLinus Torvalds * and asynchronous calls. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The interface works like this: 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 101da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 111da177e4SLinus Torvalds * (xprt_reserve). 121da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1355aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1455aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1555ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1655ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1755ae1aabSRicardo Labiaga * expired. 181da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 1955aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 201da177e4SLinus Torvalds * caller is woken up, and the timer removed. 211da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 221da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 231da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 241da177e4SLinus Torvalds * of -ETIMEDOUT. 251da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 261da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 271da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 281da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 291da177e4SLinus Torvalds * again. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 321da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 331da177e4SLinus Torvalds * tasks that rely on callbacks. 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3655aa4f58SChuck Lever * 3755aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 40a246b010SChuck Lever #include <linux/module.h> 41a246b010SChuck Lever 421da177e4SLinus Torvalds #include <linux/types.h> 43a246b010SChuck Lever #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/workqueue.h> 45bf3fcf89SChuck Lever #include <linux/net.h> 46ff839970SChuck Lever #include <linux/ktime.h> 471da177e4SLinus Torvalds 48a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 51fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 52a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 531da177e4SLinus Torvalds 543705ad64SJeff Layton #include <trace/events/sunrpc.h> 553705ad64SJeff Layton 5655ae1aabSRicardo Labiaga #include "sunrpc.h" 5755ae1aabSRicardo Labiaga 581da177e4SLinus Torvalds /* 591da177e4SLinus Torvalds * Local variables 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds 62f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 631da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 641da177e4SLinus Torvalds #endif 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Local functions 681da177e4SLinus Torvalds */ 6921de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 7037ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 714e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 721da177e4SLinus Torvalds 735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7581c098afS\"Talpey, Thomas\ 7612a80469SChuck Lever /** 7781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 7881c098afS\"Talpey, Thomas\ * @transport: transport to register 7981c098afS\"Talpey, Thomas\ * 8081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 8181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 8281c098afS\"Talpey, Thomas\ * 8381c098afS\"Talpey, Thomas\ * Returns: 8481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 8581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 8681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 8781c098afS\"Talpey, Thomas\ */ 8881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 8981c098afS\"Talpey, Thomas\ { 9081c098afS\"Talpey, Thomas\ struct xprt_class *t; 9181c098afS\"Talpey, Thomas\ int result; 9281c098afS\"Talpey, Thomas\ 9381c098afS\"Talpey, Thomas\ result = -EEXIST; 9481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 9581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 9681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 974fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 9881c098afS\"Talpey, Thomas\ goto out; 9981c098afS\"Talpey, Thomas\ } 10081c098afS\"Talpey, Thomas\ 10181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 10281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 10381c098afS\"Talpey, Thomas\ transport->name); 10481c098afS\"Talpey, Thomas\ result = 0; 10581c098afS\"Talpey, Thomas\ 10681c098afS\"Talpey, Thomas\ out: 10781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 10881c098afS\"Talpey, Thomas\ return result; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 11181c098afS\"Talpey, Thomas\ 11281c098afS\"Talpey, Thomas\ /** 11381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 11465b6e42cSRandy Dunlap * @transport: transport to unregister 11581c098afS\"Talpey, Thomas\ * 11681c098afS\"Talpey, Thomas\ * Returns: 11781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 11881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 11981c098afS\"Talpey, Thomas\ */ 12081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 12181c098afS\"Talpey, Thomas\ { 12281c098afS\"Talpey, Thomas\ struct xprt_class *t; 12381c098afS\"Talpey, Thomas\ int result; 12481c098afS\"Talpey, Thomas\ 12581c098afS\"Talpey, Thomas\ result = 0; 12681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 12781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 12881c098afS\"Talpey, Thomas\ if (t == transport) { 12981c098afS\"Talpey, Thomas\ printk(KERN_INFO 13081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 13181c098afS\"Talpey, Thomas\ transport->name); 13281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 13381c098afS\"Talpey, Thomas\ goto out; 13481c098afS\"Talpey, Thomas\ } 13581c098afS\"Talpey, Thomas\ } 13681c098afS\"Talpey, Thomas\ result = -ENOENT; 13781c098afS\"Talpey, Thomas\ 13881c098afS\"Talpey, Thomas\ out: 13981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 14081c098afS\"Talpey, Thomas\ return result; 14181c098afS\"Talpey, Thomas\ } 14281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 14381c098afS\"Talpey, Thomas\ 14481c098afS\"Talpey, Thomas\ /** 145441e3e24STom Talpey * xprt_load_transport - load a transport implementation 146441e3e24STom Talpey * @transport_name: transport to load 147441e3e24STom Talpey * 148441e3e24STom Talpey * Returns: 149441e3e24STom Talpey * 0: transport successfully loaded 150441e3e24STom Talpey * -ENOENT: transport module not available 151441e3e24STom Talpey */ 152441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 153441e3e24STom Talpey { 154441e3e24STom Talpey struct xprt_class *t; 155441e3e24STom Talpey int result; 156441e3e24STom Talpey 157441e3e24STom Talpey result = 0; 158441e3e24STom Talpey spin_lock(&xprt_list_lock); 159441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 160441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 161441e3e24STom Talpey spin_unlock(&xprt_list_lock); 162441e3e24STom Talpey goto out; 163441e3e24STom Talpey } 164441e3e24STom Talpey } 165441e3e24STom Talpey spin_unlock(&xprt_list_lock); 166ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 167441e3e24STom Talpey out: 168441e3e24STom Talpey return result; 169441e3e24STom Talpey } 170441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 171441e3e24STom Talpey 172c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 173c544577dSTrond Myklebust { 174c544577dSTrond Myklebust xprt->snd_task = NULL; 175c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 176c544577dSTrond Myklebust smp_mb__before_atomic(); 177c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 178c544577dSTrond Myklebust smp_mb__after_atomic(); 179c544577dSTrond Myklebust } else 180c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 181c544577dSTrond Myklebust } 182c544577dSTrond Myklebust 183441e3e24STom Talpey /** 18412a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 18512a80469SChuck Lever * @task: task that is requesting access to the transport 186177c27bfSRandy Dunlap * @xprt: pointer to the target transport 18712a80469SChuck Lever * 18812a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 18912a80469SChuck Lever * transport connects from colliding with writes. No congestion control 19012a80469SChuck Lever * is provided. 1911da177e4SLinus Torvalds */ 19243cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1931da177e4SLinus Torvalds { 19412a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 19512a80469SChuck Lever 19612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 19712a80469SChuck Lever if (task == xprt->snd_task) 19812a80469SChuck Lever return 1; 19912a80469SChuck Lever goto out_sleep; 20012a80469SChuck Lever } 201c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 202c544577dSTrond Myklebust goto out_unlock; 20312a80469SChuck Lever xprt->snd_task = task; 2044d4a76f3Sj223yang@asset.uwaterloo.ca 20512a80469SChuck Lever return 1; 20612a80469SChuck Lever 207c544577dSTrond Myklebust out_unlock: 208c544577dSTrond Myklebust xprt_clear_locked(xprt); 20912a80469SChuck Lever out_sleep: 21046121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 21112a80469SChuck Lever task->tk_pid, xprt); 212f05d54ecSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 21312a80469SChuck Lever task->tk_status = -EAGAIN; 21479c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 21512a80469SChuck Lever return 0; 21612a80469SChuck Lever } 21712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 21812a80469SChuck Lever 21975891f50STrond Myklebust static bool 22075891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 22175891f50STrond Myklebust { 22275891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 22375891f50STrond Myklebust } 22475891f50STrond Myklebust 22575891f50STrond Myklebust static void 22675891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 22775891f50STrond Myklebust { 22875891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 22975891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 23075891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 23175891f50STrond Myklebust rq_xmit)->rq_cong) 23275891f50STrond Myklebust return; 23375891f50STrond Myklebust } 23475891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 23575891f50STrond Myklebust } 23675891f50STrond Myklebust 23775891f50STrond Myklebust static void 23875891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 23975891f50STrond Myklebust { 24075891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 24175891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 24275891f50STrond Myklebust } 24375891f50STrond Myklebust 24412a80469SChuck Lever /* 24512a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 24612a80469SChuck Lever * @task: task that is requesting access to the transport 24712a80469SChuck Lever * 24812a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 24912a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 25012a80469SChuck Lever * woken up and given access to the transport. 25175891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 25212a80469SChuck Lever */ 25343cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 25412a80469SChuck Lever { 2551da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 2561da177e4SLinus Torvalds 2572226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2581da177e4SLinus Torvalds if (task == xprt->snd_task) 2591da177e4SLinus Torvalds return 1; 2601da177e4SLinus Torvalds goto out_sleep; 2611da177e4SLinus Torvalds } 26243cedbf0STrond Myklebust if (req == NULL) { 26343cedbf0STrond Myklebust xprt->snd_task = task; 26443cedbf0STrond Myklebust return 1; 26543cedbf0STrond Myklebust } 266c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 267c544577dSTrond Myklebust goto out_unlock; 26875891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 2691da177e4SLinus Torvalds xprt->snd_task = task; 2701da177e4SLinus Torvalds return 1; 2711da177e4SLinus Torvalds } 272c544577dSTrond Myklebust out_unlock: 273632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2741da177e4SLinus Torvalds out_sleep: 27546121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 276f05d54ecSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 2771da177e4SLinus Torvalds task->tk_status = -EAGAIN; 27879c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 28112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2821da177e4SLinus Torvalds 28312a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds int retval; 2861da177e4SLinus Torvalds 287bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 288bd79bc57STrond Myklebust return 1; 2894a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 29043cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2914a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2921da177e4SLinus Torvalds return retval; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 295961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2961da177e4SLinus Torvalds { 297961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 29849e9a890SChuck Lever 29949e9a890SChuck Lever xprt->snd_task = task; 300961a828dSTrond Myklebust return true; 301961a828dSTrond Myklebust } 302961a828dSTrond Myklebust 303961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 304961a828dSTrond Myklebust { 305961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 30649e9a890SChuck Lever return; 307c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 308c544577dSTrond Myklebust goto out_unlock; 309f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 310f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 311961a828dSTrond Myklebust return; 312c544577dSTrond Myklebust out_unlock: 313632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 31449e9a890SChuck Lever } 31549e9a890SChuck Lever 316961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 317961a828dSTrond Myklebust { 318961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 319961a828dSTrond Myklebust return; 320c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 321c544577dSTrond Myklebust goto out_unlock; 32275891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 323961a828dSTrond Myklebust goto out_unlock; 324f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 32575891f50STrond Myklebust __xprt_lock_write_func, xprt)) 326961a828dSTrond Myklebust return; 3271da177e4SLinus Torvalds out_unlock: 328632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 33149e9a890SChuck Lever /** 33249e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 33349e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 33449e9a890SChuck Lever * @task: task that is releasing access to the transport 33549e9a890SChuck Lever * 33649e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3371da177e4SLinus Torvalds */ 33849e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds if (xprt->snd_task == task) { 341632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3421da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds } 34512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3461da177e4SLinus Torvalds 34749e9a890SChuck Lever /** 34849e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 34949e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 35049e9a890SChuck Lever * @task: task that is releasing access to the transport 35149e9a890SChuck Lever * 35249e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 35349e9a890SChuck Lever * transport if the transport's congestion window allows it. 35449e9a890SChuck Lever */ 35549e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 35649e9a890SChuck Lever { 35749e9a890SChuck Lever if (xprt->snd_task == task) { 358632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 35949e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 36049e9a890SChuck Lever } 36149e9a890SChuck Lever } 36212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 36349e9a890SChuck Lever 36449e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3651da177e4SLinus Torvalds { 366bd79bc57STrond Myklebust if (xprt->snd_task != task) 367bd79bc57STrond Myklebust return; 3684a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 36949e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3704a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* 3741da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3751da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds static int 37875891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds if (req->rq_cong) 3811da177e4SLinus Torvalds return 1; 38246121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 38375891f50STrond Myklebust req->rq_task->tk_pid, xprt->cong, xprt->cwnd); 38475891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 38575891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 3861da177e4SLinus Torvalds return 0; 38775891f50STrond Myklebust } 3881da177e4SLinus Torvalds req->rq_cong = 1; 3891da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 3901da177e4SLinus Torvalds return 1; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 3951da177e4SLinus Torvalds * that has been sleeping due to congestion 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds static void 3981da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds if (!req->rq_cong) 4011da177e4SLinus Torvalds return; 4021da177e4SLinus Torvalds req->rq_cong = 0; 4031da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 40475891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 40549e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 40846c0ee8bSChuck Lever /** 40975891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 41075891f50STrond Myklebust * @xprt: pointer to transport 41175891f50STrond Myklebust * @req: pointer to RPC request 41275891f50STrond Myklebust * 41375891f50STrond Myklebust * Useful for transports that require congestion control. 41475891f50STrond Myklebust */ 41575891f50STrond Myklebust bool 41675891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 41775891f50STrond Myklebust { 41875891f50STrond Myklebust bool ret = false; 41975891f50STrond Myklebust 42075891f50STrond Myklebust if (req->rq_cong) 42175891f50STrond Myklebust return true; 42275891f50STrond Myklebust spin_lock_bh(&xprt->transport_lock); 42375891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 42475891f50STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 42575891f50STrond Myklebust return ret; 42675891f50STrond Myklebust } 42775891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 42875891f50STrond Myklebust 42975891f50STrond Myklebust /** 430a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 431a58dd398SChuck Lever * @task: RPC request that recently completed 432a58dd398SChuck Lever * 433a58dd398SChuck Lever * Useful for transports that require congestion control. 434a58dd398SChuck Lever */ 435a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 436a58dd398SChuck Lever { 437a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 438a4f0835cSTrond Myklebust 439a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 440a58dd398SChuck Lever } 44112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 442a58dd398SChuck Lever 44375891f50STrond Myklebust /* 44475891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 44575891f50STrond Myklebust * entry on xprt->sending 44675891f50STrond Myklebust */ 44775891f50STrond Myklebust static void 44875891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 44975891f50STrond Myklebust { 45075891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 45175891f50STrond Myklebust spin_lock_bh(&xprt->transport_lock); 45275891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 45375891f50STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 45475891f50STrond Myklebust } 45575891f50STrond Myklebust } 45675891f50STrond Myklebust 457a58dd398SChuck Lever /** 45846c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4596a24dfb6STrond Myklebust * @xprt: pointer to xprt 46046c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 46146c0ee8bSChuck Lever * @result: result code of completed RPC request 46246c0ee8bSChuck Lever * 4634f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4644f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4654f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4664f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4674f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4684f4cf5adSChuck Lever * 4694f4cf5adSChuck Lever * - a reply is received and 4704f4cf5adSChuck Lever * - a full number of requests are outstanding and 4714f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4721da177e4SLinus Torvalds */ 4736a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4741da177e4SLinus Torvalds { 47546c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 47646c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4791da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4801da177e4SLinus Torvalds * the result gets rounded properly. */ 4811da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4821da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4831da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 48449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4851da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4861da177e4SLinus Torvalds cwnd >>= 1; 4871da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4881da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4911da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4921da177e4SLinus Torvalds xprt->cwnd = cwnd; 49346c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4941da177e4SLinus Torvalds } 49512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4961da177e4SLinus Torvalds 49744fbac22SChuck Lever /** 49844fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 49944fbac22SChuck Lever * @xprt: transport with waiting tasks 50044fbac22SChuck Lever * @status: result code to plant in each task before waking it 50144fbac22SChuck Lever * 50244fbac22SChuck Lever */ 50344fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 50444fbac22SChuck Lever { 50544fbac22SChuck Lever if (status < 0) 50644fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 50744fbac22SChuck Lever else 50844fbac22SChuck Lever rpc_wake_up(&xprt->pending); 50944fbac22SChuck Lever } 51012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 51144fbac22SChuck Lever 512c7b2cae8SChuck Lever /** 513c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 514c544577dSTrond Myklebust * @xprt: transport 515a9a6b52eSTrond Myklebust * 516a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 517a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 518a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 519c7b2cae8SChuck Lever */ 520c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 521c7b2cae8SChuck Lever { 522c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 523c7b2cae8SChuck Lever } 52412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 525c7b2cae8SChuck Lever 526c544577dSTrond Myklebust static bool 527c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 528c544577dSTrond Myklebust { 529c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 530c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 531c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 532c544577dSTrond Myklebust "xprt %p\n", xprt); 533c544577dSTrond Myklebust return true; 534c544577dSTrond Myklebust } 535c544577dSTrond Myklebust return false; 536c544577dSTrond Myklebust } 537c544577dSTrond Myklebust 538c7b2cae8SChuck Lever /** 539c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 540c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 541c7b2cae8SChuck Lever * 542c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 543c7b2cae8SChuck Lever */ 544c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 545c7b2cae8SChuck Lever { 546c544577dSTrond Myklebust bool ret; 547c544577dSTrond Myklebust 548c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 549c544577dSTrond Myklebust return false; 550c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 551c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 552c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 553c544577dSTrond Myklebust return ret; 554c7b2cae8SChuck Lever } 55512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 556c7b2cae8SChuck Lever 5571da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5581da177e4SLinus Torvalds { 559ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5621da177e4SLinus Torvalds if (to->to_exponential) 5631da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5641da177e4SLinus Torvalds else 5651da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5661da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5671da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5681da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5719903cd1cSChuck Lever /** 5729903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5739903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5749903cd1cSChuck Lever * 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 579ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5801da177e4SLinus Torvalds int status = 0; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5831da177e4SLinus Torvalds if (to->to_exponential) 5841da177e4SLinus Torvalds req->rq_timeout <<= 1; 5851da177e4SLinus Torvalds else 5861da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5871da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5881da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5891da177e4SLinus Torvalds req->rq_retries++; 5901da177e4SLinus Torvalds } else { 5911da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5921da177e4SLinus Torvalds req->rq_retries = 0; 5931da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5941da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 5954a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 5961da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 5974a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5981da177e4SLinus Torvalds status = -ETIMEDOUT; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6021da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6031da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds return status; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 60865f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6091da177e4SLinus Torvalds { 61065f27f38SDavid Howells struct rpc_xprt *xprt = 61165f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 612a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 6131da177e4SLinus Torvalds 61466af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6154876cc77STrond Myklebust xprt->ops->close(xprt); 6161da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 61779234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 618a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6219903cd1cSChuck Lever /** 62262da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6239903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6249903cd1cSChuck Lever * 6251da177e4SLinus Torvalds */ 62662da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6294a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6301da177e4SLinus Torvalds xprt_clear_connected(xprt); 631c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 63227adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 6334a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6341da177e4SLinus Torvalds } 63562da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6361da177e4SLinus Torvalds 63766af1e55STrond Myklebust /** 63866af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 63966af1e55STrond Myklebust * @xprt: transport to disconnect 64066af1e55STrond Myklebust * 64166af1e55STrond Myklebust */ 64266af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 64366af1e55STrond Myklebust { 64466af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 64566af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 64666af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 64766af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 64866af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 64940a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6500445f92cSTrond Myklebust else if (xprt->snd_task) 6510445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 6520445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 65366af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 65466af1e55STrond Myklebust } 655e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 65666af1e55STrond Myklebust 6577f3a1d1eSTrond Myklebust static unsigned int 6587f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 6597f3a1d1eSTrond Myklebust { 6607f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 6617f3a1d1eSTrond Myklebust } 6627f3a1d1eSTrond Myklebust 6637f3a1d1eSTrond Myklebust static bool 6647f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 6657f3a1d1eSTrond Myklebust { 6667f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 6677f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 6687f3a1d1eSTrond Myklebust 6697f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 6707f3a1d1eSTrond Myklebust !xprt_connected(xprt); 6717f3a1d1eSTrond Myklebust } 6727f3a1d1eSTrond Myklebust 6737c1d71cfSTrond Myklebust /** 6747c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6757c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6767c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6777c1d71cfSTrond Myklebust * 6787c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6797c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6807c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6817c1d71cfSTrond Myklebust * a batch of RPC requests. 6827c1d71cfSTrond Myklebust * 6837c1d71cfSTrond Myklebust */ 6847c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6857c1d71cfSTrond Myklebust { 6867c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6877c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6887c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6897c1d71cfSTrond Myklebust goto out; 6902c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 6917c1d71cfSTrond Myklebust goto out; 6927c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6937c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6947c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 69540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6962a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6977c1d71cfSTrond Myklebust out: 6987c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6997c1d71cfSTrond Myklebust } 7007c1d71cfSTrond Myklebust 701ad3331acSTrond Myklebust static bool 702ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 703ad3331acSTrond Myklebust { 704ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 705ad3331acSTrond Myklebust } 706ad3331acSTrond Myklebust 707ad3331acSTrond Myklebust static void 708ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 709ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 710ad3331acSTrond Myklebust { 71195f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 712ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 713ad3331acSTrond Myklebust } 714ad3331acSTrond Myklebust 7151da177e4SLinus Torvalds static void 716ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 7171da177e4SLinus Torvalds { 718ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 7191da177e4SLinus Torvalds 7204a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 72195f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 7221da177e4SLinus Torvalds goto out_abort; 723ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 724ad3331acSTrond Myklebust xprt->last_used = jiffies; 7252226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7261da177e4SLinus Torvalds goto out_abort; 7274a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 72840a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7291da177e4SLinus Torvalds return; 7301da177e4SLinus Torvalds out_abort: 7314a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 734718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 735718ba5b8STrond Myklebust struct rpc_task *task, 736718ba5b8STrond Myklebust void *cookie) 737718ba5b8STrond Myklebust { 738718ba5b8STrond Myklebust bool ret = false; 739718ba5b8STrond Myklebust 740718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 741718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 742718ba5b8STrond Myklebust goto out; 743718ba5b8STrond Myklebust if (xprt->snd_task != task) 744718ba5b8STrond Myklebust goto out; 745718ba5b8STrond Myklebust xprt->snd_task = cookie; 746718ba5b8STrond Myklebust ret = true; 747718ba5b8STrond Myklebust out: 748718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 749718ba5b8STrond Myklebust return ret; 750718ba5b8STrond Myklebust } 751718ba5b8STrond Myklebust 752718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 753718ba5b8STrond Myklebust { 754718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 755718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 756718ba5b8STrond Myklebust goto out; 757718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 758718ba5b8STrond Myklebust goto out; 759718ba5b8STrond Myklebust xprt->snd_task =NULL; 760718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 761ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 762718ba5b8STrond Myklebust out: 763718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 76479234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 765718ba5b8STrond Myklebust } 766718ba5b8STrond Myklebust 7679903cd1cSChuck Lever /** 7689903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 7699903cd1cSChuck Lever * @task: RPC task that is requesting the connect 7701da177e4SLinus Torvalds * 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 7731da177e4SLinus Torvalds { 774ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7751da177e4SLinus Torvalds 77646121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 7771da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 7781da177e4SLinus Torvalds 779ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 78001d37c42STrond Myklebust task->tk_status = -EAGAIN; 7811da177e4SLinus Torvalds return; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7841da177e4SLinus Torvalds return; 785feb8ca37STrond Myklebust 786feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 787feb8ca37STrond Myklebust xprt->ops->close(xprt); 788feb8ca37STrond Myklebust 789718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 790a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7912c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 792abc13275STrond Myklebust rpc_sleep_on(&xprt->pending, task, NULL); 7930b9e7943STrond Myklebust 7940b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7950b9e7943STrond Myklebust return; 7960b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7970b9e7943STrond Myklebust return; 7980a9a4304STrond Myklebust /* Race breaker */ 7990a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 800262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 8011b092092STrond Myklebust xprt->ops->connect(xprt, task); 8020a9a4304STrond Myklebust } else { 8030a9a4304STrond Myklebust xprt_clear_connecting(xprt); 8040a9a4304STrond Myklebust task->tk_status = 0; 8050a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8060a9a4304STrond Myklebust } 8071da177e4SLinus Torvalds } 808718ba5b8STrond Myklebust xprt_release_write(xprt, task); 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 81195f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 81295f7691dSTrond Myklebust XID_RB_EQUAL, 81395f7691dSTrond Myklebust XID_RB_LEFT, 81495f7691dSTrond Myklebust XID_RB_RIGHT, 81595f7691dSTrond Myklebust }; 81695f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 81795f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 81895f7691dSTrond Myklebust { 81995f7691dSTrond Myklebust if (xid1 == xid2) 82095f7691dSTrond Myklebust return XID_RB_EQUAL; 82195f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 82295f7691dSTrond Myklebust return XID_RB_LEFT; 82395f7691dSTrond Myklebust return XID_RB_RIGHT; 82495f7691dSTrond Myklebust } 82595f7691dSTrond Myklebust 82695f7691dSTrond Myklebust static struct rpc_rqst * 82795f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 82895f7691dSTrond Myklebust { 82995f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 83095f7691dSTrond Myklebust struct rpc_rqst *req; 83195f7691dSTrond Myklebust 83295f7691dSTrond Myklebust while (n != NULL) { 83395f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 83495f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 83595f7691dSTrond Myklebust case XID_RB_LEFT: 83695f7691dSTrond Myklebust n = n->rb_left; 83795f7691dSTrond Myklebust break; 83895f7691dSTrond Myklebust case XID_RB_RIGHT: 83995f7691dSTrond Myklebust n = n->rb_right; 84095f7691dSTrond Myklebust break; 84195f7691dSTrond Myklebust case XID_RB_EQUAL: 84295f7691dSTrond Myklebust return req; 84395f7691dSTrond Myklebust } 84495f7691dSTrond Myklebust } 84595f7691dSTrond Myklebust return NULL; 84695f7691dSTrond Myklebust } 84795f7691dSTrond Myklebust 84895f7691dSTrond Myklebust static void 84995f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 85095f7691dSTrond Myklebust { 85195f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 85295f7691dSTrond Myklebust struct rb_node *n = NULL; 85395f7691dSTrond Myklebust struct rpc_rqst *req; 85495f7691dSTrond Myklebust 85595f7691dSTrond Myklebust while (*p != NULL) { 85695f7691dSTrond Myklebust n = *p; 85795f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 85895f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 85995f7691dSTrond Myklebust case XID_RB_LEFT: 86095f7691dSTrond Myklebust p = &n->rb_left; 86195f7691dSTrond Myklebust break; 86295f7691dSTrond Myklebust case XID_RB_RIGHT: 86395f7691dSTrond Myklebust p = &n->rb_right; 86495f7691dSTrond Myklebust break; 86595f7691dSTrond Myklebust case XID_RB_EQUAL: 86695f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 86795f7691dSTrond Myklebust return; 86895f7691dSTrond Myklebust } 86995f7691dSTrond Myklebust } 87095f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 87195f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 87295f7691dSTrond Myklebust } 87395f7691dSTrond Myklebust 87495f7691dSTrond Myklebust static void 87595f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 87695f7691dSTrond Myklebust { 87795f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 87895f7691dSTrond Myklebust } 87995f7691dSTrond Myklebust 8809903cd1cSChuck Lever /** 8819903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 8829903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 8839903cd1cSChuck Lever * @xid: RPC XID of incoming reply 8849903cd1cSChuck Lever * 88575c84151STrond Myklebust * Caller holds xprt->queue_lock. 8861da177e4SLinus Torvalds */ 887d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 8881da177e4SLinus Torvalds { 8898f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 8901da177e4SLinus Torvalds 89195f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 89295f7691dSTrond Myklebust if (entry != NULL) { 8933705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 8940b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 895262ca07dSChuck Lever return entry; 8963705ad64SJeff Layton } 89746121cf7SChuck Lever 89846121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 89946121cf7SChuck Lever ntohl(xid)); 9003705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 901262ca07dSChuck Lever xprt->stat.bad_xids++; 902262ca07dSChuck Lever return NULL; 9031da177e4SLinus Torvalds } 90412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 9051da177e4SLinus Torvalds 906cf9946cdSTrond Myklebust static bool 907cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 908cf9946cdSTrond Myklebust { 909cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 910cf9946cdSTrond Myklebust } 911cf9946cdSTrond Myklebust 912729749bbSTrond Myklebust /** 913729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 914729749bbSTrond Myklebust * @req: Request to pin 915729749bbSTrond Myklebust * 916729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 917cf9946cdSTrond Myklebust * so should be holding the xprt receive lock. 918729749bbSTrond Myklebust */ 919729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 920729749bbSTrond Myklebust { 921cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 922729749bbSTrond Myklebust } 9239590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 924729749bbSTrond Myklebust 925729749bbSTrond Myklebust /** 926729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 927729749bbSTrond Myklebust * @req: Request to pin 928729749bbSTrond Myklebust * 929cf9946cdSTrond Myklebust * Caller should be holding the xprt receive lock. 930729749bbSTrond Myklebust */ 931729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 932729749bbSTrond Myklebust { 933cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 934cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 935cf9946cdSTrond Myklebust return; 936cf9946cdSTrond Myklebust } 937cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 938cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 939729749bbSTrond Myklebust } 9409590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 941729749bbSTrond Myklebust 942729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 943729749bbSTrond Myklebust { 944cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 945729749bbSTrond Myklebust } 946729749bbSTrond Myklebust 947edc81dcdSTrond Myklebust static bool 948edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 949edc81dcdSTrond Myklebust { 950edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 951edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 952edc81dcdSTrond Myklebust } 953edc81dcdSTrond Myklebust 954edc81dcdSTrond Myklebust static bool 955edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 956edc81dcdSTrond Myklebust { 957edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 958edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 959edc81dcdSTrond Myklebust } 960edc81dcdSTrond Myklebust 961edc81dcdSTrond Myklebust /** 962edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 963edc81dcdSTrond Myklebust * @task: RPC task 964edc81dcdSTrond Myklebust * 965edc81dcdSTrond Myklebust */ 966edc81dcdSTrond Myklebust void 967edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 968edc81dcdSTrond Myklebust { 969edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 970edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 971edc81dcdSTrond Myklebust 972edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 973edc81dcdSTrond Myklebust return; 974edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 975edc81dcdSTrond Myklebust 976edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 977edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 978edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 979edc81dcdSTrond Myklebust 980edc81dcdSTrond Myklebust /* Add request to the receive list */ 98195f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 982edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 983edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 984edc81dcdSTrond Myklebust 985edc81dcdSTrond Myklebust xprt_reset_majortimeo(req); 986edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 987edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 988edc81dcdSTrond Myklebust } 989edc81dcdSTrond Myklebust 990edc81dcdSTrond Myklebust /** 991edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 992edc81dcdSTrond Myklebust * @task: RPC task 993edc81dcdSTrond Myklebust * 994edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 995edc81dcdSTrond Myklebust */ 996edc81dcdSTrond Myklebust static void 997edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 998edc81dcdSTrond Myklebust { 99995f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 100095f7691dSTrond Myklebust 1001edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 100295f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1003edc81dcdSTrond Myklebust } 1004edc81dcdSTrond Myklebust 1005ecd465eeSChuck Lever /** 1006ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1007ecd465eeSChuck Lever * @task: RPC request that recently completed 1008ecd465eeSChuck Lever * 100975c84151STrond Myklebust * Caller holds xprt->queue_lock. 1010ecd465eeSChuck Lever */ 1011ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 10121da177e4SLinus Torvalds { 10131570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 10141570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 101595c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1016d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 10171570c1e4SChuck Lever 10181da177e4SLinus Torvalds if (timer) { 10191da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1020ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 10211570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds } 1024ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 10251da177e4SLinus Torvalds 10261570c1e4SChuck Lever /** 10271570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 10281570c1e4SChuck Lever * @task: RPC request that recently completed 10291570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 10301570c1e4SChuck Lever * 103175c84151STrond Myklebust * Caller holds xprt->queue_lock. 10321570c1e4SChuck Lever */ 10331570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 10341570c1e4SChuck Lever { 10351570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1036fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 10371da177e4SLinus Torvalds 10381570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 10391570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 10403705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 10411da177e4SLinus Torvalds 1042fda13939STrond Myklebust xprt->stat.recvs++; 1043ef759a2eSChuck Lever 10441e799b67STrond Myklebust req->rq_private_buf.len = copied; 1045dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1046dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 104743ac3f29STrond Myklebust smp_wmb(); 1048dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1049edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1050fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 10511da177e4SLinus Torvalds } 105212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 10531da177e4SLinus Torvalds 105446c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 10551da177e4SLinus Torvalds { 10561da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 10571da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 10581da177e4SLinus Torvalds 10595d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 10605d00837bSTrond Myklebust return; 106146c0ee8bSChuck Lever 106282476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1063dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 106446c0ee8bSChuck Lever if (xprt->ops->timer) 10656a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 10665d00837bSTrond Myklebust } else 10675d00837bSTrond Myklebust task->tk_status = 0; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10709903cd1cSChuck Lever /** 10718ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_def - wait for reply 10728ba6a92dSTrond Myklebust * @task: pointer to rpc_task 10738ba6a92dSTrond Myklebust * 10748ba6a92dSTrond Myklebust * Set a request's retransmit timeout based on the transport's 10758ba6a92dSTrond Myklebust * default timeout parameters. Used by transports that don't adjust 10768ba6a92dSTrond Myklebust * the retransmit timeout based on round-trip time estimation, 10778ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 10788ba6a92dSTrond Myklebust */ 10798ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task) 10808ba6a92dSTrond Myklebust { 10818ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 10828ba6a92dSTrond Myklebust 10838ba6a92dSTrond Myklebust task->tk_timeout = req->rq_timeout; 10848ba6a92dSTrond Myklebust rpc_sleep_on(&req->rq_xprt->pending, task, xprt_timer); 10858ba6a92dSTrond Myklebust } 10868ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def); 10878ba6a92dSTrond Myklebust 10888ba6a92dSTrond Myklebust /** 10898ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator 10908ba6a92dSTrond Myklebust * @task: pointer to rpc_task 10918ba6a92dSTrond Myklebust * 10928ba6a92dSTrond Myklebust * Set a request's retransmit timeout using the RTT estimator, 10938ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 10948ba6a92dSTrond Myklebust */ 10958ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task) 10968ba6a92dSTrond Myklebust { 10978ba6a92dSTrond Myklebust int timer = task->tk_msg.rpc_proc->p_timer; 10988ba6a92dSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 10998ba6a92dSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 11008ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11018ba6a92dSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 11028ba6a92dSTrond Myklebust 11038ba6a92dSTrond Myklebust task->tk_timeout = rpc_calc_rto(rtt, timer); 11048ba6a92dSTrond Myklebust task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 11058ba6a92dSTrond Myklebust if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 11068ba6a92dSTrond Myklebust task->tk_timeout = max_timeout; 11078ba6a92dSTrond Myklebust rpc_sleep_on(&req->rq_xprt->pending, task, xprt_timer); 11088ba6a92dSTrond Myklebust } 11098ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt); 11108ba6a92dSTrond Myklebust 11118ba6a92dSTrond Myklebust /** 11127f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 11137f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 11147f3a1d1eSTrond Myklebust * 11157f3a1d1eSTrond Myklebust */ 11167f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 11177f3a1d1eSTrond Myklebust { 11187f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11197f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11207f3a1d1eSTrond Myklebust 11217f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 11227f3a1d1eSTrond Myklebust return; 11237f3a1d1eSTrond Myklebust /* 11247f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 11257f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 11267f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 11277f3a1d1eSTrond Myklebust */ 11287f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 11297f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 11308ba6a92dSTrond Myklebust xprt->ops->wait_for_reply_request(task); 11317f3a1d1eSTrond Myklebust /* 11327f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 11337f3a1d1eSTrond Myklebust * connection was dropped in case the call to 11347f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 11357f3a1d1eSTrond Myklebust */ 11367f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 11377f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 11387f3a1d1eSTrond Myklebust task, -ENOTCONN); 11397f3a1d1eSTrond Myklebust } 11407f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 11417f3a1d1eSTrond Myklebust } 11427f3a1d1eSTrond Myklebust 1143944b0429STrond Myklebust static bool 1144944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1145944b0429STrond Myklebust { 1146762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1147944b0429STrond Myklebust } 1148944b0429STrond Myklebust 1149944b0429STrond Myklebust /** 1150944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1151944b0429STrond Myklebust * @task: pointer to rpc_task 1152944b0429STrond Myklebust * 1153944b0429STrond Myklebust * Add a task to the transmission queue. 1154944b0429STrond Myklebust */ 1155944b0429STrond Myklebust void 1156944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1157944b0429STrond Myklebust { 1158918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1159944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1160944b0429STrond Myklebust 1161944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1162e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1163944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 116475891f50STrond Myklebust /* 116575891f50STrond Myklebust * Requests that carry congestion control credits are added 116675891f50STrond Myklebust * to the head of the list to avoid starvation issues. 116775891f50STrond Myklebust */ 116875891f50STrond Myklebust if (req->rq_cong) { 116975891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 117075891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 117175891f50STrond Myklebust if (pos->rq_cong) 117275891f50STrond Myklebust continue; 117375891f50STrond Myklebust /* Note: req is added _before_ pos */ 117475891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 117575891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 11760c77668dSChuck Lever trace_xprt_enq_xmit(task, 1); 117775891f50STrond Myklebust goto out; 117875891f50STrond Myklebust } 117986aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 118086aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 118186aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 118286aeee0eSTrond Myklebust continue; 118386aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 118486aeee0eSTrond Myklebust continue; 118586aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 118686aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 118786aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 11880c77668dSChuck Lever trace_xprt_enq_xmit(task, 2); 118986aeee0eSTrond Myklebust goto out; 119086aeee0eSTrond Myklebust } 1191deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1192918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1193918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1194918f3c1fSTrond Myklebust continue; 1195918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1196918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 11970c77668dSChuck Lever trace_xprt_enq_xmit(task, 3); 1198918f3c1fSTrond Myklebust goto out; 1199918f3c1fSTrond Myklebust } 120075891f50STrond Myklebust } 1201944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1202918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12030c77668dSChuck Lever trace_xprt_enq_xmit(task, 4); 1204918f3c1fSTrond Myklebust out: 1205944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1206944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1207944b0429STrond Myklebust } 1208944b0429STrond Myklebust } 1209944b0429STrond Myklebust 1210944b0429STrond Myklebust /** 1211944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1212944b0429STrond Myklebust * @task: pointer to rpc_task 1213944b0429STrond Myklebust * 1214944b0429STrond Myklebust * Remove a task from the transmission queue 1215944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1216944b0429STrond Myklebust */ 1217944b0429STrond Myklebust static void 1218944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1219944b0429STrond Myklebust { 1220918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1221918f3c1fSTrond Myklebust 1222918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1223918f3c1fSTrond Myklebust return; 1224918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1225918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1226918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1227918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1228918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1229918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1230918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1231918f3c1fSTrond Myklebust } 1232918f3c1fSTrond Myklebust } else 1233918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1234944b0429STrond Myklebust } 1235944b0429STrond Myklebust 1236944b0429STrond Myklebust /** 1237944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1238944b0429STrond Myklebust * @task: pointer to rpc_task 1239944b0429STrond Myklebust * 1240944b0429STrond Myklebust * Remove a task from the transmission queue 1241944b0429STrond Myklebust */ 1242944b0429STrond Myklebust static void 1243944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1244944b0429STrond Myklebust { 1245944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1246944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1247944b0429STrond Myklebust 1248944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1249944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1250944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1251944b0429STrond Myklebust } 1252944b0429STrond Myklebust 12537f3a1d1eSTrond Myklebust /** 12549d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 12559d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 12569d96acbcSTrond Myklebust * 12579d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 12589d96acbcSTrond Myklebust * the request for transmission or receive. 12599d96acbcSTrond Myklebust */ 12609d96acbcSTrond Myklebust void 12619d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 12629d96acbcSTrond Myklebust { 12639d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12649d96acbcSTrond Myklebust 12659d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 12669d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 12679d96acbcSTrond Myklebust } 12689d96acbcSTrond Myklebust 12699d96acbcSTrond Myklebust /** 1270762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1271762e4e67STrond Myklebust * @task: pointer to rpc_task 1272762e4e67STrond Myklebust * 1273762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1274762e4e67STrond Myklebust */ 1275762e4e67STrond Myklebust bool 1276762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1277762e4e67STrond Myklebust { 1278762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1279762e4e67STrond Myklebust } 1280762e4e67STrond Myklebust 1281762e4e67STrond Myklebust /** 12829903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 12839903cd1cSChuck Lever * @task: RPC task about to send a request 12849903cd1cSChuck Lever * 12851da177e4SLinus Torvalds */ 128690051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 12871da177e4SLinus Torvalds { 12881da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12891da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 12901da177e4SLinus Torvalds 129146121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 12921da177e4SLinus Torvalds 12935f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 12945f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1295944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 12965f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 12975f2f6bd9STrond Myklebust task, 0); 12985f2f6bd9STrond Myklebust return false; 12995f2f6bd9STrond Myklebust 13008a19a0b6STrond Myklebust } 13015f2f6bd9STrond Myklebust return true; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 1304e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 13055e5ce5beSTrond Myklebust { 1306343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 13075e5ce5beSTrond Myklebust } 13085e5ce5beSTrond Myklebust 13099903cd1cSChuck Lever /** 131089f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 131189f90fe1STrond Myklebust * @req: pointer to request to transmit 131289f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 13139903cd1cSChuck Lever * 131489f90fe1STrond Myklebust * This performs the transmission of a single request. 131589f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 131689f90fe1STrond Myklebust * does need to be pinned. 131789f90fe1STrond Myklebust * Returns '0' on success. 13189903cd1cSChuck Lever */ 131989f90fe1STrond Myklebust static int 132089f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 13211da177e4SLinus Torvalds { 13221da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 132389f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 132490d91b0cSTrond Myklebust unsigned int connect_cookie; 1325dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1326ff699ea8SChuck Lever int status; 13271da177e4SLinus Torvalds 1328edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 132989f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 133089f90fe1STrond Myklebust status = 0; 1331944b0429STrond Myklebust goto out_dequeue; 133289f90fe1STrond Myklebust } 13333021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1334edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 133589f90fe1STrond Myklebust status = -EBADMSG; 1336944b0429STrond Myklebust goto out_dequeue; 13373021a5bbSTrond Myklebust } 1338a79f194aSTrond Myklebust if (task->tk_ops->rpc_call_prepare_transmit) { 1339a79f194aSTrond Myklebust task->tk_ops->rpc_call_prepare_transmit(task, 1340a79f194aSTrond Myklebust task->tk_calldata); 1341a79f194aSTrond Myklebust status = task->tk_status; 1342a79f194aSTrond Myklebust if (status < 0) 1343a79f194aSTrond Myklebust goto out_dequeue; 1344a79f194aSTrond Myklebust } 1345ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) { 1346ae67bd38STrond Myklebust status = -ERESTARTSYS; 1347ae67bd38STrond Myklebust goto out_dequeue; 1348ae67bd38STrond Myklebust } 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 1351dcbbeda8STrond Myklebust /* 1352dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1353dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1354dcbbeda8STrond Myklebust * reply to the first transmission. 1355dcbbeda8STrond Myklebust */ 1356dcbbeda8STrond Myklebust req->rq_ntrans++; 1357dcbbeda8STrond Myklebust 135890d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1359adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1360c8485e4dSTrond Myklebust if (status != 0) { 1361dcbbeda8STrond Myklebust req->rq_ntrans--; 13620c77668dSChuck Lever trace_xprt_transmit(req, status); 136389f90fe1STrond Myklebust return status; 1364c8485e4dSTrond Myklebust } 13657ebbbc6eSTrond Myklebust 1366dcbbeda8STrond Myklebust if (is_retrans) 1367dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1368dcbbeda8STrond Myklebust 13694a068258SChuck Lever xprt_inject_disconnect(xprt); 1370c8485e4dSTrond Myklebust 1371468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1372fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 1373262ca07dSChuck Lever 1374262ca07dSChuck Lever xprt->stat.sends++; 1375262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1376262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 137715a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 137815a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1379fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 138090d91b0cSTrond Myklebust 138190d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1382944b0429STrond Myklebust out_dequeue: 13830c77668dSChuck Lever trace_xprt_transmit(req, status); 1384944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 138589f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 138689f90fe1STrond Myklebust return status; 138789f90fe1STrond Myklebust } 138889f90fe1STrond Myklebust 138989f90fe1STrond Myklebust /** 139089f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 139189f90fe1STrond Myklebust * @task: controlling RPC task 139289f90fe1STrond Myklebust * 139389f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 139489f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 139589f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 139689f90fe1STrond Myklebust * received a reply. 139789f90fe1STrond Myklebust */ 139889f90fe1STrond Myklebust void 139989f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 140089f90fe1STrond Myklebust { 140189f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 140289f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 140389f90fe1STrond Myklebust int status; 140489f90fe1STrond Myklebust 140589f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 140689f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 140789f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 140889f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 140989f90fe1STrond Myklebust xprt_pin_rqst(next); 141089f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 141189f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 141289f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 141389f90fe1STrond Myklebust status = 0; 141489f90fe1STrond Myklebust cond_resched(); 141589f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 141689f90fe1STrond Myklebust xprt_unpin_rqst(next); 141789f90fe1STrond Myklebust if (status == 0) { 141889f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 141989f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 142089f90fe1STrond Myklebust continue; 1421c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 142289f90fe1STrond Myklebust task->tk_status = status; 142389f90fe1STrond Myklebust break; 142489f90fe1STrond Myklebust } 142589f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 1428ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1429ba60eb25STrond Myklebust { 1430ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1431ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1432ba60eb25STrond Myklebust } 1433ba60eb25STrond Myklebust 1434ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1435ba60eb25STrond Myklebust { 1436ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1437ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1438ba60eb25STrond Myklebust } 1439ba60eb25STrond Myklebust 1440ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1441ba60eb25STrond Myklebust { 1442ba60eb25STrond Myklebust bool ret = false; 1443ba60eb25STrond Myklebust 1444ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1445ba60eb25STrond Myklebust goto out; 1446ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1447ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1448ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1449ba60eb25STrond Myklebust ret = true; 1450ba60eb25STrond Myklebust } 1451ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1452ba60eb25STrond Myklebust out: 1453ba60eb25STrond Myklebust return ret; 1454ba60eb25STrond Myklebust } 1455ba60eb25STrond Myklebust 145692ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1457d9ba131dSTrond Myklebust { 1458d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1459d9ba131dSTrond Myklebust 1460ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1461d9ba131dSTrond Myklebust goto out; 1462ff699ea8SChuck Lever ++xprt->num_reqs; 146392ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 146492ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 146592ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1466d9ba131dSTrond Myklebust if (req != NULL) 1467d9ba131dSTrond Myklebust goto out; 1468ff699ea8SChuck Lever --xprt->num_reqs; 1469d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1470d9ba131dSTrond Myklebust out: 1471d9ba131dSTrond Myklebust return req; 1472d9ba131dSTrond Myklebust } 1473d9ba131dSTrond Myklebust 1474d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1475d9ba131dSTrond Myklebust { 1476ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1477ff699ea8SChuck Lever --xprt->num_reqs; 1478d9ba131dSTrond Myklebust kfree(req); 1479d9ba131dSTrond Myklebust return true; 1480d9ba131dSTrond Myklebust } 1481d9ba131dSTrond Myklebust return false; 1482d9ba131dSTrond Myklebust } 1483d9ba131dSTrond Myklebust 1484f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 14851da177e4SLinus Torvalds { 1486d9ba131dSTrond Myklebust struct rpc_rqst *req; 14871da177e4SLinus Torvalds 1488f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 14891da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1490d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1491d9ba131dSTrond Myklebust list_del(&req->rq_list); 1492d9ba131dSTrond Myklebust goto out_init_req; 1493d9ba131dSTrond Myklebust } 149492ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1495d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1496d9ba131dSTrond Myklebust goto out_init_req; 1497d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1498d9ba131dSTrond Myklebust case -ENOMEM: 1499d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1500d9ba131dSTrond Myklebust "failed! Retrying\n"); 15011afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1502d9ba131dSTrond Myklebust break; 1503d9ba131dSTrond Myklebust case -EAGAIN: 1504ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1505d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1506e9d47639SGustavo A. R. Silva /* fall through */ 15071afeaf5cSTrond Myklebust default: 1508d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 15091afeaf5cSTrond Myklebust } 1510f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1511d9ba131dSTrond Myklebust return; 1512d9ba131dSTrond Myklebust out_init_req: 1513ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1514ff699ea8SChuck Lever xprt->num_reqs); 151537ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 151637ac86c3SChuck Lever 1517d9ba131dSTrond Myklebust task->tk_status = 0; 15181da177e4SLinus Torvalds task->tk_rqstp = req; 15191da177e4SLinus Torvalds } 1520f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1521f39c1bfbSTrond Myklebust 1522a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1523ee5ebe85STrond Myklebust { 1524ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1525c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1526c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1527ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1528c25573b5STrond Myklebust } 1529ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1530ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1531ee5ebe85STrond Myklebust } 1532a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1533ee5ebe85STrond Myklebust 153421de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 153521de0a95STrond Myklebust { 153621de0a95STrond Myklebust struct rpc_rqst *req; 153721de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 153821de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 153921de0a95STrond Myklebust list_del(&req->rq_list); 154021de0a95STrond Myklebust kfree(req); 154121de0a95STrond Myklebust } 154221de0a95STrond Myklebust } 154321de0a95STrond Myklebust 1544d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1545d9ba131dSTrond Myklebust unsigned int num_prealloc, 1546d9ba131dSTrond Myklebust unsigned int max_alloc) 1547bd1722d4SPavel Emelyanov { 1548bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 154921de0a95STrond Myklebust struct rpc_rqst *req; 155021de0a95STrond Myklebust int i; 1551bd1722d4SPavel Emelyanov 1552bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1553bd1722d4SPavel Emelyanov if (xprt == NULL) 1554bd1722d4SPavel Emelyanov goto out; 1555bd1722d4SPavel Emelyanov 155621de0a95STrond Myklebust xprt_init(xprt, net); 155721de0a95STrond Myklebust 155821de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 155921de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 156021de0a95STrond Myklebust if (!req) 15618313164cSwangweidong goto out_free; 156221de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 156321de0a95STrond Myklebust } 1564d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1565d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1566d9ba131dSTrond Myklebust else 156721de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1568d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1569ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1570bd1722d4SPavel Emelyanov 1571bd1722d4SPavel Emelyanov return xprt; 1572bd1722d4SPavel Emelyanov 1573bd1722d4SPavel Emelyanov out_free: 157421de0a95STrond Myklebust xprt_free(xprt); 1575bd1722d4SPavel Emelyanov out: 1576bd1722d4SPavel Emelyanov return NULL; 1577bd1722d4SPavel Emelyanov } 1578bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1579bd1722d4SPavel Emelyanov 1580e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1581e204e621SPavel Emelyanov { 158237aa2133SPavel Emelyanov put_net(xprt->xprt_net); 158321de0a95STrond Myklebust xprt_free_all_slots(xprt); 1584fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1585e204e621SPavel Emelyanov } 1586e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1587e204e621SPavel Emelyanov 1588902c5887STrond Myklebust static void 1589902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1590902c5887STrond Myklebust { 1591902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1592902c5887STrond Myklebust } 1593902c5887STrond Myklebust 15949dc6edcfSTrond Myklebust static __be32 15959dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 15969dc6edcfSTrond Myklebust { 15979dc6edcfSTrond Myklebust __be32 xid; 15989dc6edcfSTrond Myklebust 15999dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 16009dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 16019dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 16029dc6edcfSTrond Myklebust return xid; 16039dc6edcfSTrond Myklebust } 16049dc6edcfSTrond Myklebust 16059dc6edcfSTrond Myklebust static void 16069dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 16079dc6edcfSTrond Myklebust { 16089dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 16099dc6edcfSTrond Myklebust } 16109dc6edcfSTrond Myklebust 16119dc6edcfSTrond Myklebust static void 16129dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 16139dc6edcfSTrond Myklebust { 16149dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16159dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 16169dc6edcfSTrond Myklebust 16179dc6edcfSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 16189dc6edcfSTrond Myklebust req->rq_task = task; 16199dc6edcfSTrond Myklebust req->rq_xprt = xprt; 16209dc6edcfSTrond Myklebust req->rq_buffer = NULL; 16219dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1622902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 16239dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 16249dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 16259dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 16269dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 162771700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 162871700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 16299dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 16309dc6edcfSTrond Myklebust xprt_reset_majortimeo(req); 16319dc6edcfSTrond Myklebust dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 16329dc6edcfSTrond Myklebust req, ntohl(req->rq_xid)); 16339dc6edcfSTrond Myklebust } 16349dc6edcfSTrond Myklebust 16359dc6edcfSTrond Myklebust static void 16369dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 16379dc6edcfSTrond Myklebust { 16389dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 16399dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 16409dc6edcfSTrond Myklebust xprt_request_init(task); 16419dc6edcfSTrond Myklebust } 16429dc6edcfSTrond Myklebust 16439903cd1cSChuck Lever /** 16449903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 16459903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 16469903cd1cSChuck Lever * 1647ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1648ba60eb25STrond Myklebust * slots are available, place the task on the transport's 16499903cd1cSChuck Lever * backlog queue. 16509903cd1cSChuck Lever */ 16519903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 16521da177e4SLinus Torvalds { 1653fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16541da177e4SLinus Torvalds 165543cedbf0STrond Myklebust task->tk_status = 0; 165643cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 165743cedbf0STrond Myklebust return; 165843cedbf0STrond Myklebust 165943cedbf0STrond Myklebust task->tk_timeout = 0; 166043cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1661ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 16629dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1663ba60eb25STrond Myklebust } 1664ba60eb25STrond Myklebust 1665ba60eb25STrond Myklebust /** 1666ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1667ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1668ba60eb25STrond Myklebust * 1669ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1670ba60eb25STrond Myklebust * backlog queue. 1671ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1672ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1673ba60eb25STrond Myklebust */ 1674ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1675ba60eb25STrond Myklebust { 1676fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1677ba60eb25STrond Myklebust 1678ba60eb25STrond Myklebust task->tk_status = 0; 1679ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1680ba60eb25STrond Myklebust return; 1681ba60eb25STrond Myklebust 1682ba60eb25STrond Myklebust task->tk_timeout = 0; 1683ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 16849dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 1687edc81dcdSTrond Myklebust static void 1688edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req) 1689edc81dcdSTrond Myklebust { 1690edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1691edc81dcdSTrond Myklebust 1692944b0429STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1693944b0429STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1694edc81dcdSTrond Myklebust xprt_is_pinned_rqst(req)) { 1695edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1696944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1697edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1698edc81dcdSTrond Myklebust while (xprt_is_pinned_rqst(req)) { 1699edc81dcdSTrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1700edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1701edc81dcdSTrond Myklebust xprt_wait_on_pinned_rqst(req); 1702edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1703edc81dcdSTrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1704edc81dcdSTrond Myklebust } 1705edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1706edc81dcdSTrond Myklebust } 1707edc81dcdSTrond Myklebust } 1708edc81dcdSTrond Myklebust 17099903cd1cSChuck Lever /** 17109903cd1cSChuck Lever * xprt_release - release an RPC request slot 17119903cd1cSChuck Lever * @task: task which is finished with the slot 17129903cd1cSChuck Lever * 17131da177e4SLinus Torvalds */ 17149903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 17151da177e4SLinus Torvalds { 171655ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 171787ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17181da177e4SLinus Torvalds 171987ed5003STrond Myklebust if (req == NULL) { 172087ed5003STrond Myklebust if (task->tk_client) { 1721fb43d172STrond Myklebust xprt = task->tk_xprt; 172287ed5003STrond Myklebust xprt_release_write(xprt, task); 172387ed5003STrond Myklebust } 17241da177e4SLinus Torvalds return; 172587ed5003STrond Myklebust } 172655ae1aabSRicardo Labiaga 172755ae1aabSRicardo Labiaga xprt = req->rq_xprt; 17280a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 17290a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 17300a702195SWeston Andros Adamson else if (task->tk_client) 17310a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 1732edc81dcdSTrond Myklebust xprt_request_dequeue_all(task, req); 17334a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 173449e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1735a58dd398SChuck Lever if (xprt->ops->release_request) 1736a58dd398SChuck Lever xprt->ops->release_request(task); 17371da177e4SLinus Torvalds xprt->last_used = jiffies; 1738ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 17394a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1740ee5ebe85STrond Myklebust if (req->rq_buffer) 17413435c74aSChuck Lever xprt->ops->buf_free(task); 17424a068258SChuck Lever xprt_inject_disconnect(xprt); 17439d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 17440472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1745a17c2153STrond Myklebust if (req->rq_cred != NULL) 1746a17c2153STrond Myklebust put_rpccred(req->rq_cred); 17471da177e4SLinus Torvalds task->tk_rqstp = NULL; 1748ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1749ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 175055ae1aabSRicardo Labiaga 175146121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1752ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1753a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1754ee5ebe85STrond Myklebust else 1755c9acb42eSTrond Myklebust xprt_free_bc_request(req); 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 1758902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1759902c5887STrond Myklebust void 1760902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1761902c5887STrond Myklebust { 1762902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1763902c5887STrond Myklebust 1764902c5887STrond Myklebust task->tk_rqstp = req; 1765902c5887STrond Myklebust req->rq_task = task; 1766902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1767902c5887STrond Myklebust /* 1768902c5887STrond Myklebust * Set up the xdr_buf length. 1769902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1770902c5887STrond Myklebust */ 1771902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1772902c5887STrond Myklebust xbufp->tail[0].iov_len; 1773902c5887STrond Myklebust } 1774902c5887STrond Myklebust #endif 1775902c5887STrond Myklebust 177621de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1777c2866763SChuck Lever { 177830c5116bSTrond Myklebust kref_init(&xprt->kref); 1779c2866763SChuck Lever 1780c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1781c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 178275c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1783c2866763SChuck Lever 1784c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 178595f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1786944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 17879e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1788f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1789f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 17909e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 179180b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1792f9acac1aSRicardo Labiaga 1793c2866763SChuck Lever xprt->last_used = jiffies; 1794c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1795a509050bSChuck Lever xprt->bind_index = 0; 1796c2866763SChuck Lever 1797c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1798c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 179979c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1800c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1801c2866763SChuck Lever 1802c2866763SChuck Lever xprt_init_xid(xprt); 1803c2866763SChuck Lever 180421de0a95STrond Myklebust xprt->xprt_net = get_net(net); 18058d9266ffSTrond Myklebust } 18068d9266ffSTrond Myklebust 18078d9266ffSTrond Myklebust /** 18088d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 18098d9266ffSTrond Myklebust * @args: rpc transport creation arguments 18108d9266ffSTrond Myklebust * 18118d9266ffSTrond Myklebust */ 18128d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 18138d9266ffSTrond Myklebust { 18148d9266ffSTrond Myklebust struct rpc_xprt *xprt; 18158d9266ffSTrond Myklebust struct xprt_class *t; 18168d9266ffSTrond Myklebust 18178d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 18188d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 18198d9266ffSTrond Myklebust if (t->ident == args->ident) { 18208d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18218d9266ffSTrond Myklebust goto found; 18228d9266ffSTrond Myklebust } 18238d9266ffSTrond Myklebust } 18248d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18253c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 18268d9266ffSTrond Myklebust return ERR_PTR(-EIO); 18278d9266ffSTrond Myklebust 18288d9266ffSTrond Myklebust found: 18298d9266ffSTrond Myklebust xprt = t->setup(args); 18308d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 18318d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 18328d9266ffSTrond Myklebust -PTR_ERR(xprt)); 183321de0a95STrond Myklebust goto out; 18348d9266ffSTrond Myklebust } 183533d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 183633d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 183721de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 183821de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1839ff861c4dSKees Cook timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 184021de0a95STrond Myklebust else 1841ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 18424e0038b6STrond Myklebust 18434e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 18444e0038b6STrond Myklebust xprt_destroy(xprt); 18454e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 18464e0038b6STrond Myklebust } 18474e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 18484e0038b6STrond Myklebust if (xprt->servername == NULL) { 18494e0038b6STrond Myklebust xprt_destroy(xprt); 18504e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 18514e0038b6STrond Myklebust } 18524e0038b6STrond Myklebust 18533f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1854388f0c77SJeff Layton 1855c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1856c2866763SChuck Lever xprt->max_reqs); 185721de0a95STrond Myklebust out: 1858c2866763SChuck Lever return xprt; 1859c2866763SChuck Lever } 1860c2866763SChuck Lever 1861528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 1862528fd354STrond Myklebust { 1863528fd354STrond Myklebust struct rpc_xprt *xprt = 1864528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 1865528fd354STrond Myklebust 1866528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 1867528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1868528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1869528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1870528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1871528fd354STrond Myklebust kfree(xprt->servername); 1872528fd354STrond Myklebust /* 1873528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 1874528fd354STrond Myklebust */ 1875528fd354STrond Myklebust xprt->ops->destroy(xprt); 1876528fd354STrond Myklebust } 1877528fd354STrond Myklebust 18789903cd1cSChuck Lever /** 18799903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1880a8de240aSTrond Myklebust * @xprt: transport to destroy 18819903cd1cSChuck Lever * 18821da177e4SLinus Torvalds */ 1883a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 18841da177e4SLinus Torvalds { 18851da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 188679234c3dSTrond Myklebust 1887528fd354STrond Myklebust /* 1888528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 1889528fd354STrond Myklebust */ 189079234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 189179234c3dSTrond Myklebust 18920065db32STrond Myklebust del_timer_sync(&xprt->timer); 1893c8541ecdSChuck Lever 1894c8541ecdSChuck Lever /* 1895528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 1896528fd354STrond Myklebust * safely flush receive work running on rpciod. 1897c8541ecdSChuck Lever */ 1898528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 1899528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 19006b6ca86bSTrond Myklebust } 19011da177e4SLinus Torvalds 190230c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 190330c5116bSTrond Myklebust { 190430c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 190530c5116bSTrond Myklebust } 190630c5116bSTrond Myklebust 190730c5116bSTrond Myklebust /** 190830c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 190930c5116bSTrond Myklebust * @xprt: pointer to the transport 191030c5116bSTrond Myklebust * 191130c5116bSTrond Myklebust */ 191230c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 191330c5116bSTrond Myklebust { 191430c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 191530c5116bSTrond Myklebust return xprt; 191630c5116bSTrond Myklebust return NULL; 191730c5116bSTrond Myklebust } 191830c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 191930c5116bSTrond Myklebust 19206b6ca86bSTrond Myklebust /** 19216b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 19226b6ca86bSTrond Myklebust * @xprt: pointer to the transport 19236b6ca86bSTrond Myklebust * 19246b6ca86bSTrond Myklebust */ 19256b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 19266b6ca86bSTrond Myklebust { 192730c5116bSTrond Myklebust if (xprt != NULL) 192830c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 19296b6ca86bSTrond Myklebust } 19305d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 1931