Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/xprt.c |
| 3 | * |
| 4 | * This is a generic RPC call interface supporting congestion avoidance, |
| 5 | * and asynchronous calls. |
| 6 | * |
| 7 | * The interface works like this: |
| 8 | * |
| 9 | * - When a process places a call, it allocates a request slot if |
| 10 | * one is available. Otherwise, it sleeps on the backlog queue |
| 11 | * (xprt_reserve). |
| 12 | * - Next, the caller puts together the RPC message, stuffs it into |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 13 | * the request struct, and calls xprt_transmit(). |
| 14 | * - xprt_transmit sends the message and installs the caller on the |
| 15 | * transport's wait list. At the same time, it installs a timer that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * is run after the packet's timeout has expired. |
| 17 | * - When a packet arrives, the data_ready handler walks the list of |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 18 | * pending requests for that transport. If a matching XID is found, the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * caller is woken up, and the timer removed. |
| 20 | * - When no reply arrives within the timeout interval, the timer is |
| 21 | * fired by the kernel and runs xprt_timer(). It either adjusts the |
| 22 | * timeout values (minor timeout) or wakes up the caller with a status |
| 23 | * of -ETIMEDOUT. |
| 24 | * - When the caller receives a notification from RPC that a reply arrived, |
| 25 | * it should release the RPC slot, and process the reply. |
| 26 | * If the call timed out, it may choose to retry the operation by |
| 27 | * adjusting the initial timeout value, and simply calling rpc_call |
| 28 | * again. |
| 29 | * |
| 30 | * Support for async RPC is done through a set of RPC-specific scheduling |
| 31 | * primitives that `transparently' work for processes as well as async |
| 32 | * tasks that rely on callbacks. |
| 33 | * |
| 34 | * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 35 | * |
| 36 | * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/types.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 42 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/workqueue.h> |
Chuck Lever | bf3fcf8 | 2006-05-25 01:40:51 -0400 | [diff] [blame] | 44 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 46 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 47 | #include <linux/sunrpc/metrics.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Local variables |
| 51 | */ |
| 52 | |
| 53 | #ifdef RPC_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | # define RPCDBG_FACILITY RPCDBG_XPRT |
| 55 | #endif |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Local functions |
| 59 | */ |
| 60 | static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); |
| 61 | static inline void do_xprt_reserve(struct rpc_task *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static void xprt_connect_status(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); |
| 64 | |
Jiri Slaby | 5ba03e8 | 2007-11-22 19:40:22 +0800 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(xprt_list_lock); |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 66 | static LIST_HEAD(xprt_list); |
| 67 | |
Chuck Lever | 555ee3a | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 68 | /* |
| 69 | * The transport code maintains an estimate on the maximum number of out- |
| 70 | * standing RPC requests, using a smoothed version of the congestion |
| 71 | * avoidance implemented in 44BSD. This is basically the Van Jacobson |
| 72 | * congestion algorithm: If a retransmit occurs, the congestion window is |
| 73 | * halved; otherwise, it is incremented by 1/cwnd when |
| 74 | * |
| 75 | * - a reply is received and |
| 76 | * - a full number of requests are outstanding and |
| 77 | * - the congestion window hasn't been updated recently. |
| 78 | */ |
| 79 | #define RPC_CWNDSHIFT (8U) |
| 80 | #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT) |
| 81 | #define RPC_INITCWND RPC_CWNDSCALE |
| 82 | #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT) |
| 83 | |
| 84 | #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 86 | /** |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 87 | * xprt_register_transport - register a transport implementation |
| 88 | * @transport: transport to register |
| 89 | * |
| 90 | * If a transport implementation is loaded as a kernel module, it can |
| 91 | * call this interface to make itself known to the RPC client. |
| 92 | * |
| 93 | * Returns: |
| 94 | * 0: transport successfully registered |
| 95 | * -EEXIST: transport already registered |
| 96 | * -EINVAL: transport module being unloaded |
| 97 | */ |
| 98 | int xprt_register_transport(struct xprt_class *transport) |
| 99 | { |
| 100 | struct xprt_class *t; |
| 101 | int result; |
| 102 | |
| 103 | result = -EEXIST; |
| 104 | spin_lock(&xprt_list_lock); |
| 105 | list_for_each_entry(t, &xprt_list, list) { |
| 106 | /* don't register the same transport class twice */ |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 107 | if (t->ident == transport->ident) |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 108 | goto out; |
| 109 | } |
| 110 | |
Denis V. Lunev | c9f6cde | 2008-07-31 09:53:56 +0400 | [diff] [blame] | 111 | list_add_tail(&transport->list, &xprt_list); |
| 112 | printk(KERN_INFO "RPC: Registered %s transport module.\n", |
| 113 | transport->name); |
| 114 | result = 0; |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 115 | |
| 116 | out: |
| 117 | spin_unlock(&xprt_list_lock); |
| 118 | return result; |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(xprt_register_transport); |
| 121 | |
| 122 | /** |
| 123 | * xprt_unregister_transport - unregister a transport implementation |
Randy Dunlap | 65b6e42 | 2008-02-13 15:03:23 -0800 | [diff] [blame] | 124 | * @transport: transport to unregister |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 125 | * |
| 126 | * Returns: |
| 127 | * 0: transport successfully unregistered |
| 128 | * -ENOENT: transport never registered |
| 129 | */ |
| 130 | int xprt_unregister_transport(struct xprt_class *transport) |
| 131 | { |
| 132 | struct xprt_class *t; |
| 133 | int result; |
| 134 | |
| 135 | result = 0; |
| 136 | spin_lock(&xprt_list_lock); |
| 137 | list_for_each_entry(t, &xprt_list, list) { |
| 138 | if (t == transport) { |
| 139 | printk(KERN_INFO |
| 140 | "RPC: Unregistered %s transport module.\n", |
| 141 | transport->name); |
| 142 | list_del_init(&transport->list); |
\"Talpey, Thomas\ | 81c098a | 2007-09-10 13:46:00 -0400 | [diff] [blame] | 143 | goto out; |
| 144 | } |
| 145 | } |
| 146 | result = -ENOENT; |
| 147 | |
| 148 | out: |
| 149 | spin_unlock(&xprt_list_lock); |
| 150 | return result; |
| 151 | } |
| 152 | EXPORT_SYMBOL_GPL(xprt_unregister_transport); |
| 153 | |
| 154 | /** |
Tom Talpey | 441e3e2 | 2009-03-11 14:37:56 -0400 | [diff] [blame] | 155 | * xprt_load_transport - load a transport implementation |
| 156 | * @transport_name: transport to load |
| 157 | * |
| 158 | * Returns: |
| 159 | * 0: transport successfully loaded |
| 160 | * -ENOENT: transport module not available |
| 161 | */ |
| 162 | int xprt_load_transport(const char *transport_name) |
| 163 | { |
| 164 | struct xprt_class *t; |
| 165 | char module_name[sizeof t->name + 5]; |
| 166 | int result; |
| 167 | |
| 168 | result = 0; |
| 169 | spin_lock(&xprt_list_lock); |
| 170 | list_for_each_entry(t, &xprt_list, list) { |
| 171 | if (strcmp(t->name, transport_name) == 0) { |
| 172 | spin_unlock(&xprt_list_lock); |
| 173 | goto out; |
| 174 | } |
| 175 | } |
| 176 | spin_unlock(&xprt_list_lock); |
| 177 | strcpy(module_name, "xprt"); |
| 178 | strncat(module_name, transport_name, sizeof t->name); |
| 179 | result = request_module(module_name); |
| 180 | out: |
| 181 | return result; |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(xprt_load_transport); |
| 184 | |
| 185 | /** |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 186 | * xprt_reserve_xprt - serialize write access to transports |
| 187 | * @task: task that is requesting access to the transport |
| 188 | * |
| 189 | * This prevents mixing the payload of separate requests, and prevents |
| 190 | * transport connects from colliding with writes. No congestion control |
| 191 | * is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | */ |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 193 | int xprt_reserve_xprt(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 195 | struct rpc_xprt *xprt = task->tk_xprt; |
| 196 | struct rpc_rqst *req = task->tk_rqstp; |
| 197 | |
| 198 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
| 199 | if (task == xprt->snd_task) |
| 200 | return 1; |
| 201 | if (task == NULL) |
| 202 | return 0; |
| 203 | goto out_sleep; |
| 204 | } |
| 205 | xprt->snd_task = task; |
| 206 | if (req) { |
| 207 | req->rq_bytes_sent = 0; |
| 208 | req->rq_ntrans++; |
| 209 | } |
| 210 | return 1; |
| 211 | |
| 212 | out_sleep: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 213 | dprintk("RPC: %5u failed to lock transport %p\n", |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 214 | task->tk_pid, xprt); |
| 215 | task->tk_timeout = 0; |
| 216 | task->tk_status = -EAGAIN; |
| 217 | if (req && req->rq_ntrans) |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 218 | rpc_sleep_on(&xprt->resend, task, NULL); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 219 | else |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 220 | rpc_sleep_on(&xprt->sending, task, NULL); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 224 | |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 225 | static void xprt_clear_locked(struct rpc_xprt *xprt) |
| 226 | { |
| 227 | xprt->snd_task = NULL; |
| 228 | if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) { |
| 229 | smp_mb__before_clear_bit(); |
| 230 | clear_bit(XPRT_LOCKED, &xprt->state); |
| 231 | smp_mb__after_clear_bit(); |
| 232 | } else |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 233 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 236 | /* |
| 237 | * xprt_reserve_xprt_cong - serialize write access to transports |
| 238 | * @task: task that is requesting access to the transport |
| 239 | * |
| 240 | * Same as xprt_reserve_xprt, but Van Jacobson congestion control is |
| 241 | * integrated into the decision of whether a request is allowed to be |
| 242 | * woken up and given access to the transport. |
| 243 | */ |
| 244 | int xprt_reserve_xprt_cong(struct rpc_task *task) |
| 245 | { |
| 246 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | struct rpc_rqst *req = task->tk_rqstp; |
| 248 | |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 249 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if (task == xprt->snd_task) |
| 251 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | goto out_sleep; |
| 253 | } |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 254 | if (__xprt_get_cong(xprt, task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | xprt->snd_task = task; |
| 256 | if (req) { |
| 257 | req->rq_bytes_sent = 0; |
| 258 | req->rq_ntrans++; |
| 259 | } |
| 260 | return 1; |
| 261 | } |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 262 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | out_sleep: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 264 | dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | task->tk_timeout = 0; |
| 266 | task->tk_status = -EAGAIN; |
| 267 | if (req && req->rq_ntrans) |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 268 | rpc_sleep_on(&xprt->resend, task, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | else |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 270 | rpc_sleep_on(&xprt->sending, task, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 273 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 275 | static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | int retval; |
| 278 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 279 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 280 | retval = xprt->ops->reserve_xprt(task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 281 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return retval; |
| 283 | } |
| 284 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 285 | static void __xprt_lock_write_next(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | struct rpc_task *task; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 288 | struct rpc_rqst *req; |
| 289 | |
| 290 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
| 291 | return; |
| 292 | |
| 293 | task = rpc_wake_up_next(&xprt->resend); |
| 294 | if (!task) { |
| 295 | task = rpc_wake_up_next(&xprt->sending); |
| 296 | if (!task) |
| 297 | goto out_unlock; |
| 298 | } |
| 299 | |
| 300 | req = task->tk_rqstp; |
| 301 | xprt->snd_task = task; |
| 302 | if (req) { |
| 303 | req->rq_bytes_sent = 0; |
| 304 | req->rq_ntrans++; |
| 305 | } |
| 306 | return; |
| 307 | |
| 308 | out_unlock: |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 309 | xprt_clear_locked(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) |
| 313 | { |
| 314 | struct rpc_task *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 316 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 318 | if (RPCXPRT_CONGESTED(xprt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | goto out_unlock; |
| 320 | task = rpc_wake_up_next(&xprt->resend); |
| 321 | if (!task) { |
| 322 | task = rpc_wake_up_next(&xprt->sending); |
| 323 | if (!task) |
| 324 | goto out_unlock; |
| 325 | } |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 326 | if (__xprt_get_cong(xprt, task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | struct rpc_rqst *req = task->tk_rqstp; |
| 328 | xprt->snd_task = task; |
| 329 | if (req) { |
| 330 | req->rq_bytes_sent = 0; |
| 331 | req->rq_ntrans++; |
| 332 | } |
| 333 | return; |
| 334 | } |
| 335 | out_unlock: |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 336 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 339 | /** |
| 340 | * xprt_release_xprt - allow other requests to use a transport |
| 341 | * @xprt: transport with other tasks potentially waiting |
| 342 | * @task: task that is releasing access to the transport |
| 343 | * |
| 344 | * Note that "task" can be NULL. No congestion control is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | */ |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 346 | void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
| 348 | if (xprt->snd_task == task) { |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 349 | xprt_clear_locked(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | __xprt_lock_write_next(xprt); |
| 351 | } |
| 352 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 353 | EXPORT_SYMBOL_GPL(xprt_release_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 355 | /** |
| 356 | * xprt_release_xprt_cong - allow other requests to use a transport |
| 357 | * @xprt: transport with other tasks potentially waiting |
| 358 | * @task: task that is releasing access to the transport |
| 359 | * |
| 360 | * Note that "task" can be NULL. Another task is awoken to use the |
| 361 | * transport if the transport's congestion window allows it. |
| 362 | */ |
| 363 | void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
| 364 | { |
| 365 | if (xprt->snd_task == task) { |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 366 | xprt_clear_locked(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 367 | __xprt_lock_write_next_cong(xprt); |
| 368 | } |
| 369 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 370 | EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 371 | |
| 372 | static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 374 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 375 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 376 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * Van Jacobson congestion avoidance. Check if the congestion window |
| 381 | * overflowed. Put the task to sleep if this is the case. |
| 382 | */ |
| 383 | static int |
| 384 | __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
| 385 | { |
| 386 | struct rpc_rqst *req = task->tk_rqstp; |
| 387 | |
| 388 | if (req->rq_cong) |
| 389 | return 1; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 390 | dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | task->tk_pid, xprt->cong, xprt->cwnd); |
| 392 | if (RPCXPRT_CONGESTED(xprt)) |
| 393 | return 0; |
| 394 | req->rq_cong = 1; |
| 395 | xprt->cong += RPC_CWNDSCALE; |
| 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Adjust the congestion window, and wake up the next task |
| 401 | * that has been sleeping due to congestion |
| 402 | */ |
| 403 | static void |
| 404 | __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) |
| 405 | { |
| 406 | if (!req->rq_cong) |
| 407 | return; |
| 408 | req->rq_cong = 0; |
| 409 | xprt->cong -= RPC_CWNDSCALE; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 410 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 413 | /** |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 414 | * xprt_release_rqst_cong - housekeeping when request is complete |
| 415 | * @task: RPC request that recently completed |
| 416 | * |
| 417 | * Useful for transports that require congestion control. |
| 418 | */ |
| 419 | void xprt_release_rqst_cong(struct rpc_task *task) |
| 420 | { |
| 421 | __xprt_put_cong(task->tk_xprt, task->tk_rqstp); |
| 422 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 423 | EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 424 | |
| 425 | /** |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 426 | * xprt_adjust_cwnd - adjust transport congestion window |
| 427 | * @task: recently completed RPC request used to adjust window |
| 428 | * @result: result code of completed RPC request |
| 429 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | * We use a time-smoothed congestion estimator to avoid heavy oscillation. |
| 431 | */ |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 432 | void xprt_adjust_cwnd(struct rpc_task *task, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 434 | struct rpc_rqst *req = task->tk_rqstp; |
| 435 | struct rpc_xprt *xprt = task->tk_xprt; |
| 436 | unsigned long cwnd = xprt->cwnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (result >= 0 && cwnd <= xprt->cong) { |
| 439 | /* The (cwnd >> 1) term makes sure |
| 440 | * the result gets rounded properly. */ |
| 441 | cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; |
| 442 | if (cwnd > RPC_MAXCWND(xprt)) |
| 443 | cwnd = RPC_MAXCWND(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 444 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } else if (result == -ETIMEDOUT) { |
| 446 | cwnd >>= 1; |
| 447 | if (cwnd < RPC_CWNDSCALE) |
| 448 | cwnd = RPC_CWNDSCALE; |
| 449 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 450 | dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | xprt->cong, xprt->cwnd, cwnd); |
| 452 | xprt->cwnd = cwnd; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 453 | __xprt_put_cong(xprt, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 455 | EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 457 | /** |
| 458 | * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue |
| 459 | * @xprt: transport with waiting tasks |
| 460 | * @status: result code to plant in each task before waking it |
| 461 | * |
| 462 | */ |
| 463 | void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) |
| 464 | { |
| 465 | if (status < 0) |
| 466 | rpc_wake_up_status(&xprt->pending, status); |
| 467 | else |
| 468 | rpc_wake_up(&xprt->pending); |
| 469 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 470 | EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 471 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 472 | /** |
| 473 | * xprt_wait_for_buffer_space - wait for transport output buffer to clear |
| 474 | * @task: task to be put to sleep |
Randy Dunlap | 0b80ae4 | 2008-04-26 22:59:02 -0700 | [diff] [blame] | 475 | * @action: function pointer to be executed after wait |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 476 | */ |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 477 | void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 478 | { |
| 479 | struct rpc_rqst *req = task->tk_rqstp; |
| 480 | struct rpc_xprt *xprt = req->rq_xprt; |
| 481 | |
| 482 | task->tk_timeout = req->rq_timeout; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 483 | rpc_sleep_on(&xprt->pending, task, action); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 484 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 485 | EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 486 | |
| 487 | /** |
| 488 | * xprt_write_space - wake the task waiting for transport output buffer space |
| 489 | * @xprt: transport with waiting tasks |
| 490 | * |
| 491 | * Can be called in a soft IRQ context, so xprt_write_space never sleeps. |
| 492 | */ |
| 493 | void xprt_write_space(struct rpc_xprt *xprt) |
| 494 | { |
| 495 | if (unlikely(xprt->shutdown)) |
| 496 | return; |
| 497 | |
| 498 | spin_lock_bh(&xprt->transport_lock); |
| 499 | if (xprt->snd_task) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 500 | dprintk("RPC: write space: waking waiting task on " |
| 501 | "xprt %p\n", xprt); |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 502 | rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 503 | } |
| 504 | spin_unlock_bh(&xprt->transport_lock); |
| 505 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 506 | EXPORT_SYMBOL_GPL(xprt_write_space); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 507 | |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 508 | /** |
| 509 | * xprt_set_retrans_timeout_def - set a request's retransmit timeout |
| 510 | * @task: task whose timeout is to be set |
| 511 | * |
| 512 | * Set a request's retransmit timeout based on the transport's |
| 513 | * default timeout parameters. Used by transports that don't adjust |
| 514 | * the retransmit timeout based on round-trip time estimation. |
| 515 | */ |
| 516 | void xprt_set_retrans_timeout_def(struct rpc_task *task) |
| 517 | { |
| 518 | task->tk_timeout = task->tk_rqstp->rq_timeout; |
| 519 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout |
| 524 | * @task: task whose timeout is to be set |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 525 | * |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 526 | * Set a request's retransmit timeout using the RTT estimator. |
| 527 | */ |
| 528 | void xprt_set_retrans_timeout_rtt(struct rpc_task *task) |
| 529 | { |
| 530 | int timer = task->tk_msg.rpc_proc->p_timer; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 531 | struct rpc_clnt *clnt = task->tk_client; |
| 532 | struct rpc_rtt *rtt = clnt->cl_rtt; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 533 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 534 | unsigned long max_timeout = clnt->cl_timeout->to_maxval; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 535 | |
| 536 | task->tk_timeout = rpc_calc_rto(rtt, timer); |
| 537 | task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; |
| 538 | if (task->tk_timeout > max_timeout || task->tk_timeout == 0) |
| 539 | task->tk_timeout = max_timeout; |
| 540 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 541 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | static void xprt_reset_majortimeo(struct rpc_rqst *req) |
| 544 | { |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 545 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | req->rq_majortimeo = req->rq_timeout; |
| 548 | if (to->to_exponential) |
| 549 | req->rq_majortimeo <<= to->to_retries; |
| 550 | else |
| 551 | req->rq_majortimeo += to->to_increment * to->to_retries; |
| 552 | if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) |
| 553 | req->rq_majortimeo = to->to_maxval; |
| 554 | req->rq_majortimeo += jiffies; |
| 555 | } |
| 556 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 557 | /** |
| 558 | * xprt_adjust_timeout - adjust timeout values for next retransmit |
| 559 | * @req: RPC request containing parameters to use for the adjustment |
| 560 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | */ |
| 562 | int xprt_adjust_timeout(struct rpc_rqst *req) |
| 563 | { |
| 564 | struct rpc_xprt *xprt = req->rq_xprt; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 565 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | int status = 0; |
| 567 | |
| 568 | if (time_before(jiffies, req->rq_majortimeo)) { |
| 569 | if (to->to_exponential) |
| 570 | req->rq_timeout <<= 1; |
| 571 | else |
| 572 | req->rq_timeout += to->to_increment; |
| 573 | if (to->to_maxval && req->rq_timeout >= to->to_maxval) |
| 574 | req->rq_timeout = to->to_maxval; |
| 575 | req->rq_retries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } else { |
| 577 | req->rq_timeout = to->to_initval; |
| 578 | req->rq_retries = 0; |
| 579 | xprt_reset_majortimeo(req); |
| 580 | /* Reset the RTT counters == "slow start" */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 581 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 583 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | status = -ETIMEDOUT; |
| 585 | } |
| 586 | |
| 587 | if (req->rq_timeout == 0) { |
| 588 | printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); |
| 589 | req->rq_timeout = 5 * HZ; |
| 590 | } |
| 591 | return status; |
| 592 | } |
| 593 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 594 | static void xprt_autoclose(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 596 | struct rpc_xprt *xprt = |
| 597 | container_of(work, struct rpc_xprt, task_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 599 | xprt->ops->close(xprt); |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 600 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | xprt_release_write(xprt, NULL); |
| 602 | } |
| 603 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 604 | /** |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 605 | * xprt_disconnect_done - mark a transport as disconnected |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 606 | * @xprt: transport to flag for disconnect |
| 607 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | */ |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 609 | void xprt_disconnect_done(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 611 | dprintk("RPC: disconnected transport %p\n", xprt); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 612 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | xprt_clear_connected(xprt); |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 614 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 615 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 617 | EXPORT_SYMBOL_GPL(xprt_disconnect_done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 619 | /** |
| 620 | * xprt_force_disconnect - force a transport to disconnect |
| 621 | * @xprt: transport to disconnect |
| 622 | * |
| 623 | */ |
| 624 | void xprt_force_disconnect(struct rpc_xprt *xprt) |
| 625 | { |
| 626 | /* Don't race with the test_bit() in xprt_clear_locked() */ |
| 627 | spin_lock_bh(&xprt->transport_lock); |
| 628 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 629 | /* Try to schedule an autoclose RPC call */ |
| 630 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
| 631 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 632 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 633 | spin_unlock_bh(&xprt->transport_lock); |
| 634 | } |
Trond Myklebust | 66af1e5 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 635 | |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 636 | /** |
| 637 | * xprt_conditional_disconnect - force a transport to disconnect |
| 638 | * @xprt: transport to disconnect |
| 639 | * @cookie: 'connection cookie' |
| 640 | * |
| 641 | * This attempts to break the connection if and only if 'cookie' matches |
| 642 | * the current transport 'connection cookie'. It ensures that we don't |
| 643 | * try to break the connection more than once when we need to retransmit |
| 644 | * a batch of RPC requests. |
| 645 | * |
| 646 | */ |
| 647 | void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) |
| 648 | { |
| 649 | /* Don't race with the test_bit() in xprt_clear_locked() */ |
| 650 | spin_lock_bh(&xprt->transport_lock); |
| 651 | if (cookie != xprt->connect_cookie) |
| 652 | goto out; |
| 653 | if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) |
| 654 | goto out; |
| 655 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 656 | /* Try to schedule an autoclose RPC call */ |
| 657 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
| 658 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
| 659 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
| 660 | out: |
| 661 | spin_unlock_bh(&xprt->transport_lock); |
| 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | static void |
| 665 | xprt_init_autodisconnect(unsigned long data) |
| 666 | { |
| 667 | struct rpc_xprt *xprt = (struct rpc_xprt *)data; |
| 668 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 669 | spin_lock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (!list_empty(&xprt->recv) || xprt->shutdown) |
| 671 | goto out_abort; |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 672 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | goto out_abort; |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 674 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 675 | if (xprt_connecting(xprt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | xprt_release_write(xprt, NULL); |
| 677 | else |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 678 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return; |
| 680 | out_abort: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 681 | spin_unlock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 684 | /** |
| 685 | * xprt_connect - schedule a transport connect operation |
| 686 | * @task: RPC task that is requesting the connect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | * |
| 688 | */ |
| 689 | void xprt_connect(struct rpc_task *task) |
| 690 | { |
| 691 | struct rpc_xprt *xprt = task->tk_xprt; |
| 692 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 693 | dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | xprt, (xprt_connected(xprt) ? "is" : "is not")); |
| 695 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 696 | if (!xprt_bound(xprt)) { |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 697 | task->tk_status = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | if (!xprt_lock_write(xprt, task)) |
| 701 | return; |
| 702 | if (xprt_connected(xprt)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 703 | xprt_release_write(xprt, task); |
| 704 | else { |
| 705 | if (task->tk_rqstp) |
| 706 | task->tk_rqstp->rq_bytes_sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 708 | task->tk_timeout = xprt->connect_timeout; |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 709 | rpc_sleep_on(&xprt->pending, task, xprt_connect_status); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 710 | xprt->stat.connect_start = jiffies; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 711 | xprt->ops->connect(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 716 | static void xprt_connect_status(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
| 718 | struct rpc_xprt *xprt = task->tk_xprt; |
| 719 | |
Chuck Lever | cd983ef | 2008-06-11 17:56:13 -0400 | [diff] [blame] | 720 | if (task->tk_status == 0) { |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 721 | xprt->stat.connect_count++; |
| 722 | xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 723 | dprintk("RPC: %5u xprt_connect_status: connection established\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | task->tk_pid); |
| 725 | return; |
| 726 | } |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | switch (task->tk_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | case -ENOTCONN: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 730 | dprintk("RPC: %5u xprt_connect_status: connection broken\n", |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 731 | task->tk_pid); |
| 732 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | case -ETIMEDOUT: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 734 | dprintk("RPC: %5u xprt_connect_status: connect attempt timed " |
| 735 | "out\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | break; |
| 737 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 738 | dprintk("RPC: %5u xprt_connect_status: error %d connecting to " |
| 739 | "server %s\n", task->tk_pid, -task->tk_status, |
| 740 | task->tk_client->cl_server); |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 741 | xprt_release_write(xprt, task); |
| 742 | task->tk_status = -EIO; |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 746 | /** |
| 747 | * xprt_lookup_rqst - find an RPC request corresponding to an XID |
| 748 | * @xprt: transport on which the original request was transmitted |
| 749 | * @xid: RPC XID of incoming reply |
| 750 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 752 | struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
| 754 | struct list_head *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | list_for_each(pos, &xprt->recv) { |
| 757 | struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 758 | if (entry->rq_xid == xid) |
| 759 | return entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 761 | |
| 762 | dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", |
| 763 | ntohl(xid)); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 764 | xprt->stat.bad_xids++; |
| 765 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 767 | EXPORT_SYMBOL_GPL(xprt_lookup_rqst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 769 | /** |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 770 | * xprt_update_rtt - update an RPC client's RTT state after receiving a reply |
| 771 | * @task: RPC request that recently completed |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 772 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | */ |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 774 | void xprt_update_rtt(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 776 | struct rpc_rqst *req = task->tk_rqstp; |
| 777 | struct rpc_rtt *rtt = task->tk_client->cl_rtt; |
| 778 | unsigned timer = task->tk_msg.rpc_proc->p_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 780 | if (timer) { |
| 781 | if (req->rq_ntrans == 1) |
| 782 | rpc_update_rtt(rtt, timer, |
| 783 | (long)jiffies - req->rq_xtime); |
| 784 | rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 786 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(xprt_update_rtt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 789 | /** |
| 790 | * xprt_complete_rqst - called when reply processing is complete |
| 791 | * @task: RPC request that recently completed |
| 792 | * @copied: actual number of bytes received from the transport |
| 793 | * |
| 794 | * Caller holds transport lock. |
| 795 | */ |
| 796 | void xprt_complete_rqst(struct rpc_task *task, int copied) |
| 797 | { |
| 798 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 799 | struct rpc_xprt *xprt = req->rq_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 801 | dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", |
| 802 | task->tk_pid, ntohl(req->rq_xid), copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 804 | xprt->stat.recvs++; |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 805 | task->tk_rtt = (long)jiffies - req->rq_xtime; |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | list_del_init(&req->rq_list); |
Trond Myklebust | 1e799b6 | 2008-03-21 16:19:41 -0400 | [diff] [blame] | 808 | req->rq_private_buf.len = copied; |
Trond Myklebust | 43ac3f2 | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 809 | /* Ensure all writes are done before we update req->rq_received */ |
| 810 | smp_wmb(); |
Trond Myklebust | 1e799b6 | 2008-03-21 16:19:41 -0400 | [diff] [blame] | 811 | req->rq_received = copied; |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 812 | rpc_wake_up_queued_task(&xprt->pending, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(xprt_complete_rqst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 816 | static void xprt_timer(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 818 | struct rpc_rqst *req = task->tk_rqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | struct rpc_xprt *xprt = req->rq_xprt; |
| 820 | |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 821 | if (task->tk_status != -ETIMEDOUT) |
| 822 | return; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 823 | dprintk("RPC: %5u xprt_timer\n", task->tk_pid); |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 824 | |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 825 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 826 | if (!req->rq_received) { |
| 827 | if (xprt->ops->timer) |
| 828 | xprt->ops->timer(task); |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 829 | } else |
| 830 | task->tk_status = 0; |
| 831 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 834 | /** |
| 835 | * xprt_prepare_transmit - reserve the transport before sending a request |
| 836 | * @task: RPC task about to send a request |
| 837 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 839 | int xprt_prepare_transmit(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
| 841 | struct rpc_rqst *req = task->tk_rqstp; |
| 842 | struct rpc_xprt *xprt = req->rq_xprt; |
| 843 | int err = 0; |
| 844 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 845 | dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 847 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | if (req->rq_received && !req->rq_bytes_sent) { |
| 849 | err = req->rq_received; |
| 850 | goto out_unlock; |
| 851 | } |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 852 | if (!xprt->ops->reserve_xprt(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | err = -EAGAIN; |
| 854 | goto out_unlock; |
| 855 | } |
| 856 | |
| 857 | if (!xprt_connected(xprt)) { |
| 858 | err = -ENOTCONN; |
| 859 | goto out_unlock; |
| 860 | } |
| 861 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 862 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return err; |
| 864 | } |
| 865 | |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 866 | void xprt_end_transmit(struct rpc_task *task) |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 867 | { |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 868 | xprt_release_write(task->tk_xprt, task); |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 871 | /** |
| 872 | * xprt_transmit - send an RPC request on a transport |
| 873 | * @task: controlling RPC task |
| 874 | * |
| 875 | * We have to copy the iovec because sendmsg fiddles with its contents. |
| 876 | */ |
| 877 | void xprt_transmit(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | struct rpc_rqst *req = task->tk_rqstp; |
| 880 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 881 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 883 | dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (!req->rq_received) { |
| 886 | if (list_empty(&req->rq_list)) { |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 887 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /* Update the softirq receive buffer */ |
| 889 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, |
| 890 | sizeof(req->rq_private_buf)); |
| 891 | /* Add request to the receive list */ |
| 892 | list_add_tail(&req->rq_list, &xprt->recv); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 893 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | xprt_reset_majortimeo(req); |
Trond Myklebust | 0f9dc2b | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 895 | /* Turn off autodisconnect */ |
| 896 | del_singleshot_timer_sync(&xprt->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | } else if (!req->rq_bytes_sent) |
| 899 | return; |
| 900 | |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 901 | req->rq_connect_cookie = xprt->connect_cookie; |
Chuck Lever | b22602a | 2008-06-06 13:22:25 -0400 | [diff] [blame] | 902 | req->rq_xtime = jiffies; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 903 | status = xprt->ops->send_request(task); |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame^] | 904 | if (status != 0) { |
| 905 | task->tk_status = status; |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 906 | return; |
| 907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame^] | 909 | dprintk("RPC: %5u xmit complete\n", task->tk_pid); |
| 910 | spin_lock_bh(&xprt->transport_lock); |
| 911 | |
| 912 | xprt->ops->set_retrans_timeout(task); |
| 913 | |
| 914 | xprt->stat.sends++; |
| 915 | xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; |
| 916 | xprt->stat.bklog_u += xprt->backlog.qlen; |
| 917 | |
| 918 | /* Don't race with disconnect */ |
| 919 | if (!xprt_connected(xprt)) |
| 920 | task->tk_status = -ENOTCONN; |
| 921 | else if (!req->rq_received) |
| 922 | rpc_sleep_on(&xprt->pending, task, xprt_timer); |
| 923 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 926 | static inline void do_xprt_reserve(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
| 928 | struct rpc_xprt *xprt = task->tk_xprt; |
| 929 | |
| 930 | task->tk_status = 0; |
| 931 | if (task->tk_rqstp) |
| 932 | return; |
| 933 | if (!list_empty(&xprt->free)) { |
| 934 | struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); |
| 935 | list_del_init(&req->rq_list); |
| 936 | task->tk_rqstp = req; |
| 937 | xprt_request_init(task, xprt); |
| 938 | return; |
| 939 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 940 | dprintk("RPC: waiting for request slot\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | task->tk_status = -EAGAIN; |
| 942 | task->tk_timeout = 0; |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 943 | rpc_sleep_on(&xprt->backlog, task, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 946 | /** |
| 947 | * xprt_reserve - allocate an RPC request slot |
| 948 | * @task: RPC task requesting a slot allocation |
| 949 | * |
| 950 | * If no more slots are available, place the task on the transport's |
| 951 | * backlog queue. |
| 952 | */ |
| 953 | void xprt_reserve(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
| 955 | struct rpc_xprt *xprt = task->tk_xprt; |
| 956 | |
| 957 | task->tk_status = -EIO; |
Trond Myklebust | 0065db3 | 2006-01-03 09:55:56 +0100 | [diff] [blame] | 958 | spin_lock(&xprt->reserve_lock); |
| 959 | do_xprt_reserve(task); |
| 960 | spin_unlock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 963 | static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { |
| 965 | return xprt->xid++; |
| 966 | } |
| 967 | |
| 968 | static inline void xprt_init_xid(struct rpc_xprt *xprt) |
| 969 | { |
Chuck Lever | bf3fcf8 | 2006-05-25 01:40:51 -0400 | [diff] [blame] | 970 | xprt->xid = net_random(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 973 | static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
| 975 | struct rpc_rqst *req = task->tk_rqstp; |
| 976 | |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 977 | req->rq_timeout = task->tk_client->cl_timeout->to_initval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | req->rq_task = task; |
| 979 | req->rq_xprt = xprt; |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 980 | req->rq_buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | req->rq_xid = xprt_alloc_xid(xprt); |
J. Bruce Fields | ead5e1c | 2005-10-13 16:54:43 -0400 | [diff] [blame] | 982 | req->rq_release_snd_buf = NULL; |
Trond Myklebust | da45828 | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 983 | xprt_reset_majortimeo(req); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 984 | dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | req, ntohl(req->rq_xid)); |
| 986 | } |
| 987 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 988 | /** |
| 989 | * xprt_release - release an RPC request slot |
| 990 | * @task: task which is finished with the slot |
| 991 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 993 | void xprt_release(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
| 995 | struct rpc_xprt *xprt = task->tk_xprt; |
| 996 | struct rpc_rqst *req; |
| 997 | |
| 998 | if (!(req = task->tk_rqstp)) |
| 999 | return; |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 1000 | rpc_count_iostats(task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1001 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1002 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1003 | if (xprt->ops->release_request) |
| 1004 | xprt->ops->release_request(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (!list_empty(&req->rq_list)) |
| 1006 | list_del(&req->rq_list); |
| 1007 | xprt->last_used = jiffies; |
Trond Myklebust | 0065db3 | 2006-01-03 09:55:56 +0100 | [diff] [blame] | 1008 | if (list_empty(&xprt->recv)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1009 | mod_timer(&xprt->timer, |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1010 | xprt->last_used + xprt->idle_timeout); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1011 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 1012 | xprt->ops->buf_free(req->rq_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | task->tk_rqstp = NULL; |
J. Bruce Fields | ead5e1c | 2005-10-13 16:54:43 -0400 | [diff] [blame] | 1014 | if (req->rq_release_snd_buf) |
| 1015 | req->rq_release_snd_buf(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | memset(req, 0, sizeof(*req)); /* mark unused */ |
| 1017 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1018 | dprintk("RPC: %5u release request %p\n", task->tk_pid, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 1020 | spin_lock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | list_add(&req->rq_list, &xprt->free); |
Chuck Lever | 555ee3a | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 1022 | rpc_wake_up_next(&xprt->backlog); |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 1023 | spin_unlock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1026 | /** |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1027 | * xprt_create_transport - create an RPC transport |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1028 | * @args: rpc transport creation arguments |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1029 | * |
| 1030 | */ |
\"Talpey, Thomas\ | 3c341b0b | 2007-09-10 13:47:07 -0400 | [diff] [blame] | 1031 | struct rpc_xprt *xprt_create_transport(struct xprt_create *args) |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1032 | { |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1033 | struct rpc_xprt *xprt; |
| 1034 | struct rpc_rqst *req; |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 1035 | struct xprt_class *t; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1036 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 1037 | spin_lock(&xprt_list_lock); |
| 1038 | list_for_each_entry(t, &xprt_list, list) { |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 1039 | if (t->ident == args->ident) { |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 1040 | spin_unlock(&xprt_list_lock); |
| 1041 | goto found; |
| 1042 | } |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1043 | } |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 1044 | spin_unlock(&xprt_list_lock); |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 1045 | printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 1046 | return ERR_PTR(-EIO); |
| 1047 | |
| 1048 | found: |
| 1049 | xprt = t->setup(args); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1050 | if (IS_ERR(xprt)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1051 | dprintk("RPC: xprt_create_transport: failed, %ld\n", |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1052 | -PTR_ERR(xprt)); |
| 1053 | return xprt; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1056 | kref_init(&xprt->kref); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1057 | spin_lock_init(&xprt->transport_lock); |
| 1058 | spin_lock_init(&xprt->reserve_lock); |
| 1059 | |
| 1060 | INIT_LIST_HEAD(&xprt->free); |
| 1061 | INIT_LIST_HEAD(&xprt->recv); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1062 | INIT_WORK(&xprt->task_cleanup, xprt_autoclose); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1063 | setup_timer(&xprt->timer, xprt_init_autodisconnect, |
| 1064 | (unsigned long)xprt); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1065 | xprt->last_used = jiffies; |
| 1066 | xprt->cwnd = RPC_INITCWND; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1067 | xprt->bind_index = 0; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1068 | |
| 1069 | rpc_init_wait_queue(&xprt->binding, "xprt_binding"); |
| 1070 | rpc_init_wait_queue(&xprt->pending, "xprt_pending"); |
| 1071 | rpc_init_wait_queue(&xprt->sending, "xprt_sending"); |
| 1072 | rpc_init_wait_queue(&xprt->resend, "xprt_resend"); |
| 1073 | rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); |
| 1074 | |
| 1075 | /* initialize free list */ |
| 1076 | for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) |
| 1077 | list_add(&req->rq_list, &xprt->free); |
| 1078 | |
| 1079 | xprt_init_xid(xprt); |
| 1080 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1081 | dprintk("RPC: created transport %p with %u slots\n", xprt, |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 1082 | xprt->max_reqs); |
| 1083 | |
| 1084 | return xprt; |
| 1085 | } |
| 1086 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1087 | /** |
| 1088 | * xprt_destroy - destroy an RPC transport, killing off all requests. |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1089 | * @kref: kref for the transport to destroy |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1090 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | */ |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1092 | static void xprt_destroy(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1094 | struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref); |
| 1095 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1096 | dprintk("RPC: destroying transport %p\n", xprt); |
Trond Myklebust | 0065db3 | 2006-01-03 09:55:56 +0100 | [diff] [blame] | 1097 | xprt->shutdown = 1; |
| 1098 | del_timer_sync(&xprt->timer); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1099 | |
Trond Myklebust | f6a1cc8 | 2008-02-22 17:06:55 -0500 | [diff] [blame] | 1100 | rpc_destroy_wait_queue(&xprt->binding); |
| 1101 | rpc_destroy_wait_queue(&xprt->pending); |
| 1102 | rpc_destroy_wait_queue(&xprt->sending); |
| 1103 | rpc_destroy_wait_queue(&xprt->resend); |
| 1104 | rpc_destroy_wait_queue(&xprt->backlog); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1105 | /* |
| 1106 | * Tear down transport state and free the rpc_xprt |
| 1107 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1108 | xprt->ops->destroy(xprt); |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 1111 | /** |
| 1112 | * xprt_put - release a reference to an RPC transport. |
| 1113 | * @xprt: pointer to the transport |
| 1114 | * |
| 1115 | */ |
| 1116 | void xprt_put(struct rpc_xprt *xprt) |
| 1117 | { |
| 1118 | kref_put(&xprt->kref, xprt_destroy); |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * xprt_get - return a reference to an RPC transport. |
| 1123 | * @xprt: pointer to the transport |
| 1124 | * |
| 1125 | */ |
| 1126 | struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) |
| 1127 | { |
| 1128 | kref_get(&xprt->kref); |
| 1129 | return xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |