]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/cache.c
Sync with HEAD
[openldap] / servers / slapd / back-bdb / cache.c
index 7fee76c2abefb52a7f5a6b085f553e38a235413a..d6dd3661657ccd2801d6ad2af4d71d8c7df8b391 100644 (file)
@@ -1,8 +1,17 @@
 /* cache.c - routines to maintain an in-core cache of entries */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2004 The OpenLDAP Foundation.
+ * 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>.
  */
 
 #include "portable.h"
 
 #include "back-bdb.h"
 
-static int     bdb_cache_delete_entry_internal(Cache *cache, EntryInfo *e);
+static int     bdb_cache_delete_internal(Cache *cache, EntryInfo *e);
 #ifdef LDAP_DEBUG
 static void    bdb_lru_print(Cache *cache);
 #endif
 
 static EntryInfo *
-bdb_cache_entryinfo_new( )
+bdb_cache_entryinfo_new( Cache *cache )
 {
-       EntryInfo *ei;
+       EntryInfo *ei = NULL;
 
-       ei = ch_calloc(1, sizeof(struct bdb_entry_info));
-       ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
+       if ( cache->c_eifree ) {
+               ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
+               if ( cache->c_eifree ) {
+                       ei = cache->c_eifree;
+                       cache->c_eifree = ei->bei_lrunext;
+               }
+               ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
+       }
+       if ( ei ) {
+               ei->bei_lrunext = NULL;
+               ei->bei_state = 0;
+       } else {
+               ei = ch_calloc(1, sizeof(struct bdb_entry_info));
+               ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
+       }
 
        return ei;
 }
