livepatch: move x86 specific ftrace handler code to arch/x86
[linux-2.6/btrfs-unstable.git] / net / rxrpc / ar-transport.c
blob1976dec84f297cfb126df6bcd53129f1d518001b
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 <linux/slab.h>
16 #include <net/sock.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
21 * Time after last use at which transport record is cleaned up.
23 unsigned rxrpc_transport_expiry = 3600 * 24;
25 static void rxrpc_transport_reaper(struct work_struct *work);
27 static LIST_HEAD(rxrpc_transports);
28 static DEFINE_RWLOCK(rxrpc_transport_lock);
29 static DECLARE_DELAYED_WORK(rxrpc_transport_reap, rxrpc_transport_reaper);
32 * allocate a new transport session manager
34 static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local,
35 struct rxrpc_peer *peer,
36 gfp_t gfp)
38 struct rxrpc_transport *trans;
40 _enter("");
42 trans = kzalloc(sizeof(struct rxrpc_transport), gfp);
43 if (trans) {
44 trans->local = local;
45 trans->peer = peer;
46 INIT_LIST_HEAD(&trans->link);
47 trans->bundles = RB_ROOT;
48 trans->client_conns = RB_ROOT;
49 trans->server_conns = RB_ROOT;
50 skb_queue_head_init(&trans->error_queue);
51 spin_lock_init(&trans->client_lock);
52 rwlock_init(&trans->conn_lock);
53 atomic_set(&trans->usage, 1);
54 trans->debug_id = atomic_inc_return(&rxrpc_debug_id);
56 if (peer->srx.transport.family == AF_INET) {
57 switch (peer->srx.transport_type) {
58 case SOCK_DGRAM:
59 INIT_WORK(&trans->error_handler,
60 rxrpc_UDP_error_handler);
61 break;
62 default:
63 BUG();
64 break;
66 } else {
67 BUG();
71 _leave(" = %p", trans);
72 return trans;
76 * obtain a transport session for the nominated endpoints
78 struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local,
79 struct rxrpc_peer *peer,
80 gfp_t gfp)
82 struct rxrpc_transport *trans, *candidate;
83 const char *new = "old";
84 int usage;
86 _enter("{%pI4+%hu},{%pI4+%hu},",
87 &local->srx.transport.sin.sin_addr,
88 ntohs(local->srx.transport.sin.sin_port),
89 &peer->srx.transport.sin.sin_addr,
90 ntohs(peer->srx.transport.sin.sin_port));
92 /* search the transport list first */
93 read_lock_bh(&rxrpc_transport_lock);
94 list_for_each_entry(trans, &rxrpc_transports, link) {
95 if (trans->local == local && trans->peer == peer)
96 goto found_extant_transport;
98 read_unlock_bh(&rxrpc_transport_lock);
100 /* not yet present - create a candidate for a new record and then
101 * redo the search */
102 candidate = rxrpc_alloc_transport(local, peer, gfp);
103 if (!candidate) {
104 _leave(" = -ENOMEM");
105 return ERR_PTR(-ENOMEM);
108 write_lock_bh(&rxrpc_transport_lock);
110 list_for_each_entry(trans, &rxrpc_transports, link) {
111 if (trans->local == local && trans->peer == peer)
112 goto found_extant_second;
115 /* we can now add the new candidate to the list */
116 trans = candidate;
117 candidate = NULL;
118 usage = atomic_read(&trans->usage);
120 rxrpc_get_local(trans->local);
121 atomic_inc(&trans->peer->usage);
122 list_add_tail(&trans->link, &rxrpc_transports);
123 write_unlock_bh(&rxrpc_transport_lock);
124 new = "new";
126 success:
127 _net("TRANSPORT %s %d local %d -> peer %d",
128 new,
129 trans->debug_id,
130 trans->local->debug_id,
131 trans->peer->debug_id);
133 _leave(" = %p {u=%d}", trans, usage);
134 return trans;
136 /* we found the transport in the list immediately */
137 found_extant_transport:
138 usage = atomic_inc_return(&trans->usage);
139 read_unlock_bh(&rxrpc_transport_lock);
140 goto success;
142 /* we found the transport on the second time through the list */
143 found_extant_second:
144 usage = atomic_inc_return(&trans->usage);
145 write_unlock_bh(&rxrpc_transport_lock);
146 kfree(candidate);
147 goto success;
151 * find the transport connecting two endpoints
153 struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
154 struct rxrpc_peer *peer)
156 struct rxrpc_transport *trans;
158 _enter("{%pI4+%hu},{%pI4+%hu},",
159 &local->srx.transport.sin.sin_addr,
160 ntohs(local->srx.transport.sin.sin_port),
161 &peer->srx.transport.sin.sin_addr,
162 ntohs(peer->srx.transport.sin.sin_port));
164 /* search the transport list */
165 read_lock_bh(&rxrpc_transport_lock);
167 list_for_each_entry(trans, &rxrpc_transports, link) {
168 if (trans->local == local && trans->peer == peer)
169 goto found_extant_transport;
172 read_unlock_bh(&rxrpc_transport_lock);
173 _leave(" = NULL");
174 return NULL;
176 found_extant_transport:
177 atomic_inc(&trans->usage);
178 read_unlock_bh(&rxrpc_transport_lock);
179 _leave(" = %p", trans);
180 return trans;
184 * release a transport session
186 void rxrpc_put_transport(struct rxrpc_transport *trans)
188 _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
190 ASSERTCMP(atomic_read(&trans->usage), >, 0);
192 trans->put_time = get_seconds();
193 if (unlikely(atomic_dec_and_test(&trans->usage))) {
194 _debug("zombie");
195 /* let the reaper determine the timeout to avoid a race with
196 * overextending the timeout if the reaper is running at the
197 * same time */
198 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
200 _leave("");
204 * clean up a transport session
206 static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
208 _net("DESTROY TRANS %d", trans->debug_id);
210 rxrpc_purge_queue(&trans->error_queue);
212 rxrpc_put_local(trans->local);
213 rxrpc_put_peer(trans->peer);
214 kfree(trans);
218 * reap dead transports that have passed their expiry date
220 static void rxrpc_transport_reaper(struct work_struct *work)
222 struct rxrpc_transport *trans, *_p;
223 unsigned long now, earliest, reap_time;
225 LIST_HEAD(graveyard);
227 _enter("");
229 now = get_seconds();
230 earliest = ULONG_MAX;
232 /* extract all the transports that have been dead too long */
233 write_lock_bh(&rxrpc_transport_lock);
234 list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
235 _debug("reap TRANS %d { u=%d t=%ld }",
236 trans->debug_id, atomic_read(&trans->usage),
237 (long) now - (long) trans->put_time);
239 if (likely(atomic_read(&trans->usage) > 0))
240 continue;
242 reap_time = trans->put_time + rxrpc_transport_expiry;
243 if (reap_time <= now)
244 list_move_tail(&trans->link, &graveyard);
245 else if (reap_time < earliest)
246 earliest = reap_time;
248 write_unlock_bh(&rxrpc_transport_lock);
250 if (earliest != ULONG_MAX) {
251 _debug("reschedule reaper %ld", (long) earliest - now);
252 ASSERTCMP(earliest, >, now);
253 rxrpc_queue_delayed_work(&rxrpc_transport_reap,
254 (earliest - now) * HZ);
257 /* then destroy all those pulled out */
258 while (!list_empty(&graveyard)) {
259 trans = list_entry(graveyard.next, struct rxrpc_transport,
260 link);
261 list_del_init(&trans->link);
263 ASSERTCMP(atomic_read(&trans->usage), ==, 0);
264 rxrpc_cleanup_transport(trans);
267 _leave("");
271 * preemptively destroy all the transport session records rather than waiting
272 * for them to time out
274 void __exit rxrpc_destroy_all_transports(void)
276 _enter("");
278 rxrpc_transport_expiry = 0;
279 cancel_delayed_work(&rxrpc_transport_reap);
280 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
282 _leave("");