1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 61da177e4SLinus Torvalds * and asynchronous calls. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * The interface works like this: 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 111da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 121da177e4SLinus Torvalds * (xprt_reserve). 131da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1455aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1555aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1655ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1755ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1855ae1aabSRicardo Labiaga * expired. 191da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 2055aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 211da177e4SLinus Torvalds * caller is woken up, and the timer removed. 221da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 231da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 241da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 251da177e4SLinus Torvalds * of -ETIMEDOUT. 261da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 271da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 281da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 291da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 301da177e4SLinus Torvalds * again. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 331da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 341da177e4SLinus Torvalds * tasks that rely on callbacks. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3755aa4f58SChuck Lever * 3855aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41a246b010SChuck Lever #include <linux/module.h> 42a246b010SChuck Lever 431da177e4SLinus Torvalds #include <linux/types.h> 44a246b010SChuck Lever #include <linux/interrupt.h> 451da177e4SLinus Torvalds #include <linux/workqueue.h> 46bf3fcf89SChuck Lever #include <linux/net.h> 47ff839970SChuck Lever #include <linux/ktime.h> 481da177e4SLinus Torvalds 49a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 5011c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 51c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 52fda1bfefSTrond Myklebust #include <linux/rcupdate.h> 53a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 541da177e4SLinus Torvalds 553705ad64SJeff Layton #include <trace/events/sunrpc.h> 563705ad64SJeff Layton 5755ae1aabSRicardo Labiaga #include "sunrpc.h" 5855ae1aabSRicardo Labiaga 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * Local variables 611da177e4SLinus Torvalds */ 621da177e4SLinus Torvalds 63f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 641da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 651da177e4SLinus Torvalds #endif 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * Local functions 691da177e4SLinus Torvalds */ 7021de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 7137ac86c3SChuck Lever static __be32 xprt_alloc_xid(struct rpc_xprt *xprt); 724e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 73*e877a88dSNeilBrown static void xprt_request_init(struct rpc_task *task); 741da177e4SLinus Torvalds 755ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7681c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7781c098afS\"Talpey, Thomas\ 789e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req) 799e910bffSTrond Myklebust { 809e910bffSTrond Myklebust unsigned long timeout = jiffies + req->rq_timeout; 819e910bffSTrond Myklebust 829e910bffSTrond Myklebust if (time_before(timeout, req->rq_majortimeo)) 839e910bffSTrond Myklebust return timeout; 849e910bffSTrond Myklebust return req->rq_majortimeo; 859e910bffSTrond Myklebust } 869e910bffSTrond Myklebust 8712a80469SChuck Lever /** 8881c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 8981c098afS\"Talpey, Thomas\ * @transport: transport to register 9081c098afS\"Talpey, Thomas\ * 9181c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9281c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9381c098afS\"Talpey, Thomas\ * 9481c098afS\"Talpey, Thomas\ * Returns: 9581c098afS\"Talpey, Thomas\ * 0: transport successfully registered 9681c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 9781c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 9881c098afS\"Talpey, Thomas\ */ 9981c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 10081c098afS\"Talpey, Thomas\ { 10181c098afS\"Talpey, Thomas\ struct xprt_class *t; 10281c098afS\"Talpey, Thomas\ int result; 10381c098afS\"Talpey, Thomas\ 10481c098afS\"Talpey, Thomas\ result = -EEXIST; 10581c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 10681c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 10781c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1084fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 10981c098afS\"Talpey, Thomas\ goto out; 11081c098afS\"Talpey, Thomas\ } 11181c098afS\"Talpey, Thomas\ 11281c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11381c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11481c098afS\"Talpey, Thomas\ transport->name); 11581c098afS\"Talpey, Thomas\ result = 0; 11681c098afS\"Talpey, Thomas\ 11781c098afS\"Talpey, Thomas\ out: 11881c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 11981c098afS\"Talpey, Thomas\ return result; 12081c098afS\"Talpey, Thomas\ } 12181c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12281c098afS\"Talpey, Thomas\ 12381c098afS\"Talpey, Thomas\ /** 12481c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 12565b6e42cSRandy Dunlap * @transport: transport to unregister 12681c098afS\"Talpey, Thomas\ * 12781c098afS\"Talpey, Thomas\ * Returns: 12881c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 12981c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 13081c098afS\"Talpey, Thomas\ */ 13181c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13281c098afS\"Talpey, Thomas\ { 13381c098afS\"Talpey, Thomas\ struct xprt_class *t; 13481c098afS\"Talpey, Thomas\ int result; 13581c098afS\"Talpey, Thomas\ 13681c098afS\"Talpey, Thomas\ result = 0; 13781c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 13881c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 13981c098afS\"Talpey, Thomas\ if (t == transport) { 14081c098afS\"Talpey, Thomas\ printk(KERN_INFO 14181c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14281c098afS\"Talpey, Thomas\ transport->name); 14381c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14481c098afS\"Talpey, Thomas\ goto out; 14581c098afS\"Talpey, Thomas\ } 14681c098afS\"Talpey, Thomas\ } 14781c098afS\"Talpey, Thomas\ result = -ENOENT; 14881c098afS\"Talpey, Thomas\ 14981c098afS\"Talpey, Thomas\ out: 15081c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15181c098afS\"Talpey, Thomas\ return result; 15281c098afS\"Talpey, Thomas\ } 15381c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15481c098afS\"Talpey, Thomas\ 155d5aa6b22STrond Myklebust static void 156d5aa6b22STrond Myklebust xprt_class_release(const struct xprt_class *t) 157d5aa6b22STrond Myklebust { 158d5aa6b22STrond Myklebust module_put(t->owner); 159d5aa6b22STrond Myklebust } 160d5aa6b22STrond Myklebust 161d5aa6b22STrond Myklebust static const struct xprt_class * 1629bccd264STrond Myklebust xprt_class_find_by_ident_locked(int ident) 1639bccd264STrond Myklebust { 1649bccd264STrond Myklebust const struct xprt_class *t; 1659bccd264STrond Myklebust 1669bccd264STrond Myklebust list_for_each_entry(t, &xprt_list, list) { 1679bccd264STrond Myklebust if (t->ident != ident) 1689bccd264STrond Myklebust continue; 1699bccd264STrond Myklebust if (!try_module_get(t->owner)) 1709bccd264STrond Myklebust continue; 1719bccd264STrond Myklebust return t; 1729bccd264STrond Myklebust } 1739bccd264STrond Myklebust return NULL; 1749bccd264STrond Myklebust } 1759bccd264STrond Myklebust 1769bccd264STrond Myklebust static const struct xprt_class * 1779bccd264STrond Myklebust xprt_class_find_by_ident(int ident) 1789bccd264STrond Myklebust { 1799bccd264STrond Myklebust const struct xprt_class *t; 1809bccd264STrond Myklebust 1819bccd264STrond Myklebust spin_lock(&xprt_list_lock); 1829bccd264STrond Myklebust t = xprt_class_find_by_ident_locked(ident); 1839bccd264STrond Myklebust spin_unlock(&xprt_list_lock); 1849bccd264STrond Myklebust return t; 1859bccd264STrond Myklebust } 1869bccd264STrond Myklebust 1879bccd264STrond Myklebust static const struct xprt_class * 188d5aa6b22STrond Myklebust xprt_class_find_by_netid_locked(const char *netid) 189d5aa6b22STrond Myklebust { 190d5aa6b22STrond Myklebust const struct xprt_class *t; 191d5aa6b22STrond Myklebust unsigned int i; 192d5aa6b22STrond Myklebust 193d5aa6b22STrond Myklebust list_for_each_entry(t, &xprt_list, list) { 194d5aa6b22STrond Myklebust for (i = 0; t->netid[i][0] != '\0'; i++) { 195d5aa6b22STrond Myklebust if (strcmp(t->netid[i], netid) != 0) 196d5aa6b22STrond Myklebust continue; 197d5aa6b22STrond Myklebust if (!try_module_get(t->owner)) 198d5aa6b22STrond Myklebust continue; 199d5aa6b22STrond Myklebust return t; 200d5aa6b22STrond Myklebust } 201d5aa6b22STrond Myklebust } 202d5aa6b22STrond Myklebust return NULL; 203d5aa6b22STrond Myklebust } 204d5aa6b22STrond Myklebust 205d5aa6b22STrond Myklebust static const struct xprt_class * 206d5aa6b22STrond Myklebust xprt_class_find_by_netid(const char *netid) 207d5aa6b22STrond Myklebust { 208d5aa6b22STrond Myklebust const struct xprt_class *t; 209d5aa6b22STrond Myklebust 210d5aa6b22STrond Myklebust spin_lock(&xprt_list_lock); 211d5aa6b22STrond Myklebust t = xprt_class_find_by_netid_locked(netid); 212d5aa6b22STrond Myklebust if (!t) { 213d5aa6b22STrond Myklebust spin_unlock(&xprt_list_lock); 214d5aa6b22STrond Myklebust request_module("rpc%s", netid); 215d5aa6b22STrond Myklebust spin_lock(&xprt_list_lock); 216d5aa6b22STrond Myklebust t = xprt_class_find_by_netid_locked(netid); 217d5aa6b22STrond Myklebust } 218d5aa6b22STrond Myklebust spin_unlock(&xprt_list_lock); 219d5aa6b22STrond Myklebust return t; 220d5aa6b22STrond Myklebust } 221d5aa6b22STrond Myklebust 22281c098afS\"Talpey, Thomas\ /** 2231fc5f131STrond Myklebust * xprt_find_transport_ident - convert a netid into a transport identifier 2241fc5f131STrond Myklebust * @netid: transport to load 2251fc5f131STrond Myklebust * 2261fc5f131STrond Myklebust * Returns: 2271fc5f131STrond Myklebust * > 0: transport identifier 2281fc5f131STrond Myklebust * -ENOENT: transport module not available 2291fc5f131STrond Myklebust */ 2301fc5f131STrond Myklebust int xprt_find_transport_ident(const char *netid) 2311fc5f131STrond Myklebust { 2321fc5f131STrond Myklebust const struct xprt_class *t; 2331fc5f131STrond Myklebust int ret; 2341fc5f131STrond Myklebust 2351fc5f131STrond Myklebust t = xprt_class_find_by_netid(netid); 2361fc5f131STrond Myklebust if (!t) 2371fc5f131STrond Myklebust return -ENOENT; 2381fc5f131STrond Myklebust ret = t->ident; 2391fc5f131STrond Myklebust xprt_class_release(t); 2401fc5f131STrond Myklebust return ret; 2411fc5f131STrond Myklebust } 2421fc5f131STrond Myklebust EXPORT_SYMBOL_GPL(xprt_find_transport_ident); 2431fc5f131STrond Myklebust 244c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 245c544577dSTrond Myklebust { 246c544577dSTrond Myklebust xprt->snd_task = NULL; 247c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 248c544577dSTrond Myklebust smp_mb__before_atomic(); 249c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 250c544577dSTrond Myklebust smp_mb__after_atomic(); 251c544577dSTrond Myklebust } else 252c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 253c544577dSTrond Myklebust } 254c544577dSTrond Myklebust 255441e3e24STom Talpey /** 25612a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 25712a80469SChuck Lever * @task: task that is requesting access to the transport 258177c27bfSRandy Dunlap * @xprt: pointer to the target transport 25912a80469SChuck Lever * 26012a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 26112a80469SChuck Lever * transport connects from colliding with writes. No congestion control 26212a80469SChuck Lever * is provided. 2631da177e4SLinus Torvalds */ 26443cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 2651da177e4SLinus Torvalds { 26612a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 26712a80469SChuck Lever 26812a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 26912a80469SChuck Lever if (task == xprt->snd_task) 270bf7ca707SChuck Lever goto out_locked; 27112a80469SChuck Lever goto out_sleep; 27212a80469SChuck Lever } 273c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 274c544577dSTrond Myklebust goto out_unlock; 27512a80469SChuck Lever xprt->snd_task = task; 2764d4a76f3Sj223yang@asset.uwaterloo.ca 277bf7ca707SChuck Lever out_locked: 278bf7ca707SChuck Lever trace_xprt_reserve_xprt(xprt, task); 27912a80469SChuck Lever return 1; 28012a80469SChuck Lever 281c544577dSTrond Myklebust out_unlock: 282c544577dSTrond Myklebust xprt_clear_locked(xprt); 28312a80469SChuck Lever out_sleep: 28412a80469SChuck Lever task->tk_status = -EAGAIN; 2856b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2866b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2879e910bffSTrond Myklebust xprt_request_timeout(req)); 2886b2e6856STrond Myklebust else 28979c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 29012a80469SChuck Lever return 0; 29112a80469SChuck Lever } 29212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 29312a80469SChuck Lever 29475891f50STrond Myklebust static bool 29575891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 29675891f50STrond Myklebust { 29775891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 29875891f50STrond Myklebust } 29975891f50STrond Myklebust 30075891f50STrond Myklebust static void 30175891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 30275891f50STrond Myklebust { 30375891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 30475891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 30575891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 30675891f50STrond Myklebust rq_xmit)->rq_cong) 30775891f50STrond Myklebust return; 30875891f50STrond Myklebust } 30975891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 31075891f50STrond Myklebust } 31175891f50STrond Myklebust 31275891f50STrond Myklebust static void 31375891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 31475891f50STrond Myklebust { 31575891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 31675891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 31775891f50STrond Myklebust } 31875891f50STrond Myklebust 31912a80469SChuck Lever /* 32012a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 32112a80469SChuck Lever * @task: task that is requesting access to the transport 32212a80469SChuck Lever * 32312a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 32412a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 32512a80469SChuck Lever * woken up and given access to the transport. 32675891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 32712a80469SChuck Lever */ 32843cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 32912a80469SChuck Lever { 3301da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3311da177e4SLinus Torvalds 3322226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 3331da177e4SLinus Torvalds if (task == xprt->snd_task) 334bf7ca707SChuck Lever goto out_locked; 3351da177e4SLinus Torvalds goto out_sleep; 3361da177e4SLinus Torvalds } 33743cedbf0STrond Myklebust if (req == NULL) { 33843cedbf0STrond Myklebust xprt->snd_task = task; 339bf7ca707SChuck Lever goto out_locked; 34043cedbf0STrond Myklebust } 341c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 342c544577dSTrond Myklebust goto out_unlock; 34375891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 3441da177e4SLinus Torvalds xprt->snd_task = task; 345bf7ca707SChuck Lever goto out_locked; 3461da177e4SLinus Torvalds } 347c544577dSTrond Myklebust out_unlock: 348632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3491da177e4SLinus Torvalds out_sleep: 3501da177e4SLinus Torvalds task->tk_status = -EAGAIN; 3516b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 3526b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 3539e910bffSTrond Myklebust xprt_request_timeout(req)); 3546b2e6856STrond Myklebust else 35579c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 3561da177e4SLinus Torvalds return 0; 357bf7ca707SChuck Lever out_locked: 358bf7ca707SChuck Lever trace_xprt_reserve_cong(xprt, task); 359bf7ca707SChuck Lever return 1; 3601da177e4SLinus Torvalds } 36112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 3621da177e4SLinus Torvalds 36312a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds int retval; 3661da177e4SLinus Torvalds 367bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 368bd79bc57STrond Myklebust return 1; 369b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 37043cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 371b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3721da177e4SLinus Torvalds return retval; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 375961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 3761da177e4SLinus Torvalds { 377961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 37849e9a890SChuck Lever 37949e9a890SChuck Lever xprt->snd_task = task; 380961a828dSTrond Myklebust return true; 381961a828dSTrond Myklebust } 382961a828dSTrond Myklebust 383961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 384961a828dSTrond Myklebust { 385961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 38649e9a890SChuck Lever return; 387c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 388c544577dSTrond Myklebust goto out_unlock; 389f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 390f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 391961a828dSTrond Myklebust return; 392c544577dSTrond Myklebust out_unlock: 393632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 39449e9a890SChuck Lever } 39549e9a890SChuck Lever 396961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 397961a828dSTrond Myklebust { 398961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 399961a828dSTrond Myklebust return; 400c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 401c544577dSTrond Myklebust goto out_unlock; 40275891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 403961a828dSTrond Myklebust goto out_unlock; 404f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 40575891f50STrond Myklebust __xprt_lock_write_func, xprt)) 406961a828dSTrond Myklebust return; 4071da177e4SLinus Torvalds out_unlock: 408632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 41149e9a890SChuck Lever /** 41249e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 41349e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 41449e9a890SChuck Lever * @task: task that is releasing access to the transport 41549e9a890SChuck Lever * 41649e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 4171da177e4SLinus Torvalds */ 41849e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 4191da177e4SLinus Torvalds { 4201da177e4SLinus Torvalds if (xprt->snd_task == task) { 421632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 4221da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 4231da177e4SLinus Torvalds } 424bf7ca707SChuck Lever trace_xprt_release_xprt(xprt, task); 4251da177e4SLinus Torvalds } 42612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 4271da177e4SLinus Torvalds 42849e9a890SChuck Lever /** 42949e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 43049e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 43149e9a890SChuck Lever * @task: task that is releasing access to the transport 43249e9a890SChuck Lever * 43349e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 43449e9a890SChuck Lever * transport if the transport's congestion window allows it. 43549e9a890SChuck Lever */ 43649e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 43749e9a890SChuck Lever { 43849e9a890SChuck Lever if (xprt->snd_task == task) { 439632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 44049e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 44149e9a890SChuck Lever } 442bf7ca707SChuck Lever trace_xprt_release_cong(xprt, task); 44349e9a890SChuck Lever } 44412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 44549e9a890SChuck Lever 44649e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 4471da177e4SLinus Torvalds { 448bd79bc57STrond Myklebust if (xprt->snd_task != task) 449bd79bc57STrond Myklebust return; 450b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 45149e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 452b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 4571da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds static int 46075891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4611da177e4SLinus Torvalds { 4621da177e4SLinus Torvalds if (req->rq_cong) 4631da177e4SLinus Torvalds return 1; 464bf7ca707SChuck Lever trace_xprt_get_cong(xprt, req->rq_task); 46575891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 46675891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 4671da177e4SLinus Torvalds return 0; 46875891f50STrond Myklebust } 4691da177e4SLinus Torvalds req->rq_cong = 1; 4701da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4711da177e4SLinus Torvalds return 1; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 4751da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4761da177e4SLinus Torvalds * that has been sleeping due to congestion 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds static void 4791da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds if (!req->rq_cong) 4821da177e4SLinus Torvalds return; 4831da177e4SLinus Torvalds req->rq_cong = 0; 4841da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 48575891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 486bf7ca707SChuck Lever trace_xprt_put_cong(xprt, req->rq_task); 48749e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 49046c0ee8bSChuck Lever /** 49175891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 49275891f50STrond Myklebust * @xprt: pointer to transport 49375891f50STrond Myklebust * @req: pointer to RPC request 49475891f50STrond Myklebust * 49575891f50STrond Myklebust * Useful for transports that require congestion control. 49675891f50STrond Myklebust */ 49775891f50STrond Myklebust bool 49875891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 49975891f50STrond Myklebust { 50075891f50STrond Myklebust bool ret = false; 50175891f50STrond Myklebust 50275891f50STrond Myklebust if (req->rq_cong) 50375891f50STrond Myklebust return true; 504b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 50575891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 506b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 50775891f50STrond Myklebust return ret; 50875891f50STrond Myklebust } 50975891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 51075891f50STrond Myklebust 51175891f50STrond Myklebust /** 512a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 513a58dd398SChuck Lever * @task: RPC request that recently completed 514a58dd398SChuck Lever * 515a58dd398SChuck Lever * Useful for transports that require congestion control. 516a58dd398SChuck Lever */ 517a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 518a58dd398SChuck Lever { 519a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 520a4f0835cSTrond Myklebust 521a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 522a58dd398SChuck Lever } 52312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 524a58dd398SChuck Lever 5258593e010SChuck Lever static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt) 5268593e010SChuck Lever { 5278593e010SChuck Lever if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) 5288593e010SChuck Lever __xprt_lock_write_next_cong(xprt); 5298593e010SChuck Lever } 5308593e010SChuck Lever 53175891f50STrond Myklebust /* 53275891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 53375891f50STrond Myklebust * entry on xprt->sending 53475891f50STrond Myklebust */ 53575891f50STrond Myklebust static void 53675891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 53775891f50STrond Myklebust { 53875891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 539b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 54075891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 541b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 54275891f50STrond Myklebust } 54375891f50STrond Myklebust } 54475891f50STrond Myklebust 545a58dd398SChuck Lever /** 54646c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 5476a24dfb6STrond Myklebust * @xprt: pointer to xprt 54846c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 54946c0ee8bSChuck Lever * @result: result code of completed RPC request 55046c0ee8bSChuck Lever * 5514f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 5524f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 5534f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 5544f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 5554f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 5564f4cf5adSChuck Lever * 5574f4cf5adSChuck Lever * - a reply is received and 5584f4cf5adSChuck Lever * - a full number of requests are outstanding and 5594f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 5601da177e4SLinus Torvalds */ 5616a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 5621da177e4SLinus Torvalds { 56346c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 56446c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 5671da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 5681da177e4SLinus Torvalds * the result gets rounded properly. */ 5691da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 5701da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 5711da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 57249e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 5731da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 5741da177e4SLinus Torvalds cwnd >>= 1; 5751da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 5761da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 5791da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 5801da177e4SLinus Torvalds xprt->cwnd = cwnd; 58146c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 5821da177e4SLinus Torvalds } 58312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 5841da177e4SLinus Torvalds 58544fbac22SChuck Lever /** 58644fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 58744fbac22SChuck Lever * @xprt: transport with waiting tasks 58844fbac22SChuck Lever * @status: result code to plant in each task before waking it 58944fbac22SChuck Lever * 59044fbac22SChuck Lever */ 59144fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 59244fbac22SChuck Lever { 59344fbac22SChuck Lever if (status < 0) 59444fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 59544fbac22SChuck Lever else 59644fbac22SChuck Lever rpc_wake_up(&xprt->pending); 59744fbac22SChuck Lever } 59812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 59944fbac22SChuck Lever 600c7b2cae8SChuck Lever /** 601c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 602c544577dSTrond Myklebust * @xprt: transport 603a9a6b52eSTrond Myklebust * 604a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 605a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 606a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 607c7b2cae8SChuck Lever */ 608c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 609c7b2cae8SChuck Lever { 610c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 611c7b2cae8SChuck Lever } 61212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 613c7b2cae8SChuck Lever 614c544577dSTrond Myklebust static bool 615c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 616c544577dSTrond Myklebust { 617c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 618c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 619c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 620c544577dSTrond Myklebust "xprt %p\n", xprt); 621c544577dSTrond Myklebust return true; 622c544577dSTrond Myklebust } 623c544577dSTrond Myklebust return false; 624c544577dSTrond Myklebust } 625c544577dSTrond Myklebust 626c7b2cae8SChuck Lever /** 627c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 628c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 629c7b2cae8SChuck Lever * 630c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 631c7b2cae8SChuck Lever */ 632c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 633c7b2cae8SChuck Lever { 634c544577dSTrond Myklebust bool ret; 635c544577dSTrond Myklebust 636c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 637c544577dSTrond Myklebust return false; 638b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 639c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 640b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 641c544577dSTrond Myklebust return ret; 642c7b2cae8SChuck Lever } 64312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 644c7b2cae8SChuck Lever 645da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime) 646da953063STrond Myklebust { 647da953063STrond Myklebust s64 delta = ktime_to_ns(ktime_get() - abstime); 648da953063STrond Myklebust return likely(delta >= 0) ? 649da953063STrond Myklebust jiffies - nsecs_to_jiffies(delta) : 650da953063STrond Myklebust jiffies + nsecs_to_jiffies(-delta); 651da953063STrond Myklebust } 652da953063STrond Myklebust 653da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req) 6541da177e4SLinus Torvalds { 655ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 656da953063STrond Myklebust unsigned long majortimeo = req->rq_timeout; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds if (to->to_exponential) 659da953063STrond Myklebust majortimeo <<= to->to_retries; 6601da177e4SLinus Torvalds else 661da953063STrond Myklebust majortimeo += to->to_increment * to->to_retries; 662da953063STrond Myklebust if (majortimeo > to->to_maxval || majortimeo == 0) 663da953063STrond Myklebust majortimeo = to->to_maxval; 664da953063STrond Myklebust return majortimeo; 665da953063STrond Myklebust } 666da953063STrond Myklebust 667da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req) 668da953063STrond Myklebust { 669da953063STrond Myklebust req->rq_majortimeo += xprt_calc_majortimeo(req); 670da953063STrond Myklebust } 671da953063STrond Myklebust 6727de62bc0SOlga Kornievskaia static void xprt_reset_minortimeo(struct rpc_rqst *req) 6737de62bc0SOlga Kornievskaia { 6747de62bc0SOlga Kornievskaia req->rq_minortimeo += req->rq_timeout; 6757de62bc0SOlga Kornievskaia } 6767de62bc0SOlga Kornievskaia 677da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req) 678da953063STrond Myklebust { 679da953063STrond Myklebust unsigned long time_init; 680da953063STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 681da953063STrond Myklebust 682da953063STrond Myklebust if (likely(xprt && xprt_connected(xprt))) 683da953063STrond Myklebust time_init = jiffies; 684da953063STrond Myklebust else 685da953063STrond Myklebust time_init = xprt_abs_ktime_to_jiffies(task->tk_start); 686da953063STrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 687da953063STrond Myklebust req->rq_majortimeo = time_init + xprt_calc_majortimeo(req); 6887de62bc0SOlga Kornievskaia req->rq_minortimeo = time_init + req->rq_timeout; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6919903cd1cSChuck Lever /** 6929903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 6939903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 6949903cd1cSChuck Lever * 6951da177e4SLinus Torvalds */ 6961da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 699ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 7001da177e4SLinus Torvalds int status = 0; 7011da177e4SLinus Torvalds 70209252177SChris Dion if (time_before(jiffies, req->rq_majortimeo)) { 7037de62bc0SOlga Kornievskaia if (time_before(jiffies, req->rq_minortimeo)) 7047de62bc0SOlga Kornievskaia return status; 7051da177e4SLinus Torvalds if (to->to_exponential) 7061da177e4SLinus Torvalds req->rq_timeout <<= 1; 7071da177e4SLinus Torvalds else 7081da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 7091da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 7101da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 7111da177e4SLinus Torvalds req->rq_retries++; 7121da177e4SLinus Torvalds } else { 7131da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 7141da177e4SLinus Torvalds req->rq_retries = 0; 7151da177e4SLinus Torvalds xprt_reset_majortimeo(req); 7161da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 717b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7181da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 719b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7201da177e4SLinus Torvalds status = -ETIMEDOUT; 7211da177e4SLinus Torvalds } 7227de62bc0SOlga Kornievskaia xprt_reset_minortimeo(req); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds if (req->rq_timeout == 0) { 7251da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 7261da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds return status; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 73165f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 7321da177e4SLinus Torvalds { 73365f27f38SDavid Howells struct rpc_xprt *xprt = 73465f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 735a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 7361da177e4SLinus Torvalds 737911813d7SChuck Lever trace_xprt_disconnect_auto(xprt); 73866af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 7394876cc77STrond Myklebust xprt->ops->close(xprt); 7401da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 74179234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 742a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7459903cd1cSChuck Lever /** 74662da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 7479903cd1cSChuck Lever * @xprt: transport to flag for disconnect 7489903cd1cSChuck Lever * 7491da177e4SLinus Torvalds */ 75062da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 7511da177e4SLinus Torvalds { 752911813d7SChuck Lever trace_xprt_disconnect_done(xprt); 753b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7541da177e4SLinus Torvalds xprt_clear_connected(xprt); 755c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 7568593e010SChuck Lever xprt_clear_congestion_window_wait_locked(xprt); 75727adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 758b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7591da177e4SLinus Torvalds } 76062da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 7611da177e4SLinus Torvalds 76266af1e55STrond Myklebust /** 76366af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 76466af1e55STrond Myklebust * @xprt: transport to disconnect 76566af1e55STrond Myklebust * 76666af1e55STrond Myklebust */ 76766af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 76866af1e55STrond Myklebust { 769911813d7SChuck Lever trace_xprt_disconnect_force(xprt); 770911813d7SChuck Lever 77166af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 772b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 77366af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 77466af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 77566af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 77640a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7770445f92cSTrond Myklebust else if (xprt->snd_task) 7780445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 7790445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 780b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 78166af1e55STrond Myklebust } 782e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 78366af1e55STrond Myklebust 7847f3a1d1eSTrond Myklebust static unsigned int 7857f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 7867f3a1d1eSTrond Myklebust { 7877f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 7887f3a1d1eSTrond Myklebust } 7897f3a1d1eSTrond Myklebust 7907f3a1d1eSTrond Myklebust static bool 7917f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 7927f3a1d1eSTrond Myklebust { 7937f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 7947f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 7957f3a1d1eSTrond Myklebust 7967f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7977f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7987f3a1d1eSTrond Myklebust } 7997f3a1d1eSTrond Myklebust 8007c1d71cfSTrond Myklebust /** 8017c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 8027c1d71cfSTrond Myklebust * @xprt: transport to disconnect 8037c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 8047c1d71cfSTrond Myklebust * 8057c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 8067c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 8077c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 8087c1d71cfSTrond Myklebust * a batch of RPC requests. 8097c1d71cfSTrond Myklebust * 8107c1d71cfSTrond Myklebust */ 8117c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 8127c1d71cfSTrond Myklebust { 8137c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 814b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 8157c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 8167c1d71cfSTrond Myklebust goto out; 8172c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 8187c1d71cfSTrond Myklebust goto out; 8197c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 8207c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 8217c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 82240a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 8232a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 8247c1d71cfSTrond Myklebust out: 825b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 8267c1d71cfSTrond Myklebust } 8277c1d71cfSTrond Myklebust 828ad3331acSTrond Myklebust static bool 829ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 830ad3331acSTrond Myklebust { 831ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 832ad3331acSTrond Myklebust } 833ad3331acSTrond Myklebust 834ad3331acSTrond Myklebust static void 835ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 836ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 837ad3331acSTrond Myklebust { 83880d3c45fSDave Wysochanski xprt->last_used = jiffies; 83995f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 840ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 841ad3331acSTrond Myklebust } 842ad3331acSTrond Myklebust 8431da177e4SLinus Torvalds static void 844ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 8451da177e4SLinus Torvalds { 846ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 8471da177e4SLinus Torvalds 84895f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 849b5e92419STrond Myklebust return; 850ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 851ad3331acSTrond Myklebust xprt->last_used = jiffies; 8522226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 8531da177e4SLinus Torvalds return; 854b5e92419STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 857718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 858718ba5b8STrond Myklebust struct rpc_task *task, 859718ba5b8STrond Myklebust void *cookie) 860718ba5b8STrond Myklebust { 861718ba5b8STrond Myklebust bool ret = false; 862718ba5b8STrond Myklebust 863b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 864718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 865718ba5b8STrond Myklebust goto out; 866718ba5b8STrond Myklebust if (xprt->snd_task != task) 867718ba5b8STrond Myklebust goto out; 868718ba5b8STrond Myklebust xprt->snd_task = cookie; 869718ba5b8STrond Myklebust ret = true; 870718ba5b8STrond Myklebust out: 871b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 872718ba5b8STrond Myklebust return ret; 873718ba5b8STrond Myklebust } 874718ba5b8STrond Myklebust 875718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 876718ba5b8STrond Myklebust { 877b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 878718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 879718ba5b8STrond Myklebust goto out; 880718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 881718ba5b8STrond Myklebust goto out; 882718ba5b8STrond Myklebust xprt->snd_task =NULL; 883718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 884ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 885718ba5b8STrond Myklebust out: 886b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 88779234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 888718ba5b8STrond Myklebust } 889718ba5b8STrond Myklebust 8909903cd1cSChuck Lever /** 8919903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8929903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8931da177e4SLinus Torvalds * 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8961da177e4SLinus Torvalds { 897ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8981da177e4SLinus Torvalds 899db0a86c4SChuck Lever trace_xprt_connect(xprt); 9001da177e4SLinus Torvalds 901ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 90201d37c42STrond Myklebust task->tk_status = -EAGAIN; 9031da177e4SLinus Torvalds return; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 9061da177e4SLinus Torvalds return; 907feb8ca37STrond Myklebust 908911813d7SChuck Lever if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 909911813d7SChuck Lever trace_xprt_disconnect_cleanup(xprt); 910feb8ca37STrond Myklebust xprt->ops->close(xprt); 911911813d7SChuck Lever } 912feb8ca37STrond Myklebust 913718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 9142c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 9156b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->pending, task, NULL, 9169e910bffSTrond Myklebust xprt_request_timeout(task->tk_rqstp)); 9170b9e7943STrond Myklebust 9180b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 9190b9e7943STrond Myklebust return; 9200b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 9210b9e7943STrond Myklebust return; 9220a9a4304STrond Myklebust /* Race breaker */ 9230a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 924262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 9251b092092STrond Myklebust xprt->ops->connect(xprt, task); 9260a9a4304STrond Myklebust } else { 9270a9a4304STrond Myklebust xprt_clear_connecting(xprt); 9280a9a4304STrond Myklebust task->tk_status = 0; 9290a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 9300a9a4304STrond Myklebust } 9311da177e4SLinus Torvalds } 932718ba5b8STrond Myklebust xprt_release_write(xprt, task); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935675dd90aSChuck Lever /** 936675dd90aSChuck Lever * xprt_reconnect_delay - compute the wait before scheduling a connect 937675dd90aSChuck Lever * @xprt: transport instance 938675dd90aSChuck Lever * 939675dd90aSChuck Lever */ 940675dd90aSChuck Lever unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt) 941675dd90aSChuck Lever { 942675dd90aSChuck Lever unsigned long start, now = jiffies; 943675dd90aSChuck Lever 944675dd90aSChuck Lever start = xprt->stat.connect_start + xprt->reestablish_timeout; 945675dd90aSChuck Lever if (time_after(start, now)) 946675dd90aSChuck Lever return start - now; 947675dd90aSChuck Lever return 0; 948675dd90aSChuck Lever } 949675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_delay); 950675dd90aSChuck Lever 951675dd90aSChuck Lever /** 952675dd90aSChuck Lever * xprt_reconnect_backoff - compute the new re-establish timeout 953675dd90aSChuck Lever * @xprt: transport instance 954675dd90aSChuck Lever * @init_to: initial reestablish timeout 955675dd90aSChuck Lever * 956675dd90aSChuck Lever */ 957675dd90aSChuck Lever void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to) 958675dd90aSChuck Lever { 959675dd90aSChuck Lever xprt->reestablish_timeout <<= 1; 960675dd90aSChuck Lever if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 961675dd90aSChuck Lever xprt->reestablish_timeout = xprt->max_reconnect_timeout; 962675dd90aSChuck Lever if (xprt->reestablish_timeout < init_to) 963675dd90aSChuck Lever xprt->reestablish_timeout = init_to; 964675dd90aSChuck Lever } 965675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_backoff); 966675dd90aSChuck Lever 96795f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 96895f7691dSTrond Myklebust XID_RB_EQUAL, 96995f7691dSTrond Myklebust XID_RB_LEFT, 97095f7691dSTrond Myklebust XID_RB_RIGHT, 97195f7691dSTrond Myklebust }; 97295f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 97395f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 97495f7691dSTrond Myklebust { 97595f7691dSTrond Myklebust if (xid1 == xid2) 97695f7691dSTrond Myklebust return XID_RB_EQUAL; 97795f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 97895f7691dSTrond Myklebust return XID_RB_LEFT; 97995f7691dSTrond Myklebust return XID_RB_RIGHT; 98095f7691dSTrond Myklebust } 98195f7691dSTrond Myklebust 98295f7691dSTrond Myklebust static struct rpc_rqst * 98395f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 98495f7691dSTrond Myklebust { 98595f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 98695f7691dSTrond Myklebust struct rpc_rqst *req; 98795f7691dSTrond Myklebust 98895f7691dSTrond Myklebust while (n != NULL) { 98995f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 99095f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 99195f7691dSTrond Myklebust case XID_RB_LEFT: 99295f7691dSTrond Myklebust n = n->rb_left; 99395f7691dSTrond Myklebust break; 99495f7691dSTrond Myklebust case XID_RB_RIGHT: 99595f7691dSTrond Myklebust n = n->rb_right; 99695f7691dSTrond Myklebust break; 99795f7691dSTrond Myklebust case XID_RB_EQUAL: 99895f7691dSTrond Myklebust return req; 99995f7691dSTrond Myklebust } 100095f7691dSTrond Myklebust } 100195f7691dSTrond Myklebust return NULL; 100295f7691dSTrond Myklebust } 100395f7691dSTrond Myklebust 100495f7691dSTrond Myklebust static void 100595f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 100695f7691dSTrond Myklebust { 100795f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 100895f7691dSTrond Myklebust struct rb_node *n = NULL; 100995f7691dSTrond Myklebust struct rpc_rqst *req; 101095f7691dSTrond Myklebust 101195f7691dSTrond Myklebust while (*p != NULL) { 101295f7691dSTrond Myklebust n = *p; 101395f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 101495f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 101595f7691dSTrond Myklebust case XID_RB_LEFT: 101695f7691dSTrond Myklebust p = &n->rb_left; 101795f7691dSTrond Myklebust break; 101895f7691dSTrond Myklebust case XID_RB_RIGHT: 101995f7691dSTrond Myklebust p = &n->rb_right; 102095f7691dSTrond Myklebust break; 102195f7691dSTrond Myklebust case XID_RB_EQUAL: 102295f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 102395f7691dSTrond Myklebust return; 102495f7691dSTrond Myklebust } 102595f7691dSTrond Myklebust } 102695f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 102795f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 102895f7691dSTrond Myklebust } 102995f7691dSTrond Myklebust 103095f7691dSTrond Myklebust static void 103195f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 103295f7691dSTrond Myklebust { 103395f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 103495f7691dSTrond Myklebust } 103595f7691dSTrond Myklebust 10369903cd1cSChuck Lever /** 10379903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 10389903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 10399903cd1cSChuck Lever * @xid: RPC XID of incoming reply 10409903cd1cSChuck Lever * 104175c84151STrond Myklebust * Caller holds xprt->queue_lock. 10421da177e4SLinus Torvalds */ 1043d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 10441da177e4SLinus Torvalds { 10458f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 10461da177e4SLinus Torvalds 104795f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 104895f7691dSTrond Myklebust if (entry != NULL) { 10493705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 10500b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 1051262ca07dSChuck Lever return entry; 10523705ad64SJeff Layton } 105346121cf7SChuck Lever 105446121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 105546121cf7SChuck Lever ntohl(xid)); 10563705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 1057262ca07dSChuck Lever xprt->stat.bad_xids++; 1058262ca07dSChuck Lever return NULL; 10591da177e4SLinus Torvalds } 106012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 10611da177e4SLinus Torvalds 1062cf9946cdSTrond Myklebust static bool 1063cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 1064cf9946cdSTrond Myklebust { 1065cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 1066cf9946cdSTrond Myklebust } 1067cf9946cdSTrond Myklebust 1068729749bbSTrond Myklebust /** 1069729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 1070729749bbSTrond Myklebust * @req: Request to pin 1071729749bbSTrond Myklebust * 1072729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 10731f7d1c73SChuck Lever * so should be holding xprt->queue_lock. 1074729749bbSTrond Myklebust */ 1075729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 1076729749bbSTrond Myklebust { 1077cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 1078729749bbSTrond Myklebust } 10799590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 1080729749bbSTrond Myklebust 1081729749bbSTrond Myklebust /** 1082729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 1083729749bbSTrond Myklebust * @req: Request to pin 1084729749bbSTrond Myklebust * 10851f7d1c73SChuck Lever * Caller should be holding xprt->queue_lock. 1086729749bbSTrond Myklebust */ 1087729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 1088729749bbSTrond Myklebust { 1089cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 1090cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 1091cf9946cdSTrond Myklebust return; 1092cf9946cdSTrond Myklebust } 1093cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 1094cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 1095729749bbSTrond Myklebust } 10969590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 1097729749bbSTrond Myklebust 1098729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 1099729749bbSTrond Myklebust { 1100cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 1101729749bbSTrond Myklebust } 1102729749bbSTrond Myklebust 1103edc81dcdSTrond Myklebust static bool 1104edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 1105edc81dcdSTrond Myklebust { 1106edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1107edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 1108edc81dcdSTrond Myklebust } 1109edc81dcdSTrond Myklebust 1110edc81dcdSTrond Myklebust static bool 1111edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 1112edc81dcdSTrond Myklebust { 1113edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1114edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 1115edc81dcdSTrond Myklebust } 1116edc81dcdSTrond Myklebust 1117edc81dcdSTrond Myklebust /** 1118edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 1119edc81dcdSTrond Myklebust * @task: RPC task 1120edc81dcdSTrond Myklebust * 1121edc81dcdSTrond Myklebust */ 1122edc81dcdSTrond Myklebust void 1123edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1124edc81dcdSTrond Myklebust { 1125edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1126edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1127edc81dcdSTrond Myklebust 1128edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1129edc81dcdSTrond Myklebust return; 113075369089STrond Myklebust 113175369089STrond Myklebust xprt_request_prepare(task->tk_rqstp); 1132edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1133edc81dcdSTrond Myklebust 1134edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1135edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1136edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1137edc81dcdSTrond Myklebust 1138edc81dcdSTrond Myklebust /* Add request to the receive list */ 113995f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1140edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1141edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1142edc81dcdSTrond Myklebust 1143edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1144edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1145edc81dcdSTrond Myklebust } 1146edc81dcdSTrond Myklebust 1147edc81dcdSTrond Myklebust /** 1148edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1149edc81dcdSTrond Myklebust * @task: RPC task 1150edc81dcdSTrond Myklebust * 1151edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1152edc81dcdSTrond Myklebust */ 1153edc81dcdSTrond Myklebust static void 1154edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1155edc81dcdSTrond Myklebust { 115695f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 115795f7691dSTrond Myklebust 1158edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 115995f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1160edc81dcdSTrond Myklebust } 1161edc81dcdSTrond Myklebust 1162ecd465eeSChuck Lever /** 1163ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1164ecd465eeSChuck Lever * @task: RPC request that recently completed 1165ecd465eeSChuck Lever * 116675c84151STrond Myklebust * Caller holds xprt->queue_lock. 1167ecd465eeSChuck Lever */ 1168ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 11691da177e4SLinus Torvalds { 11701570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 11711570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 117295c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1173d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 11741570c1e4SChuck Lever 11751da177e4SLinus Torvalds if (timer) { 11761da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1177ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 11781570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds } 1181ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 11821da177e4SLinus Torvalds 11831570c1e4SChuck Lever /** 11841570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 11851570c1e4SChuck Lever * @task: RPC request that recently completed 11861570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 11871570c1e4SChuck Lever * 118875c84151STrond Myklebust * Caller holds xprt->queue_lock. 11891570c1e4SChuck Lever */ 11901570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 11911570c1e4SChuck Lever { 11921570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1193fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11941da177e4SLinus Torvalds 1195fda13939STrond Myklebust xprt->stat.recvs++; 1196ef759a2eSChuck Lever 11971e799b67STrond Myklebust req->rq_private_buf.len = copied; 1198dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1199dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 120043ac3f29STrond Myklebust smp_wmb(); 1201dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1202edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1203fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 12041da177e4SLinus Torvalds } 120512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 12061da177e4SLinus Torvalds 120746c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 12081da177e4SLinus Torvalds { 12091da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12101da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 12111da177e4SLinus Torvalds 12125d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 12135d00837bSTrond Myklebust return; 121446c0ee8bSChuck Lever 121582476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1216dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 121746c0ee8bSChuck Lever if (xprt->ops->timer) 12186a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 12195d00837bSTrond Myklebust } else 12205d00837bSTrond Myklebust task->tk_status = 0; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12239903cd1cSChuck Lever /** 12248ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_def - wait for reply 12258ba6a92dSTrond Myklebust * @task: pointer to rpc_task 12268ba6a92dSTrond Myklebust * 12278ba6a92dSTrond Myklebust * Set a request's retransmit timeout based on the transport's 12288ba6a92dSTrond Myklebust * default timeout parameters. Used by transports that don't adjust 12298ba6a92dSTrond Myklebust * the retransmit timeout based on round-trip time estimation, 12308ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 12318ba6a92dSTrond Myklebust */ 12328ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task) 12338ba6a92dSTrond Myklebust { 12348ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12358ba6a92dSTrond Myklebust 12366b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 12379e910bffSTrond Myklebust xprt_request_timeout(req)); 12388ba6a92dSTrond Myklebust } 12398ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def); 12408ba6a92dSTrond Myklebust 12418ba6a92dSTrond Myklebust /** 12428ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator 12438ba6a92dSTrond Myklebust * @task: pointer to rpc_task 12448ba6a92dSTrond Myklebust * 12458ba6a92dSTrond Myklebust * Set a request's retransmit timeout using the RTT estimator, 12468ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 12478ba6a92dSTrond Myklebust */ 12488ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task) 12498ba6a92dSTrond Myklebust { 12508ba6a92dSTrond Myklebust int timer = task->tk_msg.rpc_proc->p_timer; 12518ba6a92dSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 12528ba6a92dSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 12538ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12548ba6a92dSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 12556b2e6856STrond Myklebust unsigned long timeout; 12568ba6a92dSTrond Myklebust 12576b2e6856STrond Myklebust timeout = rpc_calc_rto(rtt, timer); 12586b2e6856STrond Myklebust timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 12596b2e6856STrond Myklebust if (timeout > max_timeout || timeout == 0) 12606b2e6856STrond Myklebust timeout = max_timeout; 12616b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 12626b2e6856STrond Myklebust jiffies + timeout); 12638ba6a92dSTrond Myklebust } 12648ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt); 12658ba6a92dSTrond Myklebust 12668ba6a92dSTrond Myklebust /** 12677f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 12687f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 12697f3a1d1eSTrond Myklebust * 12707f3a1d1eSTrond Myklebust */ 12717f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 12727f3a1d1eSTrond Myklebust { 12737f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12747f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12757f3a1d1eSTrond Myklebust 12767f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 12777f3a1d1eSTrond Myklebust return; 12787f3a1d1eSTrond Myklebust /* 12797f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 12807f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 12817f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 12827f3a1d1eSTrond Myklebust */ 12837f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 12847f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 12858ba6a92dSTrond Myklebust xprt->ops->wait_for_reply_request(task); 12867f3a1d1eSTrond Myklebust /* 12877f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 12887f3a1d1eSTrond Myklebust * connection was dropped in case the call to 12897f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 12907f3a1d1eSTrond Myklebust */ 12917f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 12927f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 12937f3a1d1eSTrond Myklebust task, -ENOTCONN); 12947f3a1d1eSTrond Myklebust } 12957f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 12967f3a1d1eSTrond Myklebust } 12977f3a1d1eSTrond Myklebust 1298944b0429STrond Myklebust static bool 1299944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1300944b0429STrond Myklebust { 1301762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1302944b0429STrond Myklebust } 1303944b0429STrond Myklebust 1304944b0429STrond Myklebust /** 1305944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1306944b0429STrond Myklebust * @task: pointer to rpc_task 1307944b0429STrond Myklebust * 1308944b0429STrond Myklebust * Add a task to the transmission queue. 1309944b0429STrond Myklebust */ 1310944b0429STrond Myklebust void 1311944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1312944b0429STrond Myklebust { 1313918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1314944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1315944b0429STrond Myklebust 1316944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1317e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1318944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 131975891f50STrond Myklebust /* 132075891f50STrond Myklebust * Requests that carry congestion control credits are added 132175891f50STrond Myklebust * to the head of the list to avoid starvation issues. 132275891f50STrond Myklebust */ 132375891f50STrond Myklebust if (req->rq_cong) { 132475891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 132575891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 132675891f50STrond Myklebust if (pos->rq_cong) 132775891f50STrond Myklebust continue; 132875891f50STrond Myklebust /* Note: req is added _before_ pos */ 132975891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 133075891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 133175891f50STrond Myklebust goto out; 133275891f50STrond Myklebust } 133386aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 133486aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 133586aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 133686aeee0eSTrond Myklebust continue; 133786aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 133886aeee0eSTrond Myklebust continue; 133986aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 134086aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 134186aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 134286aeee0eSTrond Myklebust goto out; 134386aeee0eSTrond Myklebust } 1344deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1345918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1346918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1347918f3c1fSTrond Myklebust continue; 1348918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1349918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 1350918f3c1fSTrond Myklebust goto out; 1351918f3c1fSTrond Myklebust } 135275891f50STrond Myklebust } 1353944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1354918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 1355918f3c1fSTrond Myklebust out: 1356d737e5d4STrond Myklebust atomic_long_inc(&xprt->xmit_queuelen); 1357944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1358944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1359944b0429STrond Myklebust } 1360944b0429STrond Myklebust } 1361944b0429STrond Myklebust 1362944b0429STrond Myklebust /** 1363944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1364944b0429STrond Myklebust * @task: pointer to rpc_task 1365944b0429STrond Myklebust * 1366944b0429STrond Myklebust * Remove a task from the transmission queue 1367944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1368944b0429STrond Myklebust */ 1369944b0429STrond Myklebust static void 1370944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1371944b0429STrond Myklebust { 1372918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1373918f3c1fSTrond Myklebust 1374918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1375918f3c1fSTrond Myklebust return; 1376918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1377918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1378918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1379918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1380918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1381918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1382918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1383918f3c1fSTrond Myklebust } 1384918f3c1fSTrond Myklebust } else 1385918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1386d737e5d4STrond Myklebust atomic_long_dec(&req->rq_xprt->xmit_queuelen); 1387944b0429STrond Myklebust } 1388944b0429STrond Myklebust 1389944b0429STrond Myklebust /** 1390944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1391944b0429STrond Myklebust * @task: pointer to rpc_task 1392944b0429STrond Myklebust * 1393944b0429STrond Myklebust * Remove a task from the transmission queue 1394944b0429STrond Myklebust */ 1395944b0429STrond Myklebust static void 1396944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1397944b0429STrond Myklebust { 1398944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1399944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1400944b0429STrond Myklebust 1401944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1402944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1403944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1404944b0429STrond Myklebust } 1405944b0429STrond Myklebust 14067f3a1d1eSTrond Myklebust /** 1407cc204d01STrond Myklebust * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue 1408cc204d01STrond Myklebust * @task: pointer to rpc_task 1409cc204d01STrond Myklebust * 1410cc204d01STrond Myklebust * Remove a task from the transmit and receive queues, and ensure that 1411cc204d01STrond Myklebust * it is not pinned by the receive work item. 1412cc204d01STrond Myklebust */ 1413cc204d01STrond Myklebust void 1414cc204d01STrond Myklebust xprt_request_dequeue_xprt(struct rpc_task *task) 1415cc204d01STrond Myklebust { 1416cc204d01STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1417cc204d01STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1418cc204d01STrond Myklebust 1419cc204d01STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1420cc204d01STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1421cc204d01STrond Myklebust xprt_is_pinned_rqst(req)) { 1422cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1423cc204d01STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1424cc204d01STrond Myklebust xprt_request_dequeue_receive_locked(task); 1425cc204d01STrond Myklebust while (xprt_is_pinned_rqst(req)) { 1426cc204d01STrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1427cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1428cc204d01STrond Myklebust xprt_wait_on_pinned_rqst(req); 1429cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1430cc204d01STrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1431cc204d01STrond Myklebust } 1432cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1433cc204d01STrond Myklebust } 1434cc204d01STrond Myklebust } 1435cc204d01STrond Myklebust 1436cc204d01STrond Myklebust /** 14379d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 14389d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 14399d96acbcSTrond Myklebust * 14409d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 14419d96acbcSTrond Myklebust * the request for transmission or receive. 14429d96acbcSTrond Myklebust */ 14439d96acbcSTrond Myklebust void 14449d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 14459d96acbcSTrond Myklebust { 14469d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 14479d96acbcSTrond Myklebust 14489d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 14499d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 14509d96acbcSTrond Myklebust } 14519d96acbcSTrond Myklebust 14529d96acbcSTrond Myklebust /** 1453762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1454762e4e67STrond Myklebust * @task: pointer to rpc_task 1455762e4e67STrond Myklebust * 1456762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1457762e4e67STrond Myklebust */ 1458762e4e67STrond Myklebust bool 1459762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1460762e4e67STrond Myklebust { 1461762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1462762e4e67STrond Myklebust } 1463762e4e67STrond Myklebust 1464762e4e67STrond Myklebust /** 14659903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 14669903cd1cSChuck Lever * @task: RPC task about to send a request 14679903cd1cSChuck Lever * 14681da177e4SLinus Torvalds */ 146990051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 14721da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 14731da177e4SLinus Torvalds 14745f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 14755f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1476944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 14775f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 14785f2f6bd9STrond Myklebust task, 0); 14795f2f6bd9STrond Myklebust return false; 14805f2f6bd9STrond Myklebust 14818a19a0b6STrond Myklebust } 14825f2f6bd9STrond Myklebust return true; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 1485e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 14865e5ce5beSTrond Myklebust { 14877638e0bfSChuck Lever struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 14887638e0bfSChuck Lever 14897638e0bfSChuck Lever xprt_inject_disconnect(xprt); 14907638e0bfSChuck Lever xprt_release_write(xprt, task); 14915e5ce5beSTrond Myklebust } 14925e5ce5beSTrond Myklebust 14939903cd1cSChuck Lever /** 149489f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 149589f90fe1STrond Myklebust * @req: pointer to request to transmit 149689f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 14979903cd1cSChuck Lever * 149889f90fe1STrond Myklebust * This performs the transmission of a single request. 149989f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 150089f90fe1STrond Myklebust * does need to be pinned. 150189f90fe1STrond Myklebust * Returns '0' on success. 15029903cd1cSChuck Lever */ 150389f90fe1STrond Myklebust static int 150489f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 15051da177e4SLinus Torvalds { 15061da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 150789f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 150890d91b0cSTrond Myklebust unsigned int connect_cookie; 1509dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1510ff699ea8SChuck Lever int status; 15111da177e4SLinus Torvalds 1512edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 151389f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 151489f90fe1STrond Myklebust status = 0; 1515944b0429STrond Myklebust goto out_dequeue; 151689f90fe1STrond Myklebust } 15173021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1518edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 151989f90fe1STrond Myklebust status = -EBADMSG; 1520944b0429STrond Myklebust goto out_dequeue; 15213021a5bbSTrond Myklebust } 1522ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) { 1523ae67bd38STrond Myklebust status = -ERESTARTSYS; 1524ae67bd38STrond Myklebust goto out_dequeue; 1525ae67bd38STrond Myklebust } 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 1528dcbbeda8STrond Myklebust /* 1529dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1530dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1531dcbbeda8STrond Myklebust * reply to the first transmission. 1532dcbbeda8STrond Myklebust */ 1533dcbbeda8STrond Myklebust req->rq_ntrans++; 1534dcbbeda8STrond Myklebust 1535c509f15aSChuck Lever trace_rpc_xdr_sendto(task, &req->rq_snd_buf); 153690d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1537adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1538c8485e4dSTrond Myklebust if (status != 0) { 1539dcbbeda8STrond Myklebust req->rq_ntrans--; 15400c77668dSChuck Lever trace_xprt_transmit(req, status); 154189f90fe1STrond Myklebust return status; 1542c8485e4dSTrond Myklebust } 15437ebbbc6eSTrond Myklebust 1544e936a597SChuck Lever if (is_retrans) { 1545dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1546e936a597SChuck Lever trace_xprt_retransmit(req); 1547e936a597SChuck Lever } 1548dcbbeda8STrond Myklebust 15494a068258SChuck Lever xprt_inject_disconnect(xprt); 1550c8485e4dSTrond Myklebust 1551468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1552b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 1553262ca07dSChuck Lever 1554262ca07dSChuck Lever xprt->stat.sends++; 1555262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1556262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 155715a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 155815a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1559b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 156090d91b0cSTrond Myklebust 156190d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1562944b0429STrond Myklebust out_dequeue: 15630c77668dSChuck Lever trace_xprt_transmit(req, status); 1564944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 156589f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 156689f90fe1STrond Myklebust return status; 156789f90fe1STrond Myklebust } 156889f90fe1STrond Myklebust 156989f90fe1STrond Myklebust /** 157089f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 157189f90fe1STrond Myklebust * @task: controlling RPC task 157289f90fe1STrond Myklebust * 157389f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 157489f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 157589f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 157689f90fe1STrond Myklebust * received a reply. 157789f90fe1STrond Myklebust */ 157889f90fe1STrond Myklebust void 157989f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 158089f90fe1STrond Myklebust { 158189f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 158289f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 15836f9f1728SChuck Lever int counter, status; 158489f90fe1STrond Myklebust 158589f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 15866f9f1728SChuck Lever counter = 0; 158789f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 15886f9f1728SChuck Lever if (++counter == 20) 15896f9f1728SChuck Lever break; 159089f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 159189f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 159289f90fe1STrond Myklebust xprt_pin_rqst(next); 159389f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 159489f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 159589f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 159689f90fe1STrond Myklebust status = 0; 159789f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 159889f90fe1STrond Myklebust xprt_unpin_rqst(next); 159989f90fe1STrond Myklebust if (status == 0) { 160089f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 160189f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 160289f90fe1STrond Myklebust continue; 1603c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 160489f90fe1STrond Myklebust task->tk_status = status; 160589f90fe1STrond Myklebust break; 160689f90fe1STrond Myklebust } 160789f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 1610ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1611ba60eb25STrond Myklebust { 1612ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1613ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1614ba60eb25STrond Myklebust } 1615ba60eb25STrond Myklebust 1616*e877a88dSNeilBrown static bool __xprt_set_rq(struct rpc_task *task, void *data) 1617ba60eb25STrond Myklebust { 1618*e877a88dSNeilBrown struct rpc_rqst *req = data; 1619*e877a88dSNeilBrown 1620*e877a88dSNeilBrown if (task->tk_rqstp == NULL) { 1621*e877a88dSNeilBrown memset(req, 0, sizeof(*req)); /* mark unused */ 1622*e877a88dSNeilBrown task->tk_status = -EAGAIN; 1623*e877a88dSNeilBrown task->tk_rqstp = req; 1624*e877a88dSNeilBrown return true; 1625*e877a88dSNeilBrown } 1626*e877a88dSNeilBrown return false; 1627*e877a88dSNeilBrown } 1628*e877a88dSNeilBrown 1629*e877a88dSNeilBrown static bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req) 1630*e877a88dSNeilBrown { 1631*e877a88dSNeilBrown if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) { 1632ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1633*e877a88dSNeilBrown return false; 1634*e877a88dSNeilBrown } 1635*e877a88dSNeilBrown return true; 1636ba60eb25STrond Myklebust } 1637ba60eb25STrond Myklebust 1638ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1639ba60eb25STrond Myklebust { 1640ba60eb25STrond Myklebust bool ret = false; 1641ba60eb25STrond Myklebust 1642ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1643ba60eb25STrond Myklebust goto out; 1644ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1645ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1646ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1647ba60eb25STrond Myklebust ret = true; 1648ba60eb25STrond Myklebust } 1649ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1650ba60eb25STrond Myklebust out: 1651ba60eb25STrond Myklebust return ret; 1652ba60eb25STrond Myklebust } 1653ba60eb25STrond Myklebust 165492ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1655d9ba131dSTrond Myklebust { 1656d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1657d9ba131dSTrond Myklebust 1658ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1659d9ba131dSTrond Myklebust goto out; 1660ff699ea8SChuck Lever ++xprt->num_reqs; 166192ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 166292ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 166392ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1664d9ba131dSTrond Myklebust if (req != NULL) 1665d9ba131dSTrond Myklebust goto out; 1666ff699ea8SChuck Lever --xprt->num_reqs; 1667d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1668d9ba131dSTrond Myklebust out: 1669d9ba131dSTrond Myklebust return req; 1670d9ba131dSTrond Myklebust } 1671d9ba131dSTrond Myklebust 1672d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1673d9ba131dSTrond Myklebust { 1674ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1675ff699ea8SChuck Lever --xprt->num_reqs; 1676d9ba131dSTrond Myklebust kfree(req); 1677d9ba131dSTrond Myklebust return true; 1678d9ba131dSTrond Myklebust } 1679d9ba131dSTrond Myklebust return false; 1680d9ba131dSTrond Myklebust } 1681d9ba131dSTrond Myklebust 1682f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 16831da177e4SLinus Torvalds { 1684d9ba131dSTrond Myklebust struct rpc_rqst *req; 16851da177e4SLinus Torvalds 1686f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 16871da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1688d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1689d9ba131dSTrond Myklebust list_del(&req->rq_list); 1690d9ba131dSTrond Myklebust goto out_init_req; 1691d9ba131dSTrond Myklebust } 169292ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1693d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1694d9ba131dSTrond Myklebust goto out_init_req; 1695d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1696d9ba131dSTrond Myklebust case -ENOMEM: 1697d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1698d9ba131dSTrond Myklebust "failed! Retrying\n"); 16991afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1700d9ba131dSTrond Myklebust break; 1701d9ba131dSTrond Myklebust case -EAGAIN: 1702ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1703d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1704df561f66SGustavo A. R. Silva fallthrough; 17051afeaf5cSTrond Myklebust default: 1706d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 17071afeaf5cSTrond Myklebust } 1708f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1709d9ba131dSTrond Myklebust return; 1710d9ba131dSTrond Myklebust out_init_req: 1711ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1712ff699ea8SChuck Lever xprt->num_reqs); 171337ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 171437ac86c3SChuck Lever 1715d9ba131dSTrond Myklebust task->tk_status = 0; 17161da177e4SLinus Torvalds task->tk_rqstp = req; 17171da177e4SLinus Torvalds } 1718f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1719f39c1bfbSTrond Myklebust 1720a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1721ee5ebe85STrond Myklebust { 1722ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1723*e877a88dSNeilBrown if (!xprt_wake_up_backlog(xprt, req) && 1724*e877a88dSNeilBrown !xprt_dynamic_free_slot(xprt, req)) { 1725c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1726ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1727c25573b5STrond Myklebust } 1728ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1729ee5ebe85STrond Myklebust } 1730a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1731ee5ebe85STrond Myklebust 173221de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 173321de0a95STrond Myklebust { 173421de0a95STrond Myklebust struct rpc_rqst *req; 173521de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 173621de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 173721de0a95STrond Myklebust list_del(&req->rq_list); 173821de0a95STrond Myklebust kfree(req); 173921de0a95STrond Myklebust } 174021de0a95STrond Myklebust } 174121de0a95STrond Myklebust 1742d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1743d9ba131dSTrond Myklebust unsigned int num_prealloc, 1744d9ba131dSTrond Myklebust unsigned int max_alloc) 1745bd1722d4SPavel Emelyanov { 1746bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 174721de0a95STrond Myklebust struct rpc_rqst *req; 174821de0a95STrond Myklebust int i; 1749bd1722d4SPavel Emelyanov 1750bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1751bd1722d4SPavel Emelyanov if (xprt == NULL) 1752bd1722d4SPavel Emelyanov goto out; 1753bd1722d4SPavel Emelyanov 175421de0a95STrond Myklebust xprt_init(xprt, net); 175521de0a95STrond Myklebust 175621de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 175721de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 175821de0a95STrond Myklebust if (!req) 17598313164cSwangweidong goto out_free; 176021de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 176121de0a95STrond Myklebust } 1762d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1763d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1764d9ba131dSTrond Myklebust else 176521de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1766d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1767ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1768bd1722d4SPavel Emelyanov 1769bd1722d4SPavel Emelyanov return xprt; 1770bd1722d4SPavel Emelyanov 1771bd1722d4SPavel Emelyanov out_free: 177221de0a95STrond Myklebust xprt_free(xprt); 1773bd1722d4SPavel Emelyanov out: 1774bd1722d4SPavel Emelyanov return NULL; 1775bd1722d4SPavel Emelyanov } 1776bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1777bd1722d4SPavel Emelyanov 1778e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1779e204e621SPavel Emelyanov { 178037aa2133SPavel Emelyanov put_net(xprt->xprt_net); 178121de0a95STrond Myklebust xprt_free_all_slots(xprt); 1782fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1783e204e621SPavel Emelyanov } 1784e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1785e204e621SPavel Emelyanov 1786902c5887STrond Myklebust static void 1787902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1788902c5887STrond Myklebust { 1789902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1790902c5887STrond Myklebust } 1791902c5887STrond Myklebust 17929dc6edcfSTrond Myklebust static __be32 17939dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 17949dc6edcfSTrond Myklebust { 17959dc6edcfSTrond Myklebust __be32 xid; 17969dc6edcfSTrond Myklebust 17979dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 17989dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 17999dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 18009dc6edcfSTrond Myklebust return xid; 18019dc6edcfSTrond Myklebust } 18029dc6edcfSTrond Myklebust 18039dc6edcfSTrond Myklebust static void 18049dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 18059dc6edcfSTrond Myklebust { 18069dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 18079dc6edcfSTrond Myklebust } 18089dc6edcfSTrond Myklebust 18099dc6edcfSTrond Myklebust static void 18109dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 18119dc6edcfSTrond Myklebust { 18129dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 18139dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 18149dc6edcfSTrond Myklebust 1815*e877a88dSNeilBrown if (req->rq_task) 1816*e877a88dSNeilBrown /* Already initialized */ 1817*e877a88dSNeilBrown return; 1818*e877a88dSNeilBrown 18199dc6edcfSTrond Myklebust req->rq_task = task; 18209dc6edcfSTrond Myklebust req->rq_xprt = xprt; 18219dc6edcfSTrond Myklebust req->rq_buffer = NULL; 18229dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1823902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 18249dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 18259dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 18269dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 18279dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 182871700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 182971700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 18309dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 1831da953063STrond Myklebust xprt_init_majortimeo(task, req); 183209d2ba0cSChuck Lever 183309d2ba0cSChuck Lever trace_xprt_reserve(req); 18349dc6edcfSTrond Myklebust } 18359dc6edcfSTrond Myklebust 18369dc6edcfSTrond Myklebust static void 18379dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 18389dc6edcfSTrond Myklebust { 18399dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 18409dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 18419dc6edcfSTrond Myklebust xprt_request_init(task); 18429dc6edcfSTrond Myklebust } 18439dc6edcfSTrond Myklebust 18449903cd1cSChuck Lever /** 18459903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 18469903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 18479903cd1cSChuck Lever * 1848ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1849ba60eb25STrond Myklebust * slots are available, place the task on the transport's 18509903cd1cSChuck Lever * backlog queue. 18519903cd1cSChuck Lever */ 18529903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 18531da177e4SLinus Torvalds { 1854fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 18551da177e4SLinus Torvalds 185643cedbf0STrond Myklebust task->tk_status = 0; 185743cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 185843cedbf0STrond Myklebust return; 185943cedbf0STrond Myklebust 186043cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1861ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 18629dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1863ba60eb25STrond Myklebust } 1864ba60eb25STrond Myklebust 1865ba60eb25STrond Myklebust /** 1866ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1867ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1868ba60eb25STrond Myklebust * 1869ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1870ba60eb25STrond Myklebust * backlog queue. 1871ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1872ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1873ba60eb25STrond Myklebust */ 1874ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1875ba60eb25STrond Myklebust { 1876fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1877ba60eb25STrond Myklebust 1878ba60eb25STrond Myklebust task->tk_status = 0; 1879*e877a88dSNeilBrown if (task->tk_rqstp != NULL) { 1880*e877a88dSNeilBrown xprt_request_init(task); 1881ba60eb25STrond Myklebust return; 1882*e877a88dSNeilBrown } 1883ba60eb25STrond Myklebust 1884ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 18859dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18889903cd1cSChuck Lever /** 18899903cd1cSChuck Lever * xprt_release - release an RPC request slot 18909903cd1cSChuck Lever * @task: task which is finished with the slot 18919903cd1cSChuck Lever * 18921da177e4SLinus Torvalds */ 18939903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 18941da177e4SLinus Torvalds { 189555ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 189687ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 18971da177e4SLinus Torvalds 189887ed5003STrond Myklebust if (req == NULL) { 189987ed5003STrond Myklebust if (task->tk_client) { 1900fb43d172STrond Myklebust xprt = task->tk_xprt; 190187ed5003STrond Myklebust xprt_release_write(xprt, task); 190287ed5003STrond Myklebust } 19031da177e4SLinus Torvalds return; 190487ed5003STrond Myklebust } 190555ae1aabSRicardo Labiaga 190655ae1aabSRicardo Labiaga xprt = req->rq_xprt; 1907*e877a88dSNeilBrown if (xprt) { 1908cc204d01STrond Myklebust xprt_request_dequeue_xprt(task); 1909b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 191049e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1911a58dd398SChuck Lever if (xprt->ops->release_request) 1912a58dd398SChuck Lever xprt->ops->release_request(task); 1913ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 1914b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 1915ee5ebe85STrond Myklebust if (req->rq_buffer) 19163435c74aSChuck Lever xprt->ops->buf_free(task); 19179d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 19180472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1919a17c2153STrond Myklebust if (req->rq_cred != NULL) 1920a17c2153STrond Myklebust put_rpccred(req->rq_cred); 1921ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1922ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 1923*e877a88dSNeilBrown } else 1924*e877a88dSNeilBrown xprt = task->tk_xprt; 192555ae1aabSRicardo Labiaga 1926*e877a88dSNeilBrown task->tk_rqstp = NULL; 1927ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1928a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1929ee5ebe85STrond Myklebust else 1930c9acb42eSTrond Myklebust xprt_free_bc_request(req); 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 1933902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1934902c5887STrond Myklebust void 1935902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1936902c5887STrond Myklebust { 1937902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1938902c5887STrond Myklebust 1939902c5887STrond Myklebust task->tk_rqstp = req; 1940902c5887STrond Myklebust req->rq_task = task; 1941902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1942902c5887STrond Myklebust /* 1943902c5887STrond Myklebust * Set up the xdr_buf length. 1944902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1945902c5887STrond Myklebust */ 1946902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1947902c5887STrond Myklebust xbufp->tail[0].iov_len; 1948902c5887STrond Myklebust } 1949902c5887STrond Myklebust #endif 1950902c5887STrond Myklebust 195121de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1952c2866763SChuck Lever { 195330c5116bSTrond Myklebust kref_init(&xprt->kref); 1954c2866763SChuck Lever 1955c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1956c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 195775c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1958c2866763SChuck Lever 1959c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 196095f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1961944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 19629e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1963f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1964f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 19659e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 196680b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1967f9acac1aSRicardo Labiaga 1968c2866763SChuck Lever xprt->last_used = jiffies; 1969c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1970a509050bSChuck Lever xprt->bind_index = 0; 1971c2866763SChuck Lever 1972c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1973c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 197479c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1975c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1976c2866763SChuck Lever 1977c2866763SChuck Lever xprt_init_xid(xprt); 1978c2866763SChuck Lever 197921de0a95STrond Myklebust xprt->xprt_net = get_net(net); 19808d9266ffSTrond Myklebust } 19818d9266ffSTrond Myklebust 19828d9266ffSTrond Myklebust /** 19838d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 19848d9266ffSTrond Myklebust * @args: rpc transport creation arguments 19858d9266ffSTrond Myklebust * 19868d9266ffSTrond Myklebust */ 19878d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 19888d9266ffSTrond Myklebust { 19898d9266ffSTrond Myklebust struct rpc_xprt *xprt; 19909bccd264STrond Myklebust const struct xprt_class *t; 19918d9266ffSTrond Myklebust 19929bccd264STrond Myklebust t = xprt_class_find_by_ident(args->ident); 19939bccd264STrond Myklebust if (!t) { 19943c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 19958d9266ffSTrond Myklebust return ERR_PTR(-EIO); 19969bccd264STrond Myklebust } 19978d9266ffSTrond Myklebust 19988d9266ffSTrond Myklebust xprt = t->setup(args); 19999bccd264STrond Myklebust xprt_class_release(t); 20009bccd264STrond Myklebust 2001911813d7SChuck Lever if (IS_ERR(xprt)) 200221de0a95STrond Myklebust goto out; 200333d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 200433d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 200521de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 200621de0a95STrond Myklebust if (xprt_has_timer(xprt)) 2007502980e8SAnna Schumaker timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 200821de0a95STrond Myklebust else 2009ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 20104e0038b6STrond Myklebust 20114e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 20124e0038b6STrond Myklebust xprt_destroy(xprt); 20134e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 20144e0038b6STrond Myklebust } 20154e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 20164e0038b6STrond Myklebust if (xprt->servername == NULL) { 20174e0038b6STrond Myklebust xprt_destroy(xprt); 20184e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 20194e0038b6STrond Myklebust } 20204e0038b6STrond Myklebust 20213f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 2022388f0c77SJeff Layton 2023911813d7SChuck Lever trace_xprt_create(xprt); 202421de0a95STrond Myklebust out: 2025c2866763SChuck Lever return xprt; 2026c2866763SChuck Lever } 2027c2866763SChuck Lever 2028528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 2029528fd354STrond Myklebust { 2030528fd354STrond Myklebust struct rpc_xprt *xprt = 2031528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 2032528fd354STrond Myklebust 2033911813d7SChuck Lever trace_xprt_destroy(xprt); 2034911813d7SChuck Lever 2035528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 2036528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 2037528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 2038528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 2039528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 2040528fd354STrond Myklebust kfree(xprt->servername); 2041528fd354STrond Myklebust /* 2042669996adSTrond Myklebust * Destroy any existing back channel 2043669996adSTrond Myklebust */ 2044669996adSTrond Myklebust xprt_destroy_backchannel(xprt, UINT_MAX); 2045669996adSTrond Myklebust 2046669996adSTrond Myklebust /* 2047528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 2048528fd354STrond Myklebust */ 2049528fd354STrond Myklebust xprt->ops->destroy(xprt); 2050528fd354STrond Myklebust } 2051528fd354STrond Myklebust 20529903cd1cSChuck Lever /** 20539903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 2054a8de240aSTrond Myklebust * @xprt: transport to destroy 20559903cd1cSChuck Lever * 20561da177e4SLinus Torvalds */ 2057a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 20581da177e4SLinus Torvalds { 2059528fd354STrond Myklebust /* 2060528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 2061528fd354STrond Myklebust */ 206279234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 206379234c3dSTrond Myklebust 20640065db32STrond Myklebust del_timer_sync(&xprt->timer); 2065c8541ecdSChuck Lever 2066c8541ecdSChuck Lever /* 2067528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 2068528fd354STrond Myklebust * safely flush receive work running on rpciod. 2069c8541ecdSChuck Lever */ 2070528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 2071528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 20726b6ca86bSTrond Myklebust } 20731da177e4SLinus Torvalds 207430c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 207530c5116bSTrond Myklebust { 207630c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 207730c5116bSTrond Myklebust } 207830c5116bSTrond Myklebust 207930c5116bSTrond Myklebust /** 208030c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 208130c5116bSTrond Myklebust * @xprt: pointer to the transport 208230c5116bSTrond Myklebust * 208330c5116bSTrond Myklebust */ 208430c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 208530c5116bSTrond Myklebust { 208630c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 208730c5116bSTrond Myklebust return xprt; 208830c5116bSTrond Myklebust return NULL; 208930c5116bSTrond Myklebust } 209030c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 209130c5116bSTrond Myklebust 20926b6ca86bSTrond Myklebust /** 20936b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 20946b6ca86bSTrond Myklebust * @xprt: pointer to the transport 20956b6ca86bSTrond Myklebust * 20966b6ca86bSTrond Myklebust */ 20976b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 20986b6ca86bSTrond Myklebust { 209930c5116bSTrond Myklebust if (xprt != NULL) 210030c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 21016b6ca86bSTrond Myklebust } 21025d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 2103