@@ -50,6 +72,8 @@ bdb_cache_entry_db_relock(
        DBT     lockobj;
        DB_LOCKREQ list[2];
 
+       if ( !lock ) return 0;
+
        lockobj.data = ei;
        lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
 
@@ -65,11 +89,11 @@ bdb_cache_entry_db_relock(
        if (rc) {
 #ifdef NEW_LOGGING
                LDAP_LOG( CACHE, DETAIL1, 
-                       "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
+                       "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
                        ei->bei_id, rw, rc );
 #else
                Debug( LDAP_DEBUG_TRACE,
-                       "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
+                       "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
                        ei->bei_id, rw, rc );
 #endif
        } else {
@@ -78,9 +102,10 @@ bdb_cache_entry_db_relock(
        return rc;
 #endif
 }
-int
-bdb_cache_entry_db_lock
-( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
+
+static int
+bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
+       int rw, int tryOnly, DB_LOCK *lock )
 {
 #ifdef NO_THREADS
        return 0;
@@ -89,6 +114,8 @@ bdb_cache_entry_db_lock
        DBT       lockobj;
        int       db_rw;
 
+       if ( !lock ) return 0;
+
        if (rw)
                db_rw = DB_LOCK_WRITE;
        else
@@ -102,11 +129,11 @@ bdb_cache_entry_db_lock
        if (rc) {
 #ifdef NEW_LOGGING
                LDAP_LOG( CACHE, DETAIL1, 
-                       "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
+                       "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
                        ei->bei_id, rw, rc );
 #else
                Debug( LDAP_DEBUG_TRACE,
-                       "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
+                       "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
                        ei->bei_id, rw, rc );
 #endif
        }
@@ -115,14 +142,15 @@ bdb_cache_entry_db_lock
 }
 
 int
-bdb_cache_entry_db_unlock
-( DB_ENV *env, DB_LOCK *lock )
+bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
 {
 #ifdef NO_THREADS
        return 0;
 #else
        int rc;
 
+       if ( !lock ) return 0;
+
        rc = LOCK_PUT ( env, lock );
        return rc;
 #endif
@@ -133,6 +161,9 @@ bdb_cache_entryinfo_destroy( EntryInfo *e )
 {
        ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
        free( e->bei_nrdn.bv_val );
+#ifdef BDB_HIER
+       free( e->bei_rdn.bv_val );
+#endif
        free( e );
        return 0;
 }
@@ -162,13 +193,16 @@ bdb_cache_entryinfo_destroy( EntryInfo *e )
        } \
 } while(0)
 
-/* Do a lexical sort on normalized RDNs */
+/* Do a length-ordered sort on normalized RDNs */
 static int
 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
 {
        const EntryInfo *e1 = v_e1, *e2 = v_e2;
-       int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
-       if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
+       int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
+       if (rc == 0) {
+               rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
+                       e1->bei_nrdn.bv_len );
+       }
        return rc;
 }
 
@@ -181,116 +215,47 @@ bdb_id_cmp( const void *v_e1, const void *v_e2 )
 
 /* Create an entryinfo in the cache. Caller must release the locks later.
  */
-int
+static int
 bdb_entryinfo_add_internal(
        struct bdb_info *bdb,
-       EntryInfo *eip,
-       ID id,
-       struct berval *nrdn,
-       EntryInfo **res,
-       u_int32_t locker
-)
+       EntryInfo *ei,
+       EntryInfo **res )
 {
-       Cache *cache = &bdb->bi_cache;
-       DB_ENV *env = bdb->bi_dbenv;
        EntryInfo *ei2 = NULL;
-       int incr = 1;
-       int addkid = 1;
-       int rc;
-       DB_LOCK lock;
 
        *res = NULL;
 
-       ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
-       bdb_cache_entryinfo_lock( eip );
-
-       /* if parent was previously considered a leaf node,
-        * it was on the LRU list. Now it's going to have
-        * kids, take it off the LRU list.
-        */
-       ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
-       if ( eip->bei_id && !eip->bei_kids ) {
-               LRU_DELETE( cache, eip );
-               incr = 0;
-       }
-
-       cache->c_cursize += incr;
+       ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
 
-       /* See if we're above the cache size limit */
-       if ( cache->c_cursize > cache->c_maxsize ) {
-               EntryInfo *elru, *elprev;
-               int i = 0;
-
-               /* Look for an unused entry to remove */
-               for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
-                       elprev = elru->bei_lruprev;
-
-                       /* Too many probes, not enough idle, give up */
-                       if (i > 10) break;
+       ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
+       bdb_cache_entryinfo_lock( ei->bei_parent );
 
-                       /* If we can successfully writelock it, then
-                        * the object is idle.
-                        */
-                       if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
-                               &lock ) == 0 ) {
-                               /* Need to lock parent to delete child */
-                               if ( ldap_pvt_thread_mutex_trylock(
-                                       &elru->bei_parent->bei_kids_mutex )) {
-                                       bdb_cache_entry_db_unlock( env, &lock );
-                                       continue;
-                               }
-                               bdb_cache_delete_entry_internal( cache, elru );
-                               bdb_cache_entryinfo_unlock( elru->bei_parent );
-                               elru->bei_e->e_private = NULL;
-                               bdb_entry_return( elru->bei_e );
-                               bdb_cache_entry_db_unlock( env, &lock );
-                               if (ei2) {
-                                       bdb_cache_entryinfo_destroy( elru );
-                               } else {
-                                       /* re-use this one */
-                                       ch_free(elru->bei_nrdn.bv_val);
-                                       elru->bei_nrdn.bv_val = NULL;
-                                       elru->bei_e = NULL;
-                                       elru->bei_kids = NULL;
-                                       elru->bei_lrunext = NULL;
-                                       elru->bei_lruprev = NULL;
-                                       elru->bei_state = 0;
-                                       ei2 = elru;
-                               }
-                               if (cache->c_cursize < cache->c_maxsize)
-                                       break;
-                       }
-               }
-       }
-       if (!ei2) {
-               ei2 = bdb_cache_entryinfo_new();
-       }
-       ei2->bei_id = id;
-       ei2->bei_parent = eip;
+       ei2->bei_id = ei->bei_id;
+       ei2->bei_parent = ei->bei_parent;
+#ifdef BDB_HIER
+       ei2->bei_rdn = ei->bei_rdn;
+#endif
 
        /* Add to cache ID tree */
