Merge commit 'v3.1-rc1' into imx-fixes
[deliverable/linux.git] / net / sunrpc / xprt.c
CommitLineData
1da177e4
LT
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
55aa4f58
CL
13 * the request struct, and calls xprt_transmit().
14 * - xprt_transmit sends the message and installs the caller on the
55ae1aab
RL
15 * transport's wait list. At the same time, if a reply is expected,
16 * it installs a timer that is run after the packet's timeout has
17 * expired.
1da177e4 18 * - When a packet arrives, the data_ready handler walks the list of
55aa4f58 19 * pending requests for that transport. If a matching XID is found, the
1da177e4
LT
20 * caller is woken up, and the timer removed.
21 * - When no reply arrives within the timeout interval, the timer is
22 * fired by the kernel and runs xprt_timer(). It either adjusts the
23 * timeout values (minor timeout) or wakes up the caller with a status
24 * of -ETIMEDOUT.
25 * - When the caller receives a notification from RPC that a reply arrived,
26 * it should release the RPC slot, and process the reply.
27 * If the call timed out, it may choose to retry the operation by
28 * adjusting the initial timeout value, and simply calling rpc_call
29 * again.
30 *
31 * Support for async RPC is done through a set of RPC-specific scheduling
32 * primitives that `transparently' work for processes as well as async
33 * tasks that rely on callbacks.
34 *
35 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
55aa4f58
CL
36 *
37 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
1da177e4
LT
38 */
39
a246b010
CL
40#include <linux/module.h>
41
1da177e4 42#include <linux/types.h>
a246b010 43#include <linux/interrupt.h>
1da177e4 44#include <linux/workqueue.h>
bf3fcf89 45#include <linux/net.h>
ff839970 46#include <linux/ktime.h>
1da177e4 47
a246b010 48#include <linux/sunrpc/clnt.h>
11c556b3 49#include <linux/sunrpc/metrics.h>
c9acb42e 50#include <linux/sunrpc/bc_xprt.h>
1da177e4 51
55ae1aab
RL
52#include "sunrpc.h"
53
1da177e4
LT
54/*
55 * Local variables
56 */
57
58#ifdef RPC_DEBUG
1da177e4
LT
59# define RPCDBG_FACILITY RPCDBG_XPRT
60#endif
61
1da177e4
LT
62/*
63 * Local functions
64 */
21de0a95 65static void xprt_init(struct rpc_xprt *xprt, struct net *net);
1da177e4 66static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
1da177e4 67static void xprt_connect_status(struct rpc_task *task);
1da177e4
LT
68static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
69
5ba03e82 70static DEFINE_SPINLOCK(xprt_list_lock);
81c098af
TT
71static LIST_HEAD(xprt_list);
72
555ee3af
CL
73/*
74 * The transport code maintains an estimate on the maximum number of out-
75 * standing RPC requests, using a smoothed version of the congestion
76 * avoidance implemented in 44BSD. This is basically the Van Jacobson
77 * congestion algorithm: If a retransmit occurs, the congestion window is
78 * halved; otherwise, it is incremented by 1/cwnd when
79 *
80 * - a reply is received and
81 * - a full number of requests are outstanding and
82 * - the congestion window hasn't been updated recently.
83 */
84#define RPC_CWNDSHIFT (8U)
85#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
86#define RPC_INITCWND RPC_CWNDSCALE
87#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
88
89#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
1da177e4 90
81c098af
TT
91/**
92 * xprt_register_transport - register a transport implementation
93 * @transport: transport to register
94 *
95 * If a transport implementation is loaded as a kernel module, it can
96 * call this interface to make itself known to the RPC client.
97 *
98 * Returns:
99 * 0: transport successfully registered
100 * -EEXIST: transport already registered
101 * -EINVAL: transport module being unloaded
102 */
103int xprt_register_transport(struct xprt_class *transport)
104{
105 struct xprt_class *t;
106 int result;
107
108 result = -EEXIST;
109 spin_lock(&xprt_list_lock);
110 list_for_each_entry(t, &xprt_list, list) {
111 /* don't register the same transport class twice */
4fa016eb 112 if (t->ident == transport->ident)
81c098af
TT
113 goto out;
114 }
115
c9f6cde6
DL
116 list_add_tail(&transport->list, &xprt_list);
117 printk(KERN_INFO "RPC: Registered %s transport module.\n",
118 transport->name);
119 result = 0;
81c098af
TT
120
121out:
122 spin_unlock(&xprt_list_lock);
123 return result;
124}
125EXPORT_SYMBOL_GPL(xprt_register_transport);
126
127/**
128 * xprt_unregister_transport - unregister a transport implementation
65b6e42c 129 * @transport: transport to unregister
81c098af
TT
130 *
131 * Returns:
132 * 0: transport successfully unregistered
133 * -ENOENT: transport never registered
134 */
135int xprt_unregister_transport(struct xprt_class *transport)
136{
137 struct xprt_class *t;
138 int result;
139
140 result = 0;
141 spin_lock(&xprt_list_lock);
142 list_for_each_entry(t, &xprt_list, list) {
143 if (t == transport) {
144 printk(KERN_INFO
145 "RPC: Unregistered %s transport module.\n",
146 transport->name);
147 list_del_init(&transport->list);
81c098af
TT
148 goto out;
149 }
150 }
151 result = -ENOENT;
152
153out:
154 spin_unlock(&xprt_list_lock);
155 return result;
156}
157EXPORT_SYMBOL_GPL(xprt_unregister_transport);
158
441e3e24
TT
159/**
160 * xprt_load_transport - load a transport implementation
161 * @transport_name: transport to load
162 *
163 * Returns:
164 * 0: transport successfully loaded
165 * -ENOENT: transport module not available
166 */
167int xprt_load_transport(const char *transport_name)
168{
169 struct xprt_class *t;
441e3e24
TT
170 int result;
171
172 result = 0;
173 spin_lock(&xprt_list_lock);
174 list_for_each_entry(t, &xprt_list, list) {
175 if (strcmp(t->name, transport_name) == 0) {
176 spin_unlock(&xprt_list_lock);
177 goto out;
178 }
179 }
180 spin_unlock(&xprt_list_lock);
ef7ffe8f 181 result = request_module("xprt%s", transport_name);
441e3e24
TT
182out:
183 return result;
184}
185EXPORT_SYMBOL_GPL(xprt_load_transport);
186
12a80469
CL
187/**
188 * xprt_reserve_xprt - serialize write access to transports
189 * @task: task that is requesting access to the transport
177c27bf 190 * @xprt: pointer to the target transport
12a80469
CL
191 *
192 * This prevents mixing the payload of separate requests, and prevents
193 * transport connects from colliding with writes. No congestion control
194 * is provided.
195 */
43cedbf0 196int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
12a80469 197{
12a80469 198 struct rpc_rqst *req = task->tk_rqstp;
34006cee 199 int priority;
12a80469
CL
200
201 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
202 if (task == xprt->snd_task)
203 return 1;
12a80469
CL
204 goto out_sleep;
205 }
206 xprt->snd_task = task;
43cedbf0
TM
207 if (req != NULL) {
208 req->rq_bytes_sent = 0;
209 req->rq_ntrans++;
210 }
4d4a76f3 211
12a80469
CL
212 return 1;
213
214out_sleep:
46121cf7 215 dprintk("RPC: %5u failed to lock transport %p\n",
12a80469
CL
216 task->tk_pid, xprt);
217 task->tk_timeout = 0;
218 task->tk_status = -EAGAIN;
34006cee
TM
219 if (req == NULL)
220 priority = RPC_PRIORITY_LOW;
221 else if (!req->rq_ntrans)
222 priority = RPC_PRIORITY_NORMAL;
12a80469 223 else
34006cee
TM
224 priority = RPC_PRIORITY_HIGH;
225 rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
12a80469
CL
226 return 0;
227}
12444809 228EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
12a80469 229
632e3bdc
TM
230static void xprt_clear_locked(struct rpc_xprt *xprt)
231{
232 xprt->snd_task = NULL;
233 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
234 smp_mb__before_clear_bit();
235 clear_bit(XPRT_LOCKED, &xprt->state);
236 smp_mb__after_clear_bit();
237 } else
c1384c9c 238 queue_work(rpciod_workqueue, &xprt->task_cleanup);
632e3bdc
TM
239}
240
1da177e4 241/*
12a80469
CL
242 * xprt_reserve_xprt_cong - serialize write access to transports
243 * @task: task that is requesting access to the transport
244 *
245 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
246 * integrated into the decision of whether a request is allowed to be
247 * woken up and given access to the transport.
1da177e4 248 */
43cedbf0 249int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
250{
251 struct rpc_rqst *req = task->tk_rqstp;
34006cee 252 int priority;
1da177e4 253
2226feb6 254 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
1da177e4
LT
255 if (task == xprt->snd_task)
256 return 1;
1da177e4
LT
257 goto out_sleep;
258 }
43cedbf0
TM
259 if (req == NULL) {
260 xprt->snd_task = task;
261 return 1;
262 }
12a80469 263 if (__xprt_get_cong(xprt, task)) {
1da177e4 264 xprt->snd_task = task;
43cedbf0
TM
265 req->rq_bytes_sent = 0;
266 req->rq_ntrans++;
1da177e4
LT
267 return 1;
268 }
632e3bdc 269 xprt_clear_locked(xprt);
1da177e4 270out_sleep:
46121cf7 271 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
1da177e4
LT
272 task->tk_timeout = 0;
273 task->tk_status = -EAGAIN;
34006cee
TM
274 if (req == NULL)
275 priority = RPC_PRIORITY_LOW;
276 else if (!req->rq_ntrans)
277 priority = RPC_PRIORITY_NORMAL;
1da177e4 278 else
34006cee
TM
279 priority = RPC_PRIORITY_HIGH;
280 rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
1da177e4
LT
281 return 0;
282}
12444809 283EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
1da177e4 284
12a80469 285static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
286{
287 int retval;
288
4a0f8c04 289 spin_lock_bh(&xprt->transport_lock);
43cedbf0 290 retval = xprt->ops->reserve_xprt(xprt, task);
4a0f8c04 291 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
292 return retval;
293}
294
12a80469 295static void __xprt_lock_write_next(struct rpc_xprt *xprt)
49e9a890
CL
296{
297 struct rpc_task *task;
298 struct rpc_rqst *req;
299
300 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
301 return;
302
34006cee
TM
303 task = rpc_wake_up_next(&xprt->sending);
304 if (task == NULL)
305 goto out_unlock;
49e9a890
CL
306
307 req = task->tk_rqstp;
308 xprt->snd_task = task;
309 if (req) {
310 req->rq_bytes_sent = 0;
311 req->rq_ntrans++;
312 }
313 return;
314
315out_unlock:
632e3bdc 316 xprt_clear_locked(xprt);
49e9a890
CL
317}
318
319static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
1da177e4
LT
320{
321 struct rpc_task *task;
43cedbf0 322 struct rpc_rqst *req;
1da177e4 323
2226feb6 324 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 325 return;
49e9a890 326 if (RPCXPRT_CONGESTED(xprt))
1da177e4 327 goto out_unlock;
34006cee
TM
328 task = rpc_wake_up_next(&xprt->sending);
329 if (task == NULL)
330 goto out_unlock;
43cedbf0
TM
331
332 req = task->tk_rqstp;
333 if (req == NULL) {
334 xprt->snd_task = task;
335 return;
336 }
49e9a890 337 if (__xprt_get_cong(xprt, task)) {
1da177e4 338 xprt->snd_task = task;
43cedbf0
TM
339 req->rq_bytes_sent = 0;
340 req->rq_ntrans++;
1da177e4
LT
341 return;
342 }
343out_unlock:
632e3bdc 344 xprt_clear_locked(xprt);
1da177e4
LT
345}
346
49e9a890
CL
347/**
348 * xprt_release_xprt - allow other requests to use a transport
349 * @xprt: transport with other tasks potentially waiting
350 * @task: task that is releasing access to the transport
351 *
352 * Note that "task" can be NULL. No congestion control is provided.
1da177e4 353 */
49e9a890 354void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
355{
356 if (xprt->snd_task == task) {
632e3bdc 357 xprt_clear_locked(xprt);
1da177e4
LT
358 __xprt_lock_write_next(xprt);
359 }
360}
12444809 361EXPORT_SYMBOL_GPL(xprt_release_xprt);
1da177e4 362
49e9a890
CL
363/**
364 * xprt_release_xprt_cong - allow other requests to use a transport
365 * @xprt: transport with other tasks potentially waiting
366 * @task: task that is releasing access to the transport
367 *
368 * Note that "task" can be NULL. Another task is awoken to use the
369 * transport if the transport's congestion window allows it.
370 */
371void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
372{
373 if (xprt->snd_task == task) {
632e3bdc 374 xprt_clear_locked(xprt);
49e9a890
CL
375 __xprt_lock_write_next_cong(xprt);
376 }
377}
12444809 378EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
49e9a890
CL
379
380static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4 381{
4a0f8c04 382 spin_lock_bh(&xprt->transport_lock);
49e9a890 383 xprt->ops->release_xprt(xprt, task);
4a0f8c04 384 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
385}
386
1da177e4
LT
387/*
388 * Van Jacobson congestion avoidance. Check if the congestion window
389 * overflowed. Put the task to sleep if this is the case.
390 */
391static int
392__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
393{
394 struct rpc_rqst *req = task->tk_rqstp;
395
396 if (req->rq_cong)
397 return 1;
46121cf7 398 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
1da177e4
LT
399 task->tk_pid, xprt->cong, xprt->cwnd);
400 if (RPCXPRT_CONGESTED(xprt))
401 return 0;
402 req->rq_cong = 1;
403 xprt->cong += RPC_CWNDSCALE;
404 return 1;
405}
406
407/*
408 * Adjust the congestion window, and wake up the next task
409 * that has been sleeping due to congestion
410 */
411static void
412__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
413{
414 if (!req->rq_cong)
415 return;
416 req->rq_cong = 0;
417 xprt->cong -= RPC_CWNDSCALE;
49e9a890 418 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
419}
420
a58dd398
CL
421/**
422 * xprt_release_rqst_cong - housekeeping when request is complete
423 * @task: RPC request that recently completed
424 *
425 * Useful for transports that require congestion control.
426 */
427void xprt_release_rqst_cong(struct rpc_task *task)
428{
429 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
430}
12444809 431EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
a58dd398 432
46c0ee8b
CL
433/**
434 * xprt_adjust_cwnd - adjust transport congestion window
435 * @task: recently completed RPC request used to adjust window
436 * @result: result code of completed RPC request
437 *
1da177e4
LT
438 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
439 */
46c0ee8b 440void xprt_adjust_cwnd(struct rpc_task *task, int result)
1da177e4 441{
46c0ee8b
CL
442 struct rpc_rqst *req = task->tk_rqstp;
443 struct rpc_xprt *xprt = task->tk_xprt;
444 unsigned long cwnd = xprt->cwnd;
1da177e4 445
1da177e4
LT
446 if (result >= 0 && cwnd <= xprt->cong) {
447 /* The (cwnd >> 1) term makes sure
448 * the result gets rounded properly. */
449 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
450 if (cwnd > RPC_MAXCWND(xprt))
451 cwnd = RPC_MAXCWND(xprt);
49e9a890 452 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
453 } else if (result == -ETIMEDOUT) {
454 cwnd >>= 1;
455 if (cwnd < RPC_CWNDSCALE)
456 cwnd = RPC_CWNDSCALE;
457 }
46121cf7 458 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
1da177e4
LT
459 xprt->cong, xprt->cwnd, cwnd);
460 xprt->cwnd = cwnd;
46c0ee8b 461 __xprt_put_cong(xprt, req);
1da177e4 462}
12444809 463EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
1da177e4 464
44fbac22
CL
465/**
466 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
467 * @xprt: transport with waiting tasks
468 * @status: result code to plant in each task before waking it
469 *
470 */
471void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
472{
473 if (status < 0)
474 rpc_wake_up_status(&xprt->pending, status);
475 else
476 rpc_wake_up(&xprt->pending);
477}
12444809 478EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
44fbac22 479
c7b2cae8
CL
480/**
481 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
482 * @task: task to be put to sleep
0b80ae42 483 * @action: function pointer to be executed after wait
c7b2cae8 484 */
b6ddf64f 485void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
c7b2cae8
CL
486{
487 struct rpc_rqst *req = task->tk_rqstp;
488 struct rpc_xprt *xprt = req->rq_xprt;
489
490 task->tk_timeout = req->rq_timeout;
b6ddf64f 491 rpc_sleep_on(&xprt->pending, task, action);
c7b2cae8 492}
12444809 493EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
c7b2cae8
CL
494
495/**
496 * xprt_write_space - wake the task waiting for transport output buffer space
497 * @xprt: transport with waiting tasks
498 *
499 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
500 */
501void xprt_write_space(struct rpc_xprt *xprt)
502{
503 if (unlikely(xprt->shutdown))
504 return;
505
506 spin_lock_bh(&xprt->transport_lock);
507 if (xprt->snd_task) {
46121cf7
CL
508 dprintk("RPC: write space: waking waiting task on "
509 "xprt %p\n", xprt);
fda13939 510 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
c7b2cae8
CL
511 }
512 spin_unlock_bh(&xprt->transport_lock);
513}
12444809 514EXPORT_SYMBOL_GPL(xprt_write_space);
c7b2cae8 515
fe3aca29
CL
516/**
517 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
518 * @task: task whose timeout is to be set
519 *
520 * Set a request's retransmit timeout based on the transport's
521 * default timeout parameters. Used by transports that don't adjust
522 * the retransmit timeout based on round-trip time estimation.
523 */
524void xprt_set_retrans_timeout_def(struct rpc_task *task)
525{
526 task->tk_timeout = task->tk_rqstp->rq_timeout;
527}
12444809 528EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
fe3aca29
CL
529
530/*
531 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
532 * @task: task whose timeout is to be set
cca5172a 533 *
fe3aca29
CL
534 * Set a request's retransmit timeout using the RTT estimator.
535 */
536void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
537{
538 int timer = task->tk_msg.rpc_proc->p_timer;
ba7392bb
TM
539 struct rpc_clnt *clnt = task->tk_client;
540 struct rpc_rtt *rtt = clnt->cl_rtt;
fe3aca29 541 struct rpc_rqst *req = task->tk_rqstp;
ba7392bb 542 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
fe3aca29
CL
543
544 task->tk_timeout = rpc_calc_rto(rtt, timer);
545 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
546 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
547 task->tk_timeout = max_timeout;
548}
12444809 549EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
fe3aca29 550
1da177e4
LT
551static void xprt_reset_majortimeo(struct rpc_rqst *req)
552{
ba7392bb 553 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
554
555 req->rq_majortimeo = req->rq_timeout;
556 if (to->to_exponential)
557 req->rq_majortimeo <<= to->to_retries;
558 else
559 req->rq_majortimeo += to->to_increment * to->to_retries;
560 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
561 req->rq_majortimeo = to->to_maxval;
562 req->rq_majortimeo += jiffies;
563}
564
9903cd1c
CL
565/**
566 * xprt_adjust_timeout - adjust timeout values for next retransmit
567 * @req: RPC request containing parameters to use for the adjustment
568 *
1da177e4
LT
569 */
570int xprt_adjust_timeout(struct rpc_rqst *req)
571{
572 struct rpc_xprt *xprt = req->rq_xprt;
ba7392bb 573 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
574 int status = 0;
575
576 if (time_before(jiffies, req->rq_majortimeo)) {
577 if (to->to_exponential)
578 req->rq_timeout <<= 1;
579 else
580 req->rq_timeout += to->to_increment;
581 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
582 req->rq_timeout = to->to_maxval;
583 req->rq_retries++;
1da177e4
LT
584 } else {
585 req->rq_timeout = to->to_initval;
586 req->rq_retries = 0;
587 xprt_reset_majortimeo(req);
588 /* Reset the RTT counters == "slow start" */
4a0f8c04 589 spin_lock_bh(&xprt->transport_lock);
1da177e4 590 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
4a0f8c04 591 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
592 status = -ETIMEDOUT;
593 }
594
595 if (req->rq_timeout == 0) {
596 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
597 req->rq_timeout = 5 * HZ;
598 }
599 return status;
600}
601
65f27f38 602static void xprt_autoclose(struct work_struct *work)
1da177e4 603{
65f27f38
DH
604 struct rpc_xprt *xprt =
605 container_of(work, struct rpc_xprt, task_cleanup);
1da177e4 606
a246b010 607 xprt->ops->close(xprt);
66af1e55 608 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1da177e4
LT
609 xprt_release_write(xprt, NULL);
610}
611
9903cd1c 612/**
62da3b24 613 * xprt_disconnect_done - mark a transport as disconnected
9903cd1c
CL
614 * @xprt: transport to flag for disconnect
615 *
1da177e4 616 */
62da3b24 617void xprt_disconnect_done(struct rpc_xprt *xprt)
1da177e4 618{
46121cf7 619 dprintk("RPC: disconnected transport %p\n", xprt);
4a0f8c04 620 spin_lock_bh(&xprt->transport_lock);
1da177e4 621 xprt_clear_connected(xprt);
2a491991 622 xprt_wake_pending_tasks(xprt, -EAGAIN);
4a0f8c04 623 spin_unlock_bh(&xprt->transport_lock);
1da177e4 624}
62da3b24 625EXPORT_SYMBOL_GPL(xprt_disconnect_done);
1da177e4 626
66af1e55
TM
627/**
628 * xprt_force_disconnect - force a transport to disconnect
629 * @xprt: transport to disconnect
630 *
631 */
632void xprt_force_disconnect(struct rpc_xprt *xprt)
633{
634 /* Don't race with the test_bit() in xprt_clear_locked() */
635 spin_lock_bh(&xprt->transport_lock);
636 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
637 /* Try to schedule an autoclose RPC call */
638 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
639 queue_work(rpciod_workqueue, &xprt->task_cleanup);
2a491991 640 xprt_wake_pending_tasks(xprt, -EAGAIN);
66af1e55
TM
641 spin_unlock_bh(&xprt->transport_lock);
642}
66af1e55 643
7c1d71cf
TM
644/**
645 * xprt_conditional_disconnect - force a transport to disconnect
646 * @xprt: transport to disconnect
647 * @cookie: 'connection cookie'
648 *
649 * This attempts to break the connection if and only if 'cookie' matches
650 * the current transport 'connection cookie'. It ensures that we don't
651 * try to break the connection more than once when we need to retransmit
652 * a batch of RPC requests.
653 *
654 */
655void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
656{
657 /* Don't race with the test_bit() in xprt_clear_locked() */
658 spin_lock_bh(&xprt->transport_lock);
659 if (cookie != xprt->connect_cookie)
660 goto out;
661 if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
662 goto out;
663 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
664 /* Try to schedule an autoclose RPC call */
665 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
666 queue_work(rpciod_workqueue, &xprt->task_cleanup);
2a491991 667 xprt_wake_pending_tasks(xprt, -EAGAIN);
7c1d71cf
TM
668out:
669 spin_unlock_bh(&xprt->transport_lock);
670}
671
1da177e4
LT
672static void
673xprt_init_autodisconnect(unsigned long data)
674{
675 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
676
4a0f8c04 677 spin_lock(&xprt->transport_lock);
1da177e4
LT
678 if (!list_empty(&xprt->recv) || xprt->shutdown)
679 goto out_abort;
2226feb6 680 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 681 goto out_abort;
4a0f8c04 682 spin_unlock(&xprt->transport_lock);
f75e6745
TM
683 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
684 queue_work(rpciod_workqueue, &xprt->task_cleanup);
1da177e4
LT
685 return;
686out_abort:
4a0f8c04 687 spin_unlock(&xprt->transport_lock);
1da177e4
LT
688}
689
9903cd1c
CL
690/**
691 * xprt_connect - schedule a transport connect operation
692 * @task: RPC task that is requesting the connect
1da177e4
LT
693 *
694 */
695void xprt_connect(struct rpc_task *task)
696{
697 struct rpc_xprt *xprt = task->tk_xprt;
698
46121cf7 699 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
1da177e4
LT
700 xprt, (xprt_connected(xprt) ? "is" : "is not"));
701
ec739ef0 702 if (!xprt_bound(xprt)) {
01d37c42 703 task->tk_status = -EAGAIN;
1da177e4
LT
704 return;
705 }
706 if (!xprt_lock_write(xprt, task))
707 return;
feb8ca37
TM
708
709 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
710 xprt->ops->close(xprt);
711
1da177e4 712 if (xprt_connected(xprt))
a246b010
CL
713 xprt_release_write(xprt, task);
714 else {
715 if (task->tk_rqstp)
716 task->tk_rqstp->rq_bytes_sent = 0;
1da177e4 717
a8ce4a8f 718 task->tk_timeout = task->tk_rqstp->rq_timeout;
5d00837b 719 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
0b9e7943
TM
720
721 if (test_bit(XPRT_CLOSING, &xprt->state))
722 return;
723 if (xprt_test_and_set_connecting(xprt))
724 return;
262ca07d 725 xprt->stat.connect_start = jiffies;
a246b010 726 xprt->ops->connect(task);
1da177e4 727 }
1da177e4
LT
728}
729
9903cd1c 730static void xprt_connect_status(struct rpc_task *task)
1da177e4
LT
731{
732 struct rpc_xprt *xprt = task->tk_xprt;
733
cd983ef8 734 if (task->tk_status == 0) {
262ca07d
CL
735 xprt->stat.connect_count++;
736 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
46121cf7 737 dprintk("RPC: %5u xprt_connect_status: connection established\n",
1da177e4
LT
738 task->tk_pid);
739 return;
740 }
741
1da177e4 742 switch (task->tk_status) {
2a491991
TM
743 case -EAGAIN:
744 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
23475d66 745 break;
1da177e4 746 case -ETIMEDOUT:
46121cf7
CL
747 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
748 "out\n", task->tk_pid);
1da177e4
LT
749 break;
750 default:
46121cf7
CL
751 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
752 "server %s\n", task->tk_pid, -task->tk_status,
753 task->tk_client->cl_server);
23475d66
CL
754 xprt_release_write(xprt, task);
755 task->tk_status = -EIO;
1da177e4 756 }
1da177e4
LT
757}
758
9903cd1c
CL
759/**
760 * xprt_lookup_rqst - find an RPC request corresponding to an XID
761 * @xprt: transport on which the original request was transmitted
762 * @xid: RPC XID of incoming reply
763 *
1da177e4 764 */
d8ed029d 765struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1da177e4 766{
8f3a6de3 767 struct rpc_rqst *entry;
1da177e4 768
8f3a6de3 769 list_for_each_entry(entry, &xprt->recv, rq_list)
262ca07d
CL
770 if (entry->rq_xid == xid)
771 return entry;
46121cf7
CL
772
773 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
774 ntohl(xid));
262ca07d
CL
775 xprt->stat.bad_xids++;
776 return NULL;
1da177e4 777}
12444809 778EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1da177e4 779
bbc72cea 780static void xprt_update_rtt(struct rpc_task *task)
1570c1e4
CL
781{
782 struct rpc_rqst *req = task->tk_rqstp;
783 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
784 unsigned timer = task->tk_msg.rpc_proc->p_timer;
d60dbb20 785 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1570c1e4
CL
786
787 if (timer) {
788 if (req->rq_ntrans == 1)
ff839970 789 rpc_update_rtt(rtt, timer, m);
1570c1e4
CL
790 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
791 }
792}
793
9903cd1c
CL
794/**
795 * xprt_complete_rqst - called when reply processing is complete
1570c1e4 796 * @task: RPC request that recently completed
9903cd1c
CL
797 * @copied: actual number of bytes received from the transport
798 *
1570c1e4 799 * Caller holds transport lock.
1da177e4 800 */
1570c1e4 801void xprt_complete_rqst(struct rpc_task *task, int copied)
1da177e4 802{
1570c1e4 803 struct rpc_rqst *req = task->tk_rqstp;
fda13939 804 struct rpc_xprt *xprt = req->rq_xprt;
1da177e4 805
1570c1e4
CL
806 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
807 task->tk_pid, ntohl(req->rq_xid), copied);
1da177e4 808
fda13939 809 xprt->stat.recvs++;
d60dbb20 810 req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
bbc72cea
CL
811 if (xprt->ops->timer != NULL)
812 xprt_update_rtt(task);
ef759a2e 813
1da177e4 814 list_del_init(&req->rq_list);
1e799b67 815 req->rq_private_buf.len = copied;
dd2b63d0
RL
816 /* Ensure all writes are done before we update */
817 /* req->rq_reply_bytes_recvd */
43ac3f29 818 smp_wmb();
dd2b63d0 819 req->rq_reply_bytes_recvd = copied;
fda13939 820 rpc_wake_up_queued_task(&xprt->pending, task);
1da177e4 821}
12444809 822EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1da177e4 823
46c0ee8b 824static void xprt_timer(struct rpc_task *task)
1da177e4 825{
46c0ee8b 826 struct rpc_rqst *req = task->tk_rqstp;
1da177e4
LT
827 struct rpc_xprt *xprt = req->rq_xprt;
828
5d00837b
TM
829 if (task->tk_status != -ETIMEDOUT)
830 return;
46121cf7 831 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
1da177e4 832
5d00837b 833 spin_lock_bh(&xprt->transport_lock);
dd2b63d0 834 if (!req->rq_reply_bytes_recvd) {
46c0ee8b
CL
835 if (xprt->ops->timer)
836 xprt->ops->timer(task);
5d00837b
TM
837 } else
838 task->tk_status = 0;
839 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
840}
841
4cfc7e60
RI
842static inline int xprt_has_timer(struct rpc_xprt *xprt)
843{
844 return xprt->idle_timeout != 0;
845}
846
9903cd1c
CL
847/**
848 * xprt_prepare_transmit - reserve the transport before sending a request
849 * @task: RPC task about to send a request
850 *
1da177e4 851 */
9903cd1c 852int xprt_prepare_transmit(struct rpc_task *task)
1da177e4
LT
853{
854 struct rpc_rqst *req = task->tk_rqstp;
855 struct rpc_xprt *xprt = req->rq_xprt;
856 int err = 0;
857
46121cf7 858 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1da177e4 859
4a0f8c04 860 spin_lock_bh(&xprt->transport_lock);
dd2b63d0
RL
861 if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
862 err = req->rq_reply_bytes_recvd;
1da177e4
LT
863 goto out_unlock;
864 }
43cedbf0 865 if (!xprt->ops->reserve_xprt(xprt, task))
1da177e4 866 err = -EAGAIN;
1da177e4 867out_unlock:
4a0f8c04 868 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
869 return err;
870}
871
e0ab53de 872void xprt_end_transmit(struct rpc_task *task)
5e5ce5be 873{
343952fa 874 xprt_release_write(task->tk_rqstp->rq_xprt, task);
5e5ce5be
TM
875}
876
9903cd1c
CL
877/**
878 * xprt_transmit - send an RPC request on a transport
879 * @task: controlling RPC task
880 *
881 * We have to copy the iovec because sendmsg fiddles with its contents.
882 */
883void xprt_transmit(struct rpc_task *task)
1da177e4 884{
1da177e4
LT
885 struct rpc_rqst *req = task->tk_rqstp;
886 struct rpc_xprt *xprt = req->rq_xprt;
a246b010 887 int status;
1da177e4 888
46121cf7 889 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1da177e4 890
dd2b63d0 891 if (!req->rq_reply_bytes_recvd) {
55ae1aab
RL
892 if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
893 /*
894 * Add to the list only if we're expecting a reply
895 */
4a0f8c04 896 spin_lock_bh(&xprt->transport_lock);
1da177e4
LT
897 /* Update the softirq receive buffer */
898 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
899 sizeof(req->rq_private_buf));
900 /* Add request to the receive list */
901 list_add_tail(&req->rq_list, &xprt->recv);
4a0f8c04 902 spin_unlock_bh(&xprt->transport_lock);
1da177e4 903 xprt_reset_majortimeo(req);
0f9dc2b1
TM
904 /* Turn off autodisconnect */
905 del_singleshot_timer_sync(&xprt->timer);
1da177e4
LT
906 }
907 } else if (!req->rq_bytes_sent)
908 return;
909
7c1d71cf 910 req->rq_connect_cookie = xprt->connect_cookie;
ff839970 911 req->rq_xtime = ktime_get();
a246b010 912 status = xprt->ops->send_request(task);
c8485e4d
TM
913 if (status != 0) {
914 task->tk_status = status;
915 return;
916 }
262ca07d 917
c8485e4d 918 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
468f8613 919 task->tk_flags |= RPC_TASK_SENT;
c8485e4d 920 spin_lock_bh(&xprt->transport_lock);
262ca07d 921
c8485e4d 922 xprt->ops->set_retrans_timeout(task);
262ca07d 923
c8485e4d
TM
924 xprt->stat.sends++;
925 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
926 xprt->stat.bklog_u += xprt->backlog.qlen;
1da177e4 927
c8485e4d
TM
928 /* Don't race with disconnect */
929 if (!xprt_connected(xprt))
930 task->tk_status = -ENOTCONN;
dd2b63d0 931 else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
55ae1aab
RL
932 /*
933 * Sleep on the pending queue since
934 * we're expecting a reply.
935 */
c8485e4d 936 rpc_sleep_on(&xprt->pending, task, xprt_timer);
55ae1aab 937 }
c8485e4d 938 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
939}
940
d9ba131d
TM
941static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
942{
943 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
944
945 if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
946 goto out;
947 req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
948 if (req != NULL)
949 goto out;
950 atomic_dec(&xprt->num_reqs);
951 req = ERR_PTR(-ENOMEM);
952out:
953 return req;
954}
955
956static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
957{
958 if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
959 kfree(req);
960 return true;
961 }
962 return false;
963}
964
ee5ebe85 965static void xprt_alloc_slot(struct rpc_task *task)
1da177e4
LT
966{
967 struct rpc_xprt *xprt = task->tk_xprt;
d9ba131d 968 struct rpc_rqst *req;
1da177e4 969
1da177e4 970 if (!list_empty(&xprt->free)) {
d9ba131d
TM
971 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
972 list_del(&req->rq_list);
973 goto out_init_req;
974 }
975 req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT);
976 if (!IS_ERR(req))
977 goto out_init_req;
978 switch (PTR_ERR(req)) {
979 case -ENOMEM:
980 rpc_delay(task, HZ >> 2);
981 dprintk("RPC: dynamic allocation of request slot "
982 "failed! Retrying\n");
983 break;
984 case -EAGAIN:
985 rpc_sleep_on(&xprt->backlog, task, NULL);
986 dprintk("RPC: waiting for request slot\n");
1da177e4 987 }
1da177e4 988 task->tk_status = -EAGAIN;
d9ba131d
TM
989 return;
990out_init_req:
991 task->tk_status = 0;
992 task->tk_rqstp = req;
993 xprt_request_init(task, xprt);
1da177e4
LT
994}
995
ee5ebe85
TM
996static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
997{
d9ba131d
TM
998 if (xprt_dynamic_free_slot(xprt, req))
999 return;
1000
ee5ebe85
TM
1001 memset(req, 0, sizeof(*req)); /* mark unused */
1002
1003 spin_lock(&xprt->reserve_lock);
1004 list_add(&req->rq_list, &xprt->free);
1005 rpc_wake_up_next(&xprt->backlog);
1006 spin_unlock(&xprt->reserve_lock);
1007}
1008
21de0a95
TM
1009static void xprt_free_all_slots(struct rpc_xprt *xprt)
1010{
1011 struct rpc_rqst *req;
1012 while (!list_empty(&xprt->free)) {
1013 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1014 list_del(&req->rq_list);
1015 kfree(req);
1016 }
1017}
1018
d9ba131d
TM
1019struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1020 unsigned int num_prealloc,
1021 unsigned int max_alloc)
bd1722d4
PE
1022{
1023 struct rpc_xprt *xprt;
21de0a95
TM
1024 struct rpc_rqst *req;
1025 int i;
bd1722d4
PE
1026
1027 xprt = kzalloc(size, GFP_KERNEL);
1028 if (xprt == NULL)
1029 goto out;
1030
21de0a95
TM
1031 xprt_init(xprt, net);
1032
1033 for (i = 0; i < num_prealloc; i++) {
1034 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1035 if (!req)
1036 break;
1037 list_add(&req->rq_list, &xprt->free);
1038 }
1039 if (i < num_prealloc)
bd1722d4 1040 goto out_free;
d9ba131d
TM
1041 if (max_alloc > num_prealloc)
1042 xprt->max_reqs = max_alloc;
1043 else
1044 xprt->max_reqs = num_prealloc;
1045 xprt->min_reqs = num_prealloc;
1046 atomic_set(&xprt->num_reqs, num_prealloc);
bd1722d4
PE
1047
1048 return xprt;
1049
1050out_free:
21de0a95 1051 xprt_free(xprt);
bd1722d4
PE
1052out:
1053 return NULL;
1054}
1055EXPORT_SYMBOL_GPL(xprt_alloc);
1056
e204e621
PE
1057void xprt_free(struct rpc_xprt *xprt)
1058{
37aa2133 1059 put_net(xprt->xprt_net);
21de0a95 1060 xprt_free_all_slots(xprt);
e204e621
PE
1061 kfree(xprt);
1062}
1063EXPORT_SYMBOL_GPL(xprt_free);
1064
9903cd1c
CL
1065/**
1066 * xprt_reserve - allocate an RPC request slot
1067 * @task: RPC task requesting a slot allocation
1068 *
1069 * If no more slots are available, place the task on the transport's
1070 * backlog queue.
1071 */
1072void xprt_reserve(struct rpc_task *task)
1da177e4
LT
1073{
1074 struct rpc_xprt *xprt = task->tk_xprt;
1075
43cedbf0
TM
1076 task->tk_status = 0;
1077 if (task->tk_rqstp != NULL)
1078 return;
1079
1080 /* Note: grabbing the xprt_lock_write() here is not strictly needed,
1081 * but ensures that we throttle new slot allocation if the transport
1082 * is congested (e.g. if reconnecting or if we're out of socket
1083 * write buffer space).
1084 */
1085 task->tk_timeout = 0;
1086 task->tk_status = -EAGAIN;
1087 if (!xprt_lock_write(xprt, task))
1088 return;
1089
0065db32 1090 spin_lock(&xprt->reserve_lock);
ee5ebe85 1091 xprt_alloc_slot(task);
0065db32 1092 spin_unlock(&xprt->reserve_lock);
43cedbf0 1093 xprt_release_write(xprt, task);
1da177e4
LT
1094}
1095
d8ed029d 1096static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
1da177e4 1097{
0eae88f3 1098 return (__force __be32)xprt->xid++;
1da177e4
LT
1099}
1100
1101static inline void xprt_init_xid(struct rpc_xprt *xprt)
1102{
bf3fcf89 1103 xprt->xid = net_random();
1da177e4
LT
1104}
1105
9903cd1c 1106static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
1da177e4
LT
1107{
1108 struct rpc_rqst *req = task->tk_rqstp;
1109
d9ba131d 1110 INIT_LIST_HEAD(&req->rq_list);
ba7392bb 1111 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1da177e4
LT
1112 req->rq_task = task;
1113 req->rq_xprt = xprt;
02107148 1114 req->rq_buffer = NULL;
1da177e4 1115 req->rq_xid = xprt_alloc_xid(xprt);
ead5e1c2 1116 req->rq_release_snd_buf = NULL;
da45828e 1117 xprt_reset_majortimeo(req);
46121cf7 1118 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1da177e4
LT
1119 req, ntohl(req->rq_xid));
1120}
1121
9903cd1c
CL
1122/**
1123 * xprt_release - release an RPC request slot
1124 * @task: task which is finished with the slot
1125 *
1da177e4 1126 */
9903cd1c 1127void xprt_release(struct rpc_task *task)
1da177e4 1128{
55ae1aab 1129 struct rpc_xprt *xprt;
1da177e4
LT
1130 struct rpc_rqst *req;
1131
1132 if (!(req = task->tk_rqstp))
1133 return;
55ae1aab 1134
55ae1aab 1135 xprt = req->rq_xprt;
11c556b3 1136 rpc_count_iostats(task);
4a0f8c04 1137 spin_lock_bh(&xprt->transport_lock);
49e9a890 1138 xprt->ops->release_xprt(xprt, task);
a58dd398
CL
1139 if (xprt->ops->release_request)
1140 xprt->ops->release_request(task);
1da177e4
LT
1141 if (!list_empty(&req->rq_list))
1142 list_del(&req->rq_list);
1143 xprt->last_used = jiffies;
4cfc7e60 1144 if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
a246b010 1145 mod_timer(&xprt->timer,
03bf4b70 1146 xprt->last_used + xprt->idle_timeout);
4a0f8c04 1147 spin_unlock_bh(&xprt->transport_lock);
ee5ebe85 1148 if (req->rq_buffer)
55ae1aab 1149 xprt->ops->buf_free(req->rq_buffer);
a17c2153
TM
1150 if (req->rq_cred != NULL)
1151 put_rpccred(req->rq_cred);
1da177e4 1152 task->tk_rqstp = NULL;
ead5e1c2
BF
1153 if (req->rq_release_snd_buf)
1154 req->rq_release_snd_buf(req);
55ae1aab 1155
46121cf7 1156 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
ee5ebe85
TM
1157 if (likely(!bc_prealloc(req)))
1158 xprt_free_slot(xprt, req);
1159 else
c9acb42e 1160 xprt_free_bc_request(req);
1da177e4
LT
1161}
1162
21de0a95 1163static void xprt_init(struct rpc_xprt *xprt, struct net *net)
c2866763 1164{
21de0a95 1165 atomic_set(&xprt->count, 1);
c2866763
CL
1166
1167 spin_lock_init(&xprt->transport_lock);
1168 spin_lock_init(&xprt->reserve_lock);
1169
1170 INIT_LIST_HEAD(&xprt->free);
1171 INIT_LIST_HEAD(&xprt->recv);
9e00abc3 1172#if defined(CONFIG_SUNRPC_BACKCHANNEL)
f9acac1a
RL
1173 spin_lock_init(&xprt->bc_pa_lock);
1174 INIT_LIST_HEAD(&xprt->bc_pa_list);
9e00abc3 1175#endif /* CONFIG_SUNRPC_BACKCHANNEL */
f9acac1a 1176
c2866763
CL
1177 xprt->last_used = jiffies;
1178 xprt->cwnd = RPC_INITCWND;
a509050b 1179 xprt->bind_index = 0;
c2866763
CL
1180
1181 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1182 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
34006cee 1183 rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
c2866763
CL
1184 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1185
c2866763
CL
1186 xprt_init_xid(xprt);
1187
21de0a95 1188 xprt->xprt_net = get_net(net);
8d9266ff
TM
1189}
1190
1191/**
1192 * xprt_create_transport - create an RPC transport
1193 * @args: rpc transport creation arguments
1194 *
1195 */
1196struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1197{
1198 struct rpc_xprt *xprt;
1199 struct xprt_class *t;
1200
1201 spin_lock(&xprt_list_lock);
1202 list_for_each_entry(t, &xprt_list, list) {
1203 if (t->ident == args->ident) {
1204 spin_unlock(&xprt_list_lock);
1205 goto found;
1206 }
1207 }
1208 spin_unlock(&xprt_list_lock);
1209 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
1210 return ERR_PTR(-EIO);
1211
1212found:
1213 xprt = t->setup(args);
1214 if (IS_ERR(xprt)) {
1215 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1216 -PTR_ERR(xprt));
21de0a95 1217 goto out;
8d9266ff 1218 }
21de0a95
TM
1219 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1220 if (xprt_has_timer(xprt))
1221 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1222 (unsigned long)xprt);
1223 else
1224 init_timer(&xprt->timer);
46121cf7 1225 dprintk("RPC: created transport %p with %u slots\n", xprt,
c2866763 1226 xprt->max_reqs);
21de0a95 1227out:
c2866763
CL
1228 return xprt;
1229}
1230
9903cd1c
CL
1231/**
1232 * xprt_destroy - destroy an RPC transport, killing off all requests.
a8de240a 1233 * @xprt: transport to destroy
9903cd1c 1234 *
1da177e4 1235 */
a8de240a 1236static void xprt_destroy(struct rpc_xprt *xprt)
1da177e4 1237{
46121cf7 1238 dprintk("RPC: destroying transport %p\n", xprt);
0065db32
TM
1239 xprt->shutdown = 1;
1240 del_timer_sync(&xprt->timer);
c8541ecd 1241
f6a1cc89
TM
1242 rpc_destroy_wait_queue(&xprt->binding);
1243 rpc_destroy_wait_queue(&xprt->pending);
1244 rpc_destroy_wait_queue(&xprt->sending);
f6a1cc89 1245 rpc_destroy_wait_queue(&xprt->backlog);
c3ae62ae 1246 cancel_work_sync(&xprt->task_cleanup);
c8541ecd
CL
1247 /*
1248 * Tear down transport state and free the rpc_xprt
1249 */
a246b010 1250 xprt->ops->destroy(xprt);
6b6ca86b 1251}
1da177e4 1252
6b6ca86b
TM
1253/**
1254 * xprt_put - release a reference to an RPC transport.
1255 * @xprt: pointer to the transport
1256 *
1257 */
1258void xprt_put(struct rpc_xprt *xprt)
1259{
a8de240a
TM
1260 if (atomic_dec_and_test(&xprt->count))
1261 xprt_destroy(xprt);
6b6ca86b
TM
1262}
1263
1264/**
1265 * xprt_get - return a reference to an RPC transport.
1266 * @xprt: pointer to the transport
1267 *
1268 */
1269struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1270{
a8de240a
TM
1271 if (atomic_inc_not_zero(&xprt->count))
1272 return xprt;
1273 return NULL;
1da177e4 1274}
This page took 0.636618 seconds and 5 git commands to generate.