1 /* cache.c - routines to maintain an in-core cache of entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
32 #define bdb_cache_lru_purge hdb_cache_lru_purge
34 static void bdb_cache_lru_purge( struct bdb_info *bdb );
36 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
40 static void bdb_lru_print(Cache *cache);
44 /* For concurrency experiments only! */
46 #define ldap_pvt_thread_rdwr_wlock(a) 0
47 #define ldap_pvt_thread_rdwr_wunlock(a) 0
48 #define ldap_pvt_thread_rdwr_rlock(a) 0
49 #define ldap_pvt_thread_rdwr_runlock(a) 0
53 #define ldap_pvt_thread_mutex_trylock(a) 0
57 bdb_cache_entryinfo_new( Cache *cache )
61 if ( cache->c_eifree ) {
62 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
63 if ( cache->c_eifree ) {
65 cache->c_eifree = ei->bei_lrunext;
67 ei->bei_lrunext = NULL;
69 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
72 ei = ch_calloc(1, sizeof(EntryInfo));
73 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
76 ei->bei_state = CACHE_ENTRY_REFERENCED;
82 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
84 free( ei->bei_nrdn.bv_val );
85 BER_BVZERO( &ei->bei_nrdn );
87 free( ei->bei_rdn.bv_val );
88 BER_BVZERO( &ei->bei_rdn );
93 ei->bei_parent = NULL;
95 ei->bei_lruprev = NULL;
98 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
99 ei->bei_lrunext = cache->c_eifree;
100 cache->c_eifree = ei;
101 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
107 #define LRU_DEL( c, e ) do { \
108 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
109 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
110 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
111 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
112 e->bei_lruprev = NULL; \
115 /* Note - we now use a Second-Chance / Clock algorithm instead of
116 * Least-Recently-Used. This tremendously improves concurrency
117 * because we no longer need to manipulate the lists every time an
118 * entry is touched. We only need to lock the lists when adding
119 * or deleting an entry. It's now a circular doubly-linked list.
120 * We always append to the tail, but the head traverses the circle
121 * during a purge operation.
124 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
127 /* Already linked, ignore */
128 if ( ei->bei_lruprev )
131 /* Insert into circular LRU list */
132 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
134 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
135 if ( bdb->bi_cache.c_lrutail ) {
136 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
137 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
138 if ( ei->bei_lrunext )
139 ei->bei_lrunext->bei_lruprev = ei;
141 ei->bei_lrunext = ei->bei_lruprev = ei;
142 bdb->bi_cache.c_lruhead = ei;
144 bdb->bi_cache.c_lrutail = ei;
145 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
152 /* #define NO_DB_LOCK 1 */
153 /* Note: The BerkeleyDB locks are much slower than regular
154 * mutexes or rdwr locks. But the BDB implementation has the
155 * advantage of using a fixed size lock table, instead of
156 * allocating a lock object per entry in the DB. That's a
157 * key benefit for scaling. It also frees us from worrying
158 * about undetectable deadlocks between BDB activity and our
159 * own cache activity. It's still worth exploring faster
160 * alternatives though.
163 /* Atomically release and reacquire a lock */
165 bdb_cache_entry_db_relock(
166 struct bdb_info *bdb,
180 if ( !lock ) return 0;
182 lockobj.data = &ei->bei_id;
183 lockobj.size = sizeof(ei->bei_id) + 1;
185 list[0].op = DB_LOCK_PUT;
186 list[0].lock = *lock;
187 list[1].op = DB_LOCK_GET;
188 list[1].lock = *lock;
189 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
190 list[1].obj = &lockobj;
191 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
194 if (rc && !tryOnly) {
195 Debug( LDAP_DEBUG_TRACE,
196 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
197 ei->bei_id, rw, rc );
199 *lock = list[1].lock;
206 bdb_cache_entry_db_lock( struct bdb_info *bdb, DB_TXN *txn, EntryInfo *ei,
207 int rw, int tryOnly, DB_LOCK *lock )
216 if ( !lock ) return 0;
219 db_rw = DB_LOCK_WRITE;
221 db_rw = DB_LOCK_READ;
223 lockobj.data = &ei->bei_id;
224 lockobj.size = sizeof(ei->bei_id) + 1;
226 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
227 &lockobj, db_rw, lock);
228 if (rc && !tryOnly) {
229 Debug( LDAP_DEBUG_TRACE,
230 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
231 ei->bei_id, rw, rc );
234 #endif /* NO_DB_LOCK */
238 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
245 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
247 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
253 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
254 int rw, DB_LOCK *lock )
261 ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
262 ( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
263 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
264 /* Releasing the entry can only be done when
265 * we know that nobody else is using it, i.e we
266 * should have an entry_db writelock. But the
267 * flag is only set by the thread that loads the
268 * entry, and only if no other threads has found
269 * it while it was working. All other threads
270 * clear the flag, which mean that we should be
271 * the only thread using the entry if the flag
275 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
278 bdb_cache_entryinfo_unlock( ei );
280 bdb_cache_entry_db_unlock( bdb, lock );
283 bdb_entry_return( e );
288 bdb_cache_entryinfo_destroy( EntryInfo *e )
290 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
291 free( e->bei_nrdn.bv_val );
293 free( e->bei_rdn.bv_val );
299 /* Do a length-ordered sort on normalized RDNs */
301 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
303 const EntryInfo *e1 = v_e1, *e2 = v_e2;
304 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
306 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
307 e1->bei_nrdn.bv_len );
313 bdb_id_cmp( const void *v_e1, const void *v_e2 )
315 const EntryInfo *e1 = v_e1, *e2 = v_e2;
316 return e1->bei_id - e2->bei_id;
320 bdb_id_dup_err( void *v1, void *v2 )
323 e2->bei_lrunext = v1;
327 /* Create an entryinfo in the cache. Caller must release the locks later.
330 bdb_entryinfo_add_internal(
331 struct bdb_info *bdb,
335 EntryInfo *ei2 = NULL;
339 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
341 bdb_cache_entryinfo_lock( ei->bei_parent );
342 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
344 ei2->bei_id = ei->bei_id;
345 ei2->bei_parent = ei->bei_parent;
347 ei2->bei_rdn = ei->bei_rdn;
349 #ifdef SLAP_ZONE_ALLOC
353 /* Add to cache ID tree */
354 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
356 EntryInfo *eix = ei2->bei_lrunext;
357 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
360 /* It got freed above because its value was
363 ei->bei_rdn.bv_val = NULL;
368 bdb->bi_cache.c_eiused++;
369 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
371 /* This is a new leaf node. But if parent had no kids, then it was
372 * a leaf and we would be decrementing that. So, only increment if
373 * the parent already has kids.
375 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
376 bdb->bi_cache.c_leaves++;
377 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
380 /* it's possible for hdb_cache_find_parent to beat us to it */
382 ei->bei_parent->bei_ckids++;
391 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
392 * the info for its closest ancestor. *res should be NULL to process a
393 * complete DN starting from the tree root. Otherwise *res must be the
394 * immediate parent of the requested DN, and only the RDN will be searched.
395 * The EntryInfo is locked upon return and must be unlocked by the caller.
404 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
405 EntryInfo ei, *eip, *ei2;
409 /* this function is always called with normalized DN */
411 /* we're doing a onelevel search for an RDN */
412 ei.bei_nrdn.bv_val = ndn->bv_val;
413 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
416 /* we're searching a full DN from the root */
417 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
418 ei.bei_nrdn.bv_val = ptr;
419 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
420 /* Skip to next rdn if suffix is empty */
421 if ( ei.bei_nrdn.bv_len == 0 ) {
422 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
423 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
424 if ( ptr >= ndn->bv_val ) {
425 if (DN_SEPARATOR(*ptr)) ptr++;
426 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
427 ei.bei_nrdn.bv_val = ptr;
430 eip = &bdb->bi_cache.c_dntree;
433 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
434 eip->bei_state |= CACHE_ENTRY_REFERENCED;
436 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
439 int len = ei.bei_nrdn.bv_len;
441 if ( BER_BVISEMPTY( ndn )) {
446 ei.bei_nrdn.bv_len = ndn->bv_len -
447 (ei.bei_nrdn.bv_val - ndn->bv_val);
449 bdb_cache_entryinfo_unlock( eip );
451 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
452 ei.bei_nrdn.bv_val );
454 lock.mode = DB_LOCK_NG;
455 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &lock );
457 bdb_cache_entryinfo_lock( eip );
459 bdb_cache_entry_db_unlock( bdb, &lock );
464 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
465 ei.bei_nrdn.bv_val, ei.bei_id );
467 /* DN exists but needs to be added to cache */
468 ei.bei_nrdn.bv_len = len;
469 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
470 /* add_internal left eip and c_rwlock locked */
472 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
473 bdb_cache_entry_db_unlock( bdb, &lock );
478 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
479 /* In the midst of deleting? Give it a chance to
482 bdb_cache_entryinfo_unlock( eip );
483 ldap_pvt_thread_yield();
484 bdb_cache_entryinfo_lock( eip );
488 bdb_cache_entryinfo_lock( ei2 );
489 bdb_cache_entryinfo_unlock( eip );
493 /* Advance to next lower RDN */
494 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
495 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
496 if ( ptr >= ndn->bv_val ) {
497 if (DN_SEPARATOR(*ptr)) ptr++;
498 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
499 ei.bei_nrdn.bv_val = ptr;
501 if ( ptr < ndn->bv_val ) {
511 /* Walk up the tree from a child node, looking for an ID that's already
512 * been linked into the cache.
515 hdb_cache_find_parent(
521 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
522 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
530 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
533 /* Save the previous node, if any */
536 /* Create a new node for the current ID */
537 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
538 ein->bei_id = ei.bei_id;
539 ein->bei_kids = ei.bei_kids;
540 ein->bei_nrdn = ei.bei_nrdn;
541 ein->bei_rdn = ei.bei_rdn;
542 ein->bei_ckids = ei.bei_ckids;
543 #ifdef SLAP_ZONE_ALLOC
549 /* This node is not fully connected yet */
550 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
552 /* If this is the first time, save this node
553 * to be returned later.
561 /* Insert this node into the ID tree */
562 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
563 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
564 bdb_id_cmp, bdb_id_dup_err ) ) {
565 EntryInfo *eix = ein->bei_lrunext;
567 if ( eix->bei_state & (CACHE_ENTRY_PURGED|CACHE_ENTRY_DELETED) ) {
568 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
569 ldap_pvt_thread_yield();
572 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
574 /* Someone else created this node just before us.
575 * Free our new copy and use the existing one.
577 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
579 /* if it was the node we were looking for, just return it */
583 bdb_cache_entryinfo_lock( eix );
591 /* otherwise, link up what we have and return */
595 /* If there was a previous node, link it to this one */
596 if ( ei2 ) ei2->bei_parent = ein;
598 /* Look for this node's parent */
600 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
601 (caddr_t) &eip, bdb_id_cmp );
602 if ( ei2 && ( ei2->bei_state & ( CACHE_ENTRY_PURGED|CACHE_ENTRY_DELETED ))) {
606 ei2 = &bdb->bi_cache.c_dntree;
609 bdb->bi_cache.c_eiused++;
610 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
611 bdb->bi_cache.c_leaves++;
613 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
616 /* Got the parent, link in and we're done. */
618 bdb_cache_entryinfo_lock( ei2 );
619 bdb_cache_entryinfo_lock( eir );
620 ein->bei_parent = ei2;
622 if ( avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
623 avl_dup_error) == 0 )
626 /* Reset all the state info */
627 for (ein = eir; ein != ei2; ein=ein->bei_parent)
628 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
630 bdb_cache_entryinfo_unlock( ei2 );
636 ei.bei_id = eip.bei_id;
638 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
644 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
648 struct bdb_info *bdb,
655 /* See if we already have this one */
656 bdb_cache_entryinfo_lock( ei->bei_parent );
657 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
658 bdb_cache_entryinfo_unlock( ei->bei_parent );
661 /* Not found, add it */
664 /* bei_rdn was not malloc'd before, do it now */
665 ber_dupbv( &bv, &ei->bei_rdn );
668 rc = bdb_entryinfo_add_internal( bdb, ei, res );
669 bdb_cache_entryinfo_unlock( ei->bei_parent );
670 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
672 /* Found, return it */
680 /* This is best-effort only. If all entries in the cache are
681 * busy, they will all be kept. This is unlikely to happen
682 * unless the cache is very much smaller than the working set.
685 bdb_cache_lru_purge( struct bdb_info *bdb )
687 DB_LOCK lock, *lockp;
688 EntryInfo *elru, *elnext = NULL;
689 int count, islocked, eimax;
690 int efree = 0, eifree = 0, eicount, ecount;
695 /* Wait for the mutex; we're the only one trying to purge. */
696 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
698 /* maximum number of EntryInfo leaves to cache. In slapcat
699 * we always free all leaf nodes.
701 if ( slapMode & SLAP_TOOL_READONLY )
704 eimax = bdb->bi_cache.c_eimax;
706 efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
710 efree += bdb->bi_cache.c_minfree;
712 if ( bdb->bi_cache.c_leaves > eimax ) {
713 eifree = bdb->bi_cache.c_minfree * 10;
714 if ( eifree >= eimax )
718 if ( !efree && !eifree ) {
719 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
720 bdb->bi_cache.c_purging = 0;
724 if ( bdb->bi_cache.c_txn ) {
737 /* Look for an unused entry to remove */
738 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
739 elnext = elru->bei_lrunext;
741 if ( bdb_cache_entryinfo_trylock( elru ))
744 /* This flag implements the clock replacement behavior */
745 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
746 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
747 bdb_cache_entryinfo_unlock( elru );
751 /* If this node is in the process of linking into the cache,
752 * or this node is being deleted, skip it.
754 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
755 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING |
756 CACHE_ENTRY_ONELEVEL )) ||
757 elru->bei_finders > 0 ) {
758 bdb_cache_entryinfo_unlock( elru );
762 if ( bdb_cache_entryinfo_trylock( elru->bei_parent )) {
763 bdb_cache_entryinfo_unlock( elru );
767 /* entryinfo is locked */
770 /* If we can successfully writelock it, then
771 * the object is idle.
773 if ( bdb_cache_entry_db_lock( bdb,
774 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
776 /* Free entry for this node if it's present */
780 /* the cache may have gone over the limit while we
781 * weren't looking, so double check.
783 if ( !efree && ecount > bdb->bi_cache.c_maxsize )
784 efree = bdb->bi_cache.c_minfree;
786 if ( count < efree ) {
787 elru->bei_e->e_private = NULL;
788 #ifdef SLAP_ZONE_ALLOC
789 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
791 bdb_entry_return( elru->bei_e );
796 /* Keep this node cached, skip to next */
797 bdb_cache_entry_db_unlock( bdb, lockp );
801 bdb_cache_entry_db_unlock( bdb, lockp );
804 * If it is a leaf node, and we're over the limit, free it.
806 if ( elru->bei_kids ) {
807 /* Drop from list, we ignore it... */
808 LRU_DEL( &bdb->bi_cache, elru );
809 } else if ( eicount < eifree ) {
810 /* Too many leaf nodes, free this one */
811 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
812 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
815 } /* Leave on list until we need to free it */
820 bdb_cache_entryinfo_unlock( elru );
821 bdb_cache_entryinfo_unlock( elru->bei_parent );
824 if ( count >= efree && eicount >= eifree )
827 if ( elnext == bdb->bi_cache.c_lruhead )
834 if ( count || ecount > bdb->bi_cache.c_cursize ) {
835 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
836 /* HACK: we seem to be losing track, fix up now */
837 if ( ecount > bdb->bi_cache.c_cursize )
838 bdb->bi_cache.c_cursize = ecount;
839 bdb->bi_cache.c_cursize -= count;
840 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
842 bdb->bi_cache.c_lruhead = elnext;
843 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
844 bdb->bi_cache.c_purging = 0;
848 * cache_find_id - find an entry in the cache, given id.
849 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
850 * the supplied *eip was already locked.
862 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
864 int rc = 0, load = 0;
865 EntryInfo ei = { 0 };
869 #ifdef SLAP_ZONE_ALLOC
870 slap_zh_rlock(bdb->bi_cache.c_zctx);
872 /* If we weren't given any info, see if we have it already cached */
874 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
875 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
876 (caddr_t) &ei, bdb_id_cmp );
878 /* If the lock attempt fails, the info is in use */
879 if ( bdb_cache_entryinfo_trylock( *eip )) {
880 int del = (*eip)->bei_state & CACHE_ENTRY_DELETED;
881 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
882 /* If this node is being deleted, treat
883 * as if the delete has already finished
888 /* otherwise, wait for the info to free up */
889 ldap_pvt_thread_yield();
892 /* If this info isn't hooked up to its parent yet,
893 * unlock and wait for it to be fully initialized
895 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
896 bdb_cache_entryinfo_unlock( *eip );
897 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
898 ldap_pvt_thread_yield();
903 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
906 /* See if the ID exists in the database; add it to the cache if so */
909 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
911 rc = bdb_cache_find_ndn( op, tid,
913 if ( *eip ) flag |= ID_LOCKED;
915 ep->e_private = NULL;
916 #ifdef SLAP_ZONE_ALLOC
917 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
919 bdb_entry_return( ep );
925 rc = hdb_cache_find_parent(op, tid, id, eip );
926 if ( rc == 0 ) flag |= ID_LOCKED;
930 /* Ok, we found the info, do we have the entry? */
932 if ( !( flag & ID_LOCKED )) {
933 bdb_cache_entryinfo_lock( *eip );
937 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
940 (*eip)->bei_finders++;
941 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
942 if ( flag & ID_NOENTRY ) {
943 bdb_cache_entryinfo_unlock( *eip );
946 /* Make sure only one thread tries to load the entry */
948 #ifdef SLAP_ZONE_ALLOC
949 if ((*eip)->bei_e && !slap_zn_validate(
950 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
951 (*eip)->bei_e = NULL;
952 (*eip)->bei_zseq = 0;
955 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
957 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
961 /* Clear the uncached state if we are not
962 * loading it, i.e it is already cached or
963 * another thread is currently loading it.
965 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
966 (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
971 if ( flag & ID_LOCKED ) {
972 bdb_cache_entryinfo_unlock( *eip );
975 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
976 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
978 bdb_cache_entry_db_unlock( bdb, lock );
979 } else if ( rc == 0 ) {
982 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
985 ep->e_private = *eip;
987 while ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED )
988 ldap_pvt_thread_yield();
992 #ifdef SLAP_ZONE_ALLOC
993 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
996 bdb_cache_lru_link( bdb, *eip );
997 if (( flag & ID_NOCACHE ) &&
998 ( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
999 /* Set the cached state only if no other thread
1000 * found the info while we were loading the entry.
1002 if ( (*eip)->bei_finders == 1 )
1003 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
1004 bdb_cache_entryinfo_unlock( *eip );
1008 /* If we succeeded, downgrade back to a readlock. */
1009 rc = bdb_cache_entry_db_relock( bdb, tid,
1012 /* Otherwise, release the lock. */
1013 bdb_cache_entry_db_unlock( bdb, lock );
1015 } else if ( !(*eip)->bei_e ) {
1016 /* Some other thread is trying to load the entry,
1017 * wait for it to finish.
1019 bdb_cache_entry_db_unlock( bdb, lock );
1020 bdb_cache_entryinfo_lock( *eip );
1025 /* Check for subtree renames
1027 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
1029 bdb_cache_entry_db_relock( bdb,
1030 tid, *eip, 1, 0, lock );
1031 /* check again in case other modifier did it already */
1032 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
1033 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
1034 bdb_cache_entry_db_relock( bdb,
1035 tid, *eip, 0, 0, lock );
1040 bdb_cache_entryinfo_lock( *eip );
1041 (*eip)->bei_finders--;
1043 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
1044 bdb_cache_entryinfo_unlock( *eip );
1047 if ( flag & ID_LOCKED ) {
1048 bdb_cache_entryinfo_unlock( *eip );
1051 ep->e_private = NULL;
1052 #ifdef SLAP_ZONE_ALLOC
1053 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
1055 bdb_entry_return( ep );
1061 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ||
1062 bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1063 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1064 if ( !bdb->bi_cache.c_purging ) {
1065 if ( load && !( flag & ID_NOCACHE )) {
1066 bdb->bi_cache.c_cursize++;
1067 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
1069 bdb->bi_cache.c_purging = 1;
1071 } else if ( bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1073 bdb->bi_cache.c_purging = 1;
1076 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1079 bdb_cache_lru_purge( bdb );
1082 #ifdef SLAP_ZONE_ALLOC
1083 if (rc == 0 && (*eip)->bei_e) {
1084 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
1086 slap_zh_runlock(bdb->bi_cache.c_zctx);
1099 if ( BEI(e)->bei_kids ) {
1102 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1105 rc = bdb_dn2id_children( op, txn, e );
1106 if ( rc == DB_NOTFOUND ) {
1107 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1112 /* Update the cache after a successful database Add. */
1115 struct bdb_info *bdb,
1118 struct berval *nrdn,
1125 struct berval rdn = e->e_name;
1128 ei.bei_id = e->e_id;
1129 ei.bei_parent = eip;
1130 ei.bei_nrdn = *nrdn;
1133 /* Lock this entry so that bdb_add can run to completion.
1134 * It can only fail if BDB has run out of lock resources.
1136 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1138 bdb_cache_entryinfo_unlock( eip );
1143 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1144 char *ptr = ber_bvchr( &rdn, ',' );
1145 assert( ptr != NULL );
1146 rdn.bv_len = ptr - rdn.bv_val;
1148 ber_dupbv( &ei.bei_rdn, &rdn );
1149 if ( eip->bei_dkids ) eip->bei_dkids++;
1152 if (eip->bei_parent) {
1153 bdb_cache_entryinfo_lock( eip->bei_parent );
1154 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1155 bdb_cache_entryinfo_unlock( eip->bei_parent );
1158 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1159 /* bdb_csn_commit can cause this when adding the database root entry */
1161 new->bei_e->e_private = NULL;
1162 #ifdef SLAP_ZONE_ALLOC
1163 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1165 bdb_entry_return( new->bei_e );
1170 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1171 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1172 bdb_cache_entryinfo_unlock( eip );
1174 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1175 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1176 ++bdb->bi_cache.c_cursize;
1177 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1178 !bdb->bi_cache.c_purging ) {
1180 bdb->bi_cache.c_purging = 1;
1182 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1184 bdb_cache_lru_link( bdb, new );
1187 bdb_cache_lru_purge( bdb );
1194 struct bdb_info *bdb,
1196 Attribute *newAttrs,
1200 EntryInfo *ei = BEI(e);
1202 /* Get write lock on data */
1203 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1205 /* If we've done repeated mods on a cached entry, then e_attrs
1206 * is no longer contiguous with the entry, and must be freed.
1209 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1210 attrs_free( e->e_attrs );
1212 e->e_attrs = newAttrs;
1218 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1222 struct bdb_info *bdb,
1224 struct berval *nrdn,
1230 EntryInfo *ei = BEI(e), *pei;
1236 /* Get write lock on data */
1237 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1238 if ( rc ) return rc;
1240 /* If we've done repeated mods on a cached entry, then e_attrs
1241 * is no longer contiguous with the entry, and must be freed.
1243 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1244 attrs_free( e->e_attrs );
1246 e->e_attrs = new->e_attrs;
1247 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1248 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1250 ch_free(e->e_name.bv_val);
1251 ch_free(e->e_nname.bv_val);
1253 e->e_name = new->e_name;
1254 e->e_nname = new->e_nname;
1256 /* Lock the parent's kids AVL tree */
1257 pei = ei->bei_parent;
1258 bdb_cache_entryinfo_lock( pei );
1259 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1260 free( ei->bei_nrdn.bv_val );
1261 ber_dupbv( &ei->bei_nrdn, nrdn );
1264 free( ei->bei_rdn.bv_val );
1267 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1268 char *ptr = ber_bvchr(&rdn, ',');
1269 assert( ptr != NULL );
1270 rdn.bv_len = ptr - rdn.bv_val;
1272 ber_dupbv( &ei->bei_rdn, &rdn );
1274 /* If new parent, decrement kid counts */
1277 if ( pei->bei_dkids ) {
1279 if ( pei->bei_dkids < 2 )
1280 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1286 ein = ei->bei_parent;
1288 ei->bei_parent = ein;
1289 bdb_cache_entryinfo_unlock( pei );
1290 bdb_cache_entryinfo_lock( ein );
1292 /* new parent now has kids */
1293 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1294 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1295 /* grandparent has grandkids */
1296 if ( ein->bei_parent )
1297 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1299 /* parent might now have grandkids */
1300 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1301 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1302 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1305 if ( ein->bei_dkids ) ein->bei_dkids++;
1310 /* Record the generation number of this change */
1311 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1313 ei->bei_modrdns = bdb->bi_modrdns;
1314 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1317 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1318 bdb_cache_entryinfo_unlock( ein );
1322 * cache_delete - delete the entry e from the cache.
1324 * returns: 0 e was deleted ok
1325 * 1 e was not in the cache
1326 * -1 something bad happened
1330 struct bdb_info *bdb,
1335 EntryInfo *ei = BEI(e);
1338 assert( e->e_private != NULL );
1340 /* Lock the entry's info */
1341 bdb_cache_entryinfo_lock( ei );
1343 /* Set this early, warn off any queriers */
1344 ei->bei_state |= CACHE_ENTRY_DELETED;
1346 bdb_cache_entryinfo_unlock( ei );
1348 /* Get write lock on the data */
1349 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1351 bdb_cache_entryinfo_lock( ei );
1352 /* couldn't lock, undo and give up */
1353 ei->bei_state ^= CACHE_ENTRY_DELETED;
1354 bdb_cache_entryinfo_unlock( ei );
1358 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1362 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1364 bdb_cache_entryinfo_lock( ei->bei_parent );
1365 bdb_cache_entryinfo_lock( ei );
1366 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1367 bdb_cache_entryinfo_unlock( ei );
1369 /* free lru mutex */
1370 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1376 bdb_cache_delete_cleanup(
1380 /* Enter with ei locked */
1382 /* already freed? */
1383 if ( !ei->bei_parent ) return;
1386 ei->bei_e->e_private = NULL;
1387 #ifdef SLAP_ZONE_ALLOC
1388 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1390 bdb_entry_return( ei->bei_e );
1395 bdb_cache_entryinfo_unlock( ei );
1396 bdb_cache_entryinfo_free( cache, ei );
1400 bdb_cache_delete_internal(
1405 int rc = 0; /* return code */
1408 /* already freed? */
1409 if ( !e->bei_parent ) {
1414 e->bei_state |= CACHE_ENTRY_PURGED;
1417 e->bei_parent->bei_ckids--;
1418 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1421 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1427 if ( e->bei_parent->bei_kids )
1430 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1432 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1440 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1441 bdb_cache_entryinfo_unlock( e->bei_parent );
1445 LRU_DEL( cache, e );
1448 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1450 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1458 bdb_entryinfo_release( void *data )
1460 EntryInfo *ei = (EntryInfo *)data;
1461 if ( ei->bei_kids ) {
1462 avl_free( ei->bei_kids, NULL );
1465 ei->bei_e->e_private = NULL;
1466 #ifdef SLAP_ZONE_ALLOC
1467 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1469 bdb_entry_return( ei->bei_e );
1472 bdb_cache_entryinfo_destroy( ei );
1476 bdb_cache_release_all( Cache *cache )
1478 /* set cache write lock */
1479 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1481 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1483 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1485 avl_free( cache->c_dntree.bei_kids, NULL );
1486 avl_free( cache->c_idtree, bdb_entryinfo_release );
1487 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1488 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1489 bdb_cache_entryinfo_destroy(cache->c_eifree);
1491 cache->c_cursize = 0;
1492 cache->c_eiused = 0;
1493 cache->c_leaves = 0;
1494 cache->c_idtree = NULL;
1495 cache->c_lruhead = NULL;
1496 cache->c_lrutail = NULL;
1497 cache->c_dntree.bei_kids = NULL;
1499 /* free lru mutex */
1500 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1501 /* free cache write lock */
1502 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1508 bdb_lru_print( Cache *cache )
1512 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1513 fprintf( stderr, "LRU circle (tail forward):\n" );
1514 for ( e = cache->c_lrutail; ; ) {
1515 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1516 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1518 if ( e == cache->c_lrutail )
1521 fprintf( stderr, "LRU circle (tail backward):\n" );
1522 for ( e = cache->c_lrutail; ; ) {
1523 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1524 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1526 if ( e == cache->c_lrutail )
1534 bdb_reader_free( void *key, void *data )
1536 /* DB_ENV *env = key; */
1539 if ( txn ) TXN_ABORT( txn );
1542 /* free up any keys used by the main thread */
1544 bdb_reader_flush( DB_ENV *env )
1547 void *ctx = ldap_pvt_thread_pool_context();
1549 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1550 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1551 bdb_reader_free( env, data );
1556 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1562 if ( !env || !txn ) return -1;
1564 /* If no op was provided, try to find the ctx anyway... */
1566 ctx = op->o_threadctx;
1568 ctx = ldap_pvt_thread_pool_context();
1571 /* Shouldn't happen unless we're single-threaded */
1577 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1578 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1579 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1580 if (rc) ldap_pvt_thread_yield();
1586 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1587 data, bdb_reader_free, NULL, NULL ) ) ) {
1589 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1590 db_strerror(rc), rc, 0 );