-       if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
-               EntryInfo *ei;
-               ei = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
+       if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
+               EntryInfo *eix;
+               eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
                bdb_cache_entryinfo_destroy( ei2 );
-               ei2 = ei;
-               addkid = 0;
-               cache->c_cursize -= incr;
+               ei2 = eix;
+#ifdef BDB_HIER
+               /* It got freed above because its value was
+                * assigned to ei2.
+                */
+               ei->bei_rdn.bv_val = NULL;
+#endif
        } else {
-               LRU_ADD( cache, ei2 );
-               ber_dupbv( &ei2->bei_nrdn, nrdn );
-       }
-
-       if ( addkid ) {
-               avl_insert( &eip->bei_kids, ei2, bdb_rdn_cmp, avl_dup_error );
-       }
-
-       ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
-
-#if 0 /* caller must do these frees */
-       ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
-       bdb_cache_entryinfo_unlock( eip );
+               ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
+               avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
+                       avl_dup_error );
+#ifdef BDB_HIER
+               ei->bei_parent->bei_ckids++;
 #endif
+       }
 
        *res = ei2;
        return 0;
@@ -303,16 +268,13 @@ bdb_entryinfo_add_internal(
  * The EntryInfo is locked upon return and must be unlocked by the caller.
  */
 int
-bdb_cache_find_entry_ndn2id(
-       Backend         *be,
+bdb_cache_find_ndn(
+       Operation       *op,
        DB_TXN          *txn,
        struct berval   *ndn,
-       EntryInfo       **res,
-       u_int32_t       locker,
-       void            *ctx
-)
+       EntryInfo       **res )
 {
-       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+       struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
        EntryInfo       ei, *eip, *ei2;
        int rc = 0;
        char *ptr;
@@ -321,25 +283,27 @@ bdb_cache_find_entry_ndn2id(
        if ( *res ) {
                /* we're doing a onelevel search for an RDN */
                ei.bei_nrdn.bv_val = ndn->bv_val;
-               ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
+               ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
                eip = *res;
        } else {
                /* we're searching a full DN from the root */
-               ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
+               ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
                ei.bei_nrdn.bv_val = ptr;
-               ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
+               ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
                eip = &bdb->bi_cache.c_dntree;
        }
        
        for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
+               ei.bei_parent = eip;
                ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
                if ( !ei2 ) {
                        int len = ei.bei_nrdn.bv_len;
                                
-                       ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
+                       ei.bei_nrdn.bv_len = ndn->bv_len -
+                               (ei.bei_nrdn.bv_val - ndn->bv_val);
                        bdb_cache_entryinfo_unlock( eip );
 
-                       rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
+                       rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
                        if (rc) {
                                bdb_cache_entryinfo_lock( eip );
                                *res = eip;
@@ -348,8 +312,7 @@ bdb_cache_find_entry_ndn2id(
 
                        /* DN exists but needs to be added to cache */
                        ei.bei_nrdn.bv_len = len;
-                       rc = bdb_entryinfo_add_internal( bdb,
-                               eip, ei.bei_id, &ei.bei_nrdn, &ei2, locker );
+                       rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
                        /* add_internal left eip and c_rwlock locked */
                        ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
                        if ( rc ) {
@@ -373,7 +336,7 @@ bdb_cache_find_entry_ndn2id(
 
                /* Advance to next lower RDN */
                for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
-                       && !DN_SEPARATOR(*ptr); ptr--);
+                       && !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 - 1;
@@ -388,38 +351,271 @@ bdb_cache_find_entry_ndn2id(
        return rc;
 }
 
+#ifdef BDB_HIER
+/* Walk up the tree from a child node, looking for an ID that's already
+ * been linked into the cache.
+ */
+static int
+hdb_cache_find_parent(
+       Operation *op,
+       DB_TXN *txn,
+       ID id,
+       EntryInfo **res )
+{
+       struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
+       EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
+       char ndn[SLAP_LDAPDN_MAXLEN];
+       ID parent;
+       int rc;
+       int addlru = 1;
+
+       ei.bei_id = id;
+       ei.bei_kids = NULL;
+
+       for (;;) {
+               rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
+               if ( rc ) break;
+
+               /* Save the previous node, if any */
+               ei2 = ein;
+
+               /* Create a new node for the current ID */
+               ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
+               ein->bei_id = ei.bei_id;
+               ein->bei_kids = ei.bei_kids;
+               ein->bei_nrdn = ei.bei_nrdn;
+               ein->bei_rdn = ei.bei_rdn;
+               
+               /* This node is not fully connected yet */
+               ein->bei_state = CACHE_ENTRY_NOT_LINKED;
+
+               /* Insert this node into the ID tree */
+               ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
+               if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
+                       bdb_id_cmp, avl_dup_error ) ) {
+
+                       /* Someone else created this node just before us.
+                        * Free our new copy and use the existing one.
+                        */
+                       bdb_cache_entryinfo_destroy( ein );
+                       ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
+                               (caddr_t) &ei, bdb_id_cmp );
+                       
+                       /* Link in any kids we've already processed */
+                       if ( ei2 ) {
+                               bdb_cache_entryinfo_lock( ein );
+                               avl_insert( &ein->bei_kids, (caddr_t)ei2,
+                                       bdb_rdn_cmp, avl_dup_error );
+                               bdb_cache_entryinfo_unlock( ein );
+                       }
+
+                       if ( !eir ) {
+                               addlru = 0;
+                       }
+               }
+
+               /* If this is the first time, save this node
+                * to be returned later.
+                */
+               if ( eir == NULL ) eir = ein;
+
+               /* If there was a previous node, link it to this one */
+               if ( ei2 ) ei2->bei_parent = ein;
+
+               /* Look for this node's parent */
+               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;
+               }
+               ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
+
+               /* Got the parent, link in and we're done. */
+               if ( ei2 ) {
+                       bdb_cache_entryinfo_lock( ei2 );
+                       ein->bei_parent = ei2;
+                       avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
+                               avl_dup_error);
+                       bdb_cache_entryinfo_unlock( ei2 );
+                       bdb_cache_entryinfo_lock( eir );
+
+                       /* Reset all the state info */
+                       for (ein = eir; ein != ei2; ein=ein->bei_parent)
+                               ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
+                       *res = eir;
+                       break;
+               }
+               ei.bei_kids = NULL;
+               ei.bei_id = eip.bei_id;
+               avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
+                       avl_dup_error );
+       }
+       return rc;
+}
+
+/* Used by hdb_dn2idl when loading the EntryInfo for all the children
+ * of a given node
+ */
+int hdb_cache_load(
+       struct bdb_info *bdb,
+       EntryInfo *ei,
+       EntryInfo **res )
+{
+       EntryInfo *ei2;
+       int rc;
+
+       /* See if we already have this one */
+       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 );
+
+       if ( !ei2 ) {
+               /* 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;
+
+               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 {
+               /* Found, return it */
+               *res = ei2;
+               return 0;
+       }
+       return rc;
+}
+#endif
+
+/* caller must have lru_mutex locked. mutex
+ * will be unlocked on return.
+ */
+static void
+bdb_cache_lru_add(
+       struct bdb_info *bdb,
+       u_int32_t       locker,
+       EntryInfo *ei )
+{
+       DB_LOCK         lock, *lockp;
+
+       if ( locker ) {
+               lockp = &lock;
+       } else {
+               lockp = NULL;
+       }
+
+       /* See if we're above the cache size limit */
+       if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
+               EntryInfo *elru, *elprev;
+               int i = 0;
+
+               /* Look for an unused entry to remove */
+               for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
+                       elprev = elru->bei_lruprev;
+
+                       /* Too many probes, not enough idle, give up */
+                       if (i > 10) break;
+
+                       /* If we can successfully writelock it, then
+                        * the object is idle.
+                        */
+                       if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, elru, 1, 1,
+                               lockp ) == 0 ) {
+                               /* If there's no entry, or this node is in
+                                * the process of linking into the cache,
+                                * skip it.
+                                */
+                               if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
+                                       bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
+                                       continue;
+                               }
+                               LRU_DELETE( &bdb->bi_cache, elru );
+                               elru->bei_e->e_private = NULL;
+                               bdb_entry_return( elru->bei_e );
+                               elru->bei_e = NULL;
+                               bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
+                               --bdb->bi_cache.c_cursize;
+                               if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
+                                       break;
+                       }
+               }
+       }
+       LRU_ADD( &bdb->bi_cache, ei );
+       ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
+}
+
+EntryInfo *
+bdb_cache_find_info(
+       struct bdb_info *bdb,
+       ID id )
+{
+       EntryInfo ei, *ei2;
+
+       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;
+}
+
 /*
- * cache_find_entry_id - find an entry in the cache, given id.
+ * cache_find_id - find an entry in the cache, given id.
  * The entry is locked for Read upon return. Call with islocked TRUE if
  * the supplied *eip was already locked.
  */
 
 int
