11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 51da177e4SLinus Torvalds * and asynchronous calls. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The interface works like this: 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 101da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 111da177e4SLinus Torvalds * (xprt_reserve). 121da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1355aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1455aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1555ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1655ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1755ae1aabSRicardo Labiaga * expired. 181da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 1955aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 201da177e4SLinus Torvalds * caller is woken up, and the timer removed. 211da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 221da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 231da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 241da177e4SLinus Torvalds * of -ETIMEDOUT. 251da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 261da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 271da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 281da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 291da177e4SLinus Torvalds * again. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 321da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 331da177e4SLinus Torvalds * tasks that rely on callbacks. 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3655aa4f58SChuck Lever * 3755aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 40a246b010SChuck Lever #include <linux/module.h> 41a246b010SChuck Lever 421da177e4SLinus Torvalds #include <linux/types.h> 43a246b010SChuck Lever #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/workqueue.h> 45bf3fcf89SChuck Lever #include <linux/net.h> 46ff839970SChuck Lever #include <linux/ktime.h> 471da177e4SLinus Torvalds 48a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 51fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 521da177e4SLinus Torvalds 533705ad64SJeff Layton #include <trace/events/sunrpc.h> 543705ad64SJeff Layton 5555ae1aabSRicardo Labiaga #include "sunrpc.h" 5655ae1aabSRicardo Labiaga 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * Local variables 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds 61f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 621da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 631da177e4SLinus Torvalds #endif 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * Local functions 671da177e4SLinus Torvalds */ 6821de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 691da177e4SLinus Torvalds static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 701da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 711da177e4SLinus Torvalds static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 7229807318SNeil Brown static void __xprt_put_cong(struct rpc_xprt *, struct rpc_rqst *); 734e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 741da177e4SLinus Torvalds 755ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7681c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7781c098afS\"Talpey, Thomas\ 7812a80469SChuck Lever /** 7981c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 8081c098afS\"Talpey, Thomas\ * @transport: transport to register 8181c098afS\"Talpey, Thomas\ * 8281c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 8381c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 8481c098afS\"Talpey, Thomas\ * 8581c098afS\"Talpey, Thomas\ * Returns: 8681c098afS\"Talpey, Thomas\ * 0: transport successfully registered 8781c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 8881c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 8981c098afS\"Talpey, Thomas\ */ 9081c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 9181c098afS\"Talpey, Thomas\ { 9281c098afS\"Talpey, Thomas\ struct xprt_class *t; 9381c098afS\"Talpey, Thomas\ int result; 9481c098afS\"Talpey, Thomas\ 9581c098afS\"Talpey, Thomas\ result = -EEXIST; 9681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 9781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 9881c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 994fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 10081c098afS\"Talpey, Thomas\ goto out; 10181c098afS\"Talpey, Thomas\ } 10281c098afS\"Talpey, Thomas\ 10381c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 10481c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 10581c098afS\"Talpey, Thomas\ transport->name); 10681c098afS\"Talpey, Thomas\ result = 0; 10781c098afS\"Talpey, Thomas\ 10881c098afS\"Talpey, Thomas\ out: 10981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 11081c098afS\"Talpey, Thomas\ return result; 11181c098afS\"Talpey, Thomas\ } 11281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 11381c098afS\"Talpey, Thomas\ 11481c098afS\"Talpey, Thomas\ /** 11581c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 11665b6e42cSRandy Dunlap * @transport: transport to unregister 11781c098afS\"Talpey, Thomas\ * 11881c098afS\"Talpey, Thomas\ * Returns: 11981c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 12081c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 12181c098afS\"Talpey, Thomas\ */ 12281c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 12381c098afS\"Talpey, Thomas\ { 12481c098afS\"Talpey, Thomas\ struct xprt_class *t; 12581c098afS\"Talpey, Thomas\ int result; 12681c098afS\"Talpey, Thomas\ 12781c098afS\"Talpey, Thomas\ result = 0; 12881c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 12981c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 13081c098afS\"Talpey, Thomas\ if (t == transport) { 13181c098afS\"Talpey, Thomas\ printk(KERN_INFO 13281c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 13381c098afS\"Talpey, Thomas\ transport->name); 13481c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 13581c098afS\"Talpey, Thomas\ goto out; 13681c098afS\"Talpey, Thomas\ } 13781c098afS\"Talpey, Thomas\ } 13881c098afS\"Talpey, Thomas\ result = -ENOENT; 13981c098afS\"Talpey, Thomas\ 14081c098afS\"Talpey, Thomas\ out: 14181c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 14281c098afS\"Talpey, Thomas\ return result; 14381c098afS\"Talpey, Thomas\ } 14481c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 14581c098afS\"Talpey, Thomas\ 14681c098afS\"Talpey, Thomas\ /** 147441e3e24STom Talpey * xprt_load_transport - load a transport implementation 148441e3e24STom Talpey * @transport_name: transport to load 149441e3e24STom Talpey * 150441e3e24STom Talpey * Returns: 151441e3e24STom Talpey * 0: transport successfully loaded 152441e3e24STom Talpey * -ENOENT: transport module not available 153441e3e24STom Talpey */ 154441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 155441e3e24STom Talpey { 156441e3e24STom Talpey struct xprt_class *t; 157441e3e24STom Talpey int result; 158441e3e24STom Talpey 159441e3e24STom Talpey result = 0; 160441e3e24STom Talpey spin_lock(&xprt_list_lock); 161441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 162441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 163441e3e24STom Talpey spin_unlock(&xprt_list_lock); 164441e3e24STom Talpey goto out; 165441e3e24STom Talpey } 166441e3e24STom Talpey } 167441e3e24STom Talpey spin_unlock(&xprt_list_lock); 168ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 169441e3e24STom Talpey out: 170441e3e24STom Talpey return result; 171441e3e24STom Talpey } 172441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 173441e3e24STom Talpey 174441e3e24STom Talpey /** 17512a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 17612a80469SChuck Lever * @task: task that is requesting access to the transport 177177c27bfSRandy Dunlap * @xprt: pointer to the target transport 17812a80469SChuck Lever * 17912a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 18012a80469SChuck Lever * transport connects from colliding with writes. No congestion control 18112a80469SChuck Lever * is provided. 1821da177e4SLinus Torvalds */ 18343cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1841da177e4SLinus Torvalds { 18512a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 18634006ceeSTrond Myklebust int priority; 18712a80469SChuck Lever 18812a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 18912a80469SChuck Lever if (task == xprt->snd_task) 19012a80469SChuck Lever return 1; 19112a80469SChuck Lever goto out_sleep; 19212a80469SChuck Lever } 19312a80469SChuck Lever xprt->snd_task = task; 19492551948STrond Myklebust if (req != NULL) 19512a80469SChuck Lever req->rq_ntrans++; 1964d4a76f3Sj223yang@asset.uwaterloo.ca 19712a80469SChuck Lever return 1; 19812a80469SChuck Lever 19912a80469SChuck Lever out_sleep: 20046121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 20112a80469SChuck Lever task->tk_pid, xprt); 20212a80469SChuck Lever task->tk_timeout = 0; 20312a80469SChuck Lever task->tk_status = -EAGAIN; 20434006ceeSTrond Myklebust if (req == NULL) 20534006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 20634006ceeSTrond Myklebust else if (!req->rq_ntrans) 20734006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 20812a80469SChuck Lever else 20934006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 21034006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 21112a80469SChuck Lever return 0; 21212a80469SChuck Lever } 21312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 21412a80469SChuck Lever 215632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 216632e3bdcSTrond Myklebust { 217632e3bdcSTrond Myklebust xprt->snd_task = NULL; 218d19751e7STrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 2194e857c58SPeter Zijlstra smp_mb__before_atomic(); 220632e3bdcSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 2214e857c58SPeter Zijlstra smp_mb__after_atomic(); 222632e3bdcSTrond Myklebust } else 22340a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 224632e3bdcSTrond Myklebust } 225632e3bdcSTrond Myklebust 22612a80469SChuck Lever /* 22712a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 22812a80469SChuck Lever * @task: task that is requesting access to the transport 22912a80469SChuck Lever * 23012a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 23112a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 23212a80469SChuck Lever * woken up and given access to the transport. 23312a80469SChuck Lever */ 23443cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 23512a80469SChuck Lever { 2361da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 23734006ceeSTrond Myklebust int priority; 2381da177e4SLinus Torvalds 2392226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2401da177e4SLinus Torvalds if (task == xprt->snd_task) 2411da177e4SLinus Torvalds return 1; 2421da177e4SLinus Torvalds goto out_sleep; 2431da177e4SLinus Torvalds } 24443cedbf0STrond Myklebust if (req == NULL) { 24543cedbf0STrond Myklebust xprt->snd_task = task; 24643cedbf0STrond Myklebust return 1; 24743cedbf0STrond Myklebust } 24812a80469SChuck Lever if (__xprt_get_cong(xprt, task)) { 2491da177e4SLinus Torvalds xprt->snd_task = task; 2501da177e4SLinus Torvalds req->rq_ntrans++; 2511da177e4SLinus Torvalds return 1; 2521da177e4SLinus Torvalds } 253632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2541da177e4SLinus Torvalds out_sleep: 25529807318SNeil Brown if (req) 25629807318SNeil Brown __xprt_put_cong(xprt, req); 25746121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2581da177e4SLinus Torvalds task->tk_timeout = 0; 2591da177e4SLinus Torvalds task->tk_status = -EAGAIN; 26034006ceeSTrond Myklebust if (req == NULL) 26134006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 26234006ceeSTrond Myklebust else if (!req->rq_ntrans) 26334006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 2641da177e4SLinus Torvalds else 26534006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 26634006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 2671da177e4SLinus Torvalds return 0; 2681da177e4SLinus Torvalds } 26912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2701da177e4SLinus Torvalds 27112a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds int retval; 2741da177e4SLinus Torvalds 2754a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 27643cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2774a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2781da177e4SLinus Torvalds return retval; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 281961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2821da177e4SLinus Torvalds { 283961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 28449e9a890SChuck Lever struct rpc_rqst *req; 28549e9a890SChuck Lever 28649e9a890SChuck Lever req = task->tk_rqstp; 28749e9a890SChuck Lever xprt->snd_task = task; 28892551948STrond Myklebust if (req) 28949e9a890SChuck Lever req->rq_ntrans++; 290961a828dSTrond Myklebust return true; 291961a828dSTrond Myklebust } 292961a828dSTrond Myklebust 293961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 294961a828dSTrond Myklebust { 295961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 29649e9a890SChuck Lever return; 29749e9a890SChuck Lever 298f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 299f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 300961a828dSTrond Myklebust return; 301632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 30249e9a890SChuck Lever } 30349e9a890SChuck Lever 304961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data) 30549e9a890SChuck Lever { 306961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 30743cedbf0STrond Myklebust struct rpc_rqst *req; 3081da177e4SLinus Torvalds 30943cedbf0STrond Myklebust req = task->tk_rqstp; 31043cedbf0STrond Myklebust if (req == NULL) { 3111da177e4SLinus Torvalds xprt->snd_task = task; 312961a828dSTrond Myklebust return true; 31343cedbf0STrond Myklebust } 31443cedbf0STrond Myklebust if (__xprt_get_cong(xprt, task)) { 31543cedbf0STrond Myklebust xprt->snd_task = task; 3161da177e4SLinus Torvalds req->rq_ntrans++; 317961a828dSTrond Myklebust return true; 3181da177e4SLinus Torvalds } 319961a828dSTrond Myklebust return false; 320961a828dSTrond Myklebust } 321961a828dSTrond Myklebust 322961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 323961a828dSTrond Myklebust { 324961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 325961a828dSTrond Myklebust return; 326961a828dSTrond Myklebust if (RPCXPRT_CONGESTED(xprt)) 327961a828dSTrond Myklebust goto out_unlock; 328f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 329f1dc237cSTrond Myklebust __xprt_lock_write_cong_func, xprt)) 330961a828dSTrond Myklebust return; 3311da177e4SLinus Torvalds out_unlock: 332632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3350695314eSTrond Myklebust static void xprt_task_clear_bytes_sent(struct rpc_task *task) 3360695314eSTrond Myklebust { 3370695314eSTrond Myklebust if (task != NULL) { 3380695314eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 3390695314eSTrond Myklebust if (req != NULL) 3400695314eSTrond Myklebust req->rq_bytes_sent = 0; 3410695314eSTrond Myklebust } 3420695314eSTrond Myklebust } 3430695314eSTrond Myklebust 34449e9a890SChuck Lever /** 34549e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 34649e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 34749e9a890SChuck Lever * @task: task that is releasing access to the transport 34849e9a890SChuck Lever * 34949e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3501da177e4SLinus Torvalds */ 35149e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3521da177e4SLinus Torvalds { 3531da177e4SLinus Torvalds if (xprt->snd_task == task) { 3540695314eSTrond Myklebust xprt_task_clear_bytes_sent(task); 355632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3561da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds } 35912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3601da177e4SLinus Torvalds 36149e9a890SChuck Lever /** 36249e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 36349e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 36449e9a890SChuck Lever * @task: task that is releasing access to the transport 36549e9a890SChuck Lever * 36649e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 36749e9a890SChuck Lever * transport if the transport's congestion window allows it. 36849e9a890SChuck Lever */ 36949e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 37049e9a890SChuck Lever { 37149e9a890SChuck Lever if (xprt->snd_task == task) { 3720695314eSTrond Myklebust xprt_task_clear_bytes_sent(task); 373632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 37449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 37549e9a890SChuck Lever } 37649e9a890SChuck Lever } 37712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 37849e9a890SChuck Lever 37949e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3801da177e4SLinus Torvalds { 3814a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 38249e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3834a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* 3871da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3881da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3891da177e4SLinus Torvalds */ 3901da177e4SLinus Torvalds static int 3911da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds if (req->rq_cong) 3961da177e4SLinus Torvalds return 1; 39746121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 3981da177e4SLinus Torvalds task->tk_pid, xprt->cong, xprt->cwnd); 3991da177e4SLinus Torvalds if (RPCXPRT_CONGESTED(xprt)) 4001da177e4SLinus Torvalds return 0; 4011da177e4SLinus Torvalds req->rq_cong = 1; 4021da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4031da177e4SLinus Torvalds return 1; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4081da177e4SLinus Torvalds * that has been sleeping due to congestion 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds static void 4111da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds if (!req->rq_cong) 4141da177e4SLinus Torvalds return; 4151da177e4SLinus Torvalds req->rq_cong = 0; 4161da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 41749e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 42046c0ee8bSChuck Lever /** 421a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 422a58dd398SChuck Lever * @task: RPC request that recently completed 423a58dd398SChuck Lever * 424a58dd398SChuck Lever * Useful for transports that require congestion control. 425a58dd398SChuck Lever */ 426a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 427a58dd398SChuck Lever { 428a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 429a4f0835cSTrond Myklebust 430a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 431a58dd398SChuck Lever } 43212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 433a58dd398SChuck Lever 434a58dd398SChuck Lever /** 43546c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4366a24dfb6STrond Myklebust * @xprt: pointer to xprt 43746c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 43846c0ee8bSChuck Lever * @result: result code of completed RPC request 43946c0ee8bSChuck Lever * 4404f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4414f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4424f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4434f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4444f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4454f4cf5adSChuck Lever * 4464f4cf5adSChuck Lever * - a reply is received and 4474f4cf5adSChuck Lever * - a full number of requests are outstanding and 4484f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4491da177e4SLinus Torvalds */ 4506a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4511da177e4SLinus Torvalds { 45246c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 45346c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4561da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4571da177e4SLinus Torvalds * the result gets rounded properly. */ 4581da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4591da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4601da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 46149e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4621da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4631da177e4SLinus Torvalds cwnd >>= 1; 4641da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4651da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4681da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4691da177e4SLinus Torvalds xprt->cwnd = cwnd; 47046c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4711da177e4SLinus Torvalds } 47212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4731da177e4SLinus Torvalds 47444fbac22SChuck Lever /** 47544fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 47644fbac22SChuck Lever * @xprt: transport with waiting tasks 47744fbac22SChuck Lever * @status: result code to plant in each task before waking it 47844fbac22SChuck Lever * 47944fbac22SChuck Lever */ 48044fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 48144fbac22SChuck Lever { 48244fbac22SChuck Lever if (status < 0) 48344fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 48444fbac22SChuck Lever else 48544fbac22SChuck Lever rpc_wake_up(&xprt->pending); 48644fbac22SChuck Lever } 48712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 48844fbac22SChuck Lever 489c7b2cae8SChuck Lever /** 490c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 491c7b2cae8SChuck Lever * @task: task to be put to sleep 4920b80ae42SRandy Dunlap * @action: function pointer to be executed after wait 493a9a6b52eSTrond Myklebust * 494a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 495a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 496a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 497c7b2cae8SChuck Lever */ 498b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) 499c7b2cae8SChuck Lever { 500c7b2cae8SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 501c7b2cae8SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 502c7b2cae8SChuck Lever 503a9a6b52eSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 504b6ddf64fSTrond Myklebust rpc_sleep_on(&xprt->pending, task, action); 505c7b2cae8SChuck Lever } 50612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 507c7b2cae8SChuck Lever 508c7b2cae8SChuck Lever /** 509c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 510c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 511c7b2cae8SChuck Lever * 512c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 513c7b2cae8SChuck Lever */ 514c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt) 515c7b2cae8SChuck Lever { 516c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 517c7b2cae8SChuck Lever if (xprt->snd_task) { 51846121cf7SChuck Lever dprintk("RPC: write space: waking waiting task on " 51946121cf7SChuck Lever "xprt %p\n", xprt); 520fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); 521c7b2cae8SChuck Lever } 522c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 523c7b2cae8SChuck Lever } 52412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 525c7b2cae8SChuck Lever 526fe3aca29SChuck Lever /** 527fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 528fe3aca29SChuck Lever * @task: task whose timeout is to be set 529fe3aca29SChuck Lever * 530fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 531fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 532fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 533fe3aca29SChuck Lever */ 534fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 535fe3aca29SChuck Lever { 536fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 537fe3aca29SChuck Lever } 53812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 539fe3aca29SChuck Lever 5402c53040fSBen Hutchings /** 541fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 542fe3aca29SChuck Lever * @task: task whose timeout is to be set 543fe3aca29SChuck Lever * 544fe3aca29SChuck Lever * Set a request's retransmit timeout using the RTT estimator. 545fe3aca29SChuck Lever */ 546fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 547fe3aca29SChuck Lever { 548fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 549ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 550ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 551fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 552ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 553fe3aca29SChuck Lever 554fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 555fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 556fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 557fe3aca29SChuck Lever task->tk_timeout = max_timeout; 558fe3aca29SChuck Lever } 55912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 560fe3aca29SChuck Lever 5611da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5621da177e4SLinus Torvalds { 563ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5661da177e4SLinus Torvalds if (to->to_exponential) 5671da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5681da177e4SLinus Torvalds else 5691da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5701da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5711da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5721da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5759903cd1cSChuck Lever /** 5769903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5779903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5789903cd1cSChuck Lever * 5791da177e4SLinus Torvalds */ 5801da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 583ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5841da177e4SLinus Torvalds int status = 0; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5871da177e4SLinus Torvalds if (to->to_exponential) 5881da177e4SLinus Torvalds req->rq_timeout <<= 1; 5891da177e4SLinus Torvalds else 5901da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5911da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5921da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5931da177e4SLinus Torvalds req->rq_retries++; 5941da177e4SLinus Torvalds } else { 5951da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5961da177e4SLinus Torvalds req->rq_retries = 0; 5971da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5981da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 5994a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6001da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 6014a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6021da177e4SLinus Torvalds status = -ETIMEDOUT; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6061da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6071da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds return status; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 61265f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6131da177e4SLinus Torvalds { 61465f27f38SDavid Howells struct rpc_xprt *xprt = 61565f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6161da177e4SLinus Torvalds 61766af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6184876cc77STrond Myklebust xprt->ops->close(xprt); 6191da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 62079234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6239903cd1cSChuck Lever /** 62462da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6259903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6269903cd1cSChuck Lever * 6271da177e4SLinus Torvalds */ 62862da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6314a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6321da177e4SLinus Torvalds xprt_clear_connected(xprt); 6332a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6344a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6351da177e4SLinus Torvalds } 63662da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6371da177e4SLinus Torvalds 63866af1e55STrond Myklebust /** 63966af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 64066af1e55STrond Myklebust * @xprt: transport to disconnect 64166af1e55STrond Myklebust * 64266af1e55STrond Myklebust */ 64366af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 64466af1e55STrond Myklebust { 64566af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 64666af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 64766af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 64866af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 64966af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 65040a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6512a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 65266af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 65366af1e55STrond Myklebust } 654e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 65566af1e55STrond Myklebust 6567c1d71cfSTrond Myklebust /** 6577c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6587c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6597c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6607c1d71cfSTrond Myklebust * 6617c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6627c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6637c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6647c1d71cfSTrond Myklebust * a batch of RPC requests. 6657c1d71cfSTrond Myklebust * 6667c1d71cfSTrond Myklebust */ 6677c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6687c1d71cfSTrond Myklebust { 6697c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6707c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6717c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6727c1d71cfSTrond Myklebust goto out; 6732c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 6747c1d71cfSTrond Myklebust goto out; 6757c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6767c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6777c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 67840a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 6792a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6807c1d71cfSTrond Myklebust out: 6817c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6827c1d71cfSTrond Myklebust } 6837c1d71cfSTrond Myklebust 684ad3331acSTrond Myklebust static bool 685ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 686ad3331acSTrond Myklebust { 687ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 688ad3331acSTrond Myklebust } 689ad3331acSTrond Myklebust 690ad3331acSTrond Myklebust static void 691ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 692ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 693ad3331acSTrond Myklebust { 694ad3331acSTrond Myklebust if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) 695ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 696ad3331acSTrond Myklebust } 697ad3331acSTrond Myklebust 6981da177e4SLinus Torvalds static void 6991da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data) 7001da177e4SLinus Torvalds { 7011da177e4SLinus Torvalds struct rpc_xprt *xprt = (struct rpc_xprt *)data; 7021da177e4SLinus Torvalds 7034a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 704d19751e7STrond Myklebust if (!list_empty(&xprt->recv)) 7051da177e4SLinus Torvalds goto out_abort; 706ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 707ad3331acSTrond Myklebust xprt->last_used = jiffies; 7082226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 7091da177e4SLinus Torvalds goto out_abort; 7104a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 71140a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7121da177e4SLinus Torvalds return; 7131da177e4SLinus Torvalds out_abort: 7144a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 717718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 718718ba5b8STrond Myklebust struct rpc_task *task, 719718ba5b8STrond Myklebust void *cookie) 720718ba5b8STrond Myklebust { 721718ba5b8STrond Myklebust bool ret = false; 722718ba5b8STrond Myklebust 723718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 724718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 725718ba5b8STrond Myklebust goto out; 726718ba5b8STrond Myklebust if (xprt->snd_task != task) 727718ba5b8STrond Myklebust goto out; 7280695314eSTrond Myklebust xprt_task_clear_bytes_sent(task); 729718ba5b8STrond Myklebust xprt->snd_task = cookie; 730718ba5b8STrond Myklebust ret = true; 731718ba5b8STrond Myklebust out: 732718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 733718ba5b8STrond Myklebust return ret; 734718ba5b8STrond Myklebust } 735718ba5b8STrond Myklebust 736718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 737718ba5b8STrond Myklebust { 738718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 739718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 740718ba5b8STrond Myklebust goto out; 741718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 742718ba5b8STrond Myklebust goto out; 743718ba5b8STrond Myklebust xprt->snd_task =NULL; 744718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 745ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 746718ba5b8STrond Myklebust out: 747718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 74879234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 749718ba5b8STrond Myklebust } 750718ba5b8STrond Myklebust 7519903cd1cSChuck Lever /** 7529903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 7539903cd1cSChuck Lever * @task: RPC task that is requesting the connect 7541da177e4SLinus Torvalds * 7551da177e4SLinus Torvalds */ 7561da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 7571da177e4SLinus Torvalds { 758ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7591da177e4SLinus Torvalds 76046121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 7611da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 7621da177e4SLinus Torvalds 763ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 76401d37c42STrond Myklebust task->tk_status = -EAGAIN; 7651da177e4SLinus Torvalds return; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7681da177e4SLinus Torvalds return; 769feb8ca37STrond Myklebust 770feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 771feb8ca37STrond Myklebust xprt->ops->close(xprt); 772feb8ca37STrond Myklebust 773718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 7741da177e4SLinus Torvalds task->tk_rqstp->rq_bytes_sent = 0; 775a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7762c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 7775d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 7780b9e7943STrond Myklebust 7790b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7800b9e7943STrond Myklebust return; 7810b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7820b9e7943STrond Myklebust return; 783262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 7841b092092STrond Myklebust xprt->ops->connect(xprt, task); 7851da177e4SLinus Torvalds } 786718ba5b8STrond Myklebust xprt_release_write(xprt, task); 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7899903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 7901da177e4SLinus Torvalds { 791ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7921da177e4SLinus Torvalds 793cd983ef8SChuck Lever if (task->tk_status == 0) { 794262ca07dSChuck Lever xprt->stat.connect_count++; 795262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 79646121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 7971da177e4SLinus Torvalds task->tk_pid); 7981da177e4SLinus Torvalds return; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds switch (task->tk_status) { 8020fe8d04eSTrond Myklebust case -ECONNREFUSED: 8030fe8d04eSTrond Myklebust case -ECONNRESET: 8040fe8d04eSTrond Myklebust case -ECONNABORTED: 8050fe8d04eSTrond Myklebust case -ENETUNREACH: 8060fe8d04eSTrond Myklebust case -EHOSTUNREACH: 8072fc193cfSTrond Myklebust case -EPIPE: 8082a491991STrond Myklebust case -EAGAIN: 8092a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 81023475d66SChuck Lever break; 8111da177e4SLinus Torvalds case -ETIMEDOUT: 81246121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 81346121cf7SChuck Lever "out\n", task->tk_pid); 8141da177e4SLinus Torvalds break; 8151da177e4SLinus Torvalds default: 81646121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 81746121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 8184e0038b6STrond Myklebust xprt->servername); 81923475d66SChuck Lever task->tk_status = -EIO; 82023475d66SChuck Lever } 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8239903cd1cSChuck Lever /** 8249903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 8259903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 8269903cd1cSChuck Lever * @xid: RPC XID of incoming reply 8279903cd1cSChuck Lever * 8281da177e4SLinus Torvalds */ 829d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 8301da177e4SLinus Torvalds { 8318f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 8321da177e4SLinus Torvalds 8338f3a6de3SPavel Emelyanov list_for_each_entry(entry, &xprt->recv, rq_list) 8343705ad64SJeff Layton if (entry->rq_xid == xid) { 8353705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 836262ca07dSChuck Lever return entry; 8373705ad64SJeff Layton } 83846121cf7SChuck Lever 83946121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 84046121cf7SChuck Lever ntohl(xid)); 8413705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 842262ca07dSChuck Lever xprt->stat.bad_xids++; 843262ca07dSChuck Lever return NULL; 8441da177e4SLinus Torvalds } 84512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 8461da177e4SLinus Torvalds 847729749bbSTrond Myklebust /** 848729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 849729749bbSTrond Myklebust * @req: Request to pin 850729749bbSTrond Myklebust * 851729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 852729749bbSTrond Myklebust * so should be holding the xprt transport lock. 853729749bbSTrond Myklebust */ 854729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 855729749bbSTrond Myklebust { 856729749bbSTrond Myklebust set_bit(RPC_TASK_MSG_RECV, &req->rq_task->tk_runstate); 857729749bbSTrond Myklebust } 8589590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 859729749bbSTrond Myklebust 860729749bbSTrond Myklebust /** 861729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 862729749bbSTrond Myklebust * @req: Request to pin 863729749bbSTrond Myklebust * 864729749bbSTrond Myklebust * Caller should be holding the xprt transport lock. 865729749bbSTrond Myklebust */ 866729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 867729749bbSTrond Myklebust { 868729749bbSTrond Myklebust struct rpc_task *task = req->rq_task; 869729749bbSTrond Myklebust 870729749bbSTrond Myklebust clear_bit(RPC_TASK_MSG_RECV, &task->tk_runstate); 871729749bbSTrond Myklebust if (test_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate)) 872729749bbSTrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_MSG_RECV); 873729749bbSTrond Myklebust } 8749590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 875729749bbSTrond Myklebust 876729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 877ce7c252aSTrond Myklebust __must_hold(&req->rq_xprt->recv_lock) 878729749bbSTrond Myklebust { 879729749bbSTrond Myklebust struct rpc_task *task = req->rq_task; 880729749bbSTrond Myklebust 881729749bbSTrond Myklebust if (task && test_bit(RPC_TASK_MSG_RECV, &task->tk_runstate)) { 882ce7c252aSTrond Myklebust spin_unlock(&req->rq_xprt->recv_lock); 883729749bbSTrond Myklebust set_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate); 884729749bbSTrond Myklebust wait_on_bit(&task->tk_runstate, RPC_TASK_MSG_RECV, 885729749bbSTrond Myklebust TASK_UNINTERRUPTIBLE); 886729749bbSTrond Myklebust clear_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate); 887ce7c252aSTrond Myklebust spin_lock(&req->rq_xprt->recv_lock); 888729749bbSTrond Myklebust } 889729749bbSTrond Myklebust } 890729749bbSTrond Myklebust 891bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task) 8921da177e4SLinus Torvalds { 8931570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 8941570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 89595c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 896d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 8971570c1e4SChuck Lever 8981da177e4SLinus Torvalds if (timer) { 8991da177e4SLinus Torvalds if (req->rq_ntrans == 1) 900ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 9011570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9051570c1e4SChuck Lever /** 9061570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 9071570c1e4SChuck Lever * @task: RPC request that recently completed 9081570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 9091570c1e4SChuck Lever * 9101570c1e4SChuck Lever * Caller holds transport lock. 9111570c1e4SChuck Lever */ 9121570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 9131570c1e4SChuck Lever { 9141570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 915fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 9161da177e4SLinus Torvalds 9171570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 9181570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 9193705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 9201da177e4SLinus Torvalds 921fda13939STrond Myklebust xprt->stat.recvs++; 922d60dbb20STrond Myklebust req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); 923bbc72ceaSChuck Lever if (xprt->ops->timer != NULL) 924bbc72ceaSChuck Lever xprt_update_rtt(task); 925ef759a2eSChuck Lever 9261da177e4SLinus Torvalds list_del_init(&req->rq_list); 9271e799b67STrond Myklebust req->rq_private_buf.len = copied; 928dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 929dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 93043ac3f29STrond Myklebust smp_wmb(); 931dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 932fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 9331da177e4SLinus Torvalds } 93412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 9351da177e4SLinus Torvalds 93646c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 9371da177e4SLinus Torvalds { 9381da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 9391da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 9401da177e4SLinus Torvalds 9415d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 9425d00837bSTrond Myklebust return; 94346121cf7SChuck Lever dprintk("RPC: %5u xprt_timer\n", task->tk_pid); 94446c0ee8bSChuck Lever 945dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 94646c0ee8bSChuck Lever if (xprt->ops->timer) 9476a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 9485d00837bSTrond Myklebust } else 9495d00837bSTrond Myklebust task->tk_status = 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9529903cd1cSChuck Lever /** 9539903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 9549903cd1cSChuck Lever * @task: RPC task about to send a request 9559903cd1cSChuck Lever * 9561da177e4SLinus Torvalds */ 95790051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 9581da177e4SLinus Torvalds { 9591da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 9601da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 96190051ea7STrond Myklebust bool ret = false; 9621da177e4SLinus Torvalds 96346121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 9641da177e4SLinus Torvalds 9654a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 9668a19a0b6STrond Myklebust if (!req->rq_bytes_sent) { 9678a19a0b6STrond Myklebust if (req->rq_reply_bytes_recvd) { 96890051ea7STrond Myklebust task->tk_status = req->rq_reply_bytes_recvd; 9691da177e4SLinus Torvalds goto out_unlock; 9701da177e4SLinus Torvalds } 9718a19a0b6STrond Myklebust if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) 9728a19a0b6STrond Myklebust && xprt_connected(xprt) 9738a19a0b6STrond Myklebust && req->rq_connect_cookie == xprt->connect_cookie) { 9748a19a0b6STrond Myklebust xprt->ops->set_retrans_timeout(task); 9758a19a0b6STrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 9768a19a0b6STrond Myklebust goto out_unlock; 9778a19a0b6STrond Myklebust } 9788a19a0b6STrond Myklebust } 97990051ea7STrond Myklebust if (!xprt->ops->reserve_xprt(xprt, task)) { 98090051ea7STrond Myklebust task->tk_status = -EAGAIN; 98190051ea7STrond Myklebust goto out_unlock; 98290051ea7STrond Myklebust } 98390051ea7STrond Myklebust ret = true; 9841da177e4SLinus Torvalds out_unlock: 9854a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 98690051ea7STrond Myklebust return ret; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 989e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 9905e5ce5beSTrond Myklebust { 991343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 9925e5ce5beSTrond Myklebust } 9935e5ce5beSTrond Myklebust 9949903cd1cSChuck Lever /** 9959903cd1cSChuck Lever * xprt_transmit - send an RPC request on a transport 9969903cd1cSChuck Lever * @task: controlling RPC task 9979903cd1cSChuck Lever * 9989903cd1cSChuck Lever * We have to copy the iovec because sendmsg fiddles with its contents. 9999903cd1cSChuck Lever */ 10009903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task) 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 10031da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 100415a45206SAndy Adamson int status, numreqs; 10051da177e4SLinus Torvalds 100646121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 10071da177e4SLinus Torvalds 1008dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 100955ae1aabSRicardo Labiaga if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { 101055ae1aabSRicardo Labiaga /* 101155ae1aabSRicardo Labiaga * Add to the list only if we're expecting a reply 101255ae1aabSRicardo Labiaga */ 10131da177e4SLinus Torvalds /* Update the softirq receive buffer */ 10141da177e4SLinus Torvalds memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 10151da177e4SLinus Torvalds sizeof(req->rq_private_buf)); 10161da177e4SLinus Torvalds /* Add request to the receive list */ 1017ce7c252aSTrond Myklebust spin_lock(&xprt->recv_lock); 10181da177e4SLinus Torvalds list_add_tail(&req->rq_list, &xprt->recv); 1019ce7c252aSTrond Myklebust spin_unlock(&xprt->recv_lock); 10201da177e4SLinus Torvalds xprt_reset_majortimeo(req); 10210f9dc2b1STrond Myklebust /* Turn off autodisconnect */ 10220f9dc2b1STrond Myklebust del_singleshot_timer_sync(&xprt->timer); 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds } else if (!req->rq_bytes_sent) 10251da177e4SLinus Torvalds return; 10261da177e4SLinus Torvalds 1027ff839970SChuck Lever req->rq_xtime = ktime_get(); 1028a246b010SChuck Lever status = xprt->ops->send_request(task); 10293705ad64SJeff Layton trace_xprt_transmit(xprt, req->rq_xid, status); 1030c8485e4dSTrond Myklebust if (status != 0) { 1031c8485e4dSTrond Myklebust task->tk_status = status; 1032c8485e4dSTrond Myklebust return; 1033c8485e4dSTrond Myklebust } 10344a068258SChuck Lever xprt_inject_disconnect(xprt); 1035c8485e4dSTrond Myklebust 103646121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 1037468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1038fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 1039262ca07dSChuck Lever 1040fe3aca29SChuck Lever xprt->ops->set_retrans_timeout(task); 1041262ca07dSChuck Lever 104215a45206SAndy Adamson numreqs = atomic_read(&xprt->num_reqs); 104315a45206SAndy Adamson if (numreqs > xprt->stat.max_slots) 104415a45206SAndy Adamson xprt->stat.max_slots = numreqs; 1045262ca07dSChuck Lever xprt->stat.sends++; 1046262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1047262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 104815a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 104915a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1050262ca07dSChuck Lever 1051fe3aca29SChuck Lever /* Don't race with disconnect */ 1052fe3aca29SChuck Lever if (!xprt_connected(xprt)) 1053fe3aca29SChuck Lever task->tk_status = -ENOTCONN; 10540a660521STrond Myklebust else { 105555ae1aabSRicardo Labiaga /* 105655ae1aabSRicardo Labiaga * Sleep on the pending queue since 105755ae1aabSRicardo Labiaga * we're expecting a reply. 105855ae1aabSRicardo Labiaga */ 10590a660521STrond Myklebust if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) 10605d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 10610a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie; 106255ae1aabSRicardo Labiaga } 1063fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 1066ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1067ba60eb25STrond Myklebust { 1068ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1069ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1070ba60eb25STrond Myklebust } 1071ba60eb25STrond Myklebust 1072ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1073ba60eb25STrond Myklebust { 1074ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1075ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1076ba60eb25STrond Myklebust } 1077ba60eb25STrond Myklebust 1078ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1079ba60eb25STrond Myklebust { 1080ba60eb25STrond Myklebust bool ret = false; 1081ba60eb25STrond Myklebust 1082ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1083ba60eb25STrond Myklebust goto out; 1084ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1085ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1086ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1087ba60eb25STrond Myklebust ret = true; 1088ba60eb25STrond Myklebust } 1089ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1090ba60eb25STrond Myklebust out: 1091ba60eb25STrond Myklebust return ret; 1092ba60eb25STrond Myklebust } 1093ba60eb25STrond Myklebust 109492ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1095d9ba131dSTrond Myklebust { 1096d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1097d9ba131dSTrond Myklebust 1098d9ba131dSTrond Myklebust if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs)) 1099d9ba131dSTrond Myklebust goto out; 110092ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 110192ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 110292ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1103d9ba131dSTrond Myklebust if (req != NULL) 1104d9ba131dSTrond Myklebust goto out; 1105d9ba131dSTrond Myklebust atomic_dec(&xprt->num_reqs); 1106d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1107d9ba131dSTrond Myklebust out: 1108d9ba131dSTrond Myklebust return req; 1109d9ba131dSTrond Myklebust } 1110d9ba131dSTrond Myklebust 1111d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1112d9ba131dSTrond Myklebust { 1113d9ba131dSTrond Myklebust if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) { 1114d9ba131dSTrond Myklebust kfree(req); 1115d9ba131dSTrond Myklebust return true; 1116d9ba131dSTrond Myklebust } 1117d9ba131dSTrond Myklebust return false; 1118d9ba131dSTrond Myklebust } 1119d9ba131dSTrond Myklebust 1120f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 11211da177e4SLinus Torvalds { 1122d9ba131dSTrond Myklebust struct rpc_rqst *req; 11231da177e4SLinus Torvalds 1124f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 11251da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1126d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1127d9ba131dSTrond Myklebust list_del(&req->rq_list); 1128d9ba131dSTrond Myklebust goto out_init_req; 1129d9ba131dSTrond Myklebust } 113092ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1131d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1132d9ba131dSTrond Myklebust goto out_init_req; 1133d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1134d9ba131dSTrond Myklebust case -ENOMEM: 1135d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1136d9ba131dSTrond Myklebust "failed! Retrying\n"); 11371afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1138d9ba131dSTrond Myklebust break; 1139d9ba131dSTrond Myklebust case -EAGAIN: 1140ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1141d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 11421afeaf5cSTrond Myklebust default: 1143d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 11441afeaf5cSTrond Myklebust } 1145f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1146d9ba131dSTrond Myklebust return; 1147d9ba131dSTrond Myklebust out_init_req: 1148d9ba131dSTrond Myklebust task->tk_status = 0; 11491da177e4SLinus Torvalds task->tk_rqstp = req; 11501da177e4SLinus Torvalds xprt_request_init(task, xprt); 1151f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 11521da177e4SLinus Torvalds } 1153f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1154f39c1bfbSTrond Myklebust 1155f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 1156f39c1bfbSTrond Myklebust { 1157f39c1bfbSTrond Myklebust /* Note: grabbing the xprt_lock_write() ensures that we throttle 1158f39c1bfbSTrond Myklebust * new slot allocation if the transport is congested (i.e. when 1159f39c1bfbSTrond Myklebust * reconnecting a stream transport or when out of socket write 1160f39c1bfbSTrond Myklebust * buffer space). 1161f39c1bfbSTrond Myklebust */ 1162f39c1bfbSTrond Myklebust if (xprt_lock_write(xprt, task)) { 1163f39c1bfbSTrond Myklebust xprt_alloc_slot(xprt, task); 1164f39c1bfbSTrond Myklebust xprt_release_write(xprt, task); 1165f39c1bfbSTrond Myklebust } 1166f39c1bfbSTrond Myklebust } 1167f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot); 11681da177e4SLinus Torvalds 1169ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1170ee5ebe85STrond Myklebust { 1171ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1172c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1173c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1174ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1175c25573b5STrond Myklebust } 1176ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1177ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1178ee5ebe85STrond Myklebust } 1179ee5ebe85STrond Myklebust 118021de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 118121de0a95STrond Myklebust { 118221de0a95STrond Myklebust struct rpc_rqst *req; 118321de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 118421de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 118521de0a95STrond Myklebust list_del(&req->rq_list); 118621de0a95STrond Myklebust kfree(req); 118721de0a95STrond Myklebust } 118821de0a95STrond Myklebust } 118921de0a95STrond Myklebust 1190d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1191d9ba131dSTrond Myklebust unsigned int num_prealloc, 1192d9ba131dSTrond Myklebust unsigned int max_alloc) 1193bd1722d4SPavel Emelyanov { 1194bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 119521de0a95STrond Myklebust struct rpc_rqst *req; 119621de0a95STrond Myklebust int i; 1197bd1722d4SPavel Emelyanov 1198bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1199bd1722d4SPavel Emelyanov if (xprt == NULL) 1200bd1722d4SPavel Emelyanov goto out; 1201bd1722d4SPavel Emelyanov 120221de0a95STrond Myklebust xprt_init(xprt, net); 120321de0a95STrond Myklebust 120421de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 120521de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 120621de0a95STrond Myklebust if (!req) 12078313164cSwangweidong goto out_free; 120821de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 120921de0a95STrond Myklebust } 1210d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1211d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1212d9ba131dSTrond Myklebust else 121321de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1214d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1215d9ba131dSTrond Myklebust atomic_set(&xprt->num_reqs, num_prealloc); 1216bd1722d4SPavel Emelyanov 1217bd1722d4SPavel Emelyanov return xprt; 1218bd1722d4SPavel Emelyanov 1219bd1722d4SPavel Emelyanov out_free: 122021de0a95STrond Myklebust xprt_free(xprt); 1221bd1722d4SPavel Emelyanov out: 1222bd1722d4SPavel Emelyanov return NULL; 1223bd1722d4SPavel Emelyanov } 1224bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1225bd1722d4SPavel Emelyanov 1226e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1227e204e621SPavel Emelyanov { 122837aa2133SPavel Emelyanov put_net(xprt->xprt_net); 122921de0a95STrond Myklebust xprt_free_all_slots(xprt); 1230fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1231e204e621SPavel Emelyanov } 1232e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1233e204e621SPavel Emelyanov 12349903cd1cSChuck Lever /** 12359903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 12369903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 12379903cd1cSChuck Lever * 1238ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1239ba60eb25STrond Myklebust * slots are available, place the task on the transport's 12409903cd1cSChuck Lever * backlog queue. 12419903cd1cSChuck Lever */ 12429903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 12431da177e4SLinus Torvalds { 1244fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 12451da177e4SLinus Torvalds 124643cedbf0STrond Myklebust task->tk_status = 0; 124743cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 124843cedbf0STrond Myklebust return; 124943cedbf0STrond Myklebust 125043cedbf0STrond Myklebust task->tk_timeout = 0; 125143cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1252ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 1253ba60eb25STrond Myklebust xprt->ops->alloc_slot(xprt, task); 1254ba60eb25STrond Myklebust } 1255ba60eb25STrond Myklebust 1256ba60eb25STrond Myklebust /** 1257ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1258ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1259ba60eb25STrond Myklebust * 1260ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1261ba60eb25STrond Myklebust * backlog queue. 1262ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1263ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1264ba60eb25STrond Myklebust */ 1265ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1266ba60eb25STrond Myklebust { 1267fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1268ba60eb25STrond Myklebust 1269ba60eb25STrond Myklebust task->tk_status = 0; 1270ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1271ba60eb25STrond Myklebust return; 1272ba60eb25STrond Myklebust 1273ba60eb25STrond Myklebust task->tk_timeout = 0; 1274ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 1275f39c1bfbSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 1278d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) 12791da177e4SLinus Torvalds { 12800eae88f3SEric Dumazet return (__force __be32)xprt->xid++; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt) 12841da177e4SLinus Torvalds { 128563862b5bSAruna-Hewapathirane xprt->xid = prandom_u32(); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12889903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 12891da177e4SLinus Torvalds { 12901da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12911da177e4SLinus Torvalds 1292d9ba131dSTrond Myklebust INIT_LIST_HEAD(&req->rq_list); 1293ba7392bbSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 12941da177e4SLinus Torvalds req->rq_task = task; 12951da177e4SLinus Torvalds req->rq_xprt = xprt; 129602107148SChuck Lever req->rq_buffer = NULL; 12971da177e4SLinus Torvalds req->rq_xid = xprt_alloc_xid(xprt); 12980a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie - 1; 129992551948STrond Myklebust req->rq_bytes_sent = 0; 130092551948STrond Myklebust req->rq_snd_buf.len = 0; 130192551948STrond Myklebust req->rq_snd_buf.buflen = 0; 130292551948STrond Myklebust req->rq_rcv_buf.len = 0; 130392551948STrond Myklebust req->rq_rcv_buf.buflen = 0; 1304ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf = NULL; 1305da45828eSTrond Myklebust xprt_reset_majortimeo(req); 130646121cf7SChuck Lever dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 13071da177e4SLinus Torvalds req, ntohl(req->rq_xid)); 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13109903cd1cSChuck Lever /** 13119903cd1cSChuck Lever * xprt_release - release an RPC request slot 13129903cd1cSChuck Lever * @task: task which is finished with the slot 13139903cd1cSChuck Lever * 13141da177e4SLinus Torvalds */ 13159903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 13161da177e4SLinus Torvalds { 131755ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 131887ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 13191da177e4SLinus Torvalds 132087ed5003STrond Myklebust if (req == NULL) { 132187ed5003STrond Myklebust if (task->tk_client) { 1322fb43d172STrond Myklebust xprt = task->tk_xprt; 132387ed5003STrond Myklebust if (xprt->snd_task == task) 132487ed5003STrond Myklebust xprt_release_write(xprt, task); 132587ed5003STrond Myklebust } 13261da177e4SLinus Torvalds return; 132787ed5003STrond Myklebust } 132855ae1aabSRicardo Labiaga 132955ae1aabSRicardo Labiaga xprt = req->rq_xprt; 13300a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 13310a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 13320a702195SWeston Andros Adamson else if (task->tk_client) 13330a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 1334ce7c252aSTrond Myklebust spin_lock(&xprt->recv_lock); 1335ce7c252aSTrond Myklebust if (!list_empty(&req->rq_list)) { 13364c625a97STrond Myklebust list_del_init(&req->rq_list); 1337ce7c252aSTrond Myklebust xprt_wait_on_pinned_rqst(req); 1338ce7c252aSTrond Myklebust } 1339ce7c252aSTrond Myklebust spin_unlock(&xprt->recv_lock); 13404a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 134149e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1342a58dd398SChuck Lever if (xprt->ops->release_request) 1343a58dd398SChuck Lever xprt->ops->release_request(task); 13441da177e4SLinus Torvalds xprt->last_used = jiffies; 1345ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 13464a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1347ee5ebe85STrond Myklebust if (req->rq_buffer) 13483435c74aSChuck Lever xprt->ops->buf_free(task); 13494a068258SChuck Lever xprt_inject_disconnect(xprt); 1350a17c2153STrond Myklebust if (req->rq_cred != NULL) 1351a17c2153STrond Myklebust put_rpccred(req->rq_cred); 13521da177e4SLinus Torvalds task->tk_rqstp = NULL; 1353ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1354ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 135555ae1aabSRicardo Labiaga 135646121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1357ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1358ee5ebe85STrond Myklebust xprt_free_slot(xprt, req); 1359ee5ebe85STrond Myklebust else 1360c9acb42eSTrond Myklebust xprt_free_bc_request(req); 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 136321de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1364c2866763SChuck Lever { 136530c5116bSTrond Myklebust kref_init(&xprt->kref); 1366c2866763SChuck Lever 1367c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1368c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 1369ce7c252aSTrond Myklebust spin_lock_init(&xprt->recv_lock); 1370c2866763SChuck Lever 1371c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 1372c2866763SChuck Lever INIT_LIST_HEAD(&xprt->recv); 13739e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1374f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1375f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 13769e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 137780b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1378f9acac1aSRicardo Labiaga 1379c2866763SChuck Lever xprt->last_used = jiffies; 1380c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1381a509050bSChuck Lever xprt->bind_index = 0; 1382c2866763SChuck Lever 1383c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1384c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 138534006ceeSTrond Myklebust rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending"); 1386c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1387c2866763SChuck Lever 1388c2866763SChuck Lever xprt_init_xid(xprt); 1389c2866763SChuck Lever 139021de0a95STrond Myklebust xprt->xprt_net = get_net(net); 13918d9266ffSTrond Myklebust } 13928d9266ffSTrond Myklebust 13938d9266ffSTrond Myklebust /** 13948d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 13958d9266ffSTrond Myklebust * @args: rpc transport creation arguments 13968d9266ffSTrond Myklebust * 13978d9266ffSTrond Myklebust */ 13988d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 13998d9266ffSTrond Myklebust { 14008d9266ffSTrond Myklebust struct rpc_xprt *xprt; 14018d9266ffSTrond Myklebust struct xprt_class *t; 14028d9266ffSTrond Myklebust 14038d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 14048d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 14058d9266ffSTrond Myklebust if (t->ident == args->ident) { 14068d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 14078d9266ffSTrond Myklebust goto found; 14088d9266ffSTrond Myklebust } 14098d9266ffSTrond Myklebust } 14108d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 14113c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 14128d9266ffSTrond Myklebust return ERR_PTR(-EIO); 14138d9266ffSTrond Myklebust 14148d9266ffSTrond Myklebust found: 14158d9266ffSTrond Myklebust xprt = t->setup(args); 14168d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 14178d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 14188d9266ffSTrond Myklebust -PTR_ERR(xprt)); 141921de0a95STrond Myklebust goto out; 14208d9266ffSTrond Myklebust } 142133d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 142233d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 142321de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 142421de0a95STrond Myklebust if (xprt_has_timer(xprt)) 142521de0a95STrond Myklebust setup_timer(&xprt->timer, xprt_init_autodisconnect, 142621de0a95STrond Myklebust (unsigned long)xprt); 142721de0a95STrond Myklebust else 142821de0a95STrond Myklebust init_timer(&xprt->timer); 14294e0038b6STrond Myklebust 14304e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 14314e0038b6STrond Myklebust xprt_destroy(xprt); 14324e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 14334e0038b6STrond Myklebust } 14344e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 14354e0038b6STrond Myklebust if (xprt->servername == NULL) { 14364e0038b6STrond Myklebust xprt_destroy(xprt); 14374e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 14384e0038b6STrond Myklebust } 14394e0038b6STrond Myklebust 14403f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1441388f0c77SJeff Layton 1442c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1443c2866763SChuck Lever xprt->max_reqs); 144421de0a95STrond Myklebust out: 1445c2866763SChuck Lever return xprt; 1446c2866763SChuck Lever } 1447c2866763SChuck Lever 14489903cd1cSChuck Lever /** 14499903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1450a8de240aSTrond Myklebust * @xprt: transport to destroy 14519903cd1cSChuck Lever * 14521da177e4SLinus Torvalds */ 1453a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 14541da177e4SLinus Torvalds { 14551da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 145679234c3dSTrond Myklebust 145779234c3dSTrond Myklebust /* Exclude transport connect/disconnect handlers */ 145879234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 145979234c3dSTrond Myklebust 14600065db32STrond Myklebust del_timer_sync(&xprt->timer); 1461c8541ecdSChuck Lever 1462388f0c77SJeff Layton rpc_xprt_debugfs_unregister(xprt); 1463f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1464f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1465f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1466f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1467c3ae62aeSJ. Bruce Fields cancel_work_sync(&xprt->task_cleanup); 14684e0038b6STrond Myklebust kfree(xprt->servername); 1469c8541ecdSChuck Lever /* 1470c8541ecdSChuck Lever * Tear down transport state and free the rpc_xprt 1471c8541ecdSChuck Lever */ 1472a246b010SChuck Lever xprt->ops->destroy(xprt); 14736b6ca86bSTrond Myklebust } 14741da177e4SLinus Torvalds 147530c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 147630c5116bSTrond Myklebust { 147730c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 147830c5116bSTrond Myklebust } 147930c5116bSTrond Myklebust 148030c5116bSTrond Myklebust /** 148130c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 148230c5116bSTrond Myklebust * @xprt: pointer to the transport 148330c5116bSTrond Myklebust * 148430c5116bSTrond Myklebust */ 148530c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 148630c5116bSTrond Myklebust { 148730c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 148830c5116bSTrond Myklebust return xprt; 148930c5116bSTrond Myklebust return NULL; 149030c5116bSTrond Myklebust } 149130c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 149230c5116bSTrond Myklebust 14936b6ca86bSTrond Myklebust /** 14946b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 14956b6ca86bSTrond Myklebust * @xprt: pointer to the transport 14966b6ca86bSTrond Myklebust * 14976b6ca86bSTrond Myklebust */ 14986b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 14996b6ca86bSTrond Myklebust { 150030c5116bSTrond Myklebust if (xprt != NULL) 150130c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 15026b6ca86bSTrond Myklebust } 15035d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 1504