]>
Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/ref.c: TIPC object registry code | |
3 | * | |
593a5f22 | 4 | * Copyright (c) 1991-2006, Ericsson AB |
b97bf3fd | 5 | * Copyright (c) 2004-2005, Wind River Systems |
b97bf3fd PL |
6 | * 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: | |
9ea1fd3c PL |
10 | * |
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. | |
b97bf3fd PL |
23 | * |
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 | |
53 | * (XXXX changes each time entry is acquired) | |
54 | * A free entry has object ptr == 0, reference == YYYY|next free index | |
55 | * (YYYY is one more than last used XXXX) | |
56 | * | |
57 | * Free list is initially chained from entry (2**N)-1 to entry 1. | |
58 | * 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 | ||
4323add6 | 64 | struct ref_table tipc_ref_table = { 0 }; |
b97bf3fd | 65 | |
4323add6 | 66 | static rwlock_t ref_table_lock = RW_LOCK_UNLOCKED; |
b97bf3fd PL |
67 | |
68 | /** | |
4323add6 | 69 | * tipc_ref_table_init - create reference table for objects |
b97bf3fd PL |
70 | */ |
71 | ||
4323add6 | 72 | int tipc_ref_table_init(u32 requested_size, u32 start) |
b97bf3fd PL |
73 | { |
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 | } | |
82 | table = (struct reference *)vmalloc(sz * sizeof(struct reference)); | |
83 | if (table == NULL) | |
84 | return -ENOMEM; | |
85 | ||
4323add6 | 86 | write_lock_bh(&ref_table_lock); |
b97bf3fd PL |
87 | index_mask = sz - 1; |
88 | for (i = sz - 1; i >= 0; i--) { | |
89 | table[i].object = 0; | |
90 | table[i].lock = SPIN_LOCK_UNLOCKED; | |
91 | table[i].data.next_plus_upper = (start & ~index_mask) + i - 1; | |
92 | } | |
4323add6 PL |
93 | 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); | |
b97bf3fd PL |
98 | return TIPC_OK; |
99 | } | |
100 | ||
101 | /** | |
4323add6 | 102 | * tipc_ref_table_stop - destroy reference table for objects |
b97bf3fd PL |
103 | */ |
104 | ||
4323add6 | 105 | void tipc_ref_table_stop(void) |
b97bf3fd | 106 | { |
4323add6 | 107 | if (!tipc_ref_table.entries) |
b97bf3fd PL |
108 | return; |
109 | ||
4323add6 PL |
110 | vfree(tipc_ref_table.entries); |
111 | tipc_ref_table.entries = 0; | |
b97bf3fd PL |
112 | } |
113 | ||
114 | /** | |
4323add6 | 115 | * tipc_ref_acquire - create reference to an object |
b97bf3fd PL |
116 | * |
117 | * Return a unique reference value which can be translated back to the pointer | |
118 | * 'object' at a later time. Also, pass back a pointer to the lock protecting | |
119 | * the object, but without locking it. | |
120 | */ | |
121 | ||
4323add6 | 122 | u32 tipc_ref_acquire(void *object, spinlock_t **lock) |
b97bf3fd PL |
123 | { |
124 | struct reference *entry; | |
125 | u32 index; | |
126 | u32 index_mask; | |
127 | u32 next_plus_upper; | |
128 | u32 reference = 0; | |
129 | ||
4323add6 | 130 | assert(tipc_ref_table.entries && object); |
b97bf3fd | 131 | |
4323add6 PL |
132 | write_lock_bh(&ref_table_lock); |
133 | if (tipc_ref_table.first_free) { | |
134 | index = tipc_ref_table.first_free; | |
135 | entry = &(tipc_ref_table.entries[index]); | |
136 | index_mask = tipc_ref_table.index_mask; | |
b97bf3fd PL |
137 | /* take lock in case a previous user of entry still holds it */ |
138 | spin_lock_bh(&entry->lock); | |
139 | next_plus_upper = entry->data.next_plus_upper; | |
4323add6 | 140 | tipc_ref_table.first_free = next_plus_upper & index_mask; |
b97bf3fd PL |
141 | reference = (next_plus_upper & ~index_mask) + index; |
142 | entry->data.reference = reference; | |
143 | entry->object = object; | |
144 | if (lock != 0) | |
145 | *lock = &entry->lock; | |
146 | spin_unlock_bh(&entry->lock); | |
147 | } | |
4323add6 | 148 | write_unlock_bh(&ref_table_lock); |
b97bf3fd PL |
149 | return reference; |
150 | } | |
151 | ||
152 | /** | |
4323add6 | 153 | * tipc_ref_discard - invalidate references to an object |
b97bf3fd PL |
154 | * |
155 | * Disallow future references to an object and free up the entry for re-use. | |
156 | * Note: The entry's spin_lock may still be busy after discard | |
157 | */ | |
158 | ||
4323add6 | 159 | void tipc_ref_discard(u32 ref) |
b97bf3fd PL |
160 | { |
161 | struct reference *entry; | |
162 | u32 index; | |
163 | u32 index_mask; | |
164 | ||
4323add6 | 165 | assert(tipc_ref_table.entries); |
b97bf3fd PL |
166 | assert(ref != 0); |
167 | ||
4323add6 PL |
168 | write_lock_bh(&ref_table_lock); |
169 | index_mask = tipc_ref_table.index_mask; | |
b97bf3fd | 170 | index = ref & index_mask; |
4323add6 | 171 | entry = &(tipc_ref_table.entries[index]); |
b97bf3fd PL |
172 | assert(entry->object != 0); |
173 | assert(entry->data.reference == ref); | |
174 | ||
175 | /* mark entry as unused */ | |
176 | entry->object = 0; | |
4323add6 PL |
177 | if (tipc_ref_table.first_free == 0) |
178 | tipc_ref_table.first_free = index; | |
b97bf3fd PL |
179 | else |
180 | /* next_plus_upper is always XXXX|0--0 for last free entry */ | |
4323add6 | 181 | tipc_ref_table.entries[tipc_ref_table.last_free].data.next_plus_upper |
b97bf3fd | 182 | |= index; |
4323add6 | 183 | tipc_ref_table.last_free = index; |
b97bf3fd PL |
184 | |
185 | /* increment upper bits of entry to invalidate subsequent references */ | |
186 | entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1); | |
4323add6 | 187 | write_unlock_bh(&ref_table_lock); |
b97bf3fd PL |
188 | } |
189 |