blob: 6704a58c785162e40989711bd061c44e6b8f25e8 [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
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, 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
49/*
50 * Object reference table consists of 2**N entries.
51 *
52 * A used entry has object ptr != 0, reference == XXXX|own index
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090053 * (XXXX changes each time entry is acquired)
Per Lidenb97bf3f2006-01-02 19:04:38 +010054 * A free entry has object ptr == 0, reference == YYYY|next free index
55 * (YYYY is one more than last used XXXX)
56 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090057 * Free list is initially chained from entry (2**N)-1 to entry 1.
Per Lidenb97bf3f2006-01-02 19:04:38 +010058 * Entry 0 is not used to allow index 0 to indicate the end of the free list.
59 *
60 * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
61 * because entry 0's reference field has the form XXXX|1--1.
62 */
63
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080064struct ref_table tipc_ref_table = { NULL };
Per Lidenb97bf3f2006-01-02 19:04:38 +010065
Ingo Molnar34af9462006-06-27 02:53:55 -070066static DEFINE_RWLOCK(ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
68/**
Per Liden4323add2006-01-18 00:38:21 +010069 * tipc_ref_table_init - create reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +010070 */
71
Per Liden4323add2006-01-18 00:38:21 +010072int tipc_ref_table_init(u32 requested_size, u32 start)
Per Lidenb97bf3f2006-01-02 19:04:38 +010073{
74 struct reference *table;
75 u32 sz = 1 << 4;
76 u32 index_mask;
77 int i;
78
79 while (sz < requested_size) {
80 sz <<= 1;
81 }
Panagiotis Issaris9df3f3d2006-07-21 15:52:20 -070082 table = vmalloc(sz * sizeof(*table));
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 if (table == NULL)
84 return -ENOMEM;
85
Per Liden4323add2006-01-18 00:38:21 +010086 write_lock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 index_mask = sz - 1;
88 for (i = sz - 1; i >= 0; i--) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080089 table[i].object = NULL;
Ingo Molnar34af9462006-06-27 02:53:55 -070090 spin_lock_init(&table[i].lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
92 }
Per Liden4323add2006-01-18 00:38:21 +010093 tipc_ref_table.entries = table;
94 tipc_ref_table.index_mask = index_mask;
95 tipc_ref_table.first_free = sz - 1;
96 tipc_ref_table.last_free = 1;
97 write_unlock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 return TIPC_OK;
99}
100
101/**
Per Liden4323add2006-01-18 00:38:21 +0100102 * tipc_ref_table_stop - destroy reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 */
104
Per Liden4323add2006-01-18 00:38:21 +0100105void tipc_ref_table_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106{
Per Liden4323add2006-01-18 00:38:21 +0100107 if (!tipc_ref_table.entries)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 return;
109
Per Liden4323add2006-01-18 00:38:21 +0100110 vfree(tipc_ref_table.entries);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800111 tipc_ref_table.entries = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112}
113
114/**
Per Liden4323add2006-01-18 00:38:21 +0100115 * tipc_ref_acquire - create reference to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900116 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 * Return a unique reference value which can be translated back to the pointer
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900118 * 'object' at a later time. Also, pass back a pointer to the lock protecting
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 * the object, but without locking it.
120 */
121
Per Liden4323add2006-01-18 00:38:21 +0100122u32 tipc_ref_acquire(void *object, spinlock_t **lock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123{
124 struct reference *entry;
125 u32 index;
126 u32 index_mask;
127 u32 next_plus_upper;
128 u32 reference = 0;
129
Allan Stephensf1310722006-06-25 23:51:37 -0700130 if (!object) {
131 err("Attempt to acquire reference to non-existent object\n");
132 return 0;
133 }
134 if (!tipc_ref_table.entries) {
135 err("Reference table not found during acquisition attempt\n");
136 return 0;
137 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138
Per Liden4323add2006-01-18 00:38:21 +0100139 write_lock_bh(&ref_table_lock);
140 if (tipc_ref_table.first_free) {
141 index = tipc_ref_table.first_free;
142 entry = &(tipc_ref_table.entries[index]);
143 index_mask = tipc_ref_table.index_mask;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900144 /* take lock in case a previous user of entry still holds it */
145 spin_lock_bh(&entry->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 next_plus_upper = entry->data.next_plus_upper;
Per Liden4323add2006-01-18 00:38:21 +0100147 tipc_ref_table.first_free = next_plus_upper & index_mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 reference = (next_plus_upper & ~index_mask) + index;
149 entry->data.reference = reference;
150 entry->object = object;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 if (lock != 0)
152 *lock = &entry->lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 spin_unlock_bh(&entry->lock);
154 }
Per Liden4323add2006-01-18 00:38:21 +0100155 write_unlock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 return reference;
157}
158
159/**
Per Liden4323add2006-01-18 00:38:21 +0100160 * tipc_ref_discard - invalidate references to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900161 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 * Disallow future references to an object and free up the entry for re-use.
163 * Note: The entry's spin_lock may still be busy after discard
164 */
165
Per Liden4323add2006-01-18 00:38:21 +0100166void tipc_ref_discard(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167{
168 struct reference *entry;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900169 u32 index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 u32 index_mask;
171
Allan Stephensf1310722006-06-25 23:51:37 -0700172 if (!ref) {
173 err("Attempt to discard reference 0\n");
174 return;
175 }
176 if (!tipc_ref_table.entries) {
177 err("Reference table not found during discard attempt\n");
178 return;
179 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180
Per Liden4323add2006-01-18 00:38:21 +0100181 write_lock_bh(&ref_table_lock);
182 index_mask = tipc_ref_table.index_mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 index = ref & index_mask;
Per Liden4323add2006-01-18 00:38:21 +0100184 entry = &(tipc_ref_table.entries[index]);
Allan Stephensf1310722006-06-25 23:51:37 -0700185
186 if (!entry->object) {
187 err("Attempt to discard reference to non-existent object\n");
188 goto exit;
189 }
190 if (entry->data.reference != ref) {
191 err("Attempt to discard non-existent reference\n");
192 goto exit;
193 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194
195 /* mark entry as unused */
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800196 entry->object = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100197 if (tipc_ref_table.first_free == 0)
198 tipc_ref_table.first_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 else
200 /* next_plus_upper is always XXXX|0--0 for last free entry */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900201 tipc_ref_table.entries[tipc_ref_table.last_free].data.next_plus_upper
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 |= index;
Per Liden4323add2006-01-18 00:38:21 +0100203 tipc_ref_table.last_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204
205 /* increment upper bits of entry to invalidate subsequent references */
206 entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
Allan Stephensf1310722006-06-25 23:51:37 -0700207exit:
Per Liden4323add2006-01-18 00:38:21 +0100208 write_unlock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209}
210