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