]> Git Repo - linux.git/blame - net/sunrpc/xprt.c
SUNRPC: Make "no retrans timeout" soft tasks behave like softconn for timeouts
[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 <[email protected]>
55aa4f58
CL
36 *
37 * Transport switch API copyright (C) 2005, Chuck Lever <[email protected]>
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>
fda1bfef 51#include <linux/rcupdate.h>
a1231fda 52#include <linux/sched/mm.h>
1da177e4 53
3705ad64
JL
54#include <trace/events/sunrpc.h>
55
55ae1aab
RL
56#include "sunrpc.h"
57
1da177e4
LT
58/*
59 * Local variables
60 */
61
f895b252 62#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
1da177e4
LT
63# define RPCDBG_FACILITY RPCDBG_XPRT
64#endif
65
1da177e4
LT
66/*
67 * Local functions
68 */
21de0a95 69static void xprt_init(struct rpc_xprt *xprt, struct net *net);
37ac86c3 70static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
4e0038b6 71static void xprt_destroy(struct rpc_xprt *xprt);
1da177e4 72
5ba03e82 73static DEFINE_SPINLOCK(xprt_list_lock);
81c098af
TT
74static LIST_HEAD(xprt_list);
75
9e910bff
TM
76static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
77{
78 unsigned long timeout = jiffies + req->rq_timeout;
79
80 if (time_before(timeout, req->rq_majortimeo))
81 return timeout;
82 return req->rq_majortimeo;
83}
84
81c098af
TT
85/**
86 * xprt_register_transport - register a transport implementation
87 * @transport: transport to register
88 *
89 * If a transport implementation is loaded as a kernel module, it can
90 * call this interface to make itself known to the RPC client.
91 *
92 * Returns:
93 * 0: transport successfully registered
94 * -EEXIST: transport already registered
95 * -EINVAL: transport module being unloaded
96 */
97int xprt_register_transport(struct xprt_class *transport)
98{
99 struct xprt_class *t;
100 int result;
101
102 result = -EEXIST;
103 spin_lock(&xprt_list_lock);
104 list_for_each_entry(t, &xprt_list, list) {
105 /* don't register the same transport class twice */
4fa016eb 106 if (t->ident == transport->ident)
81c098af
TT
107 goto out;
108 }
109
c9f6cde6
DL
110 list_add_tail(&transport->list, &xprt_list);
111 printk(KERN_INFO "RPC: Registered %s transport module.\n",
112 transport->name);
113 result = 0;
81c098af
TT
114
115out:
116 spin_unlock(&xprt_list_lock);
117 return result;
118}
119EXPORT_SYMBOL_GPL(xprt_register_transport);
120
121/**
122 * xprt_unregister_transport - unregister a transport implementation
65b6e42c 123 * @transport: transport to unregister
81c098af
TT
124 *
125 * Returns:
126 * 0: transport successfully unregistered
127 * -ENOENT: transport never registered
128 */
129int xprt_unregister_transport(struct xprt_class *transport)
130{
131 struct xprt_class *t;
132 int result;
133
134 result = 0;
135 spin_lock(&xprt_list_lock);
136 list_for_each_entry(t, &xprt_list, list) {
137 if (t == transport) {
138 printk(KERN_INFO
139 "RPC: Unregistered %s transport module.\n",
140 transport->name);
141 list_del_init(&transport->list);
81c098af
TT
142 goto out;
143 }
144 }
145 result = -ENOENT;
146
147out:
148 spin_unlock(&xprt_list_lock);
149 return result;
150}
151EXPORT_SYMBOL_GPL(xprt_unregister_transport);
152
441e3e24
TT
153/**
154 * xprt_load_transport - load a transport implementation
155 * @transport_name: transport to load
156 *
157 * Returns:
158 * 0: transport successfully loaded
159 * -ENOENT: transport module not available
160 */
161int xprt_load_transport(const char *transport_name)
162{
163 struct xprt_class *t;
441e3e24
TT
164 int result;
165
166 result = 0;
167 spin_lock(&xprt_list_lock);
168 list_for_each_entry(t, &xprt_list, list) {
169 if (strcmp(t->name, transport_name) == 0) {
170 spin_unlock(&xprt_list_lock);
171 goto out;
172 }
173 }
174 spin_unlock(&xprt_list_lock);
ef7ffe8f 175 result = request_module("xprt%s", transport_name);
441e3e24
TT
176out:
177 return result;
178}
179EXPORT_SYMBOL_GPL(xprt_load_transport);
180
c544577d
TM
181static void xprt_clear_locked(struct rpc_xprt *xprt)
182{
183 xprt->snd_task = NULL;
184 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
185 smp_mb__before_atomic();
186 clear_bit(XPRT_LOCKED, &xprt->state);
187 smp_mb__after_atomic();
188 } else
189 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
190}
191
12a80469
CL
192/**
193 * xprt_reserve_xprt - serialize write access to transports
194 * @task: task that is requesting access to the transport
177c27bf 195 * @xprt: pointer to the target transport
12a80469
CL
196 *
197 * This prevents mixing the payload of separate requests, and prevents
198 * transport connects from colliding with writes. No congestion control
199 * is provided.
200 */
43cedbf0 201int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
12a80469 202{
12a80469
CL
203 struct rpc_rqst *req = task->tk_rqstp;
204
205 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
206 if (task == xprt->snd_task)
207 return 1;
12a80469
CL
208 goto out_sleep;
209 }
c544577d
TM
210 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
211 goto out_unlock;
12a80469 212 xprt->snd_task = task;
4d4a76f3 213
12a80469
CL
214 return 1;
215
c544577d
TM
216out_unlock:
217 xprt_clear_locked(xprt);
12a80469 218out_sleep:
46121cf7 219 dprintk("RPC: %5u failed to lock transport %p\n",
12a80469 220 task->tk_pid, xprt);
12a80469 221 task->tk_status = -EAGAIN;
6b2e6856
TM
222 if (RPC_IS_SOFT(task))
223 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
9e910bff 224 xprt_request_timeout(req));
6b2e6856
TM
225 else
226 rpc_sleep_on(&xprt->sending, task, NULL);
12a80469
CL
227 return 0;
228}
12444809 229EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
12a80469 230
75891f50
TM
231static bool
232xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
233{
234 return test_bit(XPRT_CWND_WAIT, &xprt->state);
235}
236
237static void
238xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
239{
240 if (!list_empty(&xprt->xmit_queue)) {
241 /* Peek at head of queue to see if it can make progress */
242 if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
243 rq_xmit)->rq_cong)
244 return;
245 }
246 set_bit(XPRT_CWND_WAIT, &xprt->state);
247}
248
249static void
250xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
251{
252 if (!RPCXPRT_CONGESTED(xprt))
253 clear_bit(XPRT_CWND_WAIT, &xprt->state);
254}
255
1da177e4 256/*
12a80469
CL
257 * xprt_reserve_xprt_cong - serialize write access to transports
258 * @task: task that is requesting access to the transport
259 *
260 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
261 * integrated into the decision of whether a request is allowed to be
262 * woken up and given access to the transport.
75891f50 263 * Note that the lock is only granted if we know there are free slots.
1da177e4 264 */
43cedbf0 265int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
266{
267 struct rpc_rqst *req = task->tk_rqstp;
268
2226feb6 269 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
1da177e4
LT
270 if (task == xprt->snd_task)
271 return 1;
1da177e4
LT
272 goto out_sleep;
273 }
43cedbf0
TM
274 if (req == NULL) {
275 xprt->snd_task = task;
276 return 1;
277 }
c544577d
TM
278 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
279 goto out_unlock;
75891f50 280 if (!xprt_need_congestion_window_wait(xprt)) {
1da177e4 281 xprt->snd_task = task;
1da177e4
LT
282 return 1;
283 }
c544577d 284out_unlock:
632e3bdc 285 xprt_clear_locked(xprt);
1da177e4 286out_sleep:
46121cf7 287 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
1da177e4 288 task->tk_status = -EAGAIN;
6b2e6856
TM
289 if (RPC_IS_SOFT(task))
290 rpc_sleep_on_timeout(&xprt->sending, task, NULL,
9e910bff 291 xprt_request_timeout(req));
6b2e6856
TM
292 else
293 rpc_sleep_on(&xprt->sending, task, NULL);
1da177e4
LT
294 return 0;
295}
12444809 296EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
1da177e4 297
12a80469 298static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
299{
300 int retval;
301
bd79bc57
TM
302 if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
303 return 1;
4a0f8c04 304 spin_lock_bh(&xprt->transport_lock);
43cedbf0 305 retval = xprt->ops->reserve_xprt(xprt, task);
4a0f8c04 306 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
307 return retval;
308}
309
961a828d 310static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
49e9a890 311{
961a828d 312 struct rpc_xprt *xprt = data;
49e9a890 313
49e9a890 314 xprt->snd_task = task;
961a828d
TM
315 return true;
316}
49e9a890 317
961a828d
TM
318static void __xprt_lock_write_next(struct rpc_xprt *xprt)
319{
320 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
321 return;
c544577d
TM
322 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
323 goto out_unlock;
f1dc237c
TM
324 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
325 __xprt_lock_write_func, xprt))
961a828d 326 return;
c544577d 327out_unlock:
632e3bdc 328 xprt_clear_locked(xprt);
49e9a890
CL
329}
330
961a828d
TM
331static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
332{
333 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
334 return;
c544577d
TM
335 if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
336 goto out_unlock;
75891f50 337 if (xprt_need_congestion_window_wait(xprt))
961a828d 338 goto out_unlock;
f1dc237c 339 if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
75891f50 340 __xprt_lock_write_func, xprt))
961a828d 341 return;
1da177e4 342out_unlock:
632e3bdc 343 xprt_clear_locked(xprt);
1da177e4
LT
344}
345
49e9a890
CL
346/**
347 * xprt_release_xprt - allow other requests to use a transport
348 * @xprt: transport with other tasks potentially waiting
349 * @task: task that is releasing access to the transport
350 *
351 * Note that "task" can be NULL. No congestion control is provided.
1da177e4 352 */
49e9a890 353void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4
LT
354{
355 if (xprt->snd_task == task) {
632e3bdc 356 xprt_clear_locked(xprt);
1da177e4
LT
357 __xprt_lock_write_next(xprt);
358 }
359}
12444809 360EXPORT_SYMBOL_GPL(xprt_release_xprt);
1da177e4 361
49e9a890
CL
362/**
363 * xprt_release_xprt_cong - allow other requests to use a transport
364 * @xprt: transport with other tasks potentially waiting
365 * @task: task that is releasing access to the transport
366 *
367 * Note that "task" can be NULL. Another task is awoken to use the
368 * transport if the transport's congestion window allows it.
369 */
370void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
371{
372 if (xprt->snd_task == task) {
632e3bdc 373 xprt_clear_locked(xprt);
49e9a890
CL
374 __xprt_lock_write_next_cong(xprt);
375 }
376}
12444809 377EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
49e9a890
CL
378
379static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4 380{
bd79bc57
TM
381 if (xprt->snd_task != task)
382 return;
4a0f8c04 383 spin_lock_bh(&xprt->transport_lock);
49e9a890 384 xprt->ops->release_xprt(xprt, task);
4a0f8c04 385 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
386}
387
1da177e4
LT
388/*
389 * Van Jacobson congestion avoidance. Check if the congestion window
390 * overflowed. Put the task to sleep if this is the case.
391 */
392static int
75891f50 393__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
1da177e4 394{
1da177e4
LT
395 if (req->rq_cong)
396 return 1;
46121cf7 397 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
75891f50
TM
398 req->rq_task->tk_pid, xprt->cong, xprt->cwnd);
399 if (RPCXPRT_CONGESTED(xprt)) {
400 xprt_set_congestion_window_wait(xprt);
1da177e4 401 return 0;
75891f50 402 }
1da177e4
LT
403 req->rq_cong = 1;
404 xprt->cong += RPC_CWNDSCALE;
405 return 1;
406}
407
408/*
409 * Adjust the congestion window, and wake up the next task
410 * that has been sleeping due to congestion
411 */
412static void
413__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
414{
415 if (!req->rq_cong)
416 return;
417 req->rq_cong = 0;
418 xprt->cong -= RPC_CWNDSCALE;
75891f50 419 xprt_test_and_clear_congestion_window_wait(xprt);
49e9a890 420 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
421}
422
75891f50
TM
423/**
424 * xprt_request_get_cong - Request congestion control credits
425 * @xprt: pointer to transport
426 * @req: pointer to RPC request
427 *
428 * Useful for transports that require congestion control.
429 */
430bool
431xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
432{
433 bool ret = false;
434
435 if (req->rq_cong)
436 return true;
437 spin_lock_bh(&xprt->transport_lock);
438 ret = __xprt_get_cong(xprt, req) != 0;
439 spin_unlock_bh(&xprt->transport_lock);
440 return ret;
441}
442EXPORT_SYMBOL_GPL(xprt_request_get_cong);
443
a58dd398
CL
444/**
445 * xprt_release_rqst_cong - housekeeping when request is complete
446 * @task: RPC request that recently completed
447 *
448 * Useful for transports that require congestion control.
449 */
450void xprt_release_rqst_cong(struct rpc_task *task)
451{
a4f0835c
TM
452 struct rpc_rqst *req = task->tk_rqstp;
453
454 __xprt_put_cong(req->rq_xprt, req);
a58dd398 455}
12444809 456EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
a58dd398 457
75891f50
TM
458/*
459 * Clear the congestion window wait flag and wake up the next
460 * entry on xprt->sending
461 */
462static void
463xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
464{
465 if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
466 spin_lock_bh(&xprt->transport_lock);
467 __xprt_lock_write_next_cong(xprt);
468 spin_unlock_bh(&xprt->transport_lock);
469 }
470}
471
46c0ee8b
CL
472/**
473 * xprt_adjust_cwnd - adjust transport congestion window
6a24dfb6 474 * @xprt: pointer to xprt
46c0ee8b
CL
475 * @task: recently completed RPC request used to adjust window
476 * @result: result code of completed RPC request
477 *
4f4cf5ad
CL
478 * The transport code maintains an estimate on the maximum number of out-
479 * standing RPC requests, using a smoothed version of the congestion
480 * avoidance implemented in 44BSD. This is basically the Van Jacobson
481 * congestion algorithm: If a retransmit occurs, the congestion window is
482 * halved; otherwise, it is incremented by 1/cwnd when
483 *
484 * - a reply is received and
485 * - a full number of requests are outstanding and
486 * - the congestion window hasn't been updated recently.
1da177e4 487 */
6a24dfb6 488void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
1da177e4 489{
46c0ee8b 490 struct rpc_rqst *req = task->tk_rqstp;
46c0ee8b 491 unsigned long cwnd = xprt->cwnd;
1da177e4 492
1da177e4
LT
493 if (result >= 0 && cwnd <= xprt->cong) {
494 /* The (cwnd >> 1) term makes sure
495 * the result gets rounded properly. */
496 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
497 if (cwnd > RPC_MAXCWND(xprt))
498 cwnd = RPC_MAXCWND(xprt);
49e9a890 499 __xprt_lock_write_next_cong(xprt);
1da177e4
LT
500 } else if (result == -ETIMEDOUT) {
501 cwnd >>= 1;
502 if (cwnd < RPC_CWNDSCALE)
503 cwnd = RPC_CWNDSCALE;
504 }
46121cf7 505 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
1da177e4
LT
506 xprt->cong, xprt->cwnd, cwnd);
507 xprt->cwnd = cwnd;
46c0ee8b 508 __xprt_put_cong(xprt, req);
1da177e4 509}
12444809 510EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
1da177e4 511
44fbac22
CL
512/**
513 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
514 * @xprt: transport with waiting tasks
515 * @status: result code to plant in each task before waking it
516 *
517 */
518void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
519{
520 if (status < 0)
521 rpc_wake_up_status(&xprt->pending, status);
522 else
523 rpc_wake_up(&xprt->pending);
524}
12444809 525EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
44fbac22 526
c7b2cae8
CL
527/**
528 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
c544577d 529 * @xprt: transport
a9a6b52e
TM
530 *
531 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
532 * we don't in general want to force a socket disconnection due to
533 * an incomplete RPC call transmission.
c7b2cae8 534 */
c544577d 535void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
c7b2cae8 536{
c544577d 537 set_bit(XPRT_WRITE_SPACE, &xprt->state);
c7b2cae8 538}
12444809 539EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
c7b2cae8 540
c544577d
TM
541static bool
542xprt_clear_write_space_locked(struct rpc_xprt *xprt)
543{
544 if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
545 __xprt_lock_write_next(xprt);
546 dprintk("RPC: write space: waking waiting task on "
547 "xprt %p\n", xprt);
548 return true;
549 }
550 return false;
551}
552
c7b2cae8
CL
553/**
554 * xprt_write_space - wake the task waiting for transport output buffer space
555 * @xprt: transport with waiting tasks
556 *
557 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
558 */
c544577d 559bool xprt_write_space(struct rpc_xprt *xprt)
c7b2cae8 560{
c544577d
TM
561 bool ret;
562
563 if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
564 return false;
c7b2cae8 565 spin_lock_bh(&xprt->transport_lock);
c544577d 566 ret = xprt_clear_write_space_locked(xprt);
c7b2cae8 567 spin_unlock_bh(&xprt->transport_lock);
c544577d 568 return ret;
c7b2cae8 569}
12444809 570EXPORT_SYMBOL_GPL(xprt_write_space);
c7b2cae8 571
1da177e4
LT
572static void xprt_reset_majortimeo(struct rpc_rqst *req)
573{
ba7392bb 574 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
575
576 req->rq_majortimeo = req->rq_timeout;
577 if (to->to_exponential)
578 req->rq_majortimeo <<= to->to_retries;
579 else
580 req->rq_majortimeo += to->to_increment * to->to_retries;
581 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
582 req->rq_majortimeo = to->to_maxval;
583 req->rq_majortimeo += jiffies;
584}
585
9903cd1c
CL
586/**
587 * xprt_adjust_timeout - adjust timeout values for next retransmit
588 * @req: RPC request containing parameters to use for the adjustment
589 *
1da177e4
LT
590 */
591int xprt_adjust_timeout(struct rpc_rqst *req)
592{
593 struct rpc_xprt *xprt = req->rq_xprt;
ba7392bb 594 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
1da177e4
LT
595 int status = 0;
596
597 if (time_before(jiffies, req->rq_majortimeo)) {
598 if (to->to_exponential)
599 req->rq_timeout <<= 1;
600 else
601 req->rq_timeout += to->to_increment;
602 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
603 req->rq_timeout = to->to_maxval;
604 req->rq_retries++;
1da177e4
LT
605 } else {
606 req->rq_timeout = to->to_initval;
607 req->rq_retries = 0;
608 xprt_reset_majortimeo(req);
609 /* Reset the RTT counters == "slow start" */
4a0f8c04 610 spin_lock_bh(&xprt->transport_lock);
1da177e4 611 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
4a0f8c04 612 spin_unlock_bh(&xprt->transport_lock);
1da177e4
LT
613 status = -ETIMEDOUT;
614 }
615
616 if (req->rq_timeout == 0) {
617 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
618 req->rq_timeout = 5 * HZ;
619 }
620 return status;
621}
622
65f27f38 623static void xprt_autoclose(struct work_struct *work)
1da177e4 624{
65f27f38
DH
625 struct rpc_xprt *xprt =
626 container_of(work, struct rpc_xprt, task_cleanup);
a1231fda 627 unsigned int pflags = memalloc_nofs_save();
1da177e4 628
66af1e55 629 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
4876cc77 630 xprt->ops->close(xprt);
1da177e4 631 xprt_release_write(xprt, NULL);
79234c3d 632 wake_up_bit(&xprt->state, XPRT_LOCKED);
a1231fda 633 memalloc_nofs_restore(pflags);
1da177e4
LT
634}
635
9903cd1c 636/**
62da3b24 637 * xprt_disconnect_done - mark a transport as disconnected
9903cd1c
CL
638 * @xprt: transport to flag for disconnect
639 *
1da177e4 640 */
62da3b24 641void xprt_disconnect_done(struct rpc_xprt *xprt)
1da177e4 642{
46121cf7 643 dprintk("RPC: disconnected transport %p\n", xprt);
4a0f8c04 644 spin_lock_bh(&xprt->transport_lock);
1da177e4 645 xprt_clear_connected(xprt);
c544577d 646 xprt_clear_write_space_locked(xprt);
27adc785 647 xprt_wake_pending_tasks(xprt, -ENOTCONN);
4a0f8c04 648 spin_unlock_bh(&xprt->transport_lock);
1da177e4 649}
62da3b24 650EXPORT_SYMBOL_GPL(xprt_disconnect_done);
1da177e4 651
66af1e55
TM
652/**
653 * xprt_force_disconnect - force a transport to disconnect
654 * @xprt: transport to disconnect
655 *
656 */
657void xprt_force_disconnect(struct rpc_xprt *xprt)
658{
659 /* Don't race with the test_bit() in xprt_clear_locked() */
660 spin_lock_bh(&xprt->transport_lock);
661 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
662 /* Try to schedule an autoclose RPC call */
663 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
40a5f1b1 664 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
0445f92c
TM
665 else if (xprt->snd_task)
666 rpc_wake_up_queued_task_set_status(&xprt->pending,
667 xprt->snd_task, -ENOTCONN);
66af1e55
TM
668 spin_unlock_bh(&xprt->transport_lock);
669}
e2a4f4fb 670EXPORT_SYMBOL_GPL(xprt_force_disconnect);
66af1e55 671
7f3a1d1e
TM
672static unsigned int
673xprt_connect_cookie(struct rpc_xprt *xprt)
674{
675 return READ_ONCE(xprt->connect_cookie);
676}
677
678static bool
679xprt_request_retransmit_after_disconnect(struct rpc_task *task)
680{
681 struct rpc_rqst *req = task->tk_rqstp;
682 struct rpc_xprt *xprt = req->rq_xprt;
683
684 return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
685 !xprt_connected(xprt);
686}
687
7c1d71cf
TM
688/**
689 * xprt_conditional_disconnect - force a transport to disconnect
690 * @xprt: transport to disconnect
691 * @cookie: 'connection cookie'
692 *
693 * This attempts to break the connection if and only if 'cookie' matches
694 * the current transport 'connection cookie'. It ensures that we don't
695 * try to break the connection more than once when we need to retransmit
696 * a batch of RPC requests.
697 *
698 */
699void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
700{
701 /* Don't race with the test_bit() in xprt_clear_locked() */
702 spin_lock_bh(&xprt->transport_lock);
703 if (cookie != xprt->connect_cookie)
704 goto out;
2c2ee6d2 705 if (test_bit(XPRT_CLOSING, &xprt->state))
7c1d71cf
TM
706 goto out;
707 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
708 /* Try to schedule an autoclose RPC call */
709 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
40a5f1b1 710 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
2a491991 711 xprt_wake_pending_tasks(xprt, -EAGAIN);
7c1d71cf
TM
712out:
713 spin_unlock_bh(&xprt->transport_lock);
714}
715
ad3331ac
TM
716static bool
717xprt_has_timer(const struct rpc_xprt *xprt)
718{
719 return xprt->idle_timeout != 0;
720}
721
722static void
723xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
724 __must_hold(&xprt->transport_lock)
725{
95f7691d 726 if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
ad3331ac
TM
727 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
728}
729
1da177e4 730static void
ff861c4d 731xprt_init_autodisconnect(struct timer_list *t)
1da177e4 732{
ff861c4d 733 struct rpc_xprt *xprt = from_timer(xprt, t, timer);
1da177e4 734
4a0f8c04 735 spin_lock(&xprt->transport_lock);
95f7691d 736 if (!RB_EMPTY_ROOT(&xprt->recv_queue))
1da177e4 737 goto out_abort;
ad3331ac
TM
738 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
739 xprt->last_used = jiffies;
2226feb6 740 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
1da177e4 741 goto out_abort;
4a0f8c04 742 spin_unlock(&xprt->transport_lock);
40a5f1b1 743 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
1da177e4
LT
744 return;
745out_abort:
4a0f8c04 746 spin_unlock(&xprt->transport_lock);
1da177e4
LT
747}
748
718ba5b8
TM
749bool xprt_lock_connect(struct rpc_xprt *xprt,
750 struct rpc_task *task,
751 void *cookie)
752{
753 bool ret = false;
754
755 spin_lock_bh(&xprt->transport_lock);
756 if (!test_bit(XPRT_LOCKED, &xprt->state))
757 goto out;
758 if (xprt->snd_task != task)
759 goto out;
760 xprt->snd_task = cookie;
761 ret = true;
762out:
763 spin_unlock_bh(&xprt->transport_lock);
764 return ret;
765}
766
767void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
768{
769 spin_lock_bh(&xprt->transport_lock);
770 if (xprt->snd_task != cookie)
771 goto out;
772 if (!test_bit(XPRT_LOCKED, &xprt->state))
773 goto out;
774 xprt->snd_task =NULL;
775 xprt->ops->release_xprt(xprt, NULL);
ad3331ac 776 xprt_schedule_autodisconnect(xprt);
718ba5b8
TM
777out:
778 spin_unlock_bh(&xprt->transport_lock);
79234c3d 779 wake_up_bit(&xprt->state, XPRT_LOCKED);
718ba5b8
TM
780}
781
9903cd1c
CL
782/**
783 * xprt_connect - schedule a transport connect operation
784 * @task: RPC task that is requesting the connect
1da177e4
LT
785 *
786 */
787void xprt_connect(struct rpc_task *task)
788{
ad2368d6 789 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1da177e4 790
46121cf7 791 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
1da177e4
LT
792 xprt, (xprt_connected(xprt) ? "is" : "is not"));
793
ec739ef0 794 if (!xprt_bound(xprt)) {
01d37c42 795 task->tk_status = -EAGAIN;
1da177e4
LT
796 return;
797 }
798 if (!xprt_lock_write(xprt, task))
799 return;
feb8ca37
TM
800
801 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
802 xprt->ops->close(xprt);
803
718ba5b8 804 if (!xprt_connected(xprt)) {
2c2ee6d2 805 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
6b2e6856 806 rpc_sleep_on_timeout(&xprt->pending, task, NULL,
9e910bff 807 xprt_request_timeout(task->tk_rqstp));
0b9e7943
TM
808
809 if (test_bit(XPRT_CLOSING, &xprt->state))
810 return;
811 if (xprt_test_and_set_connecting(xprt))
812 return;
0a9a4304
TM
813 /* Race breaker */
814 if (!xprt_connected(xprt)) {
815 xprt->stat.connect_start = jiffies;
816 xprt->ops->connect(xprt, task);
817 } else {
818 xprt_clear_connecting(xprt);
819 task->tk_status = 0;
820 rpc_wake_up_queued_task(&xprt->pending, task);
821 }
1da177e4 822 }
718ba5b8 823 xprt_release_write(xprt, task);
1da177e4
LT
824}
825
95f7691d
TM
826enum xprt_xid_rb_cmp {
827 XID_RB_EQUAL,
828 XID_RB_LEFT,
829 XID_RB_RIGHT,
830};
831static enum xprt_xid_rb_cmp
832xprt_xid_cmp(__be32 xid1, __be32 xid2)
833{
834 if (xid1 == xid2)
835 return XID_RB_EQUAL;
836 if ((__force u32)xid1 < (__force u32)xid2)
837 return XID_RB_LEFT;
838 return XID_RB_RIGHT;
839}
840
841static struct rpc_rqst *
842xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
843{
844 struct rb_node *n = xprt->recv_queue.rb_node;
845 struct rpc_rqst *req;
846
847 while (n != NULL) {
848 req = rb_entry(n, struct rpc_rqst, rq_recv);
849 switch (xprt_xid_cmp(xid, req->rq_xid)) {
850 case XID_RB_LEFT:
851 n = n->rb_left;
852 break;
853 case XID_RB_RIGHT:
854 n = n->rb_right;
855 break;
856 case XID_RB_EQUAL:
857 return req;
858 }
859 }
860 return NULL;
861}
862
863static void
864xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
865{
866 struct rb_node **p = &xprt->recv_queue.rb_node;
867 struct rb_node *n = NULL;
868 struct rpc_rqst *req;
869
870 while (*p != NULL) {
871 n = *p;
872 req = rb_entry(n, struct rpc_rqst, rq_recv);
873 switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
874 case XID_RB_LEFT:
875 p = &n->rb_left;
876 break;
877 case XID_RB_RIGHT:
878 p = &n->rb_right;
879 break;
880 case XID_RB_EQUAL:
881 WARN_ON_ONCE(new != req);
882 return;
883 }
884 }
885 rb_link_node(&new->rq_recv, n, p);
886 rb_insert_color(&new->rq_recv, &xprt->recv_queue);
887}
888
889static void
890xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
891{
892 rb_erase(&req->rq_recv, &xprt->recv_queue);
893}
894
9903cd1c
CL
895/**
896 * xprt_lookup_rqst - find an RPC request corresponding to an XID
897 * @xprt: transport on which the original request was transmitted
898 * @xid: RPC XID of incoming reply
899 *
75c84151 900 * Caller holds xprt->queue_lock.
1da177e4 901 */
d8ed029d 902struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1da177e4 903{
8f3a6de3 904 struct rpc_rqst *entry;
1da177e4 905
95f7691d
TM
906 entry = xprt_request_rb_find(xprt, xid);
907 if (entry != NULL) {
908 trace_xprt_lookup_rqst(xprt, xid, 0);
909 entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
910 return entry;
911 }
46121cf7
CL
912
913 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
914 ntohl(xid));
3705ad64 915 trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
262ca07d
CL
916 xprt->stat.bad_xids++;
917 return NULL;
1da177e4 918}
12444809 919EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1da177e4 920
cf9946cd
TM
921static bool
922xprt_is_pinned_rqst(struct rpc_rqst *req)
923{
924 return atomic_read(&req->rq_pin) != 0;
925}
926
729749bb
TM
927/**
928 * xprt_pin_rqst - Pin a request on the transport receive list
929 * @req: Request to pin
930 *
931 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
cf9946cd 932 * so should be holding the xprt receive lock.
729749bb
TM
933 */
934void xprt_pin_rqst(struct rpc_rqst *req)
935{
cf9946cd 936 atomic_inc(&req->rq_pin);
729749bb 937}
9590d083 938EXPORT_SYMBOL_GPL(xprt_pin_rqst);
729749bb
TM
939
940/**
941 * xprt_unpin_rqst - Unpin a request on the transport receive list
942 * @req: Request to pin
943 *
cf9946cd 944 * Caller should be holding the xprt receive lock.
729749bb
TM
945 */
946void xprt_unpin_rqst(struct rpc_rqst *req)
947{
cf9946cd
TM
948 if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
949 atomic_dec(&req->rq_pin);
950 return;
951 }
952 if (atomic_dec_and_test(&req->rq_pin))
953 wake_up_var(&req->rq_pin);
729749bb 954}
9590d083 955EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
729749bb
TM
956
957static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
729749bb 958{
cf9946cd 959 wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
729749bb
TM
960}
961
edc81dcd
TM
962static bool
963xprt_request_data_received(struct rpc_task *task)
964{
965 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
966 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
967}
968
969static bool
970xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
971{
972 return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
973 READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
974}
975
976/**
977 * xprt_request_enqueue_receive - Add an request to the receive queue
978 * @task: RPC task
979 *
980 */
981void
982xprt_request_enqueue_receive(struct rpc_task *task)
983{
984 struct rpc_rqst *req = task->tk_rqstp;
985 struct rpc_xprt *xprt = req->rq_xprt;
986
987 if (!xprt_request_need_enqueue_receive(task, req))
988 return;
989 spin_lock(&xprt->queue_lock);
990
991 /* Update the softirq receive buffer */
992 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
993 sizeof(req->rq_private_buf));
994
995 /* Add request to the receive list */
95f7691d 996 xprt_request_rb_insert(xprt, req);
edc81dcd
TM
997 set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
998 spin_unlock(&xprt->queue_lock);
999
1000 xprt_reset_majortimeo(req);
1001 /* Turn off autodisconnect */
1002 del_singleshot_timer_sync(&xprt->timer);
1003}
1004
1005/**
1006 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1007 * @task: RPC task
1008 *
1009 * Caller must hold xprt->queue_lock.
1010 */
1011static void
1012xprt_request_dequeue_receive_locked(struct rpc_task *task)
1013{
95f7691d
TM
1014 struct rpc_rqst *req = task->tk_rqstp;
1015
edc81dcd 1016 if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
95f7691d 1017 xprt_request_rb_remove(req->rq_xprt, req);
edc81dcd
TM
1018}
1019
ecd465ee
CL
1020/**
1021 * xprt_update_rtt - Update RPC RTT statistics
1022 * @task: RPC request that recently completed
1023 *
75c84151 1024 * Caller holds xprt->queue_lock.
ecd465ee
CL
1025 */
1026void xprt_update_rtt(struct rpc_task *task)
1570c1e4
CL
1027{
1028 struct rpc_rqst *req = task->tk_rqstp;
1029 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
95c96174 1030 unsigned int timer = task->tk_msg.rpc_proc->p_timer;
d60dbb20 1031 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1570c1e4
CL
1032
1033 if (timer) {
1034 if (req->rq_ntrans == 1)
ff839970 1035 rpc_update_rtt(rtt, timer, m);
1570c1e4
CL
1036 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1037 }
1038}
ecd465ee 1039EXPORT_SYMBOL_GPL(xprt_update_rtt);
1570c1e4 1040
9903cd1c
CL
1041/**
1042 * xprt_complete_rqst - called when reply processing is complete
1570c1e4 1043 * @task: RPC request that recently completed
9903cd1c
CL
1044 * @copied: actual number of bytes received from the transport
1045 *
75c84151 1046 * Caller holds xprt->queue_lock.
1da177e4 1047 */
1570c1e4 1048void xprt_complete_rqst(struct rpc_task *task, int copied)
1da177e4 1049{
1570c1e4 1050 struct rpc_rqst *req = task->tk_rqstp;
fda13939 1051 struct rpc_xprt *xprt = req->rq_xprt;
1da177e4 1052
1570c1e4
CL
1053 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
1054 task->tk_pid, ntohl(req->rq_xid), copied);
3705ad64 1055 trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
1da177e4 1056
fda13939 1057 xprt->stat.recvs++;
ef759a2e 1058
1e799b67 1059 req->rq_private_buf.len = copied;
dd2b63d0
RL
1060 /* Ensure all writes are done before we update */
1061 /* req->rq_reply_bytes_recvd */
43ac3f29 1062 smp_wmb();
dd2b63d0 1063 req->rq_reply_bytes_recvd = copied;
edc81dcd 1064 xprt_request_dequeue_receive_locked(task);
fda13939 1065 rpc_wake_up_queued_task(&xprt->pending, task);
1da177e4 1066}
12444809 1067EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1da177e4 1068
46c0ee8b 1069static void xprt_timer(struct rpc_task *task)
1da177e4 1070{
46c0ee8b 1071 struct rpc_rqst *req = task->tk_rqstp;
1da177e4
LT
1072 struct rpc_xprt *xprt = req->rq_xprt;
1073
5d00837b
TM
1074 if (task->tk_status != -ETIMEDOUT)
1075 return;
1da177e4 1076
82476d9f 1077 trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
dd2b63d0 1078 if (!req->rq_reply_bytes_recvd) {
46c0ee8b 1079 if (xprt->ops->timer)
6a24dfb6 1080 xprt->ops->timer(xprt, task);
5d00837b
TM
1081 } else
1082 task->tk_status = 0;
1da177e4
LT
1083}
1084
8ba6a92d
TM
1085/**
1086 * xprt_wait_for_reply_request_def - wait for reply
1087 * @task: pointer to rpc_task
1088 *
1089 * Set a request's retransmit timeout based on the transport's
1090 * default timeout parameters. Used by transports that don't adjust
1091 * the retransmit timeout based on round-trip time estimation,
1092 * and put the task to sleep on the pending queue.
1093 */
1094void xprt_wait_for_reply_request_def(struct rpc_task *task)
1095{
1096 struct rpc_rqst *req = task->tk_rqstp;
1097
6b2e6856 1098 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
9e910bff 1099 xprt_request_timeout(req));
8ba6a92d
TM
1100}
1101EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1102
1103/**
1104 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1105 * @task: pointer to rpc_task
1106 *
1107 * Set a request's retransmit timeout using the RTT estimator,
1108 * and put the task to sleep on the pending queue.
1109 */
1110void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1111{
1112 int timer = task->tk_msg.rpc_proc->p_timer;
1113 struct rpc_clnt *clnt = task->tk_client;
1114 struct rpc_rtt *rtt = clnt->cl_rtt;
1115 struct rpc_rqst *req = task->tk_rqstp;
1116 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
6b2e6856 1117 unsigned long timeout;
8ba6a92d 1118
6b2e6856
TM
1119 timeout = rpc_calc_rto(rtt, timer);
1120 timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1121 if (timeout > max_timeout || timeout == 0)
1122 timeout = max_timeout;
1123 rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1124 jiffies + timeout);
8ba6a92d
TM
1125}
1126EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1127
7f3a1d1e
TM
1128/**
1129 * xprt_request_wait_receive - wait for the reply to an RPC request
1130 * @task: RPC task about to send a request
1131 *
1132 */
1133void xprt_request_wait_receive(struct rpc_task *task)
1134{
1135 struct rpc_rqst *req = task->tk_rqstp;
1136 struct rpc_xprt *xprt = req->rq_xprt;
1137
1138 if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1139 return;
1140 /*
1141 * Sleep on the pending queue if we're expecting a reply.
1142 * The spinlock ensures atomicity between the test of
1143 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1144 */
1145 spin_lock(&xprt->queue_lock);
1146 if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
8ba6a92d 1147 xprt->ops->wait_for_reply_request(task);
7f3a1d1e
TM
1148 /*
1149 * Send an extra queue wakeup call if the
1150 * connection was dropped in case the call to
1151 * rpc_sleep_on() raced.
1152 */
1153 if (xprt_request_retransmit_after_disconnect(task))
1154 rpc_wake_up_queued_task_set_status(&xprt->pending,
1155 task, -ENOTCONN);
1156 }
1157 spin_unlock(&xprt->queue_lock);
1158}
1159
944b0429
TM
1160static bool
1161xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1162{
762e4e67 1163 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
944b0429
TM
1164}
1165
1166/**
1167 * xprt_request_enqueue_transmit - queue a task for transmission
1168 * @task: pointer to rpc_task
1169 *
1170 * Add a task to the transmission queue.
1171 */
1172void
1173xprt_request_enqueue_transmit(struct rpc_task *task)
1174{
918f3c1f 1175 struct rpc_rqst *pos, *req = task->tk_rqstp;
944b0429
TM
1176 struct rpc_xprt *xprt = req->rq_xprt;
1177
1178 if (xprt_request_need_enqueue_transmit(task, req)) {
e66721f0 1179 req->rq_bytes_sent = 0;
944b0429 1180 spin_lock(&xprt->queue_lock);
75891f50
TM
1181 /*
1182 * Requests that carry congestion control credits are added
1183 * to the head of the list to avoid starvation issues.
1184 */
1185 if (req->rq_cong) {
1186 xprt_clear_congestion_window_wait(xprt);
1187 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1188 if (pos->rq_cong)
1189 continue;
1190 /* Note: req is added _before_ pos */
1191 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1192 INIT_LIST_HEAD(&req->rq_xmit2);
0c77668d 1193 trace_xprt_enq_xmit(task, 1);
75891f50
TM
1194 goto out;
1195 }
86aeee0e
TM
1196 } else if (RPC_IS_SWAPPER(task)) {
1197 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1198 if (pos->rq_cong || pos->rq_bytes_sent)
1199 continue;
1200 if (RPC_IS_SWAPPER(pos->rq_task))
1201 continue;
1202 /* Note: req is added _before_ pos */
1203 list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1204 INIT_LIST_HEAD(&req->rq_xmit2);
0c77668d 1205 trace_xprt_enq_xmit(task, 2);
86aeee0e
TM
1206 goto out;
1207 }
deaa5c96 1208 } else if (!req->rq_seqno) {
75891f50
TM
1209 list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1210 if (pos->rq_task->tk_owner != task->tk_owner)
1211 continue;
1212 list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1213 INIT_LIST_HEAD(&req->rq_xmit);
0c77668d 1214 trace_xprt_enq_xmit(task, 3);
75891f50
TM
1215 goto out;
1216 }
918f3c1f 1217 }
944b0429 1218 list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
918f3c1f 1219 INIT_LIST_HEAD(&req->rq_xmit2);
0c77668d 1220 trace_xprt_enq_xmit(task, 4);
918f3c1f 1221out:
944b0429
TM
1222 set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1223 spin_unlock(&xprt->queue_lock);
1224 }
1225}
1226
1227/**
1228 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1229 * @task: pointer to rpc_task
1230 *
1231 * Remove a task from the transmission queue
1232 * Caller must hold xprt->queue_lock
1233 */
1234static void
1235xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1236{
918f3c1f
TM
1237 struct rpc_rqst *req = task->tk_rqstp;
1238
1239 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1240 return;
1241 if (!list_empty(&req->rq_xmit)) {
1242 list_del(&req->rq_xmit);
1243 if (!list_empty(&req->rq_xmit2)) {
1244 struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1245 struct rpc_rqst, rq_xmit2);
1246 list_del(&req->rq_xmit2);
1247 list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1248 }
1249 } else
1250 list_del(&req->rq_xmit2);
944b0429
TM
1251}
1252
1253/**
1254 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1255 * @task: pointer to rpc_task
1256 *
1257 * Remove a task from the transmission queue
1258 */
1259static void
1260xprt_request_dequeue_transmit(struct rpc_task *task)
1261{
1262 struct rpc_rqst *req = task->tk_rqstp;
1263 struct rpc_xprt *xprt = req->rq_xprt;
1264
1265 spin_lock(&xprt->queue_lock);
1266 xprt_request_dequeue_transmit_locked(task);
1267 spin_unlock(&xprt->queue_lock);
1268}
1269
9d96acbc
TM
1270/**
1271 * xprt_request_prepare - prepare an encoded request for transport
1272 * @req: pointer to rpc_rqst
1273 *
1274 * Calls into the transport layer to do whatever is needed to prepare
1275 * the request for transmission or receive.
1276 */
1277void
1278xprt_request_prepare(struct rpc_rqst *req)
1279{
1280 struct rpc_xprt *xprt = req->rq_xprt;
1281
1282 if (xprt->ops->prepare_request)
1283 xprt->ops->prepare_request(req);
1284}
1285
762e4e67
TM
1286/**
1287 * xprt_request_need_retransmit - Test if a task needs retransmission
1288 * @task: pointer to rpc_task
1289 *
1290 * Test for whether a connection breakage requires the task to retransmit
1291 */
1292bool
1293xprt_request_need_retransmit(struct rpc_task *task)
1294{
1295 return xprt_request_retransmit_after_disconnect(task);
1296}
1297
9903cd1c
CL
1298/**
1299 * xprt_prepare_transmit - reserve the transport before sending a request
1300 * @task: RPC task about to send a request
1301 *
1da177e4 1302 */
90051ea7 1303bool xprt_prepare_transmit(struct rpc_task *task)
1da177e4
LT
1304{
1305 struct rpc_rqst *req = task->tk_rqstp;
1306 struct rpc_xprt *xprt = req->rq_xprt;
1da177e4 1307
46121cf7 1308 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
1da177e4 1309
5f2f6bd9
TM
1310 if (!xprt_lock_write(xprt, task)) {
1311 /* Race breaker: someone may have transmitted us */
944b0429 1312 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
5f2f6bd9
TM
1313 rpc_wake_up_queued_task_set_status(&xprt->sending,
1314 task, 0);
1315 return false;
1316
90051ea7 1317 }
5f2f6bd9 1318 return true;
1da177e4
LT
1319}
1320
e0ab53de 1321void xprt_end_transmit(struct rpc_task *task)
5e5ce5be 1322{
343952fa 1323 xprt_release_write(task->tk_rqstp->rq_xprt, task);
5e5ce5be
TM
1324}
1325
9903cd1c 1326/**
89f90fe1
TM
1327 * xprt_request_transmit - send an RPC request on a transport
1328 * @req: pointer to request to transmit
1329 * @snd_task: RPC task that owns the transport lock
9903cd1c 1330 *
89f90fe1
TM
1331 * This performs the transmission of a single request.
1332 * Note that if the request is not the same as snd_task, then it
1333 * does need to be pinned.
1334 * Returns '0' on success.
9903cd1c 1335 */
89f90fe1
TM
1336static int
1337xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
1da177e4 1338{
89f90fe1
TM
1339 struct rpc_xprt *xprt = req->rq_xprt;
1340 struct rpc_task *task = req->rq_task;
90d91b0c 1341 unsigned int connect_cookie;
dcbbeda8 1342 int is_retrans = RPC_WAS_SENT(task);
ff699ea8 1343 int status;
1da177e4 1344
edc81dcd 1345 if (!req->rq_bytes_sent) {
89f90fe1
TM
1346 if (xprt_request_data_received(task)) {
1347 status = 0;
944b0429 1348 goto out_dequeue;
89f90fe1 1349 }
3021a5bb 1350 /* Verify that our message lies in the RPCSEC_GSS window */
edc81dcd 1351 if (rpcauth_xmit_need_reencode(task)) {
89f90fe1 1352 status = -EBADMSG;
944b0429 1353 goto out_dequeue;
a79f194a
TM
1354 }
1355 if (task->tk_ops->rpc_call_prepare_transmit) {
1356 task->tk_ops->rpc_call_prepare_transmit(task,
1357 task->tk_calldata);
1358 status = task->tk_status;
1359 if (status < 0)
1360 goto out_dequeue;
ae67bd38
TM
1361 }
1362 if (RPC_SIGNALLED(task)) {
1363 status = -ERESTARTSYS;
1364 goto out_dequeue;
3021a5bb 1365 }
edc81dcd 1366 }
1da177e4 1367
dcbbeda8
TM
1368 /*
1369 * Update req->rq_ntrans before transmitting to avoid races with
1370 * xprt_update_rtt(), which needs to know that it is recording a
1371 * reply to the first transmission.
1372 */
1373 req->rq_ntrans++;
1374
90d91b0c 1375 connect_cookie = xprt->connect_cookie;
adfa7144 1376 status = xprt->ops->send_request(req);
c8485e4d 1377 if (status != 0) {
dcbbeda8 1378 req->rq_ntrans--;
0c77668d 1379 trace_xprt_transmit(req, status);
89f90fe1 1380 return status;
c8485e4d 1381 }
7ebbbc6e 1382
dcbbeda8
TM
1383 if (is_retrans)
1384 task->tk_client->cl_stats->rpcretrans++;
1385
4a068258 1386 xprt_inject_disconnect(xprt);
262ca07d 1387
468f8613 1388 task->tk_flags |= RPC_TASK_SENT;
c8485e4d 1389 spin_lock_bh(&xprt->transport_lock);
262ca07d 1390
c8485e4d
TM
1391 xprt->stat.sends++;
1392 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1393 xprt->stat.bklog_u += xprt->backlog.qlen;
15a45206
AA
1394 xprt->stat.sending_u += xprt->sending.qlen;
1395 xprt->stat.pending_u += xprt->pending.qlen;
90d91b0c 1396 spin_unlock_bh(&xprt->transport_lock);
1da177e4 1397
90d91b0c 1398 req->rq_connect_cookie = connect_cookie;
944b0429 1399out_dequeue:
0c77668d 1400 trace_xprt_transmit(req, status);
944b0429 1401 xprt_request_dequeue_transmit(task);
89f90fe1
TM
1402 rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1403 return status;
1404}
1405
1406/**
1407 * xprt_transmit - send an RPC request on a transport
1408 * @task: controlling RPC task
1409 *
1410 * Attempts to drain the transmit queue. On exit, either the transport
1411 * signalled an error that needs to be handled before transmission can
1412 * resume, or @task finished transmitting, and detected that it already
1413 * received a reply.
1414 */
1415void
1416xprt_transmit(struct rpc_task *task)
1417{
1418 struct rpc_rqst *next, *req = task->tk_rqstp;
1419 struct rpc_xprt *xprt = req->rq_xprt;
1420 int status;
1421
1422 spin_lock(&xprt->queue_lock);
1423 while (!list_empty(&xprt->xmit_queue)) {
1424 next = list_first_entry(&xprt->xmit_queue,
1425 struct rpc_rqst, rq_xmit);
1426 xprt_pin_rqst(next);
1427 spin_unlock(&xprt->queue_lock);
1428 status = xprt_request_transmit(next, task);
1429 if (status == -EBADMSG && next != req)
1430 status = 0;
1431 cond_resched();
1432 spin_lock(&xprt->queue_lock);
1433 xprt_unpin_rqst(next);
1434 if (status == 0) {
1435 if (!xprt_request_data_received(task) ||
1436 test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1437 continue;
c544577d 1438 } else if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
89f90fe1
TM
1439 task->tk_status = status;
1440 break;
1441 }
1442 spin_unlock(&xprt->queue_lock);
1da177e4
LT
1443}
1444
ba60eb25
TM
1445static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1446{
1447 set_bit(XPRT_CONGESTED, &xprt->state);
1448 rpc_sleep_on(&xprt->backlog, task, NULL);
1449}
1450
1451static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1452{
1453 if (rpc_wake_up_next(&xprt->backlog) == NULL)
1454 clear_bit(XPRT_CONGESTED, &xprt->state);
1455}
1456
1457static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1458{
1459 bool ret = false;
1460
1461 if (!test_bit(XPRT_CONGESTED, &xprt->state))
1462 goto out;
1463 spin_lock(&xprt->reserve_lock);
1464 if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1465 rpc_sleep_on(&xprt->backlog, task, NULL);
1466 ret = true;
1467 }
1468 spin_unlock(&xprt->reserve_lock);
1469out:
1470 return ret;
1471}
1472
92ea011f 1473static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
d9ba131d
TM
1474{
1475 struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1476
ff699ea8 1477 if (xprt->num_reqs >= xprt->max_reqs)
d9ba131d 1478 goto out;
ff699ea8 1479 ++xprt->num_reqs;
92ea011f
TM
1480 spin_unlock(&xprt->reserve_lock);
1481 req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1482 spin_lock(&xprt->reserve_lock);
d9ba131d
TM
1483 if (req != NULL)
1484 goto out;
ff699ea8 1485 --xprt->num_reqs;
d9ba131d
TM
1486 req = ERR_PTR(-ENOMEM);
1487out:
1488 return req;
1489}
1490
1491static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1492{
ff699ea8
CL
1493 if (xprt->num_reqs > xprt->min_reqs) {
1494 --xprt->num_reqs;
d9ba131d
TM
1495 kfree(req);
1496 return true;
1497 }
1498 return false;
1499}
1500
f39c1bfb 1501void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1da177e4 1502{
d9ba131d 1503 struct rpc_rqst *req;
1da177e4 1504
f39c1bfb 1505 spin_lock(&xprt->reserve_lock);
1da177e4 1506 if (!list_empty(&xprt->free)) {
d9ba131d
TM
1507 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1508 list_del(&req->rq_list);
1509 goto out_init_req;
1510 }
92ea011f 1511 req = xprt_dynamic_alloc_slot(xprt);
d9ba131d
TM
1512 if (!IS_ERR(req))
1513 goto out_init_req;
1514 switch (PTR_ERR(req)) {
1515 case -ENOMEM:
d9ba131d
TM
1516 dprintk("RPC: dynamic allocation of request slot "
1517 "failed! Retrying\n");
1afeaf5c 1518 task->tk_status = -ENOMEM;
d9ba131d
TM
1519 break;
1520 case -EAGAIN:
ba60eb25 1521 xprt_add_backlog(xprt, task);
d9ba131d 1522 dprintk("RPC: waiting for request slot\n");
e9d47639 1523 /* fall through */
1afeaf5c
TM
1524 default:
1525 task->tk_status = -EAGAIN;
1da177e4 1526 }
f39c1bfb 1527 spin_unlock(&xprt->reserve_lock);
d9ba131d
TM
1528 return;
1529out_init_req:
ff699ea8
CL
1530 xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1531 xprt->num_reqs);
37ac86c3
CL
1532 spin_unlock(&xprt->reserve_lock);
1533
d9ba131d
TM
1534 task->tk_status = 0;
1535 task->tk_rqstp = req;
f39c1bfb
TM
1536}
1537EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1538
a9cde23a 1539void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
ee5ebe85 1540{
ee5ebe85 1541 spin_lock(&xprt->reserve_lock);
c25573b5
TM
1542 if (!xprt_dynamic_free_slot(xprt, req)) {
1543 memset(req, 0, sizeof(*req)); /* mark unused */
1544 list_add(&req->rq_list, &xprt->free);
1545 }
ba60eb25 1546 xprt_wake_up_backlog(xprt);
ee5ebe85
TM
1547 spin_unlock(&xprt->reserve_lock);
1548}
a9cde23a 1549EXPORT_SYMBOL_GPL(xprt_free_slot);
ee5ebe85 1550
21de0a95
TM
1551static void xprt_free_all_slots(struct rpc_xprt *xprt)
1552{
1553 struct rpc_rqst *req;
1554 while (!list_empty(&xprt->free)) {
1555 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1556 list_del(&req->rq_list);
1557 kfree(req);
1558 }
1559}
1560
d9ba131d
TM
1561struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1562 unsigned int num_prealloc,
1563 unsigned int max_alloc)
bd1722d4
PE
1564{
1565 struct rpc_xprt *xprt;
21de0a95
TM
1566 struct rpc_rqst *req;
1567 int i;
bd1722d4
PE
1568
1569 xprt = kzalloc(size, GFP_KERNEL);
1570 if (xprt == NULL)
1571 goto out;
1572
21de0a95
TM
1573 xprt_init(xprt, net);
1574
1575 for (i = 0; i < num_prealloc; i++) {
1576 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1577 if (!req)
8313164c 1578 goto out_free;
21de0a95
TM
1579 list_add(&req->rq_list, &xprt->free);
1580 }
d9ba131d
TM
1581 if (max_alloc > num_prealloc)
1582 xprt->max_reqs = max_alloc;
1583 else
1584 xprt->max_reqs = num_prealloc;
1585 xprt->min_reqs = num_prealloc;
ff699ea8 1586 xprt->num_reqs = num_prealloc;
bd1722d4
PE
1587
1588 return xprt;
1589
1590out_free:
21de0a95 1591 xprt_free(xprt);
bd1722d4
PE
1592out:
1593 return NULL;
1594}
1595EXPORT_SYMBOL_GPL(xprt_alloc);
1596
e204e621
PE
1597void xprt_free(struct rpc_xprt *xprt)
1598{
37aa2133 1599 put_net(xprt->xprt_net);
21de0a95 1600 xprt_free_all_slots(xprt);
fda1bfef 1601 kfree_rcu(xprt, rcu);
e204e621
PE
1602}
1603EXPORT_SYMBOL_GPL(xprt_free);
1604
902c5887
TM
1605static void
1606xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1607{
1608 req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1609}
1610
9dc6edcf
TM
1611static __be32
1612xprt_alloc_xid(struct rpc_xprt *xprt)
1613{
1614 __be32 xid;
1615
1616 spin_lock(&xprt->reserve_lock);
1617 xid = (__force __be32)xprt->xid++;
1618 spin_unlock(&xprt->reserve_lock);
1619 return xid;
1620}
1621
1622static void
1623xprt_init_xid(struct rpc_xprt *xprt)
1624{
1625 xprt->xid = prandom_u32();
1626}
1627
1628static void
1629xprt_request_init(struct rpc_task *task)
1630{
1631 struct rpc_xprt *xprt = task->tk_xprt;
1632 struct rpc_rqst *req = task->tk_rqstp;
1633
9dc6edcf
TM
1634 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1635 req->rq_task = task;
1636 req->rq_xprt = xprt;
1637 req->rq_buffer = NULL;
1638 req->rq_xid = xprt_alloc_xid(xprt);
902c5887 1639 xprt_init_connect_cookie(req, xprt);
9dc6edcf
TM
1640 req->rq_snd_buf.len = 0;
1641 req->rq_snd_buf.buflen = 0;
1642 req->rq_rcv_buf.len = 0;
1643 req->rq_rcv_buf.buflen = 0;
71700bb9
TM
1644 req->rq_snd_buf.bvec = NULL;
1645 req->rq_rcv_buf.bvec = NULL;
9dc6edcf
TM
1646 req->rq_release_snd_buf = NULL;
1647 xprt_reset_majortimeo(req);
1648 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1649 req, ntohl(req->rq_xid));
1650}
1651
1652static void
1653xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
1654{
1655 xprt->ops->alloc_slot(xprt, task);
1656 if (task->tk_rqstp != NULL)
1657 xprt_request_init(task);
1658}
1659
9903cd1c
CL
1660/**
1661 * xprt_reserve - allocate an RPC request slot
1662 * @task: RPC task requesting a slot allocation
1663 *
ba60eb25
TM
1664 * If the transport is marked as being congested, or if no more
1665 * slots are available, place the task on the transport's
9903cd1c
CL
1666 * backlog queue.
1667 */
1668void xprt_reserve(struct rpc_task *task)
1da177e4 1669{
fb43d172 1670 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4 1671
43cedbf0
TM
1672 task->tk_status = 0;
1673 if (task->tk_rqstp != NULL)
1674 return;
1675
43cedbf0 1676 task->tk_status = -EAGAIN;
ba60eb25 1677 if (!xprt_throttle_congested(xprt, task))
9dc6edcf 1678 xprt_do_reserve(xprt, task);
ba60eb25
TM
1679}
1680
1681/**
1682 * xprt_retry_reserve - allocate an RPC request slot
1683 * @task: RPC task requesting a slot allocation
1684 *
1685 * If no more slots are available, place the task on the transport's
1686 * backlog queue.
1687 * Note that the only difference with xprt_reserve is that we now
1688 * ignore the value of the XPRT_CONGESTED flag.
1689 */
1690void xprt_retry_reserve(struct rpc_task *task)
1691{
fb43d172 1692 struct rpc_xprt *xprt = task->tk_xprt;
ba60eb25
TM
1693
1694 task->tk_status = 0;
1695 if (task->tk_rqstp != NULL)
1696 return;
1697
ba60eb25 1698 task->tk_status = -EAGAIN;
9dc6edcf 1699 xprt_do_reserve(xprt, task);
1da177e4
LT
1700}
1701
edc81dcd
TM
1702static void
1703xprt_request_dequeue_all(struct rpc_task *task, struct rpc_rqst *req)
1704{
1705 struct rpc_xprt *xprt = req->rq_xprt;
1706
944b0429
TM
1707 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1708 test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
edc81dcd
TM
1709 xprt_is_pinned_rqst(req)) {
1710 spin_lock(&xprt->queue_lock);
944b0429 1711 xprt_request_dequeue_transmit_locked(task);
edc81dcd
TM
1712 xprt_request_dequeue_receive_locked(task);
1713 while (xprt_is_pinned_rqst(req)) {
1714 set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1715 spin_unlock(&xprt->queue_lock);
1716 xprt_wait_on_pinned_rqst(req);
1717 spin_lock(&xprt->queue_lock);
1718 clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1719 }
1720 spin_unlock(&xprt->queue_lock);
1721 }
1722}
1723
9903cd1c
CL
1724/**
1725 * xprt_release - release an RPC request slot
1726 * @task: task which is finished with the slot
1727 *
1da177e4 1728 */
9903cd1c 1729void xprt_release(struct rpc_task *task)
1da177e4 1730{
55ae1aab 1731 struct rpc_xprt *xprt;
87ed5003 1732 struct rpc_rqst *req = task->tk_rqstp;
1da177e4 1733
87ed5003
TM
1734 if (req == NULL) {
1735 if (task->tk_client) {
fb43d172 1736 xprt = task->tk_xprt;
bd79bc57 1737 xprt_release_write(xprt, task);
87ed5003 1738 }
1da177e4 1739 return;
87ed5003 1740 }
55ae1aab 1741
55ae1aab 1742 xprt = req->rq_xprt;
0a702195
WAA
1743 if (task->tk_ops->rpc_count_stats != NULL)
1744 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1745 else if (task->tk_client)
1746 rpc_count_iostats(task, task->tk_client->cl_metrics);
edc81dcd 1747 xprt_request_dequeue_all(task, req);
4a0f8c04 1748 spin_lock_bh(&xprt->transport_lock);
49e9a890 1749 xprt->ops->release_xprt(xprt, task);
a58dd398
CL
1750 if (xprt->ops->release_request)
1751 xprt->ops->release_request(task);
1da177e4 1752 xprt->last_used = jiffies;
ad3331ac 1753 xprt_schedule_autodisconnect(xprt);
4a0f8c04 1754 spin_unlock_bh(&xprt->transport_lock);
ee5ebe85 1755 if (req->rq_buffer)
3435c74a 1756 xprt->ops->buf_free(task);
4a068258 1757 xprt_inject_disconnect(xprt);
9d96acbc 1758 xdr_free_bvec(&req->rq_rcv_buf);
0472e476 1759 xdr_free_bvec(&req->rq_snd_buf);
a17c2153
TM
1760 if (req->rq_cred != NULL)
1761 put_rpccred(req->rq_cred);
1da177e4 1762 task->tk_rqstp = NULL;
ead5e1c2
BF
1763 if (req->rq_release_snd_buf)
1764 req->rq_release_snd_buf(req);
55ae1aab 1765
46121cf7 1766 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
ee5ebe85 1767 if (likely(!bc_prealloc(req)))
a9cde23a 1768 xprt->ops->free_slot(xprt, req);
ee5ebe85 1769 else
c9acb42e 1770 xprt_free_bc_request(req);
1da177e4
LT
1771}
1772
902c5887
TM
1773#ifdef CONFIG_SUNRPC_BACKCHANNEL
1774void
1775xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
1776{
1777 struct xdr_buf *xbufp = &req->rq_snd_buf;
1778
1779 task->tk_rqstp = req;
1780 req->rq_task = task;
1781 xprt_init_connect_cookie(req, req->rq_xprt);
1782 /*
1783 * Set up the xdr_buf length.
1784 * This also indicates that the buffer is XDR encoded already.
1785 */
1786 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1787 xbufp->tail[0].iov_len;
902c5887
TM
1788}
1789#endif
1790
21de0a95 1791static void xprt_init(struct rpc_xprt *xprt, struct net *net)
c2866763 1792{
30c5116b 1793 kref_init(&xprt->kref);
c2866763
CL
1794
1795 spin_lock_init(&xprt->transport_lock);
1796 spin_lock_init(&xprt->reserve_lock);
75c84151 1797 spin_lock_init(&xprt->queue_lock);
c2866763
CL
1798
1799 INIT_LIST_HEAD(&xprt->free);
95f7691d 1800 xprt->recv_queue = RB_ROOT;
944b0429 1801 INIT_LIST_HEAD(&xprt->xmit_queue);
9e00abc3 1802#if defined(CONFIG_SUNRPC_BACKCHANNEL)
f9acac1a
RL
1803 spin_lock_init(&xprt->bc_pa_lock);
1804 INIT_LIST_HEAD(&xprt->bc_pa_list);
9e00abc3 1805#endif /* CONFIG_SUNRPC_BACKCHANNEL */
80b14d5e 1806 INIT_LIST_HEAD(&xprt->xprt_switch);
f9acac1a 1807
c2866763
CL
1808 xprt->last_used = jiffies;
1809 xprt->cwnd = RPC_INITCWND;
a509050b 1810 xprt->bind_index = 0;
c2866763
CL
1811
1812 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1813 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
79c99152 1814 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
c2866763
CL
1815 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1816
c2866763
CL
1817 xprt_init_xid(xprt);
1818
21de0a95 1819 xprt->xprt_net = get_net(net);
8d9266ff
TM
1820}
1821
1822/**
1823 * xprt_create_transport - create an RPC transport
1824 * @args: rpc transport creation arguments
1825 *
1826 */
1827struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1828{
1829 struct rpc_xprt *xprt;
1830 struct xprt_class *t;
1831
1832 spin_lock(&xprt_list_lock);
1833 list_for_each_entry(t, &xprt_list, list) {
1834 if (t->ident == args->ident) {
1835 spin_unlock(&xprt_list_lock);
1836 goto found;
1837 }
1838 }
1839 spin_unlock(&xprt_list_lock);
3c45ddf8 1840 dprintk("RPC: transport (%d) not supported\n", args->ident);
8d9266ff
TM
1841 return ERR_PTR(-EIO);
1842
1843found:
1844 xprt = t->setup(args);
1845 if (IS_ERR(xprt)) {
1846 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1847 -PTR_ERR(xprt));
21de0a95 1848 goto out;
8d9266ff 1849 }
33d90ac0
BF
1850 if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1851 xprt->idle_timeout = 0;
21de0a95
TM
1852 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1853 if (xprt_has_timer(xprt))
43123581
TM
1854 timer_setup(&xprt->timer,
1855 xprt_init_autodisconnect,
1856 TIMER_DEFERRABLE);
21de0a95 1857 else
ff861c4d 1858 timer_setup(&xprt->timer, NULL, 0);
4e0038b6
TM
1859
1860 if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1861 xprt_destroy(xprt);
1862 return ERR_PTR(-EINVAL);
1863 }
1864 xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1865 if (xprt->servername == NULL) {
1866 xprt_destroy(xprt);
1867 return ERR_PTR(-ENOMEM);
1868 }
1869
3f940098 1870 rpc_xprt_debugfs_register(xprt);
388f0c77 1871
46121cf7 1872 dprintk("RPC: created transport %p with %u slots\n", xprt,
c2866763 1873 xprt->max_reqs);
21de0a95 1874out:
c2866763
CL
1875 return xprt;
1876}
1877
528fd354
TM
1878static void xprt_destroy_cb(struct work_struct *work)
1879{
1880 struct rpc_xprt *xprt =
1881 container_of(work, struct rpc_xprt, task_cleanup);
1882
1883 rpc_xprt_debugfs_unregister(xprt);
1884 rpc_destroy_wait_queue(&xprt->binding);
1885 rpc_destroy_wait_queue(&xprt->pending);
1886 rpc_destroy_wait_queue(&xprt->sending);
1887 rpc_destroy_wait_queue(&xprt->backlog);
1888 kfree(xprt->servername);
1889 /*
1890 * Tear down transport state and free the rpc_xprt
1891 */
1892 xprt->ops->destroy(xprt);
1893}
1894
9903cd1c
CL
1895/**
1896 * xprt_destroy - destroy an RPC transport, killing off all requests.
a8de240a 1897 * @xprt: transport to destroy
9903cd1c 1898 *
1da177e4 1899 */
a8de240a 1900static void xprt_destroy(struct rpc_xprt *xprt)
1da177e4 1901{
46121cf7 1902 dprintk("RPC: destroying transport %p\n", xprt);
79234c3d 1903
528fd354
TM
1904 /*
1905 * Exclude transport connect/disconnect handlers and autoclose
1906 */
79234c3d
TM
1907 wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1908
0065db32 1909 del_timer_sync(&xprt->timer);
c8541ecd
CL
1910
1911 /*
528fd354
TM
1912 * Destroy sockets etc from the system workqueue so they can
1913 * safely flush receive work running on rpciod.
c8541ecd 1914 */
528fd354
TM
1915 INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1916 schedule_work(&xprt->task_cleanup);
6b6ca86b 1917}
1da177e4 1918
30c5116b
TM
1919static void xprt_destroy_kref(struct kref *kref)
1920{
1921 xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1922}
1923
1924/**
1925 * xprt_get - return a reference to an RPC transport.
1926 * @xprt: pointer to the transport
1927 *
1928 */
1929struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1930{
1931 if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1932 return xprt;
1933 return NULL;
1934}
1935EXPORT_SYMBOL_GPL(xprt_get);
1936
6b6ca86b
TM
1937/**
1938 * xprt_put - release a reference to an RPC transport.
1939 * @xprt: pointer to the transport
1940 *
1941 */
1942void xprt_put(struct rpc_xprt *xprt)
1943{
30c5116b
TM
1944 if (xprt != NULL)
1945 kref_put(&xprt->kref, xprt_destroy_kref);
6b6ca86b 1946}
5d252f90 1947EXPORT_SYMBOL_GPL(xprt_put);
This page took 1.1681 seconds and 4 git commands to generate.