blob: ce5eb68a9664f6a93776304b304c80029efce84d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Chuck Lever55aa4f52005-08-11 16:25:47 -040013 * the request struct, and calls xprt_transmit().
14 * - xprt_transmit sends the message and installs the caller on the
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040015 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * - When a packet arrives, the data_ready handler walks the list of
Chuck Lever55aa4f52005-08-11 16:25:47 -040019 * pending requests for that transport. If a matching XID is found, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * caller is woken up, and the timer removed.
21 * - When no reply arrives within the timeout interval, the timer is
22 * fired by the kernel and runs xprt_timer(). It either adjusts the
23 * timeout values (minor timeout) or wakes up the caller with a status
24 * of -ETIMEDOUT.
25 * - When the caller receives a notification from RPC that a reply arrived,
26 * it should release the RPC slot, and process the reply.
27 * If the call timed out, it may choose to retry the operation by
28 * adjusting the initial timeout value, and simply calling rpc_call
29 * again.
30 *
31 * Support for async RPC is done through a set of RPC-specific scheduling
32 * primitives that `transparently' work for processes as well as async
33 * tasks that rely on callbacks.
34 *
35 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
Chuck Lever55aa4f52005-08-11 16:25:47 -040036 *
37 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/module.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
Chuck Levera246b012005-08-11 16:25:23 -040043#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/workqueue.h>
Chuck Leverbf3fcf82006-05-25 01:40:51 -040045#include <linux/net.h>
Chuck Leverff839972010-05-07 13:34:47 -040046#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Chuck Levera246b012005-08-11 16:25:23 -040048#include <linux/sunrpc/clnt.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050049#include <linux/sunrpc/metrics.h>
Trond Myklebustc9acb422010-03-19 15:36:22 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040052#include "sunrpc.h"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Local variables
56 */
57
58#ifdef RPC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070059# define RPCDBG_FACILITY RPCDBG_XPRT
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * Local functions
64 */
65static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void xprt_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
68
Jiri Slaby5ba03e82007-11-22 19:40:22 +080069static DEFINE_SPINLOCK(xprt_list_lock);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040070static LIST_HEAD(xprt_list);
71
Chuck Lever555ee3a2005-08-25 16:25:54 -070072/*
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Chuck Lever12a80462005-08-25 16:25:51 -070090/**
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -040091 * 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 */
102int 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 */
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400111 if (t->ident == transport->ident)
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400112 goto out;
113 }
114
Denis V. Lunevc9f6cde2008-07-31 09:53:56 +0400115 list_add_tail(&transport->list, &xprt_list);
116 printk(KERN_INFO "RPC: Registered %s transport module.\n",
117 transport->name);
118 result = 0;
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400119
120out:
121 spin_unlock(&xprt_list_lock);
122 return result;
123}
124EXPORT_SYMBOL_GPL(xprt_register_transport);
125
126/**
127 * xprt_unregister_transport - unregister a transport implementation
Randy Dunlap65b6e422008-02-13 15:03:23 -0800128 * @transport: transport to unregister
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400129 *
130 * Returns:
131 * 0: transport successfully unregistered
132 * -ENOENT: transport never registered
133 */
134int 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);
\"Talpey, Thomas\81c098a2007-09-10 13:46:00 -0400147 goto out;
148 }
149 }
150 result = -ENOENT;
151
152out:
153 spin_unlock(&xprt_list_lock);
154 return result;
155}
156EXPORT_SYMBOL_GPL(xprt_unregister_transport);
157
158/**
Tom Talpey441e3e22009-03-11 14:37:56 -0400159 * 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 */
166int xprt_load_transport(const char *transport_name)
167{
168 struct xprt_class *t;
Tom Talpey441e3e22009-03-11 14:37:56 -0400169 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);
Alex Riesenef7ffe82010-05-24 14:33:05 -0700180 result = request_module("xprt%s", transport_name);
Tom Talpey441e3e22009-03-11 14:37:56 -0400181out:
182 return result;
183}
184EXPORT_SYMBOL_GPL(xprt_load_transport);
185
186/**
Chuck Lever12a80462005-08-25 16:25:51 -0700187 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Chuck Lever12a80462005-08-25 16:25:51 -0700194int xprt_reserve_xprt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Chuck Lever12a80462005-08-25 16:25:51 -0700196 struct rpc_rqst *req = task->tk_rqstp;
Rahul Iyer343952f2009-04-01 09:23:17 -0400197 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever12a80462005-08-25 16:25:51 -0700198
199 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
200 if (task == xprt->snd_task)
201 return 1;
Chuck Lever12a80462005-08-25 16:25:51 -0700202 goto out_sleep;
203 }
204 xprt->snd_task = task;
j223yang@asset.uwaterloo.ca4d4a76f2011-03-10 12:40:28 -0500205 req->rq_bytes_sent = 0;
206 req->rq_ntrans++;
207
Chuck Lever12a80462005-08-25 16:25:51 -0700208 return 1;
209
210out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500211 dprintk("RPC: %5u failed to lock transport %p\n",
Chuck Lever12a80462005-08-25 16:25:51 -0700212 task->tk_pid, xprt);
213 task->tk_timeout = 0;
214 task->tk_status = -EAGAIN;
j223yang@asset.uwaterloo.caba3c5782011-03-16 11:16:22 -0400215 if (req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500216 rpc_sleep_on(&xprt->resend, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700217 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500218 rpc_sleep_on(&xprt->sending, task, NULL);
Chuck Lever12a80462005-08-25 16:25:51 -0700219 return 0;
220}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400221EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
Chuck Lever12a80462005-08-25 16:25:51 -0700222
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100223static void xprt_clear_locked(struct rpc_xprt *xprt)
224{
225 xprt->snd_task = NULL;
226 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
227 smp_mb__before_clear_bit();
228 clear_bit(XPRT_LOCKED, &xprt->state);
229 smp_mb__after_clear_bit();
230 } else
Trond Myklebustc1384c92007-06-14 18:00:42 -0400231 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100232}
233
Chuck Lever12a80462005-08-25 16:25:51 -0700234/*
235 * xprt_reserve_xprt_cong - serialize write access to transports
236 * @task: task that is requesting access to the transport
237 *
238 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
239 * integrated into the decision of whether a request is allowed to be
240 * woken up and given access to the transport.
241 */
242int xprt_reserve_xprt_cong(struct rpc_task *task)
243{
244 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct rpc_rqst *req = task->tk_rqstp;
246
Chuck Lever2226feb2005-08-11 16:25:38 -0400247 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (task == xprt->snd_task)
249 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out_sleep;
251 }
Chuck Lever12a80462005-08-25 16:25:51 -0700252 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 xprt->snd_task = task;
254 if (req) {
255 req->rq_bytes_sent = 0;
256 req->rq_ntrans++;
257 }
258 return 1;
259 }
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100260 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261out_sleep:
Chuck Lever46121cf2007-01-31 12:14:08 -0500262 dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 task->tk_timeout = 0;
264 task->tk_status = -EAGAIN;
265 if (req && req->rq_ntrans)
Trond Myklebust5d008372008-02-22 16:34:17 -0500266 rpc_sleep_on(&xprt->resend, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else
Trond Myklebust5d008372008-02-22 16:34:17 -0500268 rpc_sleep_on(&xprt->sending, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400271EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Chuck Lever12a80462005-08-25 16:25:51 -0700273static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 int retval;
276
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400277 spin_lock_bh(&xprt->transport_lock);
Chuck Lever12a80462005-08-25 16:25:51 -0700278 retval = xprt->ops->reserve_xprt(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400279 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return retval;
281}
282
Chuck Lever12a80462005-08-25 16:25:51 -0700283static void __xprt_lock_write_next(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct rpc_task *task;
Chuck Lever49e9a892005-08-25 16:25:51 -0700286 struct rpc_rqst *req;
287
288 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
289 return;
290
291 task = rpc_wake_up_next(&xprt->resend);
292 if (!task) {
293 task = rpc_wake_up_next(&xprt->sending);
294 if (!task)
295 goto out_unlock;
296 }
297
298 req = task->tk_rqstp;
299 xprt->snd_task = task;
300 if (req) {
301 req->rq_bytes_sent = 0;
302 req->rq_ntrans++;
303 }
304 return;
305
306out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100307 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700308}
309
310static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
311{
312 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Chuck Lever2226feb2005-08-11 16:25:38 -0400314 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return;
Chuck Lever49e9a892005-08-25 16:25:51 -0700316 if (RPCXPRT_CONGESTED(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto out_unlock;
318 task = rpc_wake_up_next(&xprt->resend);
319 if (!task) {
320 task = rpc_wake_up_next(&xprt->sending);
321 if (!task)
322 goto out_unlock;
323 }
Chuck Lever49e9a892005-08-25 16:25:51 -0700324 if (__xprt_get_cong(xprt, task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct rpc_rqst *req = task->tk_rqstp;
326 xprt->snd_task = task;
327 if (req) {
328 req->rq_bytes_sent = 0;
329 req->rq_ntrans++;
330 }
331 return;
332 }
333out_unlock:
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100334 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Chuck Lever49e9a892005-08-25 16:25:51 -0700337/**
338 * xprt_release_xprt - allow other requests to use a transport
339 * @xprt: transport with other tasks potentially waiting
340 * @task: task that is releasing access to the transport
341 *
342 * Note that "task" can be NULL. No congestion control is provided.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Chuck Lever49e9a892005-08-25 16:25:51 -0700344void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100347 xprt_clear_locked(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 __xprt_lock_write_next(xprt);
349 }
350}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400351EXPORT_SYMBOL_GPL(xprt_release_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Chuck Lever49e9a892005-08-25 16:25:51 -0700353/**
354 * xprt_release_xprt_cong - allow other requests to use a transport
355 * @xprt: transport with other tasks potentially waiting
356 * @task: task that is releasing access to the transport
357 *
358 * Note that "task" can be NULL. Another task is awoken to use the
359 * transport if the transport's congestion window allows it.
360 */
361void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
362{
363 if (xprt->snd_task == task) {
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100364 xprt_clear_locked(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700365 __xprt_lock_write_next_cong(xprt);
366 }
367}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400368EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
Chuck Lever49e9a892005-08-25 16:25:51 -0700369
370static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400372 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -0700373 xprt->ops->release_xprt(xprt, task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400374 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * Van Jacobson congestion avoidance. Check if the congestion window
379 * overflowed. Put the task to sleep if this is the case.
380 */
381static int
382__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
383{
384 struct rpc_rqst *req = task->tk_rqstp;
385
386 if (req->rq_cong)
387 return 1;
Chuck Lever46121cf2007-01-31 12:14:08 -0500388 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 task->tk_pid, xprt->cong, xprt->cwnd);
390 if (RPCXPRT_CONGESTED(xprt))
391 return 0;
392 req->rq_cong = 1;
393 xprt->cong += RPC_CWNDSCALE;
394 return 1;
395}
396
397/*
398 * Adjust the congestion window, and wake up the next task
399 * that has been sleeping due to congestion
400 */
401static void
402__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
403{
404 if (!req->rq_cong)
405 return;
406 req->rq_cong = 0;
407 xprt->cong -= RPC_CWNDSCALE;
Chuck Lever49e9a892005-08-25 16:25:51 -0700408 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Chuck Lever46c0ee82005-08-25 16:25:52 -0700411/**
Chuck Levera58dd392005-08-25 16:25:53 -0700412 * xprt_release_rqst_cong - housekeeping when request is complete
413 * @task: RPC request that recently completed
414 *
415 * Useful for transports that require congestion control.
416 */
417void xprt_release_rqst_cong(struct rpc_task *task)
418{
419 __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
420}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400421EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
Chuck Levera58dd392005-08-25 16:25:53 -0700422
423/**
Chuck Lever46c0ee82005-08-25 16:25:52 -0700424 * xprt_adjust_cwnd - adjust transport congestion window
425 * @task: recently completed RPC request used to adjust window
426 * @result: result code of completed RPC request
427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
429 */
Chuck Lever46c0ee82005-08-25 16:25:52 -0700430void xprt_adjust_cwnd(struct rpc_task *task, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700432 struct rpc_rqst *req = task->tk_rqstp;
433 struct rpc_xprt *xprt = task->tk_xprt;
434 unsigned long cwnd = xprt->cwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (result >= 0 && cwnd <= xprt->cong) {
437 /* The (cwnd >> 1) term makes sure
438 * the result gets rounded properly. */
439 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
440 if (cwnd > RPC_MAXCWND(xprt))
441 cwnd = RPC_MAXCWND(xprt);
Chuck Lever49e9a892005-08-25 16:25:51 -0700442 __xprt_lock_write_next_cong(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else if (result == -ETIMEDOUT) {
444 cwnd >>= 1;
445 if (cwnd < RPC_CWNDSCALE)
446 cwnd = RPC_CWNDSCALE;
447 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500448 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 xprt->cong, xprt->cwnd, cwnd);
450 xprt->cwnd = cwnd;
Chuck Lever46c0ee82005-08-25 16:25:52 -0700451 __xprt_put_cong(xprt, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400453EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Chuck Lever44fbac22005-08-11 16:25:44 -0400455/**
456 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
457 * @xprt: transport with waiting tasks
458 * @status: result code to plant in each task before waking it
459 *
460 */
461void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
462{
463 if (status < 0)
464 rpc_wake_up_status(&xprt->pending, status);
465 else
466 rpc_wake_up(&xprt->pending);
467}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400468EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
Chuck Lever44fbac22005-08-11 16:25:44 -0400469
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400470/**
471 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
472 * @task: task to be put to sleep
Randy Dunlap0b80ae42008-04-26 22:59:02 -0700473 * @action: function pointer to be executed after wait
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400474 */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400475void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400476{
477 struct rpc_rqst *req = task->tk_rqstp;
478 struct rpc_xprt *xprt = req->rq_xprt;
479
480 task->tk_timeout = req->rq_timeout;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481 rpc_sleep_on(&xprt->pending, task, action);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400482}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400483EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400484
485/**
486 * xprt_write_space - wake the task waiting for transport output buffer space
487 * @xprt: transport with waiting tasks
488 *
489 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
490 */
491void xprt_write_space(struct rpc_xprt *xprt)
492{
493 if (unlikely(xprt->shutdown))
494 return;
495
496 spin_lock_bh(&xprt->transport_lock);
497 if (xprt->snd_task) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500498 dprintk("RPC: write space: waking waiting task on "
499 "xprt %p\n", xprt);
Trond Myklebustfda13932008-02-22 16:34:12 -0500500 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400501 }
502 spin_unlock_bh(&xprt->transport_lock);
503}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400504EXPORT_SYMBOL_GPL(xprt_write_space);
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400505
Chuck Leverfe3aca22005-08-25 16:25:50 -0700506/**
507 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
508 * @task: task whose timeout is to be set
509 *
510 * Set a request's retransmit timeout based on the transport's
511 * default timeout parameters. Used by transports that don't adjust
512 * the retransmit timeout based on round-trip time estimation.
513 */
514void xprt_set_retrans_timeout_def(struct rpc_task *task)
515{
516 task->tk_timeout = task->tk_rqstp->rq_timeout;
517}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400518EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700519
520/*
521 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
522 * @task: task whose timeout is to be set
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800523 *
Chuck Leverfe3aca22005-08-25 16:25:50 -0700524 * Set a request's retransmit timeout using the RTT estimator.
525 */
526void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
527{
528 int timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500529 struct rpc_clnt *clnt = task->tk_client;
530 struct rpc_rtt *rtt = clnt->cl_rtt;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700531 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500532 unsigned long max_timeout = clnt->cl_timeout->to_maxval;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700533
534 task->tk_timeout = rpc_calc_rto(rtt, timer);
535 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
536 if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
537 task->tk_timeout = max_timeout;
538}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400539EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
Chuck Leverfe3aca22005-08-25 16:25:50 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static void xprt_reset_majortimeo(struct rpc_rqst *req)
542{
Trond Myklebustba7392b2007-12-20 16:03:55 -0500543 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 req->rq_majortimeo = req->rq_timeout;
546 if (to->to_exponential)
547 req->rq_majortimeo <<= to->to_retries;
548 else
549 req->rq_majortimeo += to->to_increment * to->to_retries;
550 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
551 req->rq_majortimeo = to->to_maxval;
552 req->rq_majortimeo += jiffies;
553}
554
Chuck Lever9903cd12005-08-11 16:25:26 -0400555/**
556 * xprt_adjust_timeout - adjust timeout values for next retransmit
557 * @req: RPC request containing parameters to use for the adjustment
558 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
560int xprt_adjust_timeout(struct rpc_rqst *req)
561{
562 struct rpc_xprt *xprt = req->rq_xprt;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500563 const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int status = 0;
565
566 if (time_before(jiffies, req->rq_majortimeo)) {
567 if (to->to_exponential)
568 req->rq_timeout <<= 1;
569 else
570 req->rq_timeout += to->to_increment;
571 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
572 req->rq_timeout = to->to_maxval;
573 req->rq_retries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 } else {
575 req->rq_timeout = to->to_initval;
576 req->rq_retries = 0;
577 xprt_reset_majortimeo(req);
578 /* Reset the RTT counters == "slow start" */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400579 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400581 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 status = -ETIMEDOUT;
583 }
584
585 if (req->rq_timeout == 0) {
586 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
587 req->rq_timeout = 5 * HZ;
588 }
589 return status;
590}
591
David Howells65f27f32006-11-22 14:55:48 +0000592static void xprt_autoclose(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
David Howells65f27f32006-11-22 14:55:48 +0000594 struct rpc_xprt *xprt =
595 container_of(work, struct rpc_xprt, task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Chuck Levera246b012005-08-11 16:25:23 -0400597 xprt->ops->close(xprt);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500598 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 xprt_release_write(xprt, NULL);
600}
601
Chuck Lever9903cd12005-08-11 16:25:26 -0400602/**
Trond Myklebust62da3b22007-11-06 18:44:20 -0500603 * xprt_disconnect_done - mark a transport as disconnected
Chuck Lever9903cd12005-08-11 16:25:26 -0400604 * @xprt: transport to flag for disconnect
605 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Trond Myklebust62da3b22007-11-06 18:44:20 -0500607void xprt_disconnect_done(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Chuck Lever46121cf2007-01-31 12:14:08 -0500609 dprintk("RPC: disconnected transport %p\n", xprt);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400610 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xprt_clear_connected(xprt);
Trond Myklebust2a491992009-03-11 14:38:00 -0400612 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400613 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Trond Myklebust62da3b22007-11-06 18:44:20 -0500615EXPORT_SYMBOL_GPL(xprt_disconnect_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Trond Myklebust66af1e52007-11-06 10:18:36 -0500617/**
618 * xprt_force_disconnect - force a transport to disconnect
619 * @xprt: transport to disconnect
620 *
621 */
622void xprt_force_disconnect(struct rpc_xprt *xprt)
623{
624 /* Don't race with the test_bit() in xprt_clear_locked() */
625 spin_lock_bh(&xprt->transport_lock);
626 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
627 /* Try to schedule an autoclose RPC call */
628 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
629 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400630 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust66af1e52007-11-06 10:18:36 -0500631 spin_unlock_bh(&xprt->transport_lock);
632}
Trond Myklebust66af1e52007-11-06 10:18:36 -0500633
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400634/**
635 * xprt_conditional_disconnect - force a transport to disconnect
636 * @xprt: transport to disconnect
637 * @cookie: 'connection cookie'
638 *
639 * This attempts to break the connection if and only if 'cookie' matches
640 * the current transport 'connection cookie'. It ensures that we don't
641 * try to break the connection more than once when we need to retransmit
642 * a batch of RPC requests.
643 *
644 */
645void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
646{
647 /* Don't race with the test_bit() in xprt_clear_locked() */
648 spin_lock_bh(&xprt->transport_lock);
649 if (cookie != xprt->connect_cookie)
650 goto out;
651 if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
652 goto out;
653 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
654 /* Try to schedule an autoclose RPC call */
655 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
656 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Trond Myklebust2a491992009-03-11 14:38:00 -0400657 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400658out:
659 spin_unlock_bh(&xprt->transport_lock);
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662static void
663xprt_init_autodisconnect(unsigned long data)
664{
665 struct rpc_xprt *xprt = (struct rpc_xprt *)data;
666
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400667 spin_lock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!list_empty(&xprt->recv) || xprt->shutdown)
669 goto out_abort;
Chuck Lever2226feb2005-08-11 16:25:38 -0400670 if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto out_abort;
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400672 spin_unlock(&xprt->transport_lock);
Trond Myklebustf75e6742009-04-21 17:18:20 -0400673 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
674 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return;
676out_abort:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400677 spin_unlock(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Chuck Lever9903cd12005-08-11 16:25:26 -0400680/**
681 * xprt_connect - schedule a transport connect operation
682 * @task: RPC task that is requesting the connect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
684 */
685void xprt_connect(struct rpc_task *task)
686{
687 struct rpc_xprt *xprt = task->tk_xprt;
688
Chuck Lever46121cf2007-01-31 12:14:08 -0500689 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 xprt, (xprt_connected(xprt) ? "is" : "is not"));
691
Chuck Leverec739ef2006-08-22 20:06:15 -0400692 if (!xprt_bound(xprt)) {
Trond Myklebust01d37c42009-03-11 14:09:39 -0400693 task->tk_status = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return;
695 }
696 if (!xprt_lock_write(xprt, task))
697 return;
Trond Myklebustfeb8ca32009-12-03 08:10:17 -0500698
699 if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
700 xprt->ops->close(xprt);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (xprt_connected(xprt))
Chuck Levera246b012005-08-11 16:25:23 -0400703 xprt_release_write(xprt, task);
704 else {
705 if (task->tk_rqstp)
706 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Trond Myklebusta8ce4a82010-04-16 16:42:12 -0400708 task->tk_timeout = task->tk_rqstp->rq_timeout;
Trond Myklebust5d008372008-02-22 16:34:17 -0500709 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400710
711 if (test_bit(XPRT_CLOSING, &xprt->state))
712 return;
713 if (xprt_test_and_set_connecting(xprt))
714 return;
Chuck Lever262ca072006-03-20 13:44:16 -0500715 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -0400716 xprt->ops->connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Chuck Lever9903cd12005-08-11 16:25:26 -0400720static void xprt_connect_status(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct rpc_xprt *xprt = task->tk_xprt;
723
Chuck Levercd983ef2008-06-11 17:56:13 -0400724 if (task->tk_status == 0) {
Chuck Lever262ca072006-03-20 13:44:16 -0500725 xprt->stat.connect_count++;
726 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
Chuck Lever46121cf2007-01-31 12:14:08 -0500727 dprintk("RPC: %5u xprt_connect_status: connection established\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 task->tk_pid);
729 return;
730 }
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 switch (task->tk_status) {
Trond Myklebust2a491992009-03-11 14:38:00 -0400733 case -EAGAIN:
734 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
Chuck Lever23475d62005-08-11 16:25:08 -0400735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500737 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
738 "out\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500741 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
742 "server %s\n", task->tk_pid, -task->tk_status,
743 task->tk_client->cl_server);
Chuck Lever23475d62005-08-11 16:25:08 -0400744 xprt_release_write(xprt, task);
745 task->tk_status = -EIO;
Chuck Lever23475d62005-08-11 16:25:08 -0400746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Chuck Lever9903cd12005-08-11 16:25:26 -0400749/**
750 * xprt_lookup_rqst - find an RPC request corresponding to an XID
751 * @xprt: transport on which the original request was transmitted
752 * @xid: RPC XID of incoming reply
753 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700755struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400757 struct rpc_rqst *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +0400759 list_for_each_entry(entry, &xprt->recv, rq_list)
Chuck Lever262ca072006-03-20 13:44:16 -0500760 if (entry->rq_xid == xid)
761 return entry;
Chuck Lever46121cf2007-01-31 12:14:08 -0500762
763 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
764 ntohl(xid));
Chuck Lever262ca072006-03-20 13:44:16 -0500765 xprt->stat.bad_xids++;
766 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400768EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Chuck Leverbbc72ce2010-05-07 13:34:27 -0400770static void xprt_update_rtt(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Chuck Lever1570c1e2005-08-25 16:25:52 -0700772 struct rpc_rqst *req = task->tk_rqstp;
773 struct rpc_rtt *rtt = task->tk_client->cl_rtt;
774 unsigned timer = task->tk_msg.rpc_proc->p_timer;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400775 long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Chuck Lever1570c1e2005-08-25 16:25:52 -0700777 if (timer) {
778 if (req->rq_ntrans == 1)
Chuck Leverff839972010-05-07 13:34:47 -0400779 rpc_update_rtt(rtt, timer, m);
Chuck Lever1570c1e2005-08-25 16:25:52 -0700780 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Chuck Lever1570c1e2005-08-25 16:25:52 -0700782}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Chuck Lever1570c1e2005-08-25 16:25:52 -0700784/**
785 * xprt_complete_rqst - called when reply processing is complete
786 * @task: RPC request that recently completed
787 * @copied: actual number of bytes received from the transport
788 *
789 * Caller holds transport lock.
790 */
791void xprt_complete_rqst(struct rpc_task *task, int copied)
792{
793 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebustfda13932008-02-22 16:34:12 -0500794 struct rpc_xprt *xprt = req->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Chuck Lever1570c1e2005-08-25 16:25:52 -0700796 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
797 task->tk_pid, ntohl(req->rq_xid), copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Trond Myklebustfda13932008-02-22 16:34:12 -0500799 xprt->stat.recvs++;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400800 req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
Chuck Leverbbc72ce2010-05-07 13:34:27 -0400801 if (xprt->ops->timer != NULL)
802 xprt_update_rtt(task);
Chuck Leveref759a22006-03-20 13:44:17 -0500803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 list_del_init(&req->rq_list);
Trond Myklebust1e799b62008-03-21 16:19:41 -0400805 req->rq_private_buf.len = copied;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400806 /* Ensure all writes are done before we update */
807 /* req->rq_reply_bytes_recvd */
Trond Myklebust43ac3f22006-03-20 13:44:51 -0500808 smp_wmb();
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400809 req->rq_reply_bytes_recvd = copied;
Trond Myklebustfda13932008-02-22 16:34:12 -0500810 rpc_wake_up_queued_task(&xprt->pending, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400812EXPORT_SYMBOL_GPL(xprt_complete_rqst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Chuck Lever46c0ee82005-08-25 16:25:52 -0700814static void xprt_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Chuck Lever46c0ee82005-08-25 16:25:52 -0700816 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct rpc_xprt *xprt = req->rq_xprt;
818
Trond Myklebust5d008372008-02-22 16:34:17 -0500819 if (task->tk_status != -ETIMEDOUT)
820 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500821 dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
Chuck Lever46c0ee82005-08-25 16:25:52 -0700822
Trond Myklebust5d008372008-02-22 16:34:17 -0500823 spin_lock_bh(&xprt->transport_lock);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400824 if (!req->rq_reply_bytes_recvd) {
Chuck Lever46c0ee82005-08-25 16:25:52 -0700825 if (xprt->ops->timer)
826 xprt->ops->timer(task);
Trond Myklebust5d008372008-02-22 16:34:17 -0500827 } else
828 task->tk_status = 0;
829 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300832static inline int xprt_has_timer(struct rpc_xprt *xprt)
833{
834 return xprt->idle_timeout != 0;
835}
836
Chuck Lever9903cd12005-08-11 16:25:26 -0400837/**
838 * xprt_prepare_transmit - reserve the transport before sending a request
839 * @task: RPC task about to send a request
840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400842int xprt_prepare_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct rpc_rqst *req = task->tk_rqstp;
845 struct rpc_xprt *xprt = req->rq_xprt;
846 int err = 0;
847
Chuck Lever46121cf2007-01-31 12:14:08 -0500848 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400850 spin_lock_bh(&xprt->transport_lock);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400851 if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
852 err = req->rq_reply_bytes_recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto out_unlock;
854 }
Trond Myklebust2a491992009-03-11 14:38:00 -0400855 if (!xprt->ops->reserve_xprt(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400858 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return err;
860}
861
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400862void xprt_end_transmit(struct rpc_task *task)
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700863{
Rahul Iyer343952f2009-04-01 09:23:17 -0400864 xprt_release_write(task->tk_rqstp->rq_xprt, task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700865}
866
Chuck Lever9903cd12005-08-11 16:25:26 -0400867/**
868 * xprt_transmit - send an RPC request on a transport
869 * @task: controlling RPC task
870 *
871 * We have to copy the iovec because sendmsg fiddles with its contents.
872 */
873void xprt_transmit(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct rpc_rqst *req = task->tk_rqstp;
876 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400877 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Chuck Lever46121cf2007-01-31 12:14:08 -0500879 dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400881 if (!req->rq_reply_bytes_recvd) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400882 if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
883 /*
884 * Add to the list only if we're expecting a reply
885 */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400886 spin_lock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Update the softirq receive buffer */
888 memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
889 sizeof(req->rq_private_buf));
890 /* Add request to the receive list */
891 list_add_tail(&req->rq_list, &xprt->recv);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400892 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 xprt_reset_majortimeo(req);
Trond Myklebust0f9dc2b2005-06-22 17:16:28 +0000894 /* Turn off autodisconnect */
895 del_singleshot_timer_sync(&xprt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 } else if (!req->rq_bytes_sent)
898 return;
899
Trond Myklebust7c1d71c2008-04-17 16:52:57 -0400900 req->rq_connect_cookie = xprt->connect_cookie;
Chuck Leverff839972010-05-07 13:34:47 -0400901 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -0400902 status = xprt->ops->send_request(task);
Trond Myklebustc8485e42009-03-11 14:37:59 -0400903 if (status != 0) {
904 task->tk_status = status;
Chuck Leverfe3aca22005-08-25 16:25:50 -0700905 return;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebustc8485e42009-03-11 14:37:59 -0400908 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
Bryan Schumaker468f8612011-04-18 15:57:32 -0400909 task->tk_flags |= RPC_TASK_SENT;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400910 spin_lock_bh(&xprt->transport_lock);
911
912 xprt->ops->set_retrans_timeout(task);
913
914 xprt->stat.sends++;
915 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
916 xprt->stat.bklog_u += xprt->backlog.qlen;
917
918 /* Don't race with disconnect */
919 if (!xprt_connected(xprt))
920 task->tk_status = -ENOTCONN;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400921 else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400922 /*
923 * Sleep on the pending queue since
924 * we're expecting a reply.
925 */
Trond Myklebustc8485e42009-03-11 14:37:59 -0400926 rpc_sleep_on(&xprt->pending, task, xprt_timer);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400927 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400928 spin_unlock_bh(&xprt->transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Trond Myklebustee5ebe82010-04-16 16:37:01 -0400931static void xprt_alloc_slot(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct rpc_xprt *xprt = task->tk_xprt;
934
935 task->tk_status = 0;
936 if (task->tk_rqstp)
937 return;
938 if (!list_empty(&xprt->free)) {
939 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
940 list_del_init(&req->rq_list);
941 task->tk_rqstp = req;
942 xprt_request_init(task, xprt);
943 return;
944 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500945 dprintk("RPC: waiting for request slot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 task->tk_status = -EAGAIN;
947 task->tk_timeout = 0;
Trond Myklebust5d008372008-02-22 16:34:17 -0500948 rpc_sleep_on(&xprt->backlog, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Trond Myklebustee5ebe82010-04-16 16:37:01 -0400951static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
952{
953 memset(req, 0, sizeof(*req)); /* mark unused */
954
955 spin_lock(&xprt->reserve_lock);
956 list_add(&req->rq_list, &xprt->free);
957 rpc_wake_up_next(&xprt->backlog);
958 spin_unlock(&xprt->reserve_lock);
959}
960
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400961struct rpc_xprt *xprt_alloc(struct net *net, int size, int max_req)
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400962{
963 struct rpc_xprt *xprt;
964
965 xprt = kzalloc(size, GFP_KERNEL);
966 if (xprt == NULL)
967 goto out;
Trond Myklebusta8de2402011-03-15 19:56:30 -0400968 atomic_set(&xprt->count, 1);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400969
970 xprt->max_reqs = max_req;
971 xprt->slot = kcalloc(max_req, sizeof(struct rpc_rqst), GFP_KERNEL);
972 if (xprt->slot == NULL)
973 goto out_free;
974
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400975 xprt->xprt_net = get_net(net);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400976 return xprt;
977
978out_free:
979 kfree(xprt);
980out:
981 return NULL;
982}
983EXPORT_SYMBOL_GPL(xprt_alloc);
984
Pavel Emelyanove204e622010-09-29 16:03:13 +0400985void xprt_free(struct rpc_xprt *xprt)
986{
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400987 put_net(xprt->xprt_net);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400988 kfree(xprt->slot);
989 kfree(xprt);
990}
991EXPORT_SYMBOL_GPL(xprt_free);
992
Chuck Lever9903cd12005-08-11 16:25:26 -0400993/**
994 * xprt_reserve - allocate an RPC request slot
995 * @task: RPC task requesting a slot allocation
996 *
997 * If no more slots are available, place the task on the transport's
998 * backlog queue.
999 */
1000void xprt_reserve(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct rpc_xprt *xprt = task->tk_xprt;
1003
1004 task->tk_status = -EIO;
Trond Myklebust0065db32006-01-03 09:55:56 +01001005 spin_lock(&xprt->reserve_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001006 xprt_alloc_slot(task);
Trond Myklebust0065db32006-01-03 09:55:56 +01001007 spin_unlock(&xprt->reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001010static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001012 return (__force __be32)xprt->xid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static inline void xprt_init_xid(struct rpc_xprt *xprt)
1016{
Chuck Leverbf3fcf82006-05-25 01:40:51 -04001017 xprt->xid = net_random();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Chuck Lever9903cd12005-08-11 16:25:26 -04001020static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 struct rpc_rqst *req = task->tk_rqstp;
1023
Trond Myklebustba7392b2007-12-20 16:03:55 -05001024 req->rq_timeout = task->tk_client->cl_timeout->to_initval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 req->rq_task = task;
1026 req->rq_xprt = xprt;
Chuck Lever02107142006-01-03 09:55:49 +01001027 req->rq_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 req->rq_xid = xprt_alloc_xid(xprt);
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001029 req->rq_release_snd_buf = NULL;
Trond Myklebustda458282006-08-31 15:44:52 -04001030 xprt_reset_majortimeo(req);
Chuck Lever46121cf2007-01-31 12:14:08 -05001031 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 req, ntohl(req->rq_xid));
1033}
1034
Chuck Lever9903cd12005-08-11 16:25:26 -04001035/**
1036 * xprt_release - release an RPC request slot
1037 * @task: task which is finished with the slot
1038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Chuck Lever9903cd12005-08-11 16:25:26 -04001040void xprt_release(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001042 struct rpc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct rpc_rqst *req;
1044
1045 if (!(req = task->tk_rqstp))
1046 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001047
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001048 xprt = req->rq_xprt;
Chuck Lever11c556b2006-03-20 13:44:22 -05001049 rpc_count_iostats(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001050 spin_lock_bh(&xprt->transport_lock);
Chuck Lever49e9a892005-08-25 16:25:51 -07001051 xprt->ops->release_xprt(xprt, task);
Chuck Levera58dd392005-08-25 16:25:53 -07001052 if (xprt->ops->release_request)
1053 xprt->ops->release_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!list_empty(&req->rq_list))
1055 list_del(&req->rq_list);
1056 xprt->last_used = jiffies;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001057 if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001058 mod_timer(&xprt->timer,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001059 xprt->last_used + xprt->idle_timeout);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001060 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001061 if (req->rq_buffer)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001062 xprt->ops->buf_free(req->rq_buffer);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001063 if (req->rq_cred != NULL)
1064 put_rpccred(req->rq_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 task->tk_rqstp = NULL;
J. Bruce Fieldsead5e1c2005-10-13 16:54:43 -04001066 if (req->rq_release_snd_buf)
1067 req->rq_release_snd_buf(req);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001068
Chuck Lever46121cf2007-01-31 12:14:08 -05001069 dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
Trond Myklebustee5ebe82010-04-16 16:37:01 -04001070 if (likely(!bc_prealloc(req)))
1071 xprt_free_slot(xprt, req);
1072 else
Trond Myklebustc9acb422010-03-19 15:36:22 -04001073 xprt_free_bc_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Chuck Lever9903cd12005-08-11 16:25:26 -04001076/**
Chuck Leverc2866762006-08-22 20:06:20 -04001077 * xprt_create_transport - create an RPC transport
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001078 * @args: rpc transport creation arguments
Chuck Leverc2866762006-08-22 20:06:20 -04001079 *
1080 */
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04001081struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
Chuck Leverc2866762006-08-22 20:06:20 -04001082{
Chuck Leverc2866762006-08-22 20:06:20 -04001083 struct rpc_xprt *xprt;
1084 struct rpc_rqst *req;
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001085 struct xprt_class *t;
Chuck Leverc2866762006-08-22 20:06:20 -04001086
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001087 spin_lock(&xprt_list_lock);
1088 list_for_each_entry(t, &xprt_list, list) {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001089 if (t->ident == args->ident) {
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001090 spin_unlock(&xprt_list_lock);
1091 goto found;
1092 }
Chuck Leverc2866762006-08-22 20:06:20 -04001093 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001094 spin_unlock(&xprt_list_lock);
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04001095 printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001096 return ERR_PTR(-EIO);
1097
1098found:
1099 xprt = t->setup(args);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001100 if (IS_ERR(xprt)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001101 dprintk("RPC: xprt_create_transport: failed, %ld\n",
Chuck Leverc8541ec2006-10-17 14:44:27 -04001102 -PTR_ERR(xprt));
1103 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001104 }
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05001105 if (test_and_set_bit(XPRT_INITIALIZED, &xprt->state))
1106 /* ->setup returned a pre-initialized xprt: */
1107 return xprt;
Chuck Leverc2866762006-08-22 20:06:20 -04001108
1109 spin_lock_init(&xprt->transport_lock);
1110 spin_lock_init(&xprt->reserve_lock);
1111
1112 INIT_LIST_HEAD(&xprt->free);
1113 INIT_LIST_HEAD(&xprt->recv);
Ricardo Labiagaf9acac12009-04-01 09:22:59 -04001114#if defined(CONFIG_NFS_V4_1)
1115 spin_lock_init(&xprt->bc_pa_lock);
1116 INIT_LIST_HEAD(&xprt->bc_pa_list);
1117#endif /* CONFIG_NFS_V4_1 */
1118
David Howells65f27f32006-11-22 14:55:48 +00001119 INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001120 if (xprt_has_timer(xprt))
1121 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1122 (unsigned long)xprt);
1123 else
1124 init_timer(&xprt->timer);
Chuck Leverc2866762006-08-22 20:06:20 -04001125 xprt->last_used = jiffies;
1126 xprt->cwnd = RPC_INITCWND;
Chuck Levera5090502007-03-29 16:48:04 -04001127 xprt->bind_index = 0;
Chuck Leverc2866762006-08-22 20:06:20 -04001128
1129 rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1130 rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1131 rpc_init_wait_queue(&xprt->sending, "xprt_sending");
1132 rpc_init_wait_queue(&xprt->resend, "xprt_resend");
1133 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1134
1135 /* initialize free list */
1136 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
1137 list_add(&req->rq_list, &xprt->free);
1138
1139 xprt_init_xid(xprt);
1140
Chuck Lever46121cf2007-01-31 12:14:08 -05001141 dprintk("RPC: created transport %p with %u slots\n", xprt,
Chuck Leverc2866762006-08-22 20:06:20 -04001142 xprt->max_reqs);
Chuck Leverc2866762006-08-22 20:06:20 -04001143 return xprt;
1144}
1145
Chuck Lever9903cd12005-08-11 16:25:26 -04001146/**
1147 * xprt_destroy - destroy an RPC transport, killing off all requests.
Trond Myklebusta8de2402011-03-15 19:56:30 -04001148 * @xprt: transport to destroy
Chuck Lever9903cd12005-08-11 16:25:26 -04001149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Trond Myklebusta8de2402011-03-15 19:56:30 -04001151static void xprt_destroy(struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Chuck Lever46121cf2007-01-31 12:14:08 -05001153 dprintk("RPC: destroying transport %p\n", xprt);
Trond Myklebust0065db32006-01-03 09:55:56 +01001154 xprt->shutdown = 1;
1155 del_timer_sync(&xprt->timer);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001156
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001157 rpc_destroy_wait_queue(&xprt->binding);
1158 rpc_destroy_wait_queue(&xprt->pending);
1159 rpc_destroy_wait_queue(&xprt->sending);
1160 rpc_destroy_wait_queue(&xprt->resend);
1161 rpc_destroy_wait_queue(&xprt->backlog);
J. Bruce Fieldsc3ae62a2010-08-03 17:22:20 -04001162 cancel_work_sync(&xprt->task_cleanup);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001163 /*
1164 * Tear down transport state and free the rpc_xprt
1165 */
Chuck Levera246b012005-08-11 16:25:23 -04001166 xprt->ops->destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001169/**
1170 * xprt_put - release a reference to an RPC transport.
1171 * @xprt: pointer to the transport
1172 *
1173 */
1174void xprt_put(struct rpc_xprt *xprt)
1175{
Trond Myklebusta8de2402011-03-15 19:56:30 -04001176 if (atomic_dec_and_test(&xprt->count))
1177 xprt_destroy(xprt);
Trond Myklebust6b6ca862006-09-05 12:55:57 -04001178}
1179
1180/**
1181 * xprt_get - return a reference to an RPC transport.
1182 * @xprt: pointer to the transport
1183 *
1184 */
1185struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1186{
Trond Myklebusta8de2402011-03-15 19:56:30 -04001187 if (atomic_inc_not_zero(&xprt->count))
1188 return xprt;
1189 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}