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-2010 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);
41 static void bdb_idtree_print(Cache *cache);
45 /* For concurrency experiments only! */
47 #define ldap_pvt_thread_rdwr_wlock(a) 0
48 #define ldap_pvt_thread_rdwr_wunlock(a) 0
49 #define ldap_pvt_thread_rdwr_rlock(a) 0
50 #define ldap_pvt_thread_rdwr_runlock(a) 0
54 #define ldap_pvt_thread_mutex_trylock(a) 0
58 bdb_cache_entryinfo_new( Cache *cache )
62 if ( cache->c_eifree ) {
63 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
64 if ( cache->c_eifree ) {
66 cache->c_eifree = ei->bei_lrunext;
68 ei->bei_lrunext = NULL;
70 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
73 ei = ch_calloc(1, sizeof(EntryInfo));
74 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
77 ei->bei_state = CACHE_ENTRY_REFERENCED;
83 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
85 free( ei->bei_nrdn.bv_val );
86 BER_BVZERO( &ei->bei_nrdn );
88 free( ei->bei_rdn.bv_val );
89 BER_BVZERO( &ei->bei_rdn );
94 ei->bei_parent = NULL;
96 ei->bei_lruprev = NULL;
99 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
100 ei->bei_lrunext = cache->c_eifree;
101 cache->c_eifree = ei;
102 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
104 ldap_pvt_thread_mutex_destroy( &ei->bei_kids_mutex );
109 #define LRU_DEL( c, e ) do { \
110 if ( e == e->bei_lruprev ) { \
111 (c)->c_lruhead = (c)->c_lrutail = NULL; \
113 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
114 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
115 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
116 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
118 e->bei_lruprev = NULL; \
121 /* Note - we now use a Second-Chance / Clock algorithm instead of
122 * Least-Recently-Used. This tremendously improves concurrency
123 * because we no longer need to manipulate the lists every time an
124 * entry is touched. We only need to lock the lists when adding
125 * or deleting an entry. It's now a circular doubly-linked list.
126 * We always append to the tail, but the head traverses the circle
127 * during a purge operation.
130 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
133 /* Already linked, ignore */
134 if ( ei->bei_lruprev )
137 /* Insert into circular LRU list */
138 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
140 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
141 if ( bdb->bi_cache.c_lrutail ) {
142 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
143 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
144 if ( ei->bei_lrunext )
145 ei->bei_lrunext->bei_lruprev = ei;
147 ei->bei_lrunext = ei->bei_lruprev = ei;
148 bdb->bi_cache.c_lruhead = ei;
150 bdb->bi_cache.c_lrutail = ei;
151 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
158 /* #define NO_DB_LOCK 1 */
159 /* Note: The BerkeleyDB locks are much slower than regular
160 * mutexes or rdwr locks. But the BDB implementation has the
161 * advantage of using a fixed size lock table, instead of
162 * allocating a lock object per entry in the DB. That's a
163 * key benefit for scaling. It also frees us from worrying
164 * about undetectable deadlocks between BDB activity and our
165 * own cache activity. It's still worth exploring faster
166 * alternatives though.
169 /* Atomically release and reacquire a lock */
171 bdb_cache_entry_db_relock(
172 struct bdb_info *bdb,
186 if ( !lock ) return 0;
189 lockobj.data = &ei->bei_id;
190 lockobj.size = sizeof(ei->bei_id) + 1;
192 list[0].op = DB_LOCK_PUT;
193 list[0].lock = *lock;
194 list[1].op = DB_LOCK_GET;
195 list[1].lock = *lock;
196 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
197 list[1].obj = &lockobj;
198 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
201 if (rc && !tryOnly) {
202 Debug( LDAP_DEBUG_TRACE,
203 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
204 ei->bei_id, rw, rc );
206 *lock = list[1].lock;
213 bdb_cache_entry_db_lock( struct bdb_info *bdb, DB_TXN *txn, EntryInfo *ei,
214 int rw, int tryOnly, DB_LOCK *lock )
223 if ( !lock ) return 0;
226 db_rw = DB_LOCK_WRITE;
228 db_rw = DB_LOCK_READ;
231 lockobj.data = &ei->bei_id;
232 lockobj.size = sizeof(ei->bei_id) + 1;
234 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
235 &lockobj, db_rw, lock);
236 if (rc && !tryOnly) {
237 Debug( LDAP_DEBUG_TRACE,
238 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
239 ei->bei_id, rw, rc );
242 #endif /* NO_DB_LOCK */
246 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
253 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
255 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
261 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
262 int rw, DB_LOCK *lock )
269 ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
270 ( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
271 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
272 /* Releasing the entry can only be done when
273 * we know that nobody else is using it, i.e we
274 * should have an entry_db writelock. But the
275 * flag is only set by the thread that loads the
276 * entry, and only if no other threads has found
277 * it while it was working. All other threads
278 * clear the flag, which mean that we should be
279 * the only thread using the entry if the flag
283 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
286 bdb_cache_entryinfo_unlock( ei );
288 bdb_cache_entry_db_unlock( bdb, lock );
291 bdb_entry_return( e );
296 bdb_cache_entryinfo_destroy( EntryInfo *e )
298 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
299 free( e->bei_nrdn.bv_val );
301 free( e->bei_rdn.bv_val );
307 /* Do a length-ordered sort on normalized RDNs */
309 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
311 const EntryInfo *e1 = v_e1, *e2 = v_e2;
312 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
314 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
315 e1->bei_nrdn.bv_len );
321 bdb_id_cmp( const void *v_e1, const void *v_e2 )
323 const EntryInfo *e1 = v_e1, *e2 = v_e2;
324 return e1->bei_id - e2->bei_id;
328 bdb_id_dup_err( void *v1, void *v2 )
331 e2->bei_lrunext = v1;
335 /* Create an entryinfo in the cache. Caller must release the locks later.
338 bdb_entryinfo_add_internal(
339 struct bdb_info *bdb,
343 EntryInfo *ei2 = NULL;
347 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
349 bdb_cache_entryinfo_lock( ei->bei_parent );
350 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
352 ei2->bei_id = ei->bei_id;
353 ei2->bei_parent = ei->bei_parent;
355 ei2->bei_rdn = ei->bei_rdn;
357 #ifdef SLAP_ZONE_ALLOC
361 /* Add to cache ID tree */
362 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
364 EntryInfo *eix = ei2->bei_lrunext;
365 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
368 /* It got freed above because its value was
371 ei->bei_rdn.bv_val = NULL;
376 bdb->bi_cache.c_eiused++;
377 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
379 /* This is a new leaf node. But if parent had no kids, then it was
380 * a leaf and we would be decrementing that. So, only increment if
381 * the parent already has kids.
383 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
384 bdb->bi_cache.c_leaves++;
385 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
388 /* it's possible for hdb_cache_find_parent to beat us to it */
390 ei->bei_parent->bei_ckids++;
399 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
400 * the info for its closest ancestor. *res should be NULL to process a
401 * complete DN starting from the tree root. Otherwise *res must be the
402 * immediate parent of the requested DN, and only the RDN will be searched.
403 * The EntryInfo is locked upon return and must be unlocked by the caller.
412 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
413 EntryInfo ei, *eip, *ei2;
417 /* this function is always called with normalized DN */
419 /* we're doing a onelevel search for an RDN */
420 ei.bei_nrdn.bv_val = ndn->bv_val;
421 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
424 /* we're searching a full DN from the root */
425 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
426 ei.bei_nrdn.bv_val = ptr;
427 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
428 /* Skip to next rdn if suffix is empty */
429 if ( ei.bei_nrdn.bv_len == 0 ) {
430 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
431 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
432 if ( ptr >= ndn->bv_val ) {
433 if (DN_SEPARATOR(*ptr)) ptr++;
434 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
435 ei.bei_nrdn.bv_val = ptr;
438 eip = &bdb->bi_cache.c_dntree;
441 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
442 eip->bei_state |= CACHE_ENTRY_REFERENCED;
444 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
447 int len = ei.bei_nrdn.bv_len;
449 if ( BER_BVISEMPTY( ndn )) {
454 ei.bei_nrdn.bv_len = ndn->bv_len -
455 (ei.bei_nrdn.bv_val - ndn->bv_val);
457 bdb_cache_entryinfo_unlock( eip );
459 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
460 ei.bei_nrdn.bv_val );
463 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &cursor );
465 bdb_cache_entryinfo_lock( eip );
467 if ( cursor ) cursor->c_close( cursor );
472 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
473 ei.bei_nrdn.bv_val, ei.bei_id );
475 /* DN exists but needs to be added to cache */
476 ei.bei_nrdn.bv_len = len;
477 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
478 /* add_internal left eip and c_rwlock locked */
480 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
481 if ( cursor ) cursor->c_close( cursor );
487 bdb_cache_entryinfo_lock( ei2 );
488 if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
489 /* In the midst of deleting? Give it a chance to
492 bdb_cache_entryinfo_unlock( ei2 );
493 bdb_cache_entryinfo_unlock( eip );
494 ldap_pvt_thread_yield();
495 bdb_cache_entryinfo_lock( eip );
499 bdb_cache_entryinfo_unlock( eip );
503 /* Advance to next lower RDN */
504 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
505 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
506 if ( ptr >= ndn->bv_val ) {
507 if (DN_SEPARATOR(*ptr)) ptr++;
508 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
509 ei.bei_nrdn.bv_val = ptr;
511 if ( ptr < ndn->bv_val ) {
521 /* Walk up the tree from a child node, looking for an ID that's already
522 * been linked into the cache.
525 hdb_cache_find_parent(
531 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
532 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
540 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
543 /* Save the previous node, if any */
546 /* Create a new node for the current ID */
547 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
548 ein->bei_id = ei.bei_id;
549 ein->bei_kids = ei.bei_kids;
550 ein->bei_nrdn = ei.bei_nrdn;
551 ein->bei_rdn = ei.bei_rdn;
552 ein->bei_ckids = ei.bei_ckids;
553 #ifdef SLAP_ZONE_ALLOC
559 /* This node is not fully connected yet */
560 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
562 /* If this is the first time, save this node
563 * to be returned later.
571 /* Insert this node into the ID tree */
572 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
573 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
574 bdb_id_cmp, bdb_id_dup_err ) ) {
575 EntryInfo *eix = ein->bei_lrunext;
577 if ( bdb_cache_entryinfo_trylock( eix )) {
578 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
579 ldap_pvt_thread_yield();
582 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
584 /* Someone else created this node just before us.
585 * Free our new copy and use the existing one.
587 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
589 /* if it was the node we were looking for, just return it */
600 /* otherwise, link up what we have and return */
604 /* If there was a previous node, link it to this one */
605 if ( ei2 ) ei2->bei_parent = ein;
607 /* Look for this node's parent */
610 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
611 (caddr_t) &eip, bdb_id_cmp );
613 ei2 = &bdb->bi_cache.c_dntree;
615 if ( ei2 && bdb_cache_entryinfo_trylock( ei2 )) {
616 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
617 ldap_pvt_thread_yield();
618 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
622 bdb->bi_cache.c_eiused++;
623 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
624 bdb->bi_cache.c_leaves++;
625 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
628 /* Got the parent, link in and we're done. */
630 bdb_cache_entryinfo_lock( eir );
631 ein->bei_parent = ei2;
633 if ( avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
634 avl_dup_error) == 0 )
637 /* Reset all the state info */
638 for (ein = eir; ein != ei2; ein=ein->bei_parent)
639 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
641 bdb_cache_entryinfo_unlock( ei2 );
648 ei.bei_id = eip.bei_id;
650 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
656 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
660 struct bdb_info *bdb,
667 /* See if we already have this one */
668 bdb_cache_entryinfo_lock( ei->bei_parent );
669 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
670 bdb_cache_entryinfo_unlock( ei->bei_parent );
673 /* Not found, add it */
676 /* bei_rdn was not malloc'd before, do it now */
677 ber_dupbv( &bv, &ei->bei_rdn );
680 rc = bdb_entryinfo_add_internal( bdb, ei, res );
681 bdb_cache_entryinfo_unlock( ei->bei_parent );
682 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
684 /* Found, return it */
692 /* This is best-effort only. If all entries in the cache are
693 * busy, they will all be kept. This is unlikely to happen
694 * unless the cache is very much smaller than the working set.
697 bdb_cache_lru_purge( struct bdb_info *bdb )
699 DB_LOCK lock, *lockp;
700 EntryInfo *elru, *elnext = NULL;
703 ID count, efree, eifree = 0;
708 /* Wait for the mutex; we're the only one trying to purge. */
709 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
711 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
712 efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
713 efree += bdb->bi_cache.c_minfree;
718 /* maximum number of EntryInfo leaves to cache. In slapcat
719 * we always free all leaf nodes.
722 if ( slapMode & SLAP_TOOL_READONLY ) {
723 eifree = bdb->bi_cache.c_leaves;
724 } else if ( bdb->bi_cache.c_eimax &&
725 bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
726 eifree = bdb->bi_cache.c_minfree * 10;
727 if ( eifree >= bdb->bi_cache.c_leaves )
731 if ( !efree && !eifree ) {
732 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
733 bdb->bi_cache.c_purging = 0;
737 if ( bdb->bi_cache.c_txn ) {
750 /* Look for an unused entry to remove */
751 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
752 elnext = elru->bei_lrunext;
754 if ( bdb_cache_entryinfo_trylock( elru ))
757 /* This flag implements the clock replacement behavior */
758 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
759 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
760 bdb_cache_entryinfo_unlock( elru );
764 /* If this node is in the process of linking into the cache,
765 * or this node is being deleted, skip it.
767 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
768 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING |
769 CACHE_ENTRY_ONELEVEL )) ||
770 elru->bei_finders > 0 ) {
771 bdb_cache_entryinfo_unlock( elru );
775 if ( bdb_cache_entryinfo_trylock( elru->bei_parent )) {
776 bdb_cache_entryinfo_unlock( elru );
780 /* entryinfo is locked */
783 /* If we can successfully writelock it, then
784 * the object is idle.
786 if ( bdb_cache_entry_db_lock( bdb,
787 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
789 /* Free entry for this node if it's present */
793 /* the cache may have gone over the limit while we
794 * weren't looking, so double check.
796 if ( !efree && ecount > bdb->bi_cache.c_maxsize )
797 efree = bdb->bi_cache.c_minfree;
799 if ( count < efree ) {
800 elru->bei_e->e_private = NULL;
801 #ifdef SLAP_ZONE_ALLOC
802 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
804 bdb_entry_return( elru->bei_e );
809 /* Keep this node cached, skip to next */
810 bdb_cache_entry_db_unlock( bdb, lockp );
814 bdb_cache_entry_db_unlock( bdb, lockp );
817 * If it is a leaf node, and we're over the limit, free it.
819 if ( elru->bei_kids ) {
820 /* Drop from list, we ignore it... */
821 LRU_DEL( &bdb->bi_cache, elru );
822 } else if ( eicount < eifree ) {
823 /* Too many leaf nodes, free this one */
824 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
825 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
828 } /* Leave on list until we need to free it */
833 bdb_cache_entryinfo_unlock( elru );
834 bdb_cache_entryinfo_unlock( elru->bei_parent );
837 if ( count >= efree && eicount >= eifree )
840 if ( elnext == bdb->bi_cache.c_lruhead )
847 if ( count || ecount > bdb->bi_cache.c_cursize ) {
848 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
849 /* HACK: we seem to be losing track, fix up now */
850 if ( ecount > bdb->bi_cache.c_cursize )
851 bdb->bi_cache.c_cursize = ecount;
852 bdb->bi_cache.c_cursize -= count;
853 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
855 bdb->bi_cache.c_lruhead = elnext;
856 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
857 bdb->bi_cache.c_purging = 0;
861 * cache_find_id - find an entry in the cache, given id.
862 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
863 * the supplied *eip was already locked.
875 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
877 int rc = 0, load = 0;
878 EntryInfo ei = { 0 };
882 #ifdef SLAP_ZONE_ALLOC
883 slap_zh_rlock(bdb->bi_cache.c_zctx);
885 /* If we weren't given any info, see if we have it already cached */
887 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
888 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
889 (caddr_t) &ei, bdb_id_cmp );
891 /* If the lock attempt fails, the info is in use */
892 if ( bdb_cache_entryinfo_trylock( *eip )) {
893 int del = (*eip)->bei_state & CACHE_ENTRY_DELETED;
894 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
895 /* If this node is being deleted, treat
896 * as if the delete has already finished
901 /* otherwise, wait for the info to free up */
902 ldap_pvt_thread_yield();
905 /* If this info isn't hooked up to its parent yet,
906 * unlock and wait for it to be fully initialized
908 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
909 bdb_cache_entryinfo_unlock( *eip );
910 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
911 ldap_pvt_thread_yield();
916 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
919 /* See if the ID exists in the database; add it to the cache if so */
922 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
924 rc = bdb_cache_find_ndn( op, tid,
926 if ( *eip ) flag |= ID_LOCKED;
928 ep->e_private = NULL;
929 #ifdef SLAP_ZONE_ALLOC
930 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
932 bdb_entry_return( ep );
938 rc = hdb_cache_find_parent(op, tid, id, eip );
939 if ( rc == 0 ) flag |= ID_LOCKED;
943 /* Ok, we found the info, do we have the entry? */
945 if ( !( flag & ID_LOCKED )) {
946 bdb_cache_entryinfo_lock( *eip );
950 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
953 (*eip)->bei_finders++;
954 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
955 if ( flag & ID_NOENTRY ) {
956 bdb_cache_entryinfo_unlock( *eip );
959 /* Make sure only one thread tries to load the entry */
961 #ifdef SLAP_ZONE_ALLOC
962 if ((*eip)->bei_e && !slap_zn_validate(
963 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
964 (*eip)->bei_e = NULL;
965 (*eip)->bei_zseq = 0;
968 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
970 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
974 /* Clear the uncached state if we are not
975 * loading it, i.e it is already cached or
976 * another thread is currently loading it.
978 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
979 (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
980 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
981 ++bdb->bi_cache.c_cursize;
982 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
987 if ( flag & ID_LOCKED ) {
988 bdb_cache_entryinfo_unlock( *eip );
991 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
992 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
994 bdb_cache_entry_db_unlock( bdb, lock );
995 bdb_cache_entryinfo_lock( *eip );
996 (*eip)->bei_finders--;
997 bdb_cache_entryinfo_unlock( *eip );
998 } else if ( rc == 0 ) {
1001 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
1004 ep->e_private = *eip;
1006 while ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED )
1007 ldap_pvt_thread_yield();
1008 bdb_fix_dn( ep, 0 );
1011 #ifdef SLAP_ZONE_ALLOC
1012 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
1015 bdb_cache_lru_link( bdb, *eip );
1016 if (( flag & ID_NOCACHE ) &&
1017 ( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
1018 /* Set the cached state only if no other thread
1019 * found the info while we were loading the entry.
1021 if ( (*eip)->bei_finders == 1 )
1022 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
1023 bdb_cache_entryinfo_unlock( *eip );
1027 /* If we succeeded, downgrade back to a readlock. */
1028 rc = bdb_cache_entry_db_relock( bdb, tid,
1031 /* Otherwise, release the lock. */
1032 bdb_cache_entry_db_unlock( bdb, lock );
1034 } else if ( !(*eip)->bei_e ) {
1035 /* Some other thread is trying to load the entry,
1036 * wait for it to finish.
1038 bdb_cache_entry_db_unlock( bdb, lock );
1039 bdb_cache_entryinfo_lock( *eip );
1044 /* Check for subtree renames
1046 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
1048 bdb_cache_entry_db_relock( bdb,
1049 tid, *eip, 1, 0, lock );
1050 /* check again in case other modifier did it already */
1051 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
1052 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
1053 bdb_cache_entry_db_relock( bdb,
1054 tid, *eip, 0, 0, lock );
1058 bdb_cache_entryinfo_lock( *eip );
1059 (*eip)->bei_finders--;
1061 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
1062 bdb_cache_entryinfo_unlock( *eip );
1066 if ( flag & ID_LOCKED ) {
1067 bdb_cache_entryinfo_unlock( *eip );
1070 ep->e_private = NULL;
1071 #ifdef SLAP_ZONE_ALLOC
1072 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
1074 bdb_entry_return( ep );
1080 if (( load && !( flag & ID_NOCACHE )) || bdb->bi_cache.c_eimax ) {
1081 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1082 if ( load && !( flag & ID_NOCACHE )) {
1083 bdb->bi_cache.c_cursize++;
1084 if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
1086 bdb->bi_cache.c_purging = 1;
1088 } else if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_eimax && bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1090 bdb->bi_cache.c_purging = 1;
1092 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1095 bdb_cache_lru_purge( bdb );
1098 #ifdef SLAP_ZONE_ALLOC
1099 if (rc == 0 && (*eip)->bei_e) {
1100 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
1102 slap_zh_runlock(bdb->bi_cache.c_zctx);
1115 if ( BEI(e)->bei_kids ) {
1118 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1121 rc = bdb_dn2id_children( op, txn, e );
1122 if ( rc == DB_NOTFOUND ) {
1123 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1128 /* Update the cache after a successful database Add. */
1131 struct bdb_info *bdb,
1134 struct berval *nrdn,
1141 struct berval rdn = e->e_name;
1144 ei.bei_id = e->e_id;
1145 ei.bei_parent = eip;
1146 ei.bei_nrdn = *nrdn;
1149 /* Lock this entry so that bdb_add can run to completion.
1150 * It can only fail if BDB has run out of lock resources.
1152 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1154 bdb_cache_entryinfo_unlock( eip );
1159 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1160 char *ptr = ber_bvchr( &rdn, ',' );
1161 assert( ptr != NULL );
1162 rdn.bv_len = ptr - rdn.bv_val;
1164 ber_dupbv( &ei.bei_rdn, &rdn );
1165 if ( eip->bei_dkids ) eip->bei_dkids++;
1168 if (eip->bei_parent) {
1169 bdb_cache_entryinfo_lock( eip->bei_parent );
1170 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1171 bdb_cache_entryinfo_unlock( eip->bei_parent );
1174 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1175 /* bdb_csn_commit can cause this when adding the database root entry */
1177 new->bei_e->e_private = NULL;
1178 #ifdef SLAP_ZONE_ALLOC
1179 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1181 bdb_entry_return( new->bei_e );
1186 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1187 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1188 bdb_cache_entryinfo_unlock( eip );
1190 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1191 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1192 ++bdb->bi_cache.c_cursize;
1193 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1194 !bdb->bi_cache.c_purging ) {
1196 bdb->bi_cache.c_purging = 1;
1198 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1200 bdb_cache_lru_link( bdb, new );
1203 bdb_cache_lru_purge( bdb );
1210 struct bdb_info *bdb,
1212 Attribute *newAttrs,
1216 EntryInfo *ei = BEI(e);
1218 /* Get write lock on data */
1219 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1221 /* If we've done repeated mods on a cached entry, then e_attrs
1222 * is no longer contiguous with the entry, and must be freed.
1225 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1226 attrs_free( e->e_attrs );
1228 e->e_attrs = newAttrs;
1234 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1238 struct bdb_info *bdb,
1240 struct berval *nrdn,
1246 EntryInfo *ei = BEI(e), *pei;
1252 /* Get write lock on data */
1253 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1254 if ( rc ) return rc;
1256 /* If we've done repeated mods on a cached entry, then e_attrs
1257 * is no longer contiguous with the entry, and must be freed.
1259 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1260 attrs_free( e->e_attrs );
1262 e->e_attrs = new->e_attrs;
1263 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1264 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1266 ch_free(e->e_name.bv_val);
1267 ch_free(e->e_nname.bv_val);
1269 e->e_name = new->e_name;
1270 e->e_nname = new->e_nname;
1272 /* Lock the parent's kids AVL tree */
1273 pei = ei->bei_parent;
1274 bdb_cache_entryinfo_lock( pei );
1275 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1276 free( ei->bei_nrdn.bv_val );
1277 ber_dupbv( &ei->bei_nrdn, nrdn );
1280 free( ei->bei_rdn.bv_val );
1283 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1284 char *ptr = ber_bvchr(&rdn, ',');
1285 assert( ptr != NULL );
1286 rdn.bv_len = ptr - rdn.bv_val;
1288 ber_dupbv( &ei->bei_rdn, &rdn );
1290 /* If new parent, decrement kid counts */
1293 if ( pei->bei_dkids ) {
1295 if ( pei->bei_dkids < 2 )
1296 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1302 ein = ei->bei_parent;
1304 ei->bei_parent = ein;
1305 bdb_cache_entryinfo_unlock( pei );
1306 bdb_cache_entryinfo_lock( ein );
1308 /* new parent now has kids */
1309 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1310 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1311 /* grandparent has grandkids */
1312 if ( ein->bei_parent )
1313 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1315 /* parent might now have grandkids */
1316 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1317 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1318 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1321 if ( ein->bei_dkids ) ein->bei_dkids++;
1326 /* Record the generation number of this change */
1327 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1329 ei->bei_modrdns = bdb->bi_modrdns;
1330 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1333 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1334 bdb_cache_entryinfo_unlock( ein );
1338 * cache_delete - delete the entry e from the cache.
1340 * returns: 0 e was deleted ok
1341 * 1 e was not in the cache
1342 * -1 something bad happened
1346 struct bdb_info *bdb,
1351 EntryInfo *ei = BEI(e);
1354 assert( e->e_private != NULL );
1356 /* Lock the entry's info */
1357 bdb_cache_entryinfo_lock( ei );
1359 /* Set this early, warn off any queriers */
1360 ei->bei_state |= CACHE_ENTRY_DELETED;
1362 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1363 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1364 ei->bei_finders > 0 )
1367 bdb_cache_entryinfo_unlock( ei );
1370 ldap_pvt_thread_yield();
1372 bdb_cache_entryinfo_lock( ei );
1373 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1374 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1375 ei->bei_finders > 0 )
1377 bdb_cache_entryinfo_unlock( ei );
1380 /* Get write lock on the data */
1381 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1383 bdb_cache_entryinfo_lock( ei );
1384 /* couldn't lock, undo and give up */
1385 ei->bei_state ^= CACHE_ENTRY_DELETED;
1386 bdb_cache_entryinfo_unlock( ei );
1390 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1394 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1396 bdb_cache_entryinfo_lock( ei->bei_parent );
1397 bdb_cache_entryinfo_lock( ei );
1398 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1399 bdb_cache_entryinfo_unlock( ei );
1401 /* free lru mutex */
1402 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1408 bdb_cache_delete_cleanup(
1412 /* Enter with ei locked */
1414 /* already freed? */
1415 if ( !ei->bei_parent ) return;
1418 ei->bei_e->e_private = NULL;
1419 #ifdef SLAP_ZONE_ALLOC
1420 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1422 bdb_entry_return( ei->bei_e );
1427 bdb_cache_entryinfo_unlock( ei );
1428 bdb_cache_entryinfo_free( cache, ei );
1432 bdb_cache_delete_internal(
1437 int rc = 0; /* return code */
1440 /* already freed? */
1441 if ( !e->bei_parent ) {
1447 e->bei_parent->bei_ckids--;
1448 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1451 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1457 if ( e->bei_parent->bei_kids )
1460 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1462 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1470 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1471 bdb_cache_entryinfo_unlock( e->bei_parent );
1475 LRU_DEL( cache, e );
1478 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1480 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1488 bdb_entryinfo_release( void *data )
1490 EntryInfo *ei = (EntryInfo *)data;
1491 if ( ei->bei_kids ) {
1492 avl_free( ei->bei_kids, NULL );
1495 ei->bei_e->e_private = NULL;
1496 #ifdef SLAP_ZONE_ALLOC
1497 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1499 bdb_entry_return( ei->bei_e );
1502 bdb_cache_entryinfo_destroy( ei );
1506 bdb_cache_release_all( Cache *cache )
1508 /* set cache write lock */
1509 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1511 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1513 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1515 avl_free( cache->c_dntree.bei_kids, NULL );
1516 avl_free( cache->c_idtree, bdb_entryinfo_release );
1517 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1518 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1519 bdb_cache_entryinfo_destroy(cache->c_eifree);
1521 cache->c_cursize = 0;
1522 cache->c_eiused = 0;
1523 cache->c_leaves = 0;
1524 cache->c_idtree = NULL;
1525 cache->c_lruhead = NULL;
1526 cache->c_lrutail = NULL;
1527 cache->c_dntree.bei_kids = NULL;
1529 /* free lru mutex */
1530 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1531 /* free cache write lock */
1532 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1538 bdb_lru_print( Cache *cache )
1542 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1543 fprintf( stderr, "LRU circle (tail forward):\n" );
1544 for ( e = cache->c_lrutail; ; ) {
1545 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1546 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1548 if ( e == cache->c_lrutail )
1551 fprintf( stderr, "LRU circle (tail backward):\n" );
1552 for ( e = cache->c_lrutail; ; ) {
1553 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1554 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1556 if ( e == cache->c_lrutail )
1562 bdb_entryinfo_print(void *data, void *arg)
1564 EntryInfo *e = data;
1565 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1566 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1571 bdb_idtree_print(Cache *cache)
1573 avl_apply( cache->c_idtree, bdb_entryinfo_print, NULL, -1, AVL_INORDER );
1579 bdb_reader_free( void *key, void *data )
1581 /* DB_ENV *env = key; */
1584 if ( txn ) TXN_ABORT( txn );
1587 /* free up any keys used by the main thread */
1589 bdb_reader_flush( DB_ENV *env )
1592 void *ctx = ldap_pvt_thread_pool_context();
1594 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1595 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1596 bdb_reader_free( env, data );
1601 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1607 if ( !env || !txn ) return -1;
1609 /* If no op was provided, try to find the ctx anyway... */
1611 ctx = op->o_threadctx;
1613 ctx = ldap_pvt_thread_pool_context();
1616 /* Shouldn't happen unless we're single-threaded */
1622 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1623 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1624 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1625 if (rc) ldap_pvt_thread_yield();
1631 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1632 data, bdb_reader_free, NULL, NULL ) ) ) {
1634 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1635 db_strerror(rc), rc, 0 );