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> 521da177e4SLinus Torvalds 533705ad64SJeff Layton #include <trace/events/sunrpc.h> 543705ad64SJeff Layton 5555ae1aabSRicardo Labiaga #include "sunrpc.h" 5655ae1aabSRicardo Labiaga 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * Local variables 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds 61f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 621da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 631da177e4SLinus Torvalds #endif 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * Local functions 671da177e4SLinus Torvalds */ 6821de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 6937ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 701da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 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 557fe3aca29SChuck Lever /** 558fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 559fe3aca29SChuck Lever * @task: task whose timeout is to be set 560fe3aca29SChuck Lever * 561fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 562fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 563fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 564fe3aca29SChuck Lever */ 565fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 566fe3aca29SChuck Lever { 567fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 568fe3aca29SChuck Lever } 56912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 570fe3aca29SChuck Lever 5712c53040fSBen Hutchings /** 572fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 573fe3aca29SChuck Lever * @task: task whose timeout is to be set 574fe3aca29SChuck Lever * 575fe3aca29SChuck Lever * Set a request's retransmit timeout using the RTT estimator. 576fe3aca29SChuck Lever */ 577fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 578fe3aca29SChuck Lever { 579fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 580ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 581ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 582fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 583ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 584fe3aca29SChuck Lever 585fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 586fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 587fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 588fe3aca29SChuck Lever task->tk_timeout = max_timeout; 589fe3aca29SChuck Lever } 59012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 591fe3aca29SChuck Lever 5921da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5931da177e4SLinus Torvalds { 594ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5971da177e4SLinus Torvalds if (to->to_exponential) 5981da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5991da177e4SLinus Torvalds else 6001da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 6011da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 6021da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 6031da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6069903cd1cSChuck Lever /** 6079903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 6089903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 6099903cd1cSChuck Lever * 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 614ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 6151da177e4SLinus Torvalds int status = 0; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 6181da177e4SLinus Torvalds if (to->to_exponential) 6191da177e4SLinus Torvalds req->rq_timeout <<= 1; 6201da177e4SLinus Torvalds else 6211da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 6221da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 6231da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 6241da177e4SLinus Torvalds req->rq_retries++; 6251da177e4SLinus Torvalds } else { 6261da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 6271da177e4SLinus Torvalds req->rq_retries = 0; 6281da177e4SLinus Torvalds xprt_reset_majortimeo(req); 6291da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 6304a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6311da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 6324a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6331da177e4SLinus Torvalds status = -ETIMEDOUT; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6371da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6381da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds return status; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 64365f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6441da177e4SLinus Torvalds { 64565f27f38SDavid Howells struct rpc_xprt *xprt = 64665f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6471da177e4SLinus Torvalds 64866af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6494876cc77STrond Myklebust xprt->ops->close(xprt); 6501da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 65179234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6549903cd1cSChuck Lever /** 65562da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6569903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6579903cd1cSChuck Lever * 6581da177e4SLinus Torvalds */ 65962da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6624a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6631da177e4SLinus Torvalds xprt_clear_connected(xprt); 664c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 6652a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6664a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6671da177e4SLinus Torvalds } 66862da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6691da177e4SLinus Torvalds 67066af1e55STrond Myklebust /** 67166af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 67266af1e55STrond Myklebust * @xprt: transport to disconnect 67366af1e55STrond Myklebust * 67466af1e55STrond Myklebust */ 67566af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 67666af1e55STrond Myklebust { 67766af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 67866af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 67966af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 68066af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 68166af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 68240a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6832a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 68466af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 68566af1e55STrond Myklebust } 686e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 68766af1e55STrond Myklebust 6887f3a1d1eSTrond Myklebust static unsigned int 6897f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 6907f3a1d1eSTrond Myklebust { 6917f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 6927f3a1d1eSTrond Myklebust } 6937f3a1d1eSTrond Myklebust 6947f3a1d1eSTrond Myklebust static bool 6957f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 6967f3a1d1eSTrond Myklebust { 6977f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 6987f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 6997f3a1d1eSTrond Myklebust 7007f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7017f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7027f3a1d1eSTrond Myklebust } 7037f3a1d1eSTrond Myklebust 7047c1d71cfSTrond Myklebust /** 7057c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 7067c1d71cfSTrond Myklebust * @xprt: transport to disconnect 7077c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 7087c1d71cfSTrond Myklebust * 7097c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 7107c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 7117c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 7127c1d71cfSTrond Myklebust * a batch of RPC requests. 7137c1d71cfSTrond Myklebust * 7147c1d71cfSTrond Myklebust */ 7157c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 7167c1d71cfSTrond Myklebust { 7177c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 7187c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 7197c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 7207c1d71cfSTrond Myklebust goto out; 7212c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 7227c1d71cfSTrond Myklebust goto out; 7237c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 7247c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 7257c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 72640a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7272a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 7287c1d71cfSTrond Myklebust out: 7297c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 7307c1d71cfSTrond Myklebust } 7317c1d71cfSTrond Myklebust 732ad3331acSTrond Myklebust static bool 733ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 734ad3331acSTrond Myklebust { 735ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 736ad3331acSTrond Myklebust } 737ad3331acSTrond Myklebust 738ad3331acSTrond Myklebust static void 739ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 740ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 741ad3331acSTrond Myklebust { 74295f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 743ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 744ad3331acSTrond Myklebust } 745ad3331acSTrond Myklebust 7461da177e4SLinus Torvalds static void 747ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 7481da177e4SLinus Torvalds { 749ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 7501da177e4SLinus Torvalds 7514a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 75295f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 7531da177e4SLinus Torvalds goto out_abort; 754ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 755ad3331acSTrond Myklebust xprt->last_used = jiffies; 7562226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7571da177e4SLinus Torvalds goto out_abort; 7584a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 75940a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7601da177e4SLinus Torvalds return; 7611da177e4SLinus Torvalds out_abort: 7624a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 765718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 766718ba5b8STrond Myklebust struct rpc_task *task, 767718ba5b8STrond Myklebust void *cookie) 768718ba5b8STrond Myklebust { 769718ba5b8STrond Myklebust bool ret = false; 770718ba5b8STrond Myklebust 771718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 772718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 773718ba5b8STrond Myklebust goto out; 774718ba5b8STrond Myklebust if (xprt->snd_task != task) 775718ba5b8STrond Myklebust goto out; 776718ba5b8STrond Myklebust xprt->snd_task = cookie; 777718ba5b8STrond Myklebust ret = true; 778718ba5b8STrond Myklebust out: 779718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 780718ba5b8STrond Myklebust return ret; 781718ba5b8STrond Myklebust } 782718ba5b8STrond Myklebust 783718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 784718ba5b8STrond Myklebust { 785718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 786718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 787718ba5b8STrond Myklebust goto out; 788718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 789718ba5b8STrond Myklebust goto out; 790718ba5b8STrond Myklebust xprt->snd_task =NULL; 791718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 792ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 793718ba5b8STrond Myklebust out: 794718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 79579234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 796718ba5b8STrond Myklebust } 797718ba5b8STrond Myklebust 7989903cd1cSChuck Lever /** 7999903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8009903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8011da177e4SLinus Torvalds * 8021da177e4SLinus Torvalds */ 8031da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8041da177e4SLinus Torvalds { 805ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8061da177e4SLinus Torvalds 80746121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 8081da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 8091da177e4SLinus Torvalds 810ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 81101d37c42STrond Myklebust task->tk_status = -EAGAIN; 8121da177e4SLinus Torvalds return; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 8151da177e4SLinus Torvalds return; 816feb8ca37STrond Myklebust 817feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 818feb8ca37STrond Myklebust xprt->ops->close(xprt); 819feb8ca37STrond Myklebust 820718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 821a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 8222c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 8235d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 8240b9e7943STrond Myklebust 8250b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 8260b9e7943STrond Myklebust return; 8270b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 8280b9e7943STrond Myklebust return; 829262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 8301b092092STrond Myklebust xprt->ops->connect(xprt, task); 8311da177e4SLinus Torvalds } 832718ba5b8STrond Myklebust xprt_release_write(xprt, task); 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8359903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 8361da177e4SLinus Torvalds { 837ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8381da177e4SLinus Torvalds 839cd983ef8SChuck Lever if (task->tk_status == 0) { 840262ca07dSChuck Lever xprt->stat.connect_count++; 841262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 84246121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 8431da177e4SLinus Torvalds task->tk_pid); 8441da177e4SLinus Torvalds return; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds switch (task->tk_status) { 8480fe8d04eSTrond Myklebust case -ECONNREFUSED: 8490fe8d04eSTrond Myklebust case -ECONNRESET: 8500fe8d04eSTrond Myklebust case -ECONNABORTED: 8510fe8d04eSTrond Myklebust case -ENETUNREACH: 8520fe8d04eSTrond Myklebust case -EHOSTUNREACH: 8532fc193cfSTrond Myklebust case -EPIPE: 8542a491991STrond Myklebust case -EAGAIN: 8552a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 85623475d66SChuck Lever break; 8571da177e4SLinus Torvalds case -ETIMEDOUT: 85846121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 85946121cf7SChuck Lever "out\n", task->tk_pid); 8601da177e4SLinus Torvalds break; 8611da177e4SLinus Torvalds default: 86246121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 86346121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 8644e0038b6STrond Myklebust xprt->servername); 86523475d66SChuck Lever task->tk_status = -EIO; 86623475d66SChuck Lever } 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86995f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 87095f7691dSTrond Myklebust XID_RB_EQUAL, 87195f7691dSTrond Myklebust XID_RB_LEFT, 87295f7691dSTrond Myklebust XID_RB_RIGHT, 87395f7691dSTrond Myklebust }; 87495f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 87595f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 87695f7691dSTrond Myklebust { 87795f7691dSTrond Myklebust if (xid1 == xid2) 87895f7691dSTrond Myklebust return XID_RB_EQUAL; 87995f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 88095f7691dSTrond Myklebust return XID_RB_LEFT; 88195f7691dSTrond Myklebust return XID_RB_RIGHT; 88295f7691dSTrond Myklebust } 88395f7691dSTrond Myklebust 88495f7691dSTrond Myklebust static struct rpc_rqst * 88595f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 88695f7691dSTrond Myklebust { 88795f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 88895f7691dSTrond Myklebust struct rpc_rqst *req; 88995f7691dSTrond Myklebust 89095f7691dSTrond Myklebust while (n != NULL) { 89195f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 89295f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 89395f7691dSTrond Myklebust case XID_RB_LEFT: 89495f7691dSTrond Myklebust n = n->rb_left; 89595f7691dSTrond Myklebust break; 89695f7691dSTrond Myklebust case XID_RB_RIGHT: 89795f7691dSTrond Myklebust n = n->rb_right; 89895f7691dSTrond Myklebust break; 89995f7691dSTrond Myklebust case XID_RB_EQUAL: 90095f7691dSTrond Myklebust return req; 90195f7691dSTrond Myklebust } 90295f7691dSTrond Myklebust } 90395f7691dSTrond Myklebust return NULL; 90495f7691dSTrond Myklebust } 90595f7691dSTrond Myklebust 90695f7691dSTrond Myklebust static void 90795f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 90895f7691dSTrond Myklebust { 90995f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 91095f7691dSTrond Myklebust struct rb_node *n = NULL; 91195f7691dSTrond Myklebust struct rpc_rqst *req; 91295f7691dSTrond Myklebust 91395f7691dSTrond Myklebust while (*p != NULL) { 91495f7691dSTrond Myklebust n = *p; 91595f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 91695f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 91795f7691dSTrond Myklebust case XID_RB_LEFT: 91895f7691dSTrond Myklebust p = &n->rb_left; 91995f7691dSTrond Myklebust break; 92095f7691dSTrond Myklebust case XID_RB_RIGHT: 92195f7691dSTrond Myklebust p = &n->rb_right; 92295f7691dSTrond Myklebust break; 92395f7691dSTrond Myklebust case XID_RB_EQUAL: 92495f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 92595f7691dSTrond Myklebust return; 92695f7691dSTrond Myklebust } 92795f7691dSTrond Myklebust } 92895f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 92995f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 93095f7691dSTrond Myklebust } 93195f7691dSTrond Myklebust 93295f7691dSTrond Myklebust static void 93395f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 93495f7691dSTrond Myklebust { 93595f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 93695f7691dSTrond Myklebust } 93795f7691dSTrond Myklebust 9389903cd1cSChuck Lever /** 9399903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 9409903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 9419903cd1cSChuck Lever * @xid: RPC XID of incoming reply 9429903cd1cSChuck Lever * 94375c84151STrond Myklebust * Caller holds xprt->queue_lock. 9441da177e4SLinus Torvalds */ 945d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 9461da177e4SLinus Torvalds { 9478f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 9481da177e4SLinus Torvalds 94995f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 95095f7691dSTrond Myklebust if (entry != NULL) { 9513705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 9520b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 953262ca07dSChuck Lever return entry; 9543705ad64SJeff Layton } 95546121cf7SChuck Lever 95646121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 95746121cf7SChuck Lever ntohl(xid)); 9583705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 959262ca07dSChuck Lever xprt->stat.bad_xids++; 960262ca07dSChuck Lever return NULL; 9611da177e4SLinus Torvalds } 96212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 9631da177e4SLinus Torvalds 964cf9946cdSTrond Myklebust static bool 965cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 966cf9946cdSTrond Myklebust { 967cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 968cf9946cdSTrond Myklebust } 969cf9946cdSTrond Myklebust 970729749bbSTrond Myklebust /** 971729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 972729749bbSTrond Myklebust * @req: Request to pin 973729749bbSTrond Myklebust * 974729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 975cf9946cdSTrond Myklebust * so should be holding the xprt receive lock. 976729749bbSTrond Myklebust */ 977729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 978729749bbSTrond Myklebust { 979cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 980729749bbSTrond Myklebust } 9819590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 982729749bbSTrond Myklebust 983729749bbSTrond Myklebust /** 984729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 985729749bbSTrond Myklebust * @req: Request to pin 986729749bbSTrond Myklebust * 987cf9946cdSTrond Myklebust * Caller should be holding the xprt receive lock. 988729749bbSTrond Myklebust */ 989729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 990729749bbSTrond Myklebust { 991cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 992cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 993cf9946cdSTrond Myklebust return; 994cf9946cdSTrond Myklebust } 995cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 996cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 997729749bbSTrond Myklebust } 9989590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 999729749bbSTrond Myklebust 1000729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 1001729749bbSTrond Myklebust { 1002cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 1003729749bbSTrond Myklebust } 1004729749bbSTrond Myklebust 1005edc81dcdSTrond Myklebust static bool 1006edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 1007edc81dcdSTrond Myklebust { 1008edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1009edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 1010edc81dcdSTrond Myklebust } 1011edc81dcdSTrond Myklebust 1012edc81dcdSTrond Myklebust static bool 1013edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 1014edc81dcdSTrond Myklebust { 1015edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1016edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 1017edc81dcdSTrond Myklebust } 1018edc81dcdSTrond Myklebust 1019edc81dcdSTrond Myklebust /** 1020edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 1021edc81dcdSTrond Myklebust * @task: RPC task 1022edc81dcdSTrond Myklebust * 1023edc81dcdSTrond Myklebust */ 1024edc81dcdSTrond Myklebust void 1025edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1026edc81dcdSTrond Myklebust { 1027edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1028edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1029edc81dcdSTrond Myklebust 1030edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1031edc81dcdSTrond Myklebust return; 1032edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1033edc81dcdSTrond Myklebust 1034edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1035edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1036edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1037edc81dcdSTrond Myklebust 1038edc81dcdSTrond Myklebust /* Add request to the receive list */ 103995f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1040edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1041edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1042edc81dcdSTrond Myklebust 1043edc81dcdSTrond Myklebust xprt_reset_majortimeo(req); 1044edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1045edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1046edc81dcdSTrond Myklebust } 1047edc81dcdSTrond Myklebust 1048edc81dcdSTrond Myklebust /** 1049edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1050edc81dcdSTrond Myklebust * @task: RPC task 1051edc81dcdSTrond Myklebust * 1052edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1053edc81dcdSTrond Myklebust */ 1054edc81dcdSTrond Myklebust static void 1055edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1056edc81dcdSTrond Myklebust { 105795f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 105895f7691dSTrond Myklebust 1059edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 106095f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1061edc81dcdSTrond Myklebust } 1062edc81dcdSTrond Myklebust 1063ecd465eeSChuck Lever /** 1064ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1065ecd465eeSChuck Lever * @task: RPC request that recently completed 1066ecd465eeSChuck Lever * 106775c84151STrond Myklebust * Caller holds xprt->queue_lock. 1068ecd465eeSChuck Lever */ 1069ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 10701da177e4SLinus Torvalds { 10711570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 10721570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 107395c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1074d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 10751570c1e4SChuck Lever 10761da177e4SLinus Torvalds if (timer) { 10771da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1078ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 10791570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds } 1082ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 10831da177e4SLinus Torvalds 10841570c1e4SChuck Lever /** 10851570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 10861570c1e4SChuck Lever * @task: RPC request that recently completed 10871570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 10881570c1e4SChuck Lever * 108975c84151STrond Myklebust * Caller holds xprt->queue_lock. 10901570c1e4SChuck Lever */ 10911570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 10921570c1e4SChuck Lever { 10931570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1094fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 10951da177e4SLinus Torvalds 10961570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 10971570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 10983705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 10991da177e4SLinus Torvalds 1100fda13939STrond Myklebust xprt->stat.recvs++; 1101ef759a2eSChuck Lever 11021e799b67STrond Myklebust req->rq_private_buf.len = copied; 1103dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1104dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 110543ac3f29STrond Myklebust smp_wmb(); 1106dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1107edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1108fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 11091da177e4SLinus Torvalds } 111012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 11111da177e4SLinus Torvalds 111246c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 11151da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 11161da177e4SLinus Torvalds 11175d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 11185d00837bSTrond Myklebust return; 111946c0ee8bSChuck Lever 112082476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1121dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 112246c0ee8bSChuck Lever if (xprt->ops->timer) 11236a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 11245d00837bSTrond Myklebust } else 11255d00837bSTrond Myklebust task->tk_status = 0; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11289903cd1cSChuck Lever /** 11297f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 11307f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 11317f3a1d1eSTrond Myklebust * 11327f3a1d1eSTrond Myklebust */ 11337f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 11347f3a1d1eSTrond Myklebust { 11357f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11367f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11377f3a1d1eSTrond Myklebust 11387f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 11397f3a1d1eSTrond Myklebust return; 11407f3a1d1eSTrond Myklebust /* 11417f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 11427f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 11437f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 11447f3a1d1eSTrond Myklebust */ 11457f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 11467f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 11477f3a1d1eSTrond Myklebust xprt->ops->set_retrans_timeout(task); 11487f3a1d1eSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 11497f3a1d1eSTrond Myklebust /* 11507f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 11517f3a1d1eSTrond Myklebust * connection was dropped in case the call to 11527f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 11537f3a1d1eSTrond Myklebust */ 11547f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 11557f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 11567f3a1d1eSTrond Myklebust task, -ENOTCONN); 11577f3a1d1eSTrond Myklebust } 11587f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 11597f3a1d1eSTrond Myklebust } 11607f3a1d1eSTrond Myklebust 1161944b0429STrond Myklebust static bool 1162944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1163944b0429STrond Myklebust { 1164762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1165944b0429STrond Myklebust } 1166944b0429STrond Myklebust 1167944b0429STrond Myklebust /** 1168944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1169944b0429STrond Myklebust * @task: pointer to rpc_task 1170944b0429STrond Myklebust * 1171944b0429STrond Myklebust * Add a task to the transmission queue. 1172944b0429STrond Myklebust */ 1173944b0429STrond Myklebust void 1174944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1175944b0429STrond Myklebust { 1176918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1177944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1178944b0429STrond Myklebust 1179944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1180944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 118175891f50STrond Myklebust /* 118275891f50STrond Myklebust * Requests that carry congestion control credits are added 118375891f50STrond Myklebust * to the head of the list to avoid starvation issues. 118475891f50STrond Myklebust */ 118575891f50STrond Myklebust if (req->rq_cong) { 118675891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 118775891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 118875891f50STrond Myklebust if (pos->rq_cong) 118975891f50STrond Myklebust continue; 119075891f50STrond Myklebust /* Note: req is added _before_ pos */ 119175891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 119275891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 119375891f50STrond Myklebust goto out; 119475891f50STrond Myklebust } 119586aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 119686aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 119786aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 119886aeee0eSTrond Myklebust continue; 119986aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 120086aeee0eSTrond Myklebust continue; 120186aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 120286aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 120386aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 120486aeee0eSTrond Myklebust goto out; 120586aeee0eSTrond Myklebust } 120675891f50STrond Myklebust } else { 1207918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1208918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1209918f3c1fSTrond Myklebust continue; 1210918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1211918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 1212918f3c1fSTrond Myklebust goto out; 1213918f3c1fSTrond Myklebust } 121475891f50STrond Myklebust } 1215944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1216918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 1217918f3c1fSTrond Myklebust out: 1218944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1219944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1220944b0429STrond Myklebust } 1221944b0429STrond Myklebust } 1222944b0429STrond Myklebust 1223944b0429STrond Myklebust /** 1224944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1225944b0429STrond Myklebust * @task: pointer to rpc_task 1226944b0429STrond Myklebust * 1227944b0429STrond Myklebust * Remove a task from the transmission queue 1228944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1229944b0429STrond Myklebust */ 1230944b0429STrond Myklebust static void 1231944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1232944b0429STrond Myklebust { 1233918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1234918f3c1fSTrond Myklebust 1235918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1236918f3c1fSTrond Myklebust return; 1237918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1238918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1239918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1240918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1241918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1242918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1243918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1244918f3c1fSTrond Myklebust } 1245918f3c1fSTrond Myklebust } else 1246918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1247944b0429STrond Myklebust } 1248944b0429STrond Myklebust 1249944b0429STrond Myklebust /** 1250944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1251944b0429STrond Myklebust * @task: pointer to rpc_task 1252944b0429STrond Myklebust * 1253944b0429STrond Myklebust * Remove a task from the transmission queue 1254944b0429STrond Myklebust */ 1255944b0429STrond Myklebust static void 1256944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1257944b0429STrond Myklebust { 1258944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1259944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1260944b0429STrond Myklebust 1261944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1262944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1263944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1264944b0429STrond Myklebust } 1265944b0429STrond Myklebust 12667f3a1d1eSTrond Myklebust /** 12679d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 12689d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 12699d96acbcSTrond Myklebust * 12709d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 12719d96acbcSTrond Myklebust * the request for transmission or receive. 12729d96acbcSTrond Myklebust */ 12739d96acbcSTrond Myklebust void 12749d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 12759d96acbcSTrond Myklebust { 12769d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12779d96acbcSTrond Myklebust 12789d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 12799d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 12809d96acbcSTrond Myklebust } 12819d96acbcSTrond Myklebust 12829d96acbcSTrond Myklebust /** 1283762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1284762e4e67STrond Myklebust * @task: pointer to rpc_task 1285762e4e67STrond Myklebust * 1286762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1287762e4e67STrond Myklebust */ 1288762e4e67STrond Myklebust bool 1289762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1290762e4e67STrond Myklebust { 1291762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1292762e4e67STrond Myklebust } 1293762e4e67STrond Myklebust 1294762e4e67STrond Myklebust /** 12959903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 12969903cd1cSChuck Lever * @task: RPC task about to send a request 12979903cd1cSChuck Lever * 12981da177e4SLinus Torvalds */ 129990051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 13021da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 13031da177e4SLinus Torvalds 130446121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 13051da177e4SLinus Torvalds 13065f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 13075f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1308944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 13095f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 13105f2f6bd9STrond Myklebust task, 0); 13115f2f6bd9STrond Myklebust return false; 13125f2f6bd9STrond Myklebust 13138a19a0b6STrond Myklebust } 13145f2f6bd9STrond Myklebust return true; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 1317e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 13185e5ce5beSTrond Myklebust { 1319343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 13205e5ce5beSTrond Myklebust } 13215e5ce5beSTrond Myklebust 13229903cd1cSChuck Lever /** 132389f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 132489f90fe1STrond Myklebust * @req: pointer to request to transmit 132589f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 13269903cd1cSChuck Lever * 132789f90fe1STrond Myklebust * This performs the transmission of a single request. 132889f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 132989f90fe1STrond Myklebust * does need to be pinned. 133089f90fe1STrond Myklebust * Returns '0' on success. 13319903cd1cSChuck Lever */ 133289f90fe1STrond Myklebust static int 133389f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 133689f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 133790d91b0cSTrond Myklebust unsigned int connect_cookie; 1338dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1339ff699ea8SChuck Lever int status; 13401da177e4SLinus Torvalds 134146121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 13421da177e4SLinus Torvalds 1343edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 134489f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 134589f90fe1STrond Myklebust status = 0; 1346944b0429STrond Myklebust goto out_dequeue; 134789f90fe1STrond Myklebust } 13483021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1349edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 135089f90fe1STrond Myklebust status = -EBADMSG; 1351944b0429STrond Myklebust goto out_dequeue; 13523021a5bbSTrond Myklebust } 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355dcbbeda8STrond Myklebust /* 1356dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1357dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1358dcbbeda8STrond Myklebust * reply to the first transmission. 1359dcbbeda8STrond Myklebust */ 1360dcbbeda8STrond Myklebust req->rq_ntrans++; 1361dcbbeda8STrond Myklebust 136290d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1363adfa7144STrond Myklebust status = xprt->ops->send_request(req); 13643705ad64SJeff Layton trace_xprt_transmit(xprt, req->rq_xid, status); 1365c8485e4dSTrond Myklebust if (status != 0) { 1366dcbbeda8STrond Myklebust req->rq_ntrans--; 136789f90fe1STrond Myklebust return status; 1368c8485e4dSTrond Myklebust } 13697ebbbc6eSTrond Myklebust 1370dcbbeda8STrond Myklebust if (is_retrans) 1371dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1372dcbbeda8STrond Myklebust 13734a068258SChuck Lever xprt_inject_disconnect(xprt); 1374c8485e4dSTrond Myklebust 137546121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 1376468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1377fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 1378262ca07dSChuck Lever 1379262ca07dSChuck Lever xprt->stat.sends++; 1380262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1381262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 138215a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 138315a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1384fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 138590d91b0cSTrond Myklebust 138690d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1387944b0429STrond Myklebust out_dequeue: 1388944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 138989f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 139089f90fe1STrond Myklebust return status; 139189f90fe1STrond Myklebust } 139289f90fe1STrond Myklebust 139389f90fe1STrond Myklebust /** 139489f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 139589f90fe1STrond Myklebust * @task: controlling RPC task 139689f90fe1STrond Myklebust * 139789f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 139889f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 139989f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 140089f90fe1STrond Myklebust * received a reply. 140189f90fe1STrond Myklebust */ 140289f90fe1STrond Myklebust void 140389f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 140489f90fe1STrond Myklebust { 140589f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 140689f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 140789f90fe1STrond Myklebust int status; 140889f90fe1STrond Myklebust 140989f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 141089f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 141189f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 141289f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 141389f90fe1STrond Myklebust xprt_pin_rqst(next); 141489f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 141589f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 141689f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 141789f90fe1STrond Myklebust status = 0; 141889f90fe1STrond Myklebust cond_resched(); 141989f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 142089f90fe1STrond Myklebust xprt_unpin_rqst(next); 142189f90fe1STrond Myklebust if (status == 0) { 142289f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 142389f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 142489f90fe1STrond Myklebust continue; 1425c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 142689f90fe1STrond Myklebust task->tk_status = status; 142789f90fe1STrond Myklebust break; 142889f90fe1STrond Myklebust } 142989f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 1432ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1433ba60eb25STrond Myklebust { 1434ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1435ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1436ba60eb25STrond Myklebust } 1437ba60eb25STrond Myklebust 1438ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1439ba60eb25STrond Myklebust { 1440ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1441ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1442ba60eb25STrond Myklebust } 1443ba60eb25STrond Myklebust 1444ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1445ba60eb25STrond Myklebust { 1446ba60eb25STrond Myklebust bool ret = false; 1447ba60eb25STrond Myklebust 1448ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1449ba60eb25STrond Myklebust goto out; 1450ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1451ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1452ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1453ba60eb25STrond Myklebust ret = true; 1454ba60eb25STrond Myklebust } 1455ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1456ba60eb25STrond Myklebust out: 1457ba60eb25STrond Myklebust return ret; 1458ba60eb25STrond Myklebust } 1459ba60eb25STrond Myklebust 146092ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1461d9ba131dSTrond Myklebust { 1462d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1463d9ba131dSTrond Myklebust 1464ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1465d9ba131dSTrond Myklebust goto out; 1466ff699ea8SChuck Lever ++xprt->num_reqs; 146792ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 146892ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 146992ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1470d9ba131dSTrond Myklebust if (req != NULL) 1471d9ba131dSTrond Myklebust goto out; 1472ff699ea8SChuck Lever --xprt->num_reqs; 1473d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1474d9ba131dSTrond Myklebust out: 1475d9ba131dSTrond Myklebust return req; 1476d9ba131dSTrond Myklebust } 1477d9ba131dSTrond Myklebust 1478d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1479d9ba131dSTrond Myklebust { 1480ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1481ff699ea8SChuck Lever --xprt->num_reqs; 1482d9ba131dSTrond Myklebust kfree(req); 1483d9ba131dSTrond Myklebust return true; 1484d9ba131dSTrond Myklebust } 1485d9ba131dSTrond Myklebust return false; 1486d9ba131dSTrond Myklebust } 1487d9ba131dSTrond Myklebust 1488f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 14891da177e4SLinus Torvalds { 1490d9ba131dSTrond Myklebust struct rpc_rqst *req; 14911da177e4SLinus Torvalds 1492f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 14931da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1494d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1495d9ba131dSTrond Myklebust list_del(&req->rq_list); 1496d9ba131dSTrond Myklebust goto out_init_req; 1497d9ba131dSTrond Myklebust } 149892ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1499d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1500d9ba131dSTrond Myklebust goto out_init_req; 1501d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1502d9ba131dSTrond Myklebust case -ENOMEM: 1503d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1504d9ba131dSTrond Myklebust "failed! Retrying\n"); 15051afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1506d9ba131dSTrond Myklebust break; 1507d9ba131dSTrond Myklebust case -EAGAIN: 1508ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1509d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1510e9d47639SGustavo A. R. Silva /* fall through */ 15111afeaf5cSTrond Myklebust default: 1512d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 15131afeaf5cSTrond Myklebust } 1514f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1515d9ba131dSTrond Myklebust return; 1516d9ba131dSTrond Myklebust out_init_req: 1517ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1518ff699ea8SChuck Lever xprt->num_reqs); 151937ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 152037ac86c3SChuck Lever 1521d9ba131dSTrond Myklebust task->tk_status = 0; 15221da177e4SLinus Torvalds task->tk_rqstp = req; 15231da177e4SLinus Torvalds } 1524f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1525f39c1bfbSTrond Myklebust 1526a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1527ee5ebe85STrond Myklebust { 1528ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1529c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1530c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1531ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1532c25573b5STrond Myklebust } 1533ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1534ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1535ee5ebe85STrond Myklebust } 1536a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1537ee5ebe85STrond Myklebust 153821de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 153921de0a95STrond Myklebust { 154021de0a95STrond Myklebust struct rpc_rqst *req; 154121de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 154221de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 154321de0a95STrond Myklebust list_del(&req->rq_list); 154421de0a95STrond Myklebust kfree(req); 154521de0a95STrond Myklebust } 154621de0a95STrond Myklebust } 154721de0a95STrond Myklebust 1548d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1549d9ba131dSTrond Myklebust unsigned int num_prealloc, 1550d9ba131dSTrond Myklebust unsigned int max_alloc) 1551bd1722d4SPavel Emelyanov { 1552bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 155321de0a95STrond Myklebust struct rpc_rqst *req; 155421de0a95STrond Myklebust int i; 1555bd1722d4SPavel Emelyanov 1556bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1557bd1722d4SPavel Emelyanov if (xprt == NULL) 1558bd1722d4SPavel Emelyanov goto out; 1559bd1722d4SPavel Emelyanov 156021de0a95STrond Myklebust xprt_init(xprt, net); 156121de0a95STrond Myklebust 156221de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 156321de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 156421de0a95STrond Myklebust if (!req) 15658313164cSwangweidong goto out_free; 156621de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 156721de0a95STrond Myklebust } 1568d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1569d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1570d9ba131dSTrond Myklebust else 157121de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1572d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1573ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1574bd1722d4SPavel Emelyanov 1575bd1722d4SPavel Emelyanov return xprt; 1576bd1722d4SPavel Emelyanov 1577bd1722d4SPavel Emelyanov out_free: 157821de0a95STrond Myklebust xprt_free(xprt); 1579bd1722d4SPavel Emelyanov out: 1580bd1722d4SPavel Emelyanov return NULL; 1581bd1722d4SPavel Emelyanov } 1582bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1583bd1722d4SPavel Emelyanov 1584e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1585e204e621SPavel Emelyanov { 158637aa2133SPavel Emelyanov put_net(xprt->xprt_net); 158721de0a95STrond Myklebust xprt_free_all_slots(xprt); 1588fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1589e204e621SPavel Emelyanov } 1590e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1591e204e621SPavel Emelyanov 1592902c5887STrond Myklebust static void 1593902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1594902c5887STrond Myklebust { 1595902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1596902c5887STrond Myklebust } 1597902c5887STrond Myklebust 15989dc6edcfSTrond Myklebust static __be32 15999dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 16009dc6edcfSTrond Myklebust { 16019dc6edcfSTrond Myklebust __be32 xid; 16029dc6edcfSTrond Myklebust 16039dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 16049dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 16059dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 16069dc6edcfSTrond Myklebust return xid; 16079dc6edcfSTrond Myklebust } 16089dc6edcfSTrond Myklebust 16099dc6edcfSTrond Myklebust static void 16109dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 16119dc6edcfSTrond Myklebust { 16129dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 16139dc6edcfSTrond Myklebust } 16149dc6edcfSTrond Myklebust 16159dc6edcfSTrond Myklebust static void 16169dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 16179dc6edcfSTrond Myklebust { 16189dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16199dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 16209dc6edcfSTrond Myklebust 16219dc6edcfSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 16229dc6edcfSTrond Myklebust req->rq_task = task; 16239dc6edcfSTrond Myklebust req->rq_xprt = xprt; 16249dc6edcfSTrond Myklebust req->rq_buffer = NULL; 16259dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1626902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 16279dc6edcfSTrond Myklebust req->rq_bytes_sent = 0; 16289dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 16299dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 16309dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 16319dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 16329dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 16339dc6edcfSTrond Myklebust xprt_reset_majortimeo(req); 16349dc6edcfSTrond Myklebust dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 16359dc6edcfSTrond Myklebust req, ntohl(req->rq_xid)); 16369dc6edcfSTrond Myklebust } 16379dc6edcfSTrond Myklebust 16389dc6edcfSTrond Myklebust static void 16399dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 16409dc6edcfSTrond Myklebust { 16419dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 16429dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 16439dc6edcfSTrond Myklebust xprt_request_init(task); 16449dc6edcfSTrond Myklebust } 16459dc6edcfSTrond Myklebust 16469903cd1cSChuck Lever /** 16479903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 16489903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 16499903cd1cSChuck Lever * 1650ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1651ba60eb25STrond Myklebust * slots are available, place the task on the transport's 16529903cd1cSChuck Lever * backlog queue. 16539903cd1cSChuck Lever */ 16549903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 16551da177e4SLinus Torvalds { 1656fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16571da177e4SLinus Torvalds 165843cedbf0STrond Myklebust task->tk_status = 0; 165943cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 166043cedbf0STrond Myklebust return; 166143cedbf0STrond Myklebust 166243cedbf0STrond Myklebust task->tk_timeout = 0; 166343cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1664ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 16659dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1666ba60eb25STrond Myklebust } 1667ba60eb25STrond Myklebust 1668ba60eb25STrond Myklebust /** 1669ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1670ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1671ba60eb25STrond Myklebust * 1672ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1673ba60eb25STrond Myklebust * backlog queue. 1674ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1675ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1676ba60eb25STrond Myklebust */ 1677ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1678ba60eb25STrond Myklebust { 1679fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1680ba60eb25STrond Myklebust 1681ba60eb25STrond Myklebust task->tk_status = 0; 1682ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1683ba60eb25STrond Myklebust return; 1684ba60eb25STrond Myklebust 1685ba60eb25STrond Myklebust task->tk_timeout = 0; 1686ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 16879dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 1690edc81dcdSTrond Myklebust static void 1691edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req) 1692edc81dcdSTrond Myklebust { 1693edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1694edc81dcdSTrond Myklebust 1695944b0429STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1696944b0429STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1697edc81dcdSTrond Myklebust xprt_is_pinned_rqst(req)) { 1698edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1699944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1700edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1701edc81dcdSTrond Myklebust while (xprt_is_pinned_rqst(req)) { 1702edc81dcdSTrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1703edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1704edc81dcdSTrond Myklebust xprt_wait_on_pinned_rqst(req); 1705edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1706edc81dcdSTrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1707edc81dcdSTrond Myklebust } 1708edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1709edc81dcdSTrond Myklebust } 1710edc81dcdSTrond Myklebust } 1711edc81dcdSTrond Myklebust 17129903cd1cSChuck Lever /** 17139903cd1cSChuck Lever * xprt_release - release an RPC request slot 17149903cd1cSChuck Lever * @task: task which is finished with the slot 17159903cd1cSChuck Lever * 17161da177e4SLinus Torvalds */ 17179903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 17181da177e4SLinus Torvalds { 171955ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 172087ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17211da177e4SLinus Torvalds 172287ed5003STrond Myklebust if (req == NULL) { 172387ed5003STrond Myklebust if (task->tk_client) { 1724fb43d172STrond Myklebust xprt = task->tk_xprt; 172587ed5003STrond Myklebust xprt_release_write(xprt, task); 172687ed5003STrond Myklebust } 17271da177e4SLinus Torvalds return; 172887ed5003STrond Myklebust } 172955ae1aabSRicardo Labiaga 173055ae1aabSRicardo Labiaga xprt = req->rq_xprt; 17310a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 17320a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 17330a702195SWeston Andros Adamson else if (task->tk_client) 17340a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 1735edc81dcdSTrond Myklebust xprt_request_dequeue_all(task, req); 17364a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 173749e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1738a58dd398SChuck Lever if (xprt->ops->release_request) 1739a58dd398SChuck Lever xprt->ops->release_request(task); 17401da177e4SLinus Torvalds xprt->last_used = jiffies; 1741ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 17424a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1743ee5ebe85STrond Myklebust if (req->rq_buffer) 17443435c74aSChuck Lever xprt->ops->buf_free(task); 17454a068258SChuck Lever xprt_inject_disconnect(xprt); 17469d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 1747a17c2153STrond Myklebust if (req->rq_cred != NULL) 1748a17c2153STrond Myklebust put_rpccred(req->rq_cred); 17491da177e4SLinus Torvalds task->tk_rqstp = NULL; 1750ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1751ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 175255ae1aabSRicardo Labiaga 175346121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1754ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1755a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1756ee5ebe85STrond Myklebust else 1757c9acb42eSTrond Myklebust xprt_free_bc_request(req); 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 1760902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1761902c5887STrond Myklebust void 1762902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1763902c5887STrond Myklebust { 1764902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1765902c5887STrond Myklebust 1766902c5887STrond Myklebust task->tk_rqstp = req; 1767902c5887STrond Myklebust req->rq_task = task; 1768902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1769902c5887STrond Myklebust /* 1770902c5887STrond Myklebust * Set up the xdr_buf length. 1771902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1772902c5887STrond Myklebust */ 1773902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1774902c5887STrond Myklebust xbufp->tail[0].iov_len; 1775902c5887STrond Myklebust req->rq_bytes_sent = 0; 1776902c5887STrond Myklebust } 1777902c5887STrond Myklebust #endif 1778902c5887STrond Myklebust 177921de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1780c2866763SChuck Lever { 178130c5116bSTrond Myklebust kref_init(&xprt->kref); 1782c2866763SChuck Lever 1783c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1784c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 178575c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1786c2866763SChuck Lever 1787c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 178895f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1789944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 17909e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1791f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1792f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 17939e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 179480b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1795f9acac1aSRicardo Labiaga 1796c2866763SChuck Lever xprt->last_used = jiffies; 1797c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1798a509050bSChuck Lever xprt->bind_index = 0; 1799c2866763SChuck Lever 1800c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1801c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 180279c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1803c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1804c2866763SChuck Lever 1805c2866763SChuck Lever xprt_init_xid(xprt); 1806c2866763SChuck Lever 180721de0a95STrond Myklebust xprt->xprt_net = get_net(net); 18088d9266ffSTrond Myklebust } 18098d9266ffSTrond Myklebust 18108d9266ffSTrond Myklebust /** 18118d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 18128d9266ffSTrond Myklebust * @args: rpc transport creation arguments 18138d9266ffSTrond Myklebust * 18148d9266ffSTrond Myklebust */ 18158d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 18168d9266ffSTrond Myklebust { 18178d9266ffSTrond Myklebust struct rpc_xprt *xprt; 18188d9266ffSTrond Myklebust struct xprt_class *t; 18198d9266ffSTrond Myklebust 18208d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 18218d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 18228d9266ffSTrond Myklebust if (t->ident == args->ident) { 18238d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18248d9266ffSTrond Myklebust goto found; 18258d9266ffSTrond Myklebust } 18268d9266ffSTrond Myklebust } 18278d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18283c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 18298d9266ffSTrond Myklebust return ERR_PTR(-EIO); 18308d9266ffSTrond Myklebust 18318d9266ffSTrond Myklebust found: 18328d9266ffSTrond Myklebust xprt = t->setup(args); 18338d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 18348d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 18358d9266ffSTrond Myklebust -PTR_ERR(xprt)); 183621de0a95STrond Myklebust goto out; 18378d9266ffSTrond Myklebust } 183833d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 183933d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 184021de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 184121de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1842ff861c4dSKees Cook timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 184321de0a95STrond Myklebust else 1844ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 18454e0038b6STrond Myklebust 18464e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 18474e0038b6STrond Myklebust xprt_destroy(xprt); 18484e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 18494e0038b6STrond Myklebust } 18504e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 18514e0038b6STrond Myklebust if (xprt->servername == NULL) { 18524e0038b6STrond Myklebust xprt_destroy(xprt); 18534e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 18544e0038b6STrond Myklebust } 18554e0038b6STrond Myklebust 18563f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1857388f0c77SJeff Layton 1858c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1859c2866763SChuck Lever xprt->max_reqs); 186021de0a95STrond Myklebust out: 1861c2866763SChuck Lever return xprt; 1862c2866763SChuck Lever } 1863c2866763SChuck Lever 1864528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 1865528fd354STrond Myklebust { 1866528fd354STrond Myklebust struct rpc_xprt *xprt = 1867528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 1868528fd354STrond Myklebust 1869528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 1870528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1871528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1872528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1873528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1874528fd354STrond Myklebust kfree(xprt->servername); 1875528fd354STrond Myklebust /* 1876528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 1877528fd354STrond Myklebust */ 1878528fd354STrond Myklebust xprt->ops->destroy(xprt); 1879528fd354STrond Myklebust } 1880528fd354STrond Myklebust 18819903cd1cSChuck Lever /** 18829903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1883a8de240aSTrond Myklebust * @xprt: transport to destroy 18849903cd1cSChuck Lever * 18851da177e4SLinus Torvalds */ 1886a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 18871da177e4SLinus Torvalds { 18881da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 188979234c3dSTrond Myklebust 1890528fd354STrond Myklebust /* 1891528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 1892528fd354STrond Myklebust */ 189379234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 189479234c3dSTrond Myklebust 18950065db32STrond Myklebust del_timer_sync(&xprt->timer); 1896c8541ecdSChuck Lever 1897c8541ecdSChuck Lever /* 1898528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 1899528fd354STrond Myklebust * safely flush receive work running on rpciod. 1900c8541ecdSChuck Lever */ 1901528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 1902528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 19036b6ca86bSTrond Myklebust } 19041da177e4SLinus Torvalds 190530c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 190630c5116bSTrond Myklebust { 190730c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 190830c5116bSTrond Myklebust } 190930c5116bSTrond Myklebust 191030c5116bSTrond Myklebust /** 191130c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 191230c5116bSTrond Myklebust * @xprt: pointer to the transport 191330c5116bSTrond Myklebust * 191430c5116bSTrond Myklebust */ 191530c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 191630c5116bSTrond Myklebust { 191730c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 191830c5116bSTrond Myklebust return xprt; 191930c5116bSTrond Myklebust return NULL; 192030c5116bSTrond Myklebust } 192130c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 192230c5116bSTrond Myklebust 19236b6ca86bSTrond Myklebust /** 19246b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 19256b6ca86bSTrond Myklebust * @xprt: pointer to the transport 19266b6ca86bSTrond Myklebust * 19276b6ca86bSTrond Myklebust */ 19286b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 19296b6ca86bSTrond Myklebust { 193030c5116bSTrond Myklebust if (xprt != NULL) 193130c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 19326b6ca86bSTrond Myklebust } 19335d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 1934