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 */ 6521de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 661da177e4SLinus Torvalds static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 671da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 681da177e4SLinus Torvalds static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 694e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 701da177e4SLinus Torvalds 715ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7281c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7381c098afS\"Talpey, Thomas\ 74555ee3afSChuck Lever /* 75555ee3afSChuck Lever * The transport code maintains an estimate on the maximum number of out- 76555ee3afSChuck Lever * standing RPC requests, using a smoothed version of the congestion 77555ee3afSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 78555ee3afSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 79555ee3afSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 80555ee3afSChuck Lever * 81555ee3afSChuck Lever * - a reply is received and 82555ee3afSChuck Lever * - a full number of requests are outstanding and 83555ee3afSChuck Lever * - the congestion window hasn't been updated recently. 84555ee3afSChuck Lever */ 85555ee3afSChuck Lever #define RPC_CWNDSHIFT (8U) 86555ee3afSChuck Lever #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT) 87555ee3afSChuck Lever #define RPC_INITCWND RPC_CWNDSCALE 88555ee3afSChuck Lever #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT) 89555ee3afSChuck Lever 90555ee3afSChuck Lever #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd) 911da177e4SLinus Torvalds 9212a80469SChuck Lever /** 9381c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 9481c098afS\"Talpey, Thomas\ * @transport: transport to register 9581c098afS\"Talpey, Thomas\ * 9681c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9781c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9881c098afS\"Talpey, Thomas\ * 9981c098afS\"Talpey, Thomas\ * Returns: 10081c098afS\"Talpey, Thomas\ * 0: transport successfully registered 10181c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 10281c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 10381c098afS\"Talpey, Thomas\ */ 10481c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 10581c098afS\"Talpey, Thomas\ { 10681c098afS\"Talpey, Thomas\ struct xprt_class *t; 10781c098afS\"Talpey, Thomas\ int result; 10881c098afS\"Talpey, Thomas\ 10981c098afS\"Talpey, Thomas\ result = -EEXIST; 11081c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 11181c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 11281c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1134fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 11481c098afS\"Talpey, Thomas\ goto out; 11581c098afS\"Talpey, Thomas\ } 11681c098afS\"Talpey, Thomas\ 11781c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11881c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11981c098afS\"Talpey, Thomas\ transport->name); 12081c098afS\"Talpey, Thomas\ result = 0; 12181c098afS\"Talpey, Thomas\ 12281c098afS\"Talpey, Thomas\ out: 12381c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 12481c098afS\"Talpey, Thomas\ return result; 12581c098afS\"Talpey, Thomas\ } 12681c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12781c098afS\"Talpey, Thomas\ 12881c098afS\"Talpey, Thomas\ /** 12981c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 13065b6e42cSRandy Dunlap * @transport: transport to unregister 13181c098afS\"Talpey, Thomas\ * 13281c098afS\"Talpey, Thomas\ * Returns: 13381c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 13481c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 13581c098afS\"Talpey, Thomas\ */ 13681c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13781c098afS\"Talpey, Thomas\ { 13881c098afS\"Talpey, Thomas\ struct xprt_class *t; 13981c098afS\"Talpey, Thomas\ int result; 14081c098afS\"Talpey, Thomas\ 14181c098afS\"Talpey, Thomas\ result = 0; 14281c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 14381c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 14481c098afS\"Talpey, Thomas\ if (t == transport) { 14581c098afS\"Talpey, Thomas\ printk(KERN_INFO 14681c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14781c098afS\"Talpey, Thomas\ transport->name); 14881c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14981c098afS\"Talpey, Thomas\ goto out; 15081c098afS\"Talpey, Thomas\ } 15181c098afS\"Talpey, Thomas\ } 15281c098afS\"Talpey, Thomas\ result = -ENOENT; 15381c098afS\"Talpey, Thomas\ 15481c098afS\"Talpey, Thomas\ out: 15581c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15681c098afS\"Talpey, Thomas\ return result; 15781c098afS\"Talpey, Thomas\ } 15881c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15981c098afS\"Talpey, Thomas\ 16081c098afS\"Talpey, Thomas\ /** 161441e3e24STom Talpey * xprt_load_transport - load a transport implementation 162441e3e24STom Talpey * @transport_name: transport to load 163441e3e24STom Talpey * 164441e3e24STom Talpey * Returns: 165441e3e24STom Talpey * 0: transport successfully loaded 166441e3e24STom Talpey * -ENOENT: transport module not available 167441e3e24STom Talpey */ 168441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 169441e3e24STom Talpey { 170441e3e24STom Talpey struct xprt_class *t; 171441e3e24STom Talpey int result; 172441e3e24STom Talpey 173441e3e24STom Talpey result = 0; 174441e3e24STom Talpey spin_lock(&xprt_list_lock); 175441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 176441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 177441e3e24STom Talpey spin_unlock(&xprt_list_lock); 178441e3e24STom Talpey goto out; 179441e3e24STom Talpey } 180441e3e24STom Talpey } 181441e3e24STom Talpey spin_unlock(&xprt_list_lock); 182ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 183441e3e24STom Talpey out: 184441e3e24STom Talpey return result; 185441e3e24STom Talpey } 186441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 187441e3e24STom Talpey 188441e3e24STom Talpey /** 18912a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 19012a80469SChuck Lever * @task: task that is requesting access to the transport 191177c27bfSRandy Dunlap * @xprt: pointer to the target transport 19212a80469SChuck Lever * 19312a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 19412a80469SChuck Lever * transport connects from colliding with writes. No congestion control 19512a80469SChuck Lever * is provided. 1961da177e4SLinus Torvalds */ 19743cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1981da177e4SLinus Torvalds { 19912a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 20034006ceeSTrond Myklebust int priority; 20112a80469SChuck Lever 20212a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 20312a80469SChuck Lever if (task == xprt->snd_task) 20412a80469SChuck Lever return 1; 20512a80469SChuck Lever goto out_sleep; 20612a80469SChuck Lever } 20712a80469SChuck Lever xprt->snd_task = task; 20892551948STrond Myklebust if (req != NULL) 20912a80469SChuck Lever req->rq_ntrans++; 2104d4a76f3Sj223yang@asset.uwaterloo.ca 21112a80469SChuck Lever return 1; 21212a80469SChuck Lever 21312a80469SChuck Lever out_sleep: 21446121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 21512a80469SChuck Lever task->tk_pid, xprt); 21612a80469SChuck Lever task->tk_timeout = 0; 21712a80469SChuck Lever task->tk_status = -EAGAIN; 21834006ceeSTrond Myklebust if (req == NULL) 21934006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 22034006ceeSTrond Myklebust else if (!req->rq_ntrans) 22134006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 22212a80469SChuck Lever else 22334006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 22434006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 22512a80469SChuck Lever return 0; 22612a80469SChuck Lever } 22712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 22812a80469SChuck Lever 229632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 230632e3bdcSTrond Myklebust { 231632e3bdcSTrond Myklebust xprt->snd_task = NULL; 232d19751e7STrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 233632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 234632e3bdcSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 235632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 236632e3bdcSTrond Myklebust } else 237c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 238632e3bdcSTrond Myklebust } 239632e3bdcSTrond Myklebust 24012a80469SChuck Lever /* 24112a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 24212a80469SChuck Lever * @task: task that is requesting access to the transport 24312a80469SChuck Lever * 24412a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 24512a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 24612a80469SChuck Lever * woken up and given access to the transport. 24712a80469SChuck Lever */ 24843cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 24912a80469SChuck Lever { 2501da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 25134006ceeSTrond Myklebust int priority; 2521da177e4SLinus Torvalds 2532226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2541da177e4SLinus Torvalds if (task == xprt->snd_task) 2551da177e4SLinus Torvalds return 1; 2561da177e4SLinus Torvalds goto out_sleep; 2571da177e4SLinus Torvalds } 25843cedbf0STrond Myklebust if (req == NULL) { 25943cedbf0STrond Myklebust xprt->snd_task = task; 26043cedbf0STrond Myklebust return 1; 26143cedbf0STrond Myklebust } 26212a80469SChuck Lever if (__xprt_get_cong(xprt, task)) { 2631da177e4SLinus Torvalds xprt->snd_task = task; 2641da177e4SLinus Torvalds req->rq_ntrans++; 2651da177e4SLinus Torvalds return 1; 2661da177e4SLinus Torvalds } 267632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2681da177e4SLinus Torvalds out_sleep: 26946121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2701da177e4SLinus Torvalds task->tk_timeout = 0; 2711da177e4SLinus Torvalds task->tk_status = -EAGAIN; 27234006ceeSTrond Myklebust if (req == NULL) 27334006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 27434006ceeSTrond Myklebust else if (!req->rq_ntrans) 27534006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 2761da177e4SLinus Torvalds else 27734006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 27834006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 28112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2821da177e4SLinus Torvalds 28312a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds int retval; 2861da177e4SLinus Torvalds 2874a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 28843cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2894a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2901da177e4SLinus Torvalds return retval; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 293961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2941da177e4SLinus Torvalds { 295961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 29649e9a890SChuck Lever struct rpc_rqst *req; 29749e9a890SChuck Lever 29849e9a890SChuck Lever req = task->tk_rqstp; 29949e9a890SChuck Lever xprt->snd_task = task; 30092551948STrond Myklebust if (req) 30149e9a890SChuck Lever req->rq_ntrans++; 302961a828dSTrond Myklebust return true; 303961a828dSTrond Myklebust } 304961a828dSTrond Myklebust 305961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 306961a828dSTrond Myklebust { 307961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 30849e9a890SChuck Lever return; 30949e9a890SChuck Lever 310961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt)) 311961a828dSTrond Myklebust return; 312632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 31349e9a890SChuck Lever } 31449e9a890SChuck Lever 315961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data) 31649e9a890SChuck Lever { 317961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 31843cedbf0STrond Myklebust struct rpc_rqst *req; 3191da177e4SLinus Torvalds 32043cedbf0STrond Myklebust req = task->tk_rqstp; 32143cedbf0STrond Myklebust if (req == NULL) { 3221da177e4SLinus Torvalds xprt->snd_task = task; 323961a828dSTrond Myklebust return true; 32443cedbf0STrond Myklebust } 32543cedbf0STrond Myklebust if (__xprt_get_cong(xprt, task)) { 32643cedbf0STrond Myklebust xprt->snd_task = task; 3271da177e4SLinus Torvalds req->rq_ntrans++; 328961a828dSTrond Myklebust return true; 3291da177e4SLinus Torvalds } 330961a828dSTrond Myklebust return false; 331961a828dSTrond Myklebust } 332961a828dSTrond Myklebust 333961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 334961a828dSTrond Myklebust { 335961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 336961a828dSTrond Myklebust return; 337961a828dSTrond Myklebust if (RPCXPRT_CONGESTED(xprt)) 338961a828dSTrond Myklebust goto out_unlock; 339961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt)) 340961a828dSTrond Myklebust return; 3411da177e4SLinus Torvalds out_unlock: 342632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 34549e9a890SChuck Lever /** 34649e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 34749e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 34849e9a890SChuck Lever * @task: task that is releasing access to the transport 34949e9a890SChuck Lever * 35049e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3511da177e4SLinus Torvalds */ 35249e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds if (xprt->snd_task == task) { 355ee071effSTrond Myklebust if (task != NULL) { 356ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 357ee071effSTrond Myklebust if (req != NULL) 358ee071effSTrond Myklebust req->rq_bytes_sent = 0; 359ee071effSTrond Myklebust } 360632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3611da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds } 36412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3651da177e4SLinus Torvalds 36649e9a890SChuck Lever /** 36749e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 36849e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 36949e9a890SChuck Lever * @task: task that is releasing access to the transport 37049e9a890SChuck Lever * 37149e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 37249e9a890SChuck Lever * transport if the transport's congestion window allows it. 37349e9a890SChuck Lever */ 37449e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 37549e9a890SChuck Lever { 37649e9a890SChuck Lever if (xprt->snd_task == task) { 377ee071effSTrond Myklebust if (task != NULL) { 378ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 379ee071effSTrond Myklebust if (req != NULL) 380ee071effSTrond Myklebust req->rq_bytes_sent = 0; 381ee071effSTrond Myklebust } 382632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 38349e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 38449e9a890SChuck Lever } 38549e9a890SChuck Lever } 38612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 38749e9a890SChuck Lever 38849e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3891da177e4SLinus Torvalds { 3904a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 39149e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3924a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* 3961da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3971da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3981da177e4SLinus Torvalds */ 3991da177e4SLinus Torvalds static int 4001da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds if (req->rq_cong) 4051da177e4SLinus Torvalds return 1; 40646121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 4071da177e4SLinus Torvalds task->tk_pid, xprt->cong, xprt->cwnd); 4081da177e4SLinus Torvalds if (RPCXPRT_CONGESTED(xprt)) 4091da177e4SLinus Torvalds return 0; 4101da177e4SLinus Torvalds req->rq_cong = 1; 4111da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4121da177e4SLinus Torvalds return 1; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4171da177e4SLinus Torvalds * that has been sleeping due to congestion 4181da177e4SLinus Torvalds */ 4191da177e4SLinus Torvalds static void 4201da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4211da177e4SLinus Torvalds { 4221da177e4SLinus Torvalds if (!req->rq_cong) 4231da177e4SLinus Torvalds return; 4241da177e4SLinus Torvalds req->rq_cong = 0; 4251da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 42649e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 42946c0ee8bSChuck Lever /** 430a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 431a58dd398SChuck Lever * @task: RPC request that recently completed 432a58dd398SChuck Lever * 433a58dd398SChuck Lever * Useful for transports that require congestion control. 434a58dd398SChuck Lever */ 435a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 436a58dd398SChuck Lever { 437a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 438a4f0835cSTrond Myklebust 439a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 440a58dd398SChuck Lever } 44112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 442a58dd398SChuck Lever 443a58dd398SChuck Lever /** 44446c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4456a24dfb6STrond Myklebust * @xprt: pointer to xprt 44646c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 44746c0ee8bSChuck Lever * @result: result code of completed RPC request 44846c0ee8bSChuck Lever * 4491da177e4SLinus Torvalds * We use a time-smoothed congestion estimator to avoid heavy oscillation. 4501da177e4SLinus Torvalds */ 4516a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4521da177e4SLinus Torvalds { 45346c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 45446c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4571da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4581da177e4SLinus Torvalds * the result gets rounded properly. */ 4591da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4601da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4611da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 46249e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4631da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4641da177e4SLinus Torvalds cwnd >>= 1; 4651da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4661da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4691da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4701da177e4SLinus Torvalds xprt->cwnd = cwnd; 47146c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4721da177e4SLinus Torvalds } 47312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4741da177e4SLinus Torvalds 47544fbac22SChuck Lever /** 47644fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 47744fbac22SChuck Lever * @xprt: transport with waiting tasks 47844fbac22SChuck Lever * @status: result code to plant in each task before waking it 47944fbac22SChuck Lever * 48044fbac22SChuck Lever */ 48144fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 48244fbac22SChuck Lever { 48344fbac22SChuck Lever if (status < 0) 48444fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 48544fbac22SChuck Lever else 48644fbac22SChuck Lever rpc_wake_up(&xprt->pending); 48744fbac22SChuck Lever } 48812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 48944fbac22SChuck Lever 490c7b2cae8SChuck Lever /** 491c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 492c7b2cae8SChuck Lever * @task: task to be put to sleep 4930b80ae42SRandy Dunlap * @action: function pointer to be executed after wait 494a9a6b52eSTrond Myklebust * 495a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 496a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 497a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 498c7b2cae8SChuck Lever */ 499b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) 500c7b2cae8SChuck Lever { 501c7b2cae8SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 502c7b2cae8SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 503c7b2cae8SChuck Lever 504a9a6b52eSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 505b6ddf64fSTrond Myklebust rpc_sleep_on(&xprt->pending, task, action); 506c7b2cae8SChuck Lever } 50712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 508c7b2cae8SChuck Lever 509c7b2cae8SChuck Lever /** 510c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 511c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 512c7b2cae8SChuck Lever * 513c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 514c7b2cae8SChuck Lever */ 515c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt) 516c7b2cae8SChuck Lever { 517c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 518c7b2cae8SChuck Lever if (xprt->snd_task) { 51946121cf7SChuck Lever dprintk("RPC: write space: waking waiting task on " 52046121cf7SChuck Lever "xprt %p\n", xprt); 521fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); 522c7b2cae8SChuck Lever } 523c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 524c7b2cae8SChuck Lever } 52512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 526c7b2cae8SChuck Lever 527fe3aca29SChuck Lever /** 528fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 529fe3aca29SChuck Lever * @task: task whose timeout is to be set 530fe3aca29SChuck Lever * 531fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 532fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 533fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 534fe3aca29SChuck Lever */ 535fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 536fe3aca29SChuck Lever { 537fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 538fe3aca29SChuck Lever } 53912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 540fe3aca29SChuck Lever 5412c53040fSBen Hutchings /** 542fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 543fe3aca29SChuck Lever * @task: task whose timeout is to be set 544fe3aca29SChuck Lever * 545fe3aca29SChuck Lever * Set a request's retransmit timeout using the RTT estimator. 546fe3aca29SChuck Lever */ 547fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 548fe3aca29SChuck Lever { 549fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 550ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 551ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 552fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 553ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 554fe3aca29SChuck Lever 555fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 556fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 557fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 558fe3aca29SChuck Lever task->tk_timeout = max_timeout; 559fe3aca29SChuck Lever } 56012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 561fe3aca29SChuck Lever 5621da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5631da177e4SLinus Torvalds { 564ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5671da177e4SLinus Torvalds if (to->to_exponential) 5681da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5691da177e4SLinus Torvalds else 5701da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5711da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5721da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5731da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5769903cd1cSChuck Lever /** 5779903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5789903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5799903cd1cSChuck Lever * 5801da177e4SLinus Torvalds */ 5811da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 584ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5851da177e4SLinus Torvalds int status = 0; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5881da177e4SLinus Torvalds if (to->to_exponential) 5891da177e4SLinus Torvalds req->rq_timeout <<= 1; 5901da177e4SLinus Torvalds else 5911da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5921da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5931da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5941da177e4SLinus Torvalds req->rq_retries++; 5951da177e4SLinus Torvalds } else { 5961da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5971da177e4SLinus Torvalds req->rq_retries = 0; 5981da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5991da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 6004a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6011da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 6024a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6031da177e4SLinus Torvalds status = -ETIMEDOUT; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds if (req->rq_timeout == 0) { 6071da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6081da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds return status; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 61365f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6141da177e4SLinus Torvalds { 61565f27f38SDavid Howells struct rpc_xprt *xprt = 61665f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6171da177e4SLinus Torvalds 618a246b010SChuck Lever xprt->ops->close(xprt); 61966af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6201da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 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) 65066af1e55STrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6512a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 65266af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 65366af1e55STrond Myklebust } 65466af1e55STrond Myklebust 6557c1d71cfSTrond Myklebust /** 6567c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6577c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6587c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6597c1d71cfSTrond Myklebust * 6607c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6617c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6627c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6637c1d71cfSTrond Myklebust * a batch of RPC requests. 6647c1d71cfSTrond Myklebust * 6657c1d71cfSTrond Myklebust */ 6667c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6677c1d71cfSTrond Myklebust { 6687c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6697c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6707c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6717c1d71cfSTrond Myklebust goto out; 6727c1d71cfSTrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) 6737c1d71cfSTrond Myklebust goto out; 6747c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6757c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6767c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 6777c1d71cfSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6782a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6797c1d71cfSTrond Myklebust out: 6807c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6817c1d71cfSTrond Myklebust } 6827c1d71cfSTrond Myklebust 6831da177e4SLinus Torvalds static void 6841da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds struct rpc_xprt *xprt = (struct rpc_xprt *)data; 6871da177e4SLinus Torvalds 6884a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 689d19751e7STrond Myklebust if (!list_empty(&xprt->recv)) 6901da177e4SLinus Torvalds goto out_abort; 6912226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 6921da177e4SLinus Torvalds goto out_abort; 6934a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 694f75e6745STrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 695c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6961da177e4SLinus Torvalds return; 6971da177e4SLinus Torvalds out_abort: 6984a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7019903cd1cSChuck Lever /** 7029903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 7039903cd1cSChuck Lever * @task: RPC task that is requesting the connect 7041da177e4SLinus Torvalds * 7051da177e4SLinus Torvalds */ 7061da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 7071da177e4SLinus Torvalds { 708ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7091da177e4SLinus Torvalds 71046121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 7111da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 7121da177e4SLinus Torvalds 713ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 71401d37c42STrond Myklebust task->tk_status = -EAGAIN; 7151da177e4SLinus Torvalds return; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7181da177e4SLinus Torvalds return; 719feb8ca37STrond Myklebust 720feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 721feb8ca37STrond Myklebust xprt->ops->close(xprt); 722feb8ca37STrond Myklebust 7231da177e4SLinus Torvalds if (xprt_connected(xprt)) 724a246b010SChuck Lever xprt_release_write(xprt, task); 725a246b010SChuck Lever else { 7261da177e4SLinus Torvalds task->tk_rqstp->rq_bytes_sent = 0; 727a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7285d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 7290b9e7943STrond Myklebust 7300b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7310b9e7943STrond Myklebust return; 7320b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7330b9e7943STrond Myklebust return; 734262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 7351b092092STrond Myklebust xprt->ops->connect(xprt, task); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7399903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 7401da177e4SLinus Torvalds { 741ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7421da177e4SLinus Torvalds 743cd983ef8SChuck Lever if (task->tk_status == 0) { 744262ca07dSChuck Lever xprt->stat.connect_count++; 745262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 74646121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 7471da177e4SLinus Torvalds task->tk_pid); 7481da177e4SLinus Torvalds return; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds switch (task->tk_status) { 7522a491991STrond Myklebust case -EAGAIN: 7532a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 75423475d66SChuck Lever break; 7551da177e4SLinus Torvalds case -ETIMEDOUT: 75646121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 75746121cf7SChuck Lever "out\n", task->tk_pid); 7581da177e4SLinus Torvalds break; 7591da177e4SLinus Torvalds default: 76046121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 76146121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 7624e0038b6STrond Myklebust xprt->servername); 7631da177e4SLinus Torvalds xprt_release_write(xprt, task); 76423475d66SChuck Lever task->tk_status = -EIO; 76523475d66SChuck Lever } 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7689903cd1cSChuck Lever /** 7699903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 7709903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 7719903cd1cSChuck Lever * @xid: RPC XID of incoming reply 7729903cd1cSChuck Lever * 7731da177e4SLinus Torvalds */ 774d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 7751da177e4SLinus Torvalds { 7768f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 7771da177e4SLinus Torvalds 7788f3a6de3SPavel Emelyanov list_for_each_entry(entry, &xprt->recv, rq_list) 779262ca07dSChuck Lever if (entry->rq_xid == xid) 780262ca07dSChuck Lever return entry; 78146121cf7SChuck Lever 78246121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 78346121cf7SChuck Lever ntohl(xid)); 784262ca07dSChuck Lever xprt->stat.bad_xids++; 785262ca07dSChuck Lever return NULL; 7861da177e4SLinus Torvalds } 78712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 7881da177e4SLinus Torvalds 789bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task) 7901da177e4SLinus Torvalds { 7911570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 7921570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 79395c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 794d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 7951570c1e4SChuck Lever 7961da177e4SLinus Torvalds if (timer) { 7971da177e4SLinus Torvalds if (req->rq_ntrans == 1) 798ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 7991570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031570c1e4SChuck Lever /** 8041570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 8051570c1e4SChuck Lever * @task: RPC request that recently completed 8061570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 8071570c1e4SChuck Lever * 8081570c1e4SChuck Lever * Caller holds transport lock. 8091570c1e4SChuck Lever */ 8101570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 8111570c1e4SChuck Lever { 8121570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 813fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 8141da177e4SLinus Torvalds 8151570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 8161570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 8171da177e4SLinus Torvalds 818fda13939STrond Myklebust xprt->stat.recvs++; 819d60dbb20STrond Myklebust req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); 820bbc72ceaSChuck Lever if (xprt->ops->timer != NULL) 821bbc72ceaSChuck Lever xprt_update_rtt(task); 822ef759a2eSChuck Lever 8231da177e4SLinus Torvalds list_del_init(&req->rq_list); 8241e799b67STrond Myklebust req->rq_private_buf.len = copied; 825dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 826dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 82743ac3f29STrond Myklebust smp_wmb(); 828dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 829fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8301da177e4SLinus Torvalds } 83112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 8321da177e4SLinus Torvalds 83346c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8361da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 8371da177e4SLinus Torvalds 8385d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 8395d00837bSTrond Myklebust return; 84046121cf7SChuck Lever dprintk("RPC: %5u xprt_timer\n", task->tk_pid); 84146c0ee8bSChuck Lever 8425d00837bSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 843dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 84446c0ee8bSChuck Lever if (xprt->ops->timer) 8456a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 8465d00837bSTrond Myklebust } else 8475d00837bSTrond Myklebust task->tk_status = 0; 8485d00837bSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8514cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt) 8524cfc7e60SRahul Iyer { 8534cfc7e60SRahul Iyer return xprt->idle_timeout != 0; 8544cfc7e60SRahul Iyer } 8554cfc7e60SRahul Iyer 8569903cd1cSChuck Lever /** 8579903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 8589903cd1cSChuck Lever * @task: RPC task about to send a request 8599903cd1cSChuck Lever * 8601da177e4SLinus Torvalds */ 86190051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 8621da177e4SLinus Torvalds { 8631da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8641da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 86590051ea7STrond Myklebust bool ret = false; 8661da177e4SLinus Torvalds 86746121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 8681da177e4SLinus Torvalds 8694a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 8708a19a0b6STrond Myklebust if (!req->rq_bytes_sent) { 8718a19a0b6STrond Myklebust if (req->rq_reply_bytes_recvd) { 87290051ea7STrond Myklebust task->tk_status = req->rq_reply_bytes_recvd; 8731da177e4SLinus Torvalds goto out_unlock; 8741da177e4SLinus Torvalds } 8758a19a0b6STrond Myklebust if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) 8768a19a0b6STrond Myklebust && xprt_connected(xprt) 8778a19a0b6STrond Myklebust && req->rq_connect_cookie == xprt->connect_cookie) { 8788a19a0b6STrond Myklebust xprt->ops->set_retrans_timeout(task); 8798a19a0b6STrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 8808a19a0b6STrond Myklebust goto out_unlock; 8818a19a0b6STrond Myklebust } 8828a19a0b6STrond Myklebust } 88390051ea7STrond Myklebust if (!xprt->ops->reserve_xprt(xprt, task)) { 88490051ea7STrond Myklebust task->tk_status = -EAGAIN; 88590051ea7STrond Myklebust goto out_unlock; 88690051ea7STrond Myklebust } 88790051ea7STrond Myklebust ret = true; 8881da177e4SLinus Torvalds out_unlock: 8894a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 89090051ea7STrond Myklebust return ret; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 893e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 8945e5ce5beSTrond Myklebust { 895343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 8965e5ce5beSTrond Myklebust } 8975e5ce5beSTrond Myklebust 8989903cd1cSChuck Lever /** 8999903cd1cSChuck Lever * xprt_transmit - send an RPC request on a transport 9009903cd1cSChuck Lever * @task: controlling RPC task 9019903cd1cSChuck Lever * 9029903cd1cSChuck Lever * We have to copy the iovec because sendmsg fiddles with its contents. 9039903cd1cSChuck Lever */ 9049903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 9071da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 90815a45206SAndy Adamson int status, numreqs; 9091da177e4SLinus Torvalds 91046121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 9111da177e4SLinus Torvalds 912dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 91355ae1aabSRicardo Labiaga if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { 91455ae1aabSRicardo Labiaga /* 91555ae1aabSRicardo Labiaga * Add to the list only if we're expecting a reply 91655ae1aabSRicardo Labiaga */ 9174a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 9181da177e4SLinus Torvalds /* Update the softirq receive buffer */ 9191da177e4SLinus Torvalds memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 9201da177e4SLinus Torvalds sizeof(req->rq_private_buf)); 9211da177e4SLinus Torvalds /* Add request to the receive list */ 9221da177e4SLinus Torvalds list_add_tail(&req->rq_list, &xprt->recv); 9234a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9241da177e4SLinus Torvalds xprt_reset_majortimeo(req); 9250f9dc2b1STrond Myklebust /* Turn off autodisconnect */ 9260f9dc2b1STrond Myklebust del_singleshot_timer_sync(&xprt->timer); 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds } else if (!req->rq_bytes_sent) 9291da177e4SLinus Torvalds return; 9301da177e4SLinus Torvalds 931ff839970SChuck Lever req->rq_xtime = ktime_get(); 932a246b010SChuck Lever status = xprt->ops->send_request(task); 933c8485e4dSTrond Myklebust if (status != 0) { 934c8485e4dSTrond Myklebust task->tk_status = status; 935c8485e4dSTrond Myklebust return; 936c8485e4dSTrond Myklebust } 937c8485e4dSTrond Myklebust 93846121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 939468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 940fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 941262ca07dSChuck Lever 942fe3aca29SChuck Lever xprt->ops->set_retrans_timeout(task); 943262ca07dSChuck Lever 94415a45206SAndy Adamson numreqs = atomic_read(&xprt->num_reqs); 94515a45206SAndy Adamson if (numreqs > xprt->stat.max_slots) 94615a45206SAndy Adamson xprt->stat.max_slots = numreqs; 947262ca07dSChuck Lever xprt->stat.sends++; 948262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 949262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 95015a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 95115a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 952262ca07dSChuck Lever 953fe3aca29SChuck Lever /* Don't race with disconnect */ 954fe3aca29SChuck Lever if (!xprt_connected(xprt)) 955fe3aca29SChuck Lever task->tk_status = -ENOTCONN; 9560a660521STrond Myklebust else { 95755ae1aabSRicardo Labiaga /* 95855ae1aabSRicardo Labiaga * Sleep on the pending queue since 95955ae1aabSRicardo Labiaga * we're expecting a reply. 96055ae1aabSRicardo Labiaga */ 9610a660521STrond Myklebust if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) 9625d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 9630a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie; 96455ae1aabSRicardo Labiaga } 965fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 968ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 969ba60eb25STrond Myklebust { 970ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 971ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 972ba60eb25STrond Myklebust } 973ba60eb25STrond Myklebust 974ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 975ba60eb25STrond Myklebust { 976ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 977ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 978ba60eb25STrond Myklebust } 979ba60eb25STrond Myklebust 980ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 981ba60eb25STrond Myklebust { 982ba60eb25STrond Myklebust bool ret = false; 983ba60eb25STrond Myklebust 984ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 985ba60eb25STrond Myklebust goto out; 986ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 987ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 988ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 989ba60eb25STrond Myklebust ret = true; 990ba60eb25STrond Myklebust } 991ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 992ba60eb25STrond Myklebust out: 993ba60eb25STrond Myklebust return ret; 994ba60eb25STrond Myklebust } 995ba60eb25STrond Myklebust 996d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags) 997d9ba131dSTrond Myklebust { 998d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 999d9ba131dSTrond Myklebust 1000d9ba131dSTrond Myklebust if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs)) 1001d9ba131dSTrond Myklebust goto out; 1002d9ba131dSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), gfp_flags); 1003d9ba131dSTrond Myklebust if (req != NULL) 1004d9ba131dSTrond Myklebust goto out; 1005d9ba131dSTrond Myklebust atomic_dec(&xprt->num_reqs); 1006d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1007d9ba131dSTrond Myklebust out: 1008d9ba131dSTrond Myklebust return req; 1009d9ba131dSTrond Myklebust } 1010d9ba131dSTrond Myklebust 1011d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1012d9ba131dSTrond Myklebust { 1013d9ba131dSTrond Myklebust if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) { 1014d9ba131dSTrond Myklebust kfree(req); 1015d9ba131dSTrond Myklebust return true; 1016d9ba131dSTrond Myklebust } 1017d9ba131dSTrond Myklebust return false; 1018d9ba131dSTrond Myklebust } 1019d9ba131dSTrond Myklebust 1020f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 10211da177e4SLinus Torvalds { 1022d9ba131dSTrond Myklebust struct rpc_rqst *req; 10231da177e4SLinus Torvalds 1024f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 10251da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1026d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1027d9ba131dSTrond Myklebust list_del(&req->rq_list); 1028d9ba131dSTrond Myklebust goto out_init_req; 1029d9ba131dSTrond Myklebust } 10306b343099SJeff Layton req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN); 1031d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1032d9ba131dSTrond Myklebust goto out_init_req; 1033d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1034d9ba131dSTrond Myklebust case -ENOMEM: 1035d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1036d9ba131dSTrond Myklebust "failed! Retrying\n"); 10371afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1038d9ba131dSTrond Myklebust break; 1039d9ba131dSTrond Myklebust case -EAGAIN: 1040ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1041d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 10421afeaf5cSTrond Myklebust default: 1043d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 10441afeaf5cSTrond Myklebust } 1045f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1046d9ba131dSTrond Myklebust return; 1047d9ba131dSTrond Myklebust out_init_req: 1048d9ba131dSTrond Myklebust task->tk_status = 0; 10491da177e4SLinus Torvalds task->tk_rqstp = req; 10501da177e4SLinus Torvalds xprt_request_init(task, xprt); 1051f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 10521da177e4SLinus Torvalds } 1053f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1054f39c1bfbSTrond Myklebust 1055f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 1056f39c1bfbSTrond Myklebust { 1057f39c1bfbSTrond Myklebust /* Note: grabbing the xprt_lock_write() ensures that we throttle 1058f39c1bfbSTrond Myklebust * new slot allocation if the transport is congested (i.e. when 1059f39c1bfbSTrond Myklebust * reconnecting a stream transport or when out of socket write 1060f39c1bfbSTrond Myklebust * buffer space). 1061f39c1bfbSTrond Myklebust */ 1062f39c1bfbSTrond Myklebust if (xprt_lock_write(xprt, task)) { 1063f39c1bfbSTrond Myklebust xprt_alloc_slot(xprt, task); 1064f39c1bfbSTrond Myklebust xprt_release_write(xprt, task); 1065f39c1bfbSTrond Myklebust } 1066f39c1bfbSTrond Myklebust } 1067f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot); 10681da177e4SLinus Torvalds 1069ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1070ee5ebe85STrond Myklebust { 1071ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1072c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1073c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1074ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1075c25573b5STrond Myklebust } 1076ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1077ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1078ee5ebe85STrond Myklebust } 1079ee5ebe85STrond Myklebust 108021de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 108121de0a95STrond Myklebust { 108221de0a95STrond Myklebust struct rpc_rqst *req; 108321de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 108421de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 108521de0a95STrond Myklebust list_del(&req->rq_list); 108621de0a95STrond Myklebust kfree(req); 108721de0a95STrond Myklebust } 108821de0a95STrond Myklebust } 108921de0a95STrond Myklebust 1090d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1091d9ba131dSTrond Myklebust unsigned int num_prealloc, 1092d9ba131dSTrond Myklebust unsigned int max_alloc) 1093bd1722d4SPavel Emelyanov { 1094bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 109521de0a95STrond Myklebust struct rpc_rqst *req; 109621de0a95STrond Myklebust int i; 1097bd1722d4SPavel Emelyanov 1098bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1099bd1722d4SPavel Emelyanov if (xprt == NULL) 1100bd1722d4SPavel Emelyanov goto out; 1101bd1722d4SPavel Emelyanov 110221de0a95STrond Myklebust xprt_init(xprt, net); 110321de0a95STrond Myklebust 110421de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 110521de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 110621de0a95STrond Myklebust if (!req) 11078313164cSwangweidong goto out_free; 110821de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 110921de0a95STrond Myklebust } 1110d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1111d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1112d9ba131dSTrond Myklebust else 111321de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1114d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1115d9ba131dSTrond Myklebust atomic_set(&xprt->num_reqs, num_prealloc); 1116bd1722d4SPavel Emelyanov 1117bd1722d4SPavel Emelyanov return xprt; 1118bd1722d4SPavel Emelyanov 1119bd1722d4SPavel Emelyanov out_free: 112021de0a95STrond Myklebust xprt_free(xprt); 1121bd1722d4SPavel Emelyanov out: 1122bd1722d4SPavel Emelyanov return NULL; 1123bd1722d4SPavel Emelyanov } 1124bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1125bd1722d4SPavel Emelyanov 1126e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1127e204e621SPavel Emelyanov { 112837aa2133SPavel Emelyanov put_net(xprt->xprt_net); 112921de0a95STrond Myklebust xprt_free_all_slots(xprt); 1130e204e621SPavel Emelyanov kfree(xprt); 1131e204e621SPavel Emelyanov } 1132e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1133e204e621SPavel Emelyanov 11349903cd1cSChuck Lever /** 11359903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 11369903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 11379903cd1cSChuck Lever * 1138ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1139ba60eb25STrond Myklebust * slots are available, place the task on the transport's 11409903cd1cSChuck Lever * backlog queue. 11419903cd1cSChuck Lever */ 11429903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 11431da177e4SLinus Torvalds { 114445bc0dceSTrond Myklebust struct rpc_xprt *xprt; 11451da177e4SLinus Torvalds 114643cedbf0STrond Myklebust task->tk_status = 0; 114743cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 114843cedbf0STrond Myklebust return; 114943cedbf0STrond Myklebust 115043cedbf0STrond Myklebust task->tk_timeout = 0; 115143cedbf0STrond Myklebust task->tk_status = -EAGAIN; 115245bc0dceSTrond Myklebust rcu_read_lock(); 115345bc0dceSTrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1154ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 1155ba60eb25STrond Myklebust xprt->ops->alloc_slot(xprt, task); 1156ba60eb25STrond Myklebust rcu_read_unlock(); 1157ba60eb25STrond Myklebust } 1158ba60eb25STrond Myklebust 1159ba60eb25STrond Myklebust /** 1160ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1161ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1162ba60eb25STrond Myklebust * 1163ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1164ba60eb25STrond Myklebust * backlog queue. 1165ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1166ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1167ba60eb25STrond Myklebust */ 1168ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1169ba60eb25STrond Myklebust { 1170ba60eb25STrond Myklebust struct rpc_xprt *xprt; 1171ba60eb25STrond Myklebust 1172ba60eb25STrond Myklebust task->tk_status = 0; 1173ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1174ba60eb25STrond Myklebust return; 1175ba60eb25STrond Myklebust 1176ba60eb25STrond Myklebust task->tk_timeout = 0; 1177ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 1178ba60eb25STrond Myklebust rcu_read_lock(); 1179ba60eb25STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1180f39c1bfbSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 118145bc0dceSTrond Myklebust rcu_read_unlock(); 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 1184d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) 11851da177e4SLinus Torvalds { 11860eae88f3SEric Dumazet return (__force __be32)xprt->xid++; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt) 11901da177e4SLinus Torvalds { 1191bf3fcf89SChuck Lever xprt->xid = net_random(); 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11949903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 11971da177e4SLinus Torvalds 1198d9ba131dSTrond Myklebust INIT_LIST_HEAD(&req->rq_list); 1199ba7392bbSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 12001da177e4SLinus Torvalds req->rq_task = task; 12011da177e4SLinus Torvalds req->rq_xprt = xprt; 120202107148SChuck Lever req->rq_buffer = NULL; 12031da177e4SLinus Torvalds req->rq_xid = xprt_alloc_xid(xprt); 12040a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie - 1; 120592551948STrond Myklebust req->rq_bytes_sent = 0; 120692551948STrond Myklebust req->rq_snd_buf.len = 0; 120792551948STrond Myklebust req->rq_snd_buf.buflen = 0; 120892551948STrond Myklebust req->rq_rcv_buf.len = 0; 120992551948STrond Myklebust req->rq_rcv_buf.buflen = 0; 1210ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf = NULL; 1211da45828eSTrond Myklebust xprt_reset_majortimeo(req); 121246121cf7SChuck Lever dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 12131da177e4SLinus Torvalds req, ntohl(req->rq_xid)); 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12169903cd1cSChuck Lever /** 12179903cd1cSChuck Lever * xprt_release - release an RPC request slot 12189903cd1cSChuck Lever * @task: task which is finished with the slot 12199903cd1cSChuck Lever * 12201da177e4SLinus Torvalds */ 12219903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 12221da177e4SLinus Torvalds { 122355ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 122487ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12251da177e4SLinus Torvalds 122687ed5003STrond Myklebust if (req == NULL) { 122787ed5003STrond Myklebust if (task->tk_client) { 122887ed5003STrond Myklebust rcu_read_lock(); 122987ed5003STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 123087ed5003STrond Myklebust if (xprt->snd_task == task) 123187ed5003STrond Myklebust xprt_release_write(xprt, task); 123287ed5003STrond Myklebust rcu_read_unlock(); 123387ed5003STrond Myklebust } 12341da177e4SLinus Torvalds return; 123587ed5003STrond Myklebust } 123655ae1aabSRicardo Labiaga 123755ae1aabSRicardo Labiaga xprt = req->rq_xprt; 12380a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 12390a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 12400a702195SWeston Andros Adamson else if (task->tk_client) 12410a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 12424a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 124349e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1244a58dd398SChuck Lever if (xprt->ops->release_request) 1245a58dd398SChuck Lever xprt->ops->release_request(task); 12461da177e4SLinus Torvalds if (!list_empty(&req->rq_list)) 12471da177e4SLinus Torvalds list_del(&req->rq_list); 12481da177e4SLinus Torvalds xprt->last_used = jiffies; 12494cfc7e60SRahul Iyer if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) 1250a246b010SChuck Lever mod_timer(&xprt->timer, 125103bf4b70SChuck Lever xprt->last_used + xprt->idle_timeout); 12524a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1253ee5ebe85STrond Myklebust if (req->rq_buffer) 1254c5a4dd8bSChuck Lever xprt->ops->buf_free(req->rq_buffer); 1255a17c2153STrond Myklebust if (req->rq_cred != NULL) 1256a17c2153STrond Myklebust put_rpccred(req->rq_cred); 12571da177e4SLinus Torvalds task->tk_rqstp = NULL; 1258ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1259ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 126055ae1aabSRicardo Labiaga 126146121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1262ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1263ee5ebe85STrond Myklebust xprt_free_slot(xprt, req); 1264ee5ebe85STrond Myklebust else 1265c9acb42eSTrond Myklebust xprt_free_bc_request(req); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 126821de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1269c2866763SChuck Lever { 127021de0a95STrond Myklebust atomic_set(&xprt->count, 1); 1271c2866763SChuck Lever 1272c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1273c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 1274c2866763SChuck Lever 1275c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 1276c2866763SChuck Lever INIT_LIST_HEAD(&xprt->recv); 12779e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1278f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1279f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 12809e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1281f9acac1aSRicardo Labiaga 1282c2866763SChuck Lever xprt->last_used = jiffies; 1283c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1284a509050bSChuck Lever xprt->bind_index = 0; 1285c2866763SChuck Lever 1286c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1287c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 128834006ceeSTrond Myklebust rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending"); 1289c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1290c2866763SChuck Lever 1291c2866763SChuck Lever xprt_init_xid(xprt); 1292c2866763SChuck Lever 129321de0a95STrond Myklebust xprt->xprt_net = get_net(net); 12948d9266ffSTrond Myklebust } 12958d9266ffSTrond Myklebust 12968d9266ffSTrond Myklebust /** 12978d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 12988d9266ffSTrond Myklebust * @args: rpc transport creation arguments 12998d9266ffSTrond Myklebust * 13008d9266ffSTrond Myklebust */ 13018d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 13028d9266ffSTrond Myklebust { 13038d9266ffSTrond Myklebust struct rpc_xprt *xprt; 13048d9266ffSTrond Myklebust struct xprt_class *t; 13058d9266ffSTrond Myklebust 13068d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 13078d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 13088d9266ffSTrond Myklebust if (t->ident == args->ident) { 13098d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13108d9266ffSTrond Myklebust goto found; 13118d9266ffSTrond Myklebust } 13128d9266ffSTrond Myklebust } 13138d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13148d9266ffSTrond Myklebust printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident); 13158d9266ffSTrond Myklebust return ERR_PTR(-EIO); 13168d9266ffSTrond Myklebust 13178d9266ffSTrond Myklebust found: 13188d9266ffSTrond Myklebust xprt = t->setup(args); 13198d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 13208d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 13218d9266ffSTrond Myklebust -PTR_ERR(xprt)); 132221de0a95STrond Myklebust goto out; 13238d9266ffSTrond Myklebust } 132433d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 132533d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 132621de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 132721de0a95STrond Myklebust if (xprt_has_timer(xprt)) 132821de0a95STrond Myklebust setup_timer(&xprt->timer, xprt_init_autodisconnect, 132921de0a95STrond Myklebust (unsigned long)xprt); 133021de0a95STrond Myklebust else 133121de0a95STrond Myklebust init_timer(&xprt->timer); 13324e0038b6STrond Myklebust 13334e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 13344e0038b6STrond Myklebust xprt_destroy(xprt); 13354e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 13364e0038b6STrond Myklebust } 13374e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 13384e0038b6STrond Myklebust if (xprt->servername == NULL) { 13394e0038b6STrond Myklebust xprt_destroy(xprt); 13404e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 13414e0038b6STrond Myklebust } 13424e0038b6STrond Myklebust 1343c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1344c2866763SChuck Lever xprt->max_reqs); 134521de0a95STrond Myklebust out: 1346c2866763SChuck Lever return xprt; 1347c2866763SChuck Lever } 1348c2866763SChuck Lever 13499903cd1cSChuck Lever /** 13509903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1351a8de240aSTrond Myklebust * @xprt: transport to destroy 13529903cd1cSChuck Lever * 13531da177e4SLinus Torvalds */ 1354a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 13570065db32STrond Myklebust del_timer_sync(&xprt->timer); 1358c8541ecdSChuck Lever 1359f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1360f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1361f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1362f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1363c3ae62aeSJ. Bruce Fields cancel_work_sync(&xprt->task_cleanup); 13644e0038b6STrond Myklebust kfree(xprt->servername); 1365c8541ecdSChuck Lever /* 1366c8541ecdSChuck Lever * Tear down transport state and free the rpc_xprt 1367c8541ecdSChuck Lever */ 1368a246b010SChuck Lever xprt->ops->destroy(xprt); 13696b6ca86bSTrond Myklebust } 13701da177e4SLinus Torvalds 13716b6ca86bSTrond Myklebust /** 13726b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 13736b6ca86bSTrond Myklebust * @xprt: pointer to the transport 13746b6ca86bSTrond Myklebust * 13756b6ca86bSTrond Myklebust */ 13766b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 13776b6ca86bSTrond Myklebust { 1378a8de240aSTrond Myklebust if (atomic_dec_and_test(&xprt->count)) 1379a8de240aSTrond Myklebust xprt_destroy(xprt); 13806b6ca86bSTrond Myklebust } 13816b6ca86bSTrond Myklebust 13826b6ca86bSTrond Myklebust /** 13836b6ca86bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 13846b6ca86bSTrond Myklebust * @xprt: pointer to the transport 13856b6ca86bSTrond Myklebust * 13866b6ca86bSTrond Myklebust */ 13876b6ca86bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 13886b6ca86bSTrond Myklebust { 1389a8de240aSTrond Myklebust if (atomic_inc_not_zero(&xprt->count)) 13906b6ca86bSTrond Myklebust return xprt; 1391a8de240aSTrond Myklebust return NULL; 13921da177e4SLinus Torvalds } 1393