-bdb_cache_find_entry_id(
-       Backend *be,
+bdb_cache_find_id(
+       Operation *op,
        DB_TXN  *tid,
        ID                              id,
        EntryInfo       **eip,
        int             islocked,
        u_int32_t       locker,
-       DB_LOCK         *lock,
-       void            *ctx
-)
+       DB_LOCK         *lock )
 {
-       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+       struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
        Entry   *ep = NULL;
        int     rc = 0;
        EntryInfo ei;
+       int lru_del = 0;
 
        ei.bei_id = id;
 
        /* If we weren't given any info, see if we have it already cached */
        if ( !*eip ) {
-               ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
+again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
                *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
-                                       (caddr_t) &ei, bdb_id_cmp );
+                       (caddr_t) &ei, bdb_id_cmp );
                if ( *eip ) {
-                       bdb_cache_entryinfo_lock( *eip );
+                       /* If the lock attempt fails, the info is in use */
+                       if ( ldap_pvt_thread_mutex_trylock(
+                                       &(*eip)->bei_kids_mutex )) {
+                               ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
+                               /* If this node is being deleted, treat
+                                * as if the delete has already finished
+                                */
+                               if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
+                                       return DB_NOTFOUND;
+                               }
+                               /* 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 );
+                               ldap_pvt_thread_yield();
+                               goto again;
+                       }
                        islocked = 1;
                }
                ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
@@ -427,46 +623,82 @@ bdb_cache_find_entry_id(
 
        /* See if the ID exists in the database; add it to the cache if so */
        if ( !*eip ) {
-               rc = bdb_id2entry( be, tid, id, &ep );
+#ifndef BDB_HIER
+               rc = bdb_id2entry( op->o_bd, tid, id, &ep );
                if ( rc == 0 ) {
-                       rc = bdb_cache_find_entry_ndn2id( be, tid,
-                               &ep->e_nname, eip, locker, ctx );
-                       if ( *eip )
-                               islocked = 1;
+                       rc = bdb_cache_find_ndn( op, tid,
+                               &ep->e_nname, eip );
+                       if ( *eip ) islocked = 1;
                        if ( rc ) {
                                bdb_entry_return( ep );
                                ep = NULL;
                        }
                }
+#else
+               rc = hdb_cache_find_parent(op, tid, id, eip );
+               if ( rc == 0 && *eip ) islocked = 1;
+#endif
        }
 
        /* Ok, we found the info, do we have the entry? */
        if ( *eip && rc == 0 ) {
                if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
                        rc = DB_NOTFOUND;
-               } else if (!(*eip)->bei_e ) {
-                       if (!ep) {
-                               rc = bdb_id2entry( be, tid, id, &ep );
-                       }
-                       if ( rc == 0 ) {
-                               bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
-                                       *eip, 1, 0, lock );
-                               (*eip)->bei_e = ep;
-                               ep->e_private = *eip;
-                               bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
-                                       *eip, 0, 0, lock );
-                       }
                } else {
-                       bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
-                                       *eip, 0, 0, lock );
+                       bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
+                       if ( !(*eip)->bei_e ) {
+                               if (!ep) {
+                                       rc = bdb_id2entry( op->o_bd, tid, id, &ep );
+                               }
+                               if ( rc == 0 ) {
+                                       bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
+                                               *eip, 1, 0, lock );
+                                       /* Make sure no other modifier beat us to it */
+                                       if ( (*eip)->bei_e ) {
+                                               bdb_entry_return( ep );
+                                               ep = NULL;
+                                       } else {
+                                               ep->e_private = *eip;
+#ifdef BDB_HIER
+                                               bdb_fix_dn( ep, 0 );
+#endif
+                                               (*eip)->bei_e = ep;
+                                       }
+                                       bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
+                                               *eip, 0, 0, lock );
+                               }
+                       } else {
+                               /* If we had the entry already, this item
+                                * is on the LRU list.
+                                */
+                               lru_del = 1;
+#ifdef BDB_HIER
+                               rc = bdb_fix_dn( (*eip)->bei_e, 1 );
+                               if ( rc ) {
+                                       bdb_cache_entry_db_relock( bdb->bi_dbenv,
+                                               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 );
+                                       bdb_cache_entry_db_relock( bdb->bi_dbenv,
+                                               locker, *eip, 0, 0, lock );
+                               }
+#endif
+                       }
                }
        }
