2002-01-25 15:19:01 +08:00
|
|
|
/* cache.c - routines to maintain an in-core cache of entries */
|
|
|
|
/* $OpenLDAP$ */
|
2003-11-29 05:08:20 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2008-01-08 08:19:56 +08:00
|
|
|
* Copyright 2000-2008 The OpenLDAP Foundation.
|
2003-11-29 05:08:20 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2002-01-25 15:19:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
|
|
|
|
#include "back-bdb.h"
|
|
|
|
|
2005-12-09 08:20:18 +08:00
|
|
|
#include "ldap_rq.h"
|
|
|
|
|
2004-11-01 06:07:15 +08:00
|
|
|
#ifdef BDB_HIER
|
2007-01-01 00:16:48 +08:00
|
|
|
#define bdb_cache_lru_purge hdb_cache_lru_purge
|
2004-11-01 06:07:15 +08:00
|
|
|
#endif
|
2007-01-04 19:01:59 +08:00
|
|
|
static void bdb_cache_lru_purge( struct bdb_info *bdb );
|
2004-11-01 06:07:15 +08:00
|
|
|
|
|
|
|
static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
|
2002-01-25 15:19:01 +08:00
|
|
|
#ifdef LDAP_DEBUG
|
2007-01-12 15:35:34 +08:00
|
|
|
#define SLAPD_UNUSED
|
2005-11-15 05:47:17 +08:00
|
|
|
#ifdef SLAPD_UNUSED
|
2002-04-08 17:43:22 +08:00
|
|
|
static void bdb_lru_print(Cache *cache);
|
2002-01-25 15:19:01 +08:00
|
|
|
#endif
|
2005-11-15 05:47:17 +08:00
|
|
|
#endif
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
/* For concurrency experiments only! */
|
|
|
|
#if 0
|
|
|
|
#define ldap_pvt_thread_rdwr_wlock(a) 0
|
|
|
|
#define ldap_pvt_thread_rdwr_wunlock(a) 0
|
|
|
|
#define ldap_pvt_thread_rdwr_rlock(a) 0
|
|
|
|
#define ldap_pvt_thread_rdwr_runlock(a) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define ldap_pvt_thread_mutex_trylock(a) 0
|
|
|
|
#endif
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
static EntryInfo *
|
2003-09-27 12:58:36 +08:00
|
|
|
bdb_cache_entryinfo_new( Cache *cache )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-09-27 12:58:36 +08:00
|
|
|
EntryInfo *ei = NULL;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-09-27 12:58:36 +08:00
|
|
|
if ( cache->c_eifree ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
|
2003-09-27 12:58:36 +08:00
|
|
|
if ( cache->c_eifree ) {
|
|
|
|
ei = cache->c_eifree;
|
|
|
|
cache->c_eifree = ei->bei_lrunext;
|
2007-08-25 22:58:43 +08:00
|
|
|
ei->bei_finders = 0;
|
|
|
|
}
|
2007-08-25 23:01:27 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
|
2003-09-27 12:58:36 +08:00
|
|
|
}
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( !ei ) {
|
|
|
|
ei = ch_calloc(1, sizeof(EntryInfo));
|
2003-09-27 12:58:36 +08:00
|
|
|
ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
ei->bei_state = CACHE_ENTRY_REFERENCED;
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
return ei;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2007-01-17 10:43:05 +08:00
|
|
|
static void
|
|
|
|
bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
|
|
|
|
{
|
|
|
|
free( ei->bei_nrdn.bv_val );
|
|
|
|
ei->bei_nrdn.bv_val = NULL;
|
|
|
|
#ifdef BDB_HIER
|
|
|
|
free( ei->bei_rdn.bv_val );
|
|
|
|
ei->bei_rdn.bv_val = NULL;
|
|
|
|
ei->bei_modrdns = 0;
|
|
|
|
ei->bei_ckids = 0;
|
|
|
|
ei->bei_dkids = 0;
|
|
|
|
#endif
|
|
|
|
ei->bei_parent = NULL;
|
|
|
|
ei->bei_kids = NULL;
|
|
|
|
ei->bei_lruprev = NULL;
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
|
|
|
|
ei->bei_lrunext = cache->c_eifree;
|
|
|
|
cache->c_eifree = ei;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
|
|
|
|
}
|
|
|
|
|
2007-01-12 15:35:34 +08:00
|
|
|
#define LRU_DEL( c, e ) do { \
|
|
|
|
if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
|
|
|
|
if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
|
|
|
|
e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
|
|
|
|
e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
|
|
|
|
e->bei_lruprev = NULL; \
|
|
|
|
} while ( 0 )
|
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
/* Note - we now use a Second-Chance / Clock algorithm instead of
|
|
|
|
* Least-Recently-Used. This tremendously improves concurrency
|
|
|
|
* because we no longer need to manipulate the lists every time an
|
|
|
|
* entry is touched. We only need to lock the lists when adding
|
|
|
|
* or deleting an entry. It's now a circular doubly-linked list.
|
|
|
|
* We always append to the tail, but the head traverses the circle
|
|
|
|
* during a purge operation.
|
|
|
|
*/
|
|
|
|
static void
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
|
2007-01-01 00:16:48 +08:00
|
|
|
{
|
2007-01-12 15:35:34 +08:00
|
|
|
|
2007-11-12 19:16:30 +08:00
|
|
|
/* Already linked, ignore */
|
|
|
|
if ( ei->bei_lruprev )
|
|
|
|
return;
|
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
/* Insert into circular LRU list */
|
2007-01-05 14:49:25 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
|
2007-01-12 15:35:34 +08:00
|
|
|
|
2007-01-05 14:49:25 +08:00
|
|
|
ei->bei_lruprev = bdb->bi_cache.c_lrutail;
|
|
|
|
if ( bdb->bi_cache.c_lrutail ) {
|
|
|
|
ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
|
|
|
|
bdb->bi_cache.c_lrutail->bei_lrunext = ei;
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( ei->bei_lrunext )
|
|
|
|
ei->bei_lrunext->bei_lruprev = ei;
|
|
|
|
} else {
|
|
|
|
ei->bei_lrunext = ei->bei_lruprev = ei;
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb->bi_cache.c_lruhead = ei;
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb->bi_cache.c_lrutail = ei;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NO_THREADS
|
|
|
|
#define NO_DB_LOCK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* #define NO_DB_LOCK 1 */
|
|
|
|
/* Note: The BerkeleyDB locks are much slower than regular
|
|
|
|
* mutexes or rdwr locks. But the BDB implementation has the
|
|
|
|
* advantage of using a fixed size lock table, instead of
|
|
|
|
* allocating a lock object per entry in the DB. That's a
|
|
|
|
* key benefit for scaling. It also frees us from worrying
|
|
|
|
* about undetectable deadlocks between BDB activity and our
|
|
|
|
* own cache activity. It's still worth exploring faster
|
|
|
|
* alternatives though.
|
|
|
|
*/
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Atomically release and reacquire a lock */
|
2003-08-23 10:51:33 +08:00
|
|
|
int
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_cache_entry_db_relock(
|
2007-01-01 07:37:06 +08:00
|
|
|
struct bdb_info *bdb,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo *ei,
|
|
|
|
int rw,
|
|
|
|
int tryOnly,
|
|
|
|
DB_LOCK *lock )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2007-01-01 00:16:48 +08:00
|
|
|
#ifdef NO_DB_LOCK
|
2003-04-17 00:23:36 +08:00
|
|
|
return 0;
|
2002-01-25 15:19:01 +08:00
|
|
|
#else
|
2003-04-17 00:23:36 +08:00
|
|
|
int rc;
|
|
|
|
DBT lockobj;
|
|
|
|
DB_LOCKREQ list[2];
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( !lock ) return 0;
|
|
|
|
|
2004-06-25 01:56:43 +08:00
|
|
|
lockobj.data = &ei->bei_id;
|
2004-06-25 07:35:18 +08:00
|
|
|
lockobj.size = sizeof(ei->bei_id) + 1;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
list[0].op = DB_LOCK_PUT;
|
|
|
|
list[0].lock = *lock;
|
|
|
|
list[1].op = DB_LOCK_GET;
|
|
|
|
list[1].lock = *lock;
|
|
|
|
list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
|
|
|
|
list[1].obj = &lockobj;
|
2007-08-18 04:33:46 +08:00
|
|
|
rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
|
2003-04-17 00:23:36 +08:00
|
|
|
list, 2, NULL );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2004-07-13 04:02:24 +08:00
|
|
|
if (rc && !tryOnly) {
|
2003-04-17 00:23:36 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
2003-04-29 22:55:36 +08:00
|
|
|
"bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
|
2003-04-17 00:23:36 +08:00
|
|
|
ei->bei_id, rw, rc );
|
|
|
|
} else {
|
|
|
|
*lock = list[1].lock;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
return rc;
|
2003-03-26 04:17:05 +08:00
|
|
|
#endif
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
2003-12-31 12:26:17 +08:00
|
|
|
|
2003-04-23 14:34:55 +08:00
|
|
|
static int
|
2007-08-18 04:33:46 +08:00
|
|
|
bdb_cache_entry_db_lock( struct bdb_info *bdb, BDB_LOCKER locker, EntryInfo *ei,
|
2003-12-31 12:26:17 +08:00
|
|
|
int rw, int tryOnly, DB_LOCK *lock )
|
2002-06-01 04:49:19 +08:00
|
|
|
{
|
2007-01-01 00:16:48 +08:00
|
|
|
#ifdef NO_DB_LOCK
|
2002-09-19 14:34:53 +08:00
|
|
|
return 0;
|
|
|
|
#else
|
2002-06-01 04:49:19 +08:00
|
|
|
int rc;
|
|
|
|
DBT lockobj;
|
|
|
|
int db_rw;
|
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( !lock ) return 0;
|
|
|
|
|
2002-06-01 04:49:19 +08:00
|
|
|
if (rw)
|
|
|
|
db_rw = DB_LOCK_WRITE;
|
|
|
|
else
|
|
|
|
db_rw = DB_LOCK_READ;
|
|
|
|
|
2004-06-25 01:56:43 +08:00
|
|
|
lockobj.data = &ei->bei_id;
|
2004-06-25 07:35:18 +08:00
|
|
|
lockobj.size = sizeof(ei->bei_id) + 1;
|
2003-04-17 00:23:36 +08:00
|
|
|
|
2007-08-18 04:33:46 +08:00
|
|
|
rc = LOCK_GET(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
|
2002-06-01 04:49:19 +08:00
|
|
|
&lockobj, db_rw, lock);
|
2004-07-13 04:02:24 +08:00
|
|
|
if (rc && !tryOnly) {
|
2002-08-25 03:23:30 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
2003-04-29 22:55:36 +08:00
|
|
|
"bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
|
2003-04-17 00:23:36 +08:00
|
|
|
ei->bei_id, rw, rc );
|
2002-08-25 03:23:30 +08:00
|
|
|
}
|
2002-06-01 04:49:19 +08:00
|
|
|
return rc;
|
2007-01-01 00:16:48 +08:00
|
|
|
#endif /* NO_DB_LOCK */
|
2002-06-01 04:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
|
2002-06-01 04:49:19 +08:00
|
|
|
{
|
2007-01-01 00:16:48 +08:00
|
|
|
#ifdef NO_DB_LOCK
|
2002-09-19 14:34:53 +08:00
|
|
|
return 0;
|
|
|
|
#else
|
2002-06-01 04:49:19 +08:00
|
|
|
int rc;
|
|
|
|
|
2006-06-01 10:03:10 +08:00
|
|
|
if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
|
2003-11-25 18:31:10 +08:00
|
|
|
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = LOCK_PUT ( bdb->bi_dbenv, lock );
|
2002-06-01 04:49:19 +08:00
|
|
|
return rc;
|
2002-09-19 14:34:53 +08:00
|
|
|
#endif
|
2002-06-01 04:49:19 +08:00
|
|
|
}
|
|
|
|
|
2007-11-12 19:16:30 +08:00
|
|
|
void
|
|
|
|
bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
|
|
|
|
int rw, DB_LOCK *lock )
|
|
|
|
{
|
|
|
|
EntryInfo *ei;
|
|
|
|
int free = 0;
|
|
|
|
|
|
|
|
ei = e->e_private;
|
2008-04-03 16:02:11 +08:00
|
|
|
if ( ei &&
|
|
|
|
( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
|
|
|
|
( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
|
2008-02-08 14:32:41 +08:00
|
|
|
if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
|
2008-04-03 16:02:11 +08:00
|
|
|
/* Releasing the entry can only be done when
|
|
|
|
* we know that nobody else is using it, i.e we
|
|
|
|
* should have an entry_db writelock. But the
|
|
|
|
* flag is only set by the thread that loads the
|
|
|
|
* entry, and only if no other threads has found
|
|
|
|
* it while it was working. All other threads
|
|
|
|
* clear the flag, which mean that we should be
|
|
|
|
* the only thread using the entry if the flag
|
|
|
|
* is set here.
|
|
|
|
*/
|
2008-02-08 14:32:41 +08:00
|
|
|
ei->bei_e = NULL;
|
|
|
|
ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
|
|
|
|
free = 1;
|
|
|
|
}
|
|
|
|
bdb_cache_entryinfo_unlock( ei );
|
2007-11-12 19:16:30 +08:00
|
|
|
}
|
2008-04-03 16:02:11 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, lock );
|
2007-11-12 19:16:30 +08:00
|
|
|
if ( free ) {
|
|
|
|
e->e_private = NULL;
|
|
|
|
bdb_entry_return( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-25 15:19:01 +08:00
|
|
|
static int
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_cache_entryinfo_destroy( EntryInfo *e )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
|
|
|
|
free( e->bei_nrdn.bv_val );
|
2003-04-23 04:35:29 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
free( e->bei_rdn.bv_val );
|
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
free( e );
|
2002-01-25 15:19:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-04-23 18:26:06 +08:00
|
|
|
/* Do a length-ordered sort on normalized RDNs */
|
2003-04-17 00:23:36 +08:00
|
|
|
static int
|
|
|
|
bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
const EntryInfo *e1 = v_e1, *e2 = v_e2;
|
2003-04-23 18:26:06 +08:00
|
|
|
int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
|
2003-12-31 09:41:16 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
|
|
|
|
e1->bei_nrdn.bv_len );
|
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
return rc;
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
static int
|
|
|
|
bdb_id_cmp( const void *v_e1, const void *v_e2 )
|
|
|
|
{
|
|
|
|
const EntryInfo *e1 = v_e1, *e2 = v_e2;
|
|
|
|
return e1->bei_id - e2->bei_id;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2007-01-17 10:43:05 +08:00
|
|
|
static int
|
|
|
|
bdb_id_dup_err( void *v1, void *v2 )
|
|
|
|
{
|
|
|
|
EntryInfo *e2 = v2;
|
2007-01-17 18:02:53 +08:00
|
|
|
e2->bei_lrunext = v1;
|
2007-01-17 10:43:05 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Create an entryinfo in the cache. Caller must release the locks later.
|
2002-01-25 15:19:01 +08:00
|
|
|
*/
|
2003-04-23 14:34:55 +08:00
|
|
|
static int
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_entryinfo_add_internal(
|
|
|
|
struct bdb_info *bdb,
|
2003-04-23 01:06:35 +08:00
|
|
|
EntryInfo *ei,
|
2003-12-31 09:41:16 +08:00
|
|
|
EntryInfo **res )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo *ei2 = NULL;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
*res = NULL;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-09-27 12:58:36 +08:00
|
|
|
ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
|
2003-09-22 07:08:44 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
|
2003-04-23 01:06:35 +08:00
|
|
|
bdb_cache_entryinfo_lock( ei->bei_parent );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
ei2->bei_id = ei->bei_id;
|
|
|
|
ei2->bei_parent = ei->bei_parent;
|
2003-04-23 02:59:37 +08:00
|
|
|
#ifdef BDB_HIER
|
2003-04-23 01:06:35 +08:00
|
|
|
ei2->bei_rdn = ei->bei_rdn;
|
2003-04-23 02:59:37 +08:00
|
|
|
#endif
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
ei2->bei_bdb = bdb;
|
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
|
|
|
|
/* Add to cache ID tree */
|
2007-01-17 10:43:05 +08:00
|
|
|
if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
|
|
|
|
bdb_id_dup_err )) {
|
2007-01-17 18:02:53 +08:00
|
|
|
EntryInfo *eix = ei2->bei_lrunext;
|
2007-01-17 10:43:05 +08:00
|
|
|
bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
|
2003-04-23 01:06:35 +08:00
|
|
|
ei2 = eix;
|
2003-04-23 02:59:37 +08:00
|
|
|
#ifdef BDB_HIER
|
2003-05-24 08:46:52 +08:00
|
|
|
/* It got freed above because its value was
|
|
|
|
* assigned to ei2.
|
|
|
|
*/
|
|
|
|
ei->bei_rdn.bv_val = NULL;
|
2003-04-23 02:59:37 +08:00
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
} else {
|
2007-12-05 22:56:47 +08:00
|
|
|
int rc;
|
|
|
|
|
2004-11-08 12:23:37 +08:00
|
|
|
bdb->bi_cache.c_eiused++;
|
2003-04-23 01:06:35 +08:00
|
|
|
ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
|
2005-09-08 23:28:36 +08:00
|
|
|
|
|
|
|
/* This is a new leaf node. But if parent had no kids, then it was
|
|
|
|
* a leaf and we would be decrementing that. So, only increment if
|
|
|
|
* the parent already has kids.
|
|
|
|
*/
|
|
|
|
if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
|
|
|
|
bdb->bi_cache.c_leaves++;
|
2007-12-05 22:56:47 +08:00
|
|
|
rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
|
2003-04-23 01:06:35 +08:00
|
|
|
avl_dup_error );
|
2007-12-06 06:27:12 +08:00
|
|
|
if ( rc ) {
|
2007-12-06 14:20:42 +08:00
|
|
|
/* This should never happen; entry cache is corrupt */
|
2007-12-06 06:27:12 +08:00
|
|
|
bdb->bi_dbenv->log_flush( bdb->bi_dbenv, NULL );
|
|
|
|
assert( !rc );
|
|
|
|
}
|
2003-09-22 16:37:32 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
ei->bei_parent->bei_ckids++;
|
|
|
|
#endif
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
*res = ei2;
|
|
|
|
return 0;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Find the EntryInfo for the requested DN. If the DN cannot be found, return
|
|
|
|
* the info for its closest ancestor. *res should be NULL to process a
|
|
|
|
* complete DN starting from the tree root. Otherwise *res must be the
|
|
|
|
* immediate parent of the requested DN, and only the RDN will be searched.
|
|
|
|
* The EntryInfo is locked upon return and must be unlocked by the caller.
|
|
|
|
*/
|
|
|
|
int
|
2003-04-23 14:34:55 +08:00
|
|
|
bdb_cache_find_ndn(
|
2003-05-25 08:53:08 +08:00
|
|
|
Operation *op,
|
2007-12-05 22:09:34 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-04-17 00:23:36 +08:00
|
|
|
struct berval *ndn,
|
2003-12-31 09:41:16 +08:00
|
|
|
EntryInfo **res )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-05-25 08:53:08 +08:00
|
|
|
struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo ei, *eip, *ei2;
|
|
|
|
int rc = 0;
|
|
|
|
char *ptr;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
|
|
|
/* this function is always called with normalized DN */
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( *res ) {
|
|
|
|
/* we're doing a onelevel search for an RDN */
|
|
|
|
ei.bei_nrdn.bv_val = ndn->bv_val;
|
2003-05-25 08:53:08 +08:00
|
|
|
ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
|
2003-04-17 00:23:36 +08:00
|
|
|
eip = *res;
|
|
|
|
} else {
|
|
|
|
/* we're searching a full DN from the root */
|
2003-05-25 08:53:08 +08:00
|
|
|
ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
|
2003-04-17 00:23:36 +08:00
|
|
|
ei.bei_nrdn.bv_val = ptr;
|
2003-05-25 08:53:08 +08:00
|
|
|
ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
|
2004-06-12 15:39:24 +08:00
|
|
|
/* Skip to next rdn if suffix is empty */
|
|
|
|
if ( ei.bei_nrdn.bv_len == 0 ) {
|
|
|
|
for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
|
|
|
|
&& !DN_SEPARATOR(*ptr); ptr--) /* empty */;
|
|
|
|
if ( ptr >= ndn->bv_val ) {
|
|
|
|
if (DN_SEPARATOR(*ptr)) ptr++;
|
|
|
|
ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
|
|
|
|
ei.bei_nrdn.bv_val = ptr;
|
|
|
|
}
|
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
eip = &bdb->bi_cache.c_dntree;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
eip->bei_state |= CACHE_ENTRY_REFERENCED;
|
2003-04-23 01:06:35 +08:00
|
|
|
ei.bei_parent = eip;
|
2003-04-17 00:23:36 +08:00
|
|
|
ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
|
|
|
|
if ( !ei2 ) {
|
2007-12-06 13:48:02 +08:00
|
|
|
DB_LOCK lock;
|
2003-04-17 00:23:36 +08:00
|
|
|
int len = ei.bei_nrdn.bv_len;
|
|
|
|
|
2005-04-09 18:01:13 +08:00
|
|
|
if ( BER_BVISEMPTY( ndn )) {
|
|
|
|
*res = eip;
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-12-31 09:41:16 +08:00
|
|
|
ei.bei_nrdn.bv_len = ndn->bv_len -
|
|
|
|
(ei.bei_nrdn.bv_val - ndn->bv_val);
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_cache_entryinfo_unlock( eip );
|
|
|
|
|
2007-12-06 14:40:13 +08:00
|
|
|
BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
|
2007-12-06 06:27:12 +08:00
|
|
|
ei.bei_nrdn.bv_val );
|
|
|
|
|
2007-12-06 13:48:02 +08:00
|
|
|
lock.mode = DB_LOCK_NG;
|
|
|
|
rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, locker, &lock );
|
2003-04-17 00:23:36 +08:00
|
|
|
if (rc) {
|
|
|
|
bdb_cache_entryinfo_lock( eip );
|
2007-12-06 13:48:02 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, &lock );
|
2003-04-17 00:23:36 +08:00
|
|
|
*res = eip;
|
|
|
|
return rc;
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2007-12-06 14:40:13 +08:00
|
|
|
BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
|
2007-12-06 06:27:12 +08:00
|
|
|
ei.bei_nrdn.bv_val, ei.bei_id );
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* DN exists but needs to be added to cache */
|
|
|
|
ei.bei_nrdn.bv_len = len;
|
2003-09-22 16:37:32 +08:00
|
|
|
rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
|
2003-04-17 00:23:36 +08:00
|
|
|
/* add_internal left eip and c_rwlock locked */
|
|
|
|
ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
|
2007-12-06 13:48:02 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, &lock );
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( rc ) {
|
|
|
|
*res = eip;
|
|
|
|
return rc;
|
|
|
|
}
|
2003-04-17 08:52:31 +08:00
|
|
|
} else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
|
2003-04-17 00:23:36 +08:00
|
|
|
/* In the midst of deleting? Give it a chance to
|
|
|
|
* complete.
|
|
|
|
*/
|
|
|
|
bdb_cache_entryinfo_unlock( eip );
|
2002-01-25 15:19:01 +08:00
|
|
|
ldap_pvt_thread_yield();
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_cache_entryinfo_lock( eip );
|
|
|
|
*res = eip;
|
|
|
|
return DB_NOTFOUND;
|
|
|
|
}
|
|
|
|
bdb_cache_entryinfo_unlock( eip );
|
|
|
|
bdb_cache_entryinfo_lock( ei2 );
|
|
|
|
|
|
|
|
eip = ei2;
|
|
|
|
|
|
|
|
/* Advance to next lower RDN */
|
|
|
|
for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
|
2004-05-07 17:03:05 +08:00
|
|
|
&& !DN_SEPARATOR(*ptr); ptr--) /* empty */;
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( ptr >= ndn->bv_val ) {
|
2004-05-07 17:03:05 +08:00
|
|
|
if (DN_SEPARATOR(*ptr)) ptr++;
|
2003-04-17 00:23:36 +08:00
|
|
|
ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
|
|
|
|
ei.bei_nrdn.bv_val = ptr;
|
|
|
|
}
|
|
|
|
if ( ptr < ndn->bv_val ) {
|
|
|
|
*res = eip;
|
|
|
|
break;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
return rc;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
/* Walk up the tree from a child node, looking for an ID that's already
|
|
|
|
* been linked into the cache.
|
|
|
|
*/
|
2005-10-16 15:37:36 +08:00
|
|
|
int
|
2003-05-25 08:53:08 +08:00
|
|
|
hdb_cache_find_parent(
|
|
|
|
Operation *op,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-04-23 01:06:35 +08:00
|
|
|
ID id,
|
2003-12-31 12:26:17 +08:00
|
|
|
EntryInfo **res )
|
2003-04-23 01:06:35 +08:00
|
|
|
{
|
2003-05-25 08:53:08 +08:00
|
|
|
struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
|
2003-04-23 01:06:35 +08:00
|
|
|
EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
ei.bei_id = id;
|
|
|
|
ei.bei_kids = NULL;
|
2004-11-01 06:07:15 +08:00
|
|
|
ei.bei_ckids = 0;
|
2003-04-23 01:06:35 +08:00
|
|
|
|
|
|
|
for (;;) {
|
2007-12-05 22:56:47 +08:00
|
|
|
rc = hdb_dn2id_parent( op, locker, &ei, &eip.bei_id );
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( rc ) break;
|
|
|
|
|
|
|
|
/* Save the previous node, if any */
|
|
|
|
ei2 = ein;
|
|
|
|
|
|
|
|
/* Create a new node for the current ID */
|
2003-09-27 12:58:36 +08:00
|
|
|
ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
|
2003-04-23 01:06:35 +08:00
|
|
|
ein->bei_id = ei.bei_id;
|
2003-04-23 02:59:37 +08:00
|
|
|
ein->bei_kids = ei.bei_kids;
|
2003-04-23 01:06:35 +08:00
|
|
|
ein->bei_nrdn = ei.bei_nrdn;
|
|
|
|
ein->bei_rdn = ei.bei_rdn;
|
2004-11-01 06:07:15 +08:00
|
|
|
ein->bei_ckids = ei.bei_ckids;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
ein->bei_bdb = bdb;
|
|
|
|
#endif
|
2004-11-01 06:07:15 +08:00
|
|
|
ei.bei_ckids = 0;
|
2003-04-23 01:06:35 +08:00
|
|
|
|
|
|
|
/* This node is not fully connected yet */
|
2007-01-01 00:16:48 +08:00
|
|
|
ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
|
2003-04-23 01:06:35 +08:00
|
|
|
|
|
|
|
/* Insert this node into the ID tree */
|
2003-06-10 06:00:00 +08:00
|
|
|
ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
|
2007-01-17 10:43:05 +08:00
|
|
|
bdb_id_cmp, bdb_id_dup_err ) ) {
|
2007-01-17 18:02:53 +08:00
|
|
|
EntryInfo *eix = ein->bei_lrunext;
|
2003-04-23 01:06:35 +08:00
|
|
|
|
2003-09-14 18:27:59 +08:00
|
|
|
/* Someone else created this node just before us.
|
|
|
|
* Free our new copy and use the existing one.
|
|
|
|
*/
|
2007-01-17 10:43:05 +08:00
|
|
|
bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
|
|
|
|
ein = eix;
|
2003-09-14 18:27:59 +08:00
|
|
|
|
|
|
|
/* Link in any kids we've already processed */
|
2003-05-23 09:12:50 +08:00
|
|
|
if ( ei2 ) {
|
|
|
|
bdb_cache_entryinfo_lock( ein );
|
|
|
|
avl_insert( &ein->bei_kids, (caddr_t)ei2,
|
|
|
|
bdb_rdn_cmp, avl_dup_error );
|
2004-11-01 06:07:15 +08:00
|
|
|
ein->bei_ckids++;
|
2003-05-23 09:12:50 +08:00
|
|
|
bdb_cache_entryinfo_unlock( ein );
|
|
|
|
}
|
2003-04-23 01:06:35 +08:00
|
|
|
}
|
|
|
|
|
2003-05-23 07:48:02 +08:00
|
|
|
/* If this is the first time, save this node
|
|
|
|
* to be returned later.
|
|
|
|
*/
|
|
|
|
if ( eir == NULL ) eir = ein;
|
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
/* If there was a previous node, link it to this one */
|
|
|
|
if ( ei2 ) ei2->bei_parent = ein;
|
|
|
|
|
2003-09-14 18:27:59 +08:00
|
|
|
/* Look for this node's parent */
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( eip.bei_id ) {
|
|
|
|
ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
|
|
|
|
(caddr_t) &eip, bdb_id_cmp );
|
|
|
|
} else {
|
|
|
|
ei2 = &bdb->bi_cache.c_dntree;
|
|
|
|
}
|
2004-11-08 12:23:37 +08:00
|
|
|
bdb->bi_cache.c_eiused++;
|
2005-09-08 23:28:36 +08:00
|
|
|
if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
|
|
|
|
bdb->bi_cache.c_leaves++;
|
2003-09-14 18:27:59 +08:00
|
|
|
ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
|
2003-04-23 01:06:35 +08:00
|
|
|
|
2003-09-14 18:27:59 +08:00
|
|
|
/* Got the parent, link in and we're done. */
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( ei2 ) {
|
2007-11-12 19:16:30 +08:00
|
|
|
bdb_cache_entryinfo_lock( eir );
|
2003-04-23 01:06:35 +08:00
|
|
|
bdb_cache_entryinfo_lock( ei2 );
|
2003-09-14 18:27:59 +08:00
|
|
|
ein->bei_parent = ei2;
|
2007-01-01 00:16:48 +08:00
|
|
|
|
2007-01-01 15:09:22 +08:00
|
|
|
avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
|
|
|
|
avl_dup_error);
|
|
|
|
ei2->bei_ckids++;
|
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
/* Reset all the state info */
|
|
|
|
for (ein = eir; ein != ei2; ein=ein->bei_parent)
|
|
|
|
ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
|
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
bdb_cache_entryinfo_unlock( ei2 );
|
2003-09-14 18:27:59 +08:00
|
|
|
|
|
|
|
*res = eir;
|
2003-04-23 01:06:35 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ei.bei_kids = NULL;
|
|
|
|
ei.bei_id = eip.bei_id;
|
2004-11-01 06:07:15 +08:00
|
|
|
ei.bei_ckids = 1;
|
2003-04-23 01:06:35 +08:00
|
|
|
avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
|
|
|
|
avl_dup_error );
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2003-09-22 16:37:32 +08:00
|
|
|
|
2003-09-22 18:11:10 +08:00
|
|
|
/* Used by hdb_dn2idl when loading the EntryInfo for all the children
|
|
|
|
* of a given node
|
|
|
|
*/
|
|
|
|
int hdb_cache_load(
|
2003-09-22 16:37:32 +08:00
|
|
|
struct bdb_info *bdb,
|
|
|
|
EntryInfo *ei,
|
|
|
|
EntryInfo **res )
|
|
|
|
{
|
|
|
|
EntryInfo *ei2;
|
|
|
|
int rc;
|
|
|
|
|
2003-09-22 18:11:10 +08:00
|
|
|
/* See if we already have this one */
|
2003-09-22 16:37:32 +08:00
|
|
|
bdb_cache_entryinfo_lock( ei->bei_parent );
|
|
|
|
ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
|
|
|
|
bdb_cache_entryinfo_unlock( ei->bei_parent );
|
2003-09-22 18:11:10 +08:00
|
|
|
|
2003-09-22 16:37:32 +08:00
|
|
|
if ( !ei2 ) {
|
2003-09-22 18:11:10 +08:00
|
|
|
/* Not found, add it */
|
|
|
|
struct berval bv;
|
|
|
|
|
|
|
|
/* bei_rdn was not malloc'd before, do it now */
|
|
|
|
ber_dupbv( &bv, &ei->bei_rdn );
|
|
|
|
ei->bei_rdn = bv;
|
|
|
|
|
2003-09-22 16:37:32 +08:00
|
|
|
rc = bdb_entryinfo_add_internal( bdb, ei, res );
|
|
|
|
bdb_cache_entryinfo_unlock( ei->bei_parent );
|
|
|
|
ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
|
|
|
|
} else {
|
2003-09-22 18:11:10 +08:00
|
|
|
/* Found, return it */
|
2003-09-22 16:37:32 +08:00
|
|
|
*res = ei2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2003-04-23 01:06:35 +08:00
|
|
|
#endif
|
|
|
|
|
2007-01-12 15:35:34 +08:00
|
|
|
/* This is best-effort only. If all entries in the cache are
|
|
|
|
* busy, they will all be kept. This is unlikely to happen
|
|
|
|
* unless the cache is very much smaller than the working set.
|
|
|
|
*/
|
2006-04-05 15:17:11 +08:00
|
|
|
static void
|
2007-01-04 19:01:59 +08:00
|
|
|
bdb_cache_lru_purge( struct bdb_info *bdb )
|
2003-09-22 07:08:44 +08:00
|
|
|
{
|
2007-01-04 19:01:59 +08:00
|
|
|
DB_LOCK lock, *lockp;
|
2007-01-12 15:35:34 +08:00
|
|
|
EntryInfo *elru, *elnext = NULL;
|
|
|
|
int count, islocked, eimax;
|
2006-04-05 15:17:11 +08:00
|
|
|
|
2007-01-12 15:35:34 +08:00
|
|
|
/* Wait for the mutex; we're the only one trying to purge. */
|
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
|
2006-04-05 15:17:11 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb->bi_cache.c_purging = 0;
|
2006-07-12 09:55:21 +08:00
|
|
|
return;
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
2003-09-22 07:08:44 +08:00
|
|
|
|
2007-01-04 19:01:59 +08:00
|
|
|
if ( bdb->bi_cache.c_locker ) {
|
|
|
|
lockp = &lock;
|
|
|
|
} else {
|
|
|
|
lockp = NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
count = 0;
|
2007-01-04 19:01:59 +08:00
|
|
|
|
2007-01-12 15:35:34 +08:00
|
|
|
/* maximum number of EntryInfo leaves to cache. In slapcat
|
|
|
|
* we always free all leaf nodes.
|
|
|
|
*/
|
|
|
|
if ( slapMode & SLAP_TOOL_READONLY )
|
|
|
|
eimax = 0;
|
|
|
|
else
|
2007-08-12 03:29:14 +08:00
|
|
|
eimax = bdb->bi_cache.c_eimax;
|
2007-01-04 19:01:59 +08:00
|
|
|
|
2005-12-09 08:20:18 +08:00
|
|
|
/* Look for an unused entry to remove */
|
2007-01-12 15:35:34 +08:00
|
|
|
for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
elnext = elru->bei_lrunext;
|
|
|
|
|
2007-01-17 10:43:05 +08:00
|
|
|
if ( bdb_cache_entryinfo_trylock( elru ))
|
2007-01-12 15:35:34 +08:00
|
|
|
goto bottom;
|
2003-09-22 07:08:44 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
/* This flag implements the clock replacement behavior */
|
|
|
|
if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
|
|
|
|
elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
|
|
|
|
bdb_cache_entryinfo_unlock( elru );
|
2007-01-12 15:35:34 +08:00
|
|
|
goto bottom;
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this node is in the process of linking into the cache,
|
|
|
|
* or this node is being deleted, skip it.
|
2005-12-09 08:20:18 +08:00
|
|
|
*/
|
2007-01-01 19:44:39 +08:00
|
|
|
if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
|
|
|
|
CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
|
2007-01-12 15:35:34 +08:00
|
|
|
elru->bei_finders > 0 ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
bdb_cache_entryinfo_unlock( elru );
|
2007-01-12 15:35:34 +08:00
|
|
|
goto bottom;
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
2004-10-31 21:04:27 +08:00
|
|
|
|
2007-01-01 09:00:19 +08:00
|
|
|
/* entryinfo is locked */
|
2007-01-01 00:16:48 +08:00
|
|
|
islocked = 1;
|
2005-09-08 23:28:36 +08:00
|
|
|
|
2007-01-01 09:00:19 +08:00
|
|
|
/* If we can successfully writelock it, then
|
|
|
|
* the object is idle.
|
|
|
|
*/
|
2007-01-04 19:01:59 +08:00
|
|
|
if ( bdb_cache_entry_db_lock( bdb,
|
|
|
|
bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
|
2007-01-01 09:00:19 +08:00
|
|
|
|
|
|
|
/* Free entry for this node if it's present */
|
|
|
|
if ( elru->bei_e ) {
|
|
|
|
elru->bei_e->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
2007-01-01 09:00:19 +08:00
|
|
|
bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
|
2004-12-15 01:22:22 +08:00
|
|
|
#else
|
2007-01-01 09:00:19 +08:00
|
|
|
bdb_entry_return( elru->bei_e );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2007-01-01 09:00:19 +08:00
|
|
|
elru->bei_e = NULL;
|
|
|
|
count++;
|
|
|
|
}
|
2007-01-04 19:01:59 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, lockp );
|
2007-01-01 09:00:19 +08:00
|
|
|
|
2007-01-12 15:35:34 +08:00
|
|
|
/*
|
|
|
|
* If it is a leaf node, and we're over the limit, free it.
|
2007-01-01 09:00:19 +08:00
|
|
|
*/
|
2007-01-12 15:35:34 +08:00
|
|
|
if ( elru->bei_kids ) {
|
|
|
|
/* Drop from list, we ignore it... */
|
2007-01-05 14:49:25 +08:00
|
|
|
LRU_DEL( &bdb->bi_cache, elru );
|
2007-01-12 15:35:34 +08:00
|
|
|
} else if ( bdb->bi_cache.c_leaves > eimax ) {
|
|
|
|
/* Too many leaf nodes, free this one */
|
|
|
|
bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
|
|
|
|
bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
|
|
|
|
islocked = 0;
|
|
|
|
} /* Leave on list until we need to free it */
|
2007-01-01 00:16:48 +08:00
|
|
|
}
|
2005-09-08 23:28:36 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( islocked )
|
|
|
|
bdb_cache_entryinfo_unlock( elru );
|
|
|
|
|
|
|
|
if ( count >= bdb->bi_cache.c_minfree ) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
|
|
|
|
bdb->bi_cache.c_cursize -= count;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
|
|
|
|
break;
|
2005-12-09 08:20:18 +08:00
|
|
|
}
|
2007-01-12 15:35:34 +08:00
|
|
|
bottom:
|
|
|
|
if ( elnext == bdb->bi_cache.c_lruhead )
|
|
|
|
break;
|
2005-12-09 08:20:18 +08:00
|
|
|
}
|
|
|
|
|
2007-01-01 19:44:39 +08:00
|
|
|
bdb->bi_cache.c_lruhead = elnext;
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb->bi_cache.c_purging = 0;
|
2003-09-22 07:08:44 +08:00
|
|
|
}
|
|
|
|
|
2003-10-03 10:19:09 +08:00
|
|
|
EntryInfo *
|
|
|
|
bdb_cache_find_info(
|
|
|
|
struct bdb_info *bdb,
|
2003-12-31 12:26:17 +08:00
|
|
|
ID id )
|
2003-10-03 10:19:09 +08:00
|
|
|
{
|
2004-10-05 04:55:09 +08:00
|
|
|
EntryInfo ei = { 0 },
|
|
|
|
*ei2;
|
2003-10-03 10:19:09 +08:00
|
|
|
|
|
|
|
ei.bei_id = id;
|
|
|
|
|
|
|
|
ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
|
|
|
|
ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
|
|
|
|
(caddr_t) &ei, bdb_id_cmp );
|
|
|
|
ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
|
|
|
|
return ei2;
|
|
|
|
}
|
|
|
|
|
2002-01-25 15:19:01 +08:00
|
|
|
/*
|
2003-04-23 14:34:55 +08:00
|
|
|
* cache_find_id - find an entry in the cache, given id.
|
2007-11-12 19:16:30 +08:00
|
|
|
* The entry is locked for Read upon return. Call with flag ID_LOCKED if
|
2003-04-17 00:23:36 +08:00
|
|
|
* the supplied *eip was already locked.
|
2002-01-25 15:19:01 +08:00
|
|
|
*/
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
int
|
2003-04-23 14:34:55 +08:00
|
|
|
bdb_cache_find_id(
|
2003-05-25 08:53:08 +08:00
|
|
|
Operation *op,
|
2003-04-17 00:23:36 +08:00
|
|
|
DB_TXN *tid,
|
2002-01-25 15:19:01 +08:00
|
|
|
ID id,
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo **eip,
|
2007-11-12 19:16:30 +08:00
|
|
|
int flag,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-12-31 12:26:17 +08:00
|
|
|
DB_LOCK *lock )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-05-25 08:53:08 +08:00
|
|
|
struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
|
2003-04-17 00:23:36 +08:00
|
|
|
Entry *ep = NULL;
|
2004-11-08 15:18:33 +08:00
|
|
|
int rc = 0, load = 0;
|
2004-10-05 04:55:09 +08:00
|
|
|
EntryInfo ei = { 0 };
|
2003-04-17 00:23:36 +08:00
|
|
|
|
|
|
|
ei.bei_id = id;
|
|
|
|
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
slap_zh_rlock(bdb->bi_cache.c_zctx);
|
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
/* If we weren't given any info, see if we have it already cached */
|
|
|
|
if ( !*eip ) {
|
2003-12-31 12:26:17 +08:00
|
|
|
again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
|
2003-04-17 00:23:36 +08:00
|
|
|
*eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
|
2003-12-31 12:26:17 +08:00
|
|
|
(caddr_t) &ei, bdb_id_cmp );
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( *eip ) {
|
2003-09-14 18:27:59 +08:00
|
|
|
/* If the lock attempt fails, the info is in use */
|
2007-01-17 10:43:05 +08:00
|
|
|
if ( bdb_cache_entryinfo_trylock( *eip )) {
|
2003-06-10 06:00:00 +08:00
|
|
|
ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
|
2003-09-14 18:27:59 +08:00
|
|
|
/* If this node is being deleted, treat
|
|
|
|
* as if the delete has already finished
|
|
|
|
*/
|
2003-06-13 06:47:13 +08:00
|
|
|
if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
|
|
|
|
return DB_NOTFOUND;
|
|
|
|
}
|
2003-09-14 18:27:59 +08:00
|
|
|
/* otherwise, wait for the info to free up */
|
|
|
|
ldap_pvt_thread_yield();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
/* If this info isn't hooked up to its parent yet,
|
|
|
|
* unlock and wait for it to be fully initialized
|
|
|
|
*/
|
|
|
|
if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
|
|
|
|
bdb_cache_entryinfo_unlock( *eip );
|
|
|
|
ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
|
2003-06-10 06:00:00 +08:00
|
|
|
ldap_pvt_thread_yield();
|
|
|
|
goto again;
|
|
|
|
}
|
2007-11-12 19:16:30 +08:00
|
|
|
flag |= ID_LOCKED;
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
|
|
|
ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* See if the ID exists in the database; add it to the cache if so */
|
|
|
|
if ( !*eip ) {
|
2003-04-23 01:06:35 +08:00
|
|
|
#ifndef BDB_HIER
|
2005-10-19 05:35:07 +08:00
|
|
|
rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( rc == 0 ) {
|
2007-12-05 22:09:34 +08:00
|
|
|
rc = bdb_cache_find_ndn( op, locker,
|
2003-09-22 16:37:32 +08:00
|
|
|
&ep->e_nname, eip );
|
2007-11-12 19:16:30 +08:00
|
|
|
if ( *eip ) flag |= ID_LOCKED;
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( rc ) {
|
2006-08-27 07:20:00 +08:00
|
|
|
ep->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
|
|
|
|
#else
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_entry_return( ep );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
ep = NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-04-23 01:06:35 +08:00
|
|
|
#else
|
2007-12-05 22:56:47 +08:00
|
|
|
rc = hdb_cache_find_parent(op, locker, id, eip );
|
2007-11-12 19:16:30 +08:00
|
|
|
if ( rc == 0 ) flag |= ID_LOCKED;
|
2003-04-23 01:06:35 +08:00
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Ok, we found the info, do we have the entry? */
|
2006-04-01 06:24:39 +08:00
|
|
|
if ( rc == 0 ) {
|
2008-04-03 16:02:11 +08:00
|
|
|
if ( !( flag & ID_LOCKED )) {
|
|
|
|
bdb_cache_entryinfo_lock( *eip );
|
|
|
|
flag |= ID_LOCKED;
|
|
|
|
}
|
|
|
|
|
2003-04-17 08:52:31 +08:00
|
|
|
if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
|
2003-04-17 00:23:36 +08:00
|
|
|
rc = DB_NOTFOUND;
|
2003-06-12 19:11:37 +08:00
|
|
|
} else {
|
2007-01-04 19:01:59 +08:00
|
|
|
(*eip)->bei_finders++;
|
2007-01-17 18:02:53 +08:00
|
|
|
(*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
|
2004-07-10 01:00:07 +08:00
|
|
|
/* Make sure only one thread tries to load the entry */
|
2004-12-15 01:22:22 +08:00
|
|
|
load1:
|
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
if ((*eip)->bei_e && !slap_zn_validate(
|
|
|
|
bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
|
|
|
|
(*eip)->bei_e = NULL;
|
|
|
|
(*eip)->bei_zseq = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
|
2004-07-10 01:00:07 +08:00
|
|
|
load = 1;
|
|
|
|
(*eip)->bei_state |= CACHE_ENTRY_LOADING;
|
|
|
|
}
|
2007-01-04 19:01:59 +08:00
|
|
|
|
2008-04-03 16:02:11 +08:00
|
|
|
if ( !load ) {
|
|
|
|
/* Clear the uncached state if we are not
|
|
|
|
* loading it, i.e it is already cached or
|
|
|
|
* another thread is currently loading it.
|
|
|
|
*/
|
|
|
|
(*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
|
|
|
|
flag &= ~ID_NOCACHE;
|
2007-11-12 19:16:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( flag & ID_LOCKED ) {
|
2004-06-25 01:56:43 +08:00
|
|
|
bdb_cache_entryinfo_unlock( *eip );
|
2007-11-12 19:16:30 +08:00
|
|
|
flag ^= ID_LOCKED;
|
2004-06-25 01:56:43 +08:00
|
|
|
}
|
2007-08-26 22:37:12 +08:00
|
|
|
rc = bdb_cache_entry_db_lock( bdb, locker, *eip, load, 0, lock );
|
2004-07-04 04:03:14 +08:00
|
|
|
if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
|
|
|
|
rc = DB_NOTFOUND;
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, lock );
|
2004-07-04 04:03:14 +08:00
|
|
|
} else if ( rc == 0 ) {
|
2004-07-10 01:00:07 +08:00
|
|
|
if ( load ) {
|
2007-01-01 19:44:39 +08:00
|
|
|
if ( !ep) {
|
2005-10-19 05:35:07 +08:00
|
|
|
rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
|
2004-06-25 01:56:43 +08:00
|
|
|
}
|
|
|
|
if ( rc == 0 ) {
|
2004-07-10 01:00:07 +08:00
|
|
|
ep->e_private = *eip;
|
2003-04-23 01:06:35 +08:00
|
|
|
#ifdef BDB_HIER
|
2004-07-10 01:00:07 +08:00
|
|
|
bdb_fix_dn( ep, 0 );
|
2003-04-23 01:06:35 +08:00
|
|
|
#endif
|
2004-07-10 01:00:07 +08:00
|
|
|
(*eip)->bei_e = ep;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
(*eip)->bei_zseq = *((ber_len_t *)ep - 2);
|
|
|
|
#endif
|
2004-07-10 01:00:07 +08:00
|
|
|
ep = NULL;
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb_cache_lru_link( bdb, *eip );
|
2008-04-03 16:02:11 +08:00
|
|
|
if (( flag & ID_NOCACHE ) &&
|
|
|
|
( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
|
|
|
|
/* Set the cached state only if no other thread
|
2008-04-28 19:20:34 +08:00
|
|
|
* found the info while we were loading the entry.
|
2008-04-03 16:02:11 +08:00
|
|
|
*/
|
|
|
|
if ( (*eip)->bei_finders == 1 )
|
|
|
|
(*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
|
2007-11-12 19:16:30 +08:00
|
|
|
bdb_cache_entryinfo_unlock( *eip );
|
|
|
|
}
|
2004-07-10 01:00:07 +08:00
|
|
|
}
|
2007-08-26 22:37:12 +08:00
|
|
|
if ( rc == 0 ) {
|
|
|
|
/* If we succeeded, downgrade back to a readlock. */
|
|
|
|
rc = bdb_cache_entry_db_relock( bdb, locker,
|
|
|
|
*eip, 0, 0, lock );
|
|
|
|
} else {
|
2007-02-08 18:29:40 +08:00
|
|
|
/* Otherwise, release the lock. */
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, lock );
|
2003-06-12 19:11:37 +08:00
|
|
|
}
|
2004-07-10 01:00:07 +08:00
|
|
|
} else if ( !(*eip)->bei_e ) {
|
|
|
|
/* Some other thread is trying to load the entry,
|
2007-01-01 07:37:06 +08:00
|
|
|
* wait for it to finish.
|
2004-07-10 01:00:07 +08:00
|
|
|
*/
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_unlock( bdb, lock );
|
2004-07-10 01:00:07 +08:00
|
|
|
bdb_cache_entryinfo_lock( *eip );
|
2007-11-12 19:16:30 +08:00
|
|
|
flag |= ID_LOCKED;
|
2004-07-10 01:00:07 +08:00
|
|
|
goto load1;
|
2004-07-03 23:47:44 +08:00
|
|
|
#ifdef BDB_HIER
|
2004-06-25 01:56:43 +08:00
|
|
|
} else {
|
2004-07-03 23:47:44 +08:00
|
|
|
/* Check for subtree renames
|
2004-06-25 01:56:43 +08:00
|
|
|
*/
|
|
|
|
rc = bdb_fix_dn( (*eip)->bei_e, 1 );
|
|
|
|
if ( rc ) {
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_relock( bdb,
|
2004-06-25 01:56:43 +08:00
|
|
|
locker, *eip, 1, 0, lock );
|
|
|
|
/* check again in case other modifier did it already */
|
|
|
|
if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
|
|
|
|
rc = bdb_fix_dn( (*eip)->bei_e, 2 );
|
2007-01-01 07:37:06 +08:00
|
|
|
bdb_cache_entry_db_relock( bdb,
|
2004-06-25 01:56:43 +08:00
|
|
|
locker, *eip, 0, 0, lock );
|
|
|
|
}
|
2003-04-23 16:46:23 +08:00
|
|
|
#endif
|
2004-06-25 01:56:43 +08:00
|
|
|
}
|
2003-09-22 07:08:44 +08:00
|
|
|
}
|
2008-04-09 08:12:08 +08:00
|
|
|
bdb_cache_entryinfo_lock( *eip );
|
|
|
|
(*eip)->bei_finders--;
|
|
|
|
if ( load )
|
|
|
|
(*eip)->bei_state ^= CACHE_ENTRY_LOADING;
|
|
|
|
bdb_cache_entryinfo_unlock( *eip );
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
2007-11-12 19:16:30 +08:00
|
|
|
if ( flag & ID_LOCKED ) {
|
2004-07-04 04:03:14 +08:00
|
|
|
bdb_cache_entryinfo_unlock( *eip );
|
|
|
|
}
|
2004-07-06 05:00:09 +08:00
|
|
|
if ( ep ) {
|
2006-08-27 07:20:00 +08:00
|
|
|
ep->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
|
|
|
|
#else
|
2004-07-06 05:00:09 +08:00
|
|
|
bdb_entry_return( ep );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2004-07-06 05:00:09 +08:00
|
|
|
}
|
2003-09-22 07:08:44 +08:00
|
|
|
if ( rc == 0 ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
int purge = 0;
|
2004-11-08 15:18:33 +08:00
|
|
|
|
|
|
|
if ( load ) {
|
2007-11-12 19:16:30 +08:00
|
|
|
if ( !( flag & ID_NOCACHE )) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
|
|
|
|
bdb->bi_cache.c_cursize++;
|
|
|
|
if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
|
|
|
|
!bdb->bi_cache.c_purging ) {
|
|
|
|
purge = 1;
|
|
|
|
bdb->bi_cache.c_purging = 1;
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
|
2007-01-05 14:49:25 +08:00
|
|
|
}
|
2003-09-22 07:08:44 +08:00
|
|
|
}
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( purge )
|
2007-01-04 19:01:59 +08:00
|
|
|
bdb_cache_lru_purge( bdb );
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
if (rc == 0 && (*eip)->bei_e) {
|
|
|
|
slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
|
|
|
|
}
|
|
|
|
slap_zh_runlock(bdb->bi_cache.c_zctx);
|
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
return rc;
|
|
|
|
}
|
2002-06-01 04:49:19 +08:00
|
|
|
|
2003-04-22 12:06:09 +08:00
|
|
|
int
|
|
|
|
bdb_cache_children(
|
|
|
|
Operation *op,
|
|
|
|
DB_TXN *txn,
|
2003-12-31 12:26:17 +08:00
|
|
|
Entry *e )
|
2003-04-22 12:06:09 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ( BEI(e)->bei_kids ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
|
|
|
|
return DB_NOTFOUND;
|
|
|
|
}
|
|
|
|
rc = bdb_dn2id_children( op, txn, e );
|
|
|
|
if ( rc == DB_NOTFOUND ) {
|
2003-10-03 10:19:09 +08:00
|
|
|
BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
|
2003-04-22 12:06:09 +08:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Update the cache after a successful database Add. */
|
|
|
|
int
|
|
|
|
bdb_cache_add(
|
|
|
|
struct bdb_info *bdb,
|
2003-04-23 01:06:35 +08:00
|
|
|
EntryInfo *eip,
|
2003-04-17 00:23:36 +08:00
|
|
|
Entry *e,
|
|
|
|
struct berval *nrdn,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2006-12-30 20:05:55 +08:00
|
|
|
DB_LOCK *lock )
|
2003-04-17 00:23:36 +08:00
|
|
|
{
|
2003-04-23 01:06:35 +08:00
|
|
|
EntryInfo *new, ei;
|
2007-01-01 00:16:48 +08:00
|
|
|
int rc, purge = 0;
|
2005-07-15 03:52:08 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
struct berval rdn = e->e_name;
|
|
|
|
#endif
|
2002-06-01 04:49:19 +08:00
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
ei.bei_id = e->e_id;
|
|
|
|
ei.bei_parent = eip;
|
|
|
|
ei.bei_nrdn = *nrdn;
|
2004-06-25 13:29:33 +08:00
|
|
|
ei.bei_lockpad = 0;
|
2004-06-25 01:56:43 +08:00
|
|
|
|
|
|
|
/* Lock this entry so that bdb_add can run to completion.
|
|
|
|
* It can only fail if BDB has run out of lock resources.
|
|
|
|
*/
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = bdb_cache_entry_db_lock( bdb, locker, &ei, 0, 0, lock );
|
2004-06-25 01:56:43 +08:00
|
|
|
if ( rc ) {
|
|
|
|
bdb_cache_entryinfo_unlock( eip );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2003-04-23 02:59:37 +08:00
|
|
|
#ifdef BDB_HIER
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( nrdn->bv_len != e->e_nname.bv_len ) {
|
2005-11-14 06:26:53 +08:00
|
|
|
char *ptr = ber_bvchr( &rdn, ',' );
|
|
|
|
assert( ptr != NULL );
|
2003-04-23 01:06:35 +08:00
|
|
|
rdn.bv_len = ptr - rdn.bv_val;
|
|
|
|
}
|
|
|
|
ber_dupbv( &ei.bei_rdn, &rdn );
|
2003-09-22 16:37:32 +08:00
|
|
|
if ( eip->bei_dkids ) eip->bei_dkids++;
|
2003-04-23 02:59:37 +08:00
|
|
|
#endif
|
2003-12-31 09:41:16 +08:00
|
|
|
|
2003-09-22 16:37:32 +08:00
|
|
|
rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
|
2003-10-19 17:55:31 +08:00
|
|
|
/* bdb_csn_commit can cause this when adding the database root entry */
|
2004-07-03 23:47:44 +08:00
|
|
|
if ( new->bei_e ) {
|
|
|
|
new->bei_e->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
|
|
|
|
#else
|
2004-07-03 23:47:44 +08:00
|
|
|
bdb_entry_return( new->bei_e );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2004-07-03 23:47:44 +08:00
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
new->bei_e = e;
|
|
|
|
e->e_private = new;
|
2007-01-01 00:16:48 +08:00
|
|
|
new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
|
2003-04-23 01:06:35 +08:00
|
|
|
eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
|
2003-12-31 09:41:16 +08:00
|
|
|
if (eip->bei_parent) {
|
|
|
|
eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
|
|
|
|
}
|
2004-10-31 21:04:27 +08:00
|
|
|
bdb_cache_entryinfo_unlock( eip );
|
2003-09-22 07:08:44 +08:00
|
|
|
|
2004-10-31 21:04:27 +08:00
|
|
|
ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
|
2007-01-01 00:16:48 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
|
|
|
|
++bdb->bi_cache.c_cursize;
|
2007-01-05 14:49:25 +08:00
|
|
|
if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
|
|
|
|
!bdb->bi_cache.c_purging ) {
|
2007-01-01 00:16:48 +08:00
|
|
|
purge = 1;
|
2007-01-05 14:49:25 +08:00
|
|
|
bdb->bi_cache.c_purging = 1;
|
|
|
|
}
|
2007-01-01 00:16:48 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
|
2004-10-31 21:04:27 +08:00
|
|
|
|
2007-01-09 03:10:23 +08:00
|
|
|
bdb_cache_lru_link( bdb, new );
|
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( purge )
|
2007-01-04 19:01:59 +08:00
|
|
|
bdb_cache_lru_purge( bdb );
|
2003-09-22 07:08:44 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
return rc;
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
int
|
|
|
|
bdb_cache_modify(
|
2007-01-01 07:37:06 +08:00
|
|
|
struct bdb_info *bdb,
|
2003-04-17 00:23:36 +08:00
|
|
|
Entry *e,
|
|
|
|
Attribute *newAttrs,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-12-31 12:26:17 +08:00
|
|
|
DB_LOCK *lock )
|
2003-04-17 00:23:36 +08:00
|
|
|
{
|
|
|
|
EntryInfo *ei = BEI(e);
|
2004-06-16 11:29:02 +08:00
|
|
|
int rc;
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Get write lock on data */
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* If we've done repeated mods on a cached entry, then e_attrs
|
|
|
|
* is no longer contiguous with the entry, and must be freed.
|
|
|
|
*/
|
2004-06-16 05:13:40 +08:00
|
|
|
if ( ! rc ) {
|
|
|
|
if ( (void *)e->e_attrs != (void *)(e+1) ) {
|
|
|
|
attrs_free( e->e_attrs );
|
|
|
|
}
|
|
|
|
e->e_attrs = newAttrs;
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
2004-06-16 05:13:40 +08:00
|
|
|
return rc;
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/*
|
|
|
|
* Change the rdn in the entryinfo. Also move to a new parent if needed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bdb_cache_modrdn(
|
2005-07-12 21:17:56 +08:00
|
|
|
struct bdb_info *bdb,
|
2003-04-17 00:23:36 +08:00
|
|
|
Entry *e,
|
|
|
|
struct berval *nrdn,
|
|
|
|
Entry *new,
|
|
|
|
EntryInfo *ein,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-12-31 12:26:17 +08:00
|
|
|
DB_LOCK *lock )
|
2003-04-17 00:23:36 +08:00
|
|
|
{
|
|
|
|
EntryInfo *ei = BEI(e), *pei;
|
2004-06-16 11:29:02 +08:00
|
|
|
int rc;
|
2005-07-15 03:52:08 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
struct berval rdn;
|
|
|
|
#endif
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Get write lock on data */
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
|
2004-06-16 11:29:02 +08:00
|
|
|
if ( rc ) return rc;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* If we've done repeated mods on a cached entry, then e_attrs
|
|
|
|
* is no longer contiguous with the entry, and must be freed.
|
|
|
|
*/
|
|
|
|
if ( (void *)e->e_attrs != (void *)(e+1) ) {
|
|
|
|
attrs_free( e->e_attrs );
|
|
|
|
}
|
|
|
|
e->e_attrs = new->e_attrs;
|
2003-12-31 12:26:17 +08:00
|
|
|
if( e->e_nname.bv_val < e->e_bv.bv_val ||
|
|
|
|
e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
|
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
ch_free(e->e_name.bv_val);
|
|
|
|
ch_free(e->e_nname.bv_val);
|
|
|
|
}
|
|
|
|
e->e_name = new->e_name;
|
|
|
|
e->e_nname = new->e_nname;
|
|
|
|
|
|
|
|
/* Lock the parent's kids AVL tree */
|
|
|
|
pei = ei->bei_parent;
|
|
|
|
bdb_cache_entryinfo_lock( pei );
|
|
|
|
avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
|
|
|
|
free( ei->bei_nrdn.bv_val );
|
|
|
|
ber_dupbv( &ei->bei_nrdn, nrdn );
|
2006-07-18 18:22:48 +08:00
|
|
|
|
2003-04-23 02:59:37 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
free( ei->bei_rdn.bv_val );
|
|
|
|
|
2003-04-23 01:06:35 +08:00
|
|
|
rdn = e->e_name;
|
|
|
|
if ( nrdn->bv_len != e->e_nname.bv_len ) {
|
2005-11-14 06:26:53 +08:00
|
|
|
char *ptr = ber_bvchr(&rdn, ',');
|
|
|
|
assert( ptr != NULL );
|
2003-04-23 01:06:35 +08:00
|
|
|
rdn.bv_len = ptr - rdn.bv_val;
|
|
|
|
}
|
|
|
|
ber_dupbv( &ei->bei_rdn, &rdn );
|
2007-11-21 23:11:26 +08:00
|
|
|
|
|
|
|
/* If new parent, decrement kid counts */
|
|
|
|
if ( ein ) {
|
|
|
|
pei->bei_ckids--;
|
|
|
|
if ( pei->bei_dkids ) {
|
|
|
|
pei->bei_dkids--;
|
2007-11-22 16:52:39 +08:00
|
|
|
if ( pei->bei_dkids < 2 )
|
2007-11-21 23:11:26 +08:00
|
|
|
pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
|
|
|
|
}
|
2007-11-21 22:51:55 +08:00
|
|
|
}
|
2003-04-23 02:59:37 +08:00
|
|
|
#endif
|
2003-04-23 01:06:35 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
if (!ein) {
|
|
|
|
ein = ei->bei_parent;
|
|
|
|
} else {
|
|
|
|
ei->bei_parent = ein;
|
|
|
|
bdb_cache_entryinfo_unlock( pei );
|
|
|
|
bdb_cache_entryinfo_lock( ein );
|
2006-07-18 18:22:48 +08:00
|
|
|
|
2007-11-21 23:11:26 +08:00
|
|
|
/* new parent now has kids */
|
|
|
|
if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
|
|
|
|
ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
|
|
|
|
/* grandparent has grandkids */
|
|
|
|
if ( ein->bei_parent )
|
|
|
|
ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
|
2003-04-23 16:46:23 +08:00
|
|
|
#ifdef BDB_HIER
|
2007-11-21 23:11:26 +08:00
|
|
|
/* parent might now have grandkids */
|
|
|
|
if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
|
|
|
|
!(ei->bei_state & CACHE_ENTRY_NO_KIDS))
|
|
|
|
ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
|
2006-07-18 18:22:48 +08:00
|
|
|
|
2007-11-21 23:11:26 +08:00
|
|
|
ein->bei_ckids++;
|
|
|
|
if ( ein->bei_dkids ) ein->bei_dkids++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BDB_HIER
|
2007-11-21 22:55:40 +08:00
|
|
|
/* Record the generation number of this change */
|
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
|
|
|
|
bdb->bi_modrdns++;
|
|
|
|
ei->bei_modrdns = bdb->bi_modrdns;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
|
2003-04-23 16:46:23 +08:00
|
|
|
#endif
|
2007-11-21 23:11:26 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
|
|
|
|
bdb_cache_entryinfo_unlock( ein );
|
|
|
|
return rc;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
/*
|
2003-04-23 14:34:55 +08:00
|
|
|
* cache_delete - delete the entry e from the cache.
|
2002-01-25 15:19:01 +08:00
|
|
|
*
|
|
|
|
* returns: 0 e was deleted ok
|
|
|
|
* 1 e was not in the cache
|
|
|
|
* -1 something bad happened
|
|
|
|
*/
|
|
|
|
int
|
2003-04-23 14:34:55 +08:00
|
|
|
bdb_cache_delete(
|
2007-01-01 07:37:06 +08:00
|
|
|
struct bdb_info *bdb,
|
2003-04-17 00:23:36 +08:00
|
|
|
Entry *e,
|
2007-08-18 04:33:46 +08:00
|
|
|
BDB_LOCKER locker,
|
2003-12-31 12:26:17 +08:00
|
|
|
DB_LOCK *lock )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo *ei = BEI(e);
|
2002-01-25 15:19:01 +08:00
|
|
|
int rc;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( e->e_private != NULL );
|
2003-04-17 00:23:36 +08:00
|
|
|
|
2008-04-28 19:17:52 +08:00
|
|
|
/* Lock the entry's info */
|
|
|
|
bdb_cache_entryinfo_lock( ei );
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Set this early, warn off any queriers */
|
2003-04-17 08:52:31 +08:00
|
|
|
ei->bei_state |= CACHE_ENTRY_DELETED;
|
2003-04-17 00:23:36 +08:00
|
|
|
|
2008-04-28 19:17:52 +08:00
|
|
|
bdb_cache_entryinfo_unlock( ei );
|
2003-06-13 06:47:13 +08:00
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
/* Get write lock on the data */
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
|
2004-06-16 11:29:02 +08:00
|
|
|
if ( rc ) {
|
|
|
|
/* couldn't lock, undo and give up */
|
|
|
|
ei->bei_state ^= CACHE_ENTRY_DELETED;
|
|
|
|
return rc;
|
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
|
2003-04-23 14:34:55 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
|
2002-01-25 15:19:01 +08:00
|
|
|
e->e_id, 0, 0 );
|
|
|
|
|
2002-04-16 22:03:13 +08:00
|
|
|
/* set lru mutex */
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
|
2005-09-08 23:28:36 +08:00
|
|
|
|
2007-01-01 07:37:06 +08:00
|
|
|
rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
|
2005-09-08 23:28:36 +08:00
|
|
|
|
2002-04-16 22:03:13 +08:00
|
|
|
/* free lru mutex */
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
2003-05-23 10:04:03 +08:00
|
|
|
void
|
|
|
|
bdb_cache_delete_cleanup(
|
2003-09-27 12:58:36 +08:00
|
|
|
Cache *cache,
|
2004-11-08 12:23:37 +08:00
|
|
|
EntryInfo *ei )
|
2003-05-23 10:04:03 +08:00
|
|
|
{
|
2008-04-30 05:44:47 +08:00
|
|
|
/* Enter with ei locked */
|
2008-04-28 19:17:52 +08:00
|
|
|
|
2004-11-08 12:23:37 +08:00
|
|
|
if ( ei->bei_e ) {
|
|
|
|
ei->bei_e->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
|
|
|
|
#else
|
2004-11-08 12:23:37 +08:00
|
|
|
bdb_entry_return( ei->bei_e );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2004-11-08 12:23:37 +08:00
|
|
|
ei->bei_e = NULL;
|
|
|
|
}
|
2003-09-27 12:58:36 +08:00
|
|
|
|
2007-01-17 10:43:05 +08:00
|
|
|
bdb_cache_entryinfo_free( cache, ei );
|
2003-09-27 12:58:36 +08:00
|
|
|
bdb_cache_entryinfo_unlock( ei );
|
2003-05-23 10:04:03 +08:00
|
|
|
}
|
2003-09-27 12:58:36 +08:00
|
|
|
|
2002-01-25 15:19:01 +08:00
|
|
|
static int
|
2003-04-23 14:34:55 +08:00
|
|
|
bdb_cache_delete_internal(
|
2002-01-25 15:19:01 +08:00
|
|
|
Cache *cache,
|
2004-11-01 06:07:15 +08:00
|
|
|
EntryInfo *e,
|
|
|
|
int decr )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
|
|
|
int rc = 0; /* return code */
|
2007-01-01 00:16:48 +08:00
|
|
|
int decr_leaf = 0;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2004-10-31 21:04:27 +08:00
|
|
|
/* Lock the parent's kids tree */
|
|
|
|
bdb_cache_entryinfo_lock( e->bei_parent );
|
|
|
|
|
2003-09-22 16:37:32 +08:00
|
|
|
#ifdef BDB_HIER
|
|
|
|
e->bei_parent->bei_ckids--;
|
2004-11-01 06:07:15 +08:00
|
|
|
if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
|
2003-09-22 16:37:32 +08:00
|
|
|
#endif
|
2002-01-25 15:19:01 +08:00
|
|
|
/* dn tree */
|
2003-12-31 12:26:17 +08:00
|
|
|
if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
|
|
|
|
== NULL )
|
2002-01-25 15:19:01 +08:00
|
|
|
{
|
|
|
|
rc = -1;
|
|
|
|
}
|
2005-09-08 23:28:36 +08:00
|
|
|
if ( e->bei_parent->bei_kids )
|
2007-01-01 00:16:48 +08:00
|
|
|
decr_leaf = 1;
|
|
|
|
|
|
|
|
bdb_cache_entryinfo_unlock( e->bei_parent );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2007-01-01 00:16:48 +08:00
|
|
|
ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
|
2002-01-25 15:19:01 +08:00
|
|
|
/* id tree */
|
2007-01-01 00:16:48 +08:00
|
|
|
if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
|
|
|
|
cache->c_eiused--;
|
|
|
|
if ( decr_leaf )
|
|
|
|
cache->c_leaves--;
|
|
|
|
} else {
|
2002-01-25 15:19:01 +08:00
|
|
|
rc = -1;
|
|
|
|
}
|
2007-01-01 00:16:48 +08:00
|
|
|
ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2005-09-08 23:28:36 +08:00
|
|
|
if ( rc == 0 ){
|
|
|
|
/* lru */
|
2007-01-05 14:49:25 +08:00
|
|
|
LRU_DEL( cache, e );
|
2007-01-01 00:16:48 +08:00
|
|
|
|
|
|
|
if ( e->bei_e ) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
|
|
|
|
cache->c_cursize--;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
|
|
|
|
}
|
|
|
|
}
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2005-09-08 23:28:36 +08:00
|
|
|
return( rc );
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
static void
|
|
|
|
bdb_entryinfo_release( void *data )
|
|
|
|
{
|
|
|
|
EntryInfo *ei = (EntryInfo *)data;
|
2003-04-23 01:06:35 +08:00
|
|
|
if ( ei->bei_kids ) {
|
|
|
|
avl_free( ei->bei_kids, NULL );
|
|
|
|
}
|
2003-04-17 00:23:36 +08:00
|
|
|
if ( ei->bei_e ) {
|
|
|
|
ei->bei_e->e_private = NULL;
|
2004-12-15 01:22:22 +08:00
|
|
|
#ifdef SLAP_ZONE_ALLOC
|
|
|
|
bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
|
|
|
|
#else
|
2003-04-17 00:23:36 +08:00
|
|
|
bdb_entry_return( ei->bei_e );
|
2004-12-15 01:22:22 +08:00
|
|
|
#endif
|
2003-04-17 00:23:36 +08:00
|
|
|
}
|
|
|
|
bdb_cache_entryinfo_destroy( ei );
|
|
|
|
}
|
|
|
|
|
2002-01-25 15:19:01 +08:00
|
|
|
void
|
|
|
|
bdb_cache_release_all( Cache *cache )
|
|
|
|
{
|
2002-04-16 22:03:13 +08:00
|
|
|
/* set cache write lock */
|
2002-04-16 00:40:04 +08:00
|
|
|
ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
|
2002-04-16 22:03:13 +08:00
|
|
|
/* set lru mutex */
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
|
2002-01-25 15:19:01 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
|
|
|
|
|
2003-04-17 00:23:36 +08:00
|
|
|
avl_free( cache->c_dntree.bei_kids, NULL );
|
|
|
|
avl_free( cache->c_idtree, bdb_entryinfo_release );
|
2004-07-06 05:00:09 +08:00
|
|
|
for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
|
|
|
|
cache->c_lruhead = cache->c_eifree->bei_lrunext;
|
|
|
|
bdb_cache_entryinfo_destroy(cache->c_eifree);
|
|
|
|
}
|
2005-04-23 02:07:02 +08:00
|
|
|
cache->c_cursize = 0;
|
|
|
|
cache->c_eiused = 0;
|
2005-09-08 23:28:36 +08:00
|
|
|
cache->c_leaves = 0;
|
2005-04-23 02:07:02 +08:00
|
|
|
cache->c_idtree = NULL;
|
2003-04-17 00:23:36 +08:00
|
|
|
cache->c_lruhead = NULL;
|
|
|
|
cache->c_lrutail = NULL;
|
2005-04-23 02:07:02 +08:00
|
|
|
cache->c_dntree.bei_kids = NULL;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2002-04-16 22:03:13 +08:00
|
|
|
/* free lru mutex */
|
2007-01-04 19:01:59 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
|
2002-04-16 22:03:13 +08:00
|
|
|
/* free cache write lock */
|
2002-04-16 00:40:04 +08:00
|
|
|
ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
2005-11-15 05:47:17 +08:00
|
|
|
#ifdef SLAPD_UNUSED
|
2002-01-25 15:19:01 +08:00
|
|
|
static void
|
|
|
|
bdb_lru_print( Cache *cache )
|
|
|
|
{
|
2003-04-17 00:23:36 +08:00
|
|
|
EntryInfo *e;
|
2002-01-25 15:19:01 +08:00
|
|
|
|
2007-04-05 01:49:31 +08:00
|
|
|
fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
|
2007-01-01 19:17:13 +08:00
|
|
|
fprintf( stderr, "LRU circle (tail forward):\n" );
|
|
|
|
for ( e = cache->c_lrutail; ; ) {
|
|
|
|
fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
|
2007-04-05 01:49:31 +08:00
|
|
|
(void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
|
2007-01-01 19:17:13 +08:00
|
|
|
e = e->bei_lrunext;
|
|
|
|
if ( e == cache->c_lrutail )
|
|
|
|
break;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
2007-01-01 19:17:13 +08:00
|
|
|
fprintf( stderr, "LRU circle (tail backward):\n" );
|
|
|
|
for ( e = cache->c_lrutail; ; ) {
|
|
|
|
fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
|
2007-04-05 01:49:31 +08:00
|
|
|
(void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
|
2007-01-01 19:17:13 +08:00
|
|
|
e = e->bei_lruprev;
|
|
|
|
if ( e == cache->c_lrutail )
|
|
|
|
break;
|
2002-01-25 15:19:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-15 05:47:17 +08:00
|
|
|
#endif
|
2002-08-24 16:11:08 +08:00
|
|
|
|
|
|
|
#ifdef BDB_REUSE_LOCKERS
|
2003-04-23 14:34:55 +08:00
|
|
|
static void
|
2002-08-24 16:11:08 +08:00
|
|
|
bdb_locker_id_free( void *key, void *data )
|
|
|
|
{
|
|
|
|
DB_ENV *env = key;
|
2007-08-18 04:33:46 +08:00
|
|
|
u_int32_t lockid;
|
2003-05-10 06:30:28 +08:00
|
|
|
int rc;
|
|
|
|
|
2007-08-18 04:33:46 +08:00
|
|
|
#if DB_VERSION_FULL >= 0x04060012
|
|
|
|
BDB_LOCKER lptr = data;
|
|
|
|
lockid = lptr->id;
|
|
|
|
#else
|
|
|
|
lockid = (long)data;
|
|
|
|
#endif
|
2003-05-10 06:30:28 +08:00
|
|
|
rc = XLOCK_ID_FREE( env, lockid );
|
|
|
|
if ( rc == EINVAL ) {
|
|
|
|
DB_LOCKREQ lr;
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2005-07-27 20:19:16 +08:00
|
|
|
"bdb_locker_id_free: %lu err %s(%d)\n",
|
|
|
|
(unsigned long) lockid, db_strerror(rc), rc );
|
2003-05-10 06:30:28 +08:00
|
|
|
/* release all locks held by this locker. */
|
|
|
|
lr.op = DB_LOCK_PUT_ALL;
|
2004-07-10 01:00:07 +08:00
|
|
|
lr.obj = NULL;
|
2003-05-10 06:30:28 +08:00
|
|
|
env->lock_vec( env, lockid, 0, &lr, 1, NULL );
|
|
|
|
XLOCK_ID_FREE( env, lockid );
|
|
|
|
}
|
2002-08-24 16:11:08 +08:00
|
|
|
}
|
|
|
|
|
2007-01-25 20:00:40 +08:00
|
|
|
/* free up any keys used by the main thread */
|
|
|
|
void
|
|
|
|
bdb_locker_flush( DB_ENV *env )
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
void *ctx = ldap_pvt_thread_pool_context();
|
|
|
|
|
|
|
|
if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
|
2008-01-11 14:07:43 +08:00
|
|
|
ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
|
2007-01-25 20:00:40 +08:00
|
|
|
bdb_locker_id_free( env, data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-24 16:11:08 +08:00
|
|
|
int
|
2007-08-18 04:33:46 +08:00
|
|
|
bdb_locker_id( Operation *op, DB_ENV *env, BDB_LOCKER *locker )
|
2002-08-24 16:11:08 +08:00
|
|
|
{
|
2005-07-27 20:19:16 +08:00
|
|
|
int i, rc;
|
|
|
|
u_int32_t lockid;
|
2002-08-24 16:11:08 +08:00
|
|
|
void *data;
|
2003-03-29 13:15:48 +08:00
|
|
|
void *ctx;
|
2002-08-24 16:11:08 +08:00
|
|
|
|
2003-03-29 13:15:48 +08:00
|
|
|
if ( !env || !locker ) return -1;
|
|
|
|
|
|
|
|
/* If no op was provided, try to find the ctx anyway... */
|
|
|
|
if ( op ) {
|
|
|
|
ctx = op->o_threadctx;
|
|
|
|
} else {
|
2003-04-12 12:01:40 +08:00
|
|
|
ctx = ldap_pvt_thread_pool_context();
|
2003-03-29 13:15:48 +08:00
|
|
|
}
|
2002-09-19 09:13:27 +08:00
|
|
|
|
2002-09-19 14:34:53 +08:00
|
|
|
/* Shouldn't happen unless we're single-threaded */
|
2003-03-29 13:15:48 +08:00
|
|
|
if ( !ctx ) {
|
2002-08-24 16:11:08 +08:00
|
|
|
*locker = 0;
|
2002-09-19 14:34:53 +08:00
|
|
|
return 0;
|
2002-08-24 16:11:08 +08:00
|
|
|
}
|
|
|
|
|
2003-03-29 13:15:48 +08:00
|
|
|
if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
|
2002-08-24 16:11:08 +08:00
|
|
|
for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
|
|
|
|
rc = XLOCK_ID( env, &lockid );
|
|
|
|
if (rc) ldap_pvt_thread_yield();
|
|
|
|
}
|
|
|
|
if ( rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
2007-08-18 04:33:46 +08:00
|
|
|
#if DB_VERSION_FULL >= 0x04060012
|
|
|
|
{ BDB_LOCKER lptr;
|
|
|
|
__lock_getlocker( env->lk_handle, lockid, 0, &lptr );
|
|
|
|
data = lptr;
|
|
|
|
}
|
|
|
|
#else
|
2005-09-09 07:47:19 +08:00
|
|
|
data = (void *)((long)lockid);
|
2007-08-18 04:33:46 +08:00
|
|
|
#endif
|
2003-03-29 13:15:48 +08:00
|
|
|
if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
|
2008-01-08 05:35:03 +08:00
|
|
|
data, bdb_locker_id_free, NULL, NULL ) ) ) {
|
2002-08-24 16:11:08 +08:00
|
|
|
XLOCK_ID_FREE( env, lockid );
|
|
|
|
Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
|
|
|
|
db_strerror(rc), rc, 0 );
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
} else {
|
2005-09-09 07:47:19 +08:00
|
|
|
lockid = (long)data;
|
2002-08-24 16:11:08 +08:00
|
|
|
}
|
2007-08-18 04:33:46 +08:00
|
|
|
#if DB_VERSION_FULL >= 0x04060012
|
|
|
|
*locker = data;
|
|
|
|
#else
|
2002-08-24 16:11:08 +08:00
|
|
|
*locker = lockid;
|
2007-08-18 04:33:46 +08:00
|
|
|
#endif
|
2002-08-24 16:11:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2005-07-27 20:19:16 +08:00
|
|
|
#endif /* BDB_REUSE_LOCKERS */
|