11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 51da177e4SLinus Torvalds * and asynchronous calls. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The interface works like this: 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 101da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 111da177e4SLinus Torvalds * (xprt_reserve). 121da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1355aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1455aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1555ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1655ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1755ae1aabSRicardo Labiaga * expired. 181da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 1955aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 201da177e4SLinus Torvalds * caller is woken up, and the timer removed. 211da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 221da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 231da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 241da177e4SLinus Torvalds * of -ETIMEDOUT. 251da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 261da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 271da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 281da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 291da177e4SLinus Torvalds * again. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 321da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 331da177e4SLinus Torvalds * tasks that rely on callbacks. 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3655aa4f58SChuck Lever * 3755aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 40a246b010SChuck Lever #include <linux/module.h> 41a246b010SChuck Lever 421da177e4SLinus Torvalds #include <linux/types.h> 43a246b010SChuck Lever #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/workqueue.h> 45bf3fcf89SChuck Lever #include <linux/net.h> 46ff839970SChuck Lever #include <linux/ktime.h> 471da177e4SLinus Torvalds 48a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 51fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 52a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 531da177e4SLinus Torvalds 543705ad64SJeff Layton #include <trace/events/sunrpc.h> 553705ad64SJeff Layton 5655ae1aabSRicardo Labiaga #include "sunrpc.h" 5755ae1aabSRicardo Labiaga 581da177e4SLinus Torvalds /* 591da177e4SLinus Torvalds * Local variables 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds 62f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 631da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 641da177e4SLinus Torvalds #endif 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Local functions 681da177e4SLinus Torvalds */ 6921de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 7037ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 714e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 721da177e4SLinus Torvalds 735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7581c098afS\"Talpey, Thomas\ 7612a80469SChuck Lever /** 7781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 7881c098afS\"Talpey, Thomas\ * @transport: transport to register 7981c098afS\"Talpey, Thomas\ * 8081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 8181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 8281c098afS\"Talpey, Thomas\ * 8381c098afS\"Talpey, Thomas\ * Returns: 8481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 8581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 8681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 8781c098afS\"Talpey, Thomas\ */ 8881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 8981c098afS\"Talpey, Thomas\ { 9081c098afS\"Talpey, Thomas\ struct xprt_class *t; 9181c098afS\"Talpey, Thomas\ int result; 9281c098afS\"Talpey, Thomas\ 9381c098afS\"Talpey, Thomas\ result = -EEXIST; 9481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 9581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 9681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 974fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 9881c098afS\"Talpey, Thomas\ goto out; 9981c098afS\"Talpey, Thomas\ } 10081c098afS\"Talpey, Thomas\ 10181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 10281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 10381c098afS\"Talpey, Thomas\ transport->name); 10481c098afS\"Talpey, Thomas\ result = 0; 10581c098afS\"Talpey, Thomas\ 10681c098afS\"Talpey, Thomas\ out: 10781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 10881c098afS\"Talpey, Thomas\ return result; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 11181c098afS\"Talpey, Thomas\ 11281c098afS\"Talpey, Thomas\ /** 11381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 11465b6e42cSRandy Dunlap * @transport: transport to unregister 11581c098afS\"Talpey, Thomas\ * 11681c098afS\"Talpey, Thomas\ * Returns: 11781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 11881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 11981c098afS\"Talpey, Thomas\ */ 12081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 12181c098afS\"Talpey, Thomas\ { 12281c098afS\"Talpey, Thomas\ struct xprt_class *t; 12381c098afS\"Talpey, Thomas\ int result; 12481c098afS\"Talpey, Thomas\ 12581c098afS\"Talpey, Thomas\ result = 0; 12681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 12781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 12881c098afS\"Talpey, Thomas\ if (t == transport) { 12981c098afS\"Talpey, Thomas\ printk(KERN_INFO 13081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 13181c098afS\"Talpey, Thomas\ transport->name); 13281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 13381c098afS\"Talpey, Thomas\ goto out; 13481c098afS\"Talpey, Thomas\ } 13581c098afS\"Talpey, Thomas\ } 13681c098afS\"Talpey, Thomas\ result = -ENOENT; 13781c098afS\"Talpey, Thomas\ 13881c098afS\"Talpey, Thomas\ out: 13981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 14081c098afS\"Talpey, Thomas\ return result; 14181c098afS\"Talpey, Thomas\ } 14281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 14381c098afS\"Talpey, Thomas\ 14481c098afS\"Talpey, Thomas\ /** 145441e3e24STom Talpey * xprt_load_transport - load a transport implementation 146441e3e24STom Talpey * @transport_name: transport to load 147441e3e24STom Talpey * 148441e3e24STom Talpey * Returns: 149441e3e24STom Talpey * 0: transport successfully loaded 150441e3e24STom Talpey * -ENOENT: transport module not available 151441e3e24STom Talpey */ 152441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 153441e3e24STom Talpey { 154441e3e24STom Talpey struct xprt_class *t; 155441e3e24STom Talpey int result; 156441e3e24STom Talpey 157441e3e24STom Talpey result = 0; 158441e3e24STom Talpey spin_lock(&xprt_list_lock); 159441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 160441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 161441e3e24STom Talpey spin_unlock(&xprt_list_lock); 162441e3e24STom Talpey goto out; 163441e3e24STom Talpey } 164441e3e24STom Talpey } 165441e3e24STom Talpey spin_unlock(&xprt_list_lock); 166ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 167441e3e24STom Talpey out: 168441e3e24STom Talpey return result; 169441e3e24STom Talpey } 170441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 171441e3e24STom Talpey 172c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 173c544577dSTrond Myklebust { 174c544577dSTrond Myklebust xprt->snd_task = NULL; 175c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 176c544577dSTrond Myklebust smp_mb__before_atomic(); 177c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 178c544577dSTrond Myklebust smp_mb__after_atomic(); 179c544577dSTrond Myklebust } else 180c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 181c544577dSTrond Myklebust } 182c544577dSTrond Myklebust 183441e3e24STom Talpey /** 18412a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 18512a80469SChuck Lever * @task: task that is requesting access to the transport 186177c27bfSRandy Dunlap * @xprt: pointer to the target transport 18712a80469SChuck Lever * 18812a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 18912a80469SChuck Lever * transport connects from colliding with writes. No congestion control 19012a80469SChuck Lever * is provided. 1911da177e4SLinus Torvalds */ 19243cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1931da177e4SLinus Torvalds { 19412a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 19512a80469SChuck Lever 19612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 19712a80469SChuck Lever if (task == xprt->snd_task) 19812a80469SChuck Lever return 1; 19912a80469SChuck Lever goto out_sleep; 20012a80469SChuck Lever } 201c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 202c544577dSTrond Myklebust goto out_unlock; 20312a80469SChuck Lever xprt->snd_task = task; 2044d4a76f3Sj223yang@asset.uwaterloo.ca 20512a80469SChuck Lever return 1; 20612a80469SChuck Lever 207c544577dSTrond Myklebust out_unlock: 208c544577dSTrond Myklebust xprt_clear_locked(xprt); 20912a80469SChuck Lever out_sleep: 21046121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 21112a80469SChuck Lever task->tk_pid, xprt); 212f05d54ecSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 21312a80469SChuck Lever task->tk_status = -EAGAIN; 21479c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 21512a80469SChuck Lever return 0; 21612a80469SChuck Lever } 21712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 21812a80469SChuck Lever 21975891f50STrond Myklebust static bool 22075891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 22175891f50STrond Myklebust { 22275891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 22375891f50STrond Myklebust } 22475891f50STrond Myklebust 22575891f50STrond Myklebust static void 22675891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 22775891f50STrond Myklebust { 22875891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 22975891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 23075891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 23175891f50STrond Myklebust rq_xmit)->rq_cong) 23275891f50STrond Myklebust return; 23375891f50STrond Myklebust } 23475891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 23575891f50STrond Myklebust } 23675891f50STrond Myklebust 23775891f50STrond Myklebust static void 23875891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 23975891f50STrond Myklebust { 24075891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 24175891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 24275891f50STrond Myklebust } 24375891f50STrond Myklebust 24412a80469SChuck Lever /* 24512a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 24612a80469SChuck Lever * @task: task that is requesting access to the transport 24712a80469SChuck Lever * 24812a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 24912a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 25012a80469SChuck Lever * woken up and given access to the transport. 25175891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 25212a80469SChuck Lever */ 25343cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 25412a80469SChuck Lever { 2551da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 2561da177e4SLinus Torvalds 2572226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2581da177e4SLinus Torvalds if (task == xprt->snd_task) 2591da177e4SLinus Torvalds return 1; 2601da177e4SLinus Torvalds goto out_sleep; 2611da177e4SLinus Torvalds } 26243cedbf0STrond Myklebust if (req == NULL) { 26343cedbf0STrond Myklebust xprt->snd_task = task; 26443cedbf0STrond Myklebust return 1; 26543cedbf0STrond Myklebust } 266c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 267c544577dSTrond Myklebust goto out_unlock; 26875891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 2691da177e4SLinus Torvalds xprt->snd_task = task; 2701da177e4SLinus Torvalds return 1; 2711da177e4SLinus Torvalds } 272c544577dSTrond Myklebust out_unlock: 273632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2741da177e4SLinus Torvalds out_sleep: 27546121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 276f05d54ecSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 2771da177e4SLinus Torvalds task->tk_status = -EAGAIN; 27879c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 28112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2821da177e4SLinus Torvalds 28312a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds int retval; 2861da177e4SLinus Torvalds 287bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 288bd79bc57STrond Myklebust return 1; 2894a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 29043cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2914a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2921da177e4SLinus Torvalds return retval; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 295961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2961da177e4SLinus Torvalds { 297961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 29849e9a890SChuck Lever 29949e9a890SChuck Lever xprt->snd_task = task; 300961a828dSTrond Myklebust return true; 301961a828dSTrond Myklebust } 302961a828dSTrond Myklebust 303961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 304961a828dSTrond Myklebust { 305961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 30649e9a890SChuck Lever return; 307c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 308c544577dSTrond Myklebust goto out_unlock; 309f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 310f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 311961a828dSTrond Myklebust return; 312c544577dSTrond Myklebust out_unlock: 313632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 31449e9a890SChuck Lever } 31549e9a890SChuck Lever 316961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 317961a828dSTrond Myklebust { 318961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 319961a828dSTrond Myklebust return; 320c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 321c544577dSTrond Myklebust goto out_unlock; 32275891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 323961a828dSTrond Myklebust goto out_unlock; 324f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 32575891f50STrond Myklebust __xprt_lock_write_func, xprt)) 326961a828dSTrond Myklebust return; 3271da177e4SLinus Torvalds out_unlock: 328632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 33149e9a890SChuck Lever /** 33249e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 33349e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 33449e9a890SChuck Lever * @task: task that is releasing access to the transport 33549e9a890SChuck Lever * 33649e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3371da177e4SLinus Torvalds */ 33849e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds if (xprt->snd_task == task) { 341632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3421da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds } 34512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3461da177e4SLinus Torvalds 34749e9a890SChuck Lever /** 34849e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 34949e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 35049e9a890SChuck Lever * @task: task that is releasing access to the transport 35149e9a890SChuck Lever * 35249e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 35349e9a890SChuck Lever * transport if the transport's congestion window allows it. 35449e9a890SChuck Lever */ 35549e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 35649e9a890SChuck Lever { 35749e9a890SChuck Lever if (xprt->snd_task == task) { 358632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 35949e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 36049e9a890SChuck Lever } 36149e9a890SChuck Lever } 36212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 36349e9a890SChuck Lever 36449e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3651da177e4SLinus Torvalds { 366bd79bc57STrond Myklebust if (xprt->snd_task != task) 367bd79bc57STrond Myklebust return; 3684a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 36949e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3704a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* 3741da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3751da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds static int 37875891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds if (req->rq_cong) 3811da177e4SLinus Torvalds return 1; 38246121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 38375891f50STrond Myklebust req->rq_task->tk_pid, xprt->cong, xprt->cwnd); 38475891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 38575891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 3861da177e4SLinus Torvalds return 0; 38775891f50STrond Myklebust } 3881da177e4SLinus Torvalds req->rq_cong = 1; 3891da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 3901da177e4SLinus Torvalds return 1; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 3951da177e4SLinus Torvalds * that has been sleeping due to congestion 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds static void 3981da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds if (!req->rq_cong) 4011da177e4SLinus Torvalds return; 4021da177e4SLinus Torvalds req->rq_cong = 0; 4031da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 40475891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 40549e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 40846c0ee8bSChuck Lever /** 40975891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 41075891f50STrond Myklebust * @xprt: pointer to transport 41175891f50STrond Myklebust * @req: pointer to RPC request 41275891f50STrond Myklebust * 41375891f50STrond Myklebust * Useful for transports that require congestion control. 41475891f50STrond Myklebust */ 41575891f50STrond Myklebust bool 41675891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 41775891f50STrond Myklebust { 41875891f50STrond Myklebust bool ret = false; 41975891f50STrond Myklebust 42075891f50STrond Myklebust if (req->rq_cong) 42175891f50STrond Myklebust return true; 42275891f50STrond Myklebust spin_lock_bh(&xprt->transport_lock); 42375891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 42475891f50STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 42575891f50STrond Myklebust return ret; 42675891f50STrond Myklebust } 42775891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 42875891f50STrond Myklebust 42975891f50STrond Myklebust /** 430a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 431a58dd398SChuck Lever * @task: RPC request that recently completed 432a58dd398SChuck Lever * 433a58dd398SChuck Lever * Useful for transports that require congestion control. 434a58dd398SChuck Lever */ 435a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 436a58dd398SChuck Lever { 437a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 438a4f0835cSTrond Myklebust 439a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 440a58dd398SChuck Lever } 44112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 442a58dd398SChuck Lever 44375891f50STrond Myklebust /* 44475891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 44575891f50STrond Myklebust * entry on xprt->sending 44675891f50STrond Myklebust */ 44775891f50STrond Myklebust static void 44875891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 44975891f50STrond Myklebust { 45075891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 45175891f50STrond Myklebust spin_lock_bh(&xprt->transport_lock); 45275891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 45375891f50STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 45475891f50STrond Myklebust } 45575891f50STrond Myklebust } 45675891f50STrond Myklebust 457a58dd398SChuck Lever /** 45846c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4596a24dfb6STrond Myklebust * @xprt: pointer to xprt 46046c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 46146c0ee8bSChuck Lever * @result: result code of completed RPC request 46246c0ee8bSChuck Lever * 4634f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4644f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4654f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4664f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4674f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4684f4cf5adSChuck Lever * 4694f4cf5adSChuck Lever * - a reply is received and 4704f4cf5adSChuck Lever * - a full number of requests are outstanding and 4714f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4721da177e4SLinus Torvalds */ 4736a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4741da177e4SLinus Torvalds { 47546c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 47646c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4791da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4801da177e4SLinus Torvalds * the result gets rounded properly. */ 4811da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4821da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4831da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 48449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4851da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4861da177e4SLinus Torvalds cwnd >>= 1; 4871da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4881da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4911da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4921da177e4SLinus Torvalds xprt->cwnd = cwnd; 49346c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4941da177e4SLinus Torvalds } 49512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4961da177e4SLinus Torvalds 49744fbac22SChuck Lever /** 49844fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 49944fbac22SChuck Lever * @xprt: transport with waiting tasks 50044fbac22SChuck Lever * @status: result code to plant in each task before waking it 50144fbac22SChuck Lever * 50244fbac22SChuck Lever */ 50344fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 50444fbac22SChuck Lever { 50544fbac22SChuck Lever if (status < 0) 50644fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 50744fbac22SChuck Lever else 50844fbac22SChuck Lever rpc_wake_up(&xprt->pending); 50944fbac22SChuck Lever } 51012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 51144fbac22SChuck Lever 512c7b2cae8SChuck Lever /** 513c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 514c544577dSTrond Myklebust * @xprt: transport 515a9a6b52eSTrond Myklebust * 516a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 517a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 518a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 519c7b2cae8SChuck Lever */ 520c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 521c7b2cae8SChuck Lever { 522c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 523c7b2cae8SChuck Lever } 52412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 525c7b2cae8SChuck Lever 526c544577dSTrond Myklebust static bool 527c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 528c544577dSTrond Myklebust { 529c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 530c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 531c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 532c544577dSTrond Myklebust "xprt %p\n", xprt); 533c544577dSTrond Myklebust return true; 534c544577dSTrond Myklebust } 535c544577dSTrond Myklebust return false; 536c544577dSTrond Myklebust } 537c544577dSTrond Myklebust 538c7b2cae8SChuck Lever /** 539c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 540c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 541c7b2cae8SChuck Lever * 542c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 543c7b2cae8SChuck Lever */ 544c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 545c7b2cae8SChuck Lever { 546c544577dSTrond Myklebust bool ret; 547c544577dSTrond Myklebust 548c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 549c544577dSTrond Myklebust return false; 550c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 551c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 552c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 553c544577dSTrond Myklebust return ret; 554c7b2cae8SChuck Lever } 55512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 556c7b2cae8SChuck Lever 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); 647a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 6481da177e4SLinus Torvalds 64966af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6504876cc77STrond Myklebust xprt->ops->close(xprt); 6511da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 65279234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 653a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6569903cd1cSChuck Lever /** 65762da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6589903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6599903cd1cSChuck Lever * 6601da177e4SLinus Torvalds */ 66162da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6621da177e4SLinus Torvalds { 6631da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6644a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6651da177e4SLinus Torvalds xprt_clear_connected(xprt); 666c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 66727adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 6684a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6691da177e4SLinus Torvalds } 67062da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6711da177e4SLinus Torvalds 67266af1e55STrond Myklebust /** 67366af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 67466af1e55STrond Myklebust * @xprt: transport to disconnect 67566af1e55STrond Myklebust * 67666af1e55STrond Myklebust */ 67766af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 67866af1e55STrond Myklebust { 67966af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 68066af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 68166af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 68266af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 68366af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 68440a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6850445f92cSTrond Myklebust else if (xprt->snd_task) 6860445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 6870445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 68866af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 68966af1e55STrond Myklebust } 690e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 69166af1e55STrond Myklebust 6927f3a1d1eSTrond Myklebust static unsigned int 6937f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 6947f3a1d1eSTrond Myklebust { 6957f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 6967f3a1d1eSTrond Myklebust } 6977f3a1d1eSTrond Myklebust 6987f3a1d1eSTrond Myklebust static bool 6997f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 7007f3a1d1eSTrond Myklebust { 7017f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 7027f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 7037f3a1d1eSTrond Myklebust 7047f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7057f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7067f3a1d1eSTrond Myklebust } 7077f3a1d1eSTrond Myklebust 7087c1d71cfSTrond Myklebust /** 7097c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 7107c1d71cfSTrond Myklebust * @xprt: transport to disconnect 7117c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 7127c1d71cfSTrond Myklebust * 7137c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 7147c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 7157c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 7167c1d71cfSTrond Myklebust * a batch of RPC requests. 7177c1d71cfSTrond Myklebust * 7187c1d71cfSTrond Myklebust */ 7197c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 7207c1d71cfSTrond Myklebust { 7217c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 7227c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 7237c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 7247c1d71cfSTrond Myklebust goto out; 7252c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 7267c1d71cfSTrond Myklebust goto out; 7277c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 7287c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 7297c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 73040a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7312a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 7327c1d71cfSTrond Myklebust out: 7337c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 7347c1d71cfSTrond Myklebust } 7357c1d71cfSTrond Myklebust 736ad3331acSTrond Myklebust static bool 737ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 738ad3331acSTrond Myklebust { 739ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 740ad3331acSTrond Myklebust } 741ad3331acSTrond Myklebust 742ad3331acSTrond Myklebust static void 743ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 744ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 745ad3331acSTrond Myklebust { 74695f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 747ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 748ad3331acSTrond Myklebust } 749ad3331acSTrond Myklebust 7501da177e4SLinus Torvalds static void 751ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 7521da177e4SLinus Torvalds { 753ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 7541da177e4SLinus Torvalds 7554a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 75695f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 7571da177e4SLinus Torvalds goto out_abort; 758ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 759ad3331acSTrond Myklebust xprt->last_used = jiffies; 7602226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7611da177e4SLinus Torvalds goto out_abort; 7624a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 76340a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7641da177e4SLinus Torvalds return; 7651da177e4SLinus Torvalds out_abort: 7664a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 769718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 770718ba5b8STrond Myklebust struct rpc_task *task, 771718ba5b8STrond Myklebust void *cookie) 772718ba5b8STrond Myklebust { 773718ba5b8STrond Myklebust bool ret = false; 774718ba5b8STrond Myklebust 775718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 776718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 777718ba5b8STrond Myklebust goto out; 778718ba5b8STrond Myklebust if (xprt->snd_task != task) 779718ba5b8STrond Myklebust goto out; 780718ba5b8STrond Myklebust xprt->snd_task = cookie; 781718ba5b8STrond Myklebust ret = true; 782718ba5b8STrond Myklebust out: 783718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 784718ba5b8STrond Myklebust return ret; 785718ba5b8STrond Myklebust } 786718ba5b8STrond Myklebust 787718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 788718ba5b8STrond Myklebust { 789718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 790718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 791718ba5b8STrond Myklebust goto out; 792718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 793718ba5b8STrond Myklebust goto out; 794718ba5b8STrond Myklebust xprt->snd_task =NULL; 795718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 796ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 797718ba5b8STrond Myklebust out: 798718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 79979234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 800718ba5b8STrond Myklebust } 801718ba5b8STrond Myklebust 8029903cd1cSChuck Lever /** 8039903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8049903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8051da177e4SLinus Torvalds * 8061da177e4SLinus Torvalds */ 8071da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8081da177e4SLinus Torvalds { 809ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8101da177e4SLinus Torvalds 81146121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 8121da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 8131da177e4SLinus Torvalds 814ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 81501d37c42STrond Myklebust task->tk_status = -EAGAIN; 8161da177e4SLinus Torvalds return; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 8191da177e4SLinus Torvalds return; 820feb8ca37STrond Myklebust 821feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 822feb8ca37STrond Myklebust xprt->ops->close(xprt); 823feb8ca37STrond Myklebust 824718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 825a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 8262c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 827abc13275STrond Myklebust rpc_sleep_on(&xprt->pending, task, NULL); 8280b9e7943STrond Myklebust 8290b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 8300b9e7943STrond Myklebust return; 8310b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 8320b9e7943STrond Myklebust return; 8330a9a4304STrond Myklebust /* Race breaker */ 8340a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 835262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 8361b092092STrond Myklebust xprt->ops->connect(xprt, task); 8370a9a4304STrond Myklebust } else { 8380a9a4304STrond Myklebust xprt_clear_connecting(xprt); 8390a9a4304STrond Myklebust task->tk_status = 0; 8400a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8410a9a4304STrond Myklebust } 8421da177e4SLinus Torvalds } 843718ba5b8STrond Myklebust xprt_release_write(xprt, task); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 84695f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 84795f7691dSTrond Myklebust XID_RB_EQUAL, 84895f7691dSTrond Myklebust XID_RB_LEFT, 84995f7691dSTrond Myklebust XID_RB_RIGHT, 85095f7691dSTrond Myklebust }; 85195f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 85295f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 85395f7691dSTrond Myklebust { 85495f7691dSTrond Myklebust if (xid1 == xid2) 85595f7691dSTrond Myklebust return XID_RB_EQUAL; 85695f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 85795f7691dSTrond Myklebust return XID_RB_LEFT; 85895f7691dSTrond Myklebust return XID_RB_RIGHT; 85995f7691dSTrond Myklebust } 86095f7691dSTrond Myklebust 86195f7691dSTrond Myklebust static struct rpc_rqst * 86295f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 86395f7691dSTrond Myklebust { 86495f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 86595f7691dSTrond Myklebust struct rpc_rqst *req; 86695f7691dSTrond Myklebust 86795f7691dSTrond Myklebust while (n != NULL) { 86895f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 86995f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 87095f7691dSTrond Myklebust case XID_RB_LEFT: 87195f7691dSTrond Myklebust n = n->rb_left; 87295f7691dSTrond Myklebust break; 87395f7691dSTrond Myklebust case XID_RB_RIGHT: 87495f7691dSTrond Myklebust n = n->rb_right; 87595f7691dSTrond Myklebust break; 87695f7691dSTrond Myklebust case XID_RB_EQUAL: 87795f7691dSTrond Myklebust return req; 87895f7691dSTrond Myklebust } 87995f7691dSTrond Myklebust } 88095f7691dSTrond Myklebust return NULL; 88195f7691dSTrond Myklebust } 88295f7691dSTrond Myklebust 88395f7691dSTrond Myklebust static void 88495f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 88595f7691dSTrond Myklebust { 88695f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 88795f7691dSTrond Myklebust struct rb_node *n = NULL; 88895f7691dSTrond Myklebust struct rpc_rqst *req; 88995f7691dSTrond Myklebust 89095f7691dSTrond Myklebust while (*p != NULL) { 89195f7691dSTrond Myklebust n = *p; 89295f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 89395f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 89495f7691dSTrond Myklebust case XID_RB_LEFT: 89595f7691dSTrond Myklebust p = &n->rb_left; 89695f7691dSTrond Myklebust break; 89795f7691dSTrond Myklebust case XID_RB_RIGHT: 89895f7691dSTrond Myklebust p = &n->rb_right; 89995f7691dSTrond Myklebust break; 90095f7691dSTrond Myklebust case XID_RB_EQUAL: 90195f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 90295f7691dSTrond Myklebust return; 90395f7691dSTrond Myklebust } 90495f7691dSTrond Myklebust } 90595f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 90695f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 90795f7691dSTrond Myklebust } 90895f7691dSTrond Myklebust 90995f7691dSTrond Myklebust static void 91095f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 91195f7691dSTrond Myklebust { 91295f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 91395f7691dSTrond Myklebust } 91495f7691dSTrond Myklebust 9159903cd1cSChuck Lever /** 9169903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 9179903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 9189903cd1cSChuck Lever * @xid: RPC XID of incoming reply 9199903cd1cSChuck Lever * 92075c84151STrond Myklebust * Caller holds xprt->queue_lock. 9211da177e4SLinus Torvalds */ 922d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 9231da177e4SLinus Torvalds { 9248f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 9251da177e4SLinus Torvalds 92695f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 92795f7691dSTrond Myklebust if (entry != NULL) { 9283705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 9290b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 930262ca07dSChuck Lever return entry; 9313705ad64SJeff Layton } 93246121cf7SChuck Lever 93346121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 93446121cf7SChuck Lever ntohl(xid)); 9353705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 936262ca07dSChuck Lever xprt->stat.bad_xids++; 937262ca07dSChuck Lever return NULL; 9381da177e4SLinus Torvalds } 93912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 9401da177e4SLinus Torvalds 941cf9946cdSTrond Myklebust static bool 942cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 943cf9946cdSTrond Myklebust { 944cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 945cf9946cdSTrond Myklebust } 946cf9946cdSTrond Myklebust 947729749bbSTrond Myklebust /** 948729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 949729749bbSTrond Myklebust * @req: Request to pin 950729749bbSTrond Myklebust * 951729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 952cf9946cdSTrond Myklebust * so should be holding the xprt receive lock. 953729749bbSTrond Myklebust */ 954729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 955729749bbSTrond Myklebust { 956cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 957729749bbSTrond Myklebust } 9589590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 959729749bbSTrond Myklebust 960729749bbSTrond Myklebust /** 961729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 962729749bbSTrond Myklebust * @req: Request to pin 963729749bbSTrond Myklebust * 964cf9946cdSTrond Myklebust * Caller should be holding the xprt receive lock. 965729749bbSTrond Myklebust */ 966729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 967729749bbSTrond Myklebust { 968cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 969cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 970cf9946cdSTrond Myklebust return; 971cf9946cdSTrond Myklebust } 972cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 973cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 974729749bbSTrond Myklebust } 9759590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 976729749bbSTrond Myklebust 977729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 978729749bbSTrond Myklebust { 979cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 980729749bbSTrond Myklebust } 981729749bbSTrond Myklebust 982edc81dcdSTrond Myklebust static bool 983edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 984edc81dcdSTrond Myklebust { 985edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 986edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 987edc81dcdSTrond Myklebust } 988edc81dcdSTrond Myklebust 989edc81dcdSTrond Myklebust static bool 990edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 991edc81dcdSTrond Myklebust { 992edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 993edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 994edc81dcdSTrond Myklebust } 995edc81dcdSTrond Myklebust 996edc81dcdSTrond Myklebust /** 997edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 998edc81dcdSTrond Myklebust * @task: RPC task 999edc81dcdSTrond Myklebust * 1000edc81dcdSTrond Myklebust */ 1001edc81dcdSTrond Myklebust void 1002edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1003edc81dcdSTrond Myklebust { 1004edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1005edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1006edc81dcdSTrond Myklebust 1007edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1008edc81dcdSTrond Myklebust return; 1009edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1010edc81dcdSTrond Myklebust 1011edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1012edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1013edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1014edc81dcdSTrond Myklebust 1015edc81dcdSTrond Myklebust /* Add request to the receive list */ 101695f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1017edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1018edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1019edc81dcdSTrond Myklebust 1020edc81dcdSTrond Myklebust xprt_reset_majortimeo(req); 1021edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1022edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1023edc81dcdSTrond Myklebust } 1024edc81dcdSTrond Myklebust 1025edc81dcdSTrond Myklebust /** 1026edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1027edc81dcdSTrond Myklebust * @task: RPC task 1028edc81dcdSTrond Myklebust * 1029edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1030edc81dcdSTrond Myklebust */ 1031edc81dcdSTrond Myklebust static void 1032edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1033edc81dcdSTrond Myklebust { 103495f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 103595f7691dSTrond Myklebust 1036edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 103795f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1038edc81dcdSTrond Myklebust } 1039edc81dcdSTrond Myklebust 1040ecd465eeSChuck Lever /** 1041ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1042ecd465eeSChuck Lever * @task: RPC request that recently completed 1043ecd465eeSChuck Lever * 104475c84151STrond Myklebust * Caller holds xprt->queue_lock. 1045ecd465eeSChuck Lever */ 1046ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 10471da177e4SLinus Torvalds { 10481570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 10491570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 105095c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1051d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 10521570c1e4SChuck Lever 10531da177e4SLinus Torvalds if (timer) { 10541da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1055ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 10561570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds } 1059ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 10601da177e4SLinus Torvalds 10611570c1e4SChuck Lever /** 10621570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 10631570c1e4SChuck Lever * @task: RPC request that recently completed 10641570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 10651570c1e4SChuck Lever * 106675c84151STrond Myklebust * Caller holds xprt->queue_lock. 10671570c1e4SChuck Lever */ 10681570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 10691570c1e4SChuck Lever { 10701570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1071fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 10721da177e4SLinus Torvalds 10731570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 10741570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 10753705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 10761da177e4SLinus Torvalds 1077fda13939STrond Myklebust xprt->stat.recvs++; 1078ef759a2eSChuck Lever 10791e799b67STrond Myklebust req->rq_private_buf.len = copied; 1080dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1081dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 108243ac3f29STrond Myklebust smp_wmb(); 1083dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1084edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1085fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 10861da177e4SLinus Torvalds } 108712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 10881da177e4SLinus Torvalds 108946c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 10921da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 10931da177e4SLinus Torvalds 10945d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 10955d00837bSTrond Myklebust return; 109646c0ee8bSChuck Lever 109782476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1098dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 109946c0ee8bSChuck Lever if (xprt->ops->timer) 11006a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 11015d00837bSTrond Myklebust } else 11025d00837bSTrond Myklebust task->tk_status = 0; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11059903cd1cSChuck Lever /** 11067f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 11077f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 11087f3a1d1eSTrond Myklebust * 11097f3a1d1eSTrond Myklebust */ 11107f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 11117f3a1d1eSTrond Myklebust { 11127f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11137f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11147f3a1d1eSTrond Myklebust 11157f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 11167f3a1d1eSTrond Myklebust return; 11177f3a1d1eSTrond Myklebust /* 11187f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 11197f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 11207f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 11217f3a1d1eSTrond Myklebust */ 11227f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 11237f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 11247f3a1d1eSTrond Myklebust xprt->ops->set_retrans_timeout(task); 11257f3a1d1eSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 11267f3a1d1eSTrond Myklebust /* 11277f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 11287f3a1d1eSTrond Myklebust * connection was dropped in case the call to 11297f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 11307f3a1d1eSTrond Myklebust */ 11317f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 11327f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 11337f3a1d1eSTrond Myklebust task, -ENOTCONN); 11347f3a1d1eSTrond Myklebust } 11357f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 11367f3a1d1eSTrond Myklebust } 11377f3a1d1eSTrond Myklebust 1138944b0429STrond Myklebust static bool 1139944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1140944b0429STrond Myklebust { 1141762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1142944b0429STrond Myklebust } 1143944b0429STrond Myklebust 1144944b0429STrond Myklebust /** 1145944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1146944b0429STrond Myklebust * @task: pointer to rpc_task 1147944b0429STrond Myklebust * 1148944b0429STrond Myklebust * Add a task to the transmission queue. 1149944b0429STrond Myklebust */ 1150944b0429STrond Myklebust void 1151944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1152944b0429STrond Myklebust { 1153918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1154944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1155944b0429STrond Myklebust 1156944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1157e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1158944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 115975891f50STrond Myklebust /* 116075891f50STrond Myklebust * Requests that carry congestion control credits are added 116175891f50STrond Myklebust * to the head of the list to avoid starvation issues. 116275891f50STrond Myklebust */ 116375891f50STrond Myklebust if (req->rq_cong) { 116475891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 116575891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 116675891f50STrond Myklebust if (pos->rq_cong) 116775891f50STrond Myklebust continue; 116875891f50STrond Myklebust /* Note: req is added _before_ pos */ 116975891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 117075891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 11710c77668dSChuck Lever trace_xprt_enq_xmit(task, 1); 117275891f50STrond Myklebust goto out; 117375891f50STrond Myklebust } 117486aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 117586aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 117686aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 117786aeee0eSTrond Myklebust continue; 117886aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 117986aeee0eSTrond Myklebust continue; 118086aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 118186aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 118286aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 11830c77668dSChuck Lever trace_xprt_enq_xmit(task, 2); 118486aeee0eSTrond Myklebust goto out; 118586aeee0eSTrond Myklebust } 1186deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1187918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1188918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1189918f3c1fSTrond Myklebust continue; 1190918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1191918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 11920c77668dSChuck Lever trace_xprt_enq_xmit(task, 3); 1193918f3c1fSTrond Myklebust goto out; 1194918f3c1fSTrond Myklebust } 119575891f50STrond Myklebust } 1196944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1197918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 11980c77668dSChuck Lever trace_xprt_enq_xmit(task, 4); 1199918f3c1fSTrond Myklebust out: 1200944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1201944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1202944b0429STrond Myklebust } 1203944b0429STrond Myklebust } 1204944b0429STrond Myklebust 1205944b0429STrond Myklebust /** 1206944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1207944b0429STrond Myklebust * @task: pointer to rpc_task 1208944b0429STrond Myklebust * 1209944b0429STrond Myklebust * Remove a task from the transmission queue 1210944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1211944b0429STrond Myklebust */ 1212944b0429STrond Myklebust static void 1213944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1214944b0429STrond Myklebust { 1215918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1216918f3c1fSTrond Myklebust 1217918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1218918f3c1fSTrond Myklebust return; 1219918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1220918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1221918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1222918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1223918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1224918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1225918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1226918f3c1fSTrond Myklebust } 1227918f3c1fSTrond Myklebust } else 1228918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1229944b0429STrond Myklebust } 1230944b0429STrond Myklebust 1231944b0429STrond Myklebust /** 1232944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1233944b0429STrond Myklebust * @task: pointer to rpc_task 1234944b0429STrond Myklebust * 1235944b0429STrond Myklebust * Remove a task from the transmission queue 1236944b0429STrond Myklebust */ 1237944b0429STrond Myklebust static void 1238944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1239944b0429STrond Myklebust { 1240944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1241944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1242944b0429STrond Myklebust 1243944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1244944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1245944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1246944b0429STrond Myklebust } 1247944b0429STrond Myklebust 12487f3a1d1eSTrond Myklebust /** 12499d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 12509d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 12519d96acbcSTrond Myklebust * 12529d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 12539d96acbcSTrond Myklebust * the request for transmission or receive. 12549d96acbcSTrond Myklebust */ 12559d96acbcSTrond Myklebust void 12569d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 12579d96acbcSTrond Myklebust { 12589d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12599d96acbcSTrond Myklebust 12609d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 12619d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 12629d96acbcSTrond Myklebust } 12639d96acbcSTrond Myklebust 12649d96acbcSTrond Myklebust /** 1265762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1266762e4e67STrond Myklebust * @task: pointer to rpc_task 1267762e4e67STrond Myklebust * 1268762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1269762e4e67STrond Myklebust */ 1270762e4e67STrond Myklebust bool 1271762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1272762e4e67STrond Myklebust { 1273762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1274762e4e67STrond Myklebust } 1275762e4e67STrond Myklebust 1276762e4e67STrond Myklebust /** 12779903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 12789903cd1cSChuck Lever * @task: RPC task about to send a request 12799903cd1cSChuck Lever * 12801da177e4SLinus Torvalds */ 128190051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12841da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 12851da177e4SLinus Torvalds 128646121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 12871da177e4SLinus Torvalds 12885f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 12895f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1290944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 12915f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 12925f2f6bd9STrond Myklebust task, 0); 12935f2f6bd9STrond Myklebust return false; 12945f2f6bd9STrond Myklebust 12958a19a0b6STrond Myklebust } 12965f2f6bd9STrond Myklebust return true; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 1299e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 13005e5ce5beSTrond Myklebust { 1301343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 13025e5ce5beSTrond Myklebust } 13035e5ce5beSTrond Myklebust 13049903cd1cSChuck Lever /** 130589f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 130689f90fe1STrond Myklebust * @req: pointer to request to transmit 130789f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 13089903cd1cSChuck Lever * 130989f90fe1STrond Myklebust * This performs the transmission of a single request. 131089f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 131189f90fe1STrond Myklebust * does need to be pinned. 131289f90fe1STrond Myklebust * Returns '0' on success. 13139903cd1cSChuck Lever */ 131489f90fe1STrond Myklebust static int 131589f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 13161da177e4SLinus Torvalds { 13171da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 131889f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 131990d91b0cSTrond Myklebust unsigned int connect_cookie; 1320dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1321ff699ea8SChuck Lever int status; 13221da177e4SLinus Torvalds 1323edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 132489f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 132589f90fe1STrond Myklebust status = 0; 1326944b0429STrond Myklebust goto out_dequeue; 132789f90fe1STrond Myklebust } 13283021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1329edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 133089f90fe1STrond Myklebust status = -EBADMSG; 1331944b0429STrond Myklebust goto out_dequeue; 13323021a5bbSTrond Myklebust } 1333a79f194aSTrond Myklebust if (task->tk_ops->rpc_call_prepare_transmit) { 1334a79f194aSTrond Myklebust task->tk_ops->rpc_call_prepare_transmit(task, 1335a79f194aSTrond Myklebust task->tk_calldata); 1336a79f194aSTrond Myklebust status = task->tk_status; 1337a79f194aSTrond Myklebust if (status < 0) 1338a79f194aSTrond Myklebust goto out_dequeue; 1339a79f194aSTrond Myklebust } 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 1342dcbbeda8STrond Myklebust /* 1343dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1344dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1345dcbbeda8STrond Myklebust * reply to the first transmission. 1346dcbbeda8STrond Myklebust */ 1347dcbbeda8STrond Myklebust req->rq_ntrans++; 1348dcbbeda8STrond Myklebust 134990d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1350adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1351c8485e4dSTrond Myklebust if (status != 0) { 1352dcbbeda8STrond Myklebust req->rq_ntrans--; 13530c77668dSChuck Lever trace_xprt_transmit(req, status); 135489f90fe1STrond Myklebust return status; 1355c8485e4dSTrond Myklebust } 13567ebbbc6eSTrond Myklebust 1357dcbbeda8STrond Myklebust if (is_retrans) 1358dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1359dcbbeda8STrond Myklebust 13604a068258SChuck Lever xprt_inject_disconnect(xprt); 1361c8485e4dSTrond Myklebust 1362468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1363fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 1364262ca07dSChuck Lever 1365262ca07dSChuck Lever xprt->stat.sends++; 1366262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1367262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 136815a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 136915a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1370fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 137190d91b0cSTrond Myklebust 137290d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1373944b0429STrond Myklebust out_dequeue: 13740c77668dSChuck Lever trace_xprt_transmit(req, status); 1375944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 137689f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 137789f90fe1STrond Myklebust return status; 137889f90fe1STrond Myklebust } 137989f90fe1STrond Myklebust 138089f90fe1STrond Myklebust /** 138189f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 138289f90fe1STrond Myklebust * @task: controlling RPC task 138389f90fe1STrond Myklebust * 138489f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 138589f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 138689f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 138789f90fe1STrond Myklebust * received a reply. 138889f90fe1STrond Myklebust */ 138989f90fe1STrond Myklebust void 139089f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 139189f90fe1STrond Myklebust { 139289f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 139389f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 139489f90fe1STrond Myklebust int status; 139589f90fe1STrond Myklebust 139689f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 139789f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 139889f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 139989f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 140089f90fe1STrond Myklebust xprt_pin_rqst(next); 140189f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 140289f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 140389f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 140489f90fe1STrond Myklebust status = 0; 140589f90fe1STrond Myklebust cond_resched(); 140689f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 140789f90fe1STrond Myklebust xprt_unpin_rqst(next); 140889f90fe1STrond Myklebust if (status == 0) { 140989f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 141089f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 141189f90fe1STrond Myklebust continue; 1412c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 141389f90fe1STrond Myklebust task->tk_status = status; 141489f90fe1STrond Myklebust break; 141589f90fe1STrond Myklebust } 141689f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 1419ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1420ba60eb25STrond Myklebust { 1421ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1422ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1423ba60eb25STrond Myklebust } 1424ba60eb25STrond Myklebust 1425ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1426ba60eb25STrond Myklebust { 1427ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1428ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1429ba60eb25STrond Myklebust } 1430ba60eb25STrond Myklebust 1431ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1432ba60eb25STrond Myklebust { 1433ba60eb25STrond Myklebust bool ret = false; 1434ba60eb25STrond Myklebust 1435ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1436ba60eb25STrond Myklebust goto out; 1437ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1438ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1439ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1440ba60eb25STrond Myklebust ret = true; 1441ba60eb25STrond Myklebust } 1442ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1443ba60eb25STrond Myklebust out: 1444ba60eb25STrond Myklebust return ret; 1445ba60eb25STrond Myklebust } 1446ba60eb25STrond Myklebust 144792ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1448d9ba131dSTrond Myklebust { 1449d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1450d9ba131dSTrond Myklebust 1451ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1452d9ba131dSTrond Myklebust goto out; 1453ff699ea8SChuck Lever ++xprt->num_reqs; 145492ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 145592ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 145692ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1457d9ba131dSTrond Myklebust if (req != NULL) 1458d9ba131dSTrond Myklebust goto out; 1459ff699ea8SChuck Lever --xprt->num_reqs; 1460d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1461d9ba131dSTrond Myklebust out: 1462d9ba131dSTrond Myklebust return req; 1463d9ba131dSTrond Myklebust } 1464d9ba131dSTrond Myklebust 1465d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1466d9ba131dSTrond Myklebust { 1467ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1468ff699ea8SChuck Lever --xprt->num_reqs; 1469d9ba131dSTrond Myklebust kfree(req); 1470d9ba131dSTrond Myklebust return true; 1471d9ba131dSTrond Myklebust } 1472d9ba131dSTrond Myklebust return false; 1473d9ba131dSTrond Myklebust } 1474d9ba131dSTrond Myklebust 1475f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 14761da177e4SLinus Torvalds { 1477d9ba131dSTrond Myklebust struct rpc_rqst *req; 14781da177e4SLinus Torvalds 1479f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 14801da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1481d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1482d9ba131dSTrond Myklebust list_del(&req->rq_list); 1483d9ba131dSTrond Myklebust goto out_init_req; 1484d9ba131dSTrond Myklebust } 148592ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1486d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1487d9ba131dSTrond Myklebust goto out_init_req; 1488d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1489d9ba131dSTrond Myklebust case -ENOMEM: 1490d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1491d9ba131dSTrond Myklebust "failed! Retrying\n"); 14921afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1493d9ba131dSTrond Myklebust break; 1494d9ba131dSTrond Myklebust case -EAGAIN: 1495ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1496d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1497e9d47639SGustavo A. R. Silva /* fall through */ 14981afeaf5cSTrond Myklebust default: 1499d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 15001afeaf5cSTrond Myklebust } 1501f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1502d9ba131dSTrond Myklebust return; 1503d9ba131dSTrond Myklebust out_init_req: 1504ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1505ff699ea8SChuck Lever xprt->num_reqs); 150637ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 150737ac86c3SChuck Lever 1508d9ba131dSTrond Myklebust task->tk_status = 0; 15091da177e4SLinus Torvalds task->tk_rqstp = req; 15101da177e4SLinus Torvalds } 1511f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1512f39c1bfbSTrond Myklebust 1513a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1514ee5ebe85STrond Myklebust { 1515ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1516c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1517c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1518ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1519c25573b5STrond Myklebust } 1520ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1521ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1522ee5ebe85STrond Myklebust } 1523a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1524ee5ebe85STrond Myklebust 152521de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 152621de0a95STrond Myklebust { 152721de0a95STrond Myklebust struct rpc_rqst *req; 152821de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 152921de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 153021de0a95STrond Myklebust list_del(&req->rq_list); 153121de0a95STrond Myklebust kfree(req); 153221de0a95STrond Myklebust } 153321de0a95STrond Myklebust } 153421de0a95STrond Myklebust 1535d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1536d9ba131dSTrond Myklebust unsigned int num_prealloc, 1537d9ba131dSTrond Myklebust unsigned int max_alloc) 1538bd1722d4SPavel Emelyanov { 1539bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 154021de0a95STrond Myklebust struct rpc_rqst *req; 154121de0a95STrond Myklebust int i; 1542bd1722d4SPavel Emelyanov 1543bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1544bd1722d4SPavel Emelyanov if (xprt == NULL) 1545bd1722d4SPavel Emelyanov goto out; 1546bd1722d4SPavel Emelyanov 154721de0a95STrond Myklebust xprt_init(xprt, net); 154821de0a95STrond Myklebust 154921de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 155021de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 155121de0a95STrond Myklebust if (!req) 15528313164cSwangweidong goto out_free; 155321de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 155421de0a95STrond Myklebust } 1555d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1556d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1557d9ba131dSTrond Myklebust else 155821de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1559d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1560ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1561bd1722d4SPavel Emelyanov 1562bd1722d4SPavel Emelyanov return xprt; 1563bd1722d4SPavel Emelyanov 1564bd1722d4SPavel Emelyanov out_free: 156521de0a95STrond Myklebust xprt_free(xprt); 1566bd1722d4SPavel Emelyanov out: 1567bd1722d4SPavel Emelyanov return NULL; 1568bd1722d4SPavel Emelyanov } 1569bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1570bd1722d4SPavel Emelyanov 1571e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1572e204e621SPavel Emelyanov { 157337aa2133SPavel Emelyanov put_net(xprt->xprt_net); 157421de0a95STrond Myklebust xprt_free_all_slots(xprt); 1575fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1576e204e621SPavel Emelyanov } 1577e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1578e204e621SPavel Emelyanov 1579902c5887STrond Myklebust static void 1580902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1581902c5887STrond Myklebust { 1582902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1583902c5887STrond Myklebust } 1584902c5887STrond Myklebust 15859dc6edcfSTrond Myklebust static __be32 15869dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 15879dc6edcfSTrond Myklebust { 15889dc6edcfSTrond Myklebust __be32 xid; 15899dc6edcfSTrond Myklebust 15909dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 15919dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 15929dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 15939dc6edcfSTrond Myklebust return xid; 15949dc6edcfSTrond Myklebust } 15959dc6edcfSTrond Myklebust 15969dc6edcfSTrond Myklebust static void 15979dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 15989dc6edcfSTrond Myklebust { 15999dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 16009dc6edcfSTrond Myklebust } 16019dc6edcfSTrond Myklebust 16029dc6edcfSTrond Myklebust static void 16039dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 16049dc6edcfSTrond Myklebust { 16059dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16069dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 16079dc6edcfSTrond Myklebust 16089dc6edcfSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 16099dc6edcfSTrond Myklebust req->rq_task = task; 16109dc6edcfSTrond Myklebust req->rq_xprt = xprt; 16119dc6edcfSTrond Myklebust req->rq_buffer = NULL; 16129dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1613902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 16149dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 16159dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 16169dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 16179dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 161871700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 161971700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 16209dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 16219dc6edcfSTrond Myklebust xprt_reset_majortimeo(req); 16229dc6edcfSTrond Myklebust dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 16239dc6edcfSTrond Myklebust req, ntohl(req->rq_xid)); 16249dc6edcfSTrond Myklebust } 16259dc6edcfSTrond Myklebust 16269dc6edcfSTrond Myklebust static void 16279dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 16289dc6edcfSTrond Myklebust { 16299dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 16309dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 16319dc6edcfSTrond Myklebust xprt_request_init(task); 16329dc6edcfSTrond Myklebust } 16339dc6edcfSTrond Myklebust 16349903cd1cSChuck Lever /** 16359903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 16369903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 16379903cd1cSChuck Lever * 1638ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1639ba60eb25STrond Myklebust * slots are available, place the task on the transport's 16409903cd1cSChuck Lever * backlog queue. 16419903cd1cSChuck Lever */ 16429903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 16431da177e4SLinus Torvalds { 1644fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16451da177e4SLinus Torvalds 164643cedbf0STrond Myklebust task->tk_status = 0; 164743cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 164843cedbf0STrond Myklebust return; 164943cedbf0STrond Myklebust 165043cedbf0STrond Myklebust task->tk_timeout = 0; 165143cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1652ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 16539dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1654ba60eb25STrond Myklebust } 1655ba60eb25STrond Myklebust 1656ba60eb25STrond Myklebust /** 1657ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1658ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1659ba60eb25STrond Myklebust * 1660ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1661ba60eb25STrond Myklebust * backlog queue. 1662ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1663ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1664ba60eb25STrond Myklebust */ 1665ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1666ba60eb25STrond Myklebust { 1667fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1668ba60eb25STrond Myklebust 1669ba60eb25STrond Myklebust task->tk_status = 0; 1670ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1671ba60eb25STrond Myklebust return; 1672ba60eb25STrond Myklebust 1673ba60eb25STrond Myklebust task->tk_timeout = 0; 1674ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 16759dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 1678edc81dcdSTrond Myklebust static void 1679edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req) 1680edc81dcdSTrond Myklebust { 1681edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1682edc81dcdSTrond Myklebust 1683944b0429STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1684944b0429STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1685edc81dcdSTrond Myklebust xprt_is_pinned_rqst(req)) { 1686edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1687944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1688edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1689edc81dcdSTrond Myklebust while (xprt_is_pinned_rqst(req)) { 1690edc81dcdSTrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1691edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1692edc81dcdSTrond Myklebust xprt_wait_on_pinned_rqst(req); 1693edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1694edc81dcdSTrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1695edc81dcdSTrond Myklebust } 1696edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1697edc81dcdSTrond Myklebust } 1698edc81dcdSTrond Myklebust } 1699edc81dcdSTrond Myklebust 17009903cd1cSChuck Lever /** 17019903cd1cSChuck Lever * xprt_release - release an RPC request slot 17029903cd1cSChuck Lever * @task: task which is finished with the slot 17039903cd1cSChuck Lever * 17041da177e4SLinus Torvalds */ 17059903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 17061da177e4SLinus Torvalds { 170755ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 170887ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17091da177e4SLinus Torvalds 171087ed5003STrond Myklebust if (req == NULL) { 171187ed5003STrond Myklebust if (task->tk_client) { 1712fb43d172STrond Myklebust xprt = task->tk_xprt; 171387ed5003STrond Myklebust xprt_release_write(xprt, task); 171487ed5003STrond Myklebust } 17151da177e4SLinus Torvalds return; 171687ed5003STrond Myklebust } 171755ae1aabSRicardo Labiaga 171855ae1aabSRicardo Labiaga xprt = req->rq_xprt; 17190a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 17200a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 17210a702195SWeston Andros Adamson else if (task->tk_client) 17220a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 1723edc81dcdSTrond Myklebust xprt_request_dequeue_all(task, req); 17244a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 172549e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1726a58dd398SChuck Lever if (xprt->ops->release_request) 1727a58dd398SChuck Lever xprt->ops->release_request(task); 17281da177e4SLinus Torvalds xprt->last_used = jiffies; 1729ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 17304a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1731ee5ebe85STrond Myklebust if (req->rq_buffer) 17323435c74aSChuck Lever xprt->ops->buf_free(task); 17334a068258SChuck Lever xprt_inject_disconnect(xprt); 17349d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 17350472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1736a17c2153STrond Myklebust if (req->rq_cred != NULL) 1737a17c2153STrond Myklebust put_rpccred(req->rq_cred); 17381da177e4SLinus Torvalds task->tk_rqstp = NULL; 1739ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1740ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 174155ae1aabSRicardo Labiaga 174246121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1743ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1744a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1745ee5ebe85STrond Myklebust else 1746c9acb42eSTrond Myklebust xprt_free_bc_request(req); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 1749902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1750902c5887STrond Myklebust void 1751902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1752902c5887STrond Myklebust { 1753902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1754902c5887STrond Myklebust 1755902c5887STrond Myklebust task->tk_rqstp = req; 1756902c5887STrond Myklebust req->rq_task = task; 1757902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1758902c5887STrond Myklebust /* 1759902c5887STrond Myklebust * Set up the xdr_buf length. 1760902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1761902c5887STrond Myklebust */ 1762902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1763902c5887STrond Myklebust xbufp->tail[0].iov_len; 1764902c5887STrond Myklebust } 1765902c5887STrond Myklebust #endif 1766902c5887STrond Myklebust 176721de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1768c2866763SChuck Lever { 176930c5116bSTrond Myklebust kref_init(&xprt->kref); 1770c2866763SChuck Lever 1771c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1772c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 177375c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1774c2866763SChuck Lever 1775c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 177695f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1777944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 17789e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1779f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1780f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 17819e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 178280b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1783f9acac1aSRicardo Labiaga 1784c2866763SChuck Lever xprt->last_used = jiffies; 1785c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1786a509050bSChuck Lever xprt->bind_index = 0; 1787c2866763SChuck Lever 1788c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1789c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 179079c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1791c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1792c2866763SChuck Lever 1793c2866763SChuck Lever xprt_init_xid(xprt); 1794c2866763SChuck Lever 179521de0a95STrond Myklebust xprt->xprt_net = get_net(net); 17968d9266ffSTrond Myklebust } 17978d9266ffSTrond Myklebust 17988d9266ffSTrond Myklebust /** 17998d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 18008d9266ffSTrond Myklebust * @args: rpc transport creation arguments 18018d9266ffSTrond Myklebust * 18028d9266ffSTrond Myklebust */ 18038d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 18048d9266ffSTrond Myklebust { 18058d9266ffSTrond Myklebust struct rpc_xprt *xprt; 18068d9266ffSTrond Myklebust struct xprt_class *t; 18078d9266ffSTrond Myklebust 18088d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 18098d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 18108d9266ffSTrond Myklebust if (t->ident == args->ident) { 18118d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18128d9266ffSTrond Myklebust goto found; 18138d9266ffSTrond Myklebust } 18148d9266ffSTrond Myklebust } 18158d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18163c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 18178d9266ffSTrond Myklebust return ERR_PTR(-EIO); 18188d9266ffSTrond Myklebust 18198d9266ffSTrond Myklebust found: 18208d9266ffSTrond Myklebust xprt = t->setup(args); 18218d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 18228d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 18238d9266ffSTrond Myklebust -PTR_ERR(xprt)); 182421de0a95STrond Myklebust goto out; 18258d9266ffSTrond Myklebust } 182633d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 182733d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 182821de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 182921de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1830ff861c4dSKees Cook timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 183121de0a95STrond Myklebust else 1832ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 18334e0038b6STrond Myklebust 18344e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 18354e0038b6STrond Myklebust xprt_destroy(xprt); 18364e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 18374e0038b6STrond Myklebust } 18384e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 18394e0038b6STrond Myklebust if (xprt->servername == NULL) { 18404e0038b6STrond Myklebust xprt_destroy(xprt); 18414e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 18424e0038b6STrond Myklebust } 18434e0038b6STrond Myklebust 18443f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1845388f0c77SJeff Layton 1846c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1847c2866763SChuck Lever xprt->max_reqs); 184821de0a95STrond Myklebust out: 1849c2866763SChuck Lever return xprt; 1850c2866763SChuck Lever } 1851c2866763SChuck Lever 1852528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 1853528fd354STrond Myklebust { 1854528fd354STrond Myklebust struct rpc_xprt *xprt = 1855528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 1856528fd354STrond Myklebust 1857528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 1858528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1859528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1860528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1861528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1862528fd354STrond Myklebust kfree(xprt->servername); 1863528fd354STrond Myklebust /* 1864528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 1865528fd354STrond Myklebust */ 1866528fd354STrond Myklebust xprt->ops->destroy(xprt); 1867528fd354STrond Myklebust } 1868528fd354STrond Myklebust 18699903cd1cSChuck Lever /** 18709903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1871a8de240aSTrond Myklebust * @xprt: transport to destroy 18729903cd1cSChuck Lever * 18731da177e4SLinus Torvalds */ 1874a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 18751da177e4SLinus Torvalds { 18761da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 187779234c3dSTrond Myklebust 1878528fd354STrond Myklebust /* 1879528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 1880528fd354STrond Myklebust */ 188179234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 188279234c3dSTrond Myklebust 18830065db32STrond Myklebust del_timer_sync(&xprt->timer); 1884c8541ecdSChuck Lever 1885c8541ecdSChuck Lever /* 1886528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 1887528fd354STrond Myklebust * safely flush receive work running on rpciod. 1888c8541ecdSChuck Lever */ 1889528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 1890528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 18916b6ca86bSTrond Myklebust } 18921da177e4SLinus Torvalds 189330c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 189430c5116bSTrond Myklebust { 189530c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 189630c5116bSTrond Myklebust } 189730c5116bSTrond Myklebust 189830c5116bSTrond Myklebust /** 189930c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 190030c5116bSTrond Myklebust * @xprt: pointer to the transport 190130c5116bSTrond Myklebust * 190230c5116bSTrond Myklebust */ 190330c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 190430c5116bSTrond Myklebust { 190530c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 190630c5116bSTrond Myklebust return xprt; 190730c5116bSTrond Myklebust return NULL; 190830c5116bSTrond Myklebust } 190930c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 191030c5116bSTrond Myklebust 19116b6ca86bSTrond Myklebust /** 19126b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 19136b6ca86bSTrond Myklebust * @xprt: pointer to the transport 19146b6ca86bSTrond Myklebust * 19156b6ca86bSTrond Myklebust */ 19166b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 19176b6ca86bSTrond Myklebust { 191830c5116bSTrond Myklebust if (xprt != NULL) 191930c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 19206b6ca86bSTrond Myklebust } 19215d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 1922