-       if ( rc == 0 && (*eip)->bei_kids == NULL ) {
+       if ( rc == 0 ) {
                /* set lru mutex */
                ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
-               LRU_DELETE( &bdb->bi_cache, *eip );
-               LRU_ADD( &bdb->bi_cache, *eip );
-               ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
+               /* if entry is old, remove from old spot on LRU list */
+               if ( lru_del ) {
+                       LRU_DELETE( &bdb->bi_cache, *eip );
+               } else {
+               /* if entry is new, bump cache size */
+                       bdb->bi_cache.c_cursize++;
+               }
+               /* lru_mutex is unlocked for us */
+               bdb_cache_lru_add( bdb, locker, *eip );
        }
 
        if ( islocked ) {
@@ -475,25 +707,70 @@ bdb_cache_find_entry_id(
        return rc;
 }
 
+int
+bdb_cache_children(
+       Operation *op,
+       DB_TXN *txn,
+       Entry *e )
+{
+       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 ) {
+               BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
+       }
+       return rc;
+}
+
 /* Update the cache after a successful database Add. */
 int
 bdb_cache_add(
        struct bdb_info *bdb,
-       EntryInfo *ei,
+       EntryInfo *eip,
        Entry *e,
        struct berval *nrdn,
-       u_int32_t locker
-)
+       u_int32_t locker )
 {
-       EntryInfo *new;
+       EntryInfo *new, ei;
+       struct berval rdn = e->e_name;
        int rc;
 
-       rc = bdb_entryinfo_add_internal( bdb, ei, e->e_id, nrdn, &new, locker );
+       ei.bei_id = e->e_id;
+       ei.bei_parent = eip;
+       ei.bei_nrdn = *nrdn;
+#ifdef BDB_HIER
+       if ( nrdn->bv_len != e->e_nname.bv_len ) {
+               char *ptr = strchr( rdn.bv_val, ',' );
+               rdn.bv_len = ptr - rdn.bv_val;
+       }
+       ber_dupbv( &ei.bei_rdn, &rdn );
+       if ( eip->bei_dkids ) eip->bei_dkids++;
+#endif
+
+       rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
+       /* bdb_csn_commit can cause this when adding the database root entry */
+       if ( new->bei_e ) bdb_entry_return( new->bei_e );
        new->bei_e = e;
        e->e_private = new;
-       new->bei_state = CACHE_ENTRY_NO_KIDS;
-       ei->bei_state &= ~CACHE_ENTRY_NO_KIDS;
-       bdb_cache_entryinfo_unlock( ei );
+       new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
+       eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
+       if (eip->bei_parent) {
+               eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
+       }
+
+       /* set lru mutex */
+       ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
+       ++bdb->bi_cache.c_cursize;
+       /* lru_mutex is unlocked for us */
+       bdb_cache_lru_add( bdb, locker, new );
+
+       bdb_cache_entryinfo_unlock( eip );
        ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
        return rc;
 }
