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