blob: 06ca058572f236bb6fd322ff1686c0aaaa088abd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Lever55aa4f52005-08-11 16:25:47 -040013 * 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 Torvalds1da177e2005-04-16 15:20:36 -070016 * is run after the packet's timeout has expired.
17 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040018 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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 Lever55aa4f52005-08-11 16:25:47 -040035 *
36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Chuck Levera246b012005-08-11 16:25:23 -040039#include <linux/module.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040042#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040044#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Chuck Levera246b012005-08-11 16:25:23 -040046#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050047#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Local variables
51 */
52
53#ifdef RPC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070054# define RPCDBG_FACILITY RPCDBG_XPRT
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Local functions
59 */
60static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
61static inline void do_xprt_reserve(struct rpc_task *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void xprt_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
64
Jiri Slaby5ba03e82007-11-22 19:40:22 +080065static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040066static LIST_HEAD(xprt_list);
67
Chuck Lever555ee3a2005-08-25 16:25:54 -070068/*
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 Torvalds1da177e2005-04-16 15:20:36 -070085
Chuck Lever12a80462005-08-25 16:25:51 -070086/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040087 * 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 */
98int 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\4fa016e2007-09-10 13:47:57 -0400107 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400108 goto out;
109 }
110
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400111 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\81c098a2007-09-10 13:46:00 -0400115
116out:
117 spin_unlock(&xprt_list_lock);
118 return result;
119}
120EXPORT_SYMBOL_GPL(xprt_register_transport);
121
122/**
123 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800124 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400125 *
126 * Returns:
127 * 0: transport successfully unregistered
128 * -ENOENT: transport never registered
129 */
130int 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\81c098a2007-09-10 13:46:00 -0400143 goto out;
144 }
145 }
146 result = -ENOENT;
147
148out:
149 spin_unlock(&xprt_list_lock);
150 return result;
151}
152EXPORT_SYMBOL_GPL(xprt_unregister_transport);
153
154/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400155 * 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 */
162int 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);
180out:
181 return result;
182}
183EXPORT_SYMBOL_GPL(xprt_load_transport);
184
185/**
Chuck Lever12a80462005-08-25 16:25:51 -0700186 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Chuck Lever12a80462005-08-25 16:25:51 -0700193int xprt_reserve_xprt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Chuck Lever12a80462005-08-25 16:25:51 -0700195 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
212out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500213 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700214 task->tk_pid, xprt);
215 task->tk_timeout = 0;
216 task->tk_status = -EAGAIN;
217 if (req && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500218 rpc_sleep_on(&xprt->resend, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700219 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500220 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700221 return 0;
222}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400223EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700224
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100225static 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 Myklebustc1384c92007-06-14 18:00:42 -0400233 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100234}
235
Chuck Lever12a80462005-08-25 16:25:51 -0700236/*
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 */
244int xprt_reserve_xprt_cong(struct rpc_task *task)
245{
246 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 struct rpc_rqst *req = task->tk_rqstp;
248
Chuck Lever2226feb2005-08-11 16:25:38 -0400249 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (task == xprt->snd_task)
251 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto out_sleep;
253 }
Chuck Lever12a80462005-08-25 16:25:51 -0700254 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xprt->snd_task = task;
256 if (req) {
257 req->rq_bytes_sent = 0;
258 req->rq_ntrans++;
259 }
260 return 1;
261 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100262 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500264 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 task->tk_timeout = 0;
266 task->tk_status = -EAGAIN;
267 if (req && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500268 rpc_sleep_on(&xprt->resend, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500270 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400273EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Chuck Lever12a80462005-08-25 16:25:51 -0700275static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int retval;
278
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400279 spin_lock_bh(&xprt->transport_lock);
Chuck Lever12a80462005-08-25 16:25:51 -0700280 retval = xprt->ops->reserve_xprt(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400281 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return retval;
283}
284
Chuck Lever12a80462005-08-25 16:25:51 -0700285static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700288 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
308out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100309 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700310}
311
312static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
313{
314 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Chuck Lever2226feb2005-08-11 16:25:38 -0400316 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700318 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 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 Lever49e9a892005-08-25 16:25:51 -0700326 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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 }
335out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100336 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Chuck Lever49e9a892005-08-25 16:25:51 -0700339/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700346void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100349 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 __xprt_lock_write_next(xprt);
351 }
352}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400353EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Chuck Lever49e9a892005-08-25 16:25:51 -0700355/**
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 */
363void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
364{
365 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100366 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700367 __xprt_lock_write_next_cong(xprt);
368 }
369}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400370EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700371
372static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400374 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700375 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400376 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Van Jacobson congestion avoidance. Check if the congestion window
381 * overflowed. Put the task to sleep if this is the case.
382 */
383static 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 Lever46121cf2007-01-31 12:14:08 -0500390 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 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 */
403static 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 Lever49e9a892005-08-25 16:25:51 -0700410 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Chuck Lever46c0ee82005-08-25 16:25:52 -0700413/**
Chuck Levera58dd392005-08-25 16:25:53 -0700414 * 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 */
419void xprt_release_rqst_cong(struct rpc_task *task)
420{
421 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
422}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400423EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700424
425/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700426 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700430 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
431 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700432void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700434 struct rpc_rqst *req = task->tk_rqstp;
435 struct rpc_xprt *xprt = task->tk_xprt;
436 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 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 Lever49e9a892005-08-25 16:25:51 -0700444 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else if (result == -ETIMEDOUT) {
446 cwnd >>= 1;
447 if (cwnd < RPC_CWNDSCALE)
448 cwnd = RPC_CWNDSCALE;
449 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500450 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 xprt->cong, xprt->cwnd, cwnd);
452 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700453 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400455EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Chuck Lever44fbac22005-08-11 16:25:44 -0400457/**
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 */
463void 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\12444802007-09-10 13:45:36 -0400470EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400471
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400472/**
473 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
474 * @task: task to be put to sleep
Randy Dunlap0b80ae42008-04-26 22:59:02 -0700475 * @action: function pointer to be executed after wait
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400476 */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400477void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400478{
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 Myklebustb6ddf642008-04-17 18:52:19 -0400483 rpc_sleep_on(&xprt->pending, task, action);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400484}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400485EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400486
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 */
493void 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 Lever46121cf2007-01-31 12:14:08 -0500500 dprintk("RPC: write space: waking waiting task on "
501 "xprt %p\n", xprt);
Trond Myklebustfda13932008-02-22 16:34:12 -0500502 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400503 }
504 spin_unlock_bh(&xprt->transport_lock);
505}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400506EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400507
Chuck Leverfe3aca22005-08-25 16:25:50 -0700508/**
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 */
516void xprt_set_retrans_timeout_def(struct rpc_task *task)
517{
518 task->tk_timeout = task->tk_rqstp->rq_timeout;
519}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400520EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700521
522/*
523 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
524 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800525 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700526 * Set a request's retransmit timeout using the RTT estimator.
527 */
528void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
529{
530 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500531 struct rpc_clnt *clnt = task->tk_client;
532 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700533 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500534 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700535
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\12444802007-09-10 13:45:36 -0400541EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void xprt_reset_majortimeo(struct rpc_rqst *req)
544{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500545 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
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 Lever9903cd12005-08-11 16:25:26 -0400557/**
558 * xprt_adjust_timeout - adjust timeout values for next retransmit
559 * @req: RPC request containing parameters to use for the adjustment
560 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
562int xprt_adjust_timeout(struct rpc_rqst *req)
563{
564 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500565 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 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 Torvalds1da177e2005-04-16 15:20:36 -0700576 } 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 Lever4a0f8c02005-08-11 16:25:32 -0400581 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400583 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 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 Howells65f27f32006-11-22 14:55:48 +0000594static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
David Howells65f27f32006-11-22 14:55:48 +0000596 struct rpc_xprt *xprt =
597 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Chuck Levera246b012005-08-11 16:25:23 -0400599 xprt->ops->close(xprt);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500600 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xprt_release_write(xprt, NULL);
602}
603
Chuck Lever9903cd12005-08-11 16:25:26 -0400604/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500605 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400606 * @xprt: transport to flag for disconnect
607 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500609void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Chuck Lever46121cf2007-01-31 12:14:08 -0500611 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400612 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xprt_clear_connected(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400614 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400615 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500617EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Trond Myklebust66af1e52007-11-06 10:18:36 -0500619/**
620 * xprt_force_disconnect - force a transport to disconnect
621 * @xprt: transport to disconnect
622 *
623 */
624void 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 Myklebust2a491992009-03-11 14:38:00 -0400632 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500633 spin_unlock_bh(&xprt->transport_lock);
634}
Trond Myklebust66af1e52007-11-06 10:18:36 -0500635
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400636/**
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 */
647void 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);
Trond Myklebust2a491992009-03-11 14:38:00 -0400659 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400660out:
661 spin_unlock_bh(&xprt->transport_lock);
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void
665xprt_init_autodisconnect(unsigned long data)
666{
667 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
668
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400669 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!list_empty(&xprt->recv) || xprt->shutdown)
671 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400672 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400674 spin_unlock(&xprt->transport_lock);
Trond Myklebustf75e6742009-04-21 17:18:20 -0400675 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
676 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return;
678out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400679 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Chuck Lever9903cd12005-08-11 16:25:26 -0400682/**
683 * xprt_connect - schedule a transport connect operation
684 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
686 */
687void xprt_connect(struct rpc_task *task)
688{
689 struct rpc_xprt *xprt = task->tk_xprt;
690
Chuck Lever46121cf2007-01-31 12:14:08 -0500691 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 xprt, (xprt_connected(xprt) ? "is" : "is not"));
693
Chuck Leverec739ef2006-08-22 20:06:15 -0400694 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400695 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return;
697 }
698 if (!xprt_lock_write(xprt, task))
699 return;
700 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400701 xprt_release_write(xprt, task);
702 else {
703 if (task->tk_rqstp)
704 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Chuck Lever03bf4b72005-08-25 16:25:55 -0700706 task->tk_timeout = xprt->connect_timeout;
Trond Myklebust5d008372008-02-22 16:34:17 -0500707 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Chuck Lever262ca072006-03-20 13:44:16 -0500708 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400709 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Chuck Lever9903cd12005-08-11 16:25:26 -0400714static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct rpc_xprt *xprt = task->tk_xprt;
717
Chuck Levercd983ef2008-06-11 17:56:13 -0400718 if (task->tk_status == 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500719 xprt->stat.connect_count++;
720 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500721 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 task->tk_pid);
723 return;
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 switch (task->tk_status) {
Trond Myklebust2a491992009-03-11 14:38:00 -0400727 case -EAGAIN:
728 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
Chuck Lever23475d62005-08-11 16:25:08 -0400729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500731 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
732 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500735 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
736 "server %s\n", task->tk_pid, -task->tk_status,
737 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400738 xprt_release_write(xprt, task);
739 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Chuck Lever9903cd12005-08-11 16:25:26 -0400743/**
744 * xprt_lookup_rqst - find an RPC request corresponding to an XID
745 * @xprt: transport on which the original request was transmitted
746 * @xid: RPC XID of incoming reply
747 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700749struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 list_for_each(pos, &xprt->recv) {
754 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
Chuck Lever262ca072006-03-20 13:44:16 -0500755 if (entry->rq_xid == xid)
756 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500758
759 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
760 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500761 xprt->stat.bad_xids++;
762 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400764EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Chuck Lever9903cd12005-08-11 16:25:26 -0400766/**
Chuck Lever1570c1e2005-08-25 16:25:52 -0700767 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
768 * @task: RPC request that recently completed
Chuck Lever9903cd12005-08-11 16:25:26 -0400769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Chuck Lever1570c1e2005-08-25 16:25:52 -0700771void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700773 struct rpc_rqst *req = task->tk_rqstp;
774 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
775 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Chuck Lever1570c1e2005-08-25 16:25:52 -0700777 if (timer) {
778 if (req->rq_ntrans == 1)
779 rpc_update_rtt(rtt, timer,
780 (long)jiffies - req->rq_xtime);
781 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700783}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400784EXPORT_SYMBOL_GPL(xprt_update_rtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Chuck Lever1570c1e2005-08-25 16:25:52 -0700786/**
787 * xprt_complete_rqst - called when reply processing is complete
788 * @task: RPC request that recently completed
789 * @copied: actual number of bytes received from the transport
790 *
791 * Caller holds transport lock.
792 */
793void xprt_complete_rqst(struct rpc_task *task, int copied)
794{
795 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -0500796 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Chuck Lever1570c1e2005-08-25 16:25:52 -0700798 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
799 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Trond Myklebustfda13932008-02-22 16:34:12 -0500801 xprt->stat.recvs++;
Chuck Leveref759a22006-03-20 13:44:17 -0500802 task->tk_rtt = (long)jiffies - req->rq_xtime;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 list_del_init(&req->rq_list);
Trond Myklebust1e799b62008-03-21 16:19:41 -0400805 req->rq_private_buf.len = copied;
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500806 /* Ensure all writes are done before we update req->rq_received */
807 smp_wmb();
Trond Myklebust1e799b62008-03-21 16:19:41 -0400808 req->rq_received = copied;
Trond Myklebustfda13932008-02-22 16:34:12 -0500809 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400811EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chuck Lever46c0ee82005-08-25 16:25:52 -0700813static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700815 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct rpc_xprt *xprt = req->rq_xprt;
817
Trond Myklebust5d008372008-02-22 16:34:17 -0500818 if (task->tk_status != -ETIMEDOUT)
819 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500820 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700821
Trond Myklebust5d008372008-02-22 16:34:17 -0500822 spin_lock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700823 if (!req->rq_received) {
824 if (xprt->ops->timer)
825 xprt->ops->timer(task);
Trond Myklebust5d008372008-02-22 16:34:17 -0500826 } else
827 task->tk_status = 0;
828 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Chuck Lever9903cd12005-08-11 16:25:26 -0400831/**
832 * xprt_prepare_transmit - reserve the transport before sending a request
833 * @task: RPC task about to send a request
834 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400836int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct rpc_rqst *req = task->tk_rqstp;
839 struct rpc_xprt *xprt = req->rq_xprt;
840 int err = 0;
841
Chuck Lever46121cf2007-01-31 12:14:08 -0500842 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400844 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (req->rq_received && !req->rq_bytes_sent) {
846 err = req->rq_received;
847 goto out_unlock;
848 }
Trond Myklebust2a491992009-03-11 14:38:00 -0400849 if (!xprt->ops->reserve_xprt(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400852 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return err;
854}
855
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400856void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700857{
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400858 xprt_release_write(task->tk_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700859}
860
Chuck Lever9903cd12005-08-11 16:25:26 -0400861/**
862 * xprt_transmit - send an RPC request on a transport
863 * @task: controlling RPC task
864 *
865 * We have to copy the iovec because sendmsg fiddles with its contents.
866 */
867void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct rpc_rqst *req = task->tk_rqstp;
870 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400871 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Chuck Lever46121cf2007-01-31 12:14:08 -0500873 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!req->rq_received) {
876 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400877 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Update the softirq receive buffer */
879 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
880 sizeof(req->rq_private_buf));
881 /* Add request to the receive list */
882 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400883 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000885 /* Turn off autodisconnect */
886 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 } else if (!req->rq_bytes_sent)
889 return;
890
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400891 req->rq_connect_cookie = xprt->connect_cookie;
Chuck Leverb22602a2008-06-06 13:22:25 -0400892 req->rq_xtime = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400893 status = xprt->ops->send_request(task);
Trond Myklebustc8485e42009-03-11 14:37:59 -0400894 if (status != 0) {
895 task->tk_status = status;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700896 return;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Trond Myklebustc8485e42009-03-11 14:37:59 -0400899 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
900 spin_lock_bh(&xprt->transport_lock);
901
902 xprt->ops->set_retrans_timeout(task);
903
904 xprt->stat.sends++;
905 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
906 xprt->stat.bklog_u += xprt->backlog.qlen;
907
908 /* Don't race with disconnect */
909 if (!xprt_connected(xprt))
910 task->tk_status = -ENOTCONN;
911 else if (!req->rq_received)
912 rpc_sleep_on(&xprt->pending, task, xprt_timer);
913 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Chuck Lever9903cd12005-08-11 16:25:26 -0400916static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct rpc_xprt *xprt = task->tk_xprt;
919
920 task->tk_status = 0;
921 if (task->tk_rqstp)
922 return;
923 if (!list_empty(&xprt->free)) {
924 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
925 list_del_init(&req->rq_list);
926 task->tk_rqstp = req;
927 xprt_request_init(task, xprt);
928 return;
929 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500930 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 task->tk_status = -EAGAIN;
932 task->tk_timeout = 0;
Trond Myklebust5d008372008-02-22 16:34:17 -0500933 rpc_sleep_on(&xprt->backlog, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Chuck Lever9903cd12005-08-11 16:25:26 -0400936/**
937 * xprt_reserve - allocate an RPC request slot
938 * @task: RPC task requesting a slot allocation
939 *
940 * If no more slots are available, place the task on the transport's
941 * backlog queue.
942 */
943void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct rpc_xprt *xprt = task->tk_xprt;
946
947 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +0100948 spin_lock(&xprt->reserve_lock);
949 do_xprt_reserve(task);
950 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700953static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 return xprt->xid++;
956}
957
958static inline void xprt_init_xid(struct rpc_xprt *xprt)
959{
Chuck Leverbf3fcf82006-05-25 01:40:51 -0400960 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Chuck Lever9903cd12005-08-11 16:25:26 -0400963static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct rpc_rqst *req = task->tk_rqstp;
966
Trond Myklebustba7392b2007-12-20 16:03:55 -0500967 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 req->rq_task = task;
969 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +0100970 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -0400972 req->rq_release_snd_buf = NULL;
Trond Myklebustda458282006-08-31 15:44:52 -0400973 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -0500974 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 req, ntohl(req->rq_xid));
976}
977
Chuck Lever9903cd12005-08-11 16:25:26 -0400978/**
979 * xprt_release - release an RPC request slot
980 * @task: task which is finished with the slot
981 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400983void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct rpc_xprt *xprt = task->tk_xprt;
986 struct rpc_rqst *req;
987
988 if (!(req = task->tk_rqstp))
989 return;
Chuck Lever11c556b2006-03-20 13:44:22 -0500990 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400991 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700992 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -0700993 if (xprt->ops->release_request)
994 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!list_empty(&req->rq_list))
996 list_del(&req->rq_list);
997 xprt->last_used = jiffies;
Trond Myklebust0065db32006-01-03 09:55:56 +0100998 if (list_empty(&xprt->recv))
Chuck Levera246b012005-08-11 16:25:23 -0400999 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001000 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001001 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverc5a4dd82007-03-29 16:47:58 -04001002 xprt->ops->buf_free(req->rq_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001004 if (req->rq_release_snd_buf)
1005 req->rq_release_snd_buf(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 memset(req, 0, sizeof(*req)); /* mark unused */
1007
Chuck Lever46121cf2007-01-31 12:14:08 -05001008 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Chuck Lever5dc07722005-08-11 16:25:35 -04001010 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 list_add(&req->rq_list, &xprt->free);
Chuck Lever555ee3a2005-08-25 16:25:54 -07001012 rpc_wake_up_next(&xprt->backlog);
Chuck Lever5dc07722005-08-11 16:25:35 -04001013 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Chuck Lever9903cd12005-08-11 16:25:26 -04001016/**
Chuck Leverc2866762006-08-22 20:06:20 -04001017 * xprt_create_transport - create an RPC transport
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001018 * @args: rpc transport creation arguments
Chuck Leverc2866762006-08-22 20:06:20 -04001019 *
1020 */
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04001021struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
Chuck Leverc2866762006-08-22 20:06:20 -04001022{
Chuck Leverc2866762006-08-22 20:06:20 -04001023 struct rpc_xprt *xprt;
1024 struct rpc_rqst *req;
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001025 struct xprt_class *t;
Chuck Leverc2866762006-08-22 20:06:20 -04001026
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001027 spin_lock(&xprt_list_lock);
1028 list_for_each_entry(t, &xprt_list, list) {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001029 if (t->ident == args->ident) {
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001030 spin_unlock(&xprt_list_lock);
1031 goto found;
1032 }
Chuck Leverc2866762006-08-22 20:06:20 -04001033 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001034 spin_unlock(&xprt_list_lock);
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001035 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001036 return ERR_PTR(-EIO);
1037
1038found:
1039 xprt = t->setup(args);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001040 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001041 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -04001042 -PTR_ERR(xprt));
1043 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001044 }
1045
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001046 kref_init(&xprt->kref);
Chuck Leverc2866762006-08-22 20:06:20 -04001047 spin_lock_init(&xprt->transport_lock);
1048 spin_lock_init(&xprt->reserve_lock);
1049
1050 INIT_LIST_HEAD(&xprt->free);
1051 INIT_LIST_HEAD(&xprt->recv);
David Howells65f27f32006-11-22 14:55:48 +00001052 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001053 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1054 (unsigned long)xprt);
Chuck Leverc2866762006-08-22 20:06:20 -04001055 xprt->last_used = jiffies;
1056 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001057 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001058
1059 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1060 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1061 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1062 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1063 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1064
1065 /* initialize free list */
1066 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1067 list_add(&req->rq_list, &xprt->free);
1068
1069 xprt_init_xid(xprt);
1070
Chuck Lever46121cf2007-01-31 12:14:08 -05001071 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001072 xprt->max_reqs);
1073
1074 return xprt;
1075}
1076
Chuck Lever9903cd12005-08-11 16:25:26 -04001077/**
1078 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001079 * @kref: kref for the transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001082static void xprt_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001084 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
1085
Chuck Lever46121cf2007-01-31 12:14:08 -05001086 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +01001087 xprt->shutdown = 1;
1088 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001089
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001090 rpc_destroy_wait_queue(&xprt->binding);
1091 rpc_destroy_wait_queue(&xprt->pending);
1092 rpc_destroy_wait_queue(&xprt->sending);
1093 rpc_destroy_wait_queue(&xprt->resend);
1094 rpc_destroy_wait_queue(&xprt->backlog);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001095 /*
1096 * Tear down transport state and free the rpc_xprt
1097 */
Chuck Levera246b012005-08-11 16:25:23 -04001098 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001101/**
1102 * xprt_put - release a reference to an RPC transport.
1103 * @xprt: pointer to the transport
1104 *
1105 */
1106void xprt_put(struct rpc_xprt *xprt)
1107{
1108 kref_put(&xprt->kref, xprt_destroy);
1109}
1110
1111/**
1112 * xprt_get - return a reference to an RPC transport.
1113 * @xprt: pointer to the transport
1114 *
1115 */
1116struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1117{
1118 kref_get(&xprt->kref);
1119 return xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}