Back to index

openldap  2.4.31
cache.c
Go to the documentation of this file.
00001 /* cache.c - routines to maintain an in-core cache of entries */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 
00021 #include <ac/errno.h>
00022 #include <ac/string.h>
00023 #include <ac/socket.h>
00024 
00025 #include "slap.h"
00026 
00027 #include "back-bdb.h"
00028 
00029 #include "ldap_rq.h"
00030 
00031 #ifdef BDB_HIER
00032 #define bdb_cache_lru_purge hdb_cache_lru_purge
00033 #endif
00034 static void bdb_cache_lru_purge( struct bdb_info *bdb );
00035 
00036 static int    bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
00037 #ifdef LDAP_DEBUG
00038 #define SLAPD_UNUSED
00039 #ifdef SLAPD_UNUSED
00040 static void   bdb_lru_print(Cache *cache);
00041 static void   bdb_idtree_print(Cache *cache);
00042 #endif
00043 #endif
00044 
00045 /* For concurrency experiments only! */
00046 #if 0
00047 #define       ldap_pvt_thread_rdwr_wlock(a)      0
00048 #define       ldap_pvt_thread_rdwr_wunlock(a)    0
00049 #define       ldap_pvt_thread_rdwr_rlock(a)      0
00050 #define       ldap_pvt_thread_rdwr_runlock(a)    0
00051 #endif
00052 
00053 #if 0
00054 #define ldap_pvt_thread_mutex_trylock(a) 0
00055 #endif
00056 
00057 static EntryInfo *
00058 bdb_cache_entryinfo_new( Cache *cache )
00059 {
00060        EntryInfo *ei = NULL;
00061 
00062        if ( cache->c_eifree ) {
00063               ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
00064               if ( cache->c_eifree ) {
00065                      ei = cache->c_eifree;
00066                      cache->c_eifree = ei->bei_lrunext;
00067                      ei->bei_finders = 0;
00068                      ei->bei_lrunext = NULL;
00069               }
00070               ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
00071        }
00072        if ( !ei ) {
00073               ei = ch_calloc(1, sizeof(EntryInfo));
00074               ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
00075        }
00076 
00077        ei->bei_state = CACHE_ENTRY_REFERENCED;
00078 
00079        return ei;
00080 }
00081 
00082 static void
00083 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
00084 {
00085        free( ei->bei_nrdn.bv_val );
00086        BER_BVZERO( &ei->bei_nrdn );
00087 #ifdef BDB_HIER
00088        free( ei->bei_rdn.bv_val );
00089        BER_BVZERO( &ei->bei_rdn );
00090        ei->bei_modrdns = 0;
00091        ei->bei_ckids = 0;
00092        ei->bei_dkids = 0;
00093 #endif
00094        ei->bei_parent = NULL;
00095        ei->bei_kids = NULL;
00096        ei->bei_lruprev = NULL;
00097 
00098 #if 0
00099        ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
00100        ei->bei_lrunext = cache->c_eifree;
00101        cache->c_eifree = ei;
00102        ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
00103 #else
00104        ldap_pvt_thread_mutex_destroy( &ei->bei_kids_mutex );
00105        ch_free( ei );
00106 #endif
00107 }
00108 
00109 #define LRU_DEL( c, e ) do { \
00110        if ( e == e->bei_lruprev ) { \
00111               (c)->c_lruhead = (c)->c_lrutail = NULL; \
00112        } else { \
00113               if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
00114               if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
00115               e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
00116               e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
00117        } \
00118        e->bei_lruprev = NULL; \
00119 } while ( 0 )
00120 
00121 /* Note - we now use a Second-Chance / Clock algorithm instead of
00122  * Least-Recently-Used. This tremendously improves concurrency
00123  * because we no longer need to manipulate the lists every time an
00124  * entry is touched. We only need to lock the lists when adding
00125  * or deleting an entry. It's now a circular doubly-linked list.
00126  * We always append to the tail, but the head traverses the circle
00127  * during a purge operation.
00128  */
00129 static void
00130 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
00131 {
00132 
00133        /* Already linked, ignore */
00134        if ( ei->bei_lruprev )
00135               return;
00136 
00137        /* Insert into circular LRU list */
00138        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
00139 
00140        ei->bei_lruprev = bdb->bi_cache.c_lrutail;
00141        if ( bdb->bi_cache.c_lrutail ) {
00142               ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
00143               bdb->bi_cache.c_lrutail->bei_lrunext = ei;
00144               if ( ei->bei_lrunext )
00145                      ei->bei_lrunext->bei_lruprev = ei;
00146        } else {
00147               ei->bei_lrunext = ei->bei_lruprev = ei;
00148               bdb->bi_cache.c_lruhead = ei;
00149        }
00150        bdb->bi_cache.c_lrutail = ei;
00151        ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
00152 }
00153 
00154 #ifdef NO_THREADS
00155 #define NO_DB_LOCK
00156 #endif
00157 
00158 /* #define NO_DB_LOCK 1 */
00159 /* Note: The BerkeleyDB locks are much slower than regular
00160  * mutexes or rdwr locks. But the BDB implementation has the
00161  * advantage of using a fixed size lock table, instead of
00162  * allocating a lock object per entry in the DB. That's a
00163  * key benefit for scaling. It also frees us from worrying
00164  * about undetectable deadlocks between BDB activity and our
00165  * own cache activity. It's still worth exploring faster
00166  * alternatives though.
00167  */
00168 
00169 /* Atomically release and reacquire a lock */
00170 int
00171 bdb_cache_entry_db_relock(
00172        struct bdb_info *bdb,
00173        DB_TXN *txn,
00174        EntryInfo *ei,
00175        int rw,
00176        int tryOnly,
00177        DB_LOCK *lock )
00178 {
00179 #ifdef NO_DB_LOCK
00180        return 0;
00181 #else
00182        int    rc;
00183        DBT    lockobj;
00184        DB_LOCKREQ list[2];
00185 
00186        if ( !lock ) return 0;
00187 
00188        DBTzero( &lockobj );
00189        lockobj.data = &ei->bei_id;
00190        lockobj.size = sizeof(ei->bei_id) + 1;
00191 
00192        list[0].op = DB_LOCK_PUT;
00193        list[0].lock = *lock;
00194        list[1].op = DB_LOCK_GET;
00195        list[1].lock = *lock;
00196        list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
00197        list[1].obj = &lockobj;
00198        rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
00199               list, 2, NULL );
00200 
00201        if (rc && !tryOnly) {
00202               Debug( LDAP_DEBUG_TRACE,
00203                      "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
00204                      ei->bei_id, rw, rc );
00205        } else {
00206               *lock = list[1].lock;
00207        }
00208        return rc;
00209 #endif
00210 }
00211 
00212 static int
00213 bdb_cache_entry_db_lock( struct bdb_info *bdb, DB_TXN *txn, EntryInfo *ei,
00214        int rw, int tryOnly, DB_LOCK *lock )
00215 {
00216 #ifdef NO_DB_LOCK
00217        return 0;
00218 #else
00219        int       rc;
00220        DBT       lockobj;
00221        int       db_rw;
00222 
00223        if ( !lock ) return 0;
00224 
00225        if (rw)
00226               db_rw = DB_LOCK_WRITE;
00227        else
00228               db_rw = DB_LOCK_READ;
00229 
00230        DBTzero( &lockobj );
00231        lockobj.data = &ei->bei_id;
00232        lockobj.size = sizeof(ei->bei_id) + 1;
00233 
00234        rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
00235                                    &lockobj, db_rw, lock);
00236        if (rc && !tryOnly) {
00237               Debug( LDAP_DEBUG_TRACE,
00238                      "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
00239                      ei->bei_id, rw, rc );
00240        }
00241        return rc;
00242 #endif /* NO_DB_LOCK */
00243 }
00244 
00245 int
00246 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
00247 {
00248 #ifdef NO_DB_LOCK
00249        return 0;
00250 #else
00251        int rc;
00252 
00253        if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
00254 
00255        rc = LOCK_PUT ( bdb->bi_dbenv, lock );
00256        return rc;
00257 #endif
00258 }
00259 
00260 void
00261 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
00262        int rw, DB_LOCK *lock )
00263 {
00264        EntryInfo *ei;
00265        int free = 0;
00266 
00267        ei = e->e_private;
00268        if ( ei && ( ei->bei_state & CACHE_ENTRY_NOT_CACHED )) {
00269               bdb_cache_entryinfo_lock( ei );
00270               if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
00271                      /* Releasing the entry can only be done when
00272                       * we know that nobody else is using it, i.e we
00273                       * should have an entry_db writelock.  But the
00274                       * flag is only set by the thread that loads the
00275                       * entry, and only if no other threads has found
00276                       * it while it was working.  All other threads
00277                       * clear the flag, which mean that we should be
00278                       * the only thread using the entry if the flag
00279                       * is set here.
00280                       */
00281                      ei->bei_e = NULL;
00282                      ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
00283                      free = 1;
00284               }
00285               bdb_cache_entryinfo_unlock( ei );
00286        }
00287        bdb_cache_entry_db_unlock( bdb, lock );
00288        if ( free ) {
00289               e->e_private = NULL;
00290               bdb_entry_return( e );
00291        }
00292 }
00293 
00294 static int
00295 bdb_cache_entryinfo_destroy( EntryInfo *e )
00296 {
00297        ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
00298        free( e->bei_nrdn.bv_val );
00299 #ifdef BDB_HIER
00300        free( e->bei_rdn.bv_val );
00301 #endif
00302        free( e );
00303        return 0;
00304 }
00305 
00306 /* Do a length-ordered sort on normalized RDNs */
00307 static int
00308 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
00309 {
00310        const EntryInfo *e1 = v_e1, *e2 = v_e2;
00311        int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
00312        if (rc == 0) {
00313               rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
00314                      e1->bei_nrdn.bv_len );
00315        }
00316        return rc;
00317 }
00318 
00319 static int
00320 bdb_id_cmp( const void *v_e1, const void *v_e2 )
00321 {
00322        const EntryInfo *e1 = v_e1, *e2 = v_e2;
00323        return e1->bei_id - e2->bei_id;
00324 }
00325 
00326 static int
00327 bdb_id_dup_err( void *v1, void *v2 )
00328 {
00329        EntryInfo *e2 = v2;
00330        e2->bei_lrunext = v1;
00331        return -1;
00332 }
00333 
00334 /* Create an entryinfo in the cache. Caller must release the locks later.
00335  */
00336 static int
00337 bdb_entryinfo_add_internal(
00338        struct bdb_info *bdb,
00339        EntryInfo *ei,
00340        EntryInfo **res )
00341 {
00342        EntryInfo *ei2 = NULL;
00343 
00344        *res = NULL;
00345 
00346        ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
00347 
00348        bdb_cache_entryinfo_lock( ei->bei_parent );
00349        ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
00350 
00351        ei2->bei_id = ei->bei_id;
00352        ei2->bei_parent = ei->bei_parent;
00353 #ifdef BDB_HIER
00354        ei2->bei_rdn = ei->bei_rdn;
00355 #endif
00356 #ifdef SLAP_ZONE_ALLOC
00357        ei2->bei_bdb = bdb;
00358 #endif
00359 
00360        /* Add to cache ID tree */
00361        if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
00362               bdb_id_dup_err )) {
00363               EntryInfo *eix = ei2->bei_lrunext;
00364               bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
00365               ei2 = eix;
00366 #ifdef BDB_HIER
00367               /* It got freed above because its value was
00368                * assigned to ei2.
00369                */
00370               ei->bei_rdn.bv_val = NULL;
00371 #endif
00372        } else {
00373               int rc;
00374 
00375               bdb->bi_cache.c_eiused++;
00376               ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
00377 
00378               /* This is a new leaf node. But if parent had no kids, then it was
00379                * a leaf and we would be decrementing that. So, only increment if
00380                * the parent already has kids.
00381                */
00382               if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
00383                      bdb->bi_cache.c_leaves++;
00384               rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
00385                      avl_dup_error );
00386 #ifdef BDB_HIER
00387               /* it's possible for hdb_cache_find_parent to beat us to it */
00388               if ( !rc ) {
00389                      ei->bei_parent->bei_ckids++;
00390               }
00391 #endif
00392        }
00393 
00394        *res = ei2;
00395        return 0;
00396 }
00397 
00398 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
00399  * the info for its closest ancestor. *res should be NULL to process a
00400  * complete DN starting from the tree root. Otherwise *res must be the
00401  * immediate parent of the requested DN, and only the RDN will be searched.
00402  * The EntryInfo is locked upon return and must be unlocked by the caller.
00403  */
00404 int
00405 bdb_cache_find_ndn(
00406        Operation     *op,
00407        DB_TXN        *txn,
00408        struct berval *ndn,
00409        EntryInfo     **res )
00410 {
00411        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
00412        EntryInfo     ei, *eip, *ei2;
00413        int rc = 0;
00414        char *ptr;
00415 
00416        /* this function is always called with normalized DN */
00417        if ( *res ) {
00418               /* we're doing a onelevel search for an RDN */
00419               ei.bei_nrdn.bv_val = ndn->bv_val;
00420               ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
00421               eip = *res;
00422        } else {
00423               /* we're searching a full DN from the root */
00424               ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
00425               ei.bei_nrdn.bv_val = ptr;
00426               ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
00427               /* Skip to next rdn if suffix is empty */
00428               if ( ei.bei_nrdn.bv_len == 0 ) {
00429                      for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
00430                             && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
00431                      if ( ptr >= ndn->bv_val ) {
00432                             if (DN_SEPARATOR(*ptr)) ptr++;
00433                             ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
00434                             ei.bei_nrdn.bv_val = ptr;
00435                      }
00436               }
00437               eip = &bdb->bi_cache.c_dntree;
00438        }
00439        
00440        for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
00441               eip->bei_state |= CACHE_ENTRY_REFERENCED;
00442               ei.bei_parent = eip;
00443               ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
00444               if ( !ei2 ) {
00445                      DBC *cursor;
00446                      int len = ei.bei_nrdn.bv_len;
00447                             
00448                      if ( BER_BVISEMPTY( ndn )) {
00449                             *res = eip;
00450                             return LDAP_SUCCESS;
00451                      }
00452 
00453                      ei.bei_nrdn.bv_len = ndn->bv_len -
00454                             (ei.bei_nrdn.bv_val - ndn->bv_val);
00455                      eip->bei_finders++;
00456                      bdb_cache_entryinfo_unlock( eip );
00457 
00458                      BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
00459                             ei.bei_nrdn.bv_val );
00460 
00461                      cursor = NULL;
00462                      rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &cursor );
00463                      if (rc) {
00464                             bdb_cache_entryinfo_lock( eip );
00465                             eip->bei_finders--;
00466                             if ( cursor ) cursor->c_close( cursor );
00467                             *res = eip;
00468                             return rc;
00469                      }
00470 
00471                      BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
00472                             ei.bei_nrdn.bv_val, ei.bei_id );
00473 
00474                      /* DN exists but needs to be added to cache */
00475                      ei.bei_nrdn.bv_len = len;
00476                      rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
00477                      /* add_internal left eip and c_rwlock locked */
00478                      eip->bei_finders--;
00479                      ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00480                      if ( cursor ) cursor->c_close( cursor );
00481                      if ( rc ) {
00482                             *res = eip;
00483                             return rc;
00484                      }
00485               }
00486               bdb_cache_entryinfo_lock( ei2 );
00487               if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
00488                      /* In the midst of deleting? Give it a chance to
00489                       * complete.
00490                       */
00491                      bdb_cache_entryinfo_unlock( ei2 );
00492                      bdb_cache_entryinfo_unlock( eip );
00493                      ldap_pvt_thread_yield();
00494                      bdb_cache_entryinfo_lock( eip );
00495                      *res = eip;
00496                      return DB_NOTFOUND;
00497               }
00498               bdb_cache_entryinfo_unlock( eip );
00499 
00500               eip = ei2;
00501 
00502               /* Advance to next lower RDN */
00503               for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
00504                      && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
00505               if ( ptr >= ndn->bv_val ) {
00506                      if (DN_SEPARATOR(*ptr)) ptr++;
00507                      ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
00508                      ei.bei_nrdn.bv_val = ptr;
00509               }
00510               if ( ptr < ndn->bv_val ) {
00511                      *res = eip;
00512                      break;
00513               }
00514        }
00515 
00516        return rc;
00517 }
00518 
00519 #ifdef BDB_HIER
00520 /* Walk up the tree from a child node, looking for an ID that's already
00521  * been linked into the cache.
00522  */
00523 int
00524 hdb_cache_find_parent(
00525        Operation *op,
00526        DB_TXN *txn,
00527        ID id,
00528        EntryInfo **res )
00529 {
00530        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
00531        EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
00532        int rc, add;
00533 
00534        ei.bei_id = id;
00535        ei.bei_kids = NULL;
00536        ei.bei_ckids = 0;
00537 
00538        for (;;) {
00539               rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
00540               if ( rc ) break;
00541 
00542               /* Save the previous node, if any */
00543               ei2 = ein;
00544 
00545               /* Create a new node for the current ID */
00546               ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
00547               ein->bei_id = ei.bei_id;
00548               ein->bei_kids = ei.bei_kids;
00549               ein->bei_nrdn = ei.bei_nrdn;
00550               ein->bei_rdn = ei.bei_rdn;
00551               ein->bei_ckids = ei.bei_ckids;
00552 #ifdef SLAP_ZONE_ALLOC
00553               ein->bei_bdb = bdb;
00554 #endif
00555               ei.bei_ckids = 0;
00556               add = 1;
00557               
00558               /* This node is not fully connected yet */
00559               ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
00560 
00561               /* If this is the first time, save this node
00562                * to be returned later.
00563                */
00564               if ( eir == NULL ) {
00565                      eir = ein;
00566                      ein->bei_finders++;
00567               }
00568 
00569 again:
00570               /* Insert this node into the ID tree */
00571               ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
00572               if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
00573                      bdb_id_cmp, bdb_id_dup_err ) ) {
00574                      EntryInfo *eix = ein->bei_lrunext;
00575 
00576                      if ( bdb_cache_entryinfo_trylock( eix )) {
00577                             ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00578                             ldap_pvt_thread_yield();
00579                             goto again;
00580                      }
00581                      ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00582 
00583                      /* Someone else created this node just before us.
00584                       * Free our new copy and use the existing one.
00585                       */
00586                      bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
00587 
00588                      /* if it was the node we were looking for, just return it */
00589                      if ( eir == ein ) {
00590                             *res = eix;
00591                             rc = 0;
00592                             break;
00593                      }
00594 
00595                      ein = ei2;
00596                      ei2 = eix;
00597                      add = 0;
00598 
00599                      /* otherwise, link up what we have and return */
00600                      goto gotparent;
00601               }
00602 
00603               /* If there was a previous node, link it to this one */
00604               if ( ei2 ) ei2->bei_parent = ein;
00605 
00606               /* Look for this node's parent */
00607 par2:
00608               if ( eip.bei_id ) {
00609                      ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
00610                                    (caddr_t) &eip, bdb_id_cmp );
00611               } else {
00612                      ei2 = &bdb->bi_cache.c_dntree;
00613               }
00614               if ( ei2 && bdb_cache_entryinfo_trylock( ei2 )) {
00615                      ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00616                      ldap_pvt_thread_yield();
00617                      ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
00618                      goto par2;
00619               }
00620               if ( add )
00621                      bdb->bi_cache.c_eiused++;
00622               if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
00623                      bdb->bi_cache.c_leaves++;
00624               ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00625 
00626 gotparent:
00627               /* Got the parent, link in and we're done. */
00628               if ( ei2 ) {
00629                      bdb_cache_entryinfo_lock( eir );
00630                      ein->bei_parent = ei2;
00631 
00632                      if ( avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
00633                             avl_dup_error) == 0 )
00634                             ei2->bei_ckids++;
00635 
00636                      /* Reset all the state info */
00637                      for (ein = eir; ein != ei2; ein=ein->bei_parent)
00638                             ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
00639 
00640                      bdb_cache_entryinfo_unlock( ei2 );
00641                      eir->bei_finders--;
00642 
00643                      *res = eir;
00644                      break;
00645               }
00646               ei.bei_kids = NULL;
00647               ei.bei_id = eip.bei_id;
00648               ei.bei_ckids = 1;
00649               avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
00650                      avl_dup_error );
00651        }
00652        return rc;
00653 }
00654 
00655 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
00656  * of a given node
00657  */
00658 int hdb_cache_load(
00659        struct bdb_info *bdb,
00660        EntryInfo *ei,
00661        EntryInfo **res )
00662 {
00663        EntryInfo *ei2;
00664        int rc;
00665 
00666        /* See if we already have this one */
00667        bdb_cache_entryinfo_lock( ei->bei_parent );
00668        ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
00669        bdb_cache_entryinfo_unlock( ei->bei_parent );
00670 
00671        if ( !ei2 ) {
00672               /* Not found, add it */
00673               struct berval bv;
00674 
00675               /* bei_rdn was not malloc'd before, do it now */
00676               ber_dupbv( &bv, &ei->bei_rdn );
00677               ei->bei_rdn = bv;
00678 
00679               rc = bdb_entryinfo_add_internal( bdb, ei, res );
00680               bdb_cache_entryinfo_unlock( ei->bei_parent );
00681               ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
00682        } else {
00683               /* Found, return it */
00684               *res = ei2;
00685               return 0;
00686        }
00687        return rc;
00688 }
00689 #endif
00690 
00691 /* This is best-effort only. If all entries in the cache are
00692  * busy, they will all be kept. This is unlikely to happen
00693  * unless the cache is very much smaller than the working set.
00694  */
00695 static void
00696 bdb_cache_lru_purge( struct bdb_info *bdb )
00697 {
00698        DB_LOCK              lock, *lockp;
00699        EntryInfo *elru, *elnext = NULL;
00700        int islocked;
00701        ID eicount, ecount;
00702        ID count, efree, eifree = 0;
00703 #ifdef LDAP_DEBUG
00704        int iter;
00705 #endif
00706 
00707        /* Wait for the mutex; we're the only one trying to purge. */
00708        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
00709 
00710        if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
00711               efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
00712               efree += bdb->bi_cache.c_minfree;
00713        } else {
00714               efree = 0;
00715        }
00716 
00717        /* maximum number of EntryInfo leaves to cache. In slapcat
00718         * we always free all leaf nodes.
00719         */
00720 
00721        if ( slapMode & SLAP_TOOL_READONLY ) {
00722               eifree = bdb->bi_cache.c_leaves;
00723        } else if ( bdb->bi_cache.c_eimax &&
00724               bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
00725               eifree = bdb->bi_cache.c_minfree * 10;
00726               if ( eifree >= bdb->bi_cache.c_leaves )
00727                      eifree /= 2;
00728        }
00729 
00730        if ( !efree && !eifree ) {
00731               ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
00732               bdb->bi_cache.c_purging = 0;
00733               return;
00734        }
00735 
00736        if ( bdb->bi_cache.c_txn ) {
00737               lockp = &lock;
00738        } else {
00739               lockp = NULL;
00740        }
00741 
00742        count = 0;
00743        eicount = 0;
00744        ecount = 0;
00745 #ifdef LDAP_DEBUG
00746        iter = 0;
00747 #endif
00748 
00749        /* Look for an unused entry to remove */
00750        for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
00751               elnext = elru->bei_lrunext;
00752 
00753               if ( bdb_cache_entryinfo_trylock( elru ))
00754                      goto bottom;
00755 
00756               /* This flag implements the clock replacement behavior */
00757               if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
00758                      elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
00759                      bdb_cache_entryinfo_unlock( elru );
00760                      goto bottom;
00761               }
00762 
00763               /* If this node is in the process of linking into the cache,
00764                * or this node is being deleted, skip it.
00765                */
00766               if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
00767                      CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING |
00768                      CACHE_ENTRY_ONELEVEL )) ||
00769                      elru->bei_finders > 0 ) {
00770                      bdb_cache_entryinfo_unlock( elru );
00771                      goto bottom;
00772               }
00773 
00774               if ( bdb_cache_entryinfo_trylock( elru->bei_parent )) {
00775                      bdb_cache_entryinfo_unlock( elru );
00776                      goto bottom;
00777               }
00778 
00779               /* entryinfo is locked */
00780               islocked = 1;
00781 
00782               /* If we can successfully writelock it, then
00783                * the object is idle.
00784                */
00785               if ( bdb_cache_entry_db_lock( bdb,
00786                      bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
00787 
00788                      /* Free entry for this node if it's present */
00789                      if ( elru->bei_e ) {
00790                             ecount++;
00791 
00792                             /* the cache may have gone over the limit while we
00793                              * weren't looking, so double check.
00794                              */
00795                             if ( !efree && ecount > bdb->bi_cache.c_maxsize )
00796                                    efree = bdb->bi_cache.c_minfree;
00797 
00798                             if ( count < efree ) {
00799                                    elru->bei_e->e_private = NULL;
00800 #ifdef SLAP_ZONE_ALLOC
00801                                    bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
00802 #else
00803                                    bdb_entry_return( elru->bei_e );
00804 #endif
00805                                    elru->bei_e = NULL;
00806                                    count++;
00807                             } else {
00808                                    /* Keep this node cached, skip to next */
00809                                    bdb_cache_entry_db_unlock( bdb, lockp );
00810                                    goto next;
00811                             }
00812                      }
00813                      bdb_cache_entry_db_unlock( bdb, lockp );
00814 
00815                      /* 
00816                       * If it is a leaf node, and we're over the limit, free it.
00817                       */
00818                      if ( elru->bei_kids ) {
00819                             /* Drop from list, we ignore it... */
00820                             LRU_DEL( &bdb->bi_cache, elru );
00821                      } else if ( eicount < eifree ) {
00822                             /* Too many leaf nodes, free this one */
00823                             bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
00824                             bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
00825                             islocked = 0;
00826                             eicount++;
00827                      }      /* Leave on list until we need to free it */
00828               }
00829 
00830 next:
00831               if ( islocked ) {
00832                      bdb_cache_entryinfo_unlock( elru );
00833                      bdb_cache_entryinfo_unlock( elru->bei_parent );
00834               }
00835 
00836               if ( count >= efree && eicount >= eifree )
00837                      break;
00838 bottom:
00839               if ( elnext == bdb->bi_cache.c_lruhead )
00840                      break;
00841 #ifdef LDAP_DEBUG
00842               iter++;
00843 #endif
00844        }
00845 
00846        if ( count || ecount > bdb->bi_cache.c_cursize ) {
00847               ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
00848               /* HACK: we seem to be losing track, fix up now */
00849               if ( ecount > bdb->bi_cache.c_cursize )
00850                      bdb->bi_cache.c_cursize = ecount;
00851               bdb->bi_cache.c_cursize -= count;
00852               ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
00853        }
00854        bdb->bi_cache.c_lruhead = elnext;
00855        ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
00856        bdb->bi_cache.c_purging = 0;
00857 }
00858 
00859 /*
00860  * cache_find_id - find an entry in the cache, given id.
00861  * The entry is locked for Read upon return. Call with flag ID_LOCKED if
00862  * the supplied *eip was already locked.
00863  */
00864 
00865 int
00866 bdb_cache_find_id(
00867        Operation *op,
00868        DB_TXN *tid,
00869        ID                          id,
00870        EntryInfo     **eip,
00871        int           flag,
00872        DB_LOCK              *lock )
00873 {
00874        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
00875        Entry  *ep = NULL;
00876        int    rc = 0, load = 0;
00877        EntryInfo ei = { 0 };
00878 
00879        ei.bei_id = id;
00880 
00881 #ifdef SLAP_ZONE_ALLOC
00882        slap_zh_rlock(bdb->bi_cache.c_zctx);
00883 #endif
00884        /* If we weren't given any info, see if we have it already cached */
00885        if ( !*eip ) {
00886 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
00887               *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
00888                      (caddr_t) &ei, bdb_id_cmp );
00889               if ( *eip ) {
00890                      /* If the lock attempt fails, the info is in use */
00891                      if ( bdb_cache_entryinfo_trylock( *eip )) {
00892                             int del = (*eip)->bei_state & CACHE_ENTRY_DELETED;
00893                             ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
00894                             /* If this node is being deleted, treat
00895                              * as if the delete has already finished
00896                              */
00897                             if ( del ) {
00898                                    return DB_NOTFOUND;
00899                             }
00900                             /* otherwise, wait for the info to free up */
00901                             ldap_pvt_thread_yield();
00902                             goto again;
00903                      }
00904                      /* If this info isn't hooked up to its parent yet,
00905                       * unlock and wait for it to be fully initialized
00906                       */
00907                      if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
00908                             bdb_cache_entryinfo_unlock( *eip );
00909                             ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
00910                             ldap_pvt_thread_yield();
00911                             goto again;
00912                      }
00913                      flag |= ID_LOCKED;
00914               }
00915               ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
00916        }
00917 
00918        /* See if the ID exists in the database; add it to the cache if so */
00919        if ( !*eip ) {
00920 #ifndef BDB_HIER
00921               rc = bdb_id2entry( op->o_bd, tid, id, &ep );
00922               if ( rc == 0 ) {
00923                      rc = bdb_cache_find_ndn( op, tid,
00924                             &ep->e_nname, eip );
00925                      if ( *eip ) flag |= ID_LOCKED;
00926                      if ( rc ) {
00927                             ep->e_private = NULL;
00928 #ifdef SLAP_ZONE_ALLOC
00929                             bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
00930 #else
00931                             bdb_entry_return( ep );
00932 #endif
00933                             ep = NULL;
00934                      }
00935               }
00936 #else
00937               rc = hdb_cache_find_parent(op, tid, id, eip );
00938               if ( rc == 0 ) flag |= ID_LOCKED;
00939 #endif
00940        }
00941 
00942        /* Ok, we found the info, do we have the entry? */
00943        if ( rc == 0 ) {
00944               if ( !( flag & ID_LOCKED )) {
00945                      bdb_cache_entryinfo_lock( *eip );
00946                      flag |= ID_LOCKED;
00947               }
00948 
00949               if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
00950                      rc = DB_NOTFOUND;
00951               } else {
00952                      (*eip)->bei_finders++;
00953                      (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
00954                      if ( flag & ID_NOENTRY ) {
00955                             bdb_cache_entryinfo_unlock( *eip );
00956                             return 0;
00957                      }
00958                      /* Make sure only one thread tries to load the entry */
00959 load1:
00960 #ifdef SLAP_ZONE_ALLOC
00961                      if ((*eip)->bei_e && !slap_zn_validate(
00962                                    bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
00963                             (*eip)->bei_e = NULL;
00964                             (*eip)->bei_zseq = 0;
00965                      }
00966 #endif
00967                      if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
00968                             load = 1;
00969                             (*eip)->bei_state |= CACHE_ENTRY_LOADING;
00970                             flag |= ID_CHKPURGE;
00971                      }
00972 
00973                      if ( !load ) {
00974                             /* Clear the uncached state if we are not
00975                              * loading it, i.e it is already cached or
00976                              * another thread is currently loading it.
00977                              */
00978                             if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
00979                                    (*eip)->bei_state ^= CACHE_ENTRY_NOT_CACHED;
00980                                    flag |= ID_CHKPURGE;
00981                             }
00982                      }
00983 
00984                      if ( flag & ID_LOCKED ) {
00985                             bdb_cache_entryinfo_unlock( *eip );
00986                             flag ^= ID_LOCKED;
00987                      }
00988                      rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
00989                      if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
00990                             rc = DB_NOTFOUND;
00991                             bdb_cache_entry_db_unlock( bdb, lock );
00992                             bdb_cache_entryinfo_lock( *eip );
00993                             (*eip)->bei_finders--;
00994                             bdb_cache_entryinfo_unlock( *eip );
00995                      } else if ( rc == 0 ) {
00996                             if ( load ) {
00997                                    if ( !ep) {
00998                                           rc = bdb_id2entry( op->o_bd, tid, id, &ep );
00999                                    }
01000                                    if ( rc == 0 ) {
01001                                           ep->e_private = *eip;
01002 #ifdef BDB_HIER
01003                                           while ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED )
01004                                                  ldap_pvt_thread_yield();
01005                                           bdb_fix_dn( ep, 0 );
01006 #endif
01007                                           bdb_cache_entryinfo_lock( *eip );
01008 
01009                                           (*eip)->bei_e = ep;
01010 #ifdef SLAP_ZONE_ALLOC
01011                                           (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
01012 #endif
01013                                           ep = NULL;
01014                                           if ( flag & ID_NOCACHE ) {
01015                                                  /* Set the cached state only if no other thread
01016                                                   * found the info while we were loading the entry.
01017                                                   */
01018                                                  if ( (*eip)->bei_finders == 1 ) {
01019                                                         (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
01020                                                         flag ^= ID_CHKPURGE;
01021                                                  }
01022                                           }
01023                                           bdb_cache_entryinfo_unlock( *eip );
01024                                           bdb_cache_lru_link( bdb, *eip );
01025                                    }
01026                                    if ( rc == 0 ) {
01027                                           /* If we succeeded, downgrade back to a readlock. */
01028                                           rc = bdb_cache_entry_db_relock( bdb, tid,
01029                                                  *eip, 0, 0, lock );
01030                                    } else {
01031                                           /* Otherwise, release the lock. */
01032                                           bdb_cache_entry_db_unlock( bdb, lock );
01033                                    }
01034                             } else if ( !(*eip)->bei_e ) {
01035                                    /* Some other thread is trying to load the entry,
01036                                     * wait for it to finish.
01037                                     */
01038                                    bdb_cache_entry_db_unlock( bdb, lock );
01039                                    bdb_cache_entryinfo_lock( *eip );
01040                                    flag |= ID_LOCKED;
01041                                    goto load1;
01042 #ifdef BDB_HIER
01043                             } else {
01044                                    /* Check for subtree renames
01045                                     */
01046                                    rc = bdb_fix_dn( (*eip)->bei_e, 1 );
01047                                    if ( rc ) {
01048                                           bdb_cache_entry_db_relock( bdb,
01049                                                  tid, *eip, 1, 0, lock );
01050                                           /* check again in case other modifier did it already */
01051                                           if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
01052                                                  rc = bdb_fix_dn( (*eip)->bei_e, 2 );
01053                                           bdb_cache_entry_db_relock( bdb,
01054                                                  tid, *eip, 0, 0, lock );
01055                                    }
01056 #endif
01057                             }
01058                             bdb_cache_entryinfo_lock( *eip );
01059                             (*eip)->bei_finders--;
01060                             if ( load )
01061                                    (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
01062                             bdb_cache_entryinfo_unlock( *eip );
01063                      }
01064               }
01065        }
01066        if ( flag & ID_LOCKED ) {
01067               bdb_cache_entryinfo_unlock( *eip );
01068        }
01069        if ( ep ) {
01070               ep->e_private = NULL;
01071 #ifdef SLAP_ZONE_ALLOC
01072               bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
01073 #else
01074               bdb_entry_return( ep );
01075 #endif
01076        }
01077        if ( rc == 0 ) {
01078               int purge = 0;
01079 
01080               if (( flag & ID_CHKPURGE ) || bdb->bi_cache.c_eimax ) {
01081                      ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
01082                      if ( flag & ID_CHKPURGE ) {
01083                             bdb->bi_cache.c_cursize++;
01084                             if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
01085                                    purge = 1;
01086                                    bdb->bi_cache.c_purging = 1;
01087                             }
01088                      } else if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_eimax && bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
01089                             purge = 1;
01090                             bdb->bi_cache.c_purging = 1;
01091                      }
01092                      ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
01093               }
01094               if ( purge )
01095                      bdb_cache_lru_purge( bdb );
01096        }
01097 
01098 #ifdef SLAP_ZONE_ALLOC
01099        if (rc == 0 && (*eip)->bei_e) {
01100               slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
01101        }
01102        slap_zh_runlock(bdb->bi_cache.c_zctx);
01103 #endif
01104        return rc;
01105 }
01106 
01107 int
01108 bdb_cache_children(
01109        Operation *op,
01110        DB_TXN *txn,
01111        Entry *e )
01112 {
01113        int rc;
01114 
01115        if ( BEI(e)->bei_kids ) {
01116               return 0;
01117        }
01118        if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
01119               return DB_NOTFOUND;
01120        }
01121        rc = bdb_dn2id_children( op, txn, e );
01122        if ( rc == DB_NOTFOUND ) {
01123               BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
01124        }
01125        return rc;
01126 }
01127 
01128 /* Update the cache after a successful database Add. */
01129 int
01130 bdb_cache_add(
01131        struct bdb_info *bdb,
01132        EntryInfo *eip,
01133        Entry *e,
01134        struct berval *nrdn,
01135        DB_TXN *txn,
01136        DB_LOCK *lock )
01137 {
01138        EntryInfo *new, ei;
01139        int rc, purge = 0;
01140 #ifdef BDB_HIER
01141        struct berval rdn = e->e_name;
01142 #endif
01143 
01144        ei.bei_id = e->e_id;
01145        ei.bei_parent = eip;
01146        ei.bei_nrdn = *nrdn;
01147        ei.bei_lockpad = 0;
01148 
01149 #if 0
01150        /* Lock this entry so that bdb_add can run to completion.
01151         * It can only fail if BDB has run out of lock resources.
01152         */
01153        rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
01154        if ( rc ) {
01155               bdb_cache_entryinfo_unlock( eip );
01156               return rc;
01157        }
01158 #endif
01159 
01160 #ifdef BDB_HIER
01161        if ( nrdn->bv_len != e->e_nname.bv_len ) {
01162               char *ptr = ber_bvchr( &rdn, ',' );
01163               assert( ptr != NULL );
01164               rdn.bv_len = ptr - rdn.bv_val;
01165        }
01166        ber_dupbv( &ei.bei_rdn, &rdn );
01167        if ( eip->bei_dkids ) eip->bei_dkids++;
01168 #endif
01169 
01170        if (eip->bei_parent) {
01171               bdb_cache_entryinfo_lock( eip->bei_parent );
01172               eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
01173               bdb_cache_entryinfo_unlock( eip->bei_parent );
01174        }
01175 
01176        rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
01177        /* bdb_csn_commit can cause this when adding the database root entry */
01178        if ( new->bei_e ) {
01179               new->bei_e->e_private = NULL;
01180 #ifdef SLAP_ZONE_ALLOC
01181               bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
01182 #else
01183               bdb_entry_return( new->bei_e );
01184 #endif
01185        }
01186        new->bei_e = e;
01187        e->e_private = new;
01188        new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
01189        eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
01190        bdb_cache_entryinfo_unlock( eip );
01191 
01192        ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
01193        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
01194        ++bdb->bi_cache.c_cursize;
01195        if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
01196               !bdb->bi_cache.c_purging ) {
01197               purge = 1;
01198               bdb->bi_cache.c_purging = 1;
01199        }
01200        ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
01201 
01202        new->bei_finders = 1;
01203        bdb_cache_lru_link( bdb, new );
01204 
01205        if ( purge )
01206               bdb_cache_lru_purge( bdb );
01207 
01208        return rc;
01209 }
01210 
01211 void bdb_cache_deref(
01212        EntryInfo *ei
01213        )
01214 {
01215        bdb_cache_entryinfo_lock( ei );
01216        ei->bei_finders--;
01217        bdb_cache_entryinfo_unlock( ei );
01218 }
01219 
01220 int
01221 bdb_cache_modify(
01222        struct bdb_info *bdb,
01223        Entry *e,
01224        Attribute *newAttrs,
01225        DB_TXN *txn,
01226        DB_LOCK *lock )
01227 {
01228        EntryInfo *ei = BEI(e);
01229        int rc;
01230        /* Get write lock on data */
01231        rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
01232 
01233        /* If we've done repeated mods on a cached entry, then e_attrs
01234         * is no longer contiguous with the entry, and must be freed.
01235         */
01236        if ( ! rc ) {
01237               if ( (void *)e->e_attrs != (void *)(e+1) ) {
01238                      attrs_free( e->e_attrs ); 
01239               }
01240               e->e_attrs = newAttrs;
01241        }
01242        return rc;
01243 }
01244 
01245 /*
01246  * Change the rdn in the entryinfo. Also move to a new parent if needed.
01247  */
01248 int
01249 bdb_cache_modrdn(
01250        struct bdb_info *bdb,
01251        Entry *e,
01252        struct berval *nrdn,
01253        Entry *new,
01254        EntryInfo *ein,
01255        DB_TXN *txn,
01256        DB_LOCK *lock )
01257 {
01258        EntryInfo *ei = BEI(e), *pei;
01259        int rc;
01260 #ifdef BDB_HIER
01261        struct berval rdn;
01262 #endif
01263 
01264        /* Get write lock on data */
01265        rc =  bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
01266        if ( rc ) return rc;
01267 
01268        /* If we've done repeated mods on a cached entry, then e_attrs
01269         * is no longer contiguous with the entry, and must be freed.
01270         */
01271        if ( (void *)e->e_attrs != (void *)(e+1) ) {
01272               attrs_free( e->e_attrs );
01273        }
01274        e->e_attrs = new->e_attrs;
01275        if( e->e_nname.bv_val < e->e_bv.bv_val ||
01276               e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
01277        {
01278               ch_free(e->e_name.bv_val);
01279               ch_free(e->e_nname.bv_val);
01280        }
01281        e->e_name = new->e_name;
01282        e->e_nname = new->e_nname;
01283 
01284        /* Lock the parent's kids AVL tree */
01285        pei = ei->bei_parent;
01286        bdb_cache_entryinfo_lock( pei );
01287        avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
01288        free( ei->bei_nrdn.bv_val );
01289        ber_dupbv( &ei->bei_nrdn, nrdn );
01290 
01291 #ifdef BDB_HIER
01292        free( ei->bei_rdn.bv_val );
01293 
01294        rdn = e->e_name;
01295        if ( nrdn->bv_len != e->e_nname.bv_len ) {
01296               char *ptr = ber_bvchr(&rdn, ',');
01297               assert( ptr != NULL );
01298               rdn.bv_len = ptr - rdn.bv_val;
01299        }
01300        ber_dupbv( &ei->bei_rdn, &rdn );
01301 
01302        /* If new parent, decrement kid counts */
01303        if ( ein ) {
01304               pei->bei_ckids--;
01305               if ( pei->bei_dkids ) {
01306                      pei->bei_dkids--;
01307                      if ( pei->bei_dkids < 2 )
01308                             pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
01309               }
01310        }
01311 #endif
01312 
01313        if (!ein) {
01314               ein = ei->bei_parent;
01315        } else {
01316               ei->bei_parent = ein;
01317               bdb_cache_entryinfo_unlock( pei );
01318               bdb_cache_entryinfo_lock( ein );
01319 
01320               /* new parent now has kids */
01321               if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
01322                      ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
01323               /* grandparent has grandkids */
01324               if ( ein->bei_parent )
01325                      ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
01326 #ifdef BDB_HIER
01327               /* parent might now have grandkids */
01328               if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
01329                      !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
01330                      ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
01331 
01332               ein->bei_ckids++;
01333               if ( ein->bei_dkids ) ein->bei_dkids++;
01334 #endif
01335        }
01336 
01337 #ifdef BDB_HIER
01338        /* Record the generation number of this change */
01339        ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
01340        bdb->bi_modrdns++;
01341        ei->bei_modrdns = bdb->bi_modrdns;
01342        ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
01343 #endif
01344 
01345        avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
01346        bdb_cache_entryinfo_unlock( ein );
01347        return rc;
01348 }
01349 /*
01350  * cache_delete - delete the entry e from the cache. 
01351  *
01352  * returns:   0      e was deleted ok
01353  *            1      e was not in the cache
01354  *            -1     something bad happened
01355  */
01356 int
01357 bdb_cache_delete(
01358        struct bdb_info *bdb,
01359     Entry            *e,
01360     DB_TXN *txn,
01361     DB_LOCK   *lock )
01362 {
01363        EntryInfo *ei = BEI(e);
01364        int    rc, busy = 0;
01365 
01366        assert( e->e_private != NULL );
01367 
01368        /* Lock the entry's info */
01369        bdb_cache_entryinfo_lock( ei );
01370 
01371        /* Set this early, warn off any queriers */
01372        ei->bei_state |= CACHE_ENTRY_DELETED;
01373 
01374        if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
01375               CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
01376               ei->bei_finders > 0 )
01377               busy = 1;
01378 
01379        bdb_cache_entryinfo_unlock( ei );
01380 
01381        while ( busy ) {
01382               ldap_pvt_thread_yield();
01383               busy = 0;
01384               bdb_cache_entryinfo_lock( ei );
01385               if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
01386                      CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
01387                      ei->bei_finders > 0 )
01388                      busy = 1;
01389               bdb_cache_entryinfo_unlock( ei );
01390        }
01391 
01392        /* Get write lock on the data */
01393        rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
01394        if ( rc ) {
01395               bdb_cache_entryinfo_lock( ei );
01396               /* couldn't lock, undo and give up */
01397               ei->bei_state ^= CACHE_ENTRY_DELETED;
01398               bdb_cache_entryinfo_unlock( ei );
01399               return rc;
01400        }
01401 
01402        Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
01403               e->e_id, 0, 0 );
01404 
01405        /* set lru mutex */
01406        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
01407 
01408        bdb_cache_entryinfo_lock( ei->bei_parent );
01409        bdb_cache_entryinfo_lock( ei );
01410        rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
01411        bdb_cache_entryinfo_unlock( ei );
01412 
01413        /* free lru mutex */
01414        ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
01415 
01416        return( rc );
01417 }
01418 
01419 void
01420 bdb_cache_delete_cleanup(
01421        Cache *cache,
01422        EntryInfo *ei )
01423 {
01424        /* Enter with ei locked */
01425 
01426        /* already freed? */
01427        if ( !ei->bei_parent ) return;
01428 
01429        if ( ei->bei_e ) {
01430               ei->bei_e->e_private = NULL;
01431 #ifdef SLAP_ZONE_ALLOC
01432               bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
01433 #else
01434               bdb_entry_return( ei->bei_e );
01435 #endif
01436               ei->bei_e = NULL;
01437        }
01438 
01439        bdb_cache_entryinfo_unlock( ei );
01440        bdb_cache_entryinfo_free( cache, ei );
01441 }
01442 
01443 static int
01444 bdb_cache_delete_internal(
01445     Cache     *cache,
01446     EntryInfo        *e,
01447     int              decr )
01448 {
01449        int rc = 0;   /* return code */
01450        int decr_leaf = 0;
01451 
01452        /* already freed? */
01453        if ( !e->bei_parent ) {
01454               assert(0);
01455               return -1;
01456        }
01457 
01458 #ifdef BDB_HIER
01459        e->bei_parent->bei_ckids--;
01460        if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
01461 #endif
01462        /* dn tree */
01463        if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
01464               == NULL )
01465        {
01466               rc = -1;
01467               assert(0);
01468        }
01469        if ( e->bei_parent->bei_kids )
01470               decr_leaf = 1;
01471 
01472        ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
01473        /* id tree */
01474        if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
01475               cache->c_eiused--;
01476               if ( decr_leaf )
01477                      cache->c_leaves--;
01478        } else {
01479               rc = -1;
01480               assert(0);
01481        }
01482        ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
01483        bdb_cache_entryinfo_unlock( e->bei_parent );
01484 
01485        if ( rc == 0 ){
01486               /* lru */
01487               LRU_DEL( cache, e );
01488 
01489               if ( e->bei_e ) {
01490                      ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
01491                      cache->c_cursize--;
01492                      ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
01493               }
01494        }
01495 
01496        return( rc );
01497 }
01498 
01499 static void
01500 bdb_entryinfo_release( void *data )
01501 {
01502        EntryInfo *ei = (EntryInfo *)data;
01503        if ( ei->bei_kids ) {
01504               avl_free( ei->bei_kids, NULL );
01505        }
01506        if ( ei->bei_e ) {
01507               ei->bei_e->e_private = NULL;
01508 #ifdef SLAP_ZONE_ALLOC
01509               bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
01510 #else
01511               bdb_entry_return( ei->bei_e );
01512 #endif
01513        }
01514        bdb_cache_entryinfo_destroy( ei );
01515 }
01516 
01517 void
01518 bdb_cache_release_all( Cache *cache )
01519 {
01520        /* set cache write lock */
01521        ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
01522        /* set lru mutex */
01523        ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
01524 
01525        Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
01526 
01527        avl_free( cache->c_dntree.bei_kids, NULL );
01528        avl_free( cache->c_idtree, bdb_entryinfo_release );
01529        for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
01530               cache->c_lruhead = cache->c_eifree->bei_lrunext;
01531               bdb_cache_entryinfo_destroy(cache->c_eifree);
01532        }
01533        cache->c_cursize = 0;
01534        cache->c_eiused = 0;
01535        cache->c_leaves = 0;
01536        cache->c_idtree = NULL;
01537        cache->c_lruhead = NULL;
01538        cache->c_lrutail = NULL;
01539        cache->c_dntree.bei_kids = NULL;
01540 
01541        /* free lru mutex */
01542        ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
01543        /* free cache write lock */
01544        ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
01545 }
01546 
01547 #ifdef LDAP_DEBUG
01548 static void
01549 bdb_lru_count( Cache *cache )
01550 {
01551        EntryInfo     *e;
01552        int ei = 0, ent = 0, nc = 0;
01553 
01554        for ( e = cache->c_lrutail; ; ) {
01555               ei++;
01556               if ( e->bei_e ) {
01557                      ent++;
01558                      if ( e->bei_state & CACHE_ENTRY_NOT_CACHED )
01559                             nc++;
01560                      fprintf( stderr, "ei %d entry %p dn %s\n", ei, (void *) e->bei_e, e->bei_e->e_name.bv_val );
01561               }
01562               e = e->bei_lrunext;
01563               if ( e == cache->c_lrutail )
01564                      break;
01565        }
01566        fprintf( stderr, "counted %d entryInfos and %d entries, %d notcached\n",
01567               ei, ent, nc );
01568        ei = 0;
01569        for ( e = cache->c_lrutail; ; ) {
01570               ei++;
01571               e = e->bei_lruprev;
01572               if ( e == cache->c_lrutail )
01573                      break;
01574        }
01575        fprintf( stderr, "counted %d entryInfos (on lruprev)\n", ei );
01576 }
01577 
01578 #ifdef SLAPD_UNUSED
01579 static void
01580 bdb_lru_print( Cache *cache )
01581 {
01582        EntryInfo     *e;
01583 
01584        fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
01585        fprintf( stderr, "LRU circle (tail forward):\n" );
01586        for ( e = cache->c_lrutail; ; ) {
01587               fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
01588                      (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
01589               e = e->bei_lrunext;
01590               if ( e == cache->c_lrutail )
01591                      break;
01592        }
01593        fprintf( stderr, "LRU circle (tail backward):\n" );
01594        for ( e = cache->c_lrutail; ; ) {
01595               fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
01596                      (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
01597               e = e->bei_lruprev;
01598               if ( e == cache->c_lrutail )
01599                      break;
01600        }
01601 }
01602 
01603 static int
01604 bdb_entryinfo_print(void *data, void *arg)
01605 {
01606        EntryInfo *e = data;
01607        fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
01608               (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
01609        return 0;
01610 }
01611 
01612 static void
01613 bdb_idtree_print(Cache *cache)
01614 {
01615        avl_apply( cache->c_idtree, bdb_entryinfo_print, NULL, -1, AVL_INORDER );
01616 }
01617 #endif
01618 #endif
01619 
01620 static void
01621 bdb_reader_free( void *key, void *data )
01622 {
01623        /* DB_ENV *env = key; */
01624        DB_TXN *txn = data;
01625 
01626        if ( txn ) TXN_ABORT( txn );
01627 }
01628 
01629 /* free up any keys used by the main thread */
01630 void
01631 bdb_reader_flush( DB_ENV *env )
01632 {
01633        void *data;
01634        void *ctx = ldap_pvt_thread_pool_context();
01635 
01636        if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
01637               ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
01638               bdb_reader_free( env, data );
01639        }
01640 }
01641 
01642 int
01643 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
01644 {
01645        int i, rc;
01646        void *data;
01647        void *ctx;
01648 
01649        if ( !env || !txn ) return -1;
01650 
01651        /* If no op was provided, try to find the ctx anyway... */
01652        if ( op ) {
01653               ctx = op->o_threadctx;
01654        } else {
01655               ctx = ldap_pvt_thread_pool_context();
01656        }
01657 
01658        /* Shouldn't happen unless we're single-threaded */
01659        if ( !ctx ) {
01660               *txn = NULL;
01661               return 0;
01662        }
01663 
01664        if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
01665               for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
01666                      rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
01667                      if (rc) ldap_pvt_thread_yield();
01668               }
01669               if ( rc != 0) {
01670                      return rc;
01671               }
01672               data = *txn;
01673               if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
01674                      data, bdb_reader_free, NULL, NULL ) ) ) {
01675                      TXN_ABORT( *txn );
01676                      Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
01677                             db_strerror(rc), rc, 0 );
01678 
01679                      return rc;
01680               }
01681        } else {
01682               *txn = data;
01683        }
01684        return 0;
01685 }