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