2006-01-03 02:04:38 +08:00
|
|
|
/*
|
|
|
|
* net/tipc/ref.c: TIPC object registry code
|
2007-02-09 22:25:21 +08:00
|
|
|
*
|
2006-01-12 02:14:19 +08:00
|
|
|
* Copyright (c) 1991-2006, Ericsson AB
|
2008-04-17 09:21:16 +08:00
|
|
|
* Copyright (c) 2004-2007, Wind River Systems
|
2006-01-03 02:04:38 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2006-01-11 20:30:43 +08:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the names of the copyright holders nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* Alternatively, this software may be distributed under the terms of the
|
|
|
|
* GNU General Public License ("GPL") version 2 as published by the Free
|
|
|
|
* Software Foundation.
|
2006-01-03 02:04:38 +08:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "core.h"
|
|
|
|
#include "ref.h"
|
|
|
|
#include "port.h"
|
|
|
|
#include "subscr.h"
|
|
|
|
#include "name_distr.h"
|
|
|
|
#include "name_table.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "discover.h"
|
|
|
|
#include "bearer.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "bcast.h"
|
|
|
|
|
2008-04-17 09:21:16 +08:00
|
|
|
/**
|
|
|
|
* struct reference - TIPC object reference entry
|
|
|
|
* @object: pointer to object associated with reference entry
|
|
|
|
* @lock: spinlock controlling access to object
|
|
|
|
* @data: reference value associated with object (or link to next unused entry)
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct reference {
|
|
|
|
void *object;
|
|
|
|
spinlock_t lock;
|
|
|
|
union {
|
|
|
|
u32 next_plus_upper;
|
|
|
|
u32 reference;
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct tipc_ref_table - table of TIPC object reference entries
|
|
|
|
* @entries: pointer to array of reference entries
|
|
|
|
* @index_mask: bitmask for array index portion of reference values
|
|
|
|
* @first_free: array index of first unused object reference entry
|
|
|
|
* @last_free: array index of last unused object reference entry
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ref_table {
|
|
|
|
struct reference *entries;
|
|
|
|
u32 index_mask;
|
|
|
|
u32 first_free;
|
|
|
|
u32 last_free;
|
|
|
|
};
|
|
|
|
|
2006-01-03 02:04:38 +08:00
|
|
|
/*
|
|
|
|
* Object reference table consists of 2**N entries.
|
|
|
|
*
|
|
|
|
* A used entry has object ptr != 0, reference == XXXX|own index
|
2007-02-09 22:25:21 +08:00
|
|
|
* (XXXX changes each time entry is acquired)
|
2006-01-03 02:04:38 +08:00
|
|
|
* A free entry has object ptr == 0, reference == YYYY|next free index
|
|
|
|
* (YYYY is one more than last used XXXX)
|
|
|
|
*
|
2007-02-09 22:25:21 +08:00
|
|
|
* Free list is initially chained from entry (2**N)-1 to entry 1.
|
2006-01-03 02:04:38 +08:00
|
|
|
* Entry 0 is not used to allow index 0 to indicate the end of the free list.
|
|
|
|
*
|
|
|
|
* Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
|
|
|
|
* because entry 0's reference field has the form XXXX|1--1.
|
|
|
|
*/
|
|
|
|
|
2008-04-17 09:21:16 +08:00
|
|
|
static struct ref_table tipc_ref_table = { NULL };
|
2006-01-03 02:04:38 +08:00
|
|
|
|
2006-06-27 17:53:55 +08:00
|
|
|
static DEFINE_RWLOCK(ref_table_lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
|
|
|
|
/**
|
2006-01-18 07:38:21 +08:00
|
|
|
* tipc_ref_table_init - create reference table for objects
|
2006-01-03 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
int tipc_ref_table_init(u32 requested_size, u32 start)
|
2006-01-03 02:04:38 +08:00
|
|
|
{
|
|
|
|
struct reference *table;
|
|
|
|
u32 sz = 1 << 4;
|
|
|
|
u32 index_mask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (sz < requested_size) {
|
|
|
|
sz <<= 1;
|
|
|
|
}
|
2006-07-22 06:52:20 +08:00
|
|
|
table = vmalloc(sz * sizeof(*table));
|
2006-01-03 02:04:38 +08:00
|
|
|
if (table == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
write_lock_bh(&ref_table_lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
index_mask = sz - 1;
|
|
|
|
for (i = sz - 1; i >= 0; i--) {
|
2006-03-21 14:36:47 +08:00
|
|
|
table[i].object = NULL;
|
2006-06-27 17:53:55 +08:00
|
|
|
spin_lock_init(&table[i].lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
|
|
|
|
}
|
2006-01-18 07:38:21 +08:00
|
|
|
tipc_ref_table.entries = table;
|
|
|
|
tipc_ref_table.index_mask = index_mask;
|
|
|
|
tipc_ref_table.first_free = sz - 1;
|
|
|
|
tipc_ref_table.last_free = 1;
|
|
|
|
write_unlock_bh(&ref_table_lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
return TIPC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-01-18 07:38:21 +08:00
|
|
|
* tipc_ref_table_stop - destroy reference table for objects
|
2006-01-03 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
void tipc_ref_table_stop(void)
|
2006-01-03 02:04:38 +08:00
|
|
|
{
|
2006-01-18 07:38:21 +08:00
|
|
|
if (!tipc_ref_table.entries)
|
2006-01-03 02:04:38 +08:00
|
|
|
return;
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
vfree(tipc_ref_table.entries);
|
2006-03-21 14:36:47 +08:00
|
|
|
tipc_ref_table.entries = NULL;
|
2006-01-03 02:04:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-01-18 07:38:21 +08:00
|
|
|
* tipc_ref_acquire - create reference to an object
|
2007-02-09 22:25:21 +08:00
|
|
|
*
|
2006-01-03 02:04:38 +08:00
|
|
|
* Return a unique reference value which can be translated back to the pointer
|
2007-02-09 22:25:21 +08:00
|
|
|
* 'object' at a later time. Also, pass back a pointer to the lock protecting
|
2006-01-03 02:04:38 +08:00
|
|
|
* the object, but without locking it.
|
|
|
|
*/
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
u32 tipc_ref_acquire(void *object, spinlock_t **lock)
|
2006-01-03 02:04:38 +08:00
|
|
|
{
|
|
|
|
struct reference *entry;
|
|
|
|
u32 index;
|
|
|
|
u32 index_mask;
|
|
|
|
u32 next_plus_upper;
|
|
|
|
u32 reference = 0;
|
|
|
|
|
2006-06-26 14:51:37 +08:00
|
|
|
if (!object) {
|
|
|
|
err("Attempt to acquire reference to non-existent object\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!tipc_ref_table.entries) {
|
|
|
|
err("Reference table not found during acquisition attempt\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-03 02:04:38 +08:00
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
write_lock_bh(&ref_table_lock);
|
|
|
|
if (tipc_ref_table.first_free) {
|
|
|
|
index = tipc_ref_table.first_free;
|
|
|
|
entry = &(tipc_ref_table.entries[index]);
|
|
|
|
index_mask = tipc_ref_table.index_mask;
|
2007-02-09 22:25:21 +08:00
|
|
|
/* take lock in case a previous user of entry still holds it */
|
|
|
|
spin_lock_bh(&entry->lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
next_plus_upper = entry->data.next_plus_upper;
|
2006-01-18 07:38:21 +08:00
|
|
|
tipc_ref_table.first_free = next_plus_upper & index_mask;
|
2006-01-03 02:04:38 +08:00
|
|
|
reference = (next_plus_upper & ~index_mask) + index;
|
|
|
|
entry->data.reference = reference;
|
|
|
|
entry->object = object;
|
2008-02-25 10:38:31 +08:00
|
|
|
if (lock != NULL)
|
2007-02-09 22:25:21 +08:00
|
|
|
*lock = &entry->lock;
|
2006-01-03 02:04:38 +08:00
|
|
|
spin_unlock_bh(&entry->lock);
|
|
|
|
}
|
2006-01-18 07:38:21 +08:00
|
|
|
write_unlock_bh(&ref_table_lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-01-18 07:38:21 +08:00
|
|
|
* tipc_ref_discard - invalidate references to an object
|
2007-02-09 22:25:21 +08:00
|
|
|
*
|
2006-01-03 02:04:38 +08:00
|
|
|
* Disallow future references to an object and free up the entry for re-use.
|
|
|
|
* Note: The entry's spin_lock may still be busy after discard
|
|
|
|
*/
|
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
void tipc_ref_discard(u32 ref)
|
2006-01-03 02:04:38 +08:00
|
|
|
{
|
|
|
|
struct reference *entry;
|
2007-02-09 22:25:21 +08:00
|
|
|
u32 index;
|
2006-01-03 02:04:38 +08:00
|
|
|
u32 index_mask;
|
|
|
|
|
2006-06-26 14:51:37 +08:00
|
|
|
if (!ref) {
|
|
|
|
err("Attempt to discard reference 0\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!tipc_ref_table.entries) {
|
|
|
|
err("Reference table not found during discard attempt\n");
|
|
|
|
return;
|
|
|
|
}
|
2006-01-03 02:04:38 +08:00
|
|
|
|
2006-01-18 07:38:21 +08:00
|
|
|
write_lock_bh(&ref_table_lock);
|
|
|
|
index_mask = tipc_ref_table.index_mask;
|
2006-01-03 02:04:38 +08:00
|
|
|
index = ref & index_mask;
|
2006-01-18 07:38:21 +08:00
|
|
|
entry = &(tipc_ref_table.entries[index]);
|
2006-06-26 14:51:37 +08:00
|
|
|
|
|
|
|
if (!entry->object) {
|
|
|
|
err("Attempt to discard reference to non-existent object\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (entry->data.reference != ref) {
|
|
|
|
err("Attempt to discard non-existent reference\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
2006-01-03 02:04:38 +08:00
|
|
|
|
|
|
|
/* mark entry as unused */
|
2006-03-21 14:36:47 +08:00
|
|
|
entry->object = NULL;
|
2006-01-18 07:38:21 +08:00
|
|
|
if (tipc_ref_table.first_free == 0)
|
|
|
|
tipc_ref_table.first_free = index;
|
2006-01-03 02:04:38 +08:00
|
|
|
else
|
|
|
|
/* next_plus_upper is always XXXX|0--0 for last free entry */
|
2008-04-17 09:21:16 +08:00
|
|
|
tipc_ref_table.entries[tipc_ref_table.last_free].
|
|
|
|
data.next_plus_upper |= index;
|
2006-01-18 07:38:21 +08:00
|
|
|
tipc_ref_table.last_free = index;
|
2006-01-03 02:04:38 +08:00
|
|
|
|
|
|
|
/* increment upper bits of entry to invalidate subsequent references */
|
|
|
|
entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
|
2006-06-26 14:51:37 +08:00
|
|
|
exit:
|
2006-01-18 07:38:21 +08:00
|
|
|
write_unlock_bh(&ref_table_lock);
|
2006-01-03 02:04:38 +08:00
|
|
|
}
|
|
|
|
|
2008-04-17 09:21:16 +08:00
|
|
|
/**
|
|
|
|
* tipc_ref_lock - lock referenced object and return pointer to it
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *tipc_ref_lock(u32 ref)
|
|
|
|
{
|
|
|
|
if (likely(tipc_ref_table.entries)) {
|
|
|
|
struct reference *r;
|
|
|
|
|
|
|
|
r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
|
|
|
|
spin_lock_bh(&r->lock);
|
|
|
|
if (likely(r->data.reference == ref))
|
|
|
|
return r->object;
|
|
|
|
spin_unlock_bh(&r->lock);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tipc_ref_unlock - unlock referenced object
|
|
|
|
*/
|
|
|
|
|
|
|
|
void tipc_ref_unlock(u32 ref)
|
|
|
|
{
|
|
|
|
if (likely(tipc_ref_table.entries)) {
|
|
|
|
struct reference *r;
|
|
|
|
|
|
|
|
r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
|
|
|
|
if (likely(r->data.reference == ref))
|
|
|
|
spin_unlock_bh(&r->lock);
|
|
|
|
else
|
|
|
|
err("tipc_ref_unlock() invoked using "
|
|
|
|
"obsolete reference\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tipc_ref_deref - return pointer referenced object (without locking it)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *tipc_ref_deref(u32 ref)
|
|
|
|
{
|
|
|
|
if (likely(tipc_ref_table.entries)) {
|
|
|
|
struct reference *r;
|
|
|
|
|
|
|
|
r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
|
|
|
|
if (likely(r->data.reference == ref))
|
|
|
|
return r->object;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|