USB: xhci: fix less- and greater than confusion
[linux-2.6/verdex.git] / net / rxrpc / ar-transport.c
blob0936e1acc30ece6110313c01fb0ee78c0e5dd91e
1 /* RxRPC point-to-point transport session management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <net/sock.h>
16 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
19 static void rxrpc_transport_reaper(struct work_struct *work);
21 static LIST_HEAD(rxrpc_transports);
22 static DEFINE_RWLOCK(rxrpc_transport_lock);
23 static unsigned long rxrpc_transport_timeout = 3600 * 24;
24 static DECLARE_DELAYED_WORK(rxrpc_transport_reap, rxrpc_transport_reaper);
27 * allocate a new transport session manager
29 static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local,
30 struct rxrpc_peer *peer,
31 gfp_t gfp)
33 struct rxrpc_transport *trans;
35 _enter("");
37 trans = kzalloc(sizeof(struct rxrpc_transport), gfp);
38 if (trans) {
39 trans->local = local;
40 trans->peer = peer;
41 INIT_LIST_HEAD(&trans->link);
42 trans->bundles = RB_ROOT;
43 trans->client_conns = RB_ROOT;
44 trans->server_conns = RB_ROOT;
45 skb_queue_head_init(&trans->error_queue);
46 spin_lock_init(&trans->client_lock);
47 rwlock_init(&trans->conn_lock);
48 atomic_set(&trans->usage, 1);
49 trans->debug_id = atomic_inc_return(&rxrpc_debug_id);
51 if (peer->srx.transport.family == AF_INET) {
52 switch (peer->srx.transport_type) {
53 case SOCK_DGRAM:
54 INIT_WORK(&trans->error_handler,
55 rxrpc_UDP_error_handler);
56 break;
57 default:
58 BUG();
59 break;
61 } else {
62 BUG();
66 _leave(" = %p", trans);
67 return trans;
71 * obtain a transport session for the nominated endpoints
73 struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local,
74 struct rxrpc_peer *peer,
75 gfp_t gfp)
77 struct rxrpc_transport *trans, *candidate;
78 const char *new = "old";
79 int usage;
81 _enter("{%pI4+%hu},{%pI4+%hu},",
82 &local->srx.transport.sin.sin_addr,
83 ntohs(local->srx.transport.sin.sin_port),
84 &peer->srx.transport.sin.sin_addr,
85 ntohs(peer->srx.transport.sin.sin_port));
87 /* search the transport list first */
88 read_lock_bh(&rxrpc_transport_lock);
89 list_for_each_entry(trans, &rxrpc_transports, link) {
90 if (trans->local == local && trans->peer == peer)
91 goto found_extant_transport;
93 read_unlock_bh(&rxrpc_transport_lock);
95 /* not yet present - create a candidate for a new record and then
96 * redo the search */
97 candidate = rxrpc_alloc_transport(local, peer, gfp);
98 if (!candidate) {
99 _leave(" = -ENOMEM");
100 return ERR_PTR(-ENOMEM);
103 write_lock_bh(&rxrpc_transport_lock);
105 list_for_each_entry(trans, &rxrpc_transports, link) {
106 if (trans->local == local && trans->peer == peer)
107 goto found_extant_second;
110 /* we can now add the new candidate to the list */
111 trans = candidate;
112 candidate = NULL;
114 rxrpc_get_local(trans->local);
115 atomic_inc(&trans->peer->usage);
116 list_add_tail(&trans->link, &rxrpc_transports);
117 write_unlock_bh(&rxrpc_transport_lock);
118 new = "new";
120 success:
121 _net("TRANSPORT %s %d local %d -> peer %d",
122 new,
123 trans->debug_id,
124 trans->local->debug_id,
125 trans->peer->debug_id);
127 _leave(" = %p {u=%d}", trans, atomic_read(&trans->usage));
128 return trans;
130 /* we found the transport in the list immediately */
131 found_extant_transport:
132 usage = atomic_inc_return(&trans->usage);
133 read_unlock_bh(&rxrpc_transport_lock);
134 goto success;
136 /* we found the transport on the second time through the list */
137 found_extant_second:
138 usage = atomic_inc_return(&trans->usage);
139 write_unlock_bh(&rxrpc_transport_lock);
140 kfree(candidate);
141 goto success;
145 * find the transport connecting two endpoints
147 struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
148 struct rxrpc_peer *peer)
150 struct rxrpc_transport *trans;
152 _enter("{%pI4+%hu},{%pI4+%hu},",
153 &local->srx.transport.sin.sin_addr,
154 ntohs(local->srx.transport.sin.sin_port),
155 &peer->srx.transport.sin.sin_addr,
156 ntohs(peer->srx.transport.sin.sin_port));
158 /* search the transport list */
159 read_lock_bh(&rxrpc_transport_lock);
161 list_for_each_entry(trans, &rxrpc_transports, link) {
162 if (trans->local == local && trans->peer == peer)
163 goto found_extant_transport;
166 read_unlock_bh(&rxrpc_transport_lock);
167 _leave(" = NULL");
168 return NULL;
170 found_extant_transport:
171 atomic_inc(&trans->usage);
172 read_unlock_bh(&rxrpc_transport_lock);
173 _leave(" = %p", trans);
174 return trans;
178 * release a transport session
180 void rxrpc_put_transport(struct rxrpc_transport *trans)
182 _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
184 ASSERTCMP(atomic_read(&trans->usage), >, 0);
186 trans->put_time = get_seconds();
187 if (unlikely(atomic_dec_and_test(&trans->usage))) {
188 _debug("zombie");
189 /* let the reaper determine the timeout to avoid a race with
190 * overextending the timeout if the reaper is running at the
191 * same time */
192 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
194 _leave("");
198 * clean up a transport session
200 static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
202 _net("DESTROY TRANS %d", trans->debug_id);
204 rxrpc_purge_queue(&trans->error_queue);
206 rxrpc_put_local(trans->local);
207 rxrpc_put_peer(trans->peer);
208 kfree(trans);
212 * reap dead transports that have passed their expiry date
214 static void rxrpc_transport_reaper(struct work_struct *work)
216 struct rxrpc_transport *trans, *_p;
217 unsigned long now, earliest, reap_time;
219 LIST_HEAD(graveyard);
221 _enter("");
223 now = get_seconds();
224 earliest = ULONG_MAX;
226 /* extract all the transports that have been dead too long */
227 write_lock_bh(&rxrpc_transport_lock);
228 list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
229 _debug("reap TRANS %d { u=%d t=%ld }",
230 trans->debug_id, atomic_read(&trans->usage),
231 (long) now - (long) trans->put_time);
233 if (likely(atomic_read(&trans->usage) > 0))
234 continue;
236 reap_time = trans->put_time + rxrpc_transport_timeout;
237 if (reap_time <= now)
238 list_move_tail(&trans->link, &graveyard);
239 else if (reap_time < earliest)
240 earliest = reap_time;
242 write_unlock_bh(&rxrpc_transport_lock);
244 if (earliest != ULONG_MAX) {
245 _debug("reschedule reaper %ld", (long) earliest - now);
246 ASSERTCMP(earliest, >, now);
247 rxrpc_queue_delayed_work(&rxrpc_transport_reap,
248 (earliest - now) * HZ);
251 /* then destroy all those pulled out */
252 while (!list_empty(&graveyard)) {
253 trans = list_entry(graveyard.next, struct rxrpc_transport,
254 link);
255 list_del_init(&trans->link);
257 ASSERTCMP(atomic_read(&trans->usage), ==, 0);
258 rxrpc_cleanup_transport(trans);
261 _leave("");
265 * preemptively destroy all the transport session records rather than waiting
266 * for them to time out
268 void __exit rxrpc_destroy_all_transports(void)
270 _enter("");
272 rxrpc_transport_timeout = 0;
273 cancel_delayed_work(&rxrpc_transport_reap);
274 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
276 _leave("");