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); 731da177e4SLinus Torvalds 745ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7581c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7681c098afS\"Talpey, Thomas\ 779e910bffSTrond Myklebust static unsigned long xprt_request_timeout(const struct rpc_rqst *req) 789e910bffSTrond Myklebust { 799e910bffSTrond Myklebust unsigned long timeout = jiffies + req->rq_timeout; 809e910bffSTrond Myklebust 819e910bffSTrond Myklebust if (time_before(timeout, req->rq_majortimeo)) 829e910bffSTrond Myklebust return timeout; 839e910bffSTrond Myklebust return req->rq_majortimeo; 849e910bffSTrond Myklebust } 859e910bffSTrond Myklebust 8612a80469SChuck Lever /** 8781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 8881c098afS\"Talpey, Thomas\ * @transport: transport to register 8981c098afS\"Talpey, Thomas\ * 9081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 9181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 9281c098afS\"Talpey, Thomas\ * 9381c098afS\"Talpey, Thomas\ * Returns: 9481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 9581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 9681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 9781c098afS\"Talpey, Thomas\ */ 9881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 9981c098afS\"Talpey, Thomas\ { 10081c098afS\"Talpey, Thomas\ struct xprt_class *t; 10181c098afS\"Talpey, Thomas\ int result; 10281c098afS\"Talpey, Thomas\ 10381c098afS\"Talpey, Thomas\ result = -EEXIST; 10481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 10581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 10681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 1074fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 10881c098afS\"Talpey, Thomas\ goto out; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ 11181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 11281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 11381c098afS\"Talpey, Thomas\ transport->name); 11481c098afS\"Talpey, Thomas\ result = 0; 11581c098afS\"Talpey, Thomas\ 11681c098afS\"Talpey, Thomas\ out: 11781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 11881c098afS\"Talpey, Thomas\ return result; 11981c098afS\"Talpey, Thomas\ } 12081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 12181c098afS\"Talpey, Thomas\ 12281c098afS\"Talpey, Thomas\ /** 12381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 12465b6e42cSRandy Dunlap * @transport: transport to unregister 12581c098afS\"Talpey, Thomas\ * 12681c098afS\"Talpey, Thomas\ * Returns: 12781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 12881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 12981c098afS\"Talpey, Thomas\ */ 13081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 13181c098afS\"Talpey, Thomas\ { 13281c098afS\"Talpey, Thomas\ struct xprt_class *t; 13381c098afS\"Talpey, Thomas\ int result; 13481c098afS\"Talpey, Thomas\ 13581c098afS\"Talpey, Thomas\ result = 0; 13681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 13781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 13881c098afS\"Talpey, Thomas\ if (t == transport) { 13981c098afS\"Talpey, Thomas\ printk(KERN_INFO 14081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 14181c098afS\"Talpey, Thomas\ transport->name); 14281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 14381c098afS\"Talpey, Thomas\ goto out; 14481c098afS\"Talpey, Thomas\ } 14581c098afS\"Talpey, Thomas\ } 14681c098afS\"Talpey, Thomas\ result = -ENOENT; 14781c098afS\"Talpey, Thomas\ 14881c098afS\"Talpey, Thomas\ out: 14981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 15081c098afS\"Talpey, Thomas\ return result; 15181c098afS\"Talpey, Thomas\ } 15281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 15381c098afS\"Talpey, Thomas\ 154d5aa6b22STrond Myklebust static void 155d5aa6b22STrond Myklebust xprt_class_release(const struct xprt_class *t) 156d5aa6b22STrond Myklebust { 157d5aa6b22STrond Myklebust module_put(t->owner); 158d5aa6b22STrond Myklebust } 159d5aa6b22STrond Myklebust 160d5aa6b22STrond Myklebust static const struct xprt_class * 1619bccd264STrond Myklebust xprt_class_find_by_ident_locked(int ident) 1629bccd264STrond Myklebust { 1639bccd264STrond Myklebust const struct xprt_class *t; 1649bccd264STrond Myklebust 1659bccd264STrond Myklebust list_for_each_entry(t, &xprt_list, list) { 1669bccd264STrond Myklebust if (t->ident != ident) 1679bccd264STrond Myklebust continue; 1689bccd264STrond Myklebust if (!try_module_get(t->owner)) 1699bccd264STrond Myklebust continue; 1709bccd264STrond Myklebust return t; 1719bccd264STrond Myklebust } 1729bccd264STrond Myklebust return NULL; 1739bccd264STrond Myklebust } 1749bccd264STrond Myklebust 1759bccd264STrond Myklebust static const struct xprt_class * 1769bccd264STrond Myklebust xprt_class_find_by_ident(int ident) 1779bccd264STrond Myklebust { 1789bccd264STrond Myklebust const struct xprt_class *t; 1799bccd264STrond Myklebust 1809bccd264STrond Myklebust spin_lock(&xprt_list_lock); 1819bccd264STrond Myklebust t = xprt_class_find_by_ident_locked(ident); 1829bccd264STrond Myklebust spin_unlock(&xprt_list_lock); 1839bccd264STrond Myklebust return t; 1849bccd264STrond Myklebust } 1859bccd264STrond Myklebust 1869bccd264STrond Myklebust static const struct xprt_class * 187d5aa6b22STrond Myklebust xprt_class_find_by_netid_locked(const char *netid) 188d5aa6b22STrond Myklebust { 189d5aa6b22STrond Myklebust const struct xprt_class *t; 190d5aa6b22STrond Myklebust unsigned int i; 191d5aa6b22STrond Myklebust 192d5aa6b22STrond Myklebust list_for_each_entry(t, &xprt_list, list) { 193d5aa6b22STrond Myklebust for (i = 0; t->netid[i][0] != '\0'; i++) { 194d5aa6b22STrond Myklebust if (strcmp(t->netid[i], netid) != 0) 195d5aa6b22STrond Myklebust continue; 196d5aa6b22STrond Myklebust if (!try_module_get(t->owner)) 197d5aa6b22STrond Myklebust continue; 198d5aa6b22STrond Myklebust return t; 199d5aa6b22STrond Myklebust } 200d5aa6b22STrond Myklebust } 201d5aa6b22STrond Myklebust return NULL; 202d5aa6b22STrond Myklebust } 203d5aa6b22STrond Myklebust 204d5aa6b22STrond Myklebust static const struct xprt_class * 205d5aa6b22STrond Myklebust xprt_class_find_by_netid(const char *netid) 206d5aa6b22STrond Myklebust { 207d5aa6b22STrond Myklebust const struct xprt_class *t; 208d5aa6b22STrond Myklebust 209d5aa6b22STrond Myklebust spin_lock(&xprt_list_lock); 210d5aa6b22STrond Myklebust t = xprt_class_find_by_netid_locked(netid); 211d5aa6b22STrond Myklebust if (!t) { 212d5aa6b22STrond Myklebust spin_unlock(&xprt_list_lock); 213d5aa6b22STrond Myklebust request_module("rpc%s", netid); 214d5aa6b22STrond Myklebust spin_lock(&xprt_list_lock); 215d5aa6b22STrond Myklebust t = xprt_class_find_by_netid_locked(netid); 216d5aa6b22STrond Myklebust } 217d5aa6b22STrond Myklebust spin_unlock(&xprt_list_lock); 218d5aa6b22STrond Myklebust return t; 219d5aa6b22STrond Myklebust } 220d5aa6b22STrond Myklebust 22181c098afS\"Talpey, Thomas\ /** 2221fc5f131STrond Myklebust * xprt_find_transport_ident - convert a netid into a transport identifier 2231fc5f131STrond Myklebust * @netid: transport to load 2241fc5f131STrond Myklebust * 2251fc5f131STrond Myklebust * Returns: 2261fc5f131STrond Myklebust * > 0: transport identifier 2271fc5f131STrond Myklebust * -ENOENT: transport module not available 2281fc5f131STrond Myklebust */ 2291fc5f131STrond Myklebust int xprt_find_transport_ident(const char *netid) 2301fc5f131STrond Myklebust { 2311fc5f131STrond Myklebust const struct xprt_class *t; 2321fc5f131STrond Myklebust int ret; 2331fc5f131STrond Myklebust 2341fc5f131STrond Myklebust t = xprt_class_find_by_netid(netid); 2351fc5f131STrond Myklebust if (!t) 2361fc5f131STrond Myklebust return -ENOENT; 2371fc5f131STrond Myklebust ret = t->ident; 2381fc5f131STrond Myklebust xprt_class_release(t); 2391fc5f131STrond Myklebust return ret; 2401fc5f131STrond Myklebust } 2411fc5f131STrond Myklebust EXPORT_SYMBOL_GPL(xprt_find_transport_ident); 2421fc5f131STrond Myklebust 243c544577dSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 244c544577dSTrond Myklebust { 245c544577dSTrond Myklebust xprt->snd_task = NULL; 246c544577dSTrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 247c544577dSTrond Myklebust smp_mb__before_atomic(); 248c544577dSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 249c544577dSTrond Myklebust smp_mb__after_atomic(); 250c544577dSTrond Myklebust } else 251c544577dSTrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 252c544577dSTrond Myklebust } 253c544577dSTrond Myklebust 254441e3e24STom Talpey /** 25512a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 25612a80469SChuck Lever * @task: task that is requesting access to the transport 257177c27bfSRandy Dunlap * @xprt: pointer to the target transport 25812a80469SChuck Lever * 25912a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 26012a80469SChuck Lever * transport connects from colliding with writes. No congestion control 26112a80469SChuck Lever * is provided. 2621da177e4SLinus Torvalds */ 26343cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 2641da177e4SLinus Torvalds { 26512a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 26612a80469SChuck Lever 26712a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 26812a80469SChuck Lever if (task == xprt->snd_task) 269bf7ca707SChuck Lever goto out_locked; 27012a80469SChuck Lever goto out_sleep; 27112a80469SChuck Lever } 272c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 273c544577dSTrond Myklebust goto out_unlock; 27412a80469SChuck Lever xprt->snd_task = task; 2754d4a76f3Sj223yang@asset.uwaterloo.ca 276bf7ca707SChuck Lever out_locked: 277bf7ca707SChuck Lever trace_xprt_reserve_xprt(xprt, task); 27812a80469SChuck Lever return 1; 27912a80469SChuck Lever 280c544577dSTrond Myklebust out_unlock: 281c544577dSTrond Myklebust xprt_clear_locked(xprt); 28212a80469SChuck Lever out_sleep: 28312a80469SChuck Lever task->tk_status = -EAGAIN; 2846b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 2856b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 2869e910bffSTrond Myklebust xprt_request_timeout(req)); 2876b2e6856STrond Myklebust else 28879c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 28912a80469SChuck Lever return 0; 29012a80469SChuck Lever } 29112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 29212a80469SChuck Lever 29375891f50STrond Myklebust static bool 29475891f50STrond Myklebust xprt_need_congestion_window_wait(struct rpc_xprt *xprt) 29575891f50STrond Myklebust { 29675891f50STrond Myklebust return test_bit(XPRT_CWND_WAIT, &xprt->state); 29775891f50STrond Myklebust } 29875891f50STrond Myklebust 29975891f50STrond Myklebust static void 30075891f50STrond Myklebust xprt_set_congestion_window_wait(struct rpc_xprt *xprt) 30175891f50STrond Myklebust { 30275891f50STrond Myklebust if (!list_empty(&xprt->xmit_queue)) { 30375891f50STrond Myklebust /* Peek at head of queue to see if it can make progress */ 30475891f50STrond Myklebust if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst, 30575891f50STrond Myklebust rq_xmit)->rq_cong) 30675891f50STrond Myklebust return; 30775891f50STrond Myklebust } 30875891f50STrond Myklebust set_bit(XPRT_CWND_WAIT, &xprt->state); 30975891f50STrond Myklebust } 31075891f50STrond Myklebust 31175891f50STrond Myklebust static void 31275891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt) 31375891f50STrond Myklebust { 31475891f50STrond Myklebust if (!RPCXPRT_CONGESTED(xprt)) 31575891f50STrond Myklebust clear_bit(XPRT_CWND_WAIT, &xprt->state); 31675891f50STrond Myklebust } 31775891f50STrond Myklebust 31812a80469SChuck Lever /* 31912a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 32012a80469SChuck Lever * @task: task that is requesting access to the transport 32112a80469SChuck Lever * 32212a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 32312a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 32412a80469SChuck Lever * woken up and given access to the transport. 32575891f50STrond Myklebust * Note that the lock is only granted if we know there are free slots. 32612a80469SChuck Lever */ 32743cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 32812a80469SChuck Lever { 3291da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3301da177e4SLinus Torvalds 3312226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 3321da177e4SLinus Torvalds if (task == xprt->snd_task) 333bf7ca707SChuck Lever goto out_locked; 3341da177e4SLinus Torvalds goto out_sleep; 3351da177e4SLinus Torvalds } 33643cedbf0STrond Myklebust if (req == NULL) { 33743cedbf0STrond Myklebust xprt->snd_task = task; 338bf7ca707SChuck Lever goto out_locked; 33943cedbf0STrond Myklebust } 340c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 341c544577dSTrond Myklebust goto out_unlock; 34275891f50STrond Myklebust if (!xprt_need_congestion_window_wait(xprt)) { 3431da177e4SLinus Torvalds xprt->snd_task = task; 344bf7ca707SChuck Lever goto out_locked; 3451da177e4SLinus Torvalds } 346c544577dSTrond Myklebust out_unlock: 347632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3481da177e4SLinus Torvalds out_sleep: 3491da177e4SLinus Torvalds task->tk_status = -EAGAIN; 3506b2e6856STrond Myklebust if (RPC_IS_SOFT(task)) 3516b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->sending, task, NULL, 3529e910bffSTrond Myklebust xprt_request_timeout(req)); 3536b2e6856STrond Myklebust else 35479c99152STrond Myklebust rpc_sleep_on(&xprt->sending, task, NULL); 3551da177e4SLinus Torvalds return 0; 356bf7ca707SChuck Lever out_locked: 357bf7ca707SChuck Lever trace_xprt_reserve_cong(xprt, task); 358bf7ca707SChuck Lever return 1; 3591da177e4SLinus Torvalds } 36012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 3611da177e4SLinus Torvalds 36212a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds int retval; 3651da177e4SLinus Torvalds 366bd79bc57STrond Myklebust if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task) 367bd79bc57STrond Myklebust return 1; 368b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 36943cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 370b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 3711da177e4SLinus Torvalds return retval; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 374961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 3751da177e4SLinus Torvalds { 376961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 37749e9a890SChuck Lever 37849e9a890SChuck Lever xprt->snd_task = task; 379961a828dSTrond Myklebust return true; 380961a828dSTrond Myklebust } 381961a828dSTrond Myklebust 382961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 383961a828dSTrond Myklebust { 384961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 38549e9a890SChuck Lever return; 386c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 387c544577dSTrond Myklebust goto out_unlock; 388f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 389f1dc237cSTrond Myklebust __xprt_lock_write_func, xprt)) 390961a828dSTrond Myklebust return; 391c544577dSTrond Myklebust out_unlock: 392632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 39349e9a890SChuck Lever } 39449e9a890SChuck Lever 395961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 396961a828dSTrond Myklebust { 397961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 398961a828dSTrond Myklebust return; 399c544577dSTrond Myklebust if (test_bit(XPRT_WRITE_SPACE, &xprt->state)) 400c544577dSTrond Myklebust goto out_unlock; 40175891f50STrond Myklebust if (xprt_need_congestion_window_wait(xprt)) 402961a828dSTrond Myklebust goto out_unlock; 403f1dc237cSTrond Myklebust if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending, 40475891f50STrond Myklebust __xprt_lock_write_func, xprt)) 405961a828dSTrond Myklebust return; 4061da177e4SLinus Torvalds out_unlock: 407632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 41049e9a890SChuck Lever /** 41149e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 41249e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 41349e9a890SChuck Lever * @task: task that is releasing access to the transport 41449e9a890SChuck Lever * 41549e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 4161da177e4SLinus Torvalds */ 41749e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds if (xprt->snd_task == task) { 420632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 4211da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 4221da177e4SLinus Torvalds } 423bf7ca707SChuck Lever trace_xprt_release_xprt(xprt, task); 4241da177e4SLinus Torvalds } 42512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 4261da177e4SLinus Torvalds 42749e9a890SChuck Lever /** 42849e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 42949e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 43049e9a890SChuck Lever * @task: task that is releasing access to the transport 43149e9a890SChuck Lever * 43249e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 43349e9a890SChuck Lever * transport if the transport's congestion window allows it. 43449e9a890SChuck Lever */ 43549e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 43649e9a890SChuck Lever { 43749e9a890SChuck Lever if (xprt->snd_task == task) { 438632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 43949e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 44049e9a890SChuck Lever } 441bf7ca707SChuck Lever trace_xprt_release_cong(xprt, task); 44249e9a890SChuck Lever } 44312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 44449e9a890SChuck Lever 44549e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 4461da177e4SLinus Torvalds { 447bd79bc57STrond Myklebust if (xprt->snd_task != task) 448bd79bc57STrond Myklebust return; 449b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 45049e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 451b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* 4551da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 4561da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds static int 45975891f50STrond Myklebust __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds if (req->rq_cong) 4621da177e4SLinus Torvalds return 1; 463bf7ca707SChuck Lever trace_xprt_get_cong(xprt, req->rq_task); 46475891f50STrond Myklebust if (RPCXPRT_CONGESTED(xprt)) { 46575891f50STrond Myklebust xprt_set_congestion_window_wait(xprt); 4661da177e4SLinus Torvalds return 0; 46775891f50STrond Myklebust } 4681da177e4SLinus Torvalds req->rq_cong = 1; 4691da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 4701da177e4SLinus Torvalds return 1; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4751da177e4SLinus Torvalds * that has been sleeping due to congestion 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds static void 4781da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds if (!req->rq_cong) 4811da177e4SLinus Torvalds return; 4821da177e4SLinus Torvalds req->rq_cong = 0; 4831da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 48475891f50STrond Myklebust xprt_test_and_clear_congestion_window_wait(xprt); 485bf7ca707SChuck Lever trace_xprt_put_cong(xprt, req->rq_task); 48649e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 48946c0ee8bSChuck Lever /** 49075891f50STrond Myklebust * xprt_request_get_cong - Request congestion control credits 49175891f50STrond Myklebust * @xprt: pointer to transport 49275891f50STrond Myklebust * @req: pointer to RPC request 49375891f50STrond Myklebust * 49475891f50STrond Myklebust * Useful for transports that require congestion control. 49575891f50STrond Myklebust */ 49675891f50STrond Myklebust bool 49775891f50STrond Myklebust xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 49875891f50STrond Myklebust { 49975891f50STrond Myklebust bool ret = false; 50075891f50STrond Myklebust 50175891f50STrond Myklebust if (req->rq_cong) 50275891f50STrond Myklebust return true; 503b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 50475891f50STrond Myklebust ret = __xprt_get_cong(xprt, req) != 0; 505b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 50675891f50STrond Myklebust return ret; 50775891f50STrond Myklebust } 50875891f50STrond Myklebust EXPORT_SYMBOL_GPL(xprt_request_get_cong); 50975891f50STrond Myklebust 51075891f50STrond Myklebust /** 511a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 512a58dd398SChuck Lever * @task: RPC request that recently completed 513a58dd398SChuck Lever * 514a58dd398SChuck Lever * Useful for transports that require congestion control. 515a58dd398SChuck Lever */ 516a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 517a58dd398SChuck Lever { 518a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 519a4f0835cSTrond Myklebust 520a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 521a58dd398SChuck Lever } 52212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 523a58dd398SChuck Lever 5248593e010SChuck Lever static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt) 5258593e010SChuck Lever { 5268593e010SChuck Lever if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) 5278593e010SChuck Lever __xprt_lock_write_next_cong(xprt); 5288593e010SChuck Lever } 5298593e010SChuck Lever 53075891f50STrond Myklebust /* 53175891f50STrond Myklebust * Clear the congestion window wait flag and wake up the next 53275891f50STrond Myklebust * entry on xprt->sending 53375891f50STrond Myklebust */ 53475891f50STrond Myklebust static void 53575891f50STrond Myklebust xprt_clear_congestion_window_wait(struct rpc_xprt *xprt) 53675891f50STrond Myklebust { 53775891f50STrond Myklebust if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) { 538b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 53975891f50STrond Myklebust __xprt_lock_write_next_cong(xprt); 540b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 54175891f50STrond Myklebust } 54275891f50STrond Myklebust } 54375891f50STrond Myklebust 544a58dd398SChuck Lever /** 54546c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 5466a24dfb6STrond Myklebust * @xprt: pointer to xprt 54746c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 54846c0ee8bSChuck Lever * @result: result code of completed RPC request 54946c0ee8bSChuck Lever * 5504f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 5514f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 5524f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 5534f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 5544f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 5554f4cf5adSChuck Lever * 5564f4cf5adSChuck Lever * - a reply is received and 5574f4cf5adSChuck Lever * - a full number of requests are outstanding and 5584f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 5591da177e4SLinus Torvalds */ 5606a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 5611da177e4SLinus Torvalds { 56246c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 56346c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 5661da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 5671da177e4SLinus Torvalds * the result gets rounded properly. */ 5681da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 5691da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 5701da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 57149e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 5721da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 5731da177e4SLinus Torvalds cwnd >>= 1; 5741da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 5751da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 5781da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 5791da177e4SLinus Torvalds xprt->cwnd = cwnd; 58046c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 5811da177e4SLinus Torvalds } 58212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 5831da177e4SLinus Torvalds 58444fbac22SChuck Lever /** 58544fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 58644fbac22SChuck Lever * @xprt: transport with waiting tasks 58744fbac22SChuck Lever * @status: result code to plant in each task before waking it 58844fbac22SChuck Lever * 58944fbac22SChuck Lever */ 59044fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 59144fbac22SChuck Lever { 59244fbac22SChuck Lever if (status < 0) 59344fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 59444fbac22SChuck Lever else 59544fbac22SChuck Lever rpc_wake_up(&xprt->pending); 59644fbac22SChuck Lever } 59712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 59844fbac22SChuck Lever 599c7b2cae8SChuck Lever /** 600c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 601c544577dSTrond Myklebust * @xprt: transport 602a9a6b52eSTrond Myklebust * 603a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 604a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 605a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 606c7b2cae8SChuck Lever */ 607c544577dSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_xprt *xprt) 608c7b2cae8SChuck Lever { 609c544577dSTrond Myklebust set_bit(XPRT_WRITE_SPACE, &xprt->state); 610c7b2cae8SChuck Lever } 61112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 612c7b2cae8SChuck Lever 613c544577dSTrond Myklebust static bool 614c544577dSTrond Myklebust xprt_clear_write_space_locked(struct rpc_xprt *xprt) 615c544577dSTrond Myklebust { 616c544577dSTrond Myklebust if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) { 617c544577dSTrond Myklebust __xprt_lock_write_next(xprt); 618c544577dSTrond Myklebust dprintk("RPC: write space: waking waiting task on " 619c544577dSTrond Myklebust "xprt %p\n", xprt); 620c544577dSTrond Myklebust return true; 621c544577dSTrond Myklebust } 622c544577dSTrond Myklebust return false; 623c544577dSTrond Myklebust } 624c544577dSTrond Myklebust 625c7b2cae8SChuck Lever /** 626c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 627c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 628c7b2cae8SChuck Lever * 629c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 630c7b2cae8SChuck Lever */ 631c544577dSTrond Myklebust bool xprt_write_space(struct rpc_xprt *xprt) 632c7b2cae8SChuck Lever { 633c544577dSTrond Myklebust bool ret; 634c544577dSTrond Myklebust 635c544577dSTrond Myklebust if (!test_bit(XPRT_WRITE_SPACE, &xprt->state)) 636c544577dSTrond Myklebust return false; 637b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 638c544577dSTrond Myklebust ret = xprt_clear_write_space_locked(xprt); 639b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 640c544577dSTrond Myklebust return ret; 641c7b2cae8SChuck Lever } 64212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 643c7b2cae8SChuck Lever 644da953063STrond Myklebust static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime) 645da953063STrond Myklebust { 646da953063STrond Myklebust s64 delta = ktime_to_ns(ktime_get() - abstime); 647da953063STrond Myklebust return likely(delta >= 0) ? 648da953063STrond Myklebust jiffies - nsecs_to_jiffies(delta) : 649da953063STrond Myklebust jiffies + nsecs_to_jiffies(-delta); 650da953063STrond Myklebust } 651da953063STrond Myklebust 652da953063STrond Myklebust static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req) 6531da177e4SLinus Torvalds { 654ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 655da953063STrond Myklebust unsigned long majortimeo = req->rq_timeout; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds if (to->to_exponential) 658da953063STrond Myklebust majortimeo <<= to->to_retries; 6591da177e4SLinus Torvalds else 660da953063STrond Myklebust majortimeo += to->to_increment * to->to_retries; 661da953063STrond Myklebust if (majortimeo > to->to_maxval || majortimeo == 0) 662da953063STrond Myklebust majortimeo = to->to_maxval; 663da953063STrond Myklebust return majortimeo; 664da953063STrond Myklebust } 665da953063STrond Myklebust 666da953063STrond Myklebust static void xprt_reset_majortimeo(struct rpc_rqst *req) 667da953063STrond Myklebust { 668da953063STrond Myklebust req->rq_majortimeo += xprt_calc_majortimeo(req); 669da953063STrond Myklebust } 670da953063STrond Myklebust 6717de62bc0SOlga Kornievskaia static void xprt_reset_minortimeo(struct rpc_rqst *req) 6727de62bc0SOlga Kornievskaia { 6737de62bc0SOlga Kornievskaia req->rq_minortimeo += req->rq_timeout; 6747de62bc0SOlga Kornievskaia } 6757de62bc0SOlga Kornievskaia 676da953063STrond Myklebust static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req) 677da953063STrond Myklebust { 678da953063STrond Myklebust unsigned long time_init; 679da953063STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 680da953063STrond Myklebust 681da953063STrond Myklebust if (likely(xprt && xprt_connected(xprt))) 682da953063STrond Myklebust time_init = jiffies; 683da953063STrond Myklebust else 684da953063STrond Myklebust time_init = xprt_abs_ktime_to_jiffies(task->tk_start); 685da953063STrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 686da953063STrond Myklebust req->rq_majortimeo = time_init + xprt_calc_majortimeo(req); 6877de62bc0SOlga Kornievskaia req->rq_minortimeo = time_init + req->rq_timeout; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6909903cd1cSChuck Lever /** 6919903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 6929903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 6939903cd1cSChuck Lever * 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 6961da177e4SLinus Torvalds { 6971da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 698ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 6991da177e4SLinus Torvalds int status = 0; 7001da177e4SLinus Torvalds 7017de62bc0SOlga Kornievskaia if (time_before(jiffies, req->rq_minortimeo)) 7027de62bc0SOlga Kornievskaia return status; 7031da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 7041da177e4SLinus Torvalds if (to->to_exponential) 7051da177e4SLinus Torvalds req->rq_timeout <<= 1; 7061da177e4SLinus Torvalds else 7071da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 7081da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 7091da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 7101da177e4SLinus Torvalds req->rq_retries++; 7111da177e4SLinus Torvalds } else { 7121da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 7131da177e4SLinus Torvalds req->rq_retries = 0; 7141da177e4SLinus Torvalds xprt_reset_majortimeo(req); 7151da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 716b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7171da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 718b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7191da177e4SLinus Torvalds status = -ETIMEDOUT; 7201da177e4SLinus Torvalds } 7217de62bc0SOlga Kornievskaia xprt_reset_minortimeo(req); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds if (req->rq_timeout == 0) { 7241da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 7251da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds return status; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 73065f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 7311da177e4SLinus Torvalds { 73265f27f38SDavid Howells struct rpc_xprt *xprt = 73365f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 734a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 7351da177e4SLinus Torvalds 736911813d7SChuck Lever trace_xprt_disconnect_auto(xprt); 73766af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 7384876cc77STrond Myklebust xprt->ops->close(xprt); 7391da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 74079234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 741a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7449903cd1cSChuck Lever /** 74562da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 7469903cd1cSChuck Lever * @xprt: transport to flag for disconnect 7479903cd1cSChuck Lever * 7481da177e4SLinus Torvalds */ 74962da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 7501da177e4SLinus Torvalds { 751911813d7SChuck Lever trace_xprt_disconnect_done(xprt); 752b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 7531da177e4SLinus Torvalds xprt_clear_connected(xprt); 754c544577dSTrond Myklebust xprt_clear_write_space_locked(xprt); 7558593e010SChuck Lever xprt_clear_congestion_window_wait_locked(xprt); 75627adc785STrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 757b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 7581da177e4SLinus Torvalds } 75962da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 7601da177e4SLinus Torvalds 76166af1e55STrond Myklebust /** 76266af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 76366af1e55STrond Myklebust * @xprt: transport to disconnect 76466af1e55STrond Myklebust * 76566af1e55STrond Myklebust */ 76666af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 76766af1e55STrond Myklebust { 768911813d7SChuck Lever trace_xprt_disconnect_force(xprt); 769911813d7SChuck Lever 77066af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 771b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 77266af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 77366af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 77466af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 77540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 7760445f92cSTrond Myklebust else if (xprt->snd_task) 7770445f92cSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 7780445f92cSTrond Myklebust xprt->snd_task, -ENOTCONN); 779b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 78066af1e55STrond Myklebust } 781e2a4f4fbSChuck Lever EXPORT_SYMBOL_GPL(xprt_force_disconnect); 78266af1e55STrond Myklebust 7837f3a1d1eSTrond Myklebust static unsigned int 7847f3a1d1eSTrond Myklebust xprt_connect_cookie(struct rpc_xprt *xprt) 7857f3a1d1eSTrond Myklebust { 7867f3a1d1eSTrond Myklebust return READ_ONCE(xprt->connect_cookie); 7877f3a1d1eSTrond Myklebust } 7887f3a1d1eSTrond Myklebust 7897f3a1d1eSTrond Myklebust static bool 7907f3a1d1eSTrond Myklebust xprt_request_retransmit_after_disconnect(struct rpc_task *task) 7917f3a1d1eSTrond Myklebust { 7927f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 7937f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 7947f3a1d1eSTrond Myklebust 7957f3a1d1eSTrond Myklebust return req->rq_connect_cookie != xprt_connect_cookie(xprt) || 7967f3a1d1eSTrond Myklebust !xprt_connected(xprt); 7977f3a1d1eSTrond Myklebust } 7987f3a1d1eSTrond Myklebust 7997c1d71cfSTrond Myklebust /** 8007c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 8017c1d71cfSTrond Myklebust * @xprt: transport to disconnect 8027c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 8037c1d71cfSTrond Myklebust * 8047c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 8057c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 8067c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 8077c1d71cfSTrond Myklebust * a batch of RPC requests. 8087c1d71cfSTrond Myklebust * 8097c1d71cfSTrond Myklebust */ 8107c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 8117c1d71cfSTrond Myklebust { 8127c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 813b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 8147c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 8157c1d71cfSTrond Myklebust goto out; 8162c2ee6d2SNeilBrown if (test_bit(XPRT_CLOSING, &xprt->state)) 8177c1d71cfSTrond Myklebust goto out; 8187c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 8197c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 8207c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 82140a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 8222a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 8237c1d71cfSTrond Myklebust out: 824b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 8257c1d71cfSTrond Myklebust } 8267c1d71cfSTrond Myklebust 827ad3331acSTrond Myklebust static bool 828ad3331acSTrond Myklebust xprt_has_timer(const struct rpc_xprt *xprt) 829ad3331acSTrond Myklebust { 830ad3331acSTrond Myklebust return xprt->idle_timeout != 0; 831ad3331acSTrond Myklebust } 832ad3331acSTrond Myklebust 833ad3331acSTrond Myklebust static void 834ad3331acSTrond Myklebust xprt_schedule_autodisconnect(struct rpc_xprt *xprt) 835ad3331acSTrond Myklebust __must_hold(&xprt->transport_lock) 836ad3331acSTrond Myklebust { 83780d3c45fSDave Wysochanski xprt->last_used = jiffies; 83895f7691dSTrond Myklebust if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt)) 839ad3331acSTrond Myklebust mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout); 840ad3331acSTrond Myklebust } 841ad3331acSTrond Myklebust 8421da177e4SLinus Torvalds static void 843ff861c4dSKees Cook xprt_init_autodisconnect(struct timer_list *t) 8441da177e4SLinus Torvalds { 845ff861c4dSKees Cook struct rpc_xprt *xprt = from_timer(xprt, t, timer); 8461da177e4SLinus Torvalds 84795f7691dSTrond Myklebust if (!RB_EMPTY_ROOT(&xprt->recv_queue)) 848b5e92419STrond Myklebust return; 849ad3331acSTrond Myklebust /* Reset xprt->last_used to avoid connect/autodisconnect cycling */ 850ad3331acSTrond Myklebust xprt->last_used = jiffies; 8512226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 8521da177e4SLinus Torvalds return; 853b5e92419STrond Myklebust queue_work(xprtiod_workqueue, &xprt->task_cleanup); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 856718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 857718ba5b8STrond Myklebust struct rpc_task *task, 858718ba5b8STrond Myklebust void *cookie) 859718ba5b8STrond Myklebust { 860718ba5b8STrond Myklebust bool ret = false; 861718ba5b8STrond Myklebust 862b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 863718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 864718ba5b8STrond Myklebust goto out; 865718ba5b8STrond Myklebust if (xprt->snd_task != task) 866718ba5b8STrond Myklebust goto out; 867718ba5b8STrond Myklebust xprt->snd_task = cookie; 868718ba5b8STrond Myklebust ret = true; 869718ba5b8STrond Myklebust out: 870b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 871718ba5b8STrond Myklebust return ret; 872718ba5b8STrond Myklebust } 873718ba5b8STrond Myklebust 874718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 875718ba5b8STrond Myklebust { 876b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 877718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 878718ba5b8STrond Myklebust goto out; 879718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 880718ba5b8STrond Myklebust goto out; 881718ba5b8STrond Myklebust xprt->snd_task =NULL; 882718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 883ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 884718ba5b8STrond Myklebust out: 885b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 88679234c3dSTrond Myklebust wake_up_bit(&xprt->state, XPRT_LOCKED); 887718ba5b8STrond Myklebust } 888718ba5b8STrond Myklebust 8899903cd1cSChuck Lever /** 8909903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 8919903cd1cSChuck Lever * @task: RPC task that is requesting the connect 8921da177e4SLinus Torvalds * 8931da177e4SLinus Torvalds */ 8941da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 8951da177e4SLinus Torvalds { 896ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 8971da177e4SLinus Torvalds 898db0a86c4SChuck Lever trace_xprt_connect(xprt); 8991da177e4SLinus Torvalds 900ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 90101d37c42STrond Myklebust task->tk_status = -EAGAIN; 9021da177e4SLinus Torvalds return; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 9051da177e4SLinus Torvalds return; 906feb8ca37STrond Myklebust 907911813d7SChuck Lever if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 908911813d7SChuck Lever trace_xprt_disconnect_cleanup(xprt); 909feb8ca37STrond Myklebust xprt->ops->close(xprt); 910911813d7SChuck Lever } 911feb8ca37STrond Myklebust 912718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 9132c2ee6d2SNeilBrown task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie; 9146b2e6856STrond Myklebust rpc_sleep_on_timeout(&xprt->pending, task, NULL, 9159e910bffSTrond Myklebust xprt_request_timeout(task->tk_rqstp)); 9160b9e7943STrond Myklebust 9170b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 9180b9e7943STrond Myklebust return; 9190b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 9200b9e7943STrond Myklebust return; 9210a9a4304STrond Myklebust /* Race breaker */ 9220a9a4304STrond Myklebust if (!xprt_connected(xprt)) { 923262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 9241b092092STrond Myklebust xprt->ops->connect(xprt, task); 9250a9a4304STrond Myklebust } else { 9260a9a4304STrond Myklebust xprt_clear_connecting(xprt); 9270a9a4304STrond Myklebust task->tk_status = 0; 9280a9a4304STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 9290a9a4304STrond Myklebust } 9301da177e4SLinus Torvalds } 931718ba5b8STrond Myklebust xprt_release_write(xprt, task); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934675dd90aSChuck Lever /** 935675dd90aSChuck Lever * xprt_reconnect_delay - compute the wait before scheduling a connect 936675dd90aSChuck Lever * @xprt: transport instance 937675dd90aSChuck Lever * 938675dd90aSChuck Lever */ 939675dd90aSChuck Lever unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt) 940675dd90aSChuck Lever { 941675dd90aSChuck Lever unsigned long start, now = jiffies; 942675dd90aSChuck Lever 943675dd90aSChuck Lever start = xprt->stat.connect_start + xprt->reestablish_timeout; 944675dd90aSChuck Lever if (time_after(start, now)) 945675dd90aSChuck Lever return start - now; 946675dd90aSChuck Lever return 0; 947675dd90aSChuck Lever } 948675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_delay); 949675dd90aSChuck Lever 950675dd90aSChuck Lever /** 951675dd90aSChuck Lever * xprt_reconnect_backoff - compute the new re-establish timeout 952675dd90aSChuck Lever * @xprt: transport instance 953675dd90aSChuck Lever * @init_to: initial reestablish timeout 954675dd90aSChuck Lever * 955675dd90aSChuck Lever */ 956675dd90aSChuck Lever void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to) 957675dd90aSChuck Lever { 958675dd90aSChuck Lever xprt->reestablish_timeout <<= 1; 959675dd90aSChuck Lever if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 960675dd90aSChuck Lever xprt->reestablish_timeout = xprt->max_reconnect_timeout; 961675dd90aSChuck Lever if (xprt->reestablish_timeout < init_to) 962675dd90aSChuck Lever xprt->reestablish_timeout = init_to; 963675dd90aSChuck Lever } 964675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprt_reconnect_backoff); 965675dd90aSChuck Lever 96695f7691dSTrond Myklebust enum xprt_xid_rb_cmp { 96795f7691dSTrond Myklebust XID_RB_EQUAL, 96895f7691dSTrond Myklebust XID_RB_LEFT, 96995f7691dSTrond Myklebust XID_RB_RIGHT, 97095f7691dSTrond Myklebust }; 97195f7691dSTrond Myklebust static enum xprt_xid_rb_cmp 97295f7691dSTrond Myklebust xprt_xid_cmp(__be32 xid1, __be32 xid2) 97395f7691dSTrond Myklebust { 97495f7691dSTrond Myklebust if (xid1 == xid2) 97595f7691dSTrond Myklebust return XID_RB_EQUAL; 97695f7691dSTrond Myklebust if ((__force u32)xid1 < (__force u32)xid2) 97795f7691dSTrond Myklebust return XID_RB_LEFT; 97895f7691dSTrond Myklebust return XID_RB_RIGHT; 97995f7691dSTrond Myklebust } 98095f7691dSTrond Myklebust 98195f7691dSTrond Myklebust static struct rpc_rqst * 98295f7691dSTrond Myklebust xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid) 98395f7691dSTrond Myklebust { 98495f7691dSTrond Myklebust struct rb_node *n = xprt->recv_queue.rb_node; 98595f7691dSTrond Myklebust struct rpc_rqst *req; 98695f7691dSTrond Myklebust 98795f7691dSTrond Myklebust while (n != NULL) { 98895f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 98995f7691dSTrond Myklebust switch (xprt_xid_cmp(xid, req->rq_xid)) { 99095f7691dSTrond Myklebust case XID_RB_LEFT: 99195f7691dSTrond Myklebust n = n->rb_left; 99295f7691dSTrond Myklebust break; 99395f7691dSTrond Myklebust case XID_RB_RIGHT: 99495f7691dSTrond Myklebust n = n->rb_right; 99595f7691dSTrond Myklebust break; 99695f7691dSTrond Myklebust case XID_RB_EQUAL: 99795f7691dSTrond Myklebust return req; 99895f7691dSTrond Myklebust } 99995f7691dSTrond Myklebust } 100095f7691dSTrond Myklebust return NULL; 100195f7691dSTrond Myklebust } 100295f7691dSTrond Myklebust 100395f7691dSTrond Myklebust static void 100495f7691dSTrond Myklebust xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new) 100595f7691dSTrond Myklebust { 100695f7691dSTrond Myklebust struct rb_node **p = &xprt->recv_queue.rb_node; 100795f7691dSTrond Myklebust struct rb_node *n = NULL; 100895f7691dSTrond Myklebust struct rpc_rqst *req; 100995f7691dSTrond Myklebust 101095f7691dSTrond Myklebust while (*p != NULL) { 101195f7691dSTrond Myklebust n = *p; 101295f7691dSTrond Myklebust req = rb_entry(n, struct rpc_rqst, rq_recv); 101395f7691dSTrond Myklebust switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) { 101495f7691dSTrond Myklebust case XID_RB_LEFT: 101595f7691dSTrond Myklebust p = &n->rb_left; 101695f7691dSTrond Myklebust break; 101795f7691dSTrond Myklebust case XID_RB_RIGHT: 101895f7691dSTrond Myklebust p = &n->rb_right; 101995f7691dSTrond Myklebust break; 102095f7691dSTrond Myklebust case XID_RB_EQUAL: 102195f7691dSTrond Myklebust WARN_ON_ONCE(new != req); 102295f7691dSTrond Myklebust return; 102395f7691dSTrond Myklebust } 102495f7691dSTrond Myklebust } 102595f7691dSTrond Myklebust rb_link_node(&new->rq_recv, n, p); 102695f7691dSTrond Myklebust rb_insert_color(&new->rq_recv, &xprt->recv_queue); 102795f7691dSTrond Myklebust } 102895f7691dSTrond Myklebust 102995f7691dSTrond Myklebust static void 103095f7691dSTrond Myklebust xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req) 103195f7691dSTrond Myklebust { 103295f7691dSTrond Myklebust rb_erase(&req->rq_recv, &xprt->recv_queue); 103395f7691dSTrond Myklebust } 103495f7691dSTrond Myklebust 10359903cd1cSChuck Lever /** 10369903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 10379903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 10389903cd1cSChuck Lever * @xid: RPC XID of incoming reply 10399903cd1cSChuck Lever * 104075c84151STrond Myklebust * Caller holds xprt->queue_lock. 10411da177e4SLinus Torvalds */ 1042d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 10431da177e4SLinus Torvalds { 10448f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 10451da177e4SLinus Torvalds 104695f7691dSTrond Myklebust entry = xprt_request_rb_find(xprt, xid); 104795f7691dSTrond Myklebust if (entry != NULL) { 10483705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 10490b87a46bSChuck Lever entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime); 1050262ca07dSChuck Lever return entry; 10513705ad64SJeff Layton } 105246121cf7SChuck Lever 105346121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 105446121cf7SChuck Lever ntohl(xid)); 10553705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 1056262ca07dSChuck Lever xprt->stat.bad_xids++; 1057262ca07dSChuck Lever return NULL; 10581da177e4SLinus Torvalds } 105912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 10601da177e4SLinus Torvalds 1061cf9946cdSTrond Myklebust static bool 1062cf9946cdSTrond Myklebust xprt_is_pinned_rqst(struct rpc_rqst *req) 1063cf9946cdSTrond Myklebust { 1064cf9946cdSTrond Myklebust return atomic_read(&req->rq_pin) != 0; 1065cf9946cdSTrond Myklebust } 1066cf9946cdSTrond Myklebust 1067729749bbSTrond Myklebust /** 1068729749bbSTrond Myklebust * xprt_pin_rqst - Pin a request on the transport receive list 1069729749bbSTrond Myklebust * @req: Request to pin 1070729749bbSTrond Myklebust * 1071729749bbSTrond Myklebust * Caller must ensure this is atomic with the call to xprt_lookup_rqst() 10721f7d1c73SChuck Lever * so should be holding xprt->queue_lock. 1073729749bbSTrond Myklebust */ 1074729749bbSTrond Myklebust void xprt_pin_rqst(struct rpc_rqst *req) 1075729749bbSTrond Myklebust { 1076cf9946cdSTrond Myklebust atomic_inc(&req->rq_pin); 1077729749bbSTrond Myklebust } 10789590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_pin_rqst); 1079729749bbSTrond Myklebust 1080729749bbSTrond Myklebust /** 1081729749bbSTrond Myklebust * xprt_unpin_rqst - Unpin a request on the transport receive list 1082729749bbSTrond Myklebust * @req: Request to pin 1083729749bbSTrond Myklebust * 10841f7d1c73SChuck Lever * Caller should be holding xprt->queue_lock. 1085729749bbSTrond Myklebust */ 1086729749bbSTrond Myklebust void xprt_unpin_rqst(struct rpc_rqst *req) 1087729749bbSTrond Myklebust { 1088cf9946cdSTrond Myklebust if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) { 1089cf9946cdSTrond Myklebust atomic_dec(&req->rq_pin); 1090cf9946cdSTrond Myklebust return; 1091cf9946cdSTrond Myklebust } 1092cf9946cdSTrond Myklebust if (atomic_dec_and_test(&req->rq_pin)) 1093cf9946cdSTrond Myklebust wake_up_var(&req->rq_pin); 1094729749bbSTrond Myklebust } 10959590d083SChuck Lever EXPORT_SYMBOL_GPL(xprt_unpin_rqst); 1096729749bbSTrond Myklebust 1097729749bbSTrond Myklebust static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req) 1098729749bbSTrond Myklebust { 1099cf9946cdSTrond Myklebust wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req)); 1100729749bbSTrond Myklebust } 1101729749bbSTrond Myklebust 1102edc81dcdSTrond Myklebust static bool 1103edc81dcdSTrond Myklebust xprt_request_data_received(struct rpc_task *task) 1104edc81dcdSTrond Myklebust { 1105edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1106edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0; 1107edc81dcdSTrond Myklebust } 1108edc81dcdSTrond Myklebust 1109edc81dcdSTrond Myklebust static bool 1110edc81dcdSTrond Myklebust xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req) 1111edc81dcdSTrond Myklebust { 1112edc81dcdSTrond Myklebust return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) && 1113edc81dcdSTrond Myklebust READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0; 1114edc81dcdSTrond Myklebust } 1115edc81dcdSTrond Myklebust 1116edc81dcdSTrond Myklebust /** 1117edc81dcdSTrond Myklebust * xprt_request_enqueue_receive - Add an request to the receive queue 1118edc81dcdSTrond Myklebust * @task: RPC task 1119edc81dcdSTrond Myklebust * 1120edc81dcdSTrond Myklebust */ 1121edc81dcdSTrond Myklebust void 1122edc81dcdSTrond Myklebust xprt_request_enqueue_receive(struct rpc_task *task) 1123edc81dcdSTrond Myklebust { 1124edc81dcdSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1125edc81dcdSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1126edc81dcdSTrond Myklebust 1127edc81dcdSTrond Myklebust if (!xprt_request_need_enqueue_receive(task, req)) 1128edc81dcdSTrond Myklebust return; 112975369089STrond Myklebust 113075369089STrond Myklebust xprt_request_prepare(task->tk_rqstp); 1131edc81dcdSTrond Myklebust spin_lock(&xprt->queue_lock); 1132edc81dcdSTrond Myklebust 1133edc81dcdSTrond Myklebust /* Update the softirq receive buffer */ 1134edc81dcdSTrond Myklebust memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 1135edc81dcdSTrond Myklebust sizeof(req->rq_private_buf)); 1136edc81dcdSTrond Myklebust 1137edc81dcdSTrond Myklebust /* Add request to the receive list */ 113895f7691dSTrond Myklebust xprt_request_rb_insert(xprt, req); 1139edc81dcdSTrond Myklebust set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate); 1140edc81dcdSTrond Myklebust spin_unlock(&xprt->queue_lock); 1141edc81dcdSTrond Myklebust 1142edc81dcdSTrond Myklebust /* Turn off autodisconnect */ 1143edc81dcdSTrond Myklebust del_singleshot_timer_sync(&xprt->timer); 1144edc81dcdSTrond Myklebust } 1145edc81dcdSTrond Myklebust 1146edc81dcdSTrond Myklebust /** 1147edc81dcdSTrond Myklebust * xprt_request_dequeue_receive_locked - Remove a request from the receive queue 1148edc81dcdSTrond Myklebust * @task: RPC task 1149edc81dcdSTrond Myklebust * 1150edc81dcdSTrond Myklebust * Caller must hold xprt->queue_lock. 1151edc81dcdSTrond Myklebust */ 1152edc81dcdSTrond Myklebust static void 1153edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(struct rpc_task *task) 1154edc81dcdSTrond Myklebust { 115595f7691dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 115695f7691dSTrond Myklebust 1157edc81dcdSTrond Myklebust if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 115895f7691dSTrond Myklebust xprt_request_rb_remove(req->rq_xprt, req); 1159edc81dcdSTrond Myklebust } 1160edc81dcdSTrond Myklebust 1161ecd465eeSChuck Lever /** 1162ecd465eeSChuck Lever * xprt_update_rtt - Update RPC RTT statistics 1163ecd465eeSChuck Lever * @task: RPC request that recently completed 1164ecd465eeSChuck Lever * 116575c84151STrond Myklebust * Caller holds xprt->queue_lock. 1166ecd465eeSChuck Lever */ 1167ecd465eeSChuck Lever void xprt_update_rtt(struct rpc_task *task) 11681da177e4SLinus Torvalds { 11691570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 11701570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 117195c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 1172d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 11731570c1e4SChuck Lever 11741da177e4SLinus Torvalds if (timer) { 11751da177e4SLinus Torvalds if (req->rq_ntrans == 1) 1176ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 11771570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds } 1180ecd465eeSChuck Lever EXPORT_SYMBOL_GPL(xprt_update_rtt); 11811da177e4SLinus Torvalds 11821570c1e4SChuck Lever /** 11831570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 11841570c1e4SChuck Lever * @task: RPC request that recently completed 11851570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 11861570c1e4SChuck Lever * 118775c84151STrond Myklebust * Caller holds xprt->queue_lock. 11881570c1e4SChuck Lever */ 11891570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 11901570c1e4SChuck Lever { 11911570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 1192fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 11931da177e4SLinus Torvalds 1194fda13939STrond Myklebust xprt->stat.recvs++; 1195ef759a2eSChuck Lever 11961e799b67STrond Myklebust req->rq_private_buf.len = copied; 1197dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 1198dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 119943ac3f29STrond Myklebust smp_wmb(); 1200dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 1201edc81dcdSTrond Myklebust xprt_request_dequeue_receive_locked(task); 1202fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 12031da177e4SLinus Torvalds } 120412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 12051da177e4SLinus Torvalds 120646c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 12071da177e4SLinus Torvalds { 12081da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12091da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 12101da177e4SLinus Torvalds 12115d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 12125d00837bSTrond Myklebust return; 121346c0ee8bSChuck Lever 121482476d9fSChuck Lever trace_xprt_timer(xprt, req->rq_xid, task->tk_status); 1215dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 121646c0ee8bSChuck Lever if (xprt->ops->timer) 12176a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 12185d00837bSTrond Myklebust } else 12195d00837bSTrond Myklebust task->tk_status = 0; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12229903cd1cSChuck Lever /** 12238ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_def - wait for reply 12248ba6a92dSTrond Myklebust * @task: pointer to rpc_task 12258ba6a92dSTrond Myklebust * 12268ba6a92dSTrond Myklebust * Set a request's retransmit timeout based on the transport's 12278ba6a92dSTrond Myklebust * default timeout parameters. Used by transports that don't adjust 12288ba6a92dSTrond Myklebust * the retransmit timeout based on round-trip time estimation, 12298ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 12308ba6a92dSTrond Myklebust */ 12318ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_def(struct rpc_task *task) 12328ba6a92dSTrond Myklebust { 12338ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12348ba6a92dSTrond Myklebust 12356b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 12369e910bffSTrond Myklebust xprt_request_timeout(req)); 12378ba6a92dSTrond Myklebust } 12388ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def); 12398ba6a92dSTrond Myklebust 12408ba6a92dSTrond Myklebust /** 12418ba6a92dSTrond Myklebust * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator 12428ba6a92dSTrond Myklebust * @task: pointer to rpc_task 12438ba6a92dSTrond Myklebust * 12448ba6a92dSTrond Myklebust * Set a request's retransmit timeout using the RTT estimator, 12458ba6a92dSTrond Myklebust * and put the task to sleep on the pending queue. 12468ba6a92dSTrond Myklebust */ 12478ba6a92dSTrond Myklebust void xprt_wait_for_reply_request_rtt(struct rpc_task *task) 12488ba6a92dSTrond Myklebust { 12498ba6a92dSTrond Myklebust int timer = task->tk_msg.rpc_proc->p_timer; 12508ba6a92dSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 12518ba6a92dSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 12528ba6a92dSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12538ba6a92dSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 12546b2e6856STrond Myklebust unsigned long timeout; 12558ba6a92dSTrond Myklebust 12566b2e6856STrond Myklebust timeout = rpc_calc_rto(rtt, timer); 12576b2e6856STrond Myklebust timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 12586b2e6856STrond Myklebust if (timeout > max_timeout || timeout == 0) 12596b2e6856STrond Myklebust timeout = max_timeout; 12606b2e6856STrond Myklebust rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer, 12616b2e6856STrond Myklebust jiffies + timeout); 12628ba6a92dSTrond Myklebust } 12638ba6a92dSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt); 12648ba6a92dSTrond Myklebust 12658ba6a92dSTrond Myklebust /** 12667f3a1d1eSTrond Myklebust * xprt_request_wait_receive - wait for the reply to an RPC request 12677f3a1d1eSTrond Myklebust * @task: RPC task about to send a request 12687f3a1d1eSTrond Myklebust * 12697f3a1d1eSTrond Myklebust */ 12707f3a1d1eSTrond Myklebust void xprt_request_wait_receive(struct rpc_task *task) 12717f3a1d1eSTrond Myklebust { 12727f3a1d1eSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12737f3a1d1eSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 12747f3a1d1eSTrond Myklebust 12757f3a1d1eSTrond Myklebust if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) 12767f3a1d1eSTrond Myklebust return; 12777f3a1d1eSTrond Myklebust /* 12787f3a1d1eSTrond Myklebust * Sleep on the pending queue if we're expecting a reply. 12797f3a1d1eSTrond Myklebust * The spinlock ensures atomicity between the test of 12807f3a1d1eSTrond Myklebust * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on(). 12817f3a1d1eSTrond Myklebust */ 12827f3a1d1eSTrond Myklebust spin_lock(&xprt->queue_lock); 12837f3a1d1eSTrond Myklebust if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) { 12848ba6a92dSTrond Myklebust xprt->ops->wait_for_reply_request(task); 12857f3a1d1eSTrond Myklebust /* 12867f3a1d1eSTrond Myklebust * Send an extra queue wakeup call if the 12877f3a1d1eSTrond Myklebust * connection was dropped in case the call to 12887f3a1d1eSTrond Myklebust * rpc_sleep_on() raced. 12897f3a1d1eSTrond Myklebust */ 12907f3a1d1eSTrond Myklebust if (xprt_request_retransmit_after_disconnect(task)) 12917f3a1d1eSTrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->pending, 12927f3a1d1eSTrond Myklebust task, -ENOTCONN); 12937f3a1d1eSTrond Myklebust } 12947f3a1d1eSTrond Myklebust spin_unlock(&xprt->queue_lock); 12957f3a1d1eSTrond Myklebust } 12967f3a1d1eSTrond Myklebust 1297944b0429STrond Myklebust static bool 1298944b0429STrond Myklebust xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req) 1299944b0429STrond Myklebust { 1300762e4e67STrond Myklebust return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1301944b0429STrond Myklebust } 1302944b0429STrond Myklebust 1303944b0429STrond Myklebust /** 1304944b0429STrond Myklebust * xprt_request_enqueue_transmit - queue a task for transmission 1305944b0429STrond Myklebust * @task: pointer to rpc_task 1306944b0429STrond Myklebust * 1307944b0429STrond Myklebust * Add a task to the transmission queue. 1308944b0429STrond Myklebust */ 1309944b0429STrond Myklebust void 1310944b0429STrond Myklebust xprt_request_enqueue_transmit(struct rpc_task *task) 1311944b0429STrond Myklebust { 1312918f3c1fSTrond Myklebust struct rpc_rqst *pos, *req = task->tk_rqstp; 1313944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1314944b0429STrond Myklebust 1315944b0429STrond Myklebust if (xprt_request_need_enqueue_transmit(task, req)) { 1316e66721f0STrond Myklebust req->rq_bytes_sent = 0; 1317944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 131875891f50STrond Myklebust /* 131975891f50STrond Myklebust * Requests that carry congestion control credits are added 132075891f50STrond Myklebust * to the head of the list to avoid starvation issues. 132175891f50STrond Myklebust */ 132275891f50STrond Myklebust if (req->rq_cong) { 132375891f50STrond Myklebust xprt_clear_congestion_window_wait(xprt); 132475891f50STrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 132575891f50STrond Myklebust if (pos->rq_cong) 132675891f50STrond Myklebust continue; 132775891f50STrond Myklebust /* Note: req is added _before_ pos */ 132875891f50STrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 132975891f50STrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 133075891f50STrond Myklebust goto out; 133175891f50STrond Myklebust } 133286aeee0eSTrond Myklebust } else if (RPC_IS_SWAPPER(task)) { 133386aeee0eSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 133486aeee0eSTrond Myklebust if (pos->rq_cong || pos->rq_bytes_sent) 133586aeee0eSTrond Myklebust continue; 133686aeee0eSTrond Myklebust if (RPC_IS_SWAPPER(pos->rq_task)) 133786aeee0eSTrond Myklebust continue; 133886aeee0eSTrond Myklebust /* Note: req is added _before_ pos */ 133986aeee0eSTrond Myklebust list_add_tail(&req->rq_xmit, &pos->rq_xmit); 134086aeee0eSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 134186aeee0eSTrond Myklebust goto out; 134286aeee0eSTrond Myklebust } 1343deaa5c96SChuck Lever } else if (!req->rq_seqno) { 1344918f3c1fSTrond Myklebust list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) { 1345918f3c1fSTrond Myklebust if (pos->rq_task->tk_owner != task->tk_owner) 1346918f3c1fSTrond Myklebust continue; 1347918f3c1fSTrond Myklebust list_add_tail(&req->rq_xmit2, &pos->rq_xmit2); 1348918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit); 1349918f3c1fSTrond Myklebust goto out; 1350918f3c1fSTrond Myklebust } 135175891f50STrond Myklebust } 1352944b0429STrond Myklebust list_add_tail(&req->rq_xmit, &xprt->xmit_queue); 1353918f3c1fSTrond Myklebust INIT_LIST_HEAD(&req->rq_xmit2); 1354918f3c1fSTrond Myklebust out: 1355*d737e5d4STrond Myklebust atomic_long_inc(&xprt->xmit_queuelen); 1356944b0429STrond Myklebust set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1357944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1358944b0429STrond Myklebust } 1359944b0429STrond Myklebust } 1360944b0429STrond Myklebust 1361944b0429STrond Myklebust /** 1362944b0429STrond Myklebust * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue 1363944b0429STrond Myklebust * @task: pointer to rpc_task 1364944b0429STrond Myklebust * 1365944b0429STrond Myklebust * Remove a task from the transmission queue 1366944b0429STrond Myklebust * Caller must hold xprt->queue_lock 1367944b0429STrond Myklebust */ 1368944b0429STrond Myklebust static void 1369944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(struct rpc_task *task) 1370944b0429STrond Myklebust { 1371918f3c1fSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1372918f3c1fSTrond Myklebust 1373918f3c1fSTrond Myklebust if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 1374918f3c1fSTrond Myklebust return; 1375918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit)) { 1376918f3c1fSTrond Myklebust list_del(&req->rq_xmit); 1377918f3c1fSTrond Myklebust if (!list_empty(&req->rq_xmit2)) { 1378918f3c1fSTrond Myklebust struct rpc_rqst *next = list_first_entry(&req->rq_xmit2, 1379918f3c1fSTrond Myklebust struct rpc_rqst, rq_xmit2); 1380918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1381918f3c1fSTrond Myklebust list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue); 1382918f3c1fSTrond Myklebust } 1383918f3c1fSTrond Myklebust } else 1384918f3c1fSTrond Myklebust list_del(&req->rq_xmit2); 1385*d737e5d4STrond Myklebust atomic_long_dec(&req->rq_xprt->xmit_queuelen); 1386944b0429STrond Myklebust } 1387944b0429STrond Myklebust 1388944b0429STrond Myklebust /** 1389944b0429STrond Myklebust * xprt_request_dequeue_transmit - remove a task from the transmission queue 1390944b0429STrond Myklebust * @task: pointer to rpc_task 1391944b0429STrond Myklebust * 1392944b0429STrond Myklebust * Remove a task from the transmission queue 1393944b0429STrond Myklebust */ 1394944b0429STrond Myklebust static void 1395944b0429STrond Myklebust xprt_request_dequeue_transmit(struct rpc_task *task) 1396944b0429STrond Myklebust { 1397944b0429STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1398944b0429STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1399944b0429STrond Myklebust 1400944b0429STrond Myklebust spin_lock(&xprt->queue_lock); 1401944b0429STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1402944b0429STrond Myklebust spin_unlock(&xprt->queue_lock); 1403944b0429STrond Myklebust } 1404944b0429STrond Myklebust 14057f3a1d1eSTrond Myklebust /** 1406cc204d01STrond Myklebust * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue 1407cc204d01STrond Myklebust * @task: pointer to rpc_task 1408cc204d01STrond Myklebust * 1409cc204d01STrond Myklebust * Remove a task from the transmit and receive queues, and ensure that 1410cc204d01STrond Myklebust * it is not pinned by the receive work item. 1411cc204d01STrond Myklebust */ 1412cc204d01STrond Myklebust void 1413cc204d01STrond Myklebust xprt_request_dequeue_xprt(struct rpc_task *task) 1414cc204d01STrond Myklebust { 1415cc204d01STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 1416cc204d01STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 1417cc204d01STrond Myklebust 1418cc204d01STrond Myklebust if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) || 1419cc204d01STrond Myklebust test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) || 1420cc204d01STrond Myklebust xprt_is_pinned_rqst(req)) { 1421cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1422cc204d01STrond Myklebust xprt_request_dequeue_transmit_locked(task); 1423cc204d01STrond Myklebust xprt_request_dequeue_receive_locked(task); 1424cc204d01STrond Myklebust while (xprt_is_pinned_rqst(req)) { 1425cc204d01STrond Myklebust set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1426cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1427cc204d01STrond Myklebust xprt_wait_on_pinned_rqst(req); 1428cc204d01STrond Myklebust spin_lock(&xprt->queue_lock); 1429cc204d01STrond Myklebust clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate); 1430cc204d01STrond Myklebust } 1431cc204d01STrond Myklebust spin_unlock(&xprt->queue_lock); 1432cc204d01STrond Myklebust } 1433cc204d01STrond Myklebust } 1434cc204d01STrond Myklebust 1435cc204d01STrond Myklebust /** 14369d96acbcSTrond Myklebust * xprt_request_prepare - prepare an encoded request for transport 14379d96acbcSTrond Myklebust * @req: pointer to rpc_rqst 14389d96acbcSTrond Myklebust * 14399d96acbcSTrond Myklebust * Calls into the transport layer to do whatever is needed to prepare 14409d96acbcSTrond Myklebust * the request for transmission or receive. 14419d96acbcSTrond Myklebust */ 14429d96acbcSTrond Myklebust void 14439d96acbcSTrond Myklebust xprt_request_prepare(struct rpc_rqst *req) 14449d96acbcSTrond Myklebust { 14459d96acbcSTrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 14469d96acbcSTrond Myklebust 14479d96acbcSTrond Myklebust if (xprt->ops->prepare_request) 14489d96acbcSTrond Myklebust xprt->ops->prepare_request(req); 14499d96acbcSTrond Myklebust } 14509d96acbcSTrond Myklebust 14519d96acbcSTrond Myklebust /** 1452762e4e67STrond Myklebust * xprt_request_need_retransmit - Test if a task needs retransmission 1453762e4e67STrond Myklebust * @task: pointer to rpc_task 1454762e4e67STrond Myklebust * 1455762e4e67STrond Myklebust * Test for whether a connection breakage requires the task to retransmit 1456762e4e67STrond Myklebust */ 1457762e4e67STrond Myklebust bool 1458762e4e67STrond Myklebust xprt_request_need_retransmit(struct rpc_task *task) 1459762e4e67STrond Myklebust { 1460762e4e67STrond Myklebust return xprt_request_retransmit_after_disconnect(task); 1461762e4e67STrond Myklebust } 1462762e4e67STrond Myklebust 1463762e4e67STrond Myklebust /** 14649903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 14659903cd1cSChuck Lever * @task: RPC task about to send a request 14669903cd1cSChuck Lever * 14671da177e4SLinus Torvalds */ 146890051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 14691da177e4SLinus Torvalds { 14701da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 14711da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 14721da177e4SLinus Torvalds 14735f2f6bd9STrond Myklebust if (!xprt_lock_write(xprt, task)) { 14749ce07ae5SChuck Lever trace_xprt_transmit_queued(xprt, task); 14759ce07ae5SChuck Lever 14765f2f6bd9STrond Myklebust /* Race breaker: someone may have transmitted us */ 1477944b0429STrond Myklebust if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 14785f2f6bd9STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, 14795f2f6bd9STrond Myklebust task, 0); 14805f2f6bd9STrond Myklebust return false; 14815f2f6bd9STrond Myklebust 14828a19a0b6STrond Myklebust } 14835f2f6bd9STrond Myklebust return true; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 1486e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 14875e5ce5beSTrond Myklebust { 1488343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 14895e5ce5beSTrond Myklebust } 14905e5ce5beSTrond Myklebust 14919903cd1cSChuck Lever /** 149289f90fe1STrond Myklebust * xprt_request_transmit - send an RPC request on a transport 149389f90fe1STrond Myklebust * @req: pointer to request to transmit 149489f90fe1STrond Myklebust * @snd_task: RPC task that owns the transport lock 14959903cd1cSChuck Lever * 149689f90fe1STrond Myklebust * This performs the transmission of a single request. 149789f90fe1STrond Myklebust * Note that if the request is not the same as snd_task, then it 149889f90fe1STrond Myklebust * does need to be pinned. 149989f90fe1STrond Myklebust * Returns '0' on success. 15009903cd1cSChuck Lever */ 150189f90fe1STrond Myklebust static int 150289f90fe1STrond Myklebust xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 150589f90fe1STrond Myklebust struct rpc_task *task = req->rq_task; 150690d91b0cSTrond Myklebust unsigned int connect_cookie; 1507dcbbeda8STrond Myklebust int is_retrans = RPC_WAS_SENT(task); 1508ff699ea8SChuck Lever int status; 15091da177e4SLinus Torvalds 1510edc81dcdSTrond Myklebust if (!req->rq_bytes_sent) { 151189f90fe1STrond Myklebust if (xprt_request_data_received(task)) { 151289f90fe1STrond Myklebust status = 0; 1513944b0429STrond Myklebust goto out_dequeue; 151489f90fe1STrond Myklebust } 15153021a5bbSTrond Myklebust /* Verify that our message lies in the RPCSEC_GSS window */ 1516edc81dcdSTrond Myklebust if (rpcauth_xmit_need_reencode(task)) { 151789f90fe1STrond Myklebust status = -EBADMSG; 1518944b0429STrond Myklebust goto out_dequeue; 15193021a5bbSTrond Myklebust } 1520ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) { 1521ae67bd38STrond Myklebust status = -ERESTARTSYS; 1522ae67bd38STrond Myklebust goto out_dequeue; 1523ae67bd38STrond Myklebust } 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 1526dcbbeda8STrond Myklebust /* 1527dcbbeda8STrond Myklebust * Update req->rq_ntrans before transmitting to avoid races with 1528dcbbeda8STrond Myklebust * xprt_update_rtt(), which needs to know that it is recording a 1529dcbbeda8STrond Myklebust * reply to the first transmission. 1530dcbbeda8STrond Myklebust */ 1531dcbbeda8STrond Myklebust req->rq_ntrans++; 1532dcbbeda8STrond Myklebust 1533c509f15aSChuck Lever trace_rpc_xdr_sendto(task, &req->rq_snd_buf); 153490d91b0cSTrond Myklebust connect_cookie = xprt->connect_cookie; 1535adfa7144STrond Myklebust status = xprt->ops->send_request(req); 1536c8485e4dSTrond Myklebust if (status != 0) { 1537dcbbeda8STrond Myklebust req->rq_ntrans--; 15380c77668dSChuck Lever trace_xprt_transmit(req, status); 153989f90fe1STrond Myklebust return status; 1540c8485e4dSTrond Myklebust } 15417ebbbc6eSTrond Myklebust 1542dcbbeda8STrond Myklebust if (is_retrans) 1543dcbbeda8STrond Myklebust task->tk_client->cl_stats->rpcretrans++; 1544dcbbeda8STrond Myklebust 15454a068258SChuck Lever xprt_inject_disconnect(xprt); 1546c8485e4dSTrond Myklebust 1547468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 1548b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 1549262ca07dSChuck Lever 1550262ca07dSChuck Lever xprt->stat.sends++; 1551262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 1552262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 155315a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 155415a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 1555b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 155690d91b0cSTrond Myklebust 155790d91b0cSTrond Myklebust req->rq_connect_cookie = connect_cookie; 1558944b0429STrond Myklebust out_dequeue: 15590c77668dSChuck Lever trace_xprt_transmit(req, status); 1560944b0429STrond Myklebust xprt_request_dequeue_transmit(task); 156189f90fe1STrond Myklebust rpc_wake_up_queued_task_set_status(&xprt->sending, task, status); 156289f90fe1STrond Myklebust return status; 156389f90fe1STrond Myklebust } 156489f90fe1STrond Myklebust 156589f90fe1STrond Myklebust /** 156689f90fe1STrond Myklebust * xprt_transmit - send an RPC request on a transport 156789f90fe1STrond Myklebust * @task: controlling RPC task 156889f90fe1STrond Myklebust * 156989f90fe1STrond Myklebust * Attempts to drain the transmit queue. On exit, either the transport 157089f90fe1STrond Myklebust * signalled an error that needs to be handled before transmission can 157189f90fe1STrond Myklebust * resume, or @task finished transmitting, and detected that it already 157289f90fe1STrond Myklebust * received a reply. 157389f90fe1STrond Myklebust */ 157489f90fe1STrond Myklebust void 157589f90fe1STrond Myklebust xprt_transmit(struct rpc_task *task) 157689f90fe1STrond Myklebust { 157789f90fe1STrond Myklebust struct rpc_rqst *next, *req = task->tk_rqstp; 157889f90fe1STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 15796f9f1728SChuck Lever int counter, status; 158089f90fe1STrond Myklebust 158189f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 15826f9f1728SChuck Lever counter = 0; 158389f90fe1STrond Myklebust while (!list_empty(&xprt->xmit_queue)) { 15846f9f1728SChuck Lever if (++counter == 20) 15856f9f1728SChuck Lever break; 158689f90fe1STrond Myklebust next = list_first_entry(&xprt->xmit_queue, 158789f90fe1STrond Myklebust struct rpc_rqst, rq_xmit); 158889f90fe1STrond Myklebust xprt_pin_rqst(next); 158989f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 159089f90fe1STrond Myklebust status = xprt_request_transmit(next, task); 159189f90fe1STrond Myklebust if (status == -EBADMSG && next != req) 159289f90fe1STrond Myklebust status = 0; 159389f90fe1STrond Myklebust spin_lock(&xprt->queue_lock); 159489f90fe1STrond Myklebust xprt_unpin_rqst(next); 159589f90fe1STrond Myklebust if (status == 0) { 159689f90fe1STrond Myklebust if (!xprt_request_data_received(task) || 159789f90fe1STrond Myklebust test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 159889f90fe1STrond Myklebust continue; 1599c544577dSTrond Myklebust } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) 160089f90fe1STrond Myklebust task->tk_status = status; 160189f90fe1STrond Myklebust break; 160289f90fe1STrond Myklebust } 160389f90fe1STrond Myklebust spin_unlock(&xprt->queue_lock); 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 1606ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1607ba60eb25STrond Myklebust { 1608ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1609ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1610ba60eb25STrond Myklebust } 1611ba60eb25STrond Myklebust 1612ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1613ba60eb25STrond Myklebust { 1614ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1615ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1616ba60eb25STrond Myklebust } 1617ba60eb25STrond Myklebust 1618ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1619ba60eb25STrond Myklebust { 1620ba60eb25STrond Myklebust bool ret = false; 1621ba60eb25STrond Myklebust 1622ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1623ba60eb25STrond Myklebust goto out; 1624ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1625ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1626ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1627ba60eb25STrond Myklebust ret = true; 1628ba60eb25STrond Myklebust } 1629ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1630ba60eb25STrond Myklebust out: 1631ba60eb25STrond Myklebust return ret; 1632ba60eb25STrond Myklebust } 1633ba60eb25STrond Myklebust 163492ea011fSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt) 1635d9ba131dSTrond Myklebust { 1636d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1637d9ba131dSTrond Myklebust 1638ff699ea8SChuck Lever if (xprt->num_reqs >= xprt->max_reqs) 1639d9ba131dSTrond Myklebust goto out; 1640ff699ea8SChuck Lever ++xprt->num_reqs; 164192ea011fSTrond Myklebust spin_unlock(&xprt->reserve_lock); 164292ea011fSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS); 164392ea011fSTrond Myklebust spin_lock(&xprt->reserve_lock); 1644d9ba131dSTrond Myklebust if (req != NULL) 1645d9ba131dSTrond Myklebust goto out; 1646ff699ea8SChuck Lever --xprt->num_reqs; 1647d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1648d9ba131dSTrond Myklebust out: 1649d9ba131dSTrond Myklebust return req; 1650d9ba131dSTrond Myklebust } 1651d9ba131dSTrond Myklebust 1652d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1653d9ba131dSTrond Myklebust { 1654ff699ea8SChuck Lever if (xprt->num_reqs > xprt->min_reqs) { 1655ff699ea8SChuck Lever --xprt->num_reqs; 1656d9ba131dSTrond Myklebust kfree(req); 1657d9ba131dSTrond Myklebust return true; 1658d9ba131dSTrond Myklebust } 1659d9ba131dSTrond Myklebust return false; 1660d9ba131dSTrond Myklebust } 1661d9ba131dSTrond Myklebust 1662f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 16631da177e4SLinus Torvalds { 1664d9ba131dSTrond Myklebust struct rpc_rqst *req; 16651da177e4SLinus Torvalds 1666f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 16671da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1668d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1669d9ba131dSTrond Myklebust list_del(&req->rq_list); 1670d9ba131dSTrond Myklebust goto out_init_req; 1671d9ba131dSTrond Myklebust } 167292ea011fSTrond Myklebust req = xprt_dynamic_alloc_slot(xprt); 1673d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1674d9ba131dSTrond Myklebust goto out_init_req; 1675d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1676d9ba131dSTrond Myklebust case -ENOMEM: 1677d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1678d9ba131dSTrond Myklebust "failed! Retrying\n"); 16791afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1680d9ba131dSTrond Myklebust break; 1681d9ba131dSTrond Myklebust case -EAGAIN: 1682ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1683d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 1684df561f66SGustavo A. R. Silva fallthrough; 16851afeaf5cSTrond Myklebust default: 1686d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 16871afeaf5cSTrond Myklebust } 1688f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1689d9ba131dSTrond Myklebust return; 1690d9ba131dSTrond Myklebust out_init_req: 1691ff699ea8SChuck Lever xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots, 1692ff699ea8SChuck Lever xprt->num_reqs); 169337ac86c3SChuck Lever spin_unlock(&xprt->reserve_lock); 169437ac86c3SChuck Lever 1695d9ba131dSTrond Myklebust task->tk_status = 0; 16961da177e4SLinus Torvalds task->tk_rqstp = req; 16971da177e4SLinus Torvalds } 1698f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1699f39c1bfbSTrond Myklebust 1700a9cde23aSChuck Lever void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1701ee5ebe85STrond Myklebust { 1702ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1703c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1704c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1705ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1706c25573b5STrond Myklebust } 1707ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1708ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1709ee5ebe85STrond Myklebust } 1710a9cde23aSChuck Lever EXPORT_SYMBOL_GPL(xprt_free_slot); 1711ee5ebe85STrond Myklebust 171221de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 171321de0a95STrond Myklebust { 171421de0a95STrond Myklebust struct rpc_rqst *req; 171521de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 171621de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 171721de0a95STrond Myklebust list_del(&req->rq_list); 171821de0a95STrond Myklebust kfree(req); 171921de0a95STrond Myklebust } 172021de0a95STrond Myklebust } 172121de0a95STrond Myklebust 1722d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1723d9ba131dSTrond Myklebust unsigned int num_prealloc, 1724d9ba131dSTrond Myklebust unsigned int max_alloc) 1725bd1722d4SPavel Emelyanov { 1726bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 172721de0a95STrond Myklebust struct rpc_rqst *req; 172821de0a95STrond Myklebust int i; 1729bd1722d4SPavel Emelyanov 1730bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1731bd1722d4SPavel Emelyanov if (xprt == NULL) 1732bd1722d4SPavel Emelyanov goto out; 1733bd1722d4SPavel Emelyanov 173421de0a95STrond Myklebust xprt_init(xprt, net); 173521de0a95STrond Myklebust 173621de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 173721de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 173821de0a95STrond Myklebust if (!req) 17398313164cSwangweidong goto out_free; 174021de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 174121de0a95STrond Myklebust } 1742d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1743d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1744d9ba131dSTrond Myklebust else 174521de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1746d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1747ff699ea8SChuck Lever xprt->num_reqs = num_prealloc; 1748bd1722d4SPavel Emelyanov 1749bd1722d4SPavel Emelyanov return xprt; 1750bd1722d4SPavel Emelyanov 1751bd1722d4SPavel Emelyanov out_free: 175221de0a95STrond Myklebust xprt_free(xprt); 1753bd1722d4SPavel Emelyanov out: 1754bd1722d4SPavel Emelyanov return NULL; 1755bd1722d4SPavel Emelyanov } 1756bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1757bd1722d4SPavel Emelyanov 1758e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1759e204e621SPavel Emelyanov { 176037aa2133SPavel Emelyanov put_net(xprt->xprt_net); 176121de0a95STrond Myklebust xprt_free_all_slots(xprt); 1762fda1bfefSTrond Myklebust kfree_rcu(xprt, rcu); 1763e204e621SPavel Emelyanov } 1764e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1765e204e621SPavel Emelyanov 1766902c5887STrond Myklebust static void 1767902c5887STrond Myklebust xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt) 1768902c5887STrond Myklebust { 1769902c5887STrond Myklebust req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1; 1770902c5887STrond Myklebust } 1771902c5887STrond Myklebust 17729dc6edcfSTrond Myklebust static __be32 17739dc6edcfSTrond Myklebust xprt_alloc_xid(struct rpc_xprt *xprt) 17749dc6edcfSTrond Myklebust { 17759dc6edcfSTrond Myklebust __be32 xid; 17769dc6edcfSTrond Myklebust 17779dc6edcfSTrond Myklebust spin_lock(&xprt->reserve_lock); 17789dc6edcfSTrond Myklebust xid = (__force __be32)xprt->xid++; 17799dc6edcfSTrond Myklebust spin_unlock(&xprt->reserve_lock); 17809dc6edcfSTrond Myklebust return xid; 17819dc6edcfSTrond Myklebust } 17829dc6edcfSTrond Myklebust 17839dc6edcfSTrond Myklebust static void 17849dc6edcfSTrond Myklebust xprt_init_xid(struct rpc_xprt *xprt) 17859dc6edcfSTrond Myklebust { 17869dc6edcfSTrond Myklebust xprt->xid = prandom_u32(); 17879dc6edcfSTrond Myklebust } 17889dc6edcfSTrond Myklebust 17899dc6edcfSTrond Myklebust static void 17909dc6edcfSTrond Myklebust xprt_request_init(struct rpc_task *task) 17919dc6edcfSTrond Myklebust { 17929dc6edcfSTrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 17939dc6edcfSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 17949dc6edcfSTrond Myklebust 17959dc6edcfSTrond Myklebust req->rq_task = task; 17969dc6edcfSTrond Myklebust req->rq_xprt = xprt; 17979dc6edcfSTrond Myklebust req->rq_buffer = NULL; 17989dc6edcfSTrond Myklebust req->rq_xid = xprt_alloc_xid(xprt); 1799902c5887STrond Myklebust xprt_init_connect_cookie(req, xprt); 18009dc6edcfSTrond Myklebust req->rq_snd_buf.len = 0; 18019dc6edcfSTrond Myklebust req->rq_snd_buf.buflen = 0; 18029dc6edcfSTrond Myklebust req->rq_rcv_buf.len = 0; 18039dc6edcfSTrond Myklebust req->rq_rcv_buf.buflen = 0; 180471700bb9STrond Myklebust req->rq_snd_buf.bvec = NULL; 180571700bb9STrond Myklebust req->rq_rcv_buf.bvec = NULL; 18069dc6edcfSTrond Myklebust req->rq_release_snd_buf = NULL; 1807da953063STrond Myklebust xprt_init_majortimeo(task, req); 180809d2ba0cSChuck Lever 180909d2ba0cSChuck Lever trace_xprt_reserve(req); 18109dc6edcfSTrond Myklebust } 18119dc6edcfSTrond Myklebust 18129dc6edcfSTrond Myklebust static void 18139dc6edcfSTrond Myklebust xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task) 18149dc6edcfSTrond Myklebust { 18159dc6edcfSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 18169dc6edcfSTrond Myklebust if (task->tk_rqstp != NULL) 18179dc6edcfSTrond Myklebust xprt_request_init(task); 18189dc6edcfSTrond Myklebust } 18199dc6edcfSTrond Myklebust 18209903cd1cSChuck Lever /** 18219903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 18229903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 18239903cd1cSChuck Lever * 1824ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1825ba60eb25STrond Myklebust * slots are available, place the task on the transport's 18269903cd1cSChuck Lever * backlog queue. 18279903cd1cSChuck Lever */ 18289903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 18291da177e4SLinus Torvalds { 1830fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 18311da177e4SLinus Torvalds 183243cedbf0STrond Myklebust task->tk_status = 0; 183343cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 183443cedbf0STrond Myklebust return; 183543cedbf0STrond Myklebust 183643cedbf0STrond Myklebust task->tk_status = -EAGAIN; 1837ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 18389dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 1839ba60eb25STrond Myklebust } 1840ba60eb25STrond Myklebust 1841ba60eb25STrond Myklebust /** 1842ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1843ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1844ba60eb25STrond Myklebust * 1845ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1846ba60eb25STrond Myklebust * backlog queue. 1847ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1848ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1849ba60eb25STrond Myklebust */ 1850ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1851ba60eb25STrond Myklebust { 1852fb43d172STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1853ba60eb25STrond Myklebust 1854ba60eb25STrond Myklebust task->tk_status = 0; 1855ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1856ba60eb25STrond Myklebust return; 1857ba60eb25STrond Myklebust 1858ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 18599dc6edcfSTrond Myklebust xprt_do_reserve(xprt, task); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18629903cd1cSChuck Lever /** 18639903cd1cSChuck Lever * xprt_release - release an RPC request slot 18649903cd1cSChuck Lever * @task: task which is finished with the slot 18659903cd1cSChuck Lever * 18661da177e4SLinus Torvalds */ 18679903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 18681da177e4SLinus Torvalds { 186955ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 187087ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 18711da177e4SLinus Torvalds 187287ed5003STrond Myklebust if (req == NULL) { 187387ed5003STrond Myklebust if (task->tk_client) { 1874fb43d172STrond Myklebust xprt = task->tk_xprt; 187587ed5003STrond Myklebust xprt_release_write(xprt, task); 187687ed5003STrond Myklebust } 18771da177e4SLinus Torvalds return; 187887ed5003STrond Myklebust } 187955ae1aabSRicardo Labiaga 188055ae1aabSRicardo Labiaga xprt = req->rq_xprt; 1881cc204d01STrond Myklebust xprt_request_dequeue_xprt(task); 1882b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 188349e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1884a58dd398SChuck Lever if (xprt->ops->release_request) 1885a58dd398SChuck Lever xprt->ops->release_request(task); 1886ad3331acSTrond Myklebust xprt_schedule_autodisconnect(xprt); 1887b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 1888ee5ebe85STrond Myklebust if (req->rq_buffer) 18893435c74aSChuck Lever xprt->ops->buf_free(task); 18904a068258SChuck Lever xprt_inject_disconnect(xprt); 18919d96acbcSTrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 18920472e476STrond Myklebust xdr_free_bvec(&req->rq_snd_buf); 1893a17c2153STrond Myklebust if (req->rq_cred != NULL) 1894a17c2153STrond Myklebust put_rpccred(req->rq_cred); 18951da177e4SLinus Torvalds task->tk_rqstp = NULL; 1896ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1897ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 189855ae1aabSRicardo Labiaga 1899ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1900a9cde23aSChuck Lever xprt->ops->free_slot(xprt, req); 1901ee5ebe85STrond Myklebust else 1902c9acb42eSTrond Myklebust xprt_free_bc_request(req); 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 1905902c5887STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 1906902c5887STrond Myklebust void 1907902c5887STrond Myklebust xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task) 1908902c5887STrond Myklebust { 1909902c5887STrond Myklebust struct xdr_buf *xbufp = &req->rq_snd_buf; 1910902c5887STrond Myklebust 1911902c5887STrond Myklebust task->tk_rqstp = req; 1912902c5887STrond Myklebust req->rq_task = task; 1913902c5887STrond Myklebust xprt_init_connect_cookie(req, req->rq_xprt); 1914902c5887STrond Myklebust /* 1915902c5887STrond Myklebust * Set up the xdr_buf length. 1916902c5887STrond Myklebust * This also indicates that the buffer is XDR encoded already. 1917902c5887STrond Myklebust */ 1918902c5887STrond Myklebust xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + 1919902c5887STrond Myklebust xbufp->tail[0].iov_len; 1920902c5887STrond Myklebust } 1921902c5887STrond Myklebust #endif 1922902c5887STrond Myklebust 192321de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1924c2866763SChuck Lever { 192530c5116bSTrond Myklebust kref_init(&xprt->kref); 1926c2866763SChuck Lever 1927c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1928c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 192975c84151STrond Myklebust spin_lock_init(&xprt->queue_lock); 1930c2866763SChuck Lever 1931c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 193295f7691dSTrond Myklebust xprt->recv_queue = RB_ROOT; 1933944b0429STrond Myklebust INIT_LIST_HEAD(&xprt->xmit_queue); 19349e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1935f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1936f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 19379e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 193880b14d5eSTrond Myklebust INIT_LIST_HEAD(&xprt->xprt_switch); 1939f9acac1aSRicardo Labiaga 1940c2866763SChuck Lever xprt->last_used = jiffies; 1941c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1942a509050bSChuck Lever xprt->bind_index = 0; 1943c2866763SChuck Lever 1944c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1945c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 194679c99152STrond Myklebust rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 1947c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1948c2866763SChuck Lever 1949c2866763SChuck Lever xprt_init_xid(xprt); 1950c2866763SChuck Lever 195121de0a95STrond Myklebust xprt->xprt_net = get_net(net); 19528d9266ffSTrond Myklebust } 19538d9266ffSTrond Myklebust 19548d9266ffSTrond Myklebust /** 19558d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 19568d9266ffSTrond Myklebust * @args: rpc transport creation arguments 19578d9266ffSTrond Myklebust * 19588d9266ffSTrond Myklebust */ 19598d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 19608d9266ffSTrond Myklebust { 19618d9266ffSTrond Myklebust struct rpc_xprt *xprt; 19629bccd264STrond Myklebust const struct xprt_class *t; 19638d9266ffSTrond Myklebust 19649bccd264STrond Myklebust t = xprt_class_find_by_ident(args->ident); 19659bccd264STrond Myklebust if (!t) { 19663c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 19678d9266ffSTrond Myklebust return ERR_PTR(-EIO); 19689bccd264STrond Myklebust } 19698d9266ffSTrond Myklebust 19708d9266ffSTrond Myklebust xprt = t->setup(args); 19719bccd264STrond Myklebust xprt_class_release(t); 19729bccd264STrond Myklebust 1973911813d7SChuck Lever if (IS_ERR(xprt)) 197421de0a95STrond Myklebust goto out; 197533d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 197633d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 197721de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 197821de0a95STrond Myklebust if (xprt_has_timer(xprt)) 1979502980e8SAnna Schumaker timer_setup(&xprt->timer, xprt_init_autodisconnect, 0); 198021de0a95STrond Myklebust else 1981ff861c4dSKees Cook timer_setup(&xprt->timer, NULL, 0); 19824e0038b6STrond Myklebust 19834e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 19844e0038b6STrond Myklebust xprt_destroy(xprt); 19854e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 19864e0038b6STrond Myklebust } 19874e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 19884e0038b6STrond Myklebust if (xprt->servername == NULL) { 19894e0038b6STrond Myklebust xprt_destroy(xprt); 19904e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 19914e0038b6STrond Myklebust } 19924e0038b6STrond Myklebust 19933f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1994388f0c77SJeff Layton 1995911813d7SChuck Lever trace_xprt_create(xprt); 199621de0a95STrond Myklebust out: 1997c2866763SChuck Lever return xprt; 1998c2866763SChuck Lever } 1999c2866763SChuck Lever 2000528fd354STrond Myklebust static void xprt_destroy_cb(struct work_struct *work) 2001528fd354STrond Myklebust { 2002528fd354STrond Myklebust struct rpc_xprt *xprt = 2003528fd354STrond Myklebust container_of(work, struct rpc_xprt, task_cleanup); 2004528fd354STrond Myklebust 2005911813d7SChuck Lever trace_xprt_destroy(xprt); 2006911813d7SChuck Lever 2007528fd354STrond Myklebust rpc_xprt_debugfs_unregister(xprt); 2008528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 2009528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 2010528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 2011528fd354STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 2012528fd354STrond Myklebust kfree(xprt->servername); 2013528fd354STrond Myklebust /* 2014669996adSTrond Myklebust * Destroy any existing back channel 2015669996adSTrond Myklebust */ 2016669996adSTrond Myklebust xprt_destroy_backchannel(xprt, UINT_MAX); 2017669996adSTrond Myklebust 2018669996adSTrond Myklebust /* 2019528fd354STrond Myklebust * Tear down transport state and free the rpc_xprt 2020528fd354STrond Myklebust */ 2021528fd354STrond Myklebust xprt->ops->destroy(xprt); 2022528fd354STrond Myklebust } 2023528fd354STrond Myklebust 20249903cd1cSChuck Lever /** 20259903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 2026a8de240aSTrond Myklebust * @xprt: transport to destroy 20279903cd1cSChuck Lever * 20281da177e4SLinus Torvalds */ 2029a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 20301da177e4SLinus Torvalds { 2031528fd354STrond Myklebust /* 2032528fd354STrond Myklebust * Exclude transport connect/disconnect handlers and autoclose 2033528fd354STrond Myklebust */ 203479234c3dSTrond Myklebust wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE); 203579234c3dSTrond Myklebust 20360065db32STrond Myklebust del_timer_sync(&xprt->timer); 2037c8541ecdSChuck Lever 2038c8541ecdSChuck Lever /* 2039528fd354STrond Myklebust * Destroy sockets etc from the system workqueue so they can 2040528fd354STrond Myklebust * safely flush receive work running on rpciod. 2041c8541ecdSChuck Lever */ 2042528fd354STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb); 2043528fd354STrond Myklebust schedule_work(&xprt->task_cleanup); 20446b6ca86bSTrond Myklebust } 20451da177e4SLinus Torvalds 204630c5116bSTrond Myklebust static void xprt_destroy_kref(struct kref *kref) 204730c5116bSTrond Myklebust { 204830c5116bSTrond Myklebust xprt_destroy(container_of(kref, struct rpc_xprt, kref)); 204930c5116bSTrond Myklebust } 205030c5116bSTrond Myklebust 205130c5116bSTrond Myklebust /** 205230c5116bSTrond Myklebust * xprt_get - return a reference to an RPC transport. 205330c5116bSTrond Myklebust * @xprt: pointer to the transport 205430c5116bSTrond Myklebust * 205530c5116bSTrond Myklebust */ 205630c5116bSTrond Myklebust struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 205730c5116bSTrond Myklebust { 205830c5116bSTrond Myklebust if (xprt != NULL && kref_get_unless_zero(&xprt->kref)) 205930c5116bSTrond Myklebust return xprt; 206030c5116bSTrond Myklebust return NULL; 206130c5116bSTrond Myklebust } 206230c5116bSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_get); 206330c5116bSTrond Myklebust 20646b6ca86bSTrond Myklebust /** 20656b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 20666b6ca86bSTrond Myklebust * @xprt: pointer to the transport 20676b6ca86bSTrond Myklebust * 20686b6ca86bSTrond Myklebust */ 20696b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 20706b6ca86bSTrond Myklebust { 207130c5116bSTrond Myklebust if (xprt != NULL) 207230c5116bSTrond Myklebust kref_put(&xprt->kref, xprt_destroy_kref); 20736b6ca86bSTrond Myklebust } 20745d252f90SChuck Lever EXPORT_SYMBOL_GPL(xprt_put); 2075