@@ -504,8 +781,7 @@ bdb_cache_modify(
        Attribute *newAttrs,
        DB_ENV *env,
        u_int32_t locker,
-       DB_LOCK *lock
-)
+       DB_LOCK *lock )
 {
        EntryInfo *ei = BEI(e);
        
@@ -534,10 +810,10 @@ bdb_cache_modrdn(
        EntryInfo *ein,
        DB_ENV *env,
        u_int32_t locker,
-       DB_LOCK *lock
-)
+       DB_LOCK *lock )
 {
        EntryInfo *ei = BEI(e), *pei;
+       struct berval rdn;
        int rc = 0;
 
        /* Get write lock on data */
@@ -550,15 +826,12 @@ bdb_cache_modrdn(
                attrs_free( e->e_attrs );
        }
        e->e_attrs = new->e_attrs;
-#ifdef BDB_HIER
-       ch_free(e->e_name.bv_val);
-#else
-       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 ) {
+       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 )
+       {
                ch_free(e->e_name.bv_val);
                ch_free(e->e_nname.bv_val);
        }
-#endif
        e->e_name = new->e_name;
        e->e_nname = new->e_nname;
 
@@ -568,6 +841,17 @@ bdb_cache_modrdn(
        avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
        free( ei->bei_nrdn.bv_val );
        ber_dupbv( &ei->bei_nrdn, nrdn );
+#ifdef BDB_HIER
+       free( ei->bei_rdn.bv_val );
+
+       rdn = e->e_name;
+       if ( nrdn->bv_len != e->e_nname.bv_len ) {
+               char *ptr = strchr(rdn.bv_val, ',');
+               rdn.bv_len = ptr - rdn.bv_val;
+       }
+       ber_dupbv( &ei->bei_rdn, &rdn );
+#endif
+
        if (!ein) {
                ein = ei->bei_parent;
        } else {
@@ -575,25 +859,34 @@ bdb_cache_modrdn(
                bdb_cache_entryinfo_unlock( pei );
                bdb_cache_entryinfo_lock( ein );
        }
+#ifdef BDB_HIER
+       {
+               int max = ei->bei_modrdns;
+               /* Record the generation number of this change */
+               for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
+                       if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
+               }
+               ei->bei_modrdns = max + 1;
+       }
+#endif
        avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
        bdb_cache_entryinfo_unlock( ein );
        return rc;
 }
 /*
- * cache_delete_entry - delete the entry e from the cache. 
+ * cache_delete - delete the entry e from the cache. 
  *
  * returns:    0       e was deleted ok
  *             1       e was not in the cache
  *             -1      something bad happened
  */
 int
-bdb_cache_delete_entry(
+bdb_cache_delete(
     Cache      *cache,
     Entry              *e,
     DB_ENV     *env,
     u_int32_t  locker,
-    DB_LOCK    *lock
-)
+    DB_LOCK    *lock )
 {
        EntryInfo *ei = BEI(e);
        int     rc;
@@ -603,6 +896,9 @@ bdb_cache_delete_entry(
        /* Set this early, warn off any queriers */
        ei->bei_state |= CACHE_ENTRY_DELETED;
 
+       /* Lock the entry's info */
+       bdb_cache_entryinfo_lock( ei );
+
        /* Get write lock on the data */
        bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
 
@@ -614,49 +910,78 @@ bdb_cache_delete_entry(
 
 #ifdef NEW_LOGGING
        LDAP_LOG( CACHE, ENTRY, 
-               "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
+               "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
 #else
-       Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
+       Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
                e->e_id, 0, 0 );
 #endif
 
        /* set lru mutex */
        ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
-       rc = bdb_cache_delete_entry_internal( cache, e->e_private );
+       rc = bdb_cache_delete_internal( cache, e->e_private );
        /* free lru mutex */
        ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
 
        /* free cache write lock */
        ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
        bdb_cache_entryinfo_unlock( ei->bei_parent );
-       bdb_cache_entryinfo_destroy( ei );
-       e->e_private = NULL;
+
+       /* Leave entry info locked */
+
        return( rc );
 }
 
+void
+bdb_cache_delete_cleanup(
+       Cache *cache,
+       Entry *e )
+{
+       EntryInfo *ei = BEI(e);
+
+       ei->bei_e = NULL;
+       e->e_private = NULL;
+       bdb_entry_return( e );
+
+       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_rdwr_wlock( &cache->c_rwlock );
+       ei->bei_lrunext = cache->c_eifree;
+       cache->c_eifree = ei;
+       ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
+       bdb_cache_entryinfo_unlock( ei );
+}
+
 static int
-bdb_cache_delete_entry_internal(
+bdb_cache_delete_internal(
     Cache      *cache,
-    EntryInfo          *e
-)
+    EntryInfo          *e )
 {
        int rc = 0;     /* return code */
 
+#ifdef BDB_HIER
+       e->bei_parent->bei_ckids--;
+       if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
+#endif
        /* dn tree */
-       if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
+       if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
+               == NULL )
        {
                rc = -1;
        }
 
-       /* If parent has no more kids, put in on LRU list */
-       if ( e->bei_parent->bei_kids == NULL ) {
-               LRU_ADD( cache, e->bei_parent );
-               cache->c_cursize++;
-       }
-
        /* id tree */
-       if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
-       {
+       if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
                rc = -1;
        }
 
@@ -680,7 +1005,9 @@ static void
 bdb_entryinfo_release( void *data )
 {
        EntryInfo *ei = (EntryInfo *)data;
-       avl_free( ei->bei_kids, NULL );
+       if ( ei->bei_kids ) {
+               avl_free( ei->bei_kids, NULL );
+       }
        if ( ei->bei_e ) {
                ei->bei_e->e_private = NULL;
                bdb_entry_return( ei->bei_e );
@@ -733,13 +1060,32 @@ bdb_lru_print( Cache *cache )
 #endif
 
 #ifdef BDB_REUSE_LOCKERS
-void
+static void
 bdb_locker_id_free( void *key, void *data )
 {
        DB_ENV *env = key;
        int lockid = (int) data;
+       int rc;
+
+       rc = XLOCK_ID_FREE( env, lockid );
+       if ( rc == EINVAL ) {
+               DB_LOCKREQ lr;
+#ifdef NEW_LOGGING
+               LDAP_LOG( BACK_BDB, ERR,
+                       "bdb_locker_id_free: %d err %s(%d)\n",
+                       lockid, db_strerror(rc), rc );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "bdb_locker_id_free: %d err %s(%d)\n",
+                       lockid, db_strerror(rc), rc );
+#endif
+               memset( &lr, 0, sizeof(lr) );
 
-       XLOCK_ID_FREE( env, lockid );
+               /* release all locks held by this locker. */
+               lr.op = DB_LOCK_PUT_ALL;
+               env->lock_vec( env, lockid, 0, &lr, 1, NULL );
+               XLOCK_ID_FREE( env, lockid );
+       }
 }
 
 int
@@ -793,3 +1139,25 @@ bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
        return 0;
 }
 #endif
+
+void
+bdb_cache_delete_entry(
+       struct bdb_info *bdb,
+       EntryInfo *ei,
+       u_int32_t locker,
+       DB_LOCK *lock )
+{
+       ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
+       if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 )
+       {
+               if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
+                       LRU_DELETE( &bdb->bi_cache, ei );
+                       ei->bei_e->e_private = NULL;
+                       bdb_entry_return( ei->bei_e );
+                       ei->bei_e = NULL;
+                       --bdb->bi_cache.c_cursize;
+               }
+               bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
+       }
+       ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
+}