]>
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 <[email protected]> | |
55aa4f58 CL |
36 | * |
37 | * Transport switch API copyright (C) 2005, Chuck Lever <[email protected]> | |
1da177e4 LT |
38 | */ |
39 | ||
a246b010 CL |
40 | #include <linux/module.h> |
41 | ||
1da177e4 | 42 | #include <linux/types.h> |
a246b010 | 43 | #include <linux/interrupt.h> |
1da177e4 | 44 | #include <linux/workqueue.h> |
bf3fcf89 | 45 | #include <linux/net.h> |
ff839970 | 46 | #include <linux/ktime.h> |
1da177e4 | 47 | |
a246b010 | 48 | #include <linux/sunrpc/clnt.h> |
11c556b3 | 49 | #include <linux/sunrpc/metrics.h> |
c9acb42e | 50 | #include <linux/sunrpc/bc_xprt.h> |
1da177e4 | 51 | |
55ae1aab RL |
52 | #include "sunrpc.h" |
53 | ||
1da177e4 LT |
54 | /* |
55 | * Local variables | |
56 | */ | |
57 | ||
58 | #ifdef RPC_DEBUG | |
1da177e4 LT |
59 | # define RPCDBG_FACILITY RPCDBG_XPRT |
60 | #endif | |
61 | ||
1da177e4 LT |
62 | /* |
63 | * Local functions | |
64 | */ | |
65 | static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); | |
1da177e4 | 66 | static void xprt_connect_status(struct rpc_task *task); |
1da177e4 LT |
67 | static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); |
68 | ||
5ba03e82 | 69 | static DEFINE_SPINLOCK(xprt_list_lock); |
81c098af TT |
70 | static LIST_HEAD(xprt_list); |
71 | ||
555ee3af CL |
72 | /* |
73 | * The transport code maintains an estimate on the maximum number of out- | |
74 | * standing RPC requests, using a smoothed version of the congestion | |
75 | * avoidance implemented in 44BSD. This is basically the Van Jacobson | |
76 | * congestion algorithm: If a retransmit occurs, the congestion window is | |
77 | * halved; otherwise, it is incremented by 1/cwnd when | |
78 | * | |
79 | * - a reply is received and | |
80 | * - a full number of requests are outstanding and | |
81 | * - the congestion window hasn't been updated recently. | |
82 | */ | |
83 | #define RPC_CWNDSHIFT (8U) | |
84 | #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT) | |
85 | #define RPC_INITCWND RPC_CWNDSCALE | |
86 | #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT) | |
87 | ||
88 | #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd) | |
1da177e4 | 89 | |
81c098af TT |
90 | /** |
91 | * xprt_register_transport - register a transport implementation | |
92 | * @transport: transport to register | |
93 | * | |
94 | * If a transport implementation is loaded as a kernel module, it can | |
95 | * call this interface to make itself known to the RPC client. | |
96 | * | |
97 | * Returns: | |
98 | * 0: transport successfully registered | |
99 | * -EEXIST: transport already registered | |
100 | * -EINVAL: transport module being unloaded | |
101 | */ | |
102 | int xprt_register_transport(struct xprt_class *transport) | |
103 | { | |
104 | struct xprt_class *t; | |
105 | int result; | |
106 | ||
107 | result = -EEXIST; | |
108 | spin_lock(&xprt_list_lock); | |
109 | list_for_each_entry(t, &xprt_list, list) { | |
110 | /* don't register the same transport class twice */ | |
4fa016eb | 111 | if (t->ident == transport->ident) |
81c098af TT |
112 | goto out; |
113 | } | |
114 | ||
c9f6cde6 DL |
115 | list_add_tail(&transport->list, &xprt_list); |
116 | printk(KERN_INFO "RPC: Registered %s transport module.\n", | |
117 | transport->name); | |
118 | result = 0; | |
81c098af TT |
119 | |
120 | out: | |
121 | spin_unlock(&xprt_list_lock); | |
122 | return result; | |
123 | } | |
124 | EXPORT_SYMBOL_GPL(xprt_register_transport); | |
125 | ||
126 | /** | |
127 | * xprt_unregister_transport - unregister a transport implementation | |
65b6e42c | 128 | * @transport: transport to unregister |
81c098af TT |
129 | * |
130 | * Returns: | |
131 | * 0: transport successfully unregistered | |
132 | * -ENOENT: transport never registered | |
133 | */ | |
134 | int xprt_unregister_transport(struct xprt_class *transport) | |
135 | { | |
136 | struct xprt_class *t; | |
137 | int result; | |
138 | ||
139 | result = 0; | |
140 | spin_lock(&xprt_list_lock); | |
141 | list_for_each_entry(t, &xprt_list, list) { | |
142 | if (t == transport) { | |
143 | printk(KERN_INFO | |
144 | "RPC: Unregistered %s transport module.\n", | |
145 | transport->name); | |
146 | list_del_init(&transport->list); | |
81c098af TT |
147 | goto out; |
148 | } | |
149 | } | |
150 | result = -ENOENT; | |
151 | ||
152 | out: | |
153 | spin_unlock(&xprt_list_lock); | |
154 | return result; | |
155 | } | |
156 | EXPORT_SYMBOL_GPL(xprt_unregister_transport); | |
157 | ||
441e3e24 TT |
158 | /** |
159 | * xprt_load_transport - load a transport implementation | |
160 | * @transport_name: transport to load | |
161 | * | |
162 | * Returns: | |
163 | * 0: transport successfully loaded | |
164 | * -ENOENT: transport module not available | |
165 | */ | |
166 | int xprt_load_transport(const char *transport_name) | |
167 | { | |
168 | struct xprt_class *t; | |
441e3e24 TT |
169 | int result; |
170 | ||
171 | result = 0; | |
172 | spin_lock(&xprt_list_lock); | |
173 | list_for_each_entry(t, &xprt_list, list) { | |
174 | if (strcmp(t->name, transport_name) == 0) { | |
175 | spin_unlock(&xprt_list_lock); | |
176 | goto out; | |
177 | } | |
178 | } | |
179 | spin_unlock(&xprt_list_lock); | |
ef7ffe8f | 180 | result = request_module("xprt%s", transport_name); |
441e3e24 TT |
181 | out: |
182 | return result; | |
183 | } | |
184 | EXPORT_SYMBOL_GPL(xprt_load_transport); | |
185 | ||
12a80469 CL |
186 | /** |
187 | * xprt_reserve_xprt - serialize write access to transports | |
188 | * @task: task that is requesting access to the transport | |
189 | * | |
190 | * This prevents mixing the payload of separate requests, and prevents | |
191 | * transport connects from colliding with writes. No congestion control | |
192 | * is provided. | |
193 | */ | |
194 | int xprt_reserve_xprt(struct rpc_task *task) | |
195 | { | |
12a80469 | 196 | struct rpc_rqst *req = task->tk_rqstp; |
343952fa | 197 | struct rpc_xprt *xprt = req->rq_xprt; |
12a80469 CL |
198 | |
199 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { | |
200 | if (task == xprt->snd_task) | |
201 | return 1; | |
12a80469 CL |
202 | goto out_sleep; |
203 | } | |
204 | xprt->snd_task = task; | |
205 | if (req) { | |
206 | req->rq_bytes_sent = 0; | |
207 | req->rq_ntrans++; | |
208 | } | |
209 | return 1; | |
210 | ||
211 | out_sleep: | |
46121cf7 | 212 | dprintk("RPC: %5u failed to lock transport %p\n", |
12a80469 CL |
213 | task->tk_pid, xprt); |
214 | task->tk_timeout = 0; | |
215 | task->tk_status = -EAGAIN; | |
216 | if (req && req->rq_ntrans) | |
5d00837b | 217 | rpc_sleep_on(&xprt->resend, task, NULL); |
12a80469 | 218 | else |
5d00837b | 219 | rpc_sleep_on(&xprt->sending, task, NULL); |
12a80469 CL |
220 | return 0; |
221 | } | |
12444809 | 222 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt); |
12a80469 | 223 | |
632e3bdc TM |
224 | static void xprt_clear_locked(struct rpc_xprt *xprt) |
225 | { | |
226 | xprt->snd_task = NULL; | |
227 | if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) { | |
228 | smp_mb__before_clear_bit(); | |
229 | clear_bit(XPRT_LOCKED, &xprt->state); | |
230 | smp_mb__after_clear_bit(); | |
231 | } else | |
c1384c9c | 232 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
632e3bdc TM |
233 | } |
234 | ||
1da177e4 | 235 | /* |
12a80469 CL |
236 | * xprt_reserve_xprt_cong - serialize write access to transports |
237 | * @task: task that is requesting access to the transport | |
238 | * | |
239 | * Same as xprt_reserve_xprt, but Van Jacobson congestion control is | |
240 | * integrated into the decision of whether a request is allowed to be | |
241 | * woken up and given access to the transport. | |
1da177e4 | 242 | */ |
12a80469 | 243 | int xprt_reserve_xprt_cong(struct rpc_task *task) |
1da177e4 | 244 | { |
12a80469 | 245 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 LT |
246 | struct rpc_rqst *req = task->tk_rqstp; |
247 | ||
2226feb6 | 248 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
1da177e4 LT |
249 | if (task == xprt->snd_task) |
250 | return 1; | |
1da177e4 LT |
251 | goto out_sleep; |
252 | } | |
12a80469 | 253 | if (__xprt_get_cong(xprt, task)) { |
1da177e4 LT |
254 | xprt->snd_task = task; |
255 | if (req) { | |
256 | req->rq_bytes_sent = 0; | |
257 | req->rq_ntrans++; | |
258 | } | |
259 | return 1; | |
260 | } | |
632e3bdc | 261 | xprt_clear_locked(xprt); |
1da177e4 | 262 | out_sleep: |
46121cf7 | 263 | dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); |
1da177e4 LT |
264 | task->tk_timeout = 0; |
265 | task->tk_status = -EAGAIN; | |
266 | if (req && req->rq_ntrans) | |
5d00837b | 267 | rpc_sleep_on(&xprt->resend, task, NULL); |
1da177e4 | 268 | else |
5d00837b | 269 | rpc_sleep_on(&xprt->sending, task, NULL); |
1da177e4 LT |
270 | return 0; |
271 | } | |
12444809 | 272 | EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); |
1da177e4 | 273 | |
12a80469 | 274 | static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) |
1da177e4 LT |
275 | { |
276 | int retval; | |
277 | ||
4a0f8c04 | 278 | spin_lock_bh(&xprt->transport_lock); |
12a80469 | 279 | retval = xprt->ops->reserve_xprt(task); |
4a0f8c04 | 280 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
281 | return retval; |
282 | } | |
283 | ||
12a80469 | 284 | static void __xprt_lock_write_next(struct rpc_xprt *xprt) |
49e9a890 CL |
285 | { |
286 | struct rpc_task *task; | |
287 | struct rpc_rqst *req; | |
288 | ||
289 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) | |
290 | return; | |
291 | ||
292 | task = rpc_wake_up_next(&xprt->resend); | |
293 | if (!task) { | |
294 | task = rpc_wake_up_next(&xprt->sending); | |
295 | if (!task) | |
296 | goto out_unlock; | |
297 | } | |
298 | ||
299 | req = task->tk_rqstp; | |
300 | xprt->snd_task = task; | |
301 | if (req) { | |
302 | req->rq_bytes_sent = 0; | |
303 | req->rq_ntrans++; | |
304 | } | |
305 | return; | |
306 | ||
307 | out_unlock: | |
632e3bdc | 308 | xprt_clear_locked(xprt); |
49e9a890 CL |
309 | } |
310 | ||
311 | static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) | |
1da177e4 LT |
312 | { |
313 | struct rpc_task *task; | |
314 | ||
2226feb6 | 315 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
1da177e4 | 316 | return; |
49e9a890 | 317 | if (RPCXPRT_CONGESTED(xprt)) |
1da177e4 LT |
318 | goto out_unlock; |
319 | task = rpc_wake_up_next(&xprt->resend); | |
320 | if (!task) { | |
321 | task = rpc_wake_up_next(&xprt->sending); | |
322 | if (!task) | |
323 | goto out_unlock; | |
324 | } | |
49e9a890 | 325 | if (__xprt_get_cong(xprt, task)) { |
1da177e4 LT |
326 | struct rpc_rqst *req = task->tk_rqstp; |
327 | xprt->snd_task = task; | |
328 | if (req) { | |
329 | req->rq_bytes_sent = 0; | |
330 | req->rq_ntrans++; | |
331 | } | |
332 | return; | |
333 | } | |
334 | out_unlock: | |
632e3bdc | 335 | xprt_clear_locked(xprt); |
1da177e4 LT |
336 | } |
337 | ||
49e9a890 CL |
338 | /** |
339 | * xprt_release_xprt - allow other requests to use a transport | |
340 | * @xprt: transport with other tasks potentially waiting | |
341 | * @task: task that is releasing access to the transport | |
342 | * | |
343 | * Note that "task" can be NULL. No congestion control is provided. | |
1da177e4 | 344 | */ |
49e9a890 | 345 | void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
1da177e4 LT |
346 | { |
347 | if (xprt->snd_task == task) { | |
632e3bdc | 348 | xprt_clear_locked(xprt); |
1da177e4 LT |
349 | __xprt_lock_write_next(xprt); |
350 | } | |
351 | } | |
12444809 | 352 | EXPORT_SYMBOL_GPL(xprt_release_xprt); |
1da177e4 | 353 | |
49e9a890 CL |
354 | /** |
355 | * xprt_release_xprt_cong - allow other requests to use a transport | |
356 | * @xprt: transport with other tasks potentially waiting | |
357 | * @task: task that is releasing access to the transport | |
358 | * | |
359 | * Note that "task" can be NULL. Another task is awoken to use the | |
360 | * transport if the transport's congestion window allows it. | |
361 | */ | |
362 | void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) | |
363 | { | |
364 | if (xprt->snd_task == task) { | |
632e3bdc | 365 | xprt_clear_locked(xprt); |
49e9a890 CL |
366 | __xprt_lock_write_next_cong(xprt); |
367 | } | |
368 | } | |
12444809 | 369 | EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); |
49e9a890 CL |
370 | |
371 | static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) | |
1da177e4 | 372 | { |
4a0f8c04 | 373 | spin_lock_bh(&xprt->transport_lock); |
49e9a890 | 374 | xprt->ops->release_xprt(xprt, task); |
4a0f8c04 | 375 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
376 | } |
377 | ||
1da177e4 LT |
378 | /* |
379 | * Van Jacobson congestion avoidance. Check if the congestion window | |
380 | * overflowed. Put the task to sleep if this is the case. | |
381 | */ | |
382 | static int | |
383 | __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) | |
384 | { | |
385 | struct rpc_rqst *req = task->tk_rqstp; | |
386 | ||
387 | if (req->rq_cong) | |
388 | return 1; | |
46121cf7 | 389 | dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", |
1da177e4 LT |
390 | task->tk_pid, xprt->cong, xprt->cwnd); |
391 | if (RPCXPRT_CONGESTED(xprt)) | |
392 | return 0; | |
393 | req->rq_cong = 1; | |
394 | xprt->cong += RPC_CWNDSCALE; | |
395 | return 1; | |
396 | } | |
397 | ||
398 | /* | |
399 | * Adjust the congestion window, and wake up the next task | |
400 | * that has been sleeping due to congestion | |
401 | */ | |
402 | static void | |
403 | __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) | |
404 | { | |
405 | if (!req->rq_cong) | |
406 | return; | |
407 | req->rq_cong = 0; | |
408 | xprt->cong -= RPC_CWNDSCALE; | |
49e9a890 | 409 | __xprt_lock_write_next_cong(xprt); |
1da177e4 LT |
410 | } |
411 | ||
a58dd398 CL |
412 | /** |
413 | * xprt_release_rqst_cong - housekeeping when request is complete | |
414 | * @task: RPC request that recently completed | |
415 | * | |
416 | * Useful for transports that require congestion control. | |
417 | */ | |
418 | void xprt_release_rqst_cong(struct rpc_task *task) | |
419 | { | |
420 | __xprt_put_cong(task->tk_xprt, task->tk_rqstp); | |
421 | } | |
12444809 | 422 | EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); |
a58dd398 | 423 | |
46c0ee8b CL |
424 | /** |
425 | * xprt_adjust_cwnd - adjust transport congestion window | |
426 | * @task: recently completed RPC request used to adjust window | |
427 | * @result: result code of completed RPC request | |
428 | * | |
1da177e4 LT |
429 | * We use a time-smoothed congestion estimator to avoid heavy oscillation. |
430 | */ | |
46c0ee8b | 431 | void xprt_adjust_cwnd(struct rpc_task *task, int result) |
1da177e4 | 432 | { |
46c0ee8b CL |
433 | struct rpc_rqst *req = task->tk_rqstp; |
434 | struct rpc_xprt *xprt = task->tk_xprt; | |
435 | unsigned long cwnd = xprt->cwnd; | |
1da177e4 | 436 | |
1da177e4 LT |
437 | if (result >= 0 && cwnd <= xprt->cong) { |
438 | /* The (cwnd >> 1) term makes sure | |
439 | * the result gets rounded properly. */ | |
440 | cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; | |
441 | if (cwnd > RPC_MAXCWND(xprt)) | |
442 | cwnd = RPC_MAXCWND(xprt); | |
49e9a890 | 443 | __xprt_lock_write_next_cong(xprt); |
1da177e4 LT |
444 | } else if (result == -ETIMEDOUT) { |
445 | cwnd >>= 1; | |
446 | if (cwnd < RPC_CWNDSCALE) | |
447 | cwnd = RPC_CWNDSCALE; | |
448 | } | |
46121cf7 | 449 | dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", |
1da177e4 LT |
450 | xprt->cong, xprt->cwnd, cwnd); |
451 | xprt->cwnd = cwnd; | |
46c0ee8b | 452 | __xprt_put_cong(xprt, req); |
1da177e4 | 453 | } |
12444809 | 454 | EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); |
1da177e4 | 455 | |
44fbac22 CL |
456 | /** |
457 | * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue | |
458 | * @xprt: transport with waiting tasks | |
459 | * @status: result code to plant in each task before waking it | |
460 | * | |
461 | */ | |
462 | void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) | |
463 | { | |
464 | if (status < 0) | |
465 | rpc_wake_up_status(&xprt->pending, status); | |
466 | else | |
467 | rpc_wake_up(&xprt->pending); | |
468 | } | |
12444809 | 469 | EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); |
44fbac22 | 470 | |
c7b2cae8 CL |
471 | /** |
472 | * xprt_wait_for_buffer_space - wait for transport output buffer to clear | |
473 | * @task: task to be put to sleep | |
0b80ae42 | 474 | * @action: function pointer to be executed after wait |
c7b2cae8 | 475 | */ |
b6ddf64f | 476 | void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) |
c7b2cae8 CL |
477 | { |
478 | struct rpc_rqst *req = task->tk_rqstp; | |
479 | struct rpc_xprt *xprt = req->rq_xprt; | |
480 | ||
481 | task->tk_timeout = req->rq_timeout; | |
b6ddf64f | 482 | rpc_sleep_on(&xprt->pending, task, action); |
c7b2cae8 | 483 | } |
12444809 | 484 | EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); |
c7b2cae8 CL |
485 | |
486 | /** | |
487 | * xprt_write_space - wake the task waiting for transport output buffer space | |
488 | * @xprt: transport with waiting tasks | |
489 | * | |
490 | * Can be called in a soft IRQ context, so xprt_write_space never sleeps. | |
491 | */ | |
492 | void xprt_write_space(struct rpc_xprt *xprt) | |
493 | { | |
494 | if (unlikely(xprt->shutdown)) | |
495 | return; | |
496 | ||
497 | spin_lock_bh(&xprt->transport_lock); | |
498 | if (xprt->snd_task) { | |
46121cf7 CL |
499 | dprintk("RPC: write space: waking waiting task on " |
500 | "xprt %p\n", xprt); | |
fda13939 | 501 | rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); |
c7b2cae8 CL |
502 | } |
503 | spin_unlock_bh(&xprt->transport_lock); | |
504 | } | |
12444809 | 505 | EXPORT_SYMBOL_GPL(xprt_write_space); |
c7b2cae8 | 506 | |
fe3aca29 CL |
507 | /** |
508 | * xprt_set_retrans_timeout_def - set a request's retransmit timeout | |
509 | * @task: task whose timeout is to be set | |
510 | * | |
511 | * Set a request's retransmit timeout based on the transport's | |
512 | * default timeout parameters. Used by transports that don't adjust | |
513 | * the retransmit timeout based on round-trip time estimation. | |
514 | */ | |
515 | void xprt_set_retrans_timeout_def(struct rpc_task *task) | |
516 | { | |
517 | task->tk_timeout = task->tk_rqstp->rq_timeout; | |
518 | } | |
12444809 | 519 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); |
fe3aca29 CL |
520 | |
521 | /* | |
522 | * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout | |
523 | * @task: task whose timeout is to be set | |
cca5172a | 524 | * |
fe3aca29 CL |
525 | * Set a request's retransmit timeout using the RTT estimator. |
526 | */ | |
527 | void xprt_set_retrans_timeout_rtt(struct rpc_task *task) | |
528 | { | |
529 | int timer = task->tk_msg.rpc_proc->p_timer; | |
ba7392bb TM |
530 | struct rpc_clnt *clnt = task->tk_client; |
531 | struct rpc_rtt *rtt = clnt->cl_rtt; | |
fe3aca29 | 532 | struct rpc_rqst *req = task->tk_rqstp; |
ba7392bb | 533 | unsigned long max_timeout = clnt->cl_timeout->to_maxval; |
fe3aca29 CL |
534 | |
535 | task->tk_timeout = rpc_calc_rto(rtt, timer); | |
536 | task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; | |
537 | if (task->tk_timeout > max_timeout || task->tk_timeout == 0) | |
538 | task->tk_timeout = max_timeout; | |
539 | } | |
12444809 | 540 | EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); |
fe3aca29 | 541 | |
1da177e4 LT |
542 | static void xprt_reset_majortimeo(struct rpc_rqst *req) |
543 | { | |
ba7392bb | 544 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
1da177e4 LT |
545 | |
546 | req->rq_majortimeo = req->rq_timeout; | |
547 | if (to->to_exponential) | |
548 | req->rq_majortimeo <<= to->to_retries; | |
549 | else | |
550 | req->rq_majortimeo += to->to_increment * to->to_retries; | |
551 | if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) | |
552 | req->rq_majortimeo = to->to_maxval; | |
553 | req->rq_majortimeo += jiffies; | |
554 | } | |
555 | ||
9903cd1c CL |
556 | /** |
557 | * xprt_adjust_timeout - adjust timeout values for next retransmit | |
558 | * @req: RPC request containing parameters to use for the adjustment | |
559 | * | |
1da177e4 LT |
560 | */ |
561 | int xprt_adjust_timeout(struct rpc_rqst *req) | |
562 | { | |
563 | struct rpc_xprt *xprt = req->rq_xprt; | |
ba7392bb | 564 | const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; |
1da177e4 LT |
565 | int status = 0; |
566 | ||
567 | if (time_before(jiffies, req->rq_majortimeo)) { | |
568 | if (to->to_exponential) | |
569 | req->rq_timeout <<= 1; | |
570 | else | |
571 | req->rq_timeout += to->to_increment; | |
572 | if (to->to_maxval && req->rq_timeout >= to->to_maxval) | |
573 | req->rq_timeout = to->to_maxval; | |
574 | req->rq_retries++; | |
1da177e4 LT |
575 | } else { |
576 | req->rq_timeout = to->to_initval; | |
577 | req->rq_retries = 0; | |
578 | xprt_reset_majortimeo(req); | |
579 | /* Reset the RTT counters == "slow start" */ | |
4a0f8c04 | 580 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 | 581 | rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); |
4a0f8c04 | 582 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
583 | status = -ETIMEDOUT; |
584 | } | |
585 | ||
586 | if (req->rq_timeout == 0) { | |
587 | printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); | |
588 | req->rq_timeout = 5 * HZ; | |
589 | } | |
590 | return status; | |
591 | } | |
592 | ||
65f27f38 | 593 | static void xprt_autoclose(struct work_struct *work) |
1da177e4 | 594 | { |
65f27f38 DH |
595 | struct rpc_xprt *xprt = |
596 | container_of(work, struct rpc_xprt, task_cleanup); | |
1da177e4 | 597 | |
a246b010 | 598 | xprt->ops->close(xprt); |
66af1e55 | 599 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
1da177e4 LT |
600 | xprt_release_write(xprt, NULL); |
601 | } | |
602 | ||
9903cd1c | 603 | /** |
62da3b24 | 604 | * xprt_disconnect_done - mark a transport as disconnected |
9903cd1c CL |
605 | * @xprt: transport to flag for disconnect |
606 | * | |
1da177e4 | 607 | */ |
62da3b24 | 608 | void xprt_disconnect_done(struct rpc_xprt *xprt) |
1da177e4 | 609 | { |
46121cf7 | 610 | dprintk("RPC: disconnected transport %p\n", xprt); |
4a0f8c04 | 611 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 | 612 | xprt_clear_connected(xprt); |
2a491991 | 613 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
4a0f8c04 | 614 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 | 615 | } |
62da3b24 | 616 | EXPORT_SYMBOL_GPL(xprt_disconnect_done); |
1da177e4 | 617 | |
66af1e55 TM |
618 | /** |
619 | * xprt_force_disconnect - force a transport to disconnect | |
620 | * @xprt: transport to disconnect | |
621 | * | |
622 | */ | |
623 | void xprt_force_disconnect(struct rpc_xprt *xprt) | |
624 | { | |
625 | /* Don't race with the test_bit() in xprt_clear_locked() */ | |
626 | spin_lock_bh(&xprt->transport_lock); | |
627 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); | |
628 | /* Try to schedule an autoclose RPC call */ | |
629 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) | |
630 | queue_work(rpciod_workqueue, &xprt->task_cleanup); | |
2a491991 | 631 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
66af1e55 TM |
632 | spin_unlock_bh(&xprt->transport_lock); |
633 | } | |
66af1e55 | 634 | |
7c1d71cf TM |
635 | /** |
636 | * xprt_conditional_disconnect - force a transport to disconnect | |
637 | * @xprt: transport to disconnect | |
638 | * @cookie: 'connection cookie' | |
639 | * | |
640 | * This attempts to break the connection if and only if 'cookie' matches | |
641 | * the current transport 'connection cookie'. It ensures that we don't | |
642 | * try to break the connection more than once when we need to retransmit | |
643 | * a batch of RPC requests. | |
644 | * | |
645 | */ | |
646 | void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) | |
647 | { | |
648 | /* Don't race with the test_bit() in xprt_clear_locked() */ | |
649 | spin_lock_bh(&xprt->transport_lock); | |
650 | if (cookie != xprt->connect_cookie) | |
651 | goto out; | |
652 | if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) | |
653 | goto out; | |
654 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); | |
655 | /* Try to schedule an autoclose RPC call */ | |
656 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) | |
657 | queue_work(rpciod_workqueue, &xprt->task_cleanup); | |
2a491991 | 658 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
7c1d71cf TM |
659 | out: |
660 | spin_unlock_bh(&xprt->transport_lock); | |
661 | } | |
662 | ||
1da177e4 LT |
663 | static void |
664 | xprt_init_autodisconnect(unsigned long data) | |
665 | { | |
666 | struct rpc_xprt *xprt = (struct rpc_xprt *)data; | |
667 | ||
4a0f8c04 | 668 | spin_lock(&xprt->transport_lock); |
1da177e4 LT |
669 | if (!list_empty(&xprt->recv) || xprt->shutdown) |
670 | goto out_abort; | |
2226feb6 | 671 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
1da177e4 | 672 | goto out_abort; |
4a0f8c04 | 673 | spin_unlock(&xprt->transport_lock); |
f75e6745 TM |
674 | set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); |
675 | queue_work(rpciod_workqueue, &xprt->task_cleanup); | |
1da177e4 LT |
676 | return; |
677 | out_abort: | |
4a0f8c04 | 678 | spin_unlock(&xprt->transport_lock); |
1da177e4 LT |
679 | } |
680 | ||
9903cd1c CL |
681 | /** |
682 | * xprt_connect - schedule a transport connect operation | |
683 | * @task: RPC task that is requesting the connect | |
1da177e4 LT |
684 | * |
685 | */ | |
686 | void xprt_connect(struct rpc_task *task) | |
687 | { | |
688 | struct rpc_xprt *xprt = task->tk_xprt; | |
689 | ||
46121cf7 | 690 | dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, |
1da177e4 LT |
691 | xprt, (xprt_connected(xprt) ? "is" : "is not")); |
692 | ||
ec739ef0 | 693 | if (!xprt_bound(xprt)) { |
01d37c42 | 694 | task->tk_status = -EAGAIN; |
1da177e4 LT |
695 | return; |
696 | } | |
697 | if (!xprt_lock_write(xprt, task)) | |
698 | return; | |
feb8ca37 TM |
699 | |
700 | if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) | |
701 | xprt->ops->close(xprt); | |
702 | ||
1da177e4 | 703 | if (xprt_connected(xprt)) |
a246b010 CL |
704 | xprt_release_write(xprt, task); |
705 | else { | |
706 | if (task->tk_rqstp) | |
707 | task->tk_rqstp->rq_bytes_sent = 0; | |
1da177e4 | 708 | |
a8ce4a8f | 709 | task->tk_timeout = task->tk_rqstp->rq_timeout; |
5d00837b | 710 | rpc_sleep_on(&xprt->pending, task, xprt_connect_status); |
0b9e7943 TM |
711 | |
712 | if (test_bit(XPRT_CLOSING, &xprt->state)) | |
713 | return; | |
714 | if (xprt_test_and_set_connecting(xprt)) | |
715 | return; | |
262ca07d | 716 | xprt->stat.connect_start = jiffies; |
a246b010 | 717 | xprt->ops->connect(task); |
1da177e4 | 718 | } |
1da177e4 LT |
719 | } |
720 | ||
9903cd1c | 721 | static void xprt_connect_status(struct rpc_task *task) |
1da177e4 LT |
722 | { |
723 | struct rpc_xprt *xprt = task->tk_xprt; | |
724 | ||
cd983ef8 | 725 | if (task->tk_status == 0) { |
262ca07d CL |
726 | xprt->stat.connect_count++; |
727 | xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; | |
46121cf7 | 728 | dprintk("RPC: %5u xprt_connect_status: connection established\n", |
1da177e4 LT |
729 | task->tk_pid); |
730 | return; | |
731 | } | |
732 | ||
1da177e4 | 733 | switch (task->tk_status) { |
2a491991 TM |
734 | case -EAGAIN: |
735 | dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); | |
23475d66 | 736 | break; |
1da177e4 | 737 | case -ETIMEDOUT: |
46121cf7 CL |
738 | dprintk("RPC: %5u xprt_connect_status: connect attempt timed " |
739 | "out\n", task->tk_pid); | |
1da177e4 LT |
740 | break; |
741 | default: | |
46121cf7 CL |
742 | dprintk("RPC: %5u xprt_connect_status: error %d connecting to " |
743 | "server %s\n", task->tk_pid, -task->tk_status, | |
744 | task->tk_client->cl_server); | |
23475d66 CL |
745 | xprt_release_write(xprt, task); |
746 | task->tk_status = -EIO; | |
1da177e4 | 747 | } |
1da177e4 LT |
748 | } |
749 | ||
9903cd1c CL |
750 | /** |
751 | * xprt_lookup_rqst - find an RPC request corresponding to an XID | |
752 | * @xprt: transport on which the original request was transmitted | |
753 | * @xid: RPC XID of incoming reply | |
754 | * | |
1da177e4 | 755 | */ |
d8ed029d | 756 | struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) |
1da177e4 LT |
757 | { |
758 | struct list_head *pos; | |
1da177e4 LT |
759 | |
760 | list_for_each(pos, &xprt->recv) { | |
761 | struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list); | |
262ca07d CL |
762 | if (entry->rq_xid == xid) |
763 | return entry; | |
1da177e4 | 764 | } |
46121cf7 CL |
765 | |
766 | dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", | |
767 | ntohl(xid)); | |
262ca07d CL |
768 | xprt->stat.bad_xids++; |
769 | return NULL; | |
1da177e4 | 770 | } |
12444809 | 771 | EXPORT_SYMBOL_GPL(xprt_lookup_rqst); |
1da177e4 | 772 | |
bbc72cea | 773 | static void xprt_update_rtt(struct rpc_task *task) |
1570c1e4 CL |
774 | { |
775 | struct rpc_rqst *req = task->tk_rqstp; | |
776 | struct rpc_rtt *rtt = task->tk_client->cl_rtt; | |
777 | unsigned timer = task->tk_msg.rpc_proc->p_timer; | |
d60dbb20 | 778 | long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); |
1570c1e4 CL |
779 | |
780 | if (timer) { | |
781 | if (req->rq_ntrans == 1) | |
ff839970 | 782 | rpc_update_rtt(rtt, timer, m); |
1570c1e4 CL |
783 | rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); |
784 | } | |
785 | } | |
786 | ||
9903cd1c CL |
787 | /** |
788 | * xprt_complete_rqst - called when reply processing is complete | |
1570c1e4 | 789 | * @task: RPC request that recently completed |
9903cd1c CL |
790 | * @copied: actual number of bytes received from the transport |
791 | * | |
1570c1e4 | 792 | * Caller holds transport lock. |
1da177e4 | 793 | */ |
1570c1e4 | 794 | void xprt_complete_rqst(struct rpc_task *task, int copied) |
1da177e4 | 795 | { |
1570c1e4 | 796 | struct rpc_rqst *req = task->tk_rqstp; |
fda13939 | 797 | struct rpc_xprt *xprt = req->rq_xprt; |
1da177e4 | 798 | |
1570c1e4 CL |
799 | dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", |
800 | task->tk_pid, ntohl(req->rq_xid), copied); | |
1da177e4 | 801 | |
fda13939 | 802 | xprt->stat.recvs++; |
d60dbb20 | 803 | req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); |
bbc72cea CL |
804 | if (xprt->ops->timer != NULL) |
805 | xprt_update_rtt(task); | |
ef759a2e | 806 | |
1da177e4 | 807 | list_del_init(&req->rq_list); |
1e799b67 | 808 | req->rq_private_buf.len = copied; |
dd2b63d0 RL |
809 | /* Ensure all writes are done before we update */ |
810 | /* req->rq_reply_bytes_recvd */ | |
43ac3f29 | 811 | smp_wmb(); |
dd2b63d0 | 812 | req->rq_reply_bytes_recvd = copied; |
fda13939 | 813 | rpc_wake_up_queued_task(&xprt->pending, task); |
1da177e4 | 814 | } |
12444809 | 815 | EXPORT_SYMBOL_GPL(xprt_complete_rqst); |
1da177e4 | 816 | |
46c0ee8b | 817 | static void xprt_timer(struct rpc_task *task) |
1da177e4 | 818 | { |
46c0ee8b | 819 | struct rpc_rqst *req = task->tk_rqstp; |
1da177e4 LT |
820 | struct rpc_xprt *xprt = req->rq_xprt; |
821 | ||
5d00837b TM |
822 | if (task->tk_status != -ETIMEDOUT) |
823 | return; | |
46121cf7 | 824 | dprintk("RPC: %5u xprt_timer\n", task->tk_pid); |
1da177e4 | 825 | |
5d00837b | 826 | spin_lock_bh(&xprt->transport_lock); |
dd2b63d0 | 827 | if (!req->rq_reply_bytes_recvd) { |
46c0ee8b CL |
828 | if (xprt->ops->timer) |
829 | xprt->ops->timer(task); | |
5d00837b TM |
830 | } else |
831 | task->tk_status = 0; | |
832 | spin_unlock_bh(&xprt->transport_lock); | |
1da177e4 LT |
833 | } |
834 | ||
4cfc7e60 RI |
835 | static inline int xprt_has_timer(struct rpc_xprt *xprt) |
836 | { | |
837 | return xprt->idle_timeout != 0; | |
838 | } | |
839 | ||
9903cd1c CL |
840 | /** |
841 | * xprt_prepare_transmit - reserve the transport before sending a request | |
842 | * @task: RPC task about to send a request | |
843 | * | |
1da177e4 | 844 | */ |
9903cd1c | 845 | int xprt_prepare_transmit(struct rpc_task *task) |
1da177e4 LT |
846 | { |
847 | struct rpc_rqst *req = task->tk_rqstp; | |
848 | struct rpc_xprt *xprt = req->rq_xprt; | |
849 | int err = 0; | |
850 | ||
46121cf7 | 851 | dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); |
1da177e4 | 852 | |
4a0f8c04 | 853 | spin_lock_bh(&xprt->transport_lock); |
dd2b63d0 RL |
854 | if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) { |
855 | err = req->rq_reply_bytes_recvd; | |
1da177e4 LT |
856 | goto out_unlock; |
857 | } | |
2a491991 | 858 | if (!xprt->ops->reserve_xprt(task)) |
1da177e4 | 859 | err = -EAGAIN; |
1da177e4 | 860 | out_unlock: |
4a0f8c04 | 861 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
862 | return err; |
863 | } | |
864 | ||
e0ab53de | 865 | void xprt_end_transmit(struct rpc_task *task) |
5e5ce5be | 866 | { |
343952fa | 867 | xprt_release_write(task->tk_rqstp->rq_xprt, task); |
5e5ce5be TM |
868 | } |
869 | ||
9903cd1c CL |
870 | /** |
871 | * xprt_transmit - send an RPC request on a transport | |
872 | * @task: controlling RPC task | |
873 | * | |
874 | * We have to copy the iovec because sendmsg fiddles with its contents. | |
875 | */ | |
876 | void xprt_transmit(struct rpc_task *task) | |
1da177e4 | 877 | { |
1da177e4 LT |
878 | struct rpc_rqst *req = task->tk_rqstp; |
879 | struct rpc_xprt *xprt = req->rq_xprt; | |
a246b010 | 880 | int status; |
1da177e4 | 881 | |
46121cf7 | 882 | dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); |
1da177e4 | 883 | |
dd2b63d0 | 884 | if (!req->rq_reply_bytes_recvd) { |
55ae1aab RL |
885 | if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { |
886 | /* | |
887 | * Add to the list only if we're expecting a reply | |
888 | */ | |
4a0f8c04 | 889 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 LT |
890 | /* Update the softirq receive buffer */ |
891 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, | |
892 | sizeof(req->rq_private_buf)); | |
893 | /* Add request to the receive list */ | |
894 | list_add_tail(&req->rq_list, &xprt->recv); | |
4a0f8c04 | 895 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 | 896 | xprt_reset_majortimeo(req); |
0f9dc2b1 TM |
897 | /* Turn off autodisconnect */ |
898 | del_singleshot_timer_sync(&xprt->timer); | |
1da177e4 LT |
899 | } |
900 | } else if (!req->rq_bytes_sent) | |
901 | return; | |
902 | ||
7c1d71cf | 903 | req->rq_connect_cookie = xprt->connect_cookie; |
ff839970 | 904 | req->rq_xtime = ktime_get(); |
a246b010 | 905 | status = xprt->ops->send_request(task); |
c8485e4d TM |
906 | if (status != 0) { |
907 | task->tk_status = status; | |
908 | return; | |
909 | } | |
262ca07d | 910 | |
c8485e4d TM |
911 | dprintk("RPC: %5u xmit complete\n", task->tk_pid); |
912 | spin_lock_bh(&xprt->transport_lock); | |
262ca07d | 913 | |
c8485e4d | 914 | xprt->ops->set_retrans_timeout(task); |
262ca07d | 915 | |
c8485e4d TM |
916 | xprt->stat.sends++; |
917 | xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; | |
918 | xprt->stat.bklog_u += xprt->backlog.qlen; | |
1da177e4 | 919 | |
c8485e4d TM |
920 | /* Don't race with disconnect */ |
921 | if (!xprt_connected(xprt)) | |
922 | task->tk_status = -ENOTCONN; | |
dd2b63d0 | 923 | else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) { |
55ae1aab RL |
924 | /* |
925 | * Sleep on the pending queue since | |
926 | * we're expecting a reply. | |
927 | */ | |
c8485e4d | 928 | rpc_sleep_on(&xprt->pending, task, xprt_timer); |
55ae1aab | 929 | } |
c8485e4d | 930 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
931 | } |
932 | ||
ee5ebe85 | 933 | static void xprt_alloc_slot(struct rpc_task *task) |
1da177e4 LT |
934 | { |
935 | struct rpc_xprt *xprt = task->tk_xprt; | |
936 | ||
937 | task->tk_status = 0; | |
938 | if (task->tk_rqstp) | |
939 | return; | |
940 | if (!list_empty(&xprt->free)) { | |
941 | struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); | |
942 | list_del_init(&req->rq_list); | |
943 | task->tk_rqstp = req; | |
944 | xprt_request_init(task, xprt); | |
945 | return; | |
946 | } | |
46121cf7 | 947 | dprintk("RPC: waiting for request slot\n"); |
1da177e4 LT |
948 | task->tk_status = -EAGAIN; |
949 | task->tk_timeout = 0; | |
5d00837b | 950 | rpc_sleep_on(&xprt->backlog, task, NULL); |
1da177e4 LT |
951 | } |
952 | ||
ee5ebe85 TM |
953 | static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) |
954 | { | |
955 | memset(req, 0, sizeof(*req)); /* mark unused */ | |
956 | ||
957 | spin_lock(&xprt->reserve_lock); | |
958 | list_add(&req->rq_list, &xprt->free); | |
959 | rpc_wake_up_next(&xprt->backlog); | |
960 | spin_unlock(&xprt->reserve_lock); | |
961 | } | |
962 | ||
37aa2133 | 963 | struct rpc_xprt *xprt_alloc(struct net *net, int size, int max_req) |
bd1722d4 PE |
964 | { |
965 | struct rpc_xprt *xprt; | |
966 | ||
967 | xprt = kzalloc(size, GFP_KERNEL); | |
968 | if (xprt == NULL) | |
969 | goto out; | |
970 | ||
971 | xprt->max_reqs = max_req; | |
972 | xprt->slot = kcalloc(max_req, sizeof(struct rpc_rqst), GFP_KERNEL); | |
973 | if (xprt->slot == NULL) | |
974 | goto out_free; | |
975 | ||
37aa2133 | 976 | xprt->xprt_net = get_net(net); |
bd1722d4 PE |
977 | return xprt; |
978 | ||
979 | out_free: | |
980 | kfree(xprt); | |
981 | out: | |
982 | return NULL; | |
983 | } | |
984 | EXPORT_SYMBOL_GPL(xprt_alloc); | |
985 | ||
e204e621 PE |
986 | void xprt_free(struct rpc_xprt *xprt) |
987 | { | |
37aa2133 | 988 | put_net(xprt->xprt_net); |
e204e621 PE |
989 | kfree(xprt->slot); |
990 | kfree(xprt); | |
991 | } | |
992 | EXPORT_SYMBOL_GPL(xprt_free); | |
993 | ||
9903cd1c CL |
994 | /** |
995 | * xprt_reserve - allocate an RPC request slot | |
996 | * @task: RPC task requesting a slot allocation | |
997 | * | |
998 | * If no more slots are available, place the task on the transport's | |
999 | * backlog queue. | |
1000 | */ | |
1001 | void xprt_reserve(struct rpc_task *task) | |
1da177e4 LT |
1002 | { |
1003 | struct rpc_xprt *xprt = task->tk_xprt; | |
1004 | ||
1005 | task->tk_status = -EIO; | |
0065db32 | 1006 | spin_lock(&xprt->reserve_lock); |
ee5ebe85 | 1007 | xprt_alloc_slot(task); |
0065db32 | 1008 | spin_unlock(&xprt->reserve_lock); |
1da177e4 LT |
1009 | } |
1010 | ||
d8ed029d | 1011 | static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) |
1da177e4 | 1012 | { |
0eae88f3 | 1013 | return (__force __be32)xprt->xid++; |
1da177e4 LT |
1014 | } |
1015 | ||
1016 | static inline void xprt_init_xid(struct rpc_xprt *xprt) | |
1017 | { | |
bf3fcf89 | 1018 | xprt->xid = net_random(); |
1da177e4 LT |
1019 | } |
1020 | ||
9903cd1c | 1021 | static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) |
1da177e4 LT |
1022 | { |
1023 | struct rpc_rqst *req = task->tk_rqstp; | |
1024 | ||
ba7392bb | 1025 | req->rq_timeout = task->tk_client->cl_timeout->to_initval; |
1da177e4 LT |
1026 | req->rq_task = task; |
1027 | req->rq_xprt = xprt; | |
02107148 | 1028 | req->rq_buffer = NULL; |
1da177e4 | 1029 | req->rq_xid = xprt_alloc_xid(xprt); |
ead5e1c2 | 1030 | req->rq_release_snd_buf = NULL; |
da45828e | 1031 | xprt_reset_majortimeo(req); |
46121cf7 | 1032 | dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, |
1da177e4 LT |
1033 | req, ntohl(req->rq_xid)); |
1034 | } | |
1035 | ||
9903cd1c CL |
1036 | /** |
1037 | * xprt_release - release an RPC request slot | |
1038 | * @task: task which is finished with the slot | |
1039 | * | |
1da177e4 | 1040 | */ |
9903cd1c | 1041 | void xprt_release(struct rpc_task *task) |
1da177e4 | 1042 | { |
55ae1aab | 1043 | struct rpc_xprt *xprt; |
1da177e4 LT |
1044 | struct rpc_rqst *req; |
1045 | ||
1046 | if (!(req = task->tk_rqstp)) | |
1047 | return; | |
55ae1aab | 1048 | |
55ae1aab | 1049 | xprt = req->rq_xprt; |
11c556b3 | 1050 | rpc_count_iostats(task); |
4a0f8c04 | 1051 | spin_lock_bh(&xprt->transport_lock); |
49e9a890 | 1052 | xprt->ops->release_xprt(xprt, task); |
a58dd398 CL |
1053 | if (xprt->ops->release_request) |
1054 | xprt->ops->release_request(task); | |
1da177e4 LT |
1055 | if (!list_empty(&req->rq_list)) |
1056 | list_del(&req->rq_list); | |
1057 | xprt->last_used = jiffies; | |
4cfc7e60 | 1058 | if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) |
a246b010 | 1059 | mod_timer(&xprt->timer, |
03bf4b70 | 1060 | xprt->last_used + xprt->idle_timeout); |
4a0f8c04 | 1061 | spin_unlock_bh(&xprt->transport_lock); |
ee5ebe85 | 1062 | if (req->rq_buffer) |
55ae1aab | 1063 | xprt->ops->buf_free(req->rq_buffer); |
a17c2153 TM |
1064 | if (req->rq_cred != NULL) |
1065 | put_rpccred(req->rq_cred); | |
1da177e4 | 1066 | task->tk_rqstp = NULL; |
ead5e1c2 BF |
1067 | if (req->rq_release_snd_buf) |
1068 | req->rq_release_snd_buf(req); | |
55ae1aab | 1069 | |
46121cf7 | 1070 | dprintk("RPC: %5u release request %p\n", task->tk_pid, req); |
ee5ebe85 TM |
1071 | if (likely(!bc_prealloc(req))) |
1072 | xprt_free_slot(xprt, req); | |
1073 | else | |
c9acb42e | 1074 | xprt_free_bc_request(req); |
1da177e4 LT |
1075 | } |
1076 | ||
c2866763 CL |
1077 | /** |
1078 | * xprt_create_transport - create an RPC transport | |
96802a09 | 1079 | * @args: rpc transport creation arguments |
c2866763 CL |
1080 | * |
1081 | */ | |
3c341b0b | 1082 | struct rpc_xprt *xprt_create_transport(struct xprt_create *args) |
c2866763 | 1083 | { |
c2866763 CL |
1084 | struct rpc_xprt *xprt; |
1085 | struct rpc_rqst *req; | |
bc25571e | 1086 | struct xprt_class *t; |
c2866763 | 1087 | |
bc25571e TT |
1088 | spin_lock(&xprt_list_lock); |
1089 | list_for_each_entry(t, &xprt_list, list) { | |
4fa016eb | 1090 | if (t->ident == args->ident) { |
bc25571e TT |
1091 | spin_unlock(&xprt_list_lock); |
1092 | goto found; | |
1093 | } | |
c2866763 | 1094 | } |
bc25571e | 1095 | spin_unlock(&xprt_list_lock); |
4fa016eb | 1096 | printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident); |
bc25571e TT |
1097 | return ERR_PTR(-EIO); |
1098 | ||
1099 | found: | |
1100 | xprt = t->setup(args); | |
c8541ecd | 1101 | if (IS_ERR(xprt)) { |
46121cf7 | 1102 | dprintk("RPC: xprt_create_transport: failed, %ld\n", |
c8541ecd CL |
1103 | -PTR_ERR(xprt)); |
1104 | return xprt; | |
c2866763 CL |
1105 | } |
1106 | ||
6b6ca86b | 1107 | kref_init(&xprt->kref); |
c2866763 CL |
1108 | spin_lock_init(&xprt->transport_lock); |
1109 | spin_lock_init(&xprt->reserve_lock); | |
1110 | ||
1111 | INIT_LIST_HEAD(&xprt->free); | |
1112 | INIT_LIST_HEAD(&xprt->recv); | |
f9acac1a RL |
1113 | #if defined(CONFIG_NFS_V4_1) |
1114 | spin_lock_init(&xprt->bc_pa_lock); | |
1115 | INIT_LIST_HEAD(&xprt->bc_pa_list); | |
1116 | #endif /* CONFIG_NFS_V4_1 */ | |
1117 | ||
65f27f38 | 1118 | INIT_WORK(&xprt->task_cleanup, xprt_autoclose); |
4cfc7e60 RI |
1119 | if (xprt_has_timer(xprt)) |
1120 | setup_timer(&xprt->timer, xprt_init_autodisconnect, | |
1121 | (unsigned long)xprt); | |
1122 | else | |
1123 | init_timer(&xprt->timer); | |
c2866763 CL |
1124 | xprt->last_used = jiffies; |
1125 | xprt->cwnd = RPC_INITCWND; | |
a509050b | 1126 | xprt->bind_index = 0; |
c2866763 CL |
1127 | |
1128 | rpc_init_wait_queue(&xprt->binding, "xprt_binding"); | |
1129 | rpc_init_wait_queue(&xprt->pending, "xprt_pending"); | |
1130 | rpc_init_wait_queue(&xprt->sending, "xprt_sending"); | |
1131 | rpc_init_wait_queue(&xprt->resend, "xprt_resend"); | |
1132 | rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); | |
1133 | ||
1134 | /* initialize free list */ | |
1135 | for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) | |
1136 | list_add(&req->rq_list, &xprt->free); | |
1137 | ||
1138 | xprt_init_xid(xprt); | |
1139 | ||
46121cf7 | 1140 | dprintk("RPC: created transport %p with %u slots\n", xprt, |
c2866763 | 1141 | xprt->max_reqs); |
c2866763 CL |
1142 | return xprt; |
1143 | } | |
1144 | ||
9903cd1c CL |
1145 | /** |
1146 | * xprt_destroy - destroy an RPC transport, killing off all requests. | |
6b6ca86b | 1147 | * @kref: kref for the transport to destroy |
9903cd1c | 1148 | * |
1da177e4 | 1149 | */ |
6b6ca86b | 1150 | static void xprt_destroy(struct kref *kref) |
1da177e4 | 1151 | { |
6b6ca86b TM |
1152 | struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref); |
1153 | ||
46121cf7 | 1154 | dprintk("RPC: destroying transport %p\n", xprt); |
0065db32 TM |
1155 | xprt->shutdown = 1; |
1156 | del_timer_sync(&xprt->timer); | |
c8541ecd | 1157 | |
f6a1cc89 TM |
1158 | rpc_destroy_wait_queue(&xprt->binding); |
1159 | rpc_destroy_wait_queue(&xprt->pending); | |
1160 | rpc_destroy_wait_queue(&xprt->sending); | |
1161 | rpc_destroy_wait_queue(&xprt->resend); | |
1162 | rpc_destroy_wait_queue(&xprt->backlog); | |
c3ae62ae | 1163 | cancel_work_sync(&xprt->task_cleanup); |
c8541ecd CL |
1164 | /* |
1165 | * Tear down transport state and free the rpc_xprt | |
1166 | */ | |
a246b010 | 1167 | xprt->ops->destroy(xprt); |
6b6ca86b | 1168 | } |
1da177e4 | 1169 | |
6b6ca86b TM |
1170 | /** |
1171 | * xprt_put - release a reference to an RPC transport. | |
1172 | * @xprt: pointer to the transport | |
1173 | * | |
1174 | */ | |
1175 | void xprt_put(struct rpc_xprt *xprt) | |
1176 | { | |
1177 | kref_put(&xprt->kref, xprt_destroy); | |
1178 | } | |
1179 | ||
1180 | /** | |
1181 | * xprt_get - return a reference to an RPC transport. | |
1182 | * @xprt: pointer to the transport | |
1183 | * | |
1184 | */ | |
1185 | struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) | |
1186 | { | |
1187 | kref_get(&xprt->kref); | |
1188 | return xprt; | |
1da177e4 | 1189 | } |