blob: 1853cca66c680098b349de71a3a2487c33b55ef7 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/ref.c: TIPC object registry code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1991-2006, Ericsson AB
Allan Stephens4784b7c2008-04-16 18:21:16 -07005 * Copyright (c) 2004-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
Per Liden9ea1fd32006-01-11 13:30:43 +010010 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
Per Lidenb97bf3f2006-01-02 19:04:38 +010023 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "ref.h"
39#include "port.h"
40#include "subscr.h"
41#include "name_distr.h"
42#include "name_table.h"
43#include "config.h"
44#include "discover.h"
45#include "bearer.h"
46#include "node.h"
47#include "bcast.h"
48
Allan Stephens4784b7c2008-04-16 18:21:16 -070049/**
50 * struct reference - TIPC object reference entry
51 * @object: pointer to object associated with reference entry
52 * @lock: spinlock controlling access to object
Allan Stephens00895092008-04-16 18:21:47 -070053 * @data: reference value for object (combines instance & array index info)
Allan Stephens4784b7c2008-04-16 18:21:16 -070054 */
55
56struct reference {
57 void *object;
58 spinlock_t lock;
59 union {
60 u32 next_plus_upper;
61 u32 reference;
62 } data;
63};
64
65/**
66 * struct tipc_ref_table - table of TIPC object reference entries
67 * @entries: pointer to array of reference entries
Allan Stephens00895092008-04-16 18:21:47 -070068 * @capacity: array index of first unusable entry
69 * @init_point: array index of first uninitialized entry
Allan Stephens4784b7c2008-04-16 18:21:16 -070070 * @first_free: array index of first unused object reference entry
71 * @last_free: array index of last unused object reference entry
Allan Stephens00895092008-04-16 18:21:47 -070072 * @index_mask: bitmask for array index portion of reference values
73 * @start_mask: initial value for instance value portion of reference values
Allan Stephens4784b7c2008-04-16 18:21:16 -070074 */
75
76struct ref_table {
77 struct reference *entries;
Allan Stephens00895092008-04-16 18:21:47 -070078 u32 capacity;
79 u32 init_point;
Allan Stephens4784b7c2008-04-16 18:21:16 -070080 u32 first_free;
81 u32 last_free;
Allan Stephens00895092008-04-16 18:21:47 -070082 u32 index_mask;
83 u32 start_mask;
Allan Stephens4784b7c2008-04-16 18:21:16 -070084};
85
Per Lidenb97bf3f2006-01-02 19:04:38 +010086/*
87 * Object reference table consists of 2**N entries.
88 *
Allan Stephens00895092008-04-16 18:21:47 -070089 * State Object ptr Reference
90 * ----- ---------- ---------
91 * In use non-NULL XXXX|own index
92 * (XXXX changes each time entry is acquired)
93 * Free NULL YYYY|next free index
94 * (YYYY is one more than last used XXXX)
95 * Uninitialized NULL 0
Per Lidenb97bf3f2006-01-02 19:04:38 +010096 *
Allan Stephens00895092008-04-16 18:21:47 -070097 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 *
Allan Stephens00895092008-04-16 18:21:47 -070099 * Note that a reference value of 0 does not necessarily indicate that an
100 * entry is uninitialized, since the last entry in the free list could also
101 * have a reference value of 0 (although this is unlikely).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102 */
103
Allan Stephens4784b7c2008-04-16 18:21:16 -0700104static struct ref_table tipc_ref_table = { NULL };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105
Ingo Molnar34af9462006-06-27 02:53:55 -0700106static DEFINE_RWLOCK(ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107
108/**
Per Liden4323add2006-01-18 00:38:21 +0100109 * tipc_ref_table_init - create reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110 */
111
Per Liden4323add2006-01-18 00:38:21 +0100112int tipc_ref_table_init(u32 requested_size, u32 start)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
114 struct reference *table;
Allan Stephens00895092008-04-16 18:21:47 -0700115 u32 actual_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116
Allan Stephens00895092008-04-16 18:21:47 -0700117 /* account for unused entry, then round up size to a power of 2 */
118
119 requested_size++;
120 for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
121 /* do nothing */ ;
122
123 /* allocate table & mark all entries as uninitialized */
124
125 table = __vmalloc(actual_size * sizeof(struct reference),
126 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 if (table == NULL)
128 return -ENOMEM;
129
Per Liden4323add2006-01-18 00:38:21 +0100130 tipc_ref_table.entries = table;
Allan Stephens00895092008-04-16 18:21:47 -0700131 tipc_ref_table.capacity = requested_size;
132 tipc_ref_table.init_point = 1;
133 tipc_ref_table.first_free = 0;
134 tipc_ref_table.last_free = 0;
135 tipc_ref_table.index_mask = actual_size - 1;
136 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
137
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 return TIPC_OK;
139}
140
141/**
Per Liden4323add2006-01-18 00:38:21 +0100142 * tipc_ref_table_stop - destroy reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 */
144
Per Liden4323add2006-01-18 00:38:21 +0100145void tipc_ref_table_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
Per Liden4323add2006-01-18 00:38:21 +0100147 if (!tipc_ref_table.entries)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 return;
149
Per Liden4323add2006-01-18 00:38:21 +0100150 vfree(tipc_ref_table.entries);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800151 tipc_ref_table.entries = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152}
153
154/**
Per Liden4323add2006-01-18 00:38:21 +0100155 * tipc_ref_acquire - create reference to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900156 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 * Return a unique reference value which can be translated back to the pointer
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900158 * 'object' at a later time. Also, pass back a pointer to the lock protecting
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 * the object, but without locking it.
160 */
161
Per Liden4323add2006-01-18 00:38:21 +0100162u32 tipc_ref_acquire(void *object, spinlock_t **lock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163{
164 struct reference *entry;
165 u32 index;
166 u32 index_mask;
167 u32 next_plus_upper;
Allan Stephens00895092008-04-16 18:21:47 -0700168 u32 reference;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169
Allan Stephensf1310722006-06-25 23:51:37 -0700170 if (!object) {
171 err("Attempt to acquire reference to non-existent object\n");
172 return 0;
173 }
174 if (!tipc_ref_table.entries) {
175 err("Reference table not found during acquisition attempt\n");
176 return 0;
177 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178
Allan Stephens00895092008-04-16 18:21:47 -0700179 /* take a free entry, if available; otherwise initialize a new entry */
180
Per Liden4323add2006-01-18 00:38:21 +0100181 write_lock_bh(&ref_table_lock);
182 if (tipc_ref_table.first_free) {
183 index = tipc_ref_table.first_free;
184 entry = &(tipc_ref_table.entries[index]);
185 index_mask = tipc_ref_table.index_mask;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900186 /* take lock in case a previous user of entry still holds it */
187 spin_lock_bh(&entry->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188 next_plus_upper = entry->data.next_plus_upper;
Per Liden4323add2006-01-18 00:38:21 +0100189 tipc_ref_table.first_free = next_plus_upper & index_mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 reference = (next_plus_upper & ~index_mask) + index;
191 entry->data.reference = reference;
192 entry->object = object;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 spin_unlock_bh(&entry->lock);
Allan Stephens00895092008-04-16 18:21:47 -0700194 *lock = &entry->lock;
195 }
196 else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
197 index = tipc_ref_table.init_point++;
198 entry = &(tipc_ref_table.entries[index]);
199 spin_lock_init(&entry->lock);
200 reference = tipc_ref_table.start_mask + index;
201 entry->data.reference = reference;
202 entry->object = object;
203 *lock = &entry->lock;
204 }
205 else {
206 reference = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 }
Per Liden4323add2006-01-18 00:38:21 +0100208 write_unlock_bh(&ref_table_lock);
Allan Stephens00895092008-04-16 18:21:47 -0700209
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 return reference;
211}
212
213/**
Per Liden4323add2006-01-18 00:38:21 +0100214 * tipc_ref_discard - invalidate references to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 * Disallow future references to an object and free up the entry for re-use.
217 * Note: The entry's spin_lock may still be busy after discard
218 */
219
Per Liden4323add2006-01-18 00:38:21 +0100220void tipc_ref_discard(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
222 struct reference *entry;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 u32 index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 u32 index_mask;
225
Allan Stephensf1310722006-06-25 23:51:37 -0700226 if (!tipc_ref_table.entries) {
227 err("Reference table not found during discard attempt\n");
228 return;
229 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230
Per Liden4323add2006-01-18 00:38:21 +0100231 index_mask = tipc_ref_table.index_mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 index = ref & index_mask;
Per Liden4323add2006-01-18 00:38:21 +0100233 entry = &(tipc_ref_table.entries[index]);
Allan Stephensf1310722006-06-25 23:51:37 -0700234
Allan Stephens00895092008-04-16 18:21:47 -0700235 write_lock_bh(&ref_table_lock);
236
Allan Stephensf1310722006-06-25 23:51:37 -0700237 if (!entry->object) {
238 err("Attempt to discard reference to non-existent object\n");
239 goto exit;
240 }
241 if (entry->data.reference != ref) {
242 err("Attempt to discard non-existent reference\n");
243 goto exit;
244 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
Allan Stephens00895092008-04-16 18:21:47 -0700246 /*
247 * mark entry as unused; increment upper bits of entry's data field
248 * to invalidate any subsequent references
249 */
250
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800251 entry->object = NULL;
Allan Stephens00895092008-04-16 18:21:47 -0700252 entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
253
254 /* append entry to free entry list */
255
Per Liden4323add2006-01-18 00:38:21 +0100256 if (tipc_ref_table.first_free == 0)
257 tipc_ref_table.first_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 else
Allan Stephens4784b7c2008-04-16 18:21:16 -0700259 tipc_ref_table.entries[tipc_ref_table.last_free].
260 data.next_plus_upper |= index;
Per Liden4323add2006-01-18 00:38:21 +0100261 tipc_ref_table.last_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262
Allan Stephensf1310722006-06-25 23:51:37 -0700263exit:
Per Liden4323add2006-01-18 00:38:21 +0100264 write_unlock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265}
266
Allan Stephens4784b7c2008-04-16 18:21:16 -0700267/**
268 * tipc_ref_lock - lock referenced object and return pointer to it
269 */
270
271void *tipc_ref_lock(u32 ref)
272{
273 if (likely(tipc_ref_table.entries)) {
274 struct reference *r;
275
276 r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
Allan Stephens00895092008-04-16 18:21:47 -0700277
278 if (likely(r->data.reference != 0)) {
279 spin_lock_bh(&r->lock);
280 if (likely((r->data.reference == ref) && (r->object)))
281 return r->object;
282 spin_unlock_bh(&r->lock);
283 }
Allan Stephens4784b7c2008-04-16 18:21:16 -0700284 }
285 return NULL;
286}
287
288/**
289 * tipc_ref_unlock - unlock referenced object
290 */
291
292void tipc_ref_unlock(u32 ref)
293{
294 if (likely(tipc_ref_table.entries)) {
295 struct reference *r;
296
297 r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
Allan Stephens00895092008-04-16 18:21:47 -0700298
299 if (likely((r->data.reference == ref) && (r->object)))
Allan Stephens4784b7c2008-04-16 18:21:16 -0700300 spin_unlock_bh(&r->lock);
301 else
302 err("tipc_ref_unlock() invoked using "
Allan Stephens00895092008-04-16 18:21:47 -0700303 "invalid reference\n");
Allan Stephens4784b7c2008-04-16 18:21:16 -0700304 }
305}
306
307/**
308 * tipc_ref_deref - return pointer referenced object (without locking it)
309 */
310
311void *tipc_ref_deref(u32 ref)
312{
313 if (likely(tipc_ref_table.entries)) {
314 struct reference *r;
315
316 r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
317 if (likely(r->data.reference == ref))
318 return r->object;
319 }
320 return NULL;
321}
322