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