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;
97 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
98 ei->bei_lrunext = cache->c_eifree;
100 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
103 #define LRU_DEL( c, e ) do { \
104 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
105 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
106 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
107 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
108 e->bei_lruprev = NULL; \
111 /* Note - we now use a Second-Chance / Clock algorithm instead of
112 * Least-Recently-Used. This tremendously improves concurrency
113 * because we no longer need to manipulate the lists every time an
114 * entry is touched. We only need to lock the lists when adding
115 * or deleting an entry. It's now a circular doubly-linked list.
116 * We always append to the tail, but the head traverses the circle
117 * during a purge operation.
120 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
123 /* Already linked, ignore */
124 if ( ei->bei_lruprev )
127 /* Insert into circular LRU list */
128 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
130 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
131 if ( bdb->bi_cache.c_lrutail ) {
132 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
133 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
134 if ( ei->bei_lrunext )
135 ei->bei_lrunext->bei_lruprev = ei;
137 ei->bei_lrunext = ei->bei_lruprev = ei;
138 bdb->bi_cache.c_lruhead = ei;
140 bdb->bi_cache.c_lrutail = ei;
141 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
148 /* #define NO_DB_LOCK 1 */
149 /* Note: The BerkeleyDB locks are much slower than regular
150 * mutexes or rdwr locks. But the BDB implementation has the
151 * advantage of using a fixed size lock table, instead of
152 * allocating a lock object per entry in the DB. That's a
153 * key benefit for scaling. It also frees us from worrying
154 * about undetectable deadlocks between BDB activity and our
155 * own cache activity. It's still worth exploring faster
156 * alternatives though.
159 /* Atomically release and reacquire a lock */
161 bdb_cache_entry_db_relock(
162 struct bdb_info *bdb,
176 if ( !lock ) return 0;
178 lockobj.data = &ei->bei_id;
179 lockobj.size = sizeof(ei->bei_id) + 1;
181 list[0].op = DB_LOCK_PUT;
182 list[0].lock = *lock;
183 list[1].op = DB_LOCK_GET;
184 list[1].lock = *lock;
185 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
186 list[1].obj = &lockobj;
187 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
190 if (rc && !tryOnly) {
191 Debug( LDAP_DEBUG_TRACE,
192 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
193 ei->bei_id, rw, rc );
195 *lock = list[1].lock;
202 bdb_cache_entry_db_lock( struct bdb_info *bdb, DB_TXN *txn, EntryInfo *ei,
203 int rw, int tryOnly, DB_LOCK *lock )
212 if ( !lock ) return 0;
215 db_rw = DB_LOCK_WRITE;
217 db_rw = DB_LOCK_READ;
219 lockobj.data = &ei->bei_id;
220 lockobj.size = sizeof(ei->bei_id) + 1;
222 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
223 &lockobj, db_rw, lock);
224 if (rc && !tryOnly) {
225 Debug( LDAP_DEBUG_TRACE,
226 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
227 ei->bei_id, rw, rc );
230 #endif /* NO_DB_LOCK */
234 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
241 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
243 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
249 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
250 int rw, DB_LOCK *lock )
257 ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
258 ( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
259 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
260 /* Releasing the entry can only be done when
261 * we know that nobody else is using it, i.e we
262 * should have an entry_db writelock. But the
263 * flag is only set by the thread that loads the
264 * entry, and only if no other threads has found
265 * it while it was working. All other threads
266 * clear the flag, which mean that we should be
267 * the only thread using the entry if the flag
271 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
274 bdb_cache_entryinfo_unlock( ei );
276 bdb_cache_entry_db_unlock( bdb, lock );
279 bdb_entry_return( e );
284 bdb_cache_entryinfo_destroy( EntryInfo *e )
286 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
287 free( e->bei_nrdn.bv_val );
289 free( e->bei_rdn.bv_val );
295 /* Do a length-ordered sort on normalized RDNs */
297 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
299 const EntryInfo *e1 = v_e1, *e2 = v_e2;
300 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
302 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
303 e1->bei_nrdn.bv_len );
309 bdb_id_cmp( const void *v_e1, const void *v_e2 )
311 const EntryInfo *e1 = v_e1, *e2 = v_e2;
312 return e1->bei_id - e2->bei_id;
316 bdb_id_dup_err( void *v1, void *v2 )
319 e2->bei_lrunext = v1;
323 /* Create an entryinfo in the cache. Caller must release the locks later.
326 bdb_entryinfo_add_internal(
327 struct bdb_info *bdb,
331 EntryInfo *ei2 = NULL;
335 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
337 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
338 bdb_cache_entryinfo_lock( ei->bei_parent );
340 ei2->bei_id = ei->bei_id;
341 ei2->bei_parent = ei->bei_parent;
343 ei2->bei_rdn = ei->bei_rdn;
345 #ifdef SLAP_ZONE_ALLOC
349 /* Add to cache ID tree */
350 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
352 EntryInfo *eix = ei2->bei_lrunext;
353 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
356 /* It got freed above because its value was
359 ei->bei_rdn.bv_val = NULL;
364 bdb->bi_cache.c_eiused++;
365 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
367 /* This is a new leaf node. But if parent had no kids, then it was
368 * a leaf and we would be decrementing that. So, only increment if
369 * the parent already has kids.
371 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
372 bdb->bi_cache.c_leaves++;
373 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
376 /* This should never happen; entry cache is corrupt */
377 bdb->bi_dbenv->log_flush( bdb->bi_dbenv, NULL );
381 ei->bei_parent->bei_ckids++;
389 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
390 * the info for its closest ancestor. *res should be NULL to process a
391 * complete DN starting from the tree root. Otherwise *res must be the
392 * immediate parent of the requested DN, and only the RDN will be searched.
393 * The EntryInfo is locked upon return and must be unlocked by the caller.
402 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
403 EntryInfo ei, *eip, *ei2;
407 /* this function is always called with normalized DN */
409 /* we're doing a onelevel search for an RDN */
410 ei.bei_nrdn.bv_val = ndn->bv_val;
411 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
414 /* we're searching a full DN from the root */
415 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
416 ei.bei_nrdn.bv_val = ptr;
417 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
418 /* Skip to next rdn if suffix is empty */
419 if ( ei.bei_nrdn.bv_len == 0 ) {
420 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
421 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
422 if ( ptr >= ndn->bv_val ) {
423 if (DN_SEPARATOR(*ptr)) ptr++;
424 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
425 ei.bei_nrdn.bv_val = ptr;
428 eip = &bdb->bi_cache.c_dntree;
431 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
432 eip->bei_state |= CACHE_ENTRY_REFERENCED;
434 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
437 int len = ei.bei_nrdn.bv_len;
439 if ( BER_BVISEMPTY( ndn )) {
444 ei.bei_nrdn.bv_len = ndn->bv_len -
445 (ei.bei_nrdn.bv_val - ndn->bv_val);
446 bdb_cache_entryinfo_unlock( eip );
448 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
449 ei.bei_nrdn.bv_val );
451 lock.mode = DB_LOCK_NG;
452 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &lock );
454 bdb_cache_entryinfo_lock( eip );
455 bdb_cache_entry_db_unlock( bdb, &lock );
460 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
461 ei.bei_nrdn.bv_val, ei.bei_id );
463 /* DN exists but needs to be added to cache */
464 ei.bei_nrdn.bv_len = len;
465 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
466 /* add_internal left eip and c_rwlock locked */
467 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
468 bdb_cache_entry_db_unlock( bdb, &lock );
473 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
474 /* In the midst of deleting? Give it a chance to
477 bdb_cache_entryinfo_unlock( eip );
478 ldap_pvt_thread_yield();
479 bdb_cache_entryinfo_lock( eip );
483 bdb_cache_entryinfo_unlock( eip );
484 bdb_cache_entryinfo_lock( ei2 );
488 /* Advance to next lower RDN */
489 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
490 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
491 if ( ptr >= ndn->bv_val ) {
492 if (DN_SEPARATOR(*ptr)) ptr++;
493 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
494 ei.bei_nrdn.bv_val = ptr;
496 if ( ptr < ndn->bv_val ) {
506 /* Walk up the tree from a child node, looking for an ID that's already
507 * been linked into the cache.
510 hdb_cache_find_parent(
516 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
517 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
525 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
528 /* Save the previous node, if any */
531 /* Create a new node for the current ID */
532 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
533 ein->bei_id = ei.bei_id;
534 ein->bei_kids = ei.bei_kids;
535 ein->bei_nrdn = ei.bei_nrdn;
536 ein->bei_rdn = ei.bei_rdn;
537 ein->bei_ckids = ei.bei_ckids;
538 #ifdef SLAP_ZONE_ALLOC
543 /* This node is not fully connected yet */
544 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
546 /* Insert this node into the ID tree */
547 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
548 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
549 bdb_id_cmp, bdb_id_dup_err ) ) {
550 EntryInfo *eix = ein->bei_lrunext;
552 /* Someone else created this node just before us.
553 * Free our new copy and use the existing one.
555 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
558 /* Link in any kids we've already processed */
560 bdb_cache_entryinfo_lock( ein );
561 avl_insert( &ein->bei_kids, (caddr_t)ei2,
562 bdb_rdn_cmp, avl_dup_error );
564 bdb_cache_entryinfo_unlock( ein );
568 /* If this is the first time, save this node
569 * to be returned later.
571 if ( eir == NULL ) eir = ein;
573 /* If there was a previous node, link it to this one */
574 if ( ei2 ) ei2->bei_parent = ein;
576 /* Look for this node's parent */
578 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
579 (caddr_t) &eip, bdb_id_cmp );
581 ei2 = &bdb->bi_cache.c_dntree;
583 bdb->bi_cache.c_eiused++;
584 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
585 bdb->bi_cache.c_leaves++;
586 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
588 /* Got the parent, link in and we're done. */
590 bdb_cache_entryinfo_lock( eir );
591 bdb_cache_entryinfo_lock( ei2 );
592 ein->bei_parent = ei2;
594 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
598 /* Reset all the state info */
599 for (ein = eir; ein != ei2; ein=ein->bei_parent)
600 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
602 bdb_cache_entryinfo_unlock( ei2 );
608 ei.bei_id = eip.bei_id;
610 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
616 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
620 struct bdb_info *bdb,
627 /* See if we already have this one */
628 bdb_cache_entryinfo_lock( ei->bei_parent );
629 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
630 bdb_cache_entryinfo_unlock( ei->bei_parent );
633 /* Not found, add it */
636 /* bei_rdn was not malloc'd before, do it now */
637 ber_dupbv( &bv, &ei->bei_rdn );
640 rc = bdb_entryinfo_add_internal( bdb, ei, res );
641 bdb_cache_entryinfo_unlock( ei->bei_parent );
642 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
644 /* Found, return it */
652 /* This is best-effort only. If all entries in the cache are
653 * busy, they will all be kept. This is unlikely to happen
654 * unless the cache is very much smaller than the working set.
657 bdb_cache_lru_purge( struct bdb_info *bdb )
659 DB_LOCK lock, *lockp;
660 EntryInfo *elru, *elnext = NULL;
661 int count, islocked, eimax;
662 int efree = 0, eifree = 0, eicount, ecount;
667 /* Wait for the mutex; we're the only one trying to purge. */
668 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
670 /* maximum number of EntryInfo leaves to cache. In slapcat
671 * we always free all leaf nodes.
673 if ( slapMode & SLAP_TOOL_READONLY )
676 eimax = bdb->bi_cache.c_eimax;
678 efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
682 efree += bdb->bi_cache.c_minfree;
684 if ( bdb->bi_cache.c_leaves > eimax ) {
685 eifree = bdb->bi_cache.c_minfree * 10;
686 if ( eifree >= eimax )
690 if ( !efree && !eifree ) {
691 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
692 bdb->bi_cache.c_purging = 0;
696 if ( bdb->bi_cache.c_txn ) {
709 /* Look for an unused entry to remove */
710 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
711 elnext = elru->bei_lrunext;
713 if ( bdb_cache_entryinfo_trylock( elru ))
716 /* This flag implements the clock replacement behavior */
717 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
718 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
719 bdb_cache_entryinfo_unlock( elru );
723 /* If this node is in the process of linking into the cache,
724 * or this node is being deleted, skip it.
726 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
727 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
728 elru->bei_finders > 0 ) {
729 bdb_cache_entryinfo_unlock( elru );
733 /* entryinfo is locked */
736 /* If we can successfully writelock it, then
737 * the object is idle.
739 if ( bdb_cache_entry_db_lock( bdb,
740 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
742 /* Free entry for this node if it's present */
746 /* the cache may have gone over the limit while we
747 * weren't looking, so double check.
749 if ( !efree && ecount > bdb->bi_cache.c_maxsize )
750 efree = bdb->bi_cache.c_minfree;
752 if ( count < efree ) {
753 elru->bei_e->e_private = NULL;
754 #ifdef SLAP_ZONE_ALLOC
755 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
757 bdb_entry_return( elru->bei_e );
762 /* Keep this node cached, skip to next */
763 bdb_cache_entry_db_unlock( bdb, lockp );
767 bdb_cache_entry_db_unlock( bdb, lockp );
770 * If it is a leaf node, and we're over the limit, free it.
772 if ( elru->bei_kids ) {
773 /* Drop from list, we ignore it... */
774 LRU_DEL( &bdb->bi_cache, elru );
775 } else if ( eicount < eifree ) {
776 /* Too many leaf nodes, free this one */
777 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
778 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
781 } /* Leave on list until we need to free it */
786 bdb_cache_entryinfo_unlock( elru );
788 if ( count >= efree && eicount >= eifree ) {
789 if ( count || ecount > bdb->bi_cache.c_cursize ) {
790 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
791 /* HACK: we seem to be losing track, fix up now */
792 if ( ecount > bdb->bi_cache.c_cursize )
793 bdb->bi_cache.c_cursize = ecount;
794 bdb->bi_cache.c_cursize -= count;
795 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
800 if ( elnext == bdb->bi_cache.c_lruhead )
807 bdb->bi_cache.c_lruhead = elnext;
808 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
809 bdb->bi_cache.c_purging = 0;
814 struct bdb_info *bdb,
817 EntryInfo ei = { 0 },
822 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
823 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
824 (caddr_t) &ei, bdb_id_cmp );
825 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
830 * cache_find_id - find an entry in the cache, given id.
831 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
832 * the supplied *eip was already locked.
844 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
846 int rc = 0, load = 0;
847 EntryInfo ei = { 0 };
851 #ifdef SLAP_ZONE_ALLOC
852 slap_zh_rlock(bdb->bi_cache.c_zctx);
854 /* If we weren't given any info, see if we have it already cached */
856 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
857 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
858 (caddr_t) &ei, bdb_id_cmp );
860 /* If the lock attempt fails, the info is in use */
861 if ( bdb_cache_entryinfo_trylock( *eip )) {
862 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
863 /* If this node is being deleted, treat
864 * as if the delete has already finished
866 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
869 /* otherwise, wait for the info to free up */
870 ldap_pvt_thread_yield();
873 /* If this info isn't hooked up to its parent yet,
874 * unlock and wait for it to be fully initialized
876 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
877 bdb_cache_entryinfo_unlock( *eip );
878 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
879 ldap_pvt_thread_yield();
884 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
887 /* See if the ID exists in the database; add it to the cache if so */
890 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
892 rc = bdb_cache_find_ndn( op, tid,
894 if ( *eip ) flag |= ID_LOCKED;
896 ep->e_private = NULL;
897 #ifdef SLAP_ZONE_ALLOC
898 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
900 bdb_entry_return( ep );
906 rc = hdb_cache_find_parent(op, tid, id, eip );
907 if ( rc == 0 ) flag |= ID_LOCKED;
911 /* Ok, we found the info, do we have the entry? */
913 if ( !( flag & ID_LOCKED )) {
914 bdb_cache_entryinfo_lock( *eip );
918 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
921 (*eip)->bei_finders++;
922 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
923 /* Make sure only one thread tries to load the entry */
925 #ifdef SLAP_ZONE_ALLOC
926 if ((*eip)->bei_e && !slap_zn_validate(
927 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
928 (*eip)->bei_e = NULL;
929 (*eip)->bei_zseq = 0;
932 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
934 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
938 /* Clear the uncached state if we are not
939 * loading it, i.e it is already cached or
940 * another thread is currently loading it.
942 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
943 (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
948 if ( flag & ID_LOCKED ) {
949 bdb_cache_entryinfo_unlock( *eip );
952 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
953 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
955 bdb_cache_entry_db_unlock( bdb, lock );
956 } else if ( rc == 0 ) {
959 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
962 ep->e_private = *eip;
967 #ifdef SLAP_ZONE_ALLOC
968 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
971 bdb_cache_lru_link( bdb, *eip );
972 if (( flag & ID_NOCACHE ) &&
973 ( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
974 /* Set the cached state only if no other thread
975 * found the info while we were loading the entry.
977 if ( (*eip)->bei_finders == 1 )
978 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
979 bdb_cache_entryinfo_unlock( *eip );
983 /* If we succeeded, downgrade back to a readlock. */
984 rc = bdb_cache_entry_db_relock( bdb, tid,
987 /* Otherwise, release the lock. */
988 bdb_cache_entry_db_unlock( bdb, lock );
990 } else if ( !(*eip)->bei_e ) {
991 /* Some other thread is trying to load the entry,
992 * wait for it to finish.
994 bdb_cache_entry_db_unlock( bdb, lock );
995 bdb_cache_entryinfo_lock( *eip );
1000 /* Check for subtree renames
1002 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
1004 bdb_cache_entry_db_relock( bdb,
1005 tid, *eip, 1, 0, lock );
1006 /* check again in case other modifier did it already */
1007 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
1008 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
1009 bdb_cache_entry_db_relock( bdb,
1010 tid, *eip, 0, 0, lock );
1015 bdb_cache_entryinfo_lock( *eip );
1016 (*eip)->bei_finders--;
1018 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
1019 bdb_cache_entryinfo_unlock( *eip );
1022 if ( flag & ID_LOCKED ) {
1023 bdb_cache_entryinfo_unlock( *eip );
1026 ep->e_private = NULL;
1027 #ifdef SLAP_ZONE_ALLOC
1028 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
1030 bdb_entry_return( ep );
1036 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ||
1037 bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1038 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1039 if ( !bdb->bi_cache.c_purging ) {
1040 if ( !( flag & ID_NOCACHE )) {
1041 bdb->bi_cache.c_cursize++;
1042 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
1044 bdb->bi_cache.c_purging = 1;
1046 } else if ( bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1048 bdb->bi_cache.c_purging = 1;
1051 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1054 bdb_cache_lru_purge( bdb );
1057 #ifdef SLAP_ZONE_ALLOC
1058 if (rc == 0 && (*eip)->bei_e) {
1059 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
1061 slap_zh_runlock(bdb->bi_cache.c_zctx);
1074 if ( BEI(e)->bei_kids ) {
1077 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1080 rc = bdb_dn2id_children( op, txn, e );
1081 if ( rc == DB_NOTFOUND ) {
1082 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1087 /* Update the cache after a successful database Add. */
1090 struct bdb_info *bdb,
1093 struct berval *nrdn,
1100 struct berval rdn = e->e_name;
1103 ei.bei_id = e->e_id;
1104 ei.bei_parent = eip;
1105 ei.bei_nrdn = *nrdn;
1108 /* Lock this entry so that bdb_add can run to completion.
1109 * It can only fail if BDB has run out of lock resources.
1111 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1113 bdb_cache_entryinfo_unlock( eip );
1118 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1119 char *ptr = ber_bvchr( &rdn, ',' );
1120 assert( ptr != NULL );
1121 rdn.bv_len = ptr - rdn.bv_val;
1123 ber_dupbv( &ei.bei_rdn, &rdn );
1124 if ( eip->bei_dkids ) eip->bei_dkids++;
1127 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1128 /* bdb_csn_commit can cause this when adding the database root entry */
1130 new->bei_e->e_private = NULL;
1131 #ifdef SLAP_ZONE_ALLOC
1132 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1134 bdb_entry_return( new->bei_e );
1139 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1140 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1141 if (eip->bei_parent) {
1142 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1144 bdb_cache_entryinfo_unlock( eip );
1146 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1147 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1148 ++bdb->bi_cache.c_cursize;
1149 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1150 !bdb->bi_cache.c_purging ) {
1152 bdb->bi_cache.c_purging = 1;
1154 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1156 bdb_cache_lru_link( bdb, new );
1159 bdb_cache_lru_purge( bdb );
1166 struct bdb_info *bdb,
1168 Attribute *newAttrs,
1172 EntryInfo *ei = BEI(e);
1174 /* Get write lock on data */
1175 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1177 /* If we've done repeated mods on a cached entry, then e_attrs
1178 * is no longer contiguous with the entry, and must be freed.
1181 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1182 attrs_free( e->e_attrs );
1184 e->e_attrs = newAttrs;
1190 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1194 struct bdb_info *bdb,
1196 struct berval *nrdn,
1202 EntryInfo *ei = BEI(e), *pei;
1208 /* Get write lock on data */
1209 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1210 if ( rc ) return rc;
1212 /* If we've done repeated mods on a cached entry, then e_attrs
1213 * is no longer contiguous with the entry, and must be freed.
1215 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1216 attrs_free( e->e_attrs );
1218 e->e_attrs = new->e_attrs;
1219 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1220 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1222 ch_free(e->e_name.bv_val);
1223 ch_free(e->e_nname.bv_val);
1225 e->e_name = new->e_name;
1226 e->e_nname = new->e_nname;
1228 /* Lock the parent's kids AVL tree */
1229 pei = ei->bei_parent;
1230 bdb_cache_entryinfo_lock( pei );
1231 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1232 free( ei->bei_nrdn.bv_val );
1233 ber_dupbv( &ei->bei_nrdn, nrdn );
1236 free( ei->bei_rdn.bv_val );
1239 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1240 char *ptr = ber_bvchr(&rdn, ',');
1241 assert( ptr != NULL );
1242 rdn.bv_len = ptr - rdn.bv_val;
1244 ber_dupbv( &ei->bei_rdn, &rdn );
1246 /* If new parent, decrement kid counts */
1249 if ( pei->bei_dkids ) {
1251 if ( pei->bei_dkids < 2 )
1252 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1258 ein = ei->bei_parent;
1260 ei->bei_parent = ein;
1261 bdb_cache_entryinfo_unlock( pei );
1262 bdb_cache_entryinfo_lock( ein );
1264 /* new parent now has kids */
1265 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1266 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1267 /* grandparent has grandkids */
1268 if ( ein->bei_parent )
1269 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1271 /* parent might now have grandkids */
1272 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1273 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1274 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1277 if ( ein->bei_dkids ) ein->bei_dkids++;
1282 /* Record the generation number of this change */
1283 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1285 ei->bei_modrdns = bdb->bi_modrdns;
1286 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1289 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1290 bdb_cache_entryinfo_unlock( ein );
1294 * cache_delete - delete the entry e from the cache.
1296 * returns: 0 e was deleted ok
1297 * 1 e was not in the cache
1298 * -1 something bad happened
1302 struct bdb_info *bdb,
1307 EntryInfo *ei = BEI(e);
1310 assert( e->e_private != NULL );
1312 /* Lock the entry's info */
1313 bdb_cache_entryinfo_lock( ei );
1315 /* Set this early, warn off any queriers */
1316 ei->bei_state |= CACHE_ENTRY_DELETED;
1318 bdb_cache_entryinfo_unlock( ei );
1320 /* Get write lock on the data */
1321 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1323 /* couldn't lock, undo and give up */
1324 ei->bei_state ^= CACHE_ENTRY_DELETED;
1328 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1332 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1334 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1336 /* free lru mutex */
1337 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1343 bdb_cache_delete_cleanup(
1347 /* Enter with ei locked */
1349 /* already freed? */
1350 if ( !ei->bei_parent ) return;
1353 ei->bei_e->e_private = NULL;
1354 #ifdef SLAP_ZONE_ALLOC
1355 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1357 bdb_entry_return( ei->bei_e );
1362 bdb_cache_entryinfo_unlock( ei );
1363 bdb_cache_entryinfo_free( cache, ei );
1367 bdb_cache_delete_internal(
1372 int rc = 0; /* return code */
1375 /* already freed? */
1376 if ( !e->bei_parent )
1379 /* Lock the parent's kids tree */
1380 bdb_cache_entryinfo_lock( e->bei_parent );
1383 e->bei_parent->bei_ckids--;
1384 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1387 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1392 if ( e->bei_parent->bei_kids )
1395 bdb_cache_entryinfo_unlock( e->bei_parent );
1397 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1399 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1406 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1410 LRU_DEL( cache, e );
1413 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1415 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1423 bdb_entryinfo_release( void *data )
1425 EntryInfo *ei = (EntryInfo *)data;
1426 if ( ei->bei_kids ) {
1427 avl_free( ei->bei_kids, NULL );
1430 ei->bei_e->e_private = NULL;
1431 #ifdef SLAP_ZONE_ALLOC
1432 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1434 bdb_entry_return( ei->bei_e );
1437 bdb_cache_entryinfo_destroy( ei );
1441 bdb_cache_release_all( Cache *cache )
1443 /* set cache write lock */
1444 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1446 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1448 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1450 avl_free( cache->c_dntree.bei_kids, NULL );
1451 avl_free( cache->c_idtree, bdb_entryinfo_release );
1452 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1453 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1454 bdb_cache_entryinfo_destroy(cache->c_eifree);
1456 cache->c_cursize = 0;
1457 cache->c_eiused = 0;
1458 cache->c_leaves = 0;
1459 cache->c_idtree = NULL;
1460 cache->c_lruhead = NULL;
1461 cache->c_lrutail = NULL;
1462 cache->c_dntree.bei_kids = NULL;
1464 /* free lru mutex */
1465 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1466 /* free cache write lock */
1467 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1473 bdb_lru_print( Cache *cache )
1477 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1478 fprintf( stderr, "LRU circle (tail forward):\n" );
1479 for ( e = cache->c_lrutail; ; ) {
1480 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1481 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1483 if ( e == cache->c_lrutail )
1486 fprintf( stderr, "LRU circle (tail backward):\n" );
1487 for ( e = cache->c_lrutail; ; ) {
1488 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1489 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1491 if ( e == cache->c_lrutail )
1499 bdb_reader_free( void *key, void *data )
1501 /* DB_ENV *env = key; */
1504 if ( txn ) TXN_ABORT( txn );
1507 /* free up any keys used by the main thread */
1509 bdb_reader_flush( DB_ENV *env )
1512 void *ctx = ldap_pvt_thread_pool_context();
1514 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1515 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1516 bdb_reader_free( env, data );
1521 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1527 if ( !env || !txn ) return -1;
1529 /* If no op was provided, try to find the ctx anyway... */
1531 ctx = op->o_threadctx;
1533 ctx = ldap_pvt_thread_pool_context();
1536 /* Shouldn't happen unless we're single-threaded */
1542 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1543 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1544 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1545 if (rc) ldap_pvt_thread_yield();
1551 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1552 data, bdb_reader_free, NULL, NULL ) ) ) {
1554 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1555 db_strerror(rc), rc, 0 );