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