Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * net/tipc/ref.c: TIPC object registry code |
| 3 | * |
| 4 | * Copyright (c) 2003-2005, Ericsson Research Canada |
| 5 | * Copyright (c) 2004-2005, Wind River Systems |
| 6 | * Copyright (c) 2005-2006, Ericsson AB |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
| 12 | * Redistributions of source code must retain the above copyright notice, this |
| 13 | * list of conditions and the following disclaimer. |
| 14 | * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer in the documentation |
| 16 | * and/or other materials provided with the distribution. |
| 17 | * Neither the names of the copyright holders nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include "core.h" |
| 35 | #include "ref.h" |
| 36 | #include "port.h" |
| 37 | #include "subscr.h" |
| 38 | #include "name_distr.h" |
| 39 | #include "name_table.h" |
| 40 | #include "config.h" |
| 41 | #include "discover.h" |
| 42 | #include "bearer.h" |
| 43 | #include "node.h" |
| 44 | #include "bcast.h" |
| 45 | |
| 46 | /* |
| 47 | * Object reference table consists of 2**N entries. |
| 48 | * |
| 49 | * A used entry has object ptr != 0, reference == XXXX|own index |
| 50 | * (XXXX changes each time entry is acquired) |
| 51 | * A free entry has object ptr == 0, reference == YYYY|next free index |
| 52 | * (YYYY is one more than last used XXXX) |
| 53 | * |
| 54 | * Free list is initially chained from entry (2**N)-1 to entry 1. |
| 55 | * Entry 0 is not used to allow index 0 to indicate the end of the free list. |
| 56 | * |
| 57 | * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0 |
| 58 | * because entry 0's reference field has the form XXXX|1--1. |
| 59 | */ |
| 60 | |
| 61 | struct ref_table ref_table = { 0 }; |
| 62 | |
| 63 | rwlock_t reftbl_lock = RW_LOCK_UNLOCKED; |
| 64 | |
| 65 | /** |
| 66 | * ref_table_init - create reference table for objects |
| 67 | */ |
| 68 | |
| 69 | int ref_table_init(u32 requested_size, u32 start) |
| 70 | { |
| 71 | struct reference *table; |
| 72 | u32 sz = 1 << 4; |
| 73 | u32 index_mask; |
| 74 | int i; |
| 75 | |
| 76 | while (sz < requested_size) { |
| 77 | sz <<= 1; |
| 78 | } |
| 79 | table = (struct reference *)vmalloc(sz * sizeof(struct reference)); |
| 80 | if (table == NULL) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | write_lock_bh(&reftbl_lock); |
| 84 | index_mask = sz - 1; |
| 85 | for (i = sz - 1; i >= 0; i--) { |
| 86 | table[i].object = 0; |
| 87 | table[i].lock = SPIN_LOCK_UNLOCKED; |
| 88 | table[i].data.next_plus_upper = (start & ~index_mask) + i - 1; |
| 89 | } |
| 90 | ref_table.entries = table; |
| 91 | ref_table.index_mask = index_mask; |
| 92 | ref_table.first_free = sz - 1; |
| 93 | ref_table.last_free = 1; |
| 94 | write_unlock_bh(&reftbl_lock); |
| 95 | return TIPC_OK; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * ref_table_stop - destroy reference table for objects |
| 100 | */ |
| 101 | |
| 102 | void ref_table_stop(void) |
| 103 | { |
| 104 | if (!ref_table.entries) |
| 105 | return; |
| 106 | |
| 107 | vfree(ref_table.entries); |
| 108 | ref_table.entries = 0; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * ref_acquire - create reference to an object |
| 113 | * |
| 114 | * Return a unique reference value which can be translated back to the pointer |
| 115 | * 'object' at a later time. Also, pass back a pointer to the lock protecting |
| 116 | * the object, but without locking it. |
| 117 | */ |
| 118 | |
| 119 | u32 ref_acquire(void *object, spinlock_t **lock) |
| 120 | { |
| 121 | struct reference *entry; |
| 122 | u32 index; |
| 123 | u32 index_mask; |
| 124 | u32 next_plus_upper; |
| 125 | u32 reference = 0; |
| 126 | |
| 127 | assert(ref_table.entries && object); |
| 128 | |
| 129 | write_lock_bh(&reftbl_lock); |
| 130 | if (ref_table.first_free) { |
| 131 | index = ref_table.first_free; |
| 132 | entry = &(ref_table.entries[index]); |
| 133 | index_mask = ref_table.index_mask; |
| 134 | /* take lock in case a previous user of entry still holds it */ |
| 135 | spin_lock_bh(&entry->lock); |
| 136 | next_plus_upper = entry->data.next_plus_upper; |
| 137 | ref_table.first_free = next_plus_upper & index_mask; |
| 138 | reference = (next_plus_upper & ~index_mask) + index; |
| 139 | entry->data.reference = reference; |
| 140 | entry->object = object; |
| 141 | if (lock != 0) |
| 142 | *lock = &entry->lock; |
| 143 | spin_unlock_bh(&entry->lock); |
| 144 | } |
| 145 | write_unlock_bh(&reftbl_lock); |
| 146 | return reference; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * ref_discard - invalidate references to an object |
| 151 | * |
| 152 | * Disallow future references to an object and free up the entry for re-use. |
| 153 | * Note: The entry's spin_lock may still be busy after discard |
| 154 | */ |
| 155 | |
| 156 | void ref_discard(u32 ref) |
| 157 | { |
| 158 | struct reference *entry; |
| 159 | u32 index; |
| 160 | u32 index_mask; |
| 161 | |
| 162 | assert(ref_table.entries); |
| 163 | assert(ref != 0); |
| 164 | |
| 165 | write_lock_bh(&reftbl_lock); |
| 166 | index_mask = ref_table.index_mask; |
| 167 | index = ref & index_mask; |
| 168 | entry = &(ref_table.entries[index]); |
| 169 | assert(entry->object != 0); |
| 170 | assert(entry->data.reference == ref); |
| 171 | |
| 172 | /* mark entry as unused */ |
| 173 | entry->object = 0; |
| 174 | if (ref_table.first_free == 0) |
| 175 | ref_table.first_free = index; |
| 176 | else |
| 177 | /* next_plus_upper is always XXXX|0--0 for last free entry */ |
| 178 | ref_table.entries[ref_table.last_free].data.next_plus_upper |
| 179 | |= index; |
| 180 | ref_table.last_free = index; |
| 181 | |
| 182 | /* increment upper bits of entry to invalidate subsequent references */ |
| 183 | entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1); |
| 184 | write_unlock_bh(&reftbl_lock); |
| 185 | } |
| 186 | |