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> 511da177e4SLinus Torvalds 5255ae1aabSRicardo Labiaga #include "sunrpc.h" 5355ae1aabSRicardo Labiaga 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * Local variables 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #ifdef RPC_DEBUG 591da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 601da177e4SLinus Torvalds #endif 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* 631da177e4SLinus Torvalds * Local functions 641da177e4SLinus Torvalds */ 651da177e4SLinus Torvalds static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 661da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 671da177e4SLinus Torvalds static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 681da177e4SLinus Torvalds 695ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7081c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7181c098afS\"Talpey, Thomas\ 72555ee3afSChuck Lever /* 73555ee3afSChuck Lever * The transport code maintains an estimate on the maximum number of out- 74555ee3afSChuck Lever * standing RPC requests, using a smoothed version of the congestion 75555ee3afSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 76555ee3afSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 77555ee3afSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 78555ee3afSChuck Lever * 79555ee3afSChuck Lever * - a reply is received and 80555ee3afSChuck Lever * - a full number of requests are outstanding and 81555ee3afSChuck Lever * - the congestion window hasn't been updated recently. 82555ee3afSChuck Lever */ 83555ee3afSChuck Lever #define RPC_CWNDSHIFT (8U) 84555ee3afSChuck Lever #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT) 85555ee3afSChuck Lever #define RPC_INITCWND RPC_CWNDSCALE 86555ee3afSChuck Lever #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT) 87555ee3afSChuck Lever 88555ee3afSChuck Lever #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd) 891da177e4SLinus Torvalds 9012a80469SChuck Lever /** 9181c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 9281c098afS\"Talpey, Thomas\ * @transport: transport to register 9381c098afS\"Talpey, Thomas\ * 9481c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9581c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9681c098afS\"Talpey, Thomas\ * 9781c098afS\"Talpey, Thomas\ * Returns: 9881c098afS\"Talpey, Thomas\ * 0: transport successfully registered 9981c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 10081c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 10181c098afS\"Talpey, Thomas\ */ 10281c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 10381c098afS\"Talpey, Thomas\ { 10481c098afS\"Talpey, Thomas\ struct xprt_class *t; 10581c098afS\"Talpey, Thomas\ int result; 10681c098afS\"Talpey, Thomas\ 10781c098afS\"Talpey, Thomas\ result = -EEXIST; 10881c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 10981c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 11081c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1114fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 11281c098afS\"Talpey, Thomas\ goto out; 11381c098afS\"Talpey, Thomas\ } 11481c098afS\"Talpey, Thomas\ 11581c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11681c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11781c098afS\"Talpey, Thomas\ transport->name); 11881c098afS\"Talpey, Thomas\ result = 0; 11981c098afS\"Talpey, Thomas\ 12081c098afS\"Talpey, Thomas\ out: 12181c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 12281c098afS\"Talpey, Thomas\ return result; 12381c098afS\"Talpey, Thomas\ } 12481c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12581c098afS\"Talpey, Thomas\ 12681c098afS\"Talpey, Thomas\ /** 12781c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 12865b6e42cSRandy Dunlap * @transport: transport to unregister 12981c098afS\"Talpey, Thomas\ * 13081c098afS\"Talpey, Thomas\ * Returns: 13181c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 13281c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 13381c098afS\"Talpey, Thomas\ */ 13481c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13581c098afS\"Talpey, Thomas\ { 13681c098afS\"Talpey, Thomas\ struct xprt_class *t; 13781c098afS\"Talpey, Thomas\ int result; 13881c098afS\"Talpey, Thomas\ 13981c098afS\"Talpey, Thomas\ result = 0; 14081c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 14181c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 14281c098afS\"Talpey, Thomas\ if (t == transport) { 14381c098afS\"Talpey, Thomas\ printk(KERN_INFO 14481c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14581c098afS\"Talpey, Thomas\ transport->name); 14681c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14781c098afS\"Talpey, Thomas\ goto out; 14881c098afS\"Talpey, Thomas\ } 14981c098afS\"Talpey, Thomas\ } 15081c098afS\"Talpey, Thomas\ result = -ENOENT; 15181c098afS\"Talpey, Thomas\ 15281c098afS\"Talpey, Thomas\ out: 15381c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15481c098afS\"Talpey, Thomas\ return result; 15581c098afS\"Talpey, Thomas\ } 15681c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15781c098afS\"Talpey, Thomas\ 15881c098afS\"Talpey, Thomas\ /** 159441e3e24STom Talpey * xprt_load_transport - load a transport implementation 160441e3e24STom Talpey * @transport_name: transport to load 161441e3e24STom Talpey * 162441e3e24STom Talpey * Returns: 163441e3e24STom Talpey * 0: transport successfully loaded 164441e3e24STom Talpey * -ENOENT: transport module not available 165441e3e24STom Talpey */ 166441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 167441e3e24STom Talpey { 168441e3e24STom Talpey struct xprt_class *t; 169441e3e24STom Talpey int result; 170441e3e24STom Talpey 171441e3e24STom Talpey result = 0; 172441e3e24STom Talpey spin_lock(&xprt_list_lock); 173441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 174441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 175441e3e24STom Talpey spin_unlock(&xprt_list_lock); 176441e3e24STom Talpey goto out; 177441e3e24STom Talpey } 178441e3e24STom Talpey } 179441e3e24STom Talpey spin_unlock(&xprt_list_lock); 180ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 181441e3e24STom Talpey out: 182441e3e24STom Talpey return result; 183441e3e24STom Talpey } 184441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 185441e3e24STom Talpey 186441e3e24STom Talpey /** 18712a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 18812a80469SChuck Lever * @task: task that is requesting access to the transport 18912a80469SChuck Lever * 19012a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 19112a80469SChuck Lever * transport connects from colliding with writes. No congestion control 19212a80469SChuck Lever * is provided. 1931da177e4SLinus Torvalds */ 19443cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1951da177e4SLinus Torvalds { 19612a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 19712a80469SChuck Lever 19812a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 19912a80469SChuck Lever if (task == xprt->snd_task) 20012a80469SChuck Lever return 1; 20112a80469SChuck Lever goto out_sleep; 20212a80469SChuck Lever } 20312a80469SChuck Lever xprt->snd_task = task; 20443cedbf0STrond Myklebust if (req != NULL) { 20512a80469SChuck Lever req->rq_bytes_sent = 0; 20612a80469SChuck Lever req->rq_ntrans++; 20743cedbf0STrond Myklebust } 2084d4a76f3Sj223yang@asset.uwaterloo.ca 20912a80469SChuck Lever return 1; 21012a80469SChuck Lever 21112a80469SChuck Lever out_sleep: 21246121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 21312a80469SChuck Lever task->tk_pid, xprt); 21412a80469SChuck Lever task->tk_timeout = 0; 21512a80469SChuck Lever task->tk_status = -EAGAIN; 21643cedbf0STrond Myklebust if (req != NULL && req->rq_ntrans) 2175d00837bSTrond Myklebust rpc_sleep_on(&xprt->resend, task, NULL); 21812a80469SChuck Lever else 2195d00837bSTrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 22012a80469SChuck Lever return 0; 22112a80469SChuck Lever } 22212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 22312a80469SChuck Lever 224632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 225632e3bdcSTrond Myklebust { 226632e3bdcSTrond Myklebust xprt->snd_task = NULL; 227632e3bdcSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) { 228632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 229632e3bdcSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 230632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 231632e3bdcSTrond Myklebust } else 232c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 233632e3bdcSTrond Myklebust } 234632e3bdcSTrond Myklebust 23512a80469SChuck Lever /* 23612a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 23712a80469SChuck Lever * @task: task that is requesting access to the transport 23812a80469SChuck Lever * 23912a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 24012a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 24112a80469SChuck Lever * woken up and given access to the transport. 24212a80469SChuck Lever */ 24343cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 24412a80469SChuck Lever { 2451da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 2461da177e4SLinus Torvalds 2472226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2481da177e4SLinus Torvalds if (task == xprt->snd_task) 2491da177e4SLinus Torvalds return 1; 2501da177e4SLinus Torvalds goto out_sleep; 2511da177e4SLinus Torvalds } 25243cedbf0STrond Myklebust if (req == NULL) { 25343cedbf0STrond Myklebust xprt->snd_task = task; 25443cedbf0STrond Myklebust return 1; 25543cedbf0STrond Myklebust } 25612a80469SChuck Lever if (__xprt_get_cong(xprt, task)) { 2571da177e4SLinus Torvalds xprt->snd_task = task; 2581da177e4SLinus Torvalds req->rq_bytes_sent = 0; 2591da177e4SLinus Torvalds req->rq_ntrans++; 2601da177e4SLinus Torvalds return 1; 2611da177e4SLinus Torvalds } 262632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2631da177e4SLinus Torvalds out_sleep: 26446121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2651da177e4SLinus Torvalds task->tk_timeout = 0; 2661da177e4SLinus Torvalds task->tk_status = -EAGAIN; 26743cedbf0STrond Myklebust if (req != NULL && req->rq_ntrans) 2685d00837bSTrond Myklebust rpc_sleep_on(&xprt->resend, task, NULL); 2691da177e4SLinus Torvalds else 2705d00837bSTrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 2711da177e4SLinus Torvalds return 0; 2721da177e4SLinus Torvalds } 27312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2741da177e4SLinus Torvalds 27512a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2761da177e4SLinus Torvalds { 2771da177e4SLinus Torvalds int retval; 2781da177e4SLinus Torvalds 2794a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 28043cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2814a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2821da177e4SLinus Torvalds return retval; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 28512a80469SChuck Lever static void __xprt_lock_write_next(struct rpc_xprt *xprt) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct rpc_task *task; 28849e9a890SChuck Lever struct rpc_rqst *req; 28949e9a890SChuck Lever 29049e9a890SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 29149e9a890SChuck Lever return; 29249e9a890SChuck Lever 29349e9a890SChuck Lever task = rpc_wake_up_next(&xprt->resend); 29449e9a890SChuck Lever if (!task) { 29549e9a890SChuck Lever task = rpc_wake_up_next(&xprt->sending); 29643cedbf0STrond Myklebust if (task == NULL) 29749e9a890SChuck Lever goto out_unlock; 29849e9a890SChuck Lever } 29949e9a890SChuck Lever 30049e9a890SChuck Lever req = task->tk_rqstp; 30149e9a890SChuck Lever xprt->snd_task = task; 30249e9a890SChuck Lever if (req) { 30349e9a890SChuck Lever req->rq_bytes_sent = 0; 30449e9a890SChuck Lever req->rq_ntrans++; 30549e9a890SChuck Lever } 30649e9a890SChuck Lever return; 30749e9a890SChuck Lever 30849e9a890SChuck Lever out_unlock: 309632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 31049e9a890SChuck Lever } 31149e9a890SChuck Lever 31249e9a890SChuck Lever static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 31349e9a890SChuck Lever { 31449e9a890SChuck Lever struct rpc_task *task; 31543cedbf0STrond Myklebust struct rpc_rqst *req; 3161da177e4SLinus Torvalds 3172226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 3181da177e4SLinus Torvalds return; 31949e9a890SChuck Lever if (RPCXPRT_CONGESTED(xprt)) 3201da177e4SLinus Torvalds goto out_unlock; 3211da177e4SLinus Torvalds task = rpc_wake_up_next(&xprt->resend); 3221da177e4SLinus Torvalds if (!task) { 3231da177e4SLinus Torvalds task = rpc_wake_up_next(&xprt->sending); 32443cedbf0STrond Myklebust if (task == NULL) 3251da177e4SLinus Torvalds goto out_unlock; 3261da177e4SLinus Torvalds } 32743cedbf0STrond Myklebust 32843cedbf0STrond Myklebust req = task->tk_rqstp; 32943cedbf0STrond Myklebust if (req == NULL) { 3301da177e4SLinus Torvalds xprt->snd_task = task; 33143cedbf0STrond Myklebust return; 33243cedbf0STrond Myklebust } 33343cedbf0STrond Myklebust if (__xprt_get_cong(xprt, task)) { 33443cedbf0STrond Myklebust xprt->snd_task = task; 3351da177e4SLinus Torvalds req->rq_bytes_sent = 0; 3361da177e4SLinus Torvalds req->rq_ntrans++; 3371da177e4SLinus Torvalds return; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds out_unlock: 340632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 34349e9a890SChuck Lever /** 34449e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 34549e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 34649e9a890SChuck Lever * @task: task that is releasing access to the transport 34749e9a890SChuck Lever * 34849e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3491da177e4SLinus Torvalds */ 35049e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds if (xprt->snd_task == task) { 353632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3541da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds } 35712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3581da177e4SLinus Torvalds 35949e9a890SChuck Lever /** 36049e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 36149e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 36249e9a890SChuck Lever * @task: task that is releasing access to the transport 36349e9a890SChuck Lever * 36449e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 36549e9a890SChuck Lever * transport if the transport's congestion window allows it. 36649e9a890SChuck Lever */ 36749e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 36849e9a890SChuck Lever { 36949e9a890SChuck Lever if (xprt->snd_task == task) { 370632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 37149e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 37249e9a890SChuck Lever } 37349e9a890SChuck Lever } 37412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 37549e9a890SChuck Lever 37649e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3771da177e4SLinus Torvalds { 3784a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 37949e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3804a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* 3841da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3851da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds static int 3881da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds if (req->rq_cong) 3931da177e4SLinus Torvalds return 1; 39446121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 3951da177e4SLinus Torvalds task->tk_pid, xprt->cong, xprt->cwnd); 3961da177e4SLinus Torvalds if (RPCXPRT_CONGESTED(xprt)) 3971da177e4SLinus Torvalds return 0; 3981da177e4SLinus Torvalds req->rq_cong = 1; 3991da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4001da177e4SLinus Torvalds return 1; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* 4041da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4051da177e4SLinus Torvalds * that has been sleeping due to congestion 4061da177e4SLinus Torvalds */ 4071da177e4SLinus Torvalds static void 4081da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds if (!req->rq_cong) 4111da177e4SLinus Torvalds return; 4121da177e4SLinus Torvalds req->rq_cong = 0; 4131da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 41449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 41746c0ee8bSChuck Lever /** 418a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 419a58dd398SChuck Lever * @task: RPC request that recently completed 420a58dd398SChuck Lever * 421a58dd398SChuck Lever * Useful for transports that require congestion control. 422a58dd398SChuck Lever */ 423a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 424a58dd398SChuck Lever { 425a58dd398SChuck Lever __xprt_put_cong(task->tk_xprt, task->tk_rqstp); 426a58dd398SChuck Lever } 42712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 428a58dd398SChuck Lever 429a58dd398SChuck Lever /** 43046c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 43146c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 43246c0ee8bSChuck Lever * @result: result code of completed RPC request 43346c0ee8bSChuck Lever * 4341da177e4SLinus Torvalds * We use a time-smoothed congestion estimator to avoid heavy oscillation. 4351da177e4SLinus Torvalds */ 43646c0ee8bSChuck Lever void xprt_adjust_cwnd(struct rpc_task *task, int result) 4371da177e4SLinus Torvalds { 43846c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 43946c0ee8bSChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 44046c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4431da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4441da177e4SLinus Torvalds * the result gets rounded properly. */ 4451da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4461da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4471da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 44849e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4491da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4501da177e4SLinus Torvalds cwnd >>= 1; 4511da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4521da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4551da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4561da177e4SLinus Torvalds xprt->cwnd = cwnd; 45746c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4581da177e4SLinus Torvalds } 45912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4601da177e4SLinus Torvalds 46144fbac22SChuck Lever /** 46244fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 46344fbac22SChuck Lever * @xprt: transport with waiting tasks 46444fbac22SChuck Lever * @status: result code to plant in each task before waking it 46544fbac22SChuck Lever * 46644fbac22SChuck Lever */ 46744fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 46844fbac22SChuck Lever { 46944fbac22SChuck Lever if (status < 0) 47044fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 47144fbac22SChuck Lever else 47244fbac22SChuck Lever rpc_wake_up(&xprt->pending); 47344fbac22SChuck Lever } 47412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 47544fbac22SChuck Lever 476c7b2cae8SChuck Lever /** 477c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 478c7b2cae8SChuck Lever * @task: task to be put to sleep 4790b80ae42SRandy Dunlap * @action: function pointer to be executed after wait 480c7b2cae8SChuck Lever */ 481b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) 482c7b2cae8SChuck Lever { 483c7b2cae8SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 484c7b2cae8SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 485c7b2cae8SChuck Lever 486c7b2cae8SChuck Lever task->tk_timeout = req->rq_timeout; 487b6ddf64fSTrond Myklebust rpc_sleep_on(&xprt->pending, task, action); 488c7b2cae8SChuck Lever } 48912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 490c7b2cae8SChuck Lever 491c7b2cae8SChuck Lever /** 492c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 493c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 494c7b2cae8SChuck Lever * 495c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 496c7b2cae8SChuck Lever */ 497c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt) 498c7b2cae8SChuck Lever { 499c7b2cae8SChuck Lever if (unlikely(xprt->shutdown)) 500c7b2cae8SChuck Lever return; 501c7b2cae8SChuck Lever 502c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 503c7b2cae8SChuck Lever if (xprt->snd_task) { 50446121cf7SChuck Lever dprintk("RPC: write space: waking waiting task on " 50546121cf7SChuck Lever "xprt %p\n", xprt); 506fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); 507c7b2cae8SChuck Lever } 508c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 509c7b2cae8SChuck Lever } 51012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 511c7b2cae8SChuck Lever 512fe3aca29SChuck Lever /** 513fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 514fe3aca29SChuck Lever * @task: task whose timeout is to be set 515fe3aca29SChuck Lever * 516fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 517fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 518fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 519fe3aca29SChuck Lever */ 520fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 521fe3aca29SChuck Lever { 522fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 523fe3aca29SChuck Lever } 52412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 525fe3aca29SChuck Lever 526fe3aca29SChuck Lever /* 527fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - 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 using the RTT estimator. 531fe3aca29SChuck Lever */ 532fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 533fe3aca29SChuck Lever { 534fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 535ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 536ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 537fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 538ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 539fe3aca29SChuck Lever 540fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 541fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 542fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 543fe3aca29SChuck Lever task->tk_timeout = max_timeout; 544fe3aca29SChuck Lever } 54512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 546fe3aca29SChuck Lever 5471da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5481da177e4SLinus Torvalds { 549ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5521da177e4SLinus Torvalds if (to->to_exponential) 5531da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5541da177e4SLinus Torvalds else 5551da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5561da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5571da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5581da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5619903cd1cSChuck Lever /** 5629903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5639903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5649903cd1cSChuck Lever * 5651da177e4SLinus Torvalds */ 5661da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 569ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5701da177e4SLinus Torvalds int status = 0; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5731da177e4SLinus Torvalds if (to->to_exponential) 5741da177e4SLinus Torvalds req->rq_timeout <<= 1; 5751da177e4SLinus Torvalds else 5761da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5771da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5781da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5791da177e4SLinus Torvalds req->rq_retries++; 5801da177e4SLinus Torvalds } else { 5811da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5821da177e4SLinus Torvalds req->rq_retries = 0; 5831da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5841da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 5854a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 5861da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 5874a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5881da177e4SLinus Torvalds status = -ETIMEDOUT; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds if (req->rq_timeout == 0) { 5921da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 5931da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds return status; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 59865f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 5991da177e4SLinus Torvalds { 60065f27f38SDavid Howells struct rpc_xprt *xprt = 60165f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6021da177e4SLinus Torvalds 603a246b010SChuck Lever xprt->ops->close(xprt); 60466af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6051da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6089903cd1cSChuck Lever /** 60962da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6109903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6119903cd1cSChuck Lever * 6121da177e4SLinus Torvalds */ 61362da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6164a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6171da177e4SLinus Torvalds xprt_clear_connected(xprt); 6182a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6194a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6201da177e4SLinus Torvalds } 62162da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6221da177e4SLinus Torvalds 62366af1e55STrond Myklebust /** 62466af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 62566af1e55STrond Myklebust * @xprt: transport to disconnect 62666af1e55STrond Myklebust * 62766af1e55STrond Myklebust */ 62866af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 62966af1e55STrond Myklebust { 63066af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 63166af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 63266af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 63366af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 63466af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 63566af1e55STrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6362a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 63766af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 63866af1e55STrond Myklebust } 63966af1e55STrond Myklebust 6407c1d71cfSTrond Myklebust /** 6417c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6427c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6437c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6447c1d71cfSTrond Myklebust * 6457c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6467c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6477c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6487c1d71cfSTrond Myklebust * a batch of RPC requests. 6497c1d71cfSTrond Myklebust * 6507c1d71cfSTrond Myklebust */ 6517c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6527c1d71cfSTrond Myklebust { 6537c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6547c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6557c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6567c1d71cfSTrond Myklebust goto out; 6577c1d71cfSTrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) 6587c1d71cfSTrond Myklebust goto out; 6597c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6607c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6617c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 6627c1d71cfSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6632a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6647c1d71cfSTrond Myklebust out: 6657c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6667c1d71cfSTrond Myklebust } 6677c1d71cfSTrond Myklebust 6681da177e4SLinus Torvalds static void 6691da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data) 6701da177e4SLinus Torvalds { 6711da177e4SLinus Torvalds struct rpc_xprt *xprt = (struct rpc_xprt *)data; 6721da177e4SLinus Torvalds 6734a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 6741da177e4SLinus Torvalds if (!list_empty(&xprt->recv) || xprt->shutdown) 6751da177e4SLinus Torvalds goto out_abort; 6762226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 6771da177e4SLinus Torvalds goto out_abort; 6784a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 679f75e6745STrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 680c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6811da177e4SLinus Torvalds return; 6821da177e4SLinus Torvalds out_abort: 6834a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6869903cd1cSChuck Lever /** 6879903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 6889903cd1cSChuck Lever * @task: RPC task that is requesting the connect 6891da177e4SLinus Torvalds * 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds struct rpc_xprt *xprt = task->tk_xprt; 6941da177e4SLinus Torvalds 69546121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 6961da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 6971da177e4SLinus Torvalds 698ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 69901d37c42STrond Myklebust task->tk_status = -EAGAIN; 7001da177e4SLinus Torvalds return; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7031da177e4SLinus Torvalds return; 704feb8ca37STrond Myklebust 705feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 706feb8ca37STrond Myklebust xprt->ops->close(xprt); 707feb8ca37STrond Myklebust 7081da177e4SLinus Torvalds if (xprt_connected(xprt)) 709a246b010SChuck Lever xprt_release_write(xprt, task); 710a246b010SChuck Lever else { 7111da177e4SLinus Torvalds if (task->tk_rqstp) 7121da177e4SLinus Torvalds task->tk_rqstp->rq_bytes_sent = 0; 7131da177e4SLinus Torvalds 714a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7155d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 7160b9e7943STrond Myklebust 7170b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7180b9e7943STrond Myklebust return; 7190b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7200b9e7943STrond Myklebust return; 721262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 722a246b010SChuck Lever xprt->ops->connect(task); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7269903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds struct rpc_xprt *xprt = task->tk_xprt; 7291da177e4SLinus Torvalds 730cd983ef8SChuck Lever if (task->tk_status == 0) { 731262ca07dSChuck Lever xprt->stat.connect_count++; 732262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 73346121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 7341da177e4SLinus Torvalds task->tk_pid); 7351da177e4SLinus Torvalds return; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds switch (task->tk_status) { 7392a491991STrond Myklebust case -EAGAIN: 7402a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 74123475d66SChuck Lever break; 7421da177e4SLinus Torvalds case -ETIMEDOUT: 74346121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 74446121cf7SChuck Lever "out\n", task->tk_pid); 7451da177e4SLinus Torvalds break; 7461da177e4SLinus Torvalds default: 74746121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 74846121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 74946121cf7SChuck Lever task->tk_client->cl_server); 7501da177e4SLinus Torvalds xprt_release_write(xprt, task); 75123475d66SChuck Lever task->tk_status = -EIO; 75223475d66SChuck Lever } 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7559903cd1cSChuck Lever /** 7569903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 7579903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 7589903cd1cSChuck Lever * @xid: RPC XID of incoming reply 7599903cd1cSChuck Lever * 7601da177e4SLinus Torvalds */ 761d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 7621da177e4SLinus Torvalds { 7638f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 7641da177e4SLinus Torvalds 7658f3a6de3SPavel Emelyanov list_for_each_entry(entry, &xprt->recv, rq_list) 766262ca07dSChuck Lever if (entry->rq_xid == xid) 767262ca07dSChuck Lever return entry; 76846121cf7SChuck Lever 76946121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 77046121cf7SChuck Lever ntohl(xid)); 771262ca07dSChuck Lever xprt->stat.bad_xids++; 772262ca07dSChuck Lever return NULL; 7731da177e4SLinus Torvalds } 77412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 7751da177e4SLinus Torvalds 776bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task) 7771da177e4SLinus Torvalds { 7781570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 7791570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 7801da177e4SLinus Torvalds unsigned timer = task->tk_msg.rpc_proc->p_timer; 781d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 7821570c1e4SChuck Lever 7831da177e4SLinus Torvalds if (timer) { 7841da177e4SLinus Torvalds if (req->rq_ntrans == 1) 785ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 7861570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901570c1e4SChuck Lever /** 7911570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 7921570c1e4SChuck Lever * @task: RPC request that recently completed 7931570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 7941570c1e4SChuck Lever * 7951570c1e4SChuck Lever * Caller holds transport lock. 7961570c1e4SChuck Lever */ 7971570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 7981570c1e4SChuck Lever { 7991570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 800fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 8011da177e4SLinus Torvalds 8021570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 8031570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 8041da177e4SLinus Torvalds 805fda13939STrond Myklebust xprt->stat.recvs++; 806d60dbb20STrond Myklebust req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); 807bbc72ceaSChuck Lever if (xprt->ops->timer != NULL) 808bbc72ceaSChuck Lever xprt_update_rtt(task); 809ef759a2eSChuck Lever 8101da177e4SLinus Torvalds list_del_init(&req->rq_list); 8111e799b67STrond Myklebust req->rq_private_buf.len = copied; 812dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 813dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 81443ac3f29STrond Myklebust smp_wmb(); 815dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 816fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8171da177e4SLinus Torvalds } 81812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 8191da177e4SLinus Torvalds 82046c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8231da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 8241da177e4SLinus Torvalds 8255d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 8265d00837bSTrond Myklebust return; 82746121cf7SChuck Lever dprintk("RPC: %5u xprt_timer\n", task->tk_pid); 82846c0ee8bSChuck Lever 8295d00837bSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 830dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 83146c0ee8bSChuck Lever if (xprt->ops->timer) 83246c0ee8bSChuck Lever xprt->ops->timer(task); 8335d00837bSTrond Myklebust } else 8345d00837bSTrond Myklebust task->tk_status = 0; 8355d00837bSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8384cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt) 8394cfc7e60SRahul Iyer { 8404cfc7e60SRahul Iyer return xprt->idle_timeout != 0; 8414cfc7e60SRahul Iyer } 8424cfc7e60SRahul Iyer 8439903cd1cSChuck Lever /** 8449903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 8459903cd1cSChuck Lever * @task: RPC task about to send a request 8469903cd1cSChuck Lever * 8471da177e4SLinus Torvalds */ 8489903cd1cSChuck Lever int xprt_prepare_transmit(struct rpc_task *task) 8491da177e4SLinus Torvalds { 8501da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8511da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 8521da177e4SLinus Torvalds int err = 0; 8531da177e4SLinus Torvalds 85446121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 8551da177e4SLinus Torvalds 8564a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 857dd2b63d0SRicardo Labiaga if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) { 858dd2b63d0SRicardo Labiaga err = req->rq_reply_bytes_recvd; 8591da177e4SLinus Torvalds goto out_unlock; 8601da177e4SLinus Torvalds } 86143cedbf0STrond Myklebust if (!xprt->ops->reserve_xprt(xprt, task)) 8621da177e4SLinus Torvalds err = -EAGAIN; 8631da177e4SLinus Torvalds out_unlock: 8644a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 8651da177e4SLinus Torvalds return err; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 868e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 8695e5ce5beSTrond Myklebust { 870343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 8715e5ce5beSTrond Myklebust } 8725e5ce5beSTrond Myklebust 8739903cd1cSChuck Lever /** 8749903cd1cSChuck Lever * xprt_transmit - send an RPC request on a transport 8759903cd1cSChuck Lever * @task: controlling RPC task 8769903cd1cSChuck Lever * 8779903cd1cSChuck Lever * We have to copy the iovec because sendmsg fiddles with its contents. 8789903cd1cSChuck Lever */ 8799903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8821da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 883a246b010SChuck Lever int status; 8841da177e4SLinus Torvalds 88546121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 8861da177e4SLinus Torvalds 887dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 88855ae1aabSRicardo Labiaga if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { 88955ae1aabSRicardo Labiaga /* 89055ae1aabSRicardo Labiaga * Add to the list only if we're expecting a reply 89155ae1aabSRicardo Labiaga */ 8924a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 8931da177e4SLinus Torvalds /* Update the softirq receive buffer */ 8941da177e4SLinus Torvalds memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 8951da177e4SLinus Torvalds sizeof(req->rq_private_buf)); 8961da177e4SLinus Torvalds /* Add request to the receive list */ 8971da177e4SLinus Torvalds list_add_tail(&req->rq_list, &xprt->recv); 8984a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 8991da177e4SLinus Torvalds xprt_reset_majortimeo(req); 9000f9dc2b1STrond Myklebust /* Turn off autodisconnect */ 9010f9dc2b1STrond Myklebust del_singleshot_timer_sync(&xprt->timer); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds } else if (!req->rq_bytes_sent) 9041da177e4SLinus Torvalds return; 9051da177e4SLinus Torvalds 9067c1d71cfSTrond Myklebust req->rq_connect_cookie = xprt->connect_cookie; 907ff839970SChuck Lever req->rq_xtime = ktime_get(); 908a246b010SChuck Lever status = xprt->ops->send_request(task); 909c8485e4dSTrond Myklebust if (status != 0) { 910c8485e4dSTrond Myklebust task->tk_status = status; 911c8485e4dSTrond Myklebust return; 912c8485e4dSTrond Myklebust } 913c8485e4dSTrond Myklebust 91446121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 915468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 916fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 917262ca07dSChuck Lever 918fe3aca29SChuck Lever xprt->ops->set_retrans_timeout(task); 919262ca07dSChuck Lever 920262ca07dSChuck Lever xprt->stat.sends++; 921262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 922262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 923262ca07dSChuck Lever 924fe3aca29SChuck Lever /* Don't race with disconnect */ 925fe3aca29SChuck Lever if (!xprt_connected(xprt)) 926fe3aca29SChuck Lever task->tk_status = -ENOTCONN; 927dd2b63d0SRicardo Labiaga else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) { 92855ae1aabSRicardo Labiaga /* 92955ae1aabSRicardo Labiaga * Sleep on the pending queue since 93055ae1aabSRicardo Labiaga * we're expecting a reply. 93155ae1aabSRicardo Labiaga */ 9325d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 93355ae1aabSRicardo Labiaga } 934fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937ee5ebe85STrond Myklebust static void xprt_alloc_slot(struct rpc_task *task) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds struct rpc_xprt *xprt = task->tk_xprt; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds task->tk_status = 0; 9421da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 9431da177e4SLinus Torvalds struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 9441da177e4SLinus Torvalds list_del_init(&req->rq_list); 9451da177e4SLinus Torvalds task->tk_rqstp = req; 9461da177e4SLinus Torvalds xprt_request_init(task, xprt); 9471da177e4SLinus Torvalds return; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds dprintk("RPC: waiting for request slot\n"); 9501da177e4SLinus Torvalds task->tk_status = -EAGAIN; 9515d00837bSTrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 954ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 955ee5ebe85STrond Myklebust { 956ee5ebe85STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 957ee5ebe85STrond Myklebust 958ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 959ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 960ee5ebe85STrond Myklebust rpc_wake_up_next(&xprt->backlog); 961ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 962ee5ebe85STrond Myklebust } 963ee5ebe85STrond Myklebust 96437aa2133SPavel Emelyanov struct rpc_xprt *xprt_alloc(struct net *net, int size, int max_req) 965bd1722d4SPavel Emelyanov { 966bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 967bd1722d4SPavel Emelyanov 968bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 969bd1722d4SPavel Emelyanov if (xprt == NULL) 970bd1722d4SPavel Emelyanov goto out; 971a8de240aSTrond Myklebust atomic_set(&xprt->count, 1); 972bd1722d4SPavel Emelyanov 973bd1722d4SPavel Emelyanov xprt->max_reqs = max_req; 974bd1722d4SPavel Emelyanov xprt->slot = kcalloc(max_req, sizeof(struct rpc_rqst), GFP_KERNEL); 975bd1722d4SPavel Emelyanov if (xprt->slot == NULL) 976bd1722d4SPavel Emelyanov goto out_free; 977bd1722d4SPavel Emelyanov 97837aa2133SPavel Emelyanov xprt->xprt_net = get_net(net); 979bd1722d4SPavel Emelyanov return xprt; 980bd1722d4SPavel Emelyanov 981bd1722d4SPavel Emelyanov out_free: 982bd1722d4SPavel Emelyanov kfree(xprt); 983bd1722d4SPavel Emelyanov out: 984bd1722d4SPavel Emelyanov return NULL; 985bd1722d4SPavel Emelyanov } 986bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 987bd1722d4SPavel Emelyanov 988e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 989e204e621SPavel Emelyanov { 99037aa2133SPavel Emelyanov put_net(xprt->xprt_net); 991e204e621SPavel Emelyanov kfree(xprt->slot); 992e204e621SPavel Emelyanov kfree(xprt); 993e204e621SPavel Emelyanov } 994e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 995e204e621SPavel Emelyanov 9969903cd1cSChuck Lever /** 9979903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 9989903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 9999903cd1cSChuck Lever * 10009903cd1cSChuck Lever * If no more slots are available, place the task on the transport's 10019903cd1cSChuck Lever * backlog queue. 10029903cd1cSChuck Lever */ 10039903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds struct rpc_xprt *xprt = task->tk_xprt; 10061da177e4SLinus Torvalds 100743cedbf0STrond Myklebust task->tk_status = 0; 100843cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 100943cedbf0STrond Myklebust return; 101043cedbf0STrond Myklebust 101143cedbf0STrond Myklebust /* Note: grabbing the xprt_lock_write() here is not strictly needed, 101243cedbf0STrond Myklebust * but ensures that we throttle new slot allocation if the transport 101343cedbf0STrond Myklebust * is congested (e.g. if reconnecting or if we're out of socket 101443cedbf0STrond Myklebust * write buffer space). 101543cedbf0STrond Myklebust */ 101643cedbf0STrond Myklebust task->tk_timeout = 0; 101743cedbf0STrond Myklebust task->tk_status = -EAGAIN; 101843cedbf0STrond Myklebust if (!xprt_lock_write(xprt, task)) 101943cedbf0STrond Myklebust return; 102043cedbf0STrond Myklebust 10211da177e4SLinus Torvalds task->tk_status = -EIO; 10225dc07727SChuck Lever spin_lock(&xprt->reserve_lock); 1023ee5ebe85STrond Myklebust xprt_alloc_slot(task); 10245dc07727SChuck Lever spin_unlock(&xprt->reserve_lock); 102543cedbf0STrond Myklebust xprt_release_write(xprt, task); 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 1028d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) 10291da177e4SLinus Torvalds { 10300eae88f3SEric Dumazet return (__force __be32)xprt->xid++; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt) 10341da177e4SLinus Torvalds { 1035bf3fcf89SChuck Lever xprt->xid = net_random(); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10389903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 10411da177e4SLinus Torvalds 1042ba7392bbSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 10431da177e4SLinus Torvalds req->rq_task = task; 10441da177e4SLinus Torvalds req->rq_xprt = xprt; 104502107148SChuck Lever req->rq_buffer = NULL; 10461da177e4SLinus Torvalds req->rq_xid = xprt_alloc_xid(xprt); 1047ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf = NULL; 1048da45828eSTrond Myklebust xprt_reset_majortimeo(req); 104946121cf7SChuck Lever dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 10501da177e4SLinus Torvalds req, ntohl(req->rq_xid)); 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10539903cd1cSChuck Lever /** 10549903cd1cSChuck Lever * xprt_release - release an RPC request slot 10559903cd1cSChuck Lever * @task: task which is finished with the slot 10569903cd1cSChuck Lever * 10571da177e4SLinus Torvalds */ 10589903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 10591da177e4SLinus Torvalds { 106055ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 10611da177e4SLinus Torvalds struct rpc_rqst *req; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds if (!(req = task->tk_rqstp)) 10641da177e4SLinus Torvalds return; 106555ae1aabSRicardo Labiaga 106655ae1aabSRicardo Labiaga xprt = req->rq_xprt; 106711c556b3SChuck Lever rpc_count_iostats(task); 10684a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 106949e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1070a58dd398SChuck Lever if (xprt->ops->release_request) 1071a58dd398SChuck Lever xprt->ops->release_request(task); 10721da177e4SLinus Torvalds if (!list_empty(&req->rq_list)) 10731da177e4SLinus Torvalds list_del(&req->rq_list); 10741da177e4SLinus Torvalds xprt->last_used = jiffies; 10754cfc7e60SRahul Iyer if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) 1076a246b010SChuck Lever mod_timer(&xprt->timer, 107703bf4b70SChuck Lever xprt->last_used + xprt->idle_timeout); 10784a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1079ee5ebe85STrond Myklebust if (req->rq_buffer) 1080c5a4dd8bSChuck Lever xprt->ops->buf_free(req->rq_buffer); 1081a17c2153STrond Myklebust if (req->rq_cred != NULL) 1082a17c2153STrond Myklebust put_rpccred(req->rq_cred); 10831da177e4SLinus Torvalds task->tk_rqstp = NULL; 1084ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1085ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 108655ae1aabSRicardo Labiaga 108746121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1088ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1089ee5ebe85STrond Myklebust xprt_free_slot(xprt, req); 1090ee5ebe85STrond Myklebust else 1091c9acb42eSTrond Myklebust xprt_free_bc_request(req); 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 10948d9266ffSTrond Myklebust static void xprt_init(struct rpc_xprt *xprt) 1095c2866763SChuck Lever { 1096c2866763SChuck Lever struct rpc_rqst *req; 1097c2866763SChuck Lever 1098c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1099c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 1100c2866763SChuck Lever 1101c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 1102c2866763SChuck Lever INIT_LIST_HEAD(&xprt->recv); 11039e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1104f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1105f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 11069e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1107f9acac1aSRicardo Labiaga 110865f27f38SDavid Howells INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 11094cfc7e60SRahul Iyer if (xprt_has_timer(xprt)) 1110b24b8a24SPavel Emelyanov setup_timer(&xprt->timer, xprt_init_autodisconnect, 1111b24b8a24SPavel Emelyanov (unsigned long)xprt); 11124cfc7e60SRahul Iyer else 11134cfc7e60SRahul Iyer init_timer(&xprt->timer); 1114c2866763SChuck Lever xprt->last_used = jiffies; 1115c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1116a509050bSChuck Lever xprt->bind_index = 0; 1117c2866763SChuck Lever 1118c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1119c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 1120c2866763SChuck Lever rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1121c2866763SChuck Lever rpc_init_wait_queue(&xprt->resend, "xprt_resend"); 1122c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1123c2866763SChuck Lever 1124c2866763SChuck Lever /* initialize free list */ 1125c2866763SChuck Lever for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) 1126c2866763SChuck Lever list_add(&req->rq_list, &xprt->free); 1127c2866763SChuck Lever 1128c2866763SChuck Lever xprt_init_xid(xprt); 1129c2866763SChuck Lever 11308d9266ffSTrond Myklebust } 11318d9266ffSTrond Myklebust 11328d9266ffSTrond Myklebust /** 11338d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 11348d9266ffSTrond Myklebust * @args: rpc transport creation arguments 11358d9266ffSTrond Myklebust * 11368d9266ffSTrond Myklebust */ 11378d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 11388d9266ffSTrond Myklebust { 11398d9266ffSTrond Myklebust struct rpc_xprt *xprt; 11408d9266ffSTrond Myklebust struct xprt_class *t; 11418d9266ffSTrond Myklebust 11428d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 11438d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 11448d9266ffSTrond Myklebust if (t->ident == args->ident) { 11458d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 11468d9266ffSTrond Myklebust goto found; 11478d9266ffSTrond Myklebust } 11488d9266ffSTrond Myklebust } 11498d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 11508d9266ffSTrond Myklebust printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident); 11518d9266ffSTrond Myklebust return ERR_PTR(-EIO); 11528d9266ffSTrond Myklebust 11538d9266ffSTrond Myklebust found: 11548d9266ffSTrond Myklebust xprt = t->setup(args); 11558d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 11568d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 11578d9266ffSTrond Myklebust -PTR_ERR(xprt)); 11588d9266ffSTrond Myklebust return xprt; 11598d9266ffSTrond Myklebust } 11608d9266ffSTrond Myklebust if (test_and_set_bit(XPRT_INITIALIZED, &xprt->state)) 11618d9266ffSTrond Myklebust /* ->setup returned a pre-initialized xprt: */ 11628d9266ffSTrond Myklebust return xprt; 11638d9266ffSTrond Myklebust 11648d9266ffSTrond Myklebust xprt_init(xprt); 11658d9266ffSTrond Myklebust 1166c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1167c2866763SChuck Lever xprt->max_reqs); 1168c2866763SChuck Lever return xprt; 1169c2866763SChuck Lever } 1170c2866763SChuck Lever 11719903cd1cSChuck Lever /** 11729903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1173a8de240aSTrond Myklebust * @xprt: transport to destroy 11749903cd1cSChuck Lever * 11751da177e4SLinus Torvalds */ 1176a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 11771da177e4SLinus Torvalds { 11781da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 11790065db32STrond Myklebust xprt->shutdown = 1; 11800065db32STrond Myklebust del_timer_sync(&xprt->timer); 1181c8541ecdSChuck Lever 1182f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1183f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1184f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1185f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->resend); 1186f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1187c3ae62aeSJ. Bruce Fields cancel_work_sync(&xprt->task_cleanup); 1188c8541ecdSChuck Lever /* 1189c8541ecdSChuck Lever * Tear down transport state and free the rpc_xprt 1190c8541ecdSChuck Lever */ 1191a246b010SChuck Lever xprt->ops->destroy(xprt); 11926b6ca86bSTrond Myklebust } 11931da177e4SLinus Torvalds 11946b6ca86bSTrond Myklebust /** 11956b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 11966b6ca86bSTrond Myklebust * @xprt: pointer to the transport 11976b6ca86bSTrond Myklebust * 11986b6ca86bSTrond Myklebust */ 11996b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 12006b6ca86bSTrond Myklebust { 1201a8de240aSTrond Myklebust if (atomic_dec_and_test(&xprt->count)) 1202a8de240aSTrond Myklebust xprt_destroy(xprt); 12036b6ca86bSTrond Myklebust } 12046b6ca86bSTrond Myklebust 12056b6ca86bSTrond Myklebust /** 12066b6ca86bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 12076b6ca86bSTrond Myklebust * @xprt: pointer to the transport 12086b6ca86bSTrond Myklebust * 12096b6ca86bSTrond Myklebust */ 12106b6ca86bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 12116b6ca86bSTrond Myklebust { 1212a8de240aSTrond Myklebust if (atomic_inc_not_zero(&xprt->count)) 12136b6ca86bSTrond Myklebust return xprt; 1214a8de240aSTrond Myklebust return NULL; 12151da177e4SLinus Torvalds } 1216