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 523705ad64SJeff Layton #include <trace/events/sunrpc.h> 533705ad64SJeff Layton 5455ae1aabSRicardo Labiaga #include "sunrpc.h" 5555ae1aabSRicardo Labiaga 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * Local variables 581da177e4SLinus Torvalds */ 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #ifdef RPC_DEBUG 611da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 621da177e4SLinus Torvalds #endif 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * Local functions 661da177e4SLinus Torvalds */ 6721de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 681da177e4SLinus Torvalds static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 691da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 701da177e4SLinus Torvalds static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 714e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 721da177e4SLinus Torvalds 735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7581c098afS\"Talpey, Thomas\ 7612a80469SChuck Lever /** 7781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 7881c098afS\"Talpey, Thomas\ * @transport: transport to register 7981c098afS\"Talpey, Thomas\ * 8081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 8181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 8281c098afS\"Talpey, Thomas\ * 8381c098afS\"Talpey, Thomas\ * Returns: 8481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 8581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 8681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 8781c098afS\"Talpey, Thomas\ */ 8881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 8981c098afS\"Talpey, Thomas\ { 9081c098afS\"Talpey, Thomas\ struct xprt_class *t; 9181c098afS\"Talpey, Thomas\ int result; 9281c098afS\"Talpey, Thomas\ 9381c098afS\"Talpey, Thomas\ result = -EEXIST; 9481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 9581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 9681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 974fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 9881c098afS\"Talpey, Thomas\ goto out; 9981c098afS\"Talpey, Thomas\ } 10081c098afS\"Talpey, Thomas\ 10181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 10281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 10381c098afS\"Talpey, Thomas\ transport->name); 10481c098afS\"Talpey, Thomas\ result = 0; 10581c098afS\"Talpey, Thomas\ 10681c098afS\"Talpey, Thomas\ out: 10781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 10881c098afS\"Talpey, Thomas\ return result; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 11181c098afS\"Talpey, Thomas\ 11281c098afS\"Talpey, Thomas\ /** 11381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 11465b6e42cSRandy Dunlap * @transport: transport to unregister 11581c098afS\"Talpey, Thomas\ * 11681c098afS\"Talpey, Thomas\ * Returns: 11781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 11881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 11981c098afS\"Talpey, Thomas\ */ 12081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 12181c098afS\"Talpey, Thomas\ { 12281c098afS\"Talpey, Thomas\ struct xprt_class *t; 12381c098afS\"Talpey, Thomas\ int result; 12481c098afS\"Talpey, Thomas\ 12581c098afS\"Talpey, Thomas\ result = 0; 12681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 12781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 12881c098afS\"Talpey, Thomas\ if (t == transport) { 12981c098afS\"Talpey, Thomas\ printk(KERN_INFO 13081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 13181c098afS\"Talpey, Thomas\ transport->name); 13281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 13381c098afS\"Talpey, Thomas\ goto out; 13481c098afS\"Talpey, Thomas\ } 13581c098afS\"Talpey, Thomas\ } 13681c098afS\"Talpey, Thomas\ result = -ENOENT; 13781c098afS\"Talpey, Thomas\ 13881c098afS\"Talpey, Thomas\ out: 13981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 14081c098afS\"Talpey, Thomas\ return result; 14181c098afS\"Talpey, Thomas\ } 14281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 14381c098afS\"Talpey, Thomas\ 14481c098afS\"Talpey, Thomas\ /** 145441e3e24STom Talpey * xprt_load_transport - load a transport implementation 146441e3e24STom Talpey * @transport_name: transport to load 147441e3e24STom Talpey * 148441e3e24STom Talpey * Returns: 149441e3e24STom Talpey * 0: transport successfully loaded 150441e3e24STom Talpey * -ENOENT: transport module not available 151441e3e24STom Talpey */ 152441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 153441e3e24STom Talpey { 154441e3e24STom Talpey struct xprt_class *t; 155441e3e24STom Talpey int result; 156441e3e24STom Talpey 157441e3e24STom Talpey result = 0; 158441e3e24STom Talpey spin_lock(&xprt_list_lock); 159441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 160441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 161441e3e24STom Talpey spin_unlock(&xprt_list_lock); 162441e3e24STom Talpey goto out; 163441e3e24STom Talpey } 164441e3e24STom Talpey } 165441e3e24STom Talpey spin_unlock(&xprt_list_lock); 166ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 167441e3e24STom Talpey out: 168441e3e24STom Talpey return result; 169441e3e24STom Talpey } 170441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 171441e3e24STom Talpey 172441e3e24STom Talpey /** 17312a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 17412a80469SChuck Lever * @task: task that is requesting access to the transport 175177c27bfSRandy Dunlap * @xprt: pointer to the target transport 17612a80469SChuck Lever * 17712a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 17812a80469SChuck Lever * transport connects from colliding with writes. No congestion control 17912a80469SChuck Lever * is provided. 1801da177e4SLinus Torvalds */ 18143cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1821da177e4SLinus Torvalds { 18312a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 18434006ceeSTrond Myklebust int priority; 18512a80469SChuck Lever 18612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 18712a80469SChuck Lever if (task == xprt->snd_task) 18812a80469SChuck Lever return 1; 18912a80469SChuck Lever goto out_sleep; 19012a80469SChuck Lever } 19112a80469SChuck Lever xprt->snd_task = task; 19292551948STrond Myklebust if (req != NULL) 19312a80469SChuck Lever req->rq_ntrans++; 1944d4a76f3Sj223yang@asset.uwaterloo.ca 19512a80469SChuck Lever return 1; 19612a80469SChuck Lever 19712a80469SChuck Lever out_sleep: 19846121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 19912a80469SChuck Lever task->tk_pid, xprt); 20012a80469SChuck Lever task->tk_timeout = 0; 20112a80469SChuck Lever task->tk_status = -EAGAIN; 20234006ceeSTrond Myklebust if (req == NULL) 20334006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 20434006ceeSTrond Myklebust else if (!req->rq_ntrans) 20534006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 20612a80469SChuck Lever else 20734006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 20834006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 20912a80469SChuck Lever return 0; 21012a80469SChuck Lever } 21112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 21212a80469SChuck Lever 213632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 214632e3bdcSTrond Myklebust { 215632e3bdcSTrond Myklebust xprt->snd_task = NULL; 216d19751e7STrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 2174e857c58SPeter Zijlstra smp_mb__before_atomic(); 218632e3bdcSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 2194e857c58SPeter Zijlstra smp_mb__after_atomic(); 220632e3bdcSTrond Myklebust } else 221c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 222632e3bdcSTrond Myklebust } 223632e3bdcSTrond Myklebust 22412a80469SChuck Lever /* 22512a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 22612a80469SChuck Lever * @task: task that is requesting access to the transport 22712a80469SChuck Lever * 22812a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 22912a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 23012a80469SChuck Lever * woken up and given access to the transport. 23112a80469SChuck Lever */ 23243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 23312a80469SChuck Lever { 2341da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 23534006ceeSTrond Myklebust int priority; 2361da177e4SLinus Torvalds 2372226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2381da177e4SLinus Torvalds if (task == xprt->snd_task) 2391da177e4SLinus Torvalds return 1; 2401da177e4SLinus Torvalds goto out_sleep; 2411da177e4SLinus Torvalds } 24243cedbf0STrond Myklebust if (req == NULL) { 24343cedbf0STrond Myklebust xprt->snd_task = task; 24443cedbf0STrond Myklebust return 1; 24543cedbf0STrond Myklebust } 24612a80469SChuck Lever if (__xprt_get_cong(xprt, task)) { 2471da177e4SLinus Torvalds xprt->snd_task = task; 2481da177e4SLinus Torvalds req->rq_ntrans++; 2491da177e4SLinus Torvalds return 1; 2501da177e4SLinus Torvalds } 251632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2521da177e4SLinus Torvalds out_sleep: 25346121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2541da177e4SLinus Torvalds task->tk_timeout = 0; 2551da177e4SLinus Torvalds task->tk_status = -EAGAIN; 25634006ceeSTrond Myklebust if (req == NULL) 25734006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 25834006ceeSTrond Myklebust else if (!req->rq_ntrans) 25934006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 2601da177e4SLinus Torvalds else 26134006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 26234006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 2631da177e4SLinus Torvalds return 0; 2641da177e4SLinus Torvalds } 26512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2661da177e4SLinus Torvalds 26712a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds int retval; 2701da177e4SLinus Torvalds 2714a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 27243cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2734a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2741da177e4SLinus Torvalds return retval; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 277961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2781da177e4SLinus Torvalds { 279961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 28049e9a890SChuck Lever struct rpc_rqst *req; 28149e9a890SChuck Lever 28249e9a890SChuck Lever req = task->tk_rqstp; 28349e9a890SChuck Lever xprt->snd_task = task; 28492551948STrond Myklebust if (req) 28549e9a890SChuck Lever req->rq_ntrans++; 286961a828dSTrond Myklebust return true; 287961a828dSTrond Myklebust } 288961a828dSTrond Myklebust 289961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 290961a828dSTrond Myklebust { 291961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 29249e9a890SChuck Lever return; 29349e9a890SChuck Lever 294961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt)) 295961a828dSTrond Myklebust return; 296632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 29749e9a890SChuck Lever } 29849e9a890SChuck Lever 299961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data) 30049e9a890SChuck Lever { 301961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 30243cedbf0STrond Myklebust struct rpc_rqst *req; 3031da177e4SLinus Torvalds 30443cedbf0STrond Myklebust req = task->tk_rqstp; 30543cedbf0STrond Myklebust if (req == NULL) { 3061da177e4SLinus Torvalds xprt->snd_task = task; 307961a828dSTrond Myklebust return true; 30843cedbf0STrond Myklebust } 30943cedbf0STrond Myklebust if (__xprt_get_cong(xprt, task)) { 31043cedbf0STrond Myklebust xprt->snd_task = task; 3111da177e4SLinus Torvalds req->rq_ntrans++; 312961a828dSTrond Myklebust return true; 3131da177e4SLinus Torvalds } 314961a828dSTrond Myklebust return false; 315961a828dSTrond Myklebust } 316961a828dSTrond Myklebust 317961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 318961a828dSTrond Myklebust { 319961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 320961a828dSTrond Myklebust return; 321961a828dSTrond Myklebust if (RPCXPRT_CONGESTED(xprt)) 322961a828dSTrond Myklebust goto out_unlock; 323961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt)) 324961a828dSTrond Myklebust return; 3251da177e4SLinus Torvalds out_unlock: 326632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 32949e9a890SChuck Lever /** 33049e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 33149e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 33249e9a890SChuck Lever * @task: task that is releasing access to the transport 33349e9a890SChuck Lever * 33449e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3351da177e4SLinus Torvalds */ 33649e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds if (xprt->snd_task == task) { 339ee071effSTrond Myklebust if (task != NULL) { 340ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 341ee071effSTrond Myklebust if (req != NULL) 342ee071effSTrond Myklebust req->rq_bytes_sent = 0; 343ee071effSTrond Myklebust } 344632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3451da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds } 34812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3491da177e4SLinus Torvalds 35049e9a890SChuck Lever /** 35149e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 35249e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 35349e9a890SChuck Lever * @task: task that is releasing access to the transport 35449e9a890SChuck Lever * 35549e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 35649e9a890SChuck Lever * transport if the transport's congestion window allows it. 35749e9a890SChuck Lever */ 35849e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 35949e9a890SChuck Lever { 36049e9a890SChuck Lever if (xprt->snd_task == task) { 361ee071effSTrond Myklebust if (task != NULL) { 362ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 363ee071effSTrond Myklebust if (req != NULL) 364ee071effSTrond Myklebust req->rq_bytes_sent = 0; 365ee071effSTrond Myklebust } 366632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 36749e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 36849e9a890SChuck Lever } 36949e9a890SChuck Lever } 37012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 37149e9a890SChuck Lever 37249e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3731da177e4SLinus Torvalds { 3744a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 37549e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3764a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3811da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds static int 3841da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (req->rq_cong) 3891da177e4SLinus Torvalds return 1; 39046121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 3911da177e4SLinus Torvalds task->tk_pid, xprt->cong, xprt->cwnd); 3921da177e4SLinus Torvalds if (RPCXPRT_CONGESTED(xprt)) 3931da177e4SLinus Torvalds return 0; 3941da177e4SLinus Torvalds req->rq_cong = 1; 3951da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 3961da177e4SLinus Torvalds return 1; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4011da177e4SLinus Torvalds * that has been sleeping due to congestion 4021da177e4SLinus Torvalds */ 4031da177e4SLinus Torvalds static void 4041da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds if (!req->rq_cong) 4071da177e4SLinus Torvalds return; 4081da177e4SLinus Torvalds req->rq_cong = 0; 4091da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 41049e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 41346c0ee8bSChuck Lever /** 414a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 415a58dd398SChuck Lever * @task: RPC request that recently completed 416a58dd398SChuck Lever * 417a58dd398SChuck Lever * Useful for transports that require congestion control. 418a58dd398SChuck Lever */ 419a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 420a58dd398SChuck Lever { 421a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 422a4f0835cSTrond Myklebust 423a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 424a58dd398SChuck Lever } 42512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 426a58dd398SChuck Lever 427a58dd398SChuck Lever /** 42846c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4296a24dfb6STrond Myklebust * @xprt: pointer to xprt 43046c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 43146c0ee8bSChuck Lever * @result: result code of completed RPC request 43246c0ee8bSChuck Lever * 4334f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4344f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4354f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4364f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4374f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4384f4cf5adSChuck Lever * 4394f4cf5adSChuck Lever * - a reply is received and 4404f4cf5adSChuck Lever * - a full number of requests are outstanding and 4414f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4421da177e4SLinus Torvalds */ 4436a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4441da177e4SLinus Torvalds { 44546c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 44646c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4491da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4501da177e4SLinus Torvalds * the result gets rounded properly. */ 4511da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4521da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4531da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 45449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4551da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4561da177e4SLinus Torvalds cwnd >>= 1; 4571da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4581da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4611da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4621da177e4SLinus Torvalds xprt->cwnd = cwnd; 46346c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4641da177e4SLinus Torvalds } 46512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4661da177e4SLinus Torvalds 46744fbac22SChuck Lever /** 46844fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 46944fbac22SChuck Lever * @xprt: transport with waiting tasks 47044fbac22SChuck Lever * @status: result code to plant in each task before waking it 47144fbac22SChuck Lever * 47244fbac22SChuck Lever */ 47344fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 47444fbac22SChuck Lever { 47544fbac22SChuck Lever if (status < 0) 47644fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 47744fbac22SChuck Lever else 47844fbac22SChuck Lever rpc_wake_up(&xprt->pending); 47944fbac22SChuck Lever } 48012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 48144fbac22SChuck Lever 482c7b2cae8SChuck Lever /** 483c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 484c7b2cae8SChuck Lever * @task: task to be put to sleep 4850b80ae42SRandy Dunlap * @action: function pointer to be executed after wait 486a9a6b52eSTrond Myklebust * 487a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 488a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 489a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 490c7b2cae8SChuck Lever */ 491b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) 492c7b2cae8SChuck Lever { 493c7b2cae8SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 494c7b2cae8SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 495c7b2cae8SChuck Lever 496a9a6b52eSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 497b6ddf64fSTrond Myklebust rpc_sleep_on(&xprt->pending, task, action); 498c7b2cae8SChuck Lever } 49912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 500c7b2cae8SChuck Lever 501c7b2cae8SChuck Lever /** 502c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 503c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 504c7b2cae8SChuck Lever * 505c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 506c7b2cae8SChuck Lever */ 507c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt) 508c7b2cae8SChuck Lever { 509c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 510c7b2cae8SChuck Lever if (xprt->snd_task) { 51146121cf7SChuck Lever dprintk("RPC: write space: waking waiting task on " 51246121cf7SChuck Lever "xprt %p\n", xprt); 513fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); 514c7b2cae8SChuck Lever } 515c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 516c7b2cae8SChuck Lever } 51712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 518c7b2cae8SChuck Lever 519fe3aca29SChuck Lever /** 520fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 521fe3aca29SChuck Lever * @task: task whose timeout is to be set 522fe3aca29SChuck Lever * 523fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 524fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 525fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 526fe3aca29SChuck Lever */ 527fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 528fe3aca29SChuck Lever { 529fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 530fe3aca29SChuck Lever } 53112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 532fe3aca29SChuck Lever 5332c53040fSBen Hutchings /** 534fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 535fe3aca29SChuck Lever * @task: task whose timeout is to be set 536fe3aca29SChuck Lever * 537fe3aca29SChuck Lever * Set a request's retransmit timeout using the RTT estimator. 538fe3aca29SChuck Lever */ 539fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 540fe3aca29SChuck Lever { 541fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 542ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 543ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 544fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 545ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 546fe3aca29SChuck Lever 547fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 548fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 549fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 550fe3aca29SChuck Lever task->tk_timeout = max_timeout; 551fe3aca29SChuck Lever } 55212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 553fe3aca29SChuck Lever 5541da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5551da177e4SLinus Torvalds { 556ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5591da177e4SLinus Torvalds if (to->to_exponential) 5601da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5611da177e4SLinus Torvalds else 5621da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5631da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5641da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5651da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5689903cd1cSChuck Lever /** 5699903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5709903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5719903cd1cSChuck Lever * 5721da177e4SLinus Torvalds */ 5731da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 576ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5771da177e4SLinus Torvalds int status = 0; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5801da177e4SLinus Torvalds if (to->to_exponential) 5811da177e4SLinus Torvalds req->rq_timeout <<= 1; 5821da177e4SLinus Torvalds else 5831da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5841da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5851da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5861da177e4SLinus Torvalds req->rq_retries++; 5871da177e4SLinus Torvalds } else { 5881da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5891da177e4SLinus Torvalds req->rq_retries = 0; 5901da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5911da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 5924a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 5931da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 5944a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5951da177e4SLinus Torvalds status = -ETIMEDOUT; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (req->rq_timeout == 0) { 5991da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6001da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds return status; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 60565f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6061da177e4SLinus Torvalds { 60765f27f38SDavid Howells struct rpc_xprt *xprt = 60865f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6091da177e4SLinus Torvalds 610a246b010SChuck Lever xprt->ops->close(xprt); 61166af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6121da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6159903cd1cSChuck Lever /** 61662da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6179903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6189903cd1cSChuck Lever * 6191da177e4SLinus Torvalds */ 62062da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6234a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6241da177e4SLinus Torvalds xprt_clear_connected(xprt); 6252a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6264a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6271da177e4SLinus Torvalds } 62862da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6291da177e4SLinus Torvalds 63066af1e55STrond Myklebust /** 63166af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 63266af1e55STrond Myklebust * @xprt: transport to disconnect 63366af1e55STrond Myklebust * 63466af1e55STrond Myklebust */ 63566af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 63666af1e55STrond Myklebust { 63766af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 63866af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 63966af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 64066af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 64166af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 64266af1e55STrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6432a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 64466af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 64566af1e55STrond Myklebust } 64666af1e55STrond Myklebust 6477c1d71cfSTrond Myklebust /** 6487c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6497c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6507c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6517c1d71cfSTrond Myklebust * 6527c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6537c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6547c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6557c1d71cfSTrond Myklebust * a batch of RPC requests. 6567c1d71cfSTrond Myklebust * 6577c1d71cfSTrond Myklebust */ 6587c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6597c1d71cfSTrond Myklebust { 6607c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6617c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6627c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6637c1d71cfSTrond Myklebust goto out; 6647c1d71cfSTrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) 6657c1d71cfSTrond Myklebust goto out; 6667c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6677c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6687c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 6697c1d71cfSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6702a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6717c1d71cfSTrond Myklebust out: 6727c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6737c1d71cfSTrond Myklebust } 6747c1d71cfSTrond Myklebust 6751da177e4SLinus Torvalds static void 6761da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct rpc_xprt *xprt = (struct rpc_xprt *)data; 6791da177e4SLinus Torvalds 6804a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 681d19751e7STrond Myklebust if (!list_empty(&xprt->recv)) 6821da177e4SLinus Torvalds goto out_abort; 6832226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 6841da177e4SLinus Torvalds goto out_abort; 6854a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 686f75e6745STrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 687c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6881da177e4SLinus Torvalds return; 6891da177e4SLinus Torvalds out_abort: 6904a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 6939903cd1cSChuck Lever /** 6949903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 6959903cd1cSChuck Lever * @task: RPC task that is requesting the connect 6961da177e4SLinus Torvalds * 6971da177e4SLinus Torvalds */ 6981da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 6991da177e4SLinus Torvalds { 700ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7011da177e4SLinus Torvalds 70246121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 7031da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 7041da177e4SLinus Torvalds 705ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 70601d37c42STrond Myklebust task->tk_status = -EAGAIN; 7071da177e4SLinus Torvalds return; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7101da177e4SLinus Torvalds return; 711feb8ca37STrond Myklebust 712feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 713feb8ca37STrond Myklebust xprt->ops->close(xprt); 714feb8ca37STrond Myklebust 7151da177e4SLinus Torvalds if (xprt_connected(xprt)) 716a246b010SChuck Lever xprt_release_write(xprt, task); 717a246b010SChuck Lever else { 7181da177e4SLinus Torvalds task->tk_rqstp->rq_bytes_sent = 0; 719a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7205d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 7210b9e7943STrond Myklebust 7220b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7230b9e7943STrond Myklebust return; 7240b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7250b9e7943STrond Myklebust return; 726262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 7271b092092STrond Myklebust xprt->ops->connect(xprt, task); 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7319903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 7321da177e4SLinus Torvalds { 733ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7341da177e4SLinus Torvalds 735cd983ef8SChuck Lever if (task->tk_status == 0) { 736262ca07dSChuck Lever xprt->stat.connect_count++; 737262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 73846121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 7391da177e4SLinus Torvalds task->tk_pid); 7401da177e4SLinus Torvalds return; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds switch (task->tk_status) { 7440fe8d04eSTrond Myklebust case -ECONNREFUSED: 7450fe8d04eSTrond Myklebust case -ECONNRESET: 7460fe8d04eSTrond Myklebust case -ECONNABORTED: 7470fe8d04eSTrond Myklebust case -ENETUNREACH: 7480fe8d04eSTrond Myklebust case -EHOSTUNREACH: 7492fc193cfSTrond Myklebust case -EPIPE: 7502a491991STrond Myklebust case -EAGAIN: 7512a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 75223475d66SChuck Lever break; 7531da177e4SLinus Torvalds case -ETIMEDOUT: 75446121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 75546121cf7SChuck Lever "out\n", task->tk_pid); 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds default: 75846121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 75946121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 7604e0038b6STrond Myklebust xprt->servername); 7611da177e4SLinus Torvalds xprt_release_write(xprt, task); 76223475d66SChuck Lever task->tk_status = -EIO; 76323475d66SChuck Lever } 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7669903cd1cSChuck Lever /** 7679903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 7689903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 7699903cd1cSChuck Lever * @xid: RPC XID of incoming reply 7709903cd1cSChuck Lever * 7711da177e4SLinus Torvalds */ 772d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 7731da177e4SLinus Torvalds { 7748f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 7751da177e4SLinus Torvalds 7768f3a6de3SPavel Emelyanov list_for_each_entry(entry, &xprt->recv, rq_list) 7773705ad64SJeff Layton if (entry->rq_xid == xid) { 7783705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 779262ca07dSChuck Lever return entry; 7803705ad64SJeff Layton } 78146121cf7SChuck Lever 78246121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 78346121cf7SChuck Lever ntohl(xid)); 7843705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 785262ca07dSChuck Lever xprt->stat.bad_xids++; 786262ca07dSChuck Lever return NULL; 7871da177e4SLinus Torvalds } 78812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 7891da177e4SLinus Torvalds 790bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task) 7911da177e4SLinus Torvalds { 7921570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 7931570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 79495c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 795d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 7961570c1e4SChuck Lever 7971da177e4SLinus Torvalds if (timer) { 7981da177e4SLinus Torvalds if (req->rq_ntrans == 1) 799ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 8001570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041570c1e4SChuck Lever /** 8051570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 8061570c1e4SChuck Lever * @task: RPC request that recently completed 8071570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 8081570c1e4SChuck Lever * 8091570c1e4SChuck Lever * Caller holds transport lock. 8101570c1e4SChuck Lever */ 8111570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 8121570c1e4SChuck Lever { 8131570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 814fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 8151da177e4SLinus Torvalds 8161570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 8171570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 8183705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 8191da177e4SLinus Torvalds 820fda13939STrond Myklebust xprt->stat.recvs++; 821d60dbb20STrond Myklebust req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); 822bbc72ceaSChuck Lever if (xprt->ops->timer != NULL) 823bbc72ceaSChuck Lever xprt_update_rtt(task); 824ef759a2eSChuck Lever 8251da177e4SLinus Torvalds list_del_init(&req->rq_list); 8261e799b67STrond Myklebust req->rq_private_buf.len = copied; 827dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 828dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 82943ac3f29STrond Myklebust smp_wmb(); 830dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 831fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8321da177e4SLinus Torvalds } 83312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 8341da177e4SLinus Torvalds 83546c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8381da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 8391da177e4SLinus Torvalds 8405d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 8415d00837bSTrond Myklebust return; 84246121cf7SChuck Lever dprintk("RPC: %5u xprt_timer\n", task->tk_pid); 84346c0ee8bSChuck Lever 8445d00837bSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 845dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 84646c0ee8bSChuck Lever if (xprt->ops->timer) 8476a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 8485d00837bSTrond Myklebust } else 8495d00837bSTrond Myklebust task->tk_status = 0; 8505d00837bSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8534cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt) 8544cfc7e60SRahul Iyer { 8554cfc7e60SRahul Iyer return xprt->idle_timeout != 0; 8564cfc7e60SRahul Iyer } 8574cfc7e60SRahul Iyer 8589903cd1cSChuck Lever /** 8599903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 8609903cd1cSChuck Lever * @task: RPC task about to send a request 8619903cd1cSChuck Lever * 8621da177e4SLinus Torvalds */ 86390051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8661da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 86790051ea7STrond Myklebust bool ret = false; 8681da177e4SLinus Torvalds 86946121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 8701da177e4SLinus Torvalds 8714a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 8728a19a0b6STrond Myklebust if (!req->rq_bytes_sent) { 8738a19a0b6STrond Myklebust if (req->rq_reply_bytes_recvd) { 87490051ea7STrond Myklebust task->tk_status = req->rq_reply_bytes_recvd; 8751da177e4SLinus Torvalds goto out_unlock; 8761da177e4SLinus Torvalds } 8778a19a0b6STrond Myklebust if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) 8788a19a0b6STrond Myklebust && xprt_connected(xprt) 8798a19a0b6STrond Myklebust && req->rq_connect_cookie == xprt->connect_cookie) { 8808a19a0b6STrond Myklebust xprt->ops->set_retrans_timeout(task); 8818a19a0b6STrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 8828a19a0b6STrond Myklebust goto out_unlock; 8838a19a0b6STrond Myklebust } 8848a19a0b6STrond Myklebust } 88590051ea7STrond Myklebust if (!xprt->ops->reserve_xprt(xprt, task)) { 88690051ea7STrond Myklebust task->tk_status = -EAGAIN; 88790051ea7STrond Myklebust goto out_unlock; 88890051ea7STrond Myklebust } 88990051ea7STrond Myklebust ret = true; 8901da177e4SLinus Torvalds out_unlock: 8914a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 89290051ea7STrond Myklebust return ret; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 895e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 8965e5ce5beSTrond Myklebust { 897343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 8985e5ce5beSTrond Myklebust } 8995e5ce5beSTrond Myklebust 9009903cd1cSChuck Lever /** 9019903cd1cSChuck Lever * xprt_transmit - send an RPC request on a transport 9029903cd1cSChuck Lever * @task: controlling RPC task 9039903cd1cSChuck Lever * 9049903cd1cSChuck Lever * We have to copy the iovec because sendmsg fiddles with its contents. 9059903cd1cSChuck Lever */ 9069903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task) 9071da177e4SLinus Torvalds { 9081da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 9091da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 91015a45206SAndy Adamson int status, numreqs; 9111da177e4SLinus Torvalds 91246121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 9131da177e4SLinus Torvalds 914dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 91555ae1aabSRicardo Labiaga if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { 91655ae1aabSRicardo Labiaga /* 91755ae1aabSRicardo Labiaga * Add to the list only if we're expecting a reply 91855ae1aabSRicardo Labiaga */ 9194a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 9201da177e4SLinus Torvalds /* Update the softirq receive buffer */ 9211da177e4SLinus Torvalds memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 9221da177e4SLinus Torvalds sizeof(req->rq_private_buf)); 9231da177e4SLinus Torvalds /* Add request to the receive list */ 9241da177e4SLinus Torvalds list_add_tail(&req->rq_list, &xprt->recv); 9254a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9261da177e4SLinus Torvalds xprt_reset_majortimeo(req); 9270f9dc2b1STrond Myklebust /* Turn off autodisconnect */ 9280f9dc2b1STrond Myklebust del_singleshot_timer_sync(&xprt->timer); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds } else if (!req->rq_bytes_sent) 9311da177e4SLinus Torvalds return; 9321da177e4SLinus Torvalds 933ff839970SChuck Lever req->rq_xtime = ktime_get(); 934a246b010SChuck Lever status = xprt->ops->send_request(task); 9353705ad64SJeff Layton trace_xprt_transmit(xprt, req->rq_xid, status); 936c8485e4dSTrond Myklebust if (status != 0) { 937c8485e4dSTrond Myklebust task->tk_status = status; 938c8485e4dSTrond Myklebust return; 939c8485e4dSTrond Myklebust } 940c8485e4dSTrond Myklebust 94146121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 942468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 943fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 944262ca07dSChuck Lever 945fe3aca29SChuck Lever xprt->ops->set_retrans_timeout(task); 946262ca07dSChuck Lever 94715a45206SAndy Adamson numreqs = atomic_read(&xprt->num_reqs); 94815a45206SAndy Adamson if (numreqs > xprt->stat.max_slots) 94915a45206SAndy Adamson xprt->stat.max_slots = numreqs; 950262ca07dSChuck Lever xprt->stat.sends++; 951262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 952262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 95315a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 95415a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 955262ca07dSChuck Lever 956fe3aca29SChuck Lever /* Don't race with disconnect */ 957fe3aca29SChuck Lever if (!xprt_connected(xprt)) 958fe3aca29SChuck Lever task->tk_status = -ENOTCONN; 9590a660521STrond Myklebust else { 96055ae1aabSRicardo Labiaga /* 96155ae1aabSRicardo Labiaga * Sleep on the pending queue since 96255ae1aabSRicardo Labiaga * we're expecting a reply. 96355ae1aabSRicardo Labiaga */ 9640a660521STrond Myklebust if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) 9655d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 9660a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie; 96755ae1aabSRicardo Labiaga } 968fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 971ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 972ba60eb25STrond Myklebust { 973ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 974ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 975ba60eb25STrond Myklebust } 976ba60eb25STrond Myklebust 977ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 978ba60eb25STrond Myklebust { 979ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 980ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 981ba60eb25STrond Myklebust } 982ba60eb25STrond Myklebust 983ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 984ba60eb25STrond Myklebust { 985ba60eb25STrond Myklebust bool ret = false; 986ba60eb25STrond Myklebust 987ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 988ba60eb25STrond Myklebust goto out; 989ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 990ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 991ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 992ba60eb25STrond Myklebust ret = true; 993ba60eb25STrond Myklebust } 994ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 995ba60eb25STrond Myklebust out: 996ba60eb25STrond Myklebust return ret; 997ba60eb25STrond Myklebust } 998ba60eb25STrond Myklebust 999d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags) 1000d9ba131dSTrond Myklebust { 1001d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1002d9ba131dSTrond Myklebust 1003d9ba131dSTrond Myklebust if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs)) 1004d9ba131dSTrond Myklebust goto out; 1005d9ba131dSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), gfp_flags); 1006d9ba131dSTrond Myklebust if (req != NULL) 1007d9ba131dSTrond Myklebust goto out; 1008d9ba131dSTrond Myklebust atomic_dec(&xprt->num_reqs); 1009d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1010d9ba131dSTrond Myklebust out: 1011d9ba131dSTrond Myklebust return req; 1012d9ba131dSTrond Myklebust } 1013d9ba131dSTrond Myklebust 1014d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1015d9ba131dSTrond Myklebust { 1016d9ba131dSTrond Myklebust if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) { 1017d9ba131dSTrond Myklebust kfree(req); 1018d9ba131dSTrond Myklebust return true; 1019d9ba131dSTrond Myklebust } 1020d9ba131dSTrond Myklebust return false; 1021d9ba131dSTrond Myklebust } 1022d9ba131dSTrond Myklebust 1023f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 10241da177e4SLinus Torvalds { 1025d9ba131dSTrond Myklebust struct rpc_rqst *req; 10261da177e4SLinus Torvalds 1027f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 10281da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1029d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1030d9ba131dSTrond Myklebust list_del(&req->rq_list); 1031d9ba131dSTrond Myklebust goto out_init_req; 1032d9ba131dSTrond Myklebust } 10336b343099SJeff Layton req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN); 1034d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1035d9ba131dSTrond Myklebust goto out_init_req; 1036d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1037d9ba131dSTrond Myklebust case -ENOMEM: 1038d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1039d9ba131dSTrond Myklebust "failed! Retrying\n"); 10401afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1041d9ba131dSTrond Myklebust break; 1042d9ba131dSTrond Myklebust case -EAGAIN: 1043ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1044d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 10451afeaf5cSTrond Myklebust default: 1046d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 10471afeaf5cSTrond Myklebust } 1048f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1049d9ba131dSTrond Myklebust return; 1050d9ba131dSTrond Myklebust out_init_req: 1051d9ba131dSTrond Myklebust task->tk_status = 0; 10521da177e4SLinus Torvalds task->tk_rqstp = req; 10531da177e4SLinus Torvalds xprt_request_init(task, xprt); 1054f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 10551da177e4SLinus Torvalds } 1056f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1057f39c1bfbSTrond Myklebust 1058f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 1059f39c1bfbSTrond Myklebust { 1060f39c1bfbSTrond Myklebust /* Note: grabbing the xprt_lock_write() ensures that we throttle 1061f39c1bfbSTrond Myklebust * new slot allocation if the transport is congested (i.e. when 1062f39c1bfbSTrond Myklebust * reconnecting a stream transport or when out of socket write 1063f39c1bfbSTrond Myklebust * buffer space). 1064f39c1bfbSTrond Myklebust */ 1065f39c1bfbSTrond Myklebust if (xprt_lock_write(xprt, task)) { 1066f39c1bfbSTrond Myklebust xprt_alloc_slot(xprt, task); 1067f39c1bfbSTrond Myklebust xprt_release_write(xprt, task); 1068f39c1bfbSTrond Myklebust } 1069f39c1bfbSTrond Myklebust } 1070f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot); 10711da177e4SLinus Torvalds 1072ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1073ee5ebe85STrond Myklebust { 1074ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1075c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1076c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1077ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1078c25573b5STrond Myklebust } 1079ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1080ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1081ee5ebe85STrond Myklebust } 1082ee5ebe85STrond Myklebust 108321de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 108421de0a95STrond Myklebust { 108521de0a95STrond Myklebust struct rpc_rqst *req; 108621de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 108721de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 108821de0a95STrond Myklebust list_del(&req->rq_list); 108921de0a95STrond Myklebust kfree(req); 109021de0a95STrond Myklebust } 109121de0a95STrond Myklebust } 109221de0a95STrond Myklebust 1093d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1094d9ba131dSTrond Myklebust unsigned int num_prealloc, 1095d9ba131dSTrond Myklebust unsigned int max_alloc) 1096bd1722d4SPavel Emelyanov { 1097bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 109821de0a95STrond Myklebust struct rpc_rqst *req; 109921de0a95STrond Myklebust int i; 1100bd1722d4SPavel Emelyanov 1101bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1102bd1722d4SPavel Emelyanov if (xprt == NULL) 1103bd1722d4SPavel Emelyanov goto out; 1104bd1722d4SPavel Emelyanov 110521de0a95STrond Myklebust xprt_init(xprt, net); 110621de0a95STrond Myklebust 110721de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 110821de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 110921de0a95STrond Myklebust if (!req) 11108313164cSwangweidong goto out_free; 111121de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 111221de0a95STrond Myklebust } 1113d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1114d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1115d9ba131dSTrond Myklebust else 111621de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1117d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1118d9ba131dSTrond Myklebust atomic_set(&xprt->num_reqs, num_prealloc); 1119bd1722d4SPavel Emelyanov 1120bd1722d4SPavel Emelyanov return xprt; 1121bd1722d4SPavel Emelyanov 1122bd1722d4SPavel Emelyanov out_free: 112321de0a95STrond Myklebust xprt_free(xprt); 1124bd1722d4SPavel Emelyanov out: 1125bd1722d4SPavel Emelyanov return NULL; 1126bd1722d4SPavel Emelyanov } 1127bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1128bd1722d4SPavel Emelyanov 1129e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1130e204e621SPavel Emelyanov { 113137aa2133SPavel Emelyanov put_net(xprt->xprt_net); 113221de0a95STrond Myklebust xprt_free_all_slots(xprt); 1133e204e621SPavel Emelyanov kfree(xprt); 1134e204e621SPavel Emelyanov } 1135e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1136e204e621SPavel Emelyanov 11379903cd1cSChuck Lever /** 11389903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 11399903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 11409903cd1cSChuck Lever * 1141ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1142ba60eb25STrond Myklebust * slots are available, place the task on the transport's 11439903cd1cSChuck Lever * backlog queue. 11449903cd1cSChuck Lever */ 11459903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 11461da177e4SLinus Torvalds { 114745bc0dceSTrond Myklebust struct rpc_xprt *xprt; 11481da177e4SLinus Torvalds 114943cedbf0STrond Myklebust task->tk_status = 0; 115043cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 115143cedbf0STrond Myklebust return; 115243cedbf0STrond Myklebust 115343cedbf0STrond Myklebust task->tk_timeout = 0; 115443cedbf0STrond Myklebust task->tk_status = -EAGAIN; 115545bc0dceSTrond Myklebust rcu_read_lock(); 115645bc0dceSTrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1157ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 1158ba60eb25STrond Myklebust xprt->ops->alloc_slot(xprt, task); 1159ba60eb25STrond Myklebust rcu_read_unlock(); 1160ba60eb25STrond Myklebust } 1161ba60eb25STrond Myklebust 1162ba60eb25STrond Myklebust /** 1163ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1164ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1165ba60eb25STrond Myklebust * 1166ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1167ba60eb25STrond Myklebust * backlog queue. 1168ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1169ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1170ba60eb25STrond Myklebust */ 1171ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1172ba60eb25STrond Myklebust { 1173ba60eb25STrond Myklebust struct rpc_xprt *xprt; 1174ba60eb25STrond Myklebust 1175ba60eb25STrond Myklebust task->tk_status = 0; 1176ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1177ba60eb25STrond Myklebust return; 1178ba60eb25STrond Myklebust 1179ba60eb25STrond Myklebust task->tk_timeout = 0; 1180ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 1181ba60eb25STrond Myklebust rcu_read_lock(); 1182ba60eb25STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1183f39c1bfbSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 118445bc0dceSTrond Myklebust rcu_read_unlock(); 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 1187d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) 11881da177e4SLinus Torvalds { 11890eae88f3SEric Dumazet return (__force __be32)xprt->xid++; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt) 11931da177e4SLinus Torvalds { 119463862b5bSAruna-Hewapathirane xprt->xid = prandom_u32(); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11979903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 11981da177e4SLinus Torvalds { 11991da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12001da177e4SLinus Torvalds 1201d9ba131dSTrond Myklebust INIT_LIST_HEAD(&req->rq_list); 1202ba7392bbSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 12031da177e4SLinus Torvalds req->rq_task = task; 12041da177e4SLinus Torvalds req->rq_xprt = xprt; 120502107148SChuck Lever req->rq_buffer = NULL; 12061da177e4SLinus Torvalds req->rq_xid = xprt_alloc_xid(xprt); 12070a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie - 1; 120892551948STrond Myklebust req->rq_bytes_sent = 0; 120992551948STrond Myklebust req->rq_snd_buf.len = 0; 121092551948STrond Myklebust req->rq_snd_buf.buflen = 0; 121192551948STrond Myklebust req->rq_rcv_buf.len = 0; 121292551948STrond Myklebust req->rq_rcv_buf.buflen = 0; 1213ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf = NULL; 1214da45828eSTrond Myklebust xprt_reset_majortimeo(req); 121546121cf7SChuck Lever dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 12161da177e4SLinus Torvalds req, ntohl(req->rq_xid)); 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12199903cd1cSChuck Lever /** 12209903cd1cSChuck Lever * xprt_release - release an RPC request slot 12219903cd1cSChuck Lever * @task: task which is finished with the slot 12229903cd1cSChuck Lever * 12231da177e4SLinus Torvalds */ 12249903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 12251da177e4SLinus Torvalds { 122655ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 122787ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12281da177e4SLinus Torvalds 122987ed5003STrond Myklebust if (req == NULL) { 123087ed5003STrond Myklebust if (task->tk_client) { 123187ed5003STrond Myklebust rcu_read_lock(); 123287ed5003STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 123387ed5003STrond Myklebust if (xprt->snd_task == task) 123487ed5003STrond Myklebust xprt_release_write(xprt, task); 123587ed5003STrond Myklebust rcu_read_unlock(); 123687ed5003STrond Myklebust } 12371da177e4SLinus Torvalds return; 123887ed5003STrond Myklebust } 123955ae1aabSRicardo Labiaga 124055ae1aabSRicardo Labiaga xprt = req->rq_xprt; 12410a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 12420a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 12430a702195SWeston Andros Adamson else if (task->tk_client) 12440a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 12454a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 124649e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1247a58dd398SChuck Lever if (xprt->ops->release_request) 1248a58dd398SChuck Lever xprt->ops->release_request(task); 12491da177e4SLinus Torvalds if (!list_empty(&req->rq_list)) 12501da177e4SLinus Torvalds list_del(&req->rq_list); 12511da177e4SLinus Torvalds xprt->last_used = jiffies; 12524cfc7e60SRahul Iyer if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) 1253a246b010SChuck Lever mod_timer(&xprt->timer, 125403bf4b70SChuck Lever xprt->last_used + xprt->idle_timeout); 12554a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1256ee5ebe85STrond Myklebust if (req->rq_buffer) 1257c5a4dd8bSChuck Lever xprt->ops->buf_free(req->rq_buffer); 1258a17c2153STrond Myklebust if (req->rq_cred != NULL) 1259a17c2153STrond Myklebust put_rpccred(req->rq_cred); 12601da177e4SLinus Torvalds task->tk_rqstp = NULL; 1261ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1262ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 126355ae1aabSRicardo Labiaga 126446121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1265ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1266ee5ebe85STrond Myklebust xprt_free_slot(xprt, req); 1267ee5ebe85STrond Myklebust else 1268c9acb42eSTrond Myklebust xprt_free_bc_request(req); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 127121de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1272c2866763SChuck Lever { 127321de0a95STrond Myklebust atomic_set(&xprt->count, 1); 1274c2866763SChuck Lever 1275c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1276c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 1277c2866763SChuck Lever 1278c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 1279c2866763SChuck Lever INIT_LIST_HEAD(&xprt->recv); 12809e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1281f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1282f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 12839e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1284f9acac1aSRicardo Labiaga 1285c2866763SChuck Lever xprt->last_used = jiffies; 1286c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1287a509050bSChuck Lever xprt->bind_index = 0; 1288c2866763SChuck Lever 1289c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1290c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 129134006ceeSTrond Myklebust rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending"); 1292c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1293c2866763SChuck Lever 1294c2866763SChuck Lever xprt_init_xid(xprt); 1295c2866763SChuck Lever 129621de0a95STrond Myklebust xprt->xprt_net = get_net(net); 12978d9266ffSTrond Myklebust } 12988d9266ffSTrond Myklebust 12998d9266ffSTrond Myklebust /** 13008d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 13018d9266ffSTrond Myklebust * @args: rpc transport creation arguments 13028d9266ffSTrond Myklebust * 13038d9266ffSTrond Myklebust */ 13048d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 13058d9266ffSTrond Myklebust { 13068d9266ffSTrond Myklebust struct rpc_xprt *xprt; 13078d9266ffSTrond Myklebust struct xprt_class *t; 13088d9266ffSTrond Myklebust 13098d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 13108d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 13118d9266ffSTrond Myklebust if (t->ident == args->ident) { 13128d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13138d9266ffSTrond Myklebust goto found; 13148d9266ffSTrond Myklebust } 13158d9266ffSTrond Myklebust } 13168d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13173c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 13188d9266ffSTrond Myklebust return ERR_PTR(-EIO); 13198d9266ffSTrond Myklebust 13208d9266ffSTrond Myklebust found: 13218d9266ffSTrond Myklebust xprt = t->setup(args); 13228d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 13238d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 13248d9266ffSTrond Myklebust -PTR_ERR(xprt)); 132521de0a95STrond Myklebust goto out; 13268d9266ffSTrond Myklebust } 132733d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 132833d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 132921de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 133021de0a95STrond Myklebust if (xprt_has_timer(xprt)) 133121de0a95STrond Myklebust setup_timer(&xprt->timer, xprt_init_autodisconnect, 133221de0a95STrond Myklebust (unsigned long)xprt); 133321de0a95STrond Myklebust else 133421de0a95STrond Myklebust init_timer(&xprt->timer); 13354e0038b6STrond Myklebust 13364e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 13374e0038b6STrond Myklebust xprt_destroy(xprt); 13384e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 13394e0038b6STrond Myklebust } 13404e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 13414e0038b6STrond Myklebust if (xprt->servername == NULL) { 13424e0038b6STrond Myklebust xprt_destroy(xprt); 13434e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 13444e0038b6STrond Myklebust } 13454e0038b6STrond Myklebust 1346c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1347c2866763SChuck Lever xprt->max_reqs); 134821de0a95STrond Myklebust out: 1349c2866763SChuck Lever return xprt; 1350c2866763SChuck Lever } 1351c2866763SChuck Lever 13529903cd1cSChuck Lever /** 13539903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1354a8de240aSTrond Myklebust * @xprt: transport to destroy 13559903cd1cSChuck Lever * 13561da177e4SLinus Torvalds */ 1357a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 13600065db32STrond Myklebust del_timer_sync(&xprt->timer); 1361c8541ecdSChuck Lever 1362f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1363f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1364f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1365f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1366c3ae62aeSJ. Bruce Fields cancel_work_sync(&xprt->task_cleanup); 13674e0038b6STrond Myklebust kfree(xprt->servername); 1368c8541ecdSChuck Lever /* 1369c8541ecdSChuck Lever * Tear down transport state and free the rpc_xprt 1370c8541ecdSChuck Lever */ 1371a246b010SChuck Lever xprt->ops->destroy(xprt); 13726b6ca86bSTrond Myklebust } 13731da177e4SLinus Torvalds 13746b6ca86bSTrond Myklebust /** 13756b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 13766b6ca86bSTrond Myklebust * @xprt: pointer to the transport 13776b6ca86bSTrond Myklebust * 13786b6ca86bSTrond Myklebust */ 13796b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 13806b6ca86bSTrond Myklebust { 1381a8de240aSTrond Myklebust if (atomic_dec_and_test(&xprt->count)) 1382a8de240aSTrond Myklebust xprt_destroy(xprt); 13836b6ca86bSTrond Myklebust } 1384