blob: 247fa1ec870c3d97687b72540acea6cb0e895898 [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>
44#include <linux/random.h>
45
Chuck Levera246b012005-08-11 16:25:23 -040046#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Local variables
50 */
51
52#ifdef RPC_DEBUG
53# undef RPC_DEBUG_DATA
54# 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
65static int xprt_clear_backlog(struct rpc_xprt *xprt);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Chuck Lever55aa4f52005-08-11 16:25:47 -040068 * Serialize write access to transports, in order to prevent different
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * requests from interfering with each other.
Chuck Lever55aa4f52005-08-11 16:25:47 -040070 * Also prevents transport connects from colliding with writes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72static int
73__xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
74{
75 struct rpc_rqst *req = task->tk_rqstp;
76
Chuck Lever2226feb2005-08-11 16:25:38 -040077 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (task == xprt->snd_task)
79 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 goto out_sleep;
81 }
82 if (xprt->nocong || __xprt_get_cong(xprt, task)) {
83 xprt->snd_task = task;
84 if (req) {
85 req->rq_bytes_sent = 0;
86 req->rq_ntrans++;
87 }
88 return 1;
89 }
90 smp_mb__before_clear_bit();
Chuck Lever2226feb2005-08-11 16:25:38 -040091 clear_bit(XPRT_LOCKED, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 smp_mb__after_clear_bit();
93out_sleep:
Chuck Lever55aa4f52005-08-11 16:25:47 -040094 dprintk("RPC: %4d failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 task->tk_timeout = 0;
96 task->tk_status = -EAGAIN;
97 if (req && req->rq_ntrans)
98 rpc_sleep_on(&xprt->resend, task, NULL, NULL);
99 else
100 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
101 return 0;
102}
103
104static inline int
105xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
106{
107 int retval;
108
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400109 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 retval = __xprt_lock_write(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400111 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return retval;
113}
114
115
116static void
117__xprt_lock_write_next(struct rpc_xprt *xprt)
118{
119 struct rpc_task *task;
120
Chuck Lever2226feb2005-08-11 16:25:38 -0400121 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return;
123 if (!xprt->nocong && RPCXPRT_CONGESTED(xprt))
124 goto out_unlock;
125 task = rpc_wake_up_next(&xprt->resend);
126 if (!task) {
127 task = rpc_wake_up_next(&xprt->sending);
128 if (!task)
129 goto out_unlock;
130 }
131 if (xprt->nocong || __xprt_get_cong(xprt, task)) {
132 struct rpc_rqst *req = task->tk_rqstp;
133 xprt->snd_task = task;
134 if (req) {
135 req->rq_bytes_sent = 0;
136 req->rq_ntrans++;
137 }
138 return;
139 }
140out_unlock:
141 smp_mb__before_clear_bit();
Chuck Lever2226feb2005-08-11 16:25:38 -0400142 clear_bit(XPRT_LOCKED, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 smp_mb__after_clear_bit();
144}
145
146/*
Chuck Lever55aa4f52005-08-11 16:25:47 -0400147 * Releases the transport for use by other requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
149static void
150__xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
151{
152 if (xprt->snd_task == task) {
153 xprt->snd_task = NULL;
154 smp_mb__before_clear_bit();
Chuck Lever2226feb2005-08-11 16:25:38 -0400155 clear_bit(XPRT_LOCKED, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 smp_mb__after_clear_bit();
157 __xprt_lock_write_next(xprt);
158 }
159}
160
161static inline void
162xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
163{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400164 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 __xprt_release_write(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400166 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Van Jacobson congestion avoidance. Check if the congestion window
171 * overflowed. Put the task to sleep if this is the case.
172 */
173static int
174__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
175{
176 struct rpc_rqst *req = task->tk_rqstp;
177
178 if (req->rq_cong)
179 return 1;
180 dprintk("RPC: %4d xprt_cwnd_limited cong = %ld cwnd = %ld\n",
181 task->tk_pid, xprt->cong, xprt->cwnd);
182 if (RPCXPRT_CONGESTED(xprt))
183 return 0;
184 req->rq_cong = 1;
185 xprt->cong += RPC_CWNDSCALE;
186 return 1;
187}
188
189/*
190 * Adjust the congestion window, and wake up the next task
191 * that has been sleeping due to congestion
192 */
193static void
194__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
195{
196 if (!req->rq_cong)
197 return;
198 req->rq_cong = 0;
199 xprt->cong -= RPC_CWNDSCALE;
200 __xprt_lock_write_next(xprt);
201}
202
203/*
204 * Adjust RPC congestion window
205 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
206 */
207static void
208xprt_adjust_cwnd(struct rpc_xprt *xprt, int result)
209{
210 unsigned long cwnd;
211
212 cwnd = xprt->cwnd;
213 if (result >= 0 && cwnd <= xprt->cong) {
214 /* The (cwnd >> 1) term makes sure
215 * the result gets rounded properly. */
216 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
217 if (cwnd > RPC_MAXCWND(xprt))
218 cwnd = RPC_MAXCWND(xprt);
219 __xprt_lock_write_next(xprt);
220 } else if (result == -ETIMEDOUT) {
221 cwnd >>= 1;
222 if (cwnd < RPC_CWNDSCALE)
223 cwnd = RPC_CWNDSCALE;
224 }
225 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
226 xprt->cong, xprt->cwnd, cwnd);
227 xprt->cwnd = cwnd;
228}
229
Chuck Lever44fbac22005-08-11 16:25:44 -0400230/**
231 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
232 * @xprt: transport with waiting tasks
233 * @status: result code to plant in each task before waking it
234 *
235 */
236void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
237{
238 if (status < 0)
239 rpc_wake_up_status(&xprt->pending, status);
240 else
241 rpc_wake_up(&xprt->pending);
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static void xprt_reset_majortimeo(struct rpc_rqst *req)
245{
246 struct rpc_timeout *to = &req->rq_xprt->timeout;
247
248 req->rq_majortimeo = req->rq_timeout;
249 if (to->to_exponential)
250 req->rq_majortimeo <<= to->to_retries;
251 else
252 req->rq_majortimeo += to->to_increment * to->to_retries;
253 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
254 req->rq_majortimeo = to->to_maxval;
255 req->rq_majortimeo += jiffies;
256}
257
Chuck Lever9903cd12005-08-11 16:25:26 -0400258/**
259 * xprt_adjust_timeout - adjust timeout values for next retransmit
260 * @req: RPC request containing parameters to use for the adjustment
261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263int xprt_adjust_timeout(struct rpc_rqst *req)
264{
265 struct rpc_xprt *xprt = req->rq_xprt;
266 struct rpc_timeout *to = &xprt->timeout;
267 int status = 0;
268
269 if (time_before(jiffies, req->rq_majortimeo)) {
270 if (to->to_exponential)
271 req->rq_timeout <<= 1;
272 else
273 req->rq_timeout += to->to_increment;
274 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
275 req->rq_timeout = to->to_maxval;
276 req->rq_retries++;
277 pprintk("RPC: %lu retrans\n", jiffies);
278 } else {
279 req->rq_timeout = to->to_initval;
280 req->rq_retries = 0;
281 xprt_reset_majortimeo(req);
282 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400283 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400285 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 pprintk("RPC: %lu timeout\n", jiffies);
287 status = -ETIMEDOUT;
288 }
289
290 if (req->rq_timeout == 0) {
291 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
292 req->rq_timeout = 5 * HZ;
293 }
294 return status;
295}
296
Chuck Lever55aa4f52005-08-11 16:25:47 -0400297static void xprt_autoclose(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
300
301 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400302 xprt->ops->close(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xprt_release_write(xprt, NULL);
304}
305
Chuck Lever9903cd12005-08-11 16:25:26 -0400306/**
307 * xprt_disconnect - mark a transport as disconnected
308 * @xprt: transport to flag for disconnect
309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Chuck Levera246b012005-08-11 16:25:23 -0400311void xprt_disconnect(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400314 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 xprt_clear_connected(xprt);
Chuck Lever44fbac22005-08-11 16:25:44 -0400316 xprt_wake_pending_tasks(xprt, -ENOTCONN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400317 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static void
321xprt_init_autodisconnect(unsigned long data)
322{
323 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
324
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400325 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!list_empty(&xprt->recv) || xprt->shutdown)
327 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400328 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400330 spin_unlock(&xprt->transport_lock);
Chuck Lever2226feb2005-08-11 16:25:38 -0400331 if (xprt_connecting(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xprt_release_write(xprt, NULL);
333 else
334 schedule_work(&xprt->task_cleanup);
335 return;
336out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400337 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Chuck Lever9903cd12005-08-11 16:25:26 -0400340/**
341 * xprt_connect - schedule a transport connect operation
342 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
344 */
345void xprt_connect(struct rpc_task *task)
346{
347 struct rpc_xprt *xprt = task->tk_xprt;
348
349 dprintk("RPC: %4d xprt_connect xprt %p %s connected\n", task->tk_pid,
350 xprt, (xprt_connected(xprt) ? "is" : "is not"));
351
352 if (xprt->shutdown) {
353 task->tk_status = -EIO;
354 return;
355 }
356 if (!xprt->addr.sin_port) {
357 task->tk_status = -EIO;
358 return;
359 }
360 if (!xprt_lock_write(xprt, task))
361 return;
362 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400363 xprt_release_write(xprt, task);
364 else {
365 if (task->tk_rqstp)
366 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Chuck Levera246b012005-08-11 16:25:23 -0400368 task->tk_timeout = RPC_CONNECT_TIMEOUT;
369 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
370 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Chuck Lever9903cd12005-08-11 16:25:26 -0400375static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct rpc_xprt *xprt = task->tk_xprt;
378
379 if (task->tk_status >= 0) {
380 dprintk("RPC: %4d xprt_connect_status: connection established\n",
381 task->tk_pid);
382 return;
383 }
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 switch (task->tk_status) {
386 case -ECONNREFUSED:
387 case -ECONNRESET:
Chuck Lever23475d62005-08-11 16:25:08 -0400388 dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n",
389 task->tk_pid, task->tk_client->cl_server);
390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case -ENOTCONN:
Chuck Lever23475d62005-08-11 16:25:08 -0400392 dprintk("RPC: %4d xprt_connect_status: connection broken\n",
393 task->tk_pid);
394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 case -ETIMEDOUT:
Chuck Lever23475d62005-08-11 16:25:08 -0400396 dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 task->tk_pid);
398 break;
399 default:
Chuck Lever23475d62005-08-11 16:25:08 -0400400 dprintk("RPC: %4d xprt_connect_status: error %d connecting to server %s\n",
401 task->tk_pid, -task->tk_status, task->tk_client->cl_server);
402 xprt_release_write(xprt, task);
403 task->tk_status = -EIO;
404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Chuck Lever23475d62005-08-11 16:25:08 -0400406
407 /* if soft mounted, just cause this RPC to fail */
408 if (RPC_IS_SOFT(task)) {
409 xprt_release_write(xprt, task);
410 task->tk_status = -EIO;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Chuck Lever9903cd12005-08-11 16:25:26 -0400414/**
415 * xprt_lookup_rqst - find an RPC request corresponding to an XID
416 * @xprt: transport on which the original request was transmitted
417 * @xid: RPC XID of incoming reply
418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Chuck Levera246b012005-08-11 16:25:23 -0400420struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct list_head *pos;
423 struct rpc_rqst *req = NULL;
424
425 list_for_each(pos, &xprt->recv) {
426 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
427 if (entry->rq_xid == xid) {
428 req = entry;
429 break;
430 }
431 }
432 return req;
433}
434
Chuck Lever9903cd12005-08-11 16:25:26 -0400435/**
436 * xprt_complete_rqst - called when reply processing is complete
437 * @xprt: controlling transport
438 * @req: RPC request that just completed
439 * @copied: actual number of bytes received from the transport
440 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Chuck Levera246b012005-08-11 16:25:23 -0400442void xprt_complete_rqst(struct rpc_xprt *xprt, struct rpc_rqst *req, int copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct rpc_task *task = req->rq_task;
445 struct rpc_clnt *clnt = task->tk_client;
446
447 /* Adjust congestion window */
448 if (!xprt->nocong) {
449 unsigned timer = task->tk_msg.rpc_proc->p_timer;
450 xprt_adjust_cwnd(xprt, copied);
451 __xprt_put_cong(xprt, req);
452 if (timer) {
453 if (req->rq_ntrans == 1)
454 rpc_update_rtt(clnt->cl_rtt, timer,
455 (long)jiffies - req->rq_xtime);
456 rpc_set_timeo(clnt->cl_rtt, timer, req->rq_ntrans - 1);
457 }
458 }
459
460#ifdef RPC_PROFILE
461 /* Profile only reads for now */
462 if (copied > 1024) {
463 static unsigned long nextstat;
464 static unsigned long pkt_rtt, pkt_len, pkt_cnt;
465
466 pkt_cnt++;
467 pkt_len += req->rq_slen + copied;
468 pkt_rtt += jiffies - req->rq_xtime;
469 if (time_before(nextstat, jiffies)) {
470 printk("RPC: %lu %ld cwnd\n", jiffies, xprt->cwnd);
471 printk("RPC: %ld %ld %ld %ld stat\n",
472 jiffies, pkt_cnt, pkt_len, pkt_rtt);
473 pkt_rtt = pkt_len = pkt_cnt = 0;
474 nextstat = jiffies + 5 * HZ;
475 }
476 }
477#endif
478
479 dprintk("RPC: %4d has input (%d bytes)\n", task->tk_pid, copied);
480 list_del_init(&req->rq_list);
481 req->rq_received = req->rq_private_buf.len = copied;
482
483 /* ... and wake up the process. */
484 rpc_wake_up_task(task);
485 return;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * RPC receive timeout handler.
490 */
491static void
492xprt_timer(struct rpc_task *task)
493{
494 struct rpc_rqst *req = task->tk_rqstp;
495 struct rpc_xprt *xprt = req->rq_xprt;
496
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400497 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (req->rq_received)
499 goto out;
500
501 xprt_adjust_cwnd(req->rq_xprt, -ETIMEDOUT);
502 __xprt_put_cong(xprt, req);
503
504 dprintk("RPC: %4d xprt_timer (%s request)\n",
505 task->tk_pid, req ? "pending" : "backlogged");
506
507 task->tk_status = -ETIMEDOUT;
508out:
509 task->tk_timeout = 0;
510 rpc_wake_up_task(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400511 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Chuck Lever9903cd12005-08-11 16:25:26 -0400514/**
515 * xprt_prepare_transmit - reserve the transport before sending a request
516 * @task: RPC task about to send a request
517 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400519int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct rpc_rqst *req = task->tk_rqstp;
522 struct rpc_xprt *xprt = req->rq_xprt;
523 int err = 0;
524
525 dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid);
526
527 if (xprt->shutdown)
528 return -EIO;
529
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400530 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (req->rq_received && !req->rq_bytes_sent) {
532 err = req->rq_received;
533 goto out_unlock;
534 }
535 if (!__xprt_lock_write(xprt, task)) {
536 err = -EAGAIN;
537 goto out_unlock;
538 }
539
540 if (!xprt_connected(xprt)) {
541 err = -ENOTCONN;
542 goto out_unlock;
543 }
544out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400545 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return err;
547}
548
Chuck Lever9903cd12005-08-11 16:25:26 -0400549/**
550 * xprt_transmit - send an RPC request on a transport
551 * @task: controlling RPC task
552 *
553 * We have to copy the iovec because sendmsg fiddles with its contents.
554 */
555void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct rpc_clnt *clnt = task->tk_client;
558 struct rpc_rqst *req = task->tk_rqstp;
559 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400560 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 dprintk("RPC: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 smp_rmb();
565 if (!req->rq_received) {
566 if (list_empty(&req->rq_list)) {
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400567 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Update the softirq receive buffer */
569 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
570 sizeof(req->rq_private_buf));
571 /* Add request to the receive list */
572 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400573 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000575 /* Turn off autodisconnect */
576 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 } else if (!req->rq_bytes_sent)
579 return;
580
Chuck Levera246b012005-08-11 16:25:23 -0400581 status = xprt->ops->send_request(task);
582 if (!status)
583 goto out_receive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Note: at this point, task->tk_sleeping has not yet been set,
586 * hence there is no danger of the waking up task being put on
587 * schedq, and being picked up by a parallel run of rpciod().
588 */
589 task->tk_status = status;
590
591 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 case -ECONNREFUSED:
593 task->tk_timeout = RPC_REESTABLISH_TIMEOUT;
594 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
Chuck Levera246b012005-08-11 16:25:23 -0400595 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case -ENOTCONN:
597 return;
598 default:
599 if (xprt->stream)
600 xprt_disconnect(xprt);
601 }
602 xprt_release_write(xprt, task);
603 return;
604 out_receive:
605 dprintk("RPC: %4d xmit complete\n", task->tk_pid);
606 /* Set the task's receive timeout value */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400607 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!xprt->nocong) {
609 int timer = task->tk_msg.rpc_proc->p_timer;
610 task->tk_timeout = rpc_calc_rto(clnt->cl_rtt, timer);
611 task->tk_timeout <<= rpc_ntimeo(clnt->cl_rtt, timer) + req->rq_retries;
612 if (task->tk_timeout > xprt->timeout.to_maxval || task->tk_timeout == 0)
613 task->tk_timeout = xprt->timeout.to_maxval;
614 } else
615 task->tk_timeout = req->rq_timeout;
616 /* Don't race with disconnect */
617 if (!xprt_connected(xprt))
618 task->tk_status = -ENOTCONN;
619 else if (!req->rq_received)
620 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
621 __xprt_release_write(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400622 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Chuck Lever9903cd12005-08-11 16:25:26 -0400625static inline void do_xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct rpc_xprt *xprt = task->tk_xprt;
628
629 task->tk_status = 0;
630 if (task->tk_rqstp)
631 return;
632 if (!list_empty(&xprt->free)) {
633 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
634 list_del_init(&req->rq_list);
635 task->tk_rqstp = req;
636 xprt_request_init(task, xprt);
637 return;
638 }
639 dprintk("RPC: waiting for request slot\n");
640 task->tk_status = -EAGAIN;
641 task->tk_timeout = 0;
642 rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
643}
644
Chuck Lever9903cd12005-08-11 16:25:26 -0400645/**
646 * xprt_reserve - allocate an RPC request slot
647 * @task: RPC task requesting a slot allocation
648 *
649 * If no more slots are available, place the task on the transport's
650 * backlog queue.
651 */
652void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct rpc_xprt *xprt = task->tk_xprt;
655
656 task->tk_status = -EIO;
657 if (!xprt->shutdown) {
Chuck Lever5dc07722005-08-11 16:25:35 -0400658 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 do_xprt_reserve(task);
Chuck Lever5dc07722005-08-11 16:25:35 -0400660 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt)
665{
666 return xprt->xid++;
667}
668
669static inline void xprt_init_xid(struct rpc_xprt *xprt)
670{
671 get_random_bytes(&xprt->xid, sizeof(xprt->xid));
672}
673
Chuck Lever9903cd12005-08-11 16:25:26 -0400674static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct rpc_rqst *req = task->tk_rqstp;
677
678 req->rq_timeout = xprt->timeout.to_initval;
679 req->rq_task = task;
680 req->rq_xprt = xprt;
681 req->rq_xid = xprt_alloc_xid(xprt);
682 dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid,
683 req, ntohl(req->rq_xid));
684}
685
Chuck Lever9903cd12005-08-11 16:25:26 -0400686/**
687 * xprt_release - release an RPC request slot
688 * @task: task which is finished with the slot
689 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400691void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct rpc_xprt *xprt = task->tk_xprt;
694 struct rpc_rqst *req;
695
696 if (!(req = task->tk_rqstp))
697 return;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400698 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 __xprt_release_write(xprt, task);
700 __xprt_put_cong(xprt, req);
701 if (!list_empty(&req->rq_list))
702 list_del(&req->rq_list);
703 xprt->last_used = jiffies;
704 if (list_empty(&xprt->recv) && !xprt->shutdown)
Chuck Levera246b012005-08-11 16:25:23 -0400705 mod_timer(&xprt->timer,
706 xprt->last_used + RPC_IDLE_DISCONNECT_TIMEOUT);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400707 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 task->tk_rqstp = NULL;
709 memset(req, 0, sizeof(*req)); /* mark unused */
710
711 dprintk("RPC: %4d release request %p\n", task->tk_pid, req);
712
Chuck Lever5dc07722005-08-11 16:25:35 -0400713 spin_lock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 list_add(&req->rq_list, &xprt->free);
715 xprt_clear_backlog(xprt);
Chuck Lever5dc07722005-08-11 16:25:35 -0400716 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Chuck Lever9903cd12005-08-11 16:25:26 -0400719/**
720 * xprt_set_timeout - set constant RPC timeout
721 * @to: RPC timeout parameters to set up
722 * @retr: number of retries
723 * @incr: amount of increase after each retry
724 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400726void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 to->to_initval =
729 to->to_increment = incr;
Chuck Levereab5c082005-08-11 16:25:14 -0400730 to->to_maxval = to->to_initval + (incr * retr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 to->to_retries = retr;
732 to->to_exponential = 0;
733}
734
Chuck Lever9903cd12005-08-11 16:25:26 -0400735static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Chuck Levera246b012005-08-11 16:25:23 -0400737 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct rpc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct rpc_rqst *req;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL)
742 return ERR_PTR(-ENOMEM);
743 memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 xprt->addr = *ap;
Chuck Levera246b012005-08-11 16:25:23 -0400746
747 switch (proto) {
748 case IPPROTO_UDP:
749 result = xs_setup_udp(xprt, to);
750 break;
751 case IPPROTO_TCP:
752 result = xs_setup_tcp(xprt, to);
753 break;
754 default:
755 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
756 proto);
757 result = -EIO;
758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Chuck Levera246b012005-08-11 16:25:23 -0400760 if (result) {
761 kfree(xprt);
762 return ERR_PTR(result);
763 }
764
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400765 spin_lock_init(&xprt->transport_lock);
Chuck Lever5dc07722005-08-11 16:25:35 -0400766 spin_lock_init(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 init_waitqueue_head(&xprt->cong_wait);
768
769 INIT_LIST_HEAD(&xprt->free);
770 INIT_LIST_HEAD(&xprt->recv);
Chuck Lever55aa4f52005-08-11 16:25:47 -0400771 INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 init_timer(&xprt->timer);
773 xprt->timer.function = xprt_init_autodisconnect;
774 xprt->timer.data = (unsigned long) xprt;
775 xprt->last_used = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
778 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
779 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
780 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
781
782 /* initialize free list */
Chuck Levera246b012005-08-11 16:25:23 -0400783 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 list_add(&req->rq_list, &xprt->free);
785
786 xprt_init_xid(xprt);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dprintk("RPC: created transport %p with %u slots\n", xprt,
789 xprt->max_reqs);
790
791 return xprt;
792}
793
Chuck Lever9903cd12005-08-11 16:25:26 -0400794/**
795 * xprt_create_proto - create an RPC client transport
796 * @proto: requested transport protocol
797 * @sap: remote peer's address
798 * @to: timeout parameters for new transport
799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400801struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct rpc_xprt *xprt;
804
805 xprt = xprt_setup(proto, sap, to);
806 if (IS_ERR(xprt))
807 dprintk("RPC: xprt_create_proto failed\n");
808 else
809 dprintk("RPC: xprt_create_proto created xprt %p\n", xprt);
810 return xprt;
811}
812
Chuck Lever9903cd12005-08-11 16:25:26 -0400813static void xprt_shutdown(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 xprt->shutdown = 1;
816 rpc_wake_up(&xprt->sending);
817 rpc_wake_up(&xprt->resend);
Chuck Lever44fbac22005-08-11 16:25:44 -0400818 xprt_wake_pending_tasks(xprt, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rpc_wake_up(&xprt->backlog);
820 wake_up(&xprt->cong_wait);
821 del_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Chuck Lever9903cd12005-08-11 16:25:26 -0400824static int xprt_clear_backlog(struct rpc_xprt *xprt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 rpc_wake_up_next(&xprt->backlog);
826 wake_up(&xprt->cong_wait);
827 return 1;
828}
829
Chuck Lever9903cd12005-08-11 16:25:26 -0400830/**
831 * xprt_destroy - destroy an RPC transport, killing off all requests.
832 * @xprt: transport to destroy
833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400835int xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 dprintk("RPC: destroying transport %p\n", xprt);
838 xprt_shutdown(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400839 xprt->ops->destroy(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 kfree(xprt);
841
842 return 0;
843}