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