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-2007 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;
68 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
71 ei = ch_calloc(1, sizeof(EntryInfo));
72 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
75 ei->bei_state = CACHE_ENTRY_REFERENCED;
81 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
83 free( ei->bei_nrdn.bv_val );
84 ei->bei_nrdn.bv_val = NULL;
86 free( ei->bei_rdn.bv_val );
87 ei->bei_rdn.bv_val = NULL;
92 ei->bei_parent = NULL;
94 ei->bei_lruprev = NULL;
96 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
97 ei->bei_lrunext = cache->c_eifree;
99 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
102 #define LRU_DEL( c, e ) do { \
103 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
104 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
105 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
106 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
107 e->bei_lruprev = NULL; \
110 /* Note - we now use a Second-Chance / Clock algorithm instead of
111 * Least-Recently-Used. This tremendously improves concurrency
112 * because we no longer need to manipulate the lists every time an
113 * entry is touched. We only need to lock the lists when adding
114 * or deleting an entry. It's now a circular doubly-linked list.
115 * We always append to the tail, but the head traverses the circle
116 * during a purge operation.
119 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
122 /* Already linked, ignore */
123 if ( ei->bei_lruprev )
126 /* Insert into circular LRU list */
127 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
129 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
130 if ( bdb->bi_cache.c_lrutail ) {
131 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
132 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
133 if ( ei->bei_lrunext )
134 ei->bei_lrunext->bei_lruprev = ei;
136 ei->bei_lrunext = ei->bei_lruprev = ei;
137 bdb->bi_cache.c_lruhead = ei;
139 bdb->bi_cache.c_lrutail = ei;
140 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
147 /* #define NO_DB_LOCK 1 */
148 /* Note: The BerkeleyDB locks are much slower than regular
149 * mutexes or rdwr locks. But the BDB implementation has the
150 * advantage of using a fixed size lock table, instead of
151 * allocating a lock object per entry in the DB. That's a
152 * key benefit for scaling. It also frees us from worrying
153 * about undetectable deadlocks between BDB activity and our
154 * own cache activity. It's still worth exploring faster
155 * alternatives though.
158 /* Atomically release and reacquire a lock */
160 bdb_cache_entry_db_relock(
161 struct bdb_info *bdb,
175 if ( !lock ) return 0;
177 lockobj.data = &ei->bei_id;
178 lockobj.size = sizeof(ei->bei_id) + 1;
180 list[0].op = DB_LOCK_PUT;
181 list[0].lock = *lock;
182 list[1].op = DB_LOCK_GET;
183 list[1].lock = *lock;
184 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
185 list[1].obj = &lockobj;
186 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
189 if (rc && !tryOnly) {
190 Debug( LDAP_DEBUG_TRACE,
191 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
192 ei->bei_id, rw, rc );
194 *lock = list[1].lock;
201 bdb_cache_entry_db_lock( struct bdb_info *bdb, BDB_LOCKER locker, EntryInfo *ei,
202 int rw, int tryOnly, DB_LOCK *lock )
211 if ( !lock ) return 0;
214 db_rw = DB_LOCK_WRITE;
216 db_rw = DB_LOCK_READ;
218 lockobj.data = &ei->bei_id;
219 lockobj.size = sizeof(ei->bei_id) + 1;
221 rc = LOCK_GET(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
222 &lockobj, db_rw, lock);
223 if (rc && !tryOnly) {
224 Debug( LDAP_DEBUG_TRACE,
225 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
226 ei->bei_id, rw, rc );
229 #endif /* NO_DB_LOCK */
233 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
240 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
242 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
248 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
249 int rw, DB_LOCK *lock )
254 bdb_cache_entry_db_unlock( bdb, lock );
256 bdb_cache_entryinfo_lock( ei );
257 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
259 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
262 bdb_cache_entryinfo_unlock( ei );
265 bdb_entry_return( e );
270 bdb_cache_entryinfo_destroy( EntryInfo *e )
272 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
273 free( e->bei_nrdn.bv_val );
275 free( e->bei_rdn.bv_val );
281 /* Do a length-ordered sort on normalized RDNs */
283 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
285 const EntryInfo *e1 = v_e1, *e2 = v_e2;
286 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
288 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
289 e1->bei_nrdn.bv_len );
295 bdb_id_cmp( const void *v_e1, const void *v_e2 )
297 const EntryInfo *e1 = v_e1, *e2 = v_e2;
298 return e1->bei_id - e2->bei_id;
302 bdb_id_dup_err( void *v1, void *v2 )
305 e2->bei_lrunext = v1;
309 /* Create an entryinfo in the cache. Caller must release the locks later.
312 bdb_entryinfo_add_internal(
313 struct bdb_info *bdb,
317 EntryInfo *ei2 = NULL;
321 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
323 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
324 bdb_cache_entryinfo_lock( ei->bei_parent );
326 ei2->bei_id = ei->bei_id;
327 ei2->bei_parent = ei->bei_parent;
329 ei2->bei_rdn = ei->bei_rdn;
331 #ifdef SLAP_ZONE_ALLOC
335 /* Add to cache ID tree */
336 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
338 EntryInfo *eix = ei2->bei_lrunext;
339 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
342 /* It got freed above because its value was
345 ei->bei_rdn.bv_val = NULL;
350 bdb->bi_cache.c_eiused++;
351 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
353 /* This is a new leaf node. But if parent had no kids, then it was
354 * a leaf and we would be decrementing that. So, only increment if
355 * the parent already has kids.
357 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
358 bdb->bi_cache.c_leaves++;
359 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
362 /* This should never happen; entry cache is corrupt */
363 bdb->bi_dbenv->log_flush( bdb->bi_dbenv, NULL );
367 ei->bei_parent->bei_ckids++;
375 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
376 * the info for its closest ancestor. *res should be NULL to process a
377 * complete DN starting from the tree root. Otherwise *res must be the
378 * immediate parent of the requested DN, and only the RDN will be searched.
379 * The EntryInfo is locked upon return and must be unlocked by the caller.
388 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
389 EntryInfo ei, *eip, *ei2;
393 /* this function is always called with normalized DN */
395 /* we're doing a onelevel search for an RDN */
396 ei.bei_nrdn.bv_val = ndn->bv_val;
397 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
400 /* we're searching a full DN from the root */
401 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
402 ei.bei_nrdn.bv_val = ptr;
403 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
404 /* Skip to next rdn if suffix is empty */
405 if ( ei.bei_nrdn.bv_len == 0 ) {
406 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
407 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
408 if ( ptr >= ndn->bv_val ) {
409 if (DN_SEPARATOR(*ptr)) ptr++;
410 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
411 ei.bei_nrdn.bv_val = ptr;
414 eip = &bdb->bi_cache.c_dntree;
417 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
418 eip->bei_state |= CACHE_ENTRY_REFERENCED;
420 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
423 int len = ei.bei_nrdn.bv_len;
425 if ( BER_BVISEMPTY( ndn )) {
430 ei.bei_nrdn.bv_len = ndn->bv_len -
431 (ei.bei_nrdn.bv_val - ndn->bv_val);
432 bdb_cache_entryinfo_unlock( eip );
434 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
435 ei.bei_nrdn.bv_val );
437 lock.mode = DB_LOCK_NG;
438 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, locker, &lock );
440 bdb_cache_entryinfo_lock( eip );
441 bdb_cache_entry_db_unlock( bdb, &lock );
446 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
447 ei.bei_nrdn.bv_val, ei.bei_id );
449 /* DN exists but needs to be added to cache */
450 ei.bei_nrdn.bv_len = len;
451 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
452 /* add_internal left eip and c_rwlock locked */
453 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
454 bdb_cache_entry_db_unlock( bdb, &lock );
459 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
460 /* In the midst of deleting? Give it a chance to
463 bdb_cache_entryinfo_unlock( eip );
464 ldap_pvt_thread_yield();
465 bdb_cache_entryinfo_lock( eip );
469 bdb_cache_entryinfo_unlock( eip );
470 bdb_cache_entryinfo_lock( ei2 );
474 /* Advance to next lower RDN */
475 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
476 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
477 if ( ptr >= ndn->bv_val ) {
478 if (DN_SEPARATOR(*ptr)) ptr++;
479 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
480 ei.bei_nrdn.bv_val = ptr;
482 if ( ptr < ndn->bv_val ) {
492 /* Walk up the tree from a child node, looking for an ID that's already
493 * been linked into the cache.
496 hdb_cache_find_parent(
502 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
503 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
511 rc = hdb_dn2id_parent( op, locker, &ei, &eip.bei_id );
514 /* Save the previous node, if any */
517 /* Create a new node for the current ID */
518 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
519 ein->bei_id = ei.bei_id;
520 ein->bei_kids = ei.bei_kids;
521 ein->bei_nrdn = ei.bei_nrdn;
522 ein->bei_rdn = ei.bei_rdn;
523 ein->bei_ckids = ei.bei_ckids;
524 #ifdef SLAP_ZONE_ALLOC
529 /* This node is not fully connected yet */
530 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
532 /* Insert this node into the ID tree */
533 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
534 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
535 bdb_id_cmp, bdb_id_dup_err ) ) {
536 EntryInfo *eix = ein->bei_lrunext;
538 /* Someone else created this node just before us.
539 * Free our new copy and use the existing one.
541 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
544 /* Link in any kids we've already processed */
546 bdb_cache_entryinfo_lock( ein );
547 avl_insert( &ein->bei_kids, (caddr_t)ei2,
548 bdb_rdn_cmp, avl_dup_error );
550 bdb_cache_entryinfo_unlock( ein );
554 /* If this is the first time, save this node
555 * to be returned later.
557 if ( eir == NULL ) eir = ein;
559 /* If there was a previous node, link it to this one */
560 if ( ei2 ) ei2->bei_parent = ein;
562 /* Look for this node's parent */
564 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
565 (caddr_t) &eip, bdb_id_cmp );
567 ei2 = &bdb->bi_cache.c_dntree;
569 bdb->bi_cache.c_eiused++;
570 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
571 bdb->bi_cache.c_leaves++;
572 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
574 /* Got the parent, link in and we're done. */
576 bdb_cache_entryinfo_lock( eir );
577 bdb_cache_entryinfo_lock( ei2 );
578 ein->bei_parent = ei2;
580 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
584 /* Reset all the state info */
585 for (ein = eir; ein != ei2; ein=ein->bei_parent)
586 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
588 bdb_cache_entryinfo_unlock( ei2 );
594 ei.bei_id = eip.bei_id;
596 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
602 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
606 struct bdb_info *bdb,
613 /* See if we already have this one */
614 bdb_cache_entryinfo_lock( ei->bei_parent );
615 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
616 bdb_cache_entryinfo_unlock( ei->bei_parent );
619 /* Not found, add it */
622 /* bei_rdn was not malloc'd before, do it now */
623 ber_dupbv( &bv, &ei->bei_rdn );
626 rc = bdb_entryinfo_add_internal( bdb, ei, res );
627 bdb_cache_entryinfo_unlock( ei->bei_parent );
628 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
630 /* Found, return it */
638 /* This is best-effort only. If all entries in the cache are
639 * busy, they will all be kept. This is unlikely to happen
640 * unless the cache is very much smaller than the working set.
643 bdb_cache_lru_purge( struct bdb_info *bdb )
645 DB_LOCK lock, *lockp;
646 EntryInfo *elru, *elnext = NULL;
647 int count, islocked, eimax;
649 /* Wait for the mutex; we're the only one trying to purge. */
650 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
652 if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
653 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
654 bdb->bi_cache.c_purging = 0;
658 if ( bdb->bi_cache.c_locker ) {
666 /* maximum number of EntryInfo leaves to cache. In slapcat
667 * we always free all leaf nodes.
669 if ( slapMode & SLAP_TOOL_READONLY )
672 eimax = bdb->bi_cache.c_eimax;
674 /* Look for an unused entry to remove */
675 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
676 elnext = elru->bei_lrunext;
678 if ( bdb_cache_entryinfo_trylock( elru ))
681 /* This flag implements the clock replacement behavior */
682 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
683 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
684 bdb_cache_entryinfo_unlock( elru );
688 /* If this node is in the process of linking into the cache,
689 * or this node is being deleted, skip it.
691 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
692 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
693 elru->bei_finders > 0 ) {
694 bdb_cache_entryinfo_unlock( elru );
698 /* entryinfo is locked */
701 /* If we can successfully writelock it, then
702 * the object is idle.
704 if ( bdb_cache_entry_db_lock( bdb,
705 bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
707 /* Free entry for this node if it's present */
709 elru->bei_e->e_private = NULL;
710 #ifdef SLAP_ZONE_ALLOC
711 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
713 bdb_entry_return( elru->bei_e );
718 bdb_cache_entry_db_unlock( bdb, lockp );
721 * If it is a leaf node, and we're over the limit, free it.
723 if ( elru->bei_kids ) {
724 /* Drop from list, we ignore it... */
725 LRU_DEL( &bdb->bi_cache, elru );
726 } else if ( bdb->bi_cache.c_leaves > eimax ) {
727 /* Too many leaf nodes, free this one */
728 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
729 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
731 } /* Leave on list until we need to free it */
735 bdb_cache_entryinfo_unlock( elru );
737 if ( count >= bdb->bi_cache.c_minfree ) {
738 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
739 bdb->bi_cache.c_cursize -= count;
740 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
744 if ( elnext == bdb->bi_cache.c_lruhead )
748 bdb->bi_cache.c_lruhead = elnext;
749 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
750 bdb->bi_cache.c_purging = 0;
755 struct bdb_info *bdb,
758 EntryInfo ei = { 0 },
763 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
764 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
765 (caddr_t) &ei, bdb_id_cmp );
766 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
771 * cache_find_id - find an entry in the cache, given id.
772 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
773 * the supplied *eip was already locked.
786 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
788 int rc = 0, load = 0;
789 EntryInfo ei = { 0 };
793 #ifdef SLAP_ZONE_ALLOC
794 slap_zh_rlock(bdb->bi_cache.c_zctx);
796 /* If we weren't given any info, see if we have it already cached */
798 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
799 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
800 (caddr_t) &ei, bdb_id_cmp );
802 /* If the lock attempt fails, the info is in use */
803 if ( bdb_cache_entryinfo_trylock( *eip )) {
804 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
805 /* If this node is being deleted, treat
806 * as if the delete has already finished
808 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
811 /* otherwise, wait for the info to free up */
812 ldap_pvt_thread_yield();
815 /* If this info isn't hooked up to its parent yet,
816 * unlock and wait for it to be fully initialized
818 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
819 bdb_cache_entryinfo_unlock( *eip );
820 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
821 ldap_pvt_thread_yield();
826 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
829 /* See if the ID exists in the database; add it to the cache if so */
832 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
834 rc = bdb_cache_find_ndn( op, locker,
836 if ( *eip ) flag |= ID_LOCKED;
838 ep->e_private = NULL;
839 #ifdef SLAP_ZONE_ALLOC
840 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
842 bdb_entry_return( ep );
848 rc = hdb_cache_find_parent(op, locker, id, eip );
849 if ( rc == 0 ) flag |= ID_LOCKED;
853 /* Ok, we found the info, do we have the entry? */
855 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
858 (*eip)->bei_finders++;
859 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
860 /* Make sure only one thread tries to load the entry */
862 #ifdef SLAP_ZONE_ALLOC
863 if ((*eip)->bei_e && !slap_zn_validate(
864 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
865 (*eip)->bei_e = NULL;
866 (*eip)->bei_zseq = 0;
869 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
871 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
874 /* If the entry was loaded before but uncached, and we need
875 * it again, clear the uncached state
877 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
878 (*eip)->bei_state ^= CACHE_ENTRY_NOT_CACHED;
879 if ( flag & ID_NOCACHE )
883 if ( flag & ID_LOCKED ) {
884 bdb_cache_entryinfo_unlock( *eip );
887 rc = bdb_cache_entry_db_lock( bdb, locker, *eip, load, 0, lock );
888 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
890 bdb_cache_entry_db_unlock( bdb, lock );
891 } else if ( rc == 0 ) {
894 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
897 ep->e_private = *eip;
902 #ifdef SLAP_ZONE_ALLOC
903 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
906 bdb_cache_lru_link( bdb, *eip );
907 if ( flag & ID_NOCACHE ) {
908 bdb_cache_entryinfo_lock( *eip );
909 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
910 bdb_cache_entryinfo_unlock( *eip );
914 /* If we succeeded, downgrade back to a readlock. */
915 rc = bdb_cache_entry_db_relock( bdb, locker,
918 /* Otherwise, release the lock. */
919 bdb_cache_entry_db_unlock( bdb, lock );
921 } else if ( !(*eip)->bei_e ) {
922 /* Some other thread is trying to load the entry,
923 * wait for it to finish.
925 bdb_cache_entry_db_unlock( bdb, lock );
926 bdb_cache_entryinfo_lock( *eip );
931 /* Check for subtree renames
933 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
935 bdb_cache_entry_db_relock( bdb,
936 locker, *eip, 1, 0, lock );
937 /* check again in case other modifier did it already */
938 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
939 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
940 bdb_cache_entry_db_relock( bdb,
941 locker, *eip, 0, 0, lock );
945 bdb_cache_entryinfo_lock( *eip );
946 (*eip)->bei_finders--;
948 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
949 bdb_cache_entryinfo_unlock( *eip );
953 if ( flag & ID_LOCKED ) {
954 bdb_cache_entryinfo_unlock( *eip );
957 ep->e_private = NULL;
958 #ifdef SLAP_ZONE_ALLOC
959 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
961 bdb_entry_return( ep );
968 if ( !( flag & ID_NOCACHE )) {
969 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
970 bdb->bi_cache.c_cursize++;
971 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
972 !bdb->bi_cache.c_purging ) {
974 bdb->bi_cache.c_purging = 1;
976 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
980 bdb_cache_lru_purge( bdb );
983 #ifdef SLAP_ZONE_ALLOC
984 if (rc == 0 && (*eip)->bei_e) {
985 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
987 slap_zh_runlock(bdb->bi_cache.c_zctx);
1000 if ( BEI(e)->bei_kids ) {
1003 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1006 rc = bdb_dn2id_children( op, txn, e );
1007 if ( rc == DB_NOTFOUND ) {
1008 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1013 /* Update the cache after a successful database Add. */
1016 struct bdb_info *bdb,
1019 struct berval *nrdn,
1026 struct berval rdn = e->e_name;
1029 ei.bei_id = e->e_id;
1030 ei.bei_parent = eip;
1031 ei.bei_nrdn = *nrdn;
1034 /* Lock this entry so that bdb_add can run to completion.
1035 * It can only fail if BDB has run out of lock resources.
1037 rc = bdb_cache_entry_db_lock( bdb, locker, &ei, 0, 0, lock );
1039 bdb_cache_entryinfo_unlock( eip );
1044 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1045 char *ptr = ber_bvchr( &rdn, ',' );
1046 assert( ptr != NULL );
1047 rdn.bv_len = ptr - rdn.bv_val;
1049 ber_dupbv( &ei.bei_rdn, &rdn );
1050 if ( eip->bei_dkids ) eip->bei_dkids++;
1053 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1054 /* bdb_csn_commit can cause this when adding the database root entry */
1056 new->bei_e->e_private = NULL;
1057 #ifdef SLAP_ZONE_ALLOC
1058 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1060 bdb_entry_return( new->bei_e );
1065 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1066 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1067 if (eip->bei_parent) {
1068 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1070 bdb_cache_entryinfo_unlock( eip );
1072 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1073 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1074 ++bdb->bi_cache.c_cursize;
1075 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1076 !bdb->bi_cache.c_purging ) {
1078 bdb->bi_cache.c_purging = 1;
1080 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1082 bdb_cache_lru_link( bdb, new );
1085 bdb_cache_lru_purge( bdb );
1092 struct bdb_info *bdb,
1094 Attribute *newAttrs,
1098 EntryInfo *ei = BEI(e);
1100 /* Get write lock on data */
1101 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1103 /* If we've done repeated mods on a cached entry, then e_attrs
1104 * is no longer contiguous with the entry, and must be freed.
1107 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1108 attrs_free( e->e_attrs );
1110 e->e_attrs = newAttrs;
1116 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1120 struct bdb_info *bdb,
1122 struct berval *nrdn,
1128 EntryInfo *ei = BEI(e), *pei;
1134 /* Get write lock on data */
1135 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1136 if ( rc ) return rc;
1138 /* If we've done repeated mods on a cached entry, then e_attrs
1139 * is no longer contiguous with the entry, and must be freed.
1141 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1142 attrs_free( e->e_attrs );
1144 e->e_attrs = new->e_attrs;
1145 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1146 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1148 ch_free(e->e_name.bv_val);
1149 ch_free(e->e_nname.bv_val);
1151 e->e_name = new->e_name;
1152 e->e_nname = new->e_nname;
1154 /* Lock the parent's kids AVL tree */
1155 pei = ei->bei_parent;
1156 bdb_cache_entryinfo_lock( pei );
1157 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1158 free( ei->bei_nrdn.bv_val );
1159 ber_dupbv( &ei->bei_nrdn, nrdn );
1162 free( ei->bei_rdn.bv_val );
1165 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1166 char *ptr = ber_bvchr(&rdn, ',');
1167 assert( ptr != NULL );
1168 rdn.bv_len = ptr - rdn.bv_val;
1170 ber_dupbv( &ei->bei_rdn, &rdn );
1172 /* If new parent, decrement kid counts */
1175 if ( pei->bei_dkids ) {
1177 if ( pei->bei_dkids < 2 )
1178 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1184 ein = ei->bei_parent;
1186 ei->bei_parent = ein;
1187 bdb_cache_entryinfo_unlock( pei );
1188 bdb_cache_entryinfo_lock( ein );
1190 /* new parent now has kids */
1191 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1192 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1193 /* grandparent has grandkids */
1194 if ( ein->bei_parent )
1195 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1197 /* parent might now have grandkids */
1198 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1199 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1200 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1203 if ( ein->bei_dkids ) ein->bei_dkids++;
1208 /* Record the generation number of this change */
1209 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1211 ei->bei_modrdns = bdb->bi_modrdns;
1212 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1215 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1216 bdb_cache_entryinfo_unlock( ein );
1220 * cache_delete - delete the entry e from the cache.
1222 * returns: 0 e was deleted ok
1223 * 1 e was not in the cache
1224 * -1 something bad happened
1228 struct bdb_info *bdb,
1233 EntryInfo *ei = BEI(e);
1236 assert( e->e_private != NULL );
1238 /* Set this early, warn off any queriers */
1239 ei->bei_state |= CACHE_ENTRY_DELETED;
1241 /* Lock the entry's info */
1242 bdb_cache_entryinfo_lock( ei );
1244 /* Get write lock on the data */
1245 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1247 /* couldn't lock, undo and give up */
1248 ei->bei_state ^= CACHE_ENTRY_DELETED;
1249 bdb_cache_entryinfo_unlock( ei );
1253 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1257 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1259 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1261 /* free lru mutex */
1262 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1264 /* Leave entry info locked */
1270 bdb_cache_delete_cleanup(
1275 ei->bei_e->e_private = NULL;
1276 #ifdef SLAP_ZONE_ALLOC
1277 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1279 bdb_entry_return( ei->bei_e );
1284 bdb_cache_entryinfo_free( cache, ei );
1285 bdb_cache_entryinfo_unlock( ei );
1289 bdb_cache_delete_internal(
1294 int rc = 0; /* return code */
1297 /* Lock the parent's kids tree */
1298 bdb_cache_entryinfo_lock( e->bei_parent );
1301 e->bei_parent->bei_ckids--;
1302 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1305 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1310 if ( e->bei_parent->bei_kids )
1313 bdb_cache_entryinfo_unlock( e->bei_parent );
1315 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1317 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1324 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1328 LRU_DEL( cache, e );
1331 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1333 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1341 bdb_entryinfo_release( void *data )
1343 EntryInfo *ei = (EntryInfo *)data;
1344 if ( ei->bei_kids ) {
1345 avl_free( ei->bei_kids, NULL );
1348 ei->bei_e->e_private = NULL;
1349 #ifdef SLAP_ZONE_ALLOC
1350 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1352 bdb_entry_return( ei->bei_e );
1355 bdb_cache_entryinfo_destroy( ei );
1359 bdb_cache_release_all( Cache *cache )
1361 /* set cache write lock */
1362 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1364 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1366 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1368 avl_free( cache->c_dntree.bei_kids, NULL );
1369 avl_free( cache->c_idtree, bdb_entryinfo_release );
1370 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1371 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1372 bdb_cache_entryinfo_destroy(cache->c_eifree);
1374 cache->c_cursize = 0;
1375 cache->c_eiused = 0;
1376 cache->c_leaves = 0;
1377 cache->c_idtree = NULL;
1378 cache->c_lruhead = NULL;
1379 cache->c_lrutail = NULL;
1380 cache->c_dntree.bei_kids = NULL;
1382 /* free lru mutex */
1383 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1384 /* free cache write lock */
1385 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1391 bdb_lru_print( Cache *cache )
1395 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1396 fprintf( stderr, "LRU circle (tail forward):\n" );
1397 for ( e = cache->c_lrutail; ; ) {
1398 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1399 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1401 if ( e == cache->c_lrutail )
1404 fprintf( stderr, "LRU circle (tail backward):\n" );
1405 for ( e = cache->c_lrutail; ; ) {
1406 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1407 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1409 if ( e == cache->c_lrutail )
1416 #ifdef BDB_REUSE_LOCKERS
1418 bdb_locker_id_free( void *key, void *data )
1424 #if DB_VERSION_FULL >= 0x04060012
1425 BDB_LOCKER lptr = data;
1428 lockid = (long)data;
1430 rc = XLOCK_ID_FREE( env, lockid );
1431 if ( rc == EINVAL ) {
1433 Debug( LDAP_DEBUG_ANY,
1434 "bdb_locker_id_free: %lu err %s(%d)\n",
1435 (unsigned long) lockid, db_strerror(rc), rc );
1436 /* release all locks held by this locker. */
1437 lr.op = DB_LOCK_PUT_ALL;
1439 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1440 XLOCK_ID_FREE( env, lockid );
1444 /* free up any keys used by the main thread */
1446 bdb_locker_flush( DB_ENV *env )
1449 void *ctx = ldap_pvt_thread_pool_context();
1451 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1452 ldap_pvt_thread_pool_setkey( ctx, env, NULL, NULL );
1453 bdb_locker_id_free( env, data );
1458 bdb_locker_id( Operation *op, DB_ENV *env, BDB_LOCKER *locker )
1465 if ( !env || !locker ) return -1;
1467 /* If no op was provided, try to find the ctx anyway... */
1469 ctx = op->o_threadctx;
1471 ctx = ldap_pvt_thread_pool_context();
1474 /* Shouldn't happen unless we're single-threaded */
1480 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1481 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1482 rc = XLOCK_ID( env, &lockid );
1483 if (rc) ldap_pvt_thread_yield();
1488 #if DB_VERSION_FULL >= 0x04060012
1490 __lock_getlocker( env->lk_handle, lockid, 0, &lptr );
1494 data = (void *)((long)lockid);
1496 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1497 data, bdb_locker_id_free ) ) ) {
1498 XLOCK_ID_FREE( env, lockid );
1499 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1500 db_strerror(rc), rc, 0 );
1505 lockid = (long)data;
1507 #if DB_VERSION_FULL >= 0x04060012
1514 #endif /* BDB_REUSE_LOCKERS */