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