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-2008 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, TXN_ID(txn), 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, DB_TXN *txn, 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, TXN_ID(txn), 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 )
256 ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
257 ( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
258 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
259 /* Releasing the entry can only be done when
260 * we know that nobody else is using it, i.e we
261 * should have an entry_db writelock. But the
262 * flag is only set by the thread that loads the
263 * entry, and only if no other threads has found
264 * it while it was working. All other threads
265 * clear the flag, which mean that we should be
266 * the only thread using the entry if the flag
270 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
273 bdb_cache_entryinfo_unlock( ei );
275 bdb_cache_entry_db_unlock( bdb, lock );
278 bdb_entry_return( e );
283 bdb_cache_entryinfo_destroy( EntryInfo *e )
285 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
286 free( e->bei_nrdn.bv_val );
288 free( e->bei_rdn.bv_val );
294 /* Do a length-ordered sort on normalized RDNs */
296 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
298 const EntryInfo *e1 = v_e1, *e2 = v_e2;
299 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
301 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
302 e1->bei_nrdn.bv_len );
308 bdb_id_cmp( const void *v_e1, const void *v_e2 )
310 const EntryInfo *e1 = v_e1, *e2 = v_e2;
311 return e1->bei_id - e2->bei_id;
315 bdb_id_dup_err( void *v1, void *v2 )
318 e2->bei_lrunext = v1;
322 /* Create an entryinfo in the cache. Caller must release the locks later.
325 bdb_entryinfo_add_internal(
326 struct bdb_info *bdb,
330 EntryInfo *ei2 = NULL;
334 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
336 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
337 bdb_cache_entryinfo_lock( ei->bei_parent );
339 ei2->bei_id = ei->bei_id;
340 ei2->bei_parent = ei->bei_parent;
342 ei2->bei_rdn = ei->bei_rdn;
344 #ifdef SLAP_ZONE_ALLOC
348 /* Add to cache ID tree */
349 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
351 EntryInfo *eix = ei2->bei_lrunext;
352 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
355 /* It got freed above because its value was
358 ei->bei_rdn.bv_val = NULL;
363 bdb->bi_cache.c_eiused++;
364 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
366 /* This is a new leaf node. But if parent had no kids, then it was
367 * a leaf and we would be decrementing that. So, only increment if
368 * the parent already has kids.
370 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
371 bdb->bi_cache.c_leaves++;
372 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
375 /* This should never happen; entry cache is corrupt */
376 bdb->bi_dbenv->log_flush( bdb->bi_dbenv, NULL );
380 ei->bei_parent->bei_ckids++;
388 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
389 * the info for its closest ancestor. *res should be NULL to process a
390 * complete DN starting from the tree root. Otherwise *res must be the
391 * immediate parent of the requested DN, and only the RDN will be searched.
392 * The EntryInfo is locked upon return and must be unlocked by the caller.
401 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
402 EntryInfo ei, *eip, *ei2;
406 /* this function is always called with normalized DN */
408 /* we're doing a onelevel search for an RDN */
409 ei.bei_nrdn.bv_val = ndn->bv_val;
410 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
413 /* we're searching a full DN from the root */
414 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
415 ei.bei_nrdn.bv_val = ptr;
416 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
417 /* Skip to next rdn if suffix is empty */
418 if ( ei.bei_nrdn.bv_len == 0 ) {
419 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
420 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
421 if ( ptr >= ndn->bv_val ) {
422 if (DN_SEPARATOR(*ptr)) ptr++;
423 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
424 ei.bei_nrdn.bv_val = ptr;
427 eip = &bdb->bi_cache.c_dntree;
430 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
431 eip->bei_state |= CACHE_ENTRY_REFERENCED;
433 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
436 int len = ei.bei_nrdn.bv_len;
438 if ( BER_BVISEMPTY( ndn )) {
443 ei.bei_nrdn.bv_len = ndn->bv_len -
444 (ei.bei_nrdn.bv_val - ndn->bv_val);
445 bdb_cache_entryinfo_unlock( eip );
447 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
448 ei.bei_nrdn.bv_val );
450 lock.mode = DB_LOCK_NG;
451 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &lock );
453 bdb_cache_entryinfo_lock( eip );
454 bdb_cache_entry_db_unlock( bdb, &lock );
459 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
460 ei.bei_nrdn.bv_val, ei.bei_id );
462 /* DN exists but needs to be added to cache */
463 ei.bei_nrdn.bv_len = len;
464 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
465 /* add_internal left eip and c_rwlock locked */
466 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
467 bdb_cache_entry_db_unlock( bdb, &lock );
472 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
473 /* In the midst of deleting? Give it a chance to
476 bdb_cache_entryinfo_unlock( eip );
477 ldap_pvt_thread_yield();
478 bdb_cache_entryinfo_lock( eip );
482 bdb_cache_entryinfo_unlock( eip );
483 bdb_cache_entryinfo_lock( ei2 );
487 /* Advance to next lower RDN */
488 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
489 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
490 if ( ptr >= ndn->bv_val ) {
491 if (DN_SEPARATOR(*ptr)) ptr++;
492 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
493 ei.bei_nrdn.bv_val = ptr;
495 if ( ptr < ndn->bv_val ) {
505 /* Walk up the tree from a child node, looking for an ID that's already
506 * been linked into the cache.
509 hdb_cache_find_parent(
515 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
516 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
524 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
527 /* Save the previous node, if any */
530 /* Create a new node for the current ID */
531 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
532 ein->bei_id = ei.bei_id;
533 ein->bei_kids = ei.bei_kids;
534 ein->bei_nrdn = ei.bei_nrdn;
535 ein->bei_rdn = ei.bei_rdn;
536 ein->bei_ckids = ei.bei_ckids;
537 #ifdef SLAP_ZONE_ALLOC
542 /* This node is not fully connected yet */
543 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
545 /* Insert this node into the ID tree */
546 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
547 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
548 bdb_id_cmp, bdb_id_dup_err ) ) {
549 EntryInfo *eix = ein->bei_lrunext;
551 /* Someone else created this node just before us.
552 * Free our new copy and use the existing one.
554 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
557 /* Link in any kids we've already processed */
559 bdb_cache_entryinfo_lock( ein );
560 avl_insert( &ein->bei_kids, (caddr_t)ei2,
561 bdb_rdn_cmp, avl_dup_error );
563 bdb_cache_entryinfo_unlock( ein );
567 /* If this is the first time, save this node
568 * to be returned later.
570 if ( eir == NULL ) eir = ein;
572 /* If there was a previous node, link it to this one */
573 if ( ei2 ) ei2->bei_parent = ein;
575 /* Look for this node's parent */
577 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
578 (caddr_t) &eip, bdb_id_cmp );
580 ei2 = &bdb->bi_cache.c_dntree;
582 bdb->bi_cache.c_eiused++;
583 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
584 bdb->bi_cache.c_leaves++;
585 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
587 /* Got the parent, link in and we're done. */
589 bdb_cache_entryinfo_lock( eir );
590 bdb_cache_entryinfo_lock( ei2 );
591 ein->bei_parent = ei2;
593 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
597 /* Reset all the state info */
598 for (ein = eir; ein != ei2; ein=ein->bei_parent)
599 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
601 bdb_cache_entryinfo_unlock( ei2 );
607 ei.bei_id = eip.bei_id;
609 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
615 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
619 struct bdb_info *bdb,
626 /* See if we already have this one */
627 bdb_cache_entryinfo_lock( ei->bei_parent );
628 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
629 bdb_cache_entryinfo_unlock( ei->bei_parent );
632 /* Not found, add it */
635 /* bei_rdn was not malloc'd before, do it now */
636 ber_dupbv( &bv, &ei->bei_rdn );
639 rc = bdb_entryinfo_add_internal( bdb, ei, res );
640 bdb_cache_entryinfo_unlock( ei->bei_parent );
641 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
643 /* Found, return it */
651 /* This is best-effort only. If all entries in the cache are
652 * busy, they will all be kept. This is unlikely to happen
653 * unless the cache is very much smaller than the working set.
656 bdb_cache_lru_purge( struct bdb_info *bdb )
658 DB_LOCK lock, *lockp;
659 EntryInfo *elru, *elnext = NULL;
660 int count, islocked, eimax;
662 /* Wait for the mutex; we're the only one trying to purge. */
663 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
665 if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
666 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
667 bdb->bi_cache.c_purging = 0;
671 if ( bdb->bi_cache.c_txn ) {
679 /* maximum number of EntryInfo leaves to cache. In slapcat
680 * we always free all leaf nodes.
682 if ( slapMode & SLAP_TOOL_READONLY )
685 eimax = bdb->bi_cache.c_eimax;
687 /* Look for an unused entry to remove */
688 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
689 elnext = elru->bei_lrunext;
691 if ( bdb_cache_entryinfo_trylock( elru ))
694 /* This flag implements the clock replacement behavior */
695 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
696 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
697 bdb_cache_entryinfo_unlock( elru );
701 /* If this node is in the process of linking into the cache,
702 * or this node is being deleted, skip it.
704 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
705 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
706 elru->bei_finders > 0 ) {
707 bdb_cache_entryinfo_unlock( elru );
711 /* entryinfo is locked */
714 /* If we can successfully writelock it, then
715 * the object is idle.
717 if ( bdb_cache_entry_db_lock( bdb,
718 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
720 /* Free entry for this node if it's present */
722 elru->bei_e->e_private = NULL;
723 #ifdef SLAP_ZONE_ALLOC
724 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
726 bdb_entry_return( elru->bei_e );
731 bdb_cache_entry_db_unlock( bdb, lockp );
734 * If it is a leaf node, and we're over the limit, free it.
736 if ( elru->bei_kids ) {
737 /* Drop from list, we ignore it... */
738 LRU_DEL( &bdb->bi_cache, elru );
739 } else if ( bdb->bi_cache.c_leaves > eimax ) {
740 /* Too many leaf nodes, free this one */
741 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
742 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
744 } /* Leave on list until we need to free it */
748 bdb_cache_entryinfo_unlock( elru );
750 if ( (unsigned) count >= bdb->bi_cache.c_minfree ) {
751 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
752 bdb->bi_cache.c_cursize -= count;
753 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
757 if ( elnext == bdb->bi_cache.c_lruhead )
761 bdb->bi_cache.c_lruhead = elnext;
762 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
763 bdb->bi_cache.c_purging = 0;
768 struct bdb_info *bdb,
771 EntryInfo ei = { 0 },
776 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
777 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
778 (caddr_t) &ei, bdb_id_cmp );
779 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
784 * cache_find_id - find an entry in the cache, given id.
785 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
786 * the supplied *eip was already locked.
798 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
800 int rc = 0, load = 0;
801 EntryInfo ei = { 0 };
805 #ifdef SLAP_ZONE_ALLOC
806 slap_zh_rlock(bdb->bi_cache.c_zctx);
808 /* If we weren't given any info, see if we have it already cached */
810 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
811 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
812 (caddr_t) &ei, bdb_id_cmp );
814 /* If the lock attempt fails, the info is in use */
815 if ( bdb_cache_entryinfo_trylock( *eip )) {
816 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
817 /* If this node is being deleted, treat
818 * as if the delete has already finished
820 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
823 /* otherwise, wait for the info to free up */
824 ldap_pvt_thread_yield();
827 /* If this info isn't hooked up to its parent yet,
828 * unlock and wait for it to be fully initialized
830 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
831 bdb_cache_entryinfo_unlock( *eip );
832 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
833 ldap_pvt_thread_yield();
838 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
841 /* See if the ID exists in the database; add it to the cache if so */
844 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
846 rc = bdb_cache_find_ndn( op, tid,
848 if ( *eip ) flag |= ID_LOCKED;
850 ep->e_private = NULL;
851 #ifdef SLAP_ZONE_ALLOC
852 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
854 bdb_entry_return( ep );
860 rc = hdb_cache_find_parent(op, tid, id, eip );
861 if ( rc == 0 ) flag |= ID_LOCKED;
865 /* Ok, we found the info, do we have the entry? */
867 if ( !( flag & ID_LOCKED )) {
868 bdb_cache_entryinfo_lock( *eip );
872 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
875 (*eip)->bei_finders++;
876 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
877 /* Make sure only one thread tries to load the entry */
879 #ifdef SLAP_ZONE_ALLOC
880 if ((*eip)->bei_e && !slap_zn_validate(
881 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
882 (*eip)->bei_e = NULL;
883 (*eip)->bei_zseq = 0;
886 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
888 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
892 /* Clear the uncached state if we are not
893 * loading it, i.e it is already cached or
894 * another thread is currently loading it.
896 (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
900 if ( flag & ID_LOCKED ) {
901 bdb_cache_entryinfo_unlock( *eip );
904 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
905 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
907 bdb_cache_entry_db_unlock( bdb, lock );
908 } else if ( rc == 0 ) {
911 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
914 ep->e_private = *eip;
919 #ifdef SLAP_ZONE_ALLOC
920 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
923 bdb_cache_lru_link( bdb, *eip );
924 if (( flag & ID_NOCACHE ) &&
925 ( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
926 /* Set the cached state only if no other thread
927 * found the info while we were loading the entry.
929 if ( (*eip)->bei_finders == 1 )
930 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
931 bdb_cache_entryinfo_unlock( *eip );
935 /* If we succeeded, downgrade back to a readlock. */
936 rc = bdb_cache_entry_db_relock( bdb, tid,
939 /* Otherwise, release the lock. */
940 bdb_cache_entry_db_unlock( bdb, lock );
942 } else if ( !(*eip)->bei_e ) {
943 /* Some other thread is trying to load the entry,
944 * wait for it to finish.
946 bdb_cache_entry_db_unlock( bdb, lock );
947 bdb_cache_entryinfo_lock( *eip );
952 /* Check for subtree renames
954 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
956 bdb_cache_entry_db_relock( bdb,
957 tid, *eip, 1, 0, lock );
958 /* check again in case other modifier did it already */
959 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
960 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
961 bdb_cache_entry_db_relock( bdb,
962 tid, *eip, 0, 0, lock );
967 bdb_cache_entryinfo_lock( *eip );
968 (*eip)->bei_finders--;
970 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
971 bdb_cache_entryinfo_unlock( *eip );
974 if ( flag & ID_LOCKED ) {
975 bdb_cache_entryinfo_unlock( *eip );
978 ep->e_private = NULL;
979 #ifdef SLAP_ZONE_ALLOC
980 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
982 bdb_entry_return( ep );
989 if ( !( flag & ID_NOCACHE )) {
990 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
991 bdb->bi_cache.c_cursize++;
992 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
993 !bdb->bi_cache.c_purging ) {
995 bdb->bi_cache.c_purging = 1;
997 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1001 bdb_cache_lru_purge( bdb );
1004 #ifdef SLAP_ZONE_ALLOC
1005 if (rc == 0 && (*eip)->bei_e) {
1006 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
1008 slap_zh_runlock(bdb->bi_cache.c_zctx);
1021 if ( BEI(e)->bei_kids ) {
1024 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1027 rc = bdb_dn2id_children( op, txn, e );
1028 if ( rc == DB_NOTFOUND ) {
1029 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1034 /* Update the cache after a successful database Add. */
1037 struct bdb_info *bdb,
1040 struct berval *nrdn,
1047 struct berval rdn = e->e_name;
1050 ei.bei_id = e->e_id;
1051 ei.bei_parent = eip;
1052 ei.bei_nrdn = *nrdn;
1055 /* Lock this entry so that bdb_add can run to completion.
1056 * It can only fail if BDB has run out of lock resources.
1058 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1060 bdb_cache_entryinfo_unlock( eip );
1065 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1066 char *ptr = ber_bvchr( &rdn, ',' );
1067 assert( ptr != NULL );
1068 rdn.bv_len = ptr - rdn.bv_val;
1070 ber_dupbv( &ei.bei_rdn, &rdn );
1071 if ( eip->bei_dkids ) eip->bei_dkids++;
1074 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1075 /* bdb_csn_commit can cause this when adding the database root entry */
1077 new->bei_e->e_private = NULL;
1078 #ifdef SLAP_ZONE_ALLOC
1079 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1081 bdb_entry_return( new->bei_e );
1086 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1087 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1088 if (eip->bei_parent) {
1089 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1091 bdb_cache_entryinfo_unlock( eip );
1093 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1094 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1095 ++bdb->bi_cache.c_cursize;
1096 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1097 !bdb->bi_cache.c_purging ) {
1099 bdb->bi_cache.c_purging = 1;
1101 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1103 bdb_cache_lru_link( bdb, new );
1106 bdb_cache_lru_purge( bdb );
1113 struct bdb_info *bdb,
1115 Attribute *newAttrs,
1119 EntryInfo *ei = BEI(e);
1121 /* Get write lock on data */
1122 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1124 /* If we've done repeated mods on a cached entry, then e_attrs
1125 * is no longer contiguous with the entry, and must be freed.
1128 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1129 attrs_free( e->e_attrs );
1131 e->e_attrs = newAttrs;
1137 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1141 struct bdb_info *bdb,
1143 struct berval *nrdn,
1149 EntryInfo *ei = BEI(e), *pei;
1155 /* Get write lock on data */
1156 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1157 if ( rc ) return rc;
1159 /* If we've done repeated mods on a cached entry, then e_attrs
1160 * is no longer contiguous with the entry, and must be freed.
1162 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1163 attrs_free( e->e_attrs );
1165 e->e_attrs = new->e_attrs;
1166 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1167 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1169 ch_free(e->e_name.bv_val);
1170 ch_free(e->e_nname.bv_val);
1172 e->e_name = new->e_name;
1173 e->e_nname = new->e_nname;
1175 /* Lock the parent's kids AVL tree */
1176 pei = ei->bei_parent;
1177 bdb_cache_entryinfo_lock( pei );
1178 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1179 free( ei->bei_nrdn.bv_val );
1180 ber_dupbv( &ei->bei_nrdn, nrdn );
1183 free( ei->bei_rdn.bv_val );
1186 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1187 char *ptr = ber_bvchr(&rdn, ',');
1188 assert( ptr != NULL );
1189 rdn.bv_len = ptr - rdn.bv_val;
1191 ber_dupbv( &ei->bei_rdn, &rdn );
1193 /* If new parent, decrement kid counts */
1196 if ( pei->bei_dkids ) {
1198 if ( pei->bei_dkids < 2 )
1199 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1205 ein = ei->bei_parent;
1207 ei->bei_parent = ein;
1208 bdb_cache_entryinfo_unlock( pei );
1209 bdb_cache_entryinfo_lock( ein );
1211 /* new parent now has kids */
1212 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1213 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1214 /* grandparent has grandkids */
1215 if ( ein->bei_parent )
1216 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1218 /* parent might now have grandkids */
1219 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1220 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1221 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1224 if ( ein->bei_dkids ) ein->bei_dkids++;
1229 /* Record the generation number of this change */
1230 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1232 ei->bei_modrdns = bdb->bi_modrdns;
1233 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1236 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1237 bdb_cache_entryinfo_unlock( ein );
1241 * cache_delete - delete the entry e from the cache.
1243 * returns: 0 e was deleted ok
1244 * 1 e was not in the cache
1245 * -1 something bad happened
1249 struct bdb_info *bdb,
1254 EntryInfo *ei = BEI(e);
1257 assert( e->e_private != NULL );
1259 /* Lock the entry's info */
1260 bdb_cache_entryinfo_lock( ei );
1262 /* Set this early, warn off any queriers */
1263 ei->bei_state |= CACHE_ENTRY_DELETED;
1265 bdb_cache_entryinfo_unlock( ei );
1267 /* Get write lock on the data */
1268 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1270 /* couldn't lock, undo and give up */
1271 ei->bei_state ^= CACHE_ENTRY_DELETED;
1275 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1279 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1281 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1283 /* free lru mutex */
1284 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1290 bdb_cache_delete_cleanup(
1294 /* Enter with ei locked */
1297 ei->bei_e->e_private = NULL;
1298 #ifdef SLAP_ZONE_ALLOC
1299 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1301 bdb_entry_return( ei->bei_e );
1306 bdb_cache_entryinfo_free( cache, ei );
1307 bdb_cache_entryinfo_unlock( ei );
1311 bdb_cache_delete_internal(
1316 int rc = 0; /* return code */
1319 /* Lock the parent's kids tree */
1320 bdb_cache_entryinfo_lock( e->bei_parent );
1323 e->bei_parent->bei_ckids--;
1324 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1327 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1332 if ( e->bei_parent->bei_kids )
1335 bdb_cache_entryinfo_unlock( e->bei_parent );
1337 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1339 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1346 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1350 LRU_DEL( cache, e );
1353 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1355 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1363 bdb_entryinfo_release( void *data )
1365 EntryInfo *ei = (EntryInfo *)data;
1366 if ( ei->bei_kids ) {
1367 avl_free( ei->bei_kids, NULL );
1370 ei->bei_e->e_private = NULL;
1371 #ifdef SLAP_ZONE_ALLOC
1372 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1374 bdb_entry_return( ei->bei_e );
1377 bdb_cache_entryinfo_destroy( ei );
1381 bdb_cache_release_all( Cache *cache )
1383 /* set cache write lock */
1384 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1386 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1388 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1390 avl_free( cache->c_dntree.bei_kids, NULL );
1391 avl_free( cache->c_idtree, bdb_entryinfo_release );
1392 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1393 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1394 bdb_cache_entryinfo_destroy(cache->c_eifree);
1396 cache->c_cursize = 0;
1397 cache->c_eiused = 0;
1398 cache->c_leaves = 0;
1399 cache->c_idtree = NULL;
1400 cache->c_lruhead = NULL;
1401 cache->c_lrutail = NULL;
1402 cache->c_dntree.bei_kids = NULL;
1404 /* free lru mutex */
1405 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1406 /* free cache write lock */
1407 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1413 bdb_lru_print( Cache *cache )
1417 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1418 fprintf( stderr, "LRU circle (tail forward):\n" );
1419 for ( e = cache->c_lrutail; ; ) {
1420 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1421 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1423 if ( e == cache->c_lrutail )
1426 fprintf( stderr, "LRU circle (tail backward):\n" );
1427 for ( e = cache->c_lrutail; ; ) {
1428 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1429 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1431 if ( e == cache->c_lrutail )
1439 bdb_reader_free( void *key, void *data )
1441 /* DB_ENV *env = key; */
1447 /* free up any keys used by the main thread */
1449 bdb_reader_flush( DB_ENV *env )
1452 void *ctx = ldap_pvt_thread_pool_context();
1454 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1455 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1456 bdb_reader_free( env, data );
1461 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1467 if ( !env || !txn ) return -1;
1469 /* If no op was provided, try to find the ctx anyway... */
1471 ctx = op->o_threadctx;
1473 ctx = ldap_pvt_thread_pool_context();
1476 /* Shouldn't happen unless we're single-threaded */
1482 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1483 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1484 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1485 if (rc) ldap_pvt_thread_yield();
1491 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1492 data, bdb_reader_free, NULL, NULL ) ) ) {
1494 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1495 db_strerror(rc), rc, 0 );