1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 61da177e4SLinus Torvalds * and asynchronous calls. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * The interface works like this: 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 111da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 121da177e4SLinus Torvalds * (xprt_reserve). 131da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1455aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1555aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1655ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1755ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1855ae1aabSRicardo Labiaga * expired. 191da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 2055aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 211da177e4SLinus Torvalds * caller is woken up, and the timer removed. 221da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 231da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 241da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 251da177e4SLinus Torvalds * of -ETIMEDOUT. 261da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 271da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 281da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 291da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 301da177e4SLinus Torvalds * again. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 331da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 341da177e4SLinus Torvalds * tasks that rely on callbacks. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3755aa4f58SChuck Lever * 3855aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41a246b010SChuck Lever #include <linux/module.h> 42a246b010SChuck Lever 431da177e4SLinus Torvalds #include <linux/types.h> 44a246b010SChuck Lever #include <linux/interrupt.h> 451da177e4SLinus Torvalds #include <linux/workqueue.h> 46bf3fcf89SChuck Lever #include <linux/net.h> 47ff839970SChuck Lever #include <linux/ktime.h> 481da177e4SLinus Torvalds 49a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 5011c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 51c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 52fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 53a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 541da177e4SLinus Torvalds 553705ad64SJeff Layton #include <trace/events/sunrpc.h> 563705ad64SJeff Layton 5755ae1aabSRicardo Labiaga #include "sunrpc.h" 5855ae1aabSRicardo Labiaga 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * Local variables 611da177e4SLinus Torvalds */ 621da177e4SLinus Torvalds 63f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 641da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 651da177e4SLinus Torvalds #endif 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * Local functions 691da177e4SLinus Torvalds */ 7021de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 7137ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 724e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 731da177e4SLinus Torvalds 745ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7581c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7681c098afS\"Talpey, Thomas\ 779e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req) 789e910bffSTrond Myklebust { 799e910bffSTrond Myklebust unsigned long timeout = jiffies + req->rq_timeout; 809e910bffSTrond Myklebust 819e910bffSTrond Myklebust if (time_before(timeout, req->rq_majortimeo)) 829e910bffSTrond Myklebust return timeout; 839e910bffSTrond Myklebust return req->rq_majortimeo; 849e910bffSTrond Myklebust } 859e910bffSTrond Myklebust 8612a80469SChuck Lever /** 8781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 8881c098afS\"Talpey, Thomas\ * @transport: transport to register 8981c098afS\"Talpey, Thomas\ * 9081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9281c098afS\"Talpey, Thomas\ * 9381c098afS\"Talpey, Thomas\ * Returns: 9481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 9581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 9681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 9781c098afS\"Talpey, Thomas\ */ 9881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 9981c098afS\"Talpey, Thomas\ { 10081c098afS\"Talpey, Thomas\ struct xprt_class *t; 10181c098afS\"Talpey, Thomas\ int result; 10281c098afS\"Talpey, Thomas\ 10381c098afS\"Talpey, Thomas\ result = -EEXIST; 10481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 10581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 10681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1074fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 10881c098afS\"Talpey, Thomas\ goto out; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ 11181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11381c098afS\"Talpey, Thomas\ transport->name); 11481c098afS\"Talpey, Thomas\ result = 0; 11581c098afS\"Talpey, Thomas\ 11681c098afS\"Talpey, Thomas\ out: 11781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 11881c098afS\"Talpey, Thomas\ return result; 11981c098afS\"Talpey, Thomas\ } 12081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12181c098afS\"Talpey, Thomas\ 12281c098afS\"Talpey, Thomas\ /** 12381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 12465b6e42cSRandy Dunlap * @transport: transport to unregister 12581c098afS\"Talpey, Thomas\ * 12681c098afS\"Talpey, Thomas\ * Returns: 12781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 12881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 12981c098afS\"Talpey, Thomas\ */ 13081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13181c098afS\"Talpey, Thomas\ { 13281c098afS\"Talpey, Thomas\ struct xprt_class *t; 13381c098afS\"Talpey, Thomas\ int result; 13481c098afS\"Talpey, Thomas\ 13581c098afS\"Talpey, Thomas\ result = 0; 13681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 13781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 13881c098afS\"Talpey, Thomas\ if (t == transport) { 13981c098afS\"Talpey, Thomas\ printk(KERN_INFO 14081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14181c098afS\"Talpey, Thomas\ transport->name); 14281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14381c098afS\"Talpey, Thomas\ goto out; 14481c098afS\"Talpey, Thomas\ } 14581c098afS\"Talpey, Thomas\ } 14681c098afS\"Talpey, Thomas\ result = -ENOENT; 14781c098afS\"Talpey, Thomas\ 14881c098afS\"Talpey, Thomas\ out: 14981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15081c098afS\"Talpey, Thomas\ return result; 15181c098afS\"Talpey, Thomas\ } 15281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15381c098afS\"Talpey, Thomas\ 15481c098afS\"Talpey, Thomas\ /** 155441e3e24STom Talpey * xprt_load_transport - load a transport implementation 156441e3e24STom Talpey * @transport_name: transport to load 157441e3e24STom Talpey * 158441e3e24STom Talpey * Returns: 159441e3e24STom Talpey * 0: transport successfully loaded 160441e3e24STom Talpey * -ENOENT: transport module not available 161441e3e24STom Talpey */ 162441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 163441e3e24STom Talpey { 164441e3e24STom Talpey struct xprt_class *t; 165441e3e24STom Talpey int result; 166441e3e24STom Talpey 167441e3e24STom Talpey result = 0; 168441e3e24STom Talpey spin_lock(&xprt_list_lock); 169441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 170441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 171441e3e24STom Talpey spin_unlock(&xprt_list_lock); 172441e3e24STom Talpey goto out; 173441e3e24STom Talpey } 174441e3e24STom Talpey } 175441e3e24STom Talpey spin_unlock(&xprt_list_lock); 176ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 177441e3e24STom Talpey out: 178441e3e24STom Talpey return result; 179441e3e24STom Talpey } 180441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 181441e3e24STom Talpey 182c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 183c544577dSTrond Myklebust { 184c544577dSTrond Myklebust xprt->snd_task = NULL; 185c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 186c544577dSTrond Myklebust smp_mb__before_atomic(); 187c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 188c544577dSTrond Myklebust smp_mb__after_atomic(); 189c544577dSTrond Myklebust } else 190c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 191c544577dSTrond Myklebust } 192c544577dSTrond Myklebust 193441e3e24STom Talpey /** 19412a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 19512a80469SChuck Lever * @task: task that is requesting access to the transport 196177c27bfSRandy Dunlap * @xprt: pointer to the target transport 19712a80469SChuck Lever * 19812a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 19912a80469SChuck Lever * transport connects from colliding with writes. No congestion control 20012a80469SChuck Lever * is provided. 2011da177e4SLinus Torvalds */ 20243cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 2031da177e4SLinus Torvalds { 20412a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 20512a80469SChuck Lever 20612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 20712a80469SChuck Lever if (task == xprt->snd_task) 20812a80469SChuck Lever return 1; 20912a80469SChuck Lever goto out_sleep; 21012a80469SChuck Lever } 211c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 212c544577dSTrond Myklebust goto out_unlock; 21312a80469SChuck Lever xprt->snd_task = task; 2144d4a76f3Sj223yang@asset.uwaterloo.ca 21512a80469SChuck Lever return 1; 21612a80469SChuck Lever 217c544577dSTrond Myklebust out_unlock: 218c544577dSTrond Myklebust xprt_clear_locked(xprt); 21912a80469SChuck Lever out_sleep: 22046121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 22112a80469SChuck Lever task->tk_pid, xprt); 22212a80469SChuck Lever task->tk_status = -EAGAIN; 2236b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2246b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2259e910bffSTrond Myklebust xprt_request_timeout(req)); 2266b2e6856STrond Myklebust else 22779c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 22812a80469SChuck Lever return 0; 22912a80469SChuck Lever } 23012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 23112a80469SChuck Lever 23275891f50STrond Myklebust static bool 23375891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 23475891f50STrond Myklebust { 23575891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 23675891f50STrond Myklebust } 23775891f50STrond Myklebust 23875891f50STrond Myklebust static void 23975891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 24075891f50STrond Myklebust { 24175891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 24275891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 24375891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 24475891f50STrond Myklebust rq_xmit)->rq_cong) 24575891f50STrond Myklebust return; 24675891f50STrond Myklebust } 24775891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 24875891f50STrond Myklebust } 24975891f50STrond Myklebust 25075891f50STrond Myklebust static void 25175891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 25275891f50STrond Myklebust { 25375891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 25475891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 25575891f50STrond Myklebust } 25675891f50STrond Myklebust 25712a80469SChuck Lever /* 25812a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 25912a80469SChuck Lever * @task: task that is requesting access to the transport 26012a80469SChuck Lever * 26112a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 26212a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 26312a80469SChuck Lever * woken up and given access to the transport. 26475891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 26512a80469SChuck Lever */ 26643cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 26712a80469SChuck Lever { 2681da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 2691da177e4SLinus Torvalds 2702226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2711da177e4SLinus Torvalds if (task == xprt->snd_task) 2721da177e4SLinus Torvalds return 1; 2731da177e4SLinus Torvalds goto out_sleep; 2741da177e4SLinus Torvalds } 27543cedbf0STrond Myklebust if (req == NULL) { 27643cedbf0STrond Myklebust xprt->snd_task = task; 27743cedbf0STrond Myklebust return 1; 27843cedbf0STrond Myklebust } 279c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 280c544577dSTrond Myklebust goto out_unlock; 28175891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 2821da177e4SLinus Torvalds xprt->snd_task = task; 2831da177e4SLinus Torvalds return 1; 2841da177e4SLinus Torvalds } 285c544577dSTrond Myklebust out_unlock: 286632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2871da177e4SLinus Torvalds out_sleep: 28846121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2891da177e4SLinus Torvalds task->tk_status = -EAGAIN; 2906b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2916b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2929e910bffSTrond Myklebust xprt_request_timeout(req)); 2936b2e6856STrond Myklebust else 29479c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds } 29712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2981da177e4SLinus Torvalds 29912a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds int retval; 3021da177e4SLinus Torvalds 303bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 304bd79bc57STrond Myklebust return 1; 305b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 30643cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 307b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3081da177e4SLinus Torvalds return retval; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 311961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 3121da177e4SLinus Torvalds { 313961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 31449e9a890SChuck Lever 31549e9a890SChuck Lever xprt->snd_task = task; 316961a828dSTrond Myklebust return true; 317961a828dSTrond Myklebust } 318961a828dSTrond Myklebust 319961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 320961a828dSTrond Myklebust { 321961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 32249e9a890SChuck Lever return; 323c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 324c544577dSTrond Myklebust goto out_unlock; 325f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 326f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 327961a828dSTrond Myklebust return; 328c544577dSTrond Myklebust out_unlock: 329632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 33049e9a890SChuck Lever } 33149e9a890SChuck Lever 332961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 333961a828dSTrond Myklebust { 334961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 335961a828dSTrond Myklebust return; 336c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 337c544577dSTrond Myklebust goto out_unlock; 33875891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 339961a828dSTrond Myklebust goto out_unlock; 340f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 34175891f50STrond Myklebust __xprt_lock_write_func, xprt)) 342961a828dSTrond Myklebust return; 3431da177e4SLinus Torvalds out_unlock: 344632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 34749e9a890SChuck Lever /** 34849e9a890SChuck Lever * xprt_release_xprt - 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. No congestion control is provided. 3531da177e4SLinus Torvalds */ 35449e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds if (xprt->snd_task == task) { 357632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3581da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds } 36112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3621da177e4SLinus Torvalds 36349e9a890SChuck Lever /** 36449e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 36549e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 36649e9a890SChuck Lever * @task: task that is releasing access to the transport 36749e9a890SChuck Lever * 36849e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 36949e9a890SChuck Lever * transport if the transport's congestion window allows it. 37049e9a890SChuck Lever */ 37149e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 37249e9a890SChuck Lever { 37349e9a890SChuck Lever if (xprt->snd_task == task) { 374632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 37549e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 37649e9a890SChuck Lever } 37749e9a890SChuck Lever } 37812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 37949e9a890SChuck Lever 38049e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3811da177e4SLinus Torvalds { 382bd79bc57STrond Myklebust if (xprt->snd_task != task) 383bd79bc57STrond Myklebust return; 384b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 38549e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 386b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* 3901da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3911da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3921da177e4SLinus Torvalds */ 3931da177e4SLinus Torvalds static int 39475891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds if (req->rq_cong) 3971da177e4SLinus Torvalds return 1; 39846121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 39975891f50STrond Myklebust req->rq_task->tk_pid, xprt->cong, xprt->cwnd); 40075891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 40175891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 4021da177e4SLinus Torvalds return 0; 40375891f50STrond Myklebust } 4041da177e4SLinus Torvalds req->rq_cong = 1; 4051da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4061da177e4SLinus Torvalds return 1; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4111da177e4SLinus Torvalds * that has been sleeping due to congestion 4121da177e4SLinus Torvalds */ 4131da177e4SLinus Torvalds static void 4141da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds if (!req->rq_cong) 4171da177e4SLinus Torvalds return; 4181da177e4SLinus Torvalds req->rq_cong = 0; 4191da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 42075891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 42149e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 42446c0ee8bSChuck Lever /** 42575891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 42675891f50STrond Myklebust * @xprt: pointer to transport 42775891f50STrond Myklebust * @req: pointer to RPC request 42875891f50STrond Myklebust * 42975891f50STrond Myklebust * Useful for transports that require congestion control. 43075891f50STrond Myklebust */ 43175891f50STrond Myklebust bool 43275891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 43375891f50STrond Myklebust { 43475891f50STrond Myklebust bool ret = false; 43575891f50STrond Myklebust 43675891f50STrond Myklebust if (req->rq_cong) 43775891f50STrond Myklebust return true; 438b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 43975891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 440b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 44175891f50STrond Myklebust return ret; 44275891f50STrond Myklebust } 44375891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 44475891f50STrond Myklebust 44575891f50STrond Myklebust /** 446a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 447a58dd398SChuck Lever * @task: RPC request that recently completed 448a58dd398SChuck Lever * 449a58dd398SChuck Lever * Useful for transports that require congestion control. 450a58dd398SChuck Lever */ 451a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 452a58dd398SChuck Lever { 453a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 454a4f0835cSTrond Myklebust 455a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 456a58dd398SChuck Lever } 45712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 458a58dd398SChuck Lever 45975891f50STrond Myklebust /* 46075891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 46175891f50STrond Myklebust * entry on xprt->sending 46275891f50STrond Myklebust */ 46375891f50STrond Myklebust static void 46475891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 46575891f50STrond Myklebust { 46675891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 467b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 46875891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 469b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 47075891f50STrond Myklebust } 47175891f50STrond Myklebust } 47275891f50STrond Myklebust 473a58dd398SChuck Lever /** 47446c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4756a24dfb6STrond Myklebust * @xprt: pointer to xprt 47646c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 47746c0ee8bSChuck Lever * @result: result code of completed RPC request 47846c0ee8bSChuck Lever * 4794f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4804f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4814f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4824f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4834f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4844f4cf5adSChuck Lever * 4854f4cf5adSChuck Lever * - a reply is received and 4864f4cf5adSChuck Lever * - a full number of requests are outstanding and 4874f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4881da177e4SLinus Torvalds */ 4896a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4901da177e4SLinus Torvalds { 49146c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 49246c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4951da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4961da177e4SLinus Torvalds * the result gets rounded properly. */ 4971da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4981da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4991da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 50049e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 5011da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 5021da177e4SLinus Torvalds cwnd >>= 1; 5031da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 5041da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 5071da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 5081da177e4SLinus Torvalds xprt->cwnd = cwnd; 50946c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 5101da177e4SLinus Torvalds } 51112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 5121da177e4SLinus Torvalds 51344fbac22SChuck Lever /** 51444fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 51544fbac22SChuck Lever * @xprt: transport with waiting tasks 51644fbac22SChuck Lever * @status: result code to plant in each task before waking it 51744fbac22SChuck Lever * 51844fbac22SChuck Lever */ 51944fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 52044fbac22SChuck Lever { 52144fbac22SChuck Lever if (status < 0) 52244fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 52344fbac22SChuck Lever else 52444fbac22SChuck Lever rpc_wake_up(&xprt->pending); 52544fbac22SChuck Lever } 52612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 52744fbac22SChuck Lever 528c7b2cae8SChuck Lever /** 529c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 530c544577dSTrond Myklebust * @xprt: transport 531a9a6b52eSTrond Myklebust * 532a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 533a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 534a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 535c7b2cae8SChuck Lever */ 536c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 537c7b2cae8SChuck Lever { 538c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 539c7b2cae8SChuck Lever } 54012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 541c7b2cae8SChuck Lever 542c544577dSTrond Myklebust static bool 543c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 544c544577dSTrond Myklebust { 545c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 546c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 547c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 548c544577dSTrond Myklebust "xprt %p\n", xprt); 549c544577dSTrond Myklebust return true; 550c544577dSTrond Myklebust } 551c544577dSTrond Myklebust return false; 552c544577dSTrond Myklebust } 553c544577dSTrond Myklebust 554c7b2cae8SChuck Lever /** 555c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 556c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 557c7b2cae8SChuck Lever * 558c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 559c7b2cae8SChuck Lever */ 560c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 561c7b2cae8SChuck Lever { 562c544577dSTrond Myklebust bool ret; 563c544577dSTrond Myklebust 564c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 565c544577dSTrond Myklebust return false; 566b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 567c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 568b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 569c544577dSTrond Myklebust return ret; 570c7b2cae8SChuck Lever } 57112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 572c7b2cae8SChuck Lever 573da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime) 574da953063STrond Myklebust { 575da953063STrond Myklebust s64 delta = ktime_to_ns(ktime_get() - abstime); 576da953063STrond Myklebust return likely(delta >= 0) ? 577da953063STrond Myklebust jiffies - nsecs_to_jiffies(delta) : 578da953063STrond Myklebust jiffies + nsecs_to_jiffies(-delta); 579da953063STrond Myklebust } 580da953063STrond Myklebust 581da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req) 5821da177e4SLinus Torvalds { 583ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 584da953063STrond Myklebust unsigned long majortimeo = req->rq_timeout; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (to->to_exponential) 587da953063STrond Myklebust majortimeo <<= to->to_retries; 5881da177e4SLinus Torvalds else 589da953063STrond Myklebust majortimeo += to->to_increment * to->to_retries; 590da953063STrond Myklebust if (majortimeo > to->to_maxval || majortimeo == 0) 591da953063STrond Myklebust majortimeo = to->to_maxval; 592da953063STrond Myklebust return majortimeo; 593da953063STrond Myklebust } 594da953063STrond Myklebust 595da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req) 596da953063STrond Myklebust { 597da953063STrond Myklebust req->rq_majortimeo += xprt_calc_majortimeo(req); 598da953063STrond Myklebust } 599da953063STrond Myklebust 600da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req) 601da953063STrond Myklebust { 602da953063STrond Myklebust unsigned long time_init; 603da953063STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 604da953063STrond Myklebust 605da953063STrond Myklebust if (likely(xprt && xprt_connected(xprt))) 606da953063STrond Myklebust time_init = jiffies; 607da953063STrond Myklebust else 608da953063STrond Myklebust time_init = xprt_abs_ktime_to_jiffies(task->tk_start); 609da953063STrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 610da953063STrond Myklebust req->rq_majortimeo = time_init + xprt_calc_majortimeo(req); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6139903cd1cSChuck Lever /** 6149903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 6159903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 6169903cd1cSChuck Lever * 6171da177e4SLinus Torvalds */ 6181da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 621ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 6221da177e4SLinus Torvalds int status = 0; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 6251da177e4SLinus Torvalds if (to->to_exponential) 6261da177e4SLinus Torvalds req->rq_timeout <<= 1; 6271da177e4SLinus Torvalds else 6281da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 6291da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 6301da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 6311da177e4SLinus Torvalds req->rq_retries++; 6321da177e4SLinus Torvalds } else { 6331da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 6341da177e4SLinus Torvalds req->rq_retries = 0; 6351da177e4SLinus Torvalds xprt_reset_majortimeo(req); 6361da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 637b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 6381da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 639b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 6401da177e4SLinus Torvalds status = -ETIMEDOUT; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6441da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6451da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds return status; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 65065f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6511da177e4SLinus Torvalds { 65265f27f38SDavid Howells struct rpc_xprt *xprt = 65365f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 654a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 6551da177e4SLinus Torvalds 65666af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6574876cc77STrond Myklebust xprt->ops->close(xprt); 6581da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 65979234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 660a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6639903cd1cSChuck Lever /** 66462da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6659903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6669903cd1cSChuck Lever * 6671da177e4SLinus Torvalds */ 66862da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 671b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 6721da177e4SLinus Torvalds xprt_clear_connected(xprt); 673c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 67427adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 675b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 6761da177e4SLinus Torvalds } 67762da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6781da177e4SLinus Torvalds 67966af1e55STrond Myklebust /** 68066af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 68166af1e55STrond Myklebust * @xprt: transport to disconnect 68266af1e55STrond Myklebust * 68366af1e55STrond Myklebust */ 68466af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 68566af1e55STrond Myklebust { 68666af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 687b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 68866af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 68966af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 69066af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 69140a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6920445f92cSTrond Myklebust else if (xprt->snd_task) 6930445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 6940445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 695b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 69666af1e55STrond Myklebust } 697e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 69866af1e55STrond Myklebust 6997f3a1d1eSTrond Myklebust static unsigned int 7007f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 7017f3a1d1eSTrond Myklebust { 7027f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 7037f3a1d1eSTrond Myklebust } 7047f3a1d1eSTrond Myklebust 7057f3a1d1eSTrond Myklebust static bool 7067f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 7077f3a1d1eSTrond Myklebust { 7087f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 7097f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 7107f3a1d1eSTrond Myklebust 7117f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7127f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7137f3a1d1eSTrond Myklebust } 7147f3a1d1eSTrond Myklebust 7157c1d71cfSTrond Myklebust /** 7167c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 7177c1d71cfSTrond Myklebust * @xprt: transport to disconnect 7187c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 7197c1d71cfSTrond Myklebust * 7207c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 7217c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 7227c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 7237c1d71cfSTrond Myklebust * a batch of RPC requests. 7247c1d71cfSTrond Myklebust * 7257c1d71cfSTrond Myklebust */ 7267c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 7277c1d71cfSTrond Myklebust { 7287c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 729b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7307c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 7317c1d71cfSTrond Myklebust goto out; 7322c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 7337c1d71cfSTrond Myklebust goto out; 7347c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 7357c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 7367c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 73740a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7382a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 7397c1d71cfSTrond Myklebust out: 740b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7417c1d71cfSTrond Myklebust } 7427c1d71cfSTrond Myklebust 743ad3331acSTrond Myklebust static bool 744ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 745ad3331acSTrond Myklebust { 746ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 747ad3331acSTrond Myklebust } 748ad3331acSTrond Myklebust 749ad3331acSTrond Myklebust static void 750ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 751ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 752ad3331acSTrond Myklebust { 75380d3c45fSDave Wysochanski xprt->last_used = jiffies; 75495f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 755ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 756ad3331acSTrond Myklebust } 757ad3331acSTrond Myklebust 7581da177e4SLinus Torvalds static void 759ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 7601da177e4SLinus Torvalds { 761ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 7621da177e4SLinus Torvalds 76395f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 764b5e92419STrond Myklebust return; 765ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 766ad3331acSTrond Myklebust xprt->last_used = jiffies; 7672226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7681da177e4SLinus Torvalds return; 769b5e92419STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 772718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 773718ba5b8STrond Myklebust struct rpc_task *task, 774718ba5b8STrond Myklebust void *cookie) 775718ba5b8STrond Myklebust { 776718ba5b8STrond Myklebust bool ret = false; 777718ba5b8STrond Myklebust 778b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 779718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 780718ba5b8STrond Myklebust goto out; 781718ba5b8STrond Myklebust if (xprt->snd_task != task) 782718ba5b8STrond Myklebust goto out; 783718ba5b8STrond Myklebust xprt->snd_task = cookie; 784718ba5b8STrond Myklebust ret = true; 785718ba5b8STrond Myklebust out: 786b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 787718ba5b8STrond Myklebust return ret; 788718ba5b8STrond Myklebust } 789718ba5b8STrond Myklebust 790718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 791718ba5b8STrond Myklebust { 792b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 793718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 794718ba5b8STrond Myklebust goto out; 795718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 796718ba5b8STrond Myklebust goto out; 797718ba5b8STrond Myklebust xprt->snd_task =NULL; 798718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 799ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 800718ba5b8STrond Myklebust out: 801b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 80279234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 803718ba5b8STrond Myklebust } 804718ba5b8STrond Myklebust 8059903cd1cSChuck Lever /** 8069903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8079903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8081da177e4SLinus Torvalds * 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8111da177e4SLinus Torvalds { 812ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8131da177e4SLinus Torvalds 81446121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 8151da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 8161da177e4SLinus Torvalds 817ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 81801d37c42STrond Myklebust task->tk_status = -EAGAIN; 8191da177e4SLinus Torvalds return; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 8221da177e4SLinus Torvalds return; 823feb8ca37STrond Myklebust 824feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 825feb8ca37STrond Myklebust xprt->ops->close(xprt); 826feb8ca37STrond Myklebust 827718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 8282c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 8296b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->pending, task, NULL, 8309e910bffSTrond Myklebust xprt_request_timeout(task->tk_rqstp)); 8310b9e7943STrond Myklebust 8320b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 8330b9e7943STrond Myklebust return; 8340b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 8350b9e7943STrond Myklebust return; 8360a9a4304STrond Myklebust /* Race breaker */ 8370a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 838262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 8391b092092STrond Myklebust xprt->ops->connect(xprt, task); 8400a9a4304STrond Myklebust } else { 8410a9a4304STrond Myklebust xprt_clear_connecting(xprt); 8420a9a4304STrond Myklebust task->tk_status = 0; 8430a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8440a9a4304STrond Myklebust } 8451da177e4SLinus Torvalds } 846718ba5b8STrond Myklebust xprt_release_write(xprt, task); 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 84995f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 85095f7691dSTrond Myklebust XID_RB_EQUAL, 85195f7691dSTrond Myklebust XID_RB_LEFT, 85295f7691dSTrond Myklebust XID_RB_RIGHT, 85395f7691dSTrond Myklebust }; 85495f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 85595f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 85695f7691dSTrond Myklebust { 85795f7691dSTrond Myklebust if (xid1 == xid2) 85895f7691dSTrond Myklebust return XID_RB_EQUAL; 85995f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 86095f7691dSTrond Myklebust return XID_RB_LEFT; 86195f7691dSTrond Myklebust return XID_RB_RIGHT; 86295f7691dSTrond Myklebust } 86395f7691dSTrond Myklebust 86495f7691dSTrond Myklebust static struct rpc_rqst * 86595f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 86695f7691dSTrond Myklebust { 86795f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 86895f7691dSTrond Myklebust struct rpc_rqst *req; 86995f7691dSTrond Myklebust 87095f7691dSTrond Myklebust while (n != NULL) { 87195f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 87295f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 87395f7691dSTrond Myklebust case XID_RB_LEFT: 87495f7691dSTrond Myklebust n = n->rb_left; 87595f7691dSTrond Myklebust break; 87695f7691dSTrond Myklebust case XID_RB_RIGHT: 87795f7691dSTrond Myklebust n = n->rb_right; 87895f7691dSTrond Myklebust break; 87995f7691dSTrond Myklebust case XID_RB_EQUAL: 88095f7691dSTrond Myklebust return req; 88195f7691dSTrond Myklebust } 88295f7691dSTrond Myklebust } 88395f7691dSTrond Myklebust return NULL; 88495f7691dSTrond Myklebust } 88595f7691dSTrond Myklebust 88695f7691dSTrond Myklebust static void 88795f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 88895f7691dSTrond Myklebust { 88995f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 89095f7691dSTrond Myklebust struct rb_node *n = NULL; 89195f7691dSTrond Myklebust struct rpc_rqst *req; 89295f7691dSTrond Myklebust 89395f7691dSTrond Myklebust while (*p != NULL) { 89495f7691dSTrond Myklebust n = *p; 89595f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 89695f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 89795f7691dSTrond Myklebust case XID_RB_LEFT: 89895f7691dSTrond Myklebust p = &n->rb_left; 89995f7691dSTrond Myklebust break; 90095f7691dSTrond Myklebust case XID_RB_RIGHT: 90195f7691dSTrond Myklebust p = &n->rb_right; 90295f7691dSTrond Myklebust break; 90395f7691dSTrond Myklebust case XID_RB_EQUAL: 90495f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 90595f7691dSTrond Myklebust return; 90695f7691dSTrond Myklebust } 90795f7691dSTrond Myklebust } 90895f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 90995f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 91095f7691dSTrond Myklebust } 91195f7691dSTrond Myklebust 91295f7691dSTrond Myklebust static void 91395f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 91495f7691dSTrond Myklebust { 91595f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 91695f7691dSTrond Myklebust } 91795f7691dSTrond Myklebust 9189903cd1cSChuck Lever /** 9199903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 9209903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 9219903cd1cSChuck Lever * @xid: RPC XID of incoming reply 9229903cd1cSChuck Lever * 92375c84151STrond Myklebust * Caller holds xprt->queue_lock. 9241da177e4SLinus Torvalds */ 925d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 9261da177e4SLinus Torvalds { 9278f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 9281da177e4SLinus Torvalds 92995f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 93095f7691dSTrond Myklebust if (entry != NULL) { 9313705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 9320b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 933262ca07dSChuck Lever return entry; 9343705ad64SJeff Layton } 93546121cf7SChuck Lever 93646121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 93746121cf7SChuck Lever ntohl(xid)); 9383705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 939262ca07dSChuck Lever xprt->stat.bad_xids++; 940262ca07dSChuck Lever return NULL; 9411da177e4SLinus Torvalds } 94212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 9431da177e4SLinus Torvalds 944cf9946cdSTrond Myklebust static bool 945cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 946cf9946cdSTrond Myklebust { 947cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 948cf9946cdSTrond Myklebust } 949cf9946cdSTrond Myklebust 950729749bbSTrond Myklebust /** 951729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 952729749bbSTrond Myklebust * @req: Request to pin 953729749bbSTrond Myklebust * 954729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 9551f7d1c73SChuck Lever * so should be holding xprt->queue_lock. 956729749bbSTrond Myklebust */ 957729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 958729749bbSTrond Myklebust { 959cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 960729749bbSTrond Myklebust } 9619590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 962729749bbSTrond Myklebust 963729749bbSTrond Myklebust /** 964729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 965729749bbSTrond Myklebust * @req: Request to pin 966729749bbSTrond Myklebust * 9671f7d1c73SChuck Lever * Caller should be holding xprt->queue_lock. 968729749bbSTrond Myklebust */ 969729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 970729749bbSTrond Myklebust { 971cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 972cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 973cf9946cdSTrond Myklebust return; 974cf9946cdSTrond Myklebust } 975cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 976cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 977729749bbSTrond Myklebust } 9789590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 979729749bbSTrond Myklebust 980729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 981729749bbSTrond Myklebust { 982cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 983729749bbSTrond Myklebust } 984729749bbSTrond Myklebust 985edc81dcdSTrond Myklebust static bool 986edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 987edc81dcdSTrond Myklebust { 988edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 989edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 990edc81dcdSTrond Myklebust } 991edc81dcdSTrond Myklebust 992edc81dcdSTrond Myklebust static bool 993edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 994edc81dcdSTrond Myklebust { 995edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 996edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 997edc81dcdSTrond Myklebust } 998edc81dcdSTrond Myklebust 999edc81dcdSTrond Myklebust /** 1000edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 1001edc81dcdSTrond Myklebust * @task: RPC task 1002edc81dcdSTrond Myklebust * 1003edc81dcdSTrond Myklebust */ 1004edc81dcdSTrond Myklebust void 1005edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1006edc81dcdSTrond Myklebust { 1007edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1008edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1009edc81dcdSTrond Myklebust 1010edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1011edc81dcdSTrond Myklebust return; 1012edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1013edc81dcdSTrond Myklebust 1014edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1015edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1016edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1017edc81dcdSTrond Myklebust 1018edc81dcdSTrond Myklebust /* Add request to the receive list */ 101995f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1020edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1021edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1022edc81dcdSTrond Myklebust 1023edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1024edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1025edc81dcdSTrond Myklebust } 1026edc81dcdSTrond Myklebust 1027edc81dcdSTrond Myklebust /** 1028edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1029edc81dcdSTrond Myklebust * @task: RPC task 1030edc81dcdSTrond Myklebust * 1031edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1032edc81dcdSTrond Myklebust */ 1033edc81dcdSTrond Myklebust static void 1034edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1035edc81dcdSTrond Myklebust { 103695f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 103795f7691dSTrond Myklebust 1038edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 103995f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1040edc81dcdSTrond Myklebust } 1041edc81dcdSTrond Myklebust 1042ecd465eeSChuck Lever /** 1043ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1044ecd465eeSChuck Lever * @task: RPC request that recently completed 1045ecd465eeSChuck Lever * 104675c84151STrond Myklebust * Caller holds xprt->queue_lock. 1047ecd465eeSChuck Lever */ 1048ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 10491da177e4SLinus Torvalds { 10501570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 10511570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 105295c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1053d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 10541570c1e4SChuck Lever 10551da177e4SLinus Torvalds if (timer) { 10561da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1057ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 10581570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds } 1061ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 10621da177e4SLinus Torvalds 10631570c1e4SChuck Lever /** 10641570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 10651570c1e4SChuck Lever * @task: RPC request that recently completed 10661570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 10671570c1e4SChuck Lever * 106875c84151STrond Myklebust * Caller holds xprt->queue_lock. 10691570c1e4SChuck Lever */ 10701570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 10711570c1e4SChuck Lever { 10721570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1073fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 10741da177e4SLinus Torvalds 10751570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 10761570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 10773705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 10781da177e4SLinus Torvalds 1079fda13939STrond Myklebust xprt->stat.recvs++; 1080ef759a2eSChuck Lever 10811e799b67STrond Myklebust req->rq_private_buf.len = copied; 1082dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1083dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 108443ac3f29STrond Myklebust smp_wmb(); 1085dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1086edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1087fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 10881da177e4SLinus Torvalds } 108912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 10901da177e4SLinus Torvalds 109146c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 10921da177e4SLinus Torvalds { 10931da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 10941da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 10951da177e4SLinus Torvalds 10965d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 10975d00837bSTrond Myklebust return; 109846c0ee8bSChuck Lever 109982476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1100dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 110146c0ee8bSChuck Lever if (xprt->ops->timer) 11026a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 11035d00837bSTrond Myklebust } else 11045d00837bSTrond Myklebust task->tk_status = 0; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11079903cd1cSChuck Lever /** 11088ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_def - wait for reply 11098ba6a92dSTrond Myklebust * @task: pointer to rpc_task 11108ba6a92dSTrond Myklebust * 11118ba6a92dSTrond Myklebust * Set a request's retransmit timeout based on the transport's 11128ba6a92dSTrond Myklebust * default timeout parameters. Used by transports that don't adjust 11138ba6a92dSTrond Myklebust * the retransmit timeout based on round-trip time estimation, 11148ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 11158ba6a92dSTrond Myklebust */ 11168ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task) 11178ba6a92dSTrond Myklebust { 11188ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11198ba6a92dSTrond Myklebust 11206b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 11219e910bffSTrond Myklebust xprt_request_timeout(req)); 11228ba6a92dSTrond Myklebust } 11238ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def); 11248ba6a92dSTrond Myklebust 11258ba6a92dSTrond Myklebust /** 11268ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator 11278ba6a92dSTrond Myklebust * @task: pointer to rpc_task 11288ba6a92dSTrond Myklebust * 11298ba6a92dSTrond Myklebust * Set a request's retransmit timeout using the RTT estimator, 11308ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 11318ba6a92dSTrond Myklebust */ 11328ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task) 11338ba6a92dSTrond Myklebust { 11348ba6a92dSTrond Myklebust int timer = task->tk_msg.rpc_proc->p_timer; 11358ba6a92dSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 11368ba6a92dSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 11378ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11388ba6a92dSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 11396b2e6856STrond Myklebust unsigned long timeout; 11408ba6a92dSTrond Myklebust 11416b2e6856STrond Myklebust timeout = rpc_calc_rto(rtt, timer); 11426b2e6856STrond Myklebust timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 11436b2e6856STrond Myklebust if (timeout > max_timeout || timeout == 0) 11446b2e6856STrond Myklebust timeout = max_timeout; 11456b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 11466b2e6856STrond Myklebust jiffies + timeout); 11478ba6a92dSTrond Myklebust } 11488ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt); 11498ba6a92dSTrond Myklebust 11508ba6a92dSTrond Myklebust /** 11517f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 11527f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 11537f3a1d1eSTrond Myklebust * 11547f3a1d1eSTrond Myklebust */ 11557f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 11567f3a1d1eSTrond Myklebust { 11577f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 11587f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11597f3a1d1eSTrond Myklebust 11607f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 11617f3a1d1eSTrond Myklebust return; 11627f3a1d1eSTrond Myklebust /* 11637f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 11647f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 11657f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 11667f3a1d1eSTrond Myklebust */ 11677f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 11687f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 11698ba6a92dSTrond Myklebust xprt->ops->wait_for_reply_request(task); 11707f3a1d1eSTrond Myklebust /* 11717f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 11727f3a1d1eSTrond Myklebust * connection was dropped in case the call to 11737f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 11747f3a1d1eSTrond Myklebust */ 11757f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 11767f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 11777f3a1d1eSTrond Myklebust task, -ENOTCONN); 11787f3a1d1eSTrond Myklebust } 11797f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 11807f3a1d1eSTrond Myklebust } 11817f3a1d1eSTrond Myklebust 1182944b0429STrond Myklebust static bool 1183944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1184944b0429STrond Myklebust { 1185762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1186944b0429STrond Myklebust } 1187944b0429STrond Myklebust 1188944b0429STrond Myklebust /** 1189944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1190944b0429STrond Myklebust * @task: pointer to rpc_task 1191944b0429STrond Myklebust * 1192944b0429STrond Myklebust * Add a task to the transmission queue. 1193944b0429STrond Myklebust */ 1194944b0429STrond Myklebust void 1195944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1196944b0429STrond Myklebust { 1197918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1198944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1199944b0429STrond Myklebust 1200944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1201e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1202944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 120375891f50STrond Myklebust /* 120475891f50STrond Myklebust * Requests that carry congestion control credits are added 120575891f50STrond Myklebust * to the head of the list to avoid starvation issues. 120675891f50STrond Myklebust */ 120775891f50STrond Myklebust if (req->rq_cong) { 120875891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 120975891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 121075891f50STrond Myklebust if (pos->rq_cong) 121175891f50STrond Myklebust continue; 121275891f50STrond Myklebust /* Note: req is added _before_ pos */ 121375891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 121475891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12150c77668dSChuck Lever trace_xprt_enq_xmit(task, 1); 121675891f50STrond Myklebust goto out; 121775891f50STrond Myklebust } 121886aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 121986aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 122086aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 122186aeee0eSTrond Myklebust continue; 122286aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 122386aeee0eSTrond Myklebust continue; 122486aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 122586aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 122686aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12270c77668dSChuck Lever trace_xprt_enq_xmit(task, 2); 122886aeee0eSTrond Myklebust goto out; 122986aeee0eSTrond Myklebust } 1230deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1231918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1232918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1233918f3c1fSTrond Myklebust continue; 1234918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1235918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 12360c77668dSChuck Lever trace_xprt_enq_xmit(task, 3); 1237918f3c1fSTrond Myklebust goto out; 1238918f3c1fSTrond Myklebust } 123975891f50STrond Myklebust } 1240944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1241918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 12420c77668dSChuck Lever trace_xprt_enq_xmit(task, 4); 1243918f3c1fSTrond Myklebust out: 1244944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1245944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1246944b0429STrond Myklebust } 1247944b0429STrond Myklebust } 1248944b0429STrond Myklebust 1249944b0429STrond Myklebust /** 1250944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1251944b0429STrond Myklebust * @task: pointer to rpc_task 1252944b0429STrond Myklebust * 1253944b0429STrond Myklebust * Remove a task from the transmission queue 1254944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1255944b0429STrond Myklebust */ 1256944b0429STrond Myklebust static void 1257944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1258944b0429STrond Myklebust { 1259918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1260918f3c1fSTrond Myklebust 1261918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1262918f3c1fSTrond Myklebust return; 1263918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1264918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1265918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1266918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1267918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1268918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1269918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1270918f3c1fSTrond Myklebust } 1271918f3c1fSTrond Myklebust } else 1272918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1273944b0429STrond Myklebust } 1274944b0429STrond Myklebust 1275944b0429STrond Myklebust /** 1276944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1277944b0429STrond Myklebust * @task: pointer to rpc_task 1278944b0429STrond Myklebust * 1279944b0429STrond Myklebust * Remove a task from the transmission queue 1280944b0429STrond Myklebust */ 1281944b0429STrond Myklebust static void 1282944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1283944b0429STrond Myklebust { 1284944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1285944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1286944b0429STrond Myklebust 1287944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1288944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1289944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1290944b0429STrond Myklebust } 1291944b0429STrond Myklebust 12927f3a1d1eSTrond Myklebust /** 12939d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 12949d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 12959d96acbcSTrond Myklebust * 12969d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 12979d96acbcSTrond Myklebust * the request for transmission or receive. 12989d96acbcSTrond Myklebust */ 12999d96acbcSTrond Myklebust void 13009d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 13019d96acbcSTrond Myklebust { 13029d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 13039d96acbcSTrond Myklebust 13049d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 13059d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 13069d96acbcSTrond Myklebust } 13079d96acbcSTrond Myklebust 13089d96acbcSTrond Myklebust /** 1309762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1310762e4e67STrond Myklebust * @task: pointer to rpc_task 1311762e4e67STrond Myklebust * 1312762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1313762e4e67STrond Myklebust */ 1314762e4e67STrond Myklebust bool 1315762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1316762e4e67STrond Myklebust { 1317762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1318762e4e67STrond Myklebust } 1319762e4e67STrond Myklebust 1320762e4e67STrond Myklebust /** 13219903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 13229903cd1cSChuck Lever * @task: RPC task about to send a request 13239903cd1cSChuck Lever * 13241da177e4SLinus Torvalds */ 132590051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 13281da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 13291da177e4SLinus Torvalds 133046121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 13311da177e4SLinus Torvalds 13325f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 13335f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1334944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 13355f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 13365f2f6bd9STrond Myklebust task, 0); 13375f2f6bd9STrond Myklebust return false; 13385f2f6bd9STrond Myklebust 13398a19a0b6STrond Myklebust } 13405f2f6bd9STrond Myklebust return true; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 1343e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 13445e5ce5beSTrond Myklebust { 1345343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 13465e5ce5beSTrond Myklebust } 13475e5ce5beSTrond Myklebust 13489903cd1cSChuck Lever /** 134989f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 135089f90fe1STrond Myklebust * @req: pointer to request to transmit 135189f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 13529903cd1cSChuck Lever * 135389f90fe1STrond Myklebust * This performs the transmission of a single request. 135489f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 135589f90fe1STrond Myklebust * does need to be pinned. 135689f90fe1STrond Myklebust * Returns '0' on success. 13579903cd1cSChuck Lever */ 135889f90fe1STrond Myklebust static int 135989f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 13601da177e4SLinus Torvalds { 13611da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 136289f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 136390d91b0cSTrond Myklebust unsigned int connect_cookie; 1364dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1365ff699ea8SChuck Lever int status; 13661da177e4SLinus Torvalds 1367edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 136889f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 136989f90fe1STrond Myklebust status = 0; 1370944b0429STrond Myklebust goto out_dequeue; 137189f90fe1STrond Myklebust } 13723021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1373edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 137489f90fe1STrond Myklebust status = -EBADMSG; 1375944b0429STrond Myklebust goto out_dequeue; 13763021a5bbSTrond Myklebust } 1377a79f194aSTrond Myklebust if (task->tk_ops->rpc_call_prepare_transmit) { 1378a79f194aSTrond Myklebust task->tk_ops->rpc_call_prepare_transmit(task, 1379a79f194aSTrond Myklebust task->tk_calldata); 1380a79f194aSTrond Myklebust status = task->tk_status; 1381a79f194aSTrond Myklebust if (status < 0) 1382a79f194aSTrond Myklebust goto out_dequeue; 1383a79f194aSTrond Myklebust } 1384ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) { 1385ae67bd38STrond Myklebust status = -ERESTARTSYS; 1386ae67bd38STrond Myklebust goto out_dequeue; 1387ae67bd38STrond Myklebust } 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 1390dcbbeda8STrond Myklebust /* 1391dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1392dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1393dcbbeda8STrond Myklebust * reply to the first transmission. 1394dcbbeda8STrond Myklebust */ 1395dcbbeda8STrond Myklebust req->rq_ntrans++; 1396dcbbeda8STrond Myklebust 139790d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1398adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1399c8485e4dSTrond Myklebust if (status != 0) { 1400dcbbeda8STrond Myklebust req->rq_ntrans--; 14010c77668dSChuck Lever trace_xprt_transmit(req, status); 140289f90fe1STrond Myklebust return status; 1403c8485e4dSTrond Myklebust } 14047ebbbc6eSTrond Myklebust 1405dcbbeda8STrond Myklebust if (is_retrans) 1406dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1407dcbbeda8STrond Myklebust 14084a068258SChuck Lever xprt_inject_disconnect(xprt); 1409c8485e4dSTrond Myklebust 1410468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1411b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 1412262ca07dSChuck Lever 1413262ca07dSChuck Lever xprt->stat.sends++; 1414262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1415262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 141615a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 141715a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1418b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 141990d91b0cSTrond Myklebust 142090d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1421944b0429STrond Myklebust out_dequeue: 14220c77668dSChuck Lever trace_xprt_transmit(req, status); 1423944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 142489f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 142589f90fe1STrond Myklebust return status; 142689f90fe1STrond Myklebust } 142789f90fe1STrond Myklebust 142889f90fe1STrond Myklebust /** 142989f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 143089f90fe1STrond Myklebust * @task: controlling RPC task 143189f90fe1STrond Myklebust * 143289f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 143389f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 143489f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 143589f90fe1STrond Myklebust * received a reply. 143689f90fe1STrond Myklebust */ 143789f90fe1STrond Myklebust void 143889f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 143989f90fe1STrond Myklebust { 144089f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 144189f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 144289f90fe1STrond Myklebust int status; 144389f90fe1STrond Myklebust 144489f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 144589f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 144689f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 144789f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 144889f90fe1STrond Myklebust xprt_pin_rqst(next); 144989f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 145089f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 145189f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 145289f90fe1STrond Myklebust status = 0; 145389f90fe1STrond Myklebust cond_resched(); 145489f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 145589f90fe1STrond Myklebust xprt_unpin_rqst(next); 145689f90fe1STrond Myklebust if (status == 0) { 145789f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 145889f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 145989f90fe1STrond Myklebust continue; 1460c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 146189f90fe1STrond Myklebust task->tk_status = status; 146289f90fe1STrond Myklebust break; 146389f90fe1STrond Myklebust } 146489f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 1467ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1468ba60eb25STrond Myklebust { 1469ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1470ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1471ba60eb25STrond Myklebust } 1472ba60eb25STrond Myklebust 1473ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1474ba60eb25STrond Myklebust { 1475ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1476ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1477ba60eb25STrond Myklebust } 1478ba60eb25STrond Myklebust 1479ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1480ba60eb25STrond Myklebust { 1481ba60eb25STrond Myklebust bool ret = false; 1482ba60eb25STrond Myklebust 1483ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1484ba60eb25STrond Myklebust goto out; 1485ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1486ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1487ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1488ba60eb25STrond Myklebust ret = true; 1489ba60eb25STrond Myklebust } 1490ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1491ba60eb25STrond Myklebust out: 1492ba60eb25STrond Myklebust return ret; 1493ba60eb25STrond Myklebust } 1494ba60eb25STrond Myklebust 149592ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1496d9ba131dSTrond Myklebust { 1497d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1498d9ba131dSTrond Myklebust 1499ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1500d9ba131dSTrond Myklebust goto out; 1501ff699ea8SChuck Lever ++xprt->num_reqs; 150292ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 150392ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 150492ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1505d9ba131dSTrond Myklebust if (req != NULL) 1506d9ba131dSTrond Myklebust goto out; 1507ff699ea8SChuck Lever --xprt->num_reqs; 1508d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1509d9ba131dSTrond Myklebust out: 1510d9ba131dSTrond Myklebust return req; 1511d9ba131dSTrond Myklebust } 1512d9ba131dSTrond Myklebust 1513d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1514d9ba131dSTrond Myklebust { 1515ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1516ff699ea8SChuck Lever --xprt->num_reqs; 1517d9ba131dSTrond Myklebust kfree(req); 1518d9ba131dSTrond Myklebust return true; 1519d9ba131dSTrond Myklebust } 1520d9ba131dSTrond Myklebust return false; 1521d9ba131dSTrond Myklebust } 1522d9ba131dSTrond Myklebust 1523f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 15241da177e4SLinus Torvalds { 1525d9ba131dSTrond Myklebust struct rpc_rqst *req; 15261da177e4SLinus Torvalds 1527f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 15281da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1529d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1530d9ba131dSTrond Myklebust list_del(&req->rq_list); 1531d9ba131dSTrond Myklebust goto out_init_req; 1532d9ba131dSTrond Myklebust } 153392ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1534d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1535d9ba131dSTrond Myklebust goto out_init_req; 1536d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1537d9ba131dSTrond Myklebust case -ENOMEM: 1538d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1539d9ba131dSTrond Myklebust "failed! Retrying\n"); 15401afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1541d9ba131dSTrond Myklebust break; 1542d9ba131dSTrond Myklebust case -EAGAIN: 1543ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1544d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1545e9d47639SGustavo A. R. Silva /* fall through */ 15461afeaf5cSTrond Myklebust default: 1547d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 15481afeaf5cSTrond Myklebust } 1549f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1550d9ba131dSTrond Myklebust return; 1551d9ba131dSTrond Myklebust out_init_req: 1552ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1553ff699ea8SChuck Lever xprt->num_reqs); 155437ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 155537ac86c3SChuck Lever 1556d9ba131dSTrond Myklebust task->tk_status = 0; 15571da177e4SLinus Torvalds task->tk_rqstp = req; 15581da177e4SLinus Torvalds } 1559f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1560f39c1bfbSTrond Myklebust 1561a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1562ee5ebe85STrond Myklebust { 1563ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1564c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1565c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1566ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1567c25573b5STrond Myklebust } 1568ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1569ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1570ee5ebe85STrond Myklebust } 1571a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1572ee5ebe85STrond Myklebust 157321de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 157421de0a95STrond Myklebust { 157521de0a95STrond Myklebust struct rpc_rqst *req; 157621de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 157721de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 157821de0a95STrond Myklebust list_del(&req->rq_list); 157921de0a95STrond Myklebust kfree(req); 158021de0a95STrond Myklebust } 158121de0a95STrond Myklebust } 158221de0a95STrond Myklebust 1583d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1584d9ba131dSTrond Myklebust unsigned int num_prealloc, 1585d9ba131dSTrond Myklebust unsigned int max_alloc) 1586bd1722d4SPavel Emelyanov { 1587bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 158821de0a95STrond Myklebust struct rpc_rqst *req; 158921de0a95STrond Myklebust int i; 1590bd1722d4SPavel Emelyanov 1591bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1592bd1722d4SPavel Emelyanov if (xprt == NULL) 1593bd1722d4SPavel Emelyanov goto out; 1594bd1722d4SPavel Emelyanov 159521de0a95STrond Myklebust xprt_init(xprt, net); 159621de0a95STrond Myklebust 159721de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 159821de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 159921de0a95STrond Myklebust if (!req) 16008313164cSwangweidong goto out_free; 160121de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 160221de0a95STrond Myklebust } 1603d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1604d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1605d9ba131dSTrond Myklebust else 160621de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1607d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1608ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1609bd1722d4SPavel Emelyanov 1610bd1722d4SPavel Emelyanov return xprt; 1611bd1722d4SPavel Emelyanov 1612bd1722d4SPavel Emelyanov out_free: 161321de0a95STrond Myklebust xprt_free(xprt); 1614bd1722d4SPavel Emelyanov out: 1615bd1722d4SPavel Emelyanov return NULL; 1616bd1722d4SPavel Emelyanov } 1617bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1618bd1722d4SPavel Emelyanov 1619e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1620e204e621SPavel Emelyanov { 162137aa2133SPavel Emelyanov put_net(xprt->xprt_net); 162221de0a95STrond Myklebust xprt_free_all_slots(xprt); 1623fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1624e204e621SPavel Emelyanov } 1625e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1626e204e621SPavel Emelyanov 1627902c5887STrond Myklebust static void 1628902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1629902c5887STrond Myklebust { 1630902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1631902c5887STrond Myklebust } 1632902c5887STrond Myklebust 16339dc6edcfSTrond Myklebust static __be32 16349dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 16359dc6edcfSTrond Myklebust { 16369dc6edcfSTrond Myklebust __be32 xid; 16379dc6edcfSTrond Myklebust 16389dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 16399dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 16409dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 16419dc6edcfSTrond Myklebust return xid; 16429dc6edcfSTrond Myklebust } 16439dc6edcfSTrond Myklebust 16449dc6edcfSTrond Myklebust static void 16459dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 16469dc6edcfSTrond Myklebust { 16479dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 16489dc6edcfSTrond Myklebust } 16499dc6edcfSTrond Myklebust 16509dc6edcfSTrond Myklebust static void 16519dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 16529dc6edcfSTrond Myklebust { 16539dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16549dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 16559dc6edcfSTrond Myklebust 16569dc6edcfSTrond Myklebust req->rq_task = task; 16579dc6edcfSTrond Myklebust req->rq_xprt = xprt; 16589dc6edcfSTrond Myklebust req->rq_buffer = NULL; 16599dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1660902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 16619dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 16629dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 16639dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 16649dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 166571700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 166671700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 16679dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 1668da953063STrond Myklebust xprt_init_majortimeo(task, req); 16699dc6edcfSTrond Myklebust dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 16709dc6edcfSTrond Myklebust req, ntohl(req->rq_xid)); 16719dc6edcfSTrond Myklebust } 16729dc6edcfSTrond Myklebust 16739dc6edcfSTrond Myklebust static void 16749dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 16759dc6edcfSTrond Myklebust { 16769dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 16779dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 16789dc6edcfSTrond Myklebust xprt_request_init(task); 16799dc6edcfSTrond Myklebust } 16809dc6edcfSTrond Myklebust 16819903cd1cSChuck Lever /** 16829903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 16839903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 16849903cd1cSChuck Lever * 1685ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1686ba60eb25STrond Myklebust * slots are available, place the task on the transport's 16879903cd1cSChuck Lever * backlog queue. 16889903cd1cSChuck Lever */ 16899903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 16901da177e4SLinus Torvalds { 1691fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 16921da177e4SLinus Torvalds 169343cedbf0STrond Myklebust task->tk_status = 0; 169443cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 169543cedbf0STrond Myklebust return; 169643cedbf0STrond Myklebust 169743cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1698ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 16999dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1700ba60eb25STrond Myklebust } 1701ba60eb25STrond Myklebust 1702ba60eb25STrond Myklebust /** 1703ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1704ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1705ba60eb25STrond Myklebust * 1706ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1707ba60eb25STrond Myklebust * backlog queue. 1708ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1709ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1710ba60eb25STrond Myklebust */ 1711ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1712ba60eb25STrond Myklebust { 1713fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1714ba60eb25STrond Myklebust 1715ba60eb25STrond Myklebust task->tk_status = 0; 1716ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1717ba60eb25STrond Myklebust return; 1718ba60eb25STrond Myklebust 1719ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 17209dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 1723edc81dcdSTrond Myklebust static void 1724edc81dcdSTrond Myklebust xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req) 1725edc81dcdSTrond Myklebust { 1726edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1727edc81dcdSTrond Myklebust 1728944b0429STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1729944b0429STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1730edc81dcdSTrond Myklebust xprt_is_pinned_rqst(req)) { 1731edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1732944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1733edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1734edc81dcdSTrond Myklebust while (xprt_is_pinned_rqst(req)) { 1735edc81dcdSTrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1736edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1737edc81dcdSTrond Myklebust xprt_wait_on_pinned_rqst(req); 1738edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1739edc81dcdSTrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1740edc81dcdSTrond Myklebust } 1741edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1742edc81dcdSTrond Myklebust } 1743edc81dcdSTrond Myklebust } 1744edc81dcdSTrond Myklebust 17459903cd1cSChuck Lever /** 17469903cd1cSChuck Lever * xprt_release - release an RPC request slot 17479903cd1cSChuck Lever * @task: task which is finished with the slot 17489903cd1cSChuck Lever * 17491da177e4SLinus Torvalds */ 17509903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 17511da177e4SLinus Torvalds { 175255ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 175387ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17541da177e4SLinus Torvalds 175587ed5003STrond Myklebust if (req == NULL) { 175687ed5003STrond Myklebust if (task->tk_client) { 1757fb43d172STrond Myklebust xprt = task->tk_xprt; 175887ed5003STrond Myklebust xprt_release_write(xprt, task); 175987ed5003STrond Myklebust } 17601da177e4SLinus Torvalds return; 176187ed5003STrond Myklebust } 176255ae1aabSRicardo Labiaga 176355ae1aabSRicardo Labiaga xprt = req->rq_xprt; 1764edc81dcdSTrond Myklebust xprt_request_dequeue_all(task, req); 1765b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 176649e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1767a58dd398SChuck Lever if (xprt->ops->release_request) 1768a58dd398SChuck Lever xprt->ops->release_request(task); 1769ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 1770b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 1771ee5ebe85STrond Myklebust if (req->rq_buffer) 17723435c74aSChuck Lever xprt->ops->buf_free(task); 17734a068258SChuck Lever xprt_inject_disconnect(xprt); 17749d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 17750472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1776a17c2153STrond Myklebust if (req->rq_cred != NULL) 1777a17c2153STrond Myklebust put_rpccred(req->rq_cred); 17781da177e4SLinus Torvalds task->tk_rqstp = NULL; 1779ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1780ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 178155ae1aabSRicardo Labiaga 178246121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1783ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1784a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1785ee5ebe85STrond Myklebust else 1786c9acb42eSTrond Myklebust xprt_free_bc_request(req); 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 1789902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1790902c5887STrond Myklebust void 1791902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1792902c5887STrond Myklebust { 1793902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1794902c5887STrond Myklebust 1795902c5887STrond Myklebust task->tk_rqstp = req; 1796902c5887STrond Myklebust req->rq_task = task; 1797902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1798902c5887STrond Myklebust /* 1799902c5887STrond Myklebust * Set up the xdr_buf length. 1800902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1801902c5887STrond Myklebust */ 1802902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1803902c5887STrond Myklebust xbufp->tail[0].iov_len; 1804902c5887STrond Myklebust } 1805902c5887STrond Myklebust #endif 1806902c5887STrond Myklebust 180721de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1808c2866763SChuck Lever { 180930c5116bSTrond Myklebust kref_init(&xprt->kref); 1810c2866763SChuck Lever 1811c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1812c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 181375c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1814c2866763SChuck Lever 1815c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 181695f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1817944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 18189e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1819f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1820f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 18219e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 182280b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1823f9acac1aSRicardo Labiaga 1824c2866763SChuck Lever xprt->last_used = jiffies; 1825c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1826a509050bSChuck Lever xprt->bind_index = 0; 1827c2866763SChuck Lever 1828c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1829c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 183079c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1831c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1832c2866763SChuck Lever 1833c2866763SChuck Lever xprt_init_xid(xprt); 1834c2866763SChuck Lever 183521de0a95STrond Myklebust xprt->xprt_net = get_net(net); 18368d9266ffSTrond Myklebust } 18378d9266ffSTrond Myklebust 18388d9266ffSTrond Myklebust /** 18398d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 18408d9266ffSTrond Myklebust * @args: rpc transport creation arguments 18418d9266ffSTrond Myklebust * 18428d9266ffSTrond Myklebust */ 18438d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 18448d9266ffSTrond Myklebust { 18458d9266ffSTrond Myklebust struct rpc_xprt *xprt; 18468d9266ffSTrond Myklebust struct xprt_class *t; 18478d9266ffSTrond Myklebust 18488d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 18498d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 18508d9266ffSTrond Myklebust if (t->ident == args->ident) { 18518d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18528d9266ffSTrond Myklebust goto found; 18538d9266ffSTrond Myklebust } 18548d9266ffSTrond Myklebust } 18558d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 18563c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 18578d9266ffSTrond Myklebust return ERR_PTR(-EIO); 18588d9266ffSTrond Myklebust 18598d9266ffSTrond Myklebust found: 18608d9266ffSTrond Myklebust xprt = t->setup(args); 18618d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 18628d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 18638d9266ffSTrond Myklebust -PTR_ERR(xprt)); 186421de0a95STrond Myklebust goto out; 18658d9266ffSTrond Myklebust } 186633d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 186733d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 186821de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 186921de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1870502980e8SAnna Schumaker timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 187121de0a95STrond Myklebust else 1872ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 18734e0038b6STrond Myklebust 18744e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 18754e0038b6STrond Myklebust xprt_destroy(xprt); 18764e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 18774e0038b6STrond Myklebust } 18784e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 18794e0038b6STrond Myklebust if (xprt->servername == NULL) { 18804e0038b6STrond Myklebust xprt_destroy(xprt); 18814e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 18824e0038b6STrond Myklebust } 18834e0038b6STrond Myklebust 18843f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1885388f0c77SJeff Layton 1886c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1887c2866763SChuck Lever xprt->max_reqs); 188821de0a95STrond Myklebust out: 1889c2866763SChuck Lever return xprt; 1890c2866763SChuck Lever } 1891c2866763SChuck Lever 1892528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 1893528fd354STrond Myklebust { 1894528fd354STrond Myklebust struct rpc_xprt *xprt = 1895528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 1896528fd354STrond Myklebust 1897528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 1898528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1899528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1900528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1901528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1902528fd354STrond Myklebust kfree(xprt->servername); 1903528fd354STrond Myklebust /* 1904528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 1905528fd354STrond Myklebust */ 1906528fd354STrond Myklebust xprt->ops->destroy(xprt); 1907528fd354STrond Myklebust } 1908528fd354STrond Myklebust 19099903cd1cSChuck Lever /** 19109903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1911a8de240aSTrond Myklebust * @xprt: transport to destroy 19129903cd1cSChuck Lever * 19131da177e4SLinus Torvalds */ 1914a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 19151da177e4SLinus Torvalds { 19161da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 191779234c3dSTrond Myklebust 1918528fd354STrond Myklebust /* 1919528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 1920528fd354STrond Myklebust */ 192179234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 192279234c3dSTrond Myklebust 19230065db32STrond Myklebust del_timer_sync(&xprt->timer); 1924c8541ecdSChuck Lever 1925c8541ecdSChuck Lever /* 1926528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 1927528fd354STrond Myklebust * safely flush receive work running on rpciod. 1928c8541ecdSChuck Lever */ 1929528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 1930528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 19316b6ca86bSTrond Myklebust } 19321da177e4SLinus Torvalds 193330c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 193430c5116bSTrond Myklebust { 193530c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 193630c5116bSTrond Myklebust } 193730c5116bSTrond Myklebust 193830c5116bSTrond Myklebust /** 193930c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 194030c5116bSTrond Myklebust * @xprt: pointer to the transport 194130c5116bSTrond Myklebust * 194230c5116bSTrond Myklebust */ 194330c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 194430c5116bSTrond Myklebust { 194530c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 194630c5116bSTrond Myklebust return xprt; 194730c5116bSTrond Myklebust return NULL; 194830c5116bSTrond Myklebust } 194930c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 195030c5116bSTrond Myklebust 19516b6ca86bSTrond Myklebust /** 19526b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 19536b6ca86bSTrond Myklebust * @xprt: pointer to the transport 19546b6ca86bSTrond Myklebust * 19556b6ca86bSTrond Myklebust */ 19566b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 19576b6ca86bSTrond Myklebust { 195830c5116bSTrond Myklebust if (xprt != NULL) 195930c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 19606b6ca86bSTrond Myklebust } 19615d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 1962