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-2014 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 )
268 if ( ei && ( ei->bei_state & CACHE_ENTRY_NOT_CACHED )) {
269 bdb_cache_entryinfo_lock( ei );
270 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
271 /* Releasing the entry can only be done when
272 * we know that nobody else is using it, i.e we
273 * should have an entry_db writelock. But the
274 * flag is only set by the thread that loads the
275 * entry, and only if no other threads has found
276 * it while it was working. All other threads
277 * clear the flag, which mean that we should be
278 * the only thread using the entry if the flag
282 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
285 bdb_cache_entryinfo_unlock( ei );
287 bdb_cache_entry_db_unlock( bdb, lock );
290 bdb_entry_return( e );
295 bdb_cache_entryinfo_destroy( EntryInfo *e )
297 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
298 free( e->bei_nrdn.bv_val );
300 free( e->bei_rdn.bv_val );
306 /* Do a length-ordered sort on normalized RDNs */
308 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
310 const EntryInfo *e1 = v_e1, *e2 = v_e2;
311 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
313 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
314 e1->bei_nrdn.bv_len );
320 bdb_id_cmp( const void *v_e1, const void *v_e2 )
322 const EntryInfo *e1 = v_e1, *e2 = v_e2;
323 return e1->bei_id - e2->bei_id;
327 bdb_id_dup_err( void *v1, void *v2 )
330 e2->bei_lrunext = v1;
334 /* Create an entryinfo in the cache. Caller must release the locks later.
337 bdb_entryinfo_add_internal(
338 struct bdb_info *bdb,
342 EntryInfo *ei2 = NULL;
346 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
348 bdb_cache_entryinfo_lock( ei->bei_parent );
349 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
351 ei2->bei_id = ei->bei_id;
352 ei2->bei_parent = ei->bei_parent;
354 ei2->bei_rdn = ei->bei_rdn;
356 #ifdef SLAP_ZONE_ALLOC
360 /* Add to cache ID tree */
361 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
363 EntryInfo *eix = ei2->bei_lrunext;
364 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
367 /* It got freed above because its value was
370 ei->bei_rdn.bv_val = NULL;
375 bdb->bi_cache.c_eiused++;
376 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
378 /* This is a new leaf node. But if parent had no kids, then it was
379 * a leaf and we would be decrementing that. So, only increment if
380 * the parent already has kids.
382 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
383 bdb->bi_cache.c_leaves++;
384 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
387 /* it's possible for hdb_cache_find_parent to beat us to it */
389 ei->bei_parent->bei_ckids++;
398 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
399 * the info for its closest ancestor. *res should be NULL to process a
400 * complete DN starting from the tree root. Otherwise *res must be the
401 * immediate parent of the requested DN, and only the RDN will be searched.
402 * The EntryInfo is locked upon return and must be unlocked by the caller.
411 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
412 EntryInfo ei, *eip, *ei2;
416 /* this function is always called with normalized DN */
418 /* we're doing a onelevel search for an RDN */
419 ei.bei_nrdn.bv_val = ndn->bv_val;
420 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
423 /* we're searching a full DN from the root */
424 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
425 ei.bei_nrdn.bv_val = ptr;
426 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
427 /* Skip to next rdn if suffix is empty */
428 if ( ei.bei_nrdn.bv_len == 0 ) {
429 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
430 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
431 if ( ptr >= ndn->bv_val ) {
432 if (DN_SEPARATOR(*ptr)) ptr++;
433 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
434 ei.bei_nrdn.bv_val = ptr;
437 eip = &bdb->bi_cache.c_dntree;
440 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
441 eip->bei_state |= CACHE_ENTRY_REFERENCED;
443 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
446 int len = ei.bei_nrdn.bv_len;
448 if ( BER_BVISEMPTY( ndn )) {
453 ei.bei_nrdn.bv_len = ndn->bv_len -
454 (ei.bei_nrdn.bv_val - ndn->bv_val);
456 bdb_cache_entryinfo_unlock( eip );
458 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
459 ei.bei_nrdn.bv_val );
462 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &cursor );
464 bdb_cache_entryinfo_lock( eip );
466 if ( cursor ) cursor->c_close( cursor );
471 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
472 ei.bei_nrdn.bv_val, ei.bei_id );
474 /* DN exists but needs to be added to cache */
475 ei.bei_nrdn.bv_len = len;
476 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
477 /* add_internal left eip and c_rwlock locked */
479 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
480 if ( cursor ) cursor->c_close( cursor );
486 bdb_cache_entryinfo_lock( ei2 );
487 if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
488 /* In the midst of deleting? Give it a chance to
491 bdb_cache_entryinfo_unlock( ei2 );
492 bdb_cache_entryinfo_unlock( eip );
493 ldap_pvt_thread_yield();
494 bdb_cache_entryinfo_lock( eip );
498 bdb_cache_entryinfo_unlock( eip );
502 /* Advance to next lower RDN */
503 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
504 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
505 if ( ptr >= ndn->bv_val ) {
506 if (DN_SEPARATOR(*ptr)) ptr++;
507 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
508 ei.bei_nrdn.bv_val = ptr;
510 if ( ptr < ndn->bv_val ) {
520 /* Walk up the tree from a child node, looking for an ID that's already
521 * been linked into the cache.
524 hdb_cache_find_parent(
530 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
531 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
539 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
542 /* Save the previous node, if any */
545 /* Create a new node for the current ID */
546 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
547 ein->bei_id = ei.bei_id;
548 ein->bei_kids = ei.bei_kids;
549 ein->bei_nrdn = ei.bei_nrdn;
550 ein->bei_rdn = ei.bei_rdn;
551 ein->bei_ckids = ei.bei_ckids;
552 #ifdef SLAP_ZONE_ALLOC
558 /* This node is not fully connected yet */
559 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
561 /* If this is the first time, save this node
562 * to be returned later.
570 /* Insert this node into the ID tree */
571 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
572 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
573 bdb_id_cmp, bdb_id_dup_err ) ) {
574 EntryInfo *eix = ein->bei_lrunext;
576 if ( bdb_cache_entryinfo_trylock( eix )) {
577 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
578 ldap_pvt_thread_yield();
581 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
583 /* Someone else created this node just before us.
584 * Free our new copy and use the existing one.
586 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
588 /* if it was the node we were looking for, just return it */
599 /* otherwise, link up what we have and return */
603 /* If there was a previous node, link it to this one */
604 if ( ei2 ) ei2->bei_parent = ein;
606 /* Look for this node's parent */
609 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
610 (caddr_t) &eip, bdb_id_cmp );
612 ei2 = &bdb->bi_cache.c_dntree;
614 if ( ei2 && bdb_cache_entryinfo_trylock( ei2 )) {
615 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
616 ldap_pvt_thread_yield();
617 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
621 bdb->bi_cache.c_eiused++;
622 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
623 bdb->bi_cache.c_leaves++;
624 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
627 /* Got the parent, link in and we're done. */
629 bdb_cache_entryinfo_lock( eir );
630 ein->bei_parent = ei2;
632 if ( avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
633 avl_dup_error) == 0 )
636 /* Reset all the state info */
637 for (ein = eir; ein != ei2; ein=ein->bei_parent)
638 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
640 bdb_cache_entryinfo_unlock( ei2 );
647 ei.bei_id = eip.bei_id;
649 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
655 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
659 struct bdb_info *bdb,
666 /* See if we already have this one */
667 bdb_cache_entryinfo_lock( ei->bei_parent );
668 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
669 bdb_cache_entryinfo_unlock( ei->bei_parent );
672 /* Not found, add it */
675 /* bei_rdn was not malloc'd before, do it now */
676 ber_dupbv( &bv, &ei->bei_rdn );
679 rc = bdb_entryinfo_add_internal( bdb, ei, res );
680 bdb_cache_entryinfo_unlock( ei->bei_parent );
681 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
683 /* Found, return it */
691 /* This is best-effort only. If all entries in the cache are
692 * busy, they will all be kept. This is unlikely to happen
693 * unless the cache is very much smaller than the working set.
696 bdb_cache_lru_purge( struct bdb_info *bdb )
698 DB_LOCK lock, *lockp;
699 EntryInfo *elru, *elnext = NULL;
702 ID count, efree, eifree = 0;
707 /* Wait for the mutex; we're the only one trying to purge. */
708 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
710 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
711 efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
712 efree += bdb->bi_cache.c_minfree;
717 /* maximum number of EntryInfo leaves to cache. In slapcat
718 * we always free all leaf nodes.
721 if ( slapMode & SLAP_TOOL_READONLY ) {
722 eifree = bdb->bi_cache.c_leaves;
723 } else if ( bdb->bi_cache.c_eimax &&
724 bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
725 eifree = bdb->bi_cache.c_minfree * 10;
726 if ( eifree >= bdb->bi_cache.c_leaves )
730 if ( !efree && !eifree ) {
731 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
732 bdb->bi_cache.c_purging = 0;
736 if ( bdb->bi_cache.c_txn ) {
749 /* Look for an unused entry to remove */
750 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
751 elnext = elru->bei_lrunext;
753 if ( bdb_cache_entryinfo_trylock( elru ))
756 /* This flag implements the clock replacement behavior */
757 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
758 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
759 bdb_cache_entryinfo_unlock( elru );
763 /* If this node is in the process of linking into the cache,
764 * or this node is being deleted, skip it.
766 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
767 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING |
768 CACHE_ENTRY_ONELEVEL )) ||
769 elru->bei_finders > 0 ) {
770 bdb_cache_entryinfo_unlock( elru );
774 if ( bdb_cache_entryinfo_trylock( elru->bei_parent )) {
775 bdb_cache_entryinfo_unlock( elru );
779 /* entryinfo is locked */
782 /* If we can successfully writelock it, then
783 * the object is idle.
785 if ( bdb_cache_entry_db_lock( bdb,
786 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
788 /* Free entry for this node if it's present */
792 /* the cache may have gone over the limit while we
793 * weren't looking, so double check.
795 if ( !efree && ecount > bdb->bi_cache.c_maxsize )
796 efree = bdb->bi_cache.c_minfree;
798 if ( count < efree ) {
799 elru->bei_e->e_private = NULL;
800 #ifdef SLAP_ZONE_ALLOC
801 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
803 bdb_entry_return( elru->bei_e );
808 /* Keep this node cached, skip to next */
809 bdb_cache_entry_db_unlock( bdb, lockp );
813 bdb_cache_entry_db_unlock( bdb, lockp );
816 * If it is a leaf node, and we're over the limit, free it.
818 if ( elru->bei_kids ) {
819 /* Drop from list, we ignore it... */
820 LRU_DEL( &bdb->bi_cache, elru );
821 } else if ( eicount < eifree ) {
822 /* Too many leaf nodes, free this one */
823 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
824 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
827 } /* Leave on list until we need to free it */
832 bdb_cache_entryinfo_unlock( elru );
833 bdb_cache_entryinfo_unlock( elru->bei_parent );
836 if ( count >= efree && eicount >= eifree )
839 if ( elnext == bdb->bi_cache.c_lruhead )
846 if ( count || ecount > bdb->bi_cache.c_cursize ) {
847 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
848 /* HACK: we seem to be losing track, fix up now */
849 if ( ecount > bdb->bi_cache.c_cursize )
850 bdb->bi_cache.c_cursize = ecount;
851 bdb->bi_cache.c_cursize -= count;
852 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
854 bdb->bi_cache.c_lruhead = elnext;
855 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
856 bdb->bi_cache.c_purging = 0;
860 * cache_find_id - find an entry in the cache, given id.
861 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
862 * the supplied *eip was already locked.
874 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
876 int rc = 0, load = 0;
877 EntryInfo ei = { 0 };
881 #ifdef SLAP_ZONE_ALLOC
882 slap_zh_rlock(bdb->bi_cache.c_zctx);
884 /* If we weren't given any info, see if we have it already cached */
886 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
887 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
888 (caddr_t) &ei, bdb_id_cmp );
890 /* If the lock attempt fails, the info is in use */
891 if ( bdb_cache_entryinfo_trylock( *eip )) {
892 int del = (*eip)->bei_state & CACHE_ENTRY_DELETED;
893 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
894 /* If this node is being deleted, treat
895 * as if the delete has already finished
900 /* otherwise, wait for the info to free up */
901 ldap_pvt_thread_yield();
904 /* If this info isn't hooked up to its parent yet,
905 * unlock and wait for it to be fully initialized
907 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
908 bdb_cache_entryinfo_unlock( *eip );
909 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
910 ldap_pvt_thread_yield();
915 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
918 /* See if the ID exists in the database; add it to the cache if so */
921 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
923 rc = bdb_cache_find_ndn( op, tid,
925 if ( *eip ) flag |= ID_LOCKED;
927 ep->e_private = NULL;
928 #ifdef SLAP_ZONE_ALLOC
929 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
931 bdb_entry_return( ep );
937 rc = hdb_cache_find_parent(op, tid, id, eip );
938 if ( rc == 0 ) flag |= ID_LOCKED;
942 /* Ok, we found the info, do we have the entry? */
944 if ( !( flag & ID_LOCKED )) {
945 bdb_cache_entryinfo_lock( *eip );
949 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
952 (*eip)->bei_finders++;
953 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
954 if ( flag & ID_NOENTRY ) {
955 bdb_cache_entryinfo_unlock( *eip );
958 /* Make sure only one thread tries to load the entry */
960 #ifdef SLAP_ZONE_ALLOC
961 if ((*eip)->bei_e && !slap_zn_validate(
962 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
963 (*eip)->bei_e = NULL;
964 (*eip)->bei_zseq = 0;
967 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
969 (*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;
984 if ( flag & ID_LOCKED ) {
985 bdb_cache_entryinfo_unlock( *eip );
988 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
989 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
991 bdb_cache_entry_db_unlock( bdb, lock );
992 bdb_cache_entryinfo_lock( *eip );
993 (*eip)->bei_finders--;
994 bdb_cache_entryinfo_unlock( *eip );
995 } else if ( rc == 0 ) {
998 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
1001 ep->e_private = *eip;
1003 while ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED )
1004 ldap_pvt_thread_yield();
1005 bdb_fix_dn( ep, 0 );
1007 bdb_cache_entryinfo_lock( *eip );
1010 #ifdef SLAP_ZONE_ALLOC
1011 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
1014 if ( flag & ID_NOCACHE ) {
1015 /* Set the cached state only if no other thread
1016 * found the info while we were loading the entry.
1018 if ( (*eip)->bei_finders == 1 ) {
1019 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
1020 flag ^= ID_CHKPURGE;
1023 bdb_cache_entryinfo_unlock( *eip );
1024 bdb_cache_lru_link( bdb, *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 (( flag & ID_CHKPURGE ) || bdb->bi_cache.c_eimax ) {
1081 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1082 if ( flag & ID_CHKPURGE ) {
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;
1150 /* Lock this entry so that bdb_add can run to completion.
1151 * It can only fail if BDB has run out of lock resources.
1153 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1155 bdb_cache_entryinfo_unlock( eip );
1161 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1162 char *ptr = ber_bvchr( &rdn, ',' );
1163 assert( ptr != NULL );
1164 rdn.bv_len = ptr - rdn.bv_val;
1166 ber_dupbv( &ei.bei_rdn, &rdn );
1167 if ( eip->bei_dkids ) eip->bei_dkids++;
1170 if (eip->bei_parent) {
1171 bdb_cache_entryinfo_lock( eip->bei_parent );
1172 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1173 bdb_cache_entryinfo_unlock( eip->bei_parent );
1176 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1177 /* bdb_csn_commit can cause this when adding the database root entry */
1179 new->bei_e->e_private = NULL;
1180 #ifdef SLAP_ZONE_ALLOC
1181 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1183 bdb_entry_return( new->bei_e );
1188 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1189 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1190 bdb_cache_entryinfo_unlock( eip );
1192 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1193 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1194 ++bdb->bi_cache.c_cursize;
1195 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1196 !bdb->bi_cache.c_purging ) {
1198 bdb->bi_cache.c_purging = 1;
1200 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1202 new->bei_finders = 1;
1203 bdb_cache_lru_link( bdb, new );
1206 bdb_cache_lru_purge( bdb );
1211 void bdb_cache_deref(
1215 bdb_cache_entryinfo_lock( ei );
1217 bdb_cache_entryinfo_unlock( ei );
1222 struct bdb_info *bdb,
1224 Attribute *newAttrs,
1228 EntryInfo *ei = BEI(e);
1230 /* Get write lock on data */
1231 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1233 /* If we've done repeated mods on a cached entry, then e_attrs
1234 * is no longer contiguous with the entry, and must be freed.
1237 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1238 attrs_free( e->e_attrs );
1240 e->e_attrs = newAttrs;
1246 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1250 struct bdb_info *bdb,
1252 struct berval *nrdn,
1258 EntryInfo *ei = BEI(e), *pei;
1264 /* Get write lock on data */
1265 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1266 if ( rc ) return rc;
1268 /* If we've done repeated mods on a cached entry, then e_attrs
1269 * is no longer contiguous with the entry, and must be freed.
1271 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1272 attrs_free( e->e_attrs );
1274 e->e_attrs = new->e_attrs;
1275 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1276 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1278 ch_free(e->e_name.bv_val);
1279 ch_free(e->e_nname.bv_val);
1281 e->e_name = new->e_name;
1282 e->e_nname = new->e_nname;
1284 /* Lock the parent's kids AVL tree */
1285 pei = ei->bei_parent;
1286 bdb_cache_entryinfo_lock( pei );
1287 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1288 free( ei->bei_nrdn.bv_val );
1289 ber_dupbv( &ei->bei_nrdn, nrdn );
1292 free( ei->bei_rdn.bv_val );
1295 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1296 char *ptr = ber_bvchr(&rdn, ',');
1297 assert( ptr != NULL );
1298 rdn.bv_len = ptr - rdn.bv_val;
1300 ber_dupbv( &ei->bei_rdn, &rdn );
1302 /* If new parent, decrement kid counts */
1305 if ( pei->bei_dkids ) {
1307 if ( pei->bei_dkids < 2 )
1308 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1314 ein = ei->bei_parent;
1316 ei->bei_parent = ein;
1317 bdb_cache_entryinfo_unlock( pei );
1318 bdb_cache_entryinfo_lock( ein );
1320 /* new parent now has kids */
1321 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1322 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1323 /* grandparent has grandkids */
1324 if ( ein->bei_parent )
1325 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1327 /* parent might now have grandkids */
1328 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1329 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1330 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1333 if ( ein->bei_dkids ) ein->bei_dkids++;
1338 /* Record the generation number of this change */
1339 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1341 ei->bei_modrdns = bdb->bi_modrdns;
1342 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1345 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1346 bdb_cache_entryinfo_unlock( ein );
1350 * cache_delete - delete the entry e from the cache.
1352 * returns: 0 e was deleted ok
1353 * 1 e was not in the cache
1354 * -1 something bad happened
1358 struct bdb_info *bdb,
1363 EntryInfo *ei = BEI(e);
1364 int rc, busy = 0, counter = 0;
1366 assert( e->e_private != NULL );
1368 /* Lock the entry's info */
1369 bdb_cache_entryinfo_lock( ei );
1371 /* Set this early, warn off any queriers */
1372 ei->bei_state |= CACHE_ENTRY_DELETED;
1374 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1375 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1376 ei->bei_finders > 0 )
1379 bdb_cache_entryinfo_unlock( ei );
1381 while ( busy && counter < 1000) {
1382 ldap_pvt_thread_yield();
1384 bdb_cache_entryinfo_lock( ei );
1385 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1386 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1387 ei->bei_finders > 0 )
1389 bdb_cache_entryinfo_unlock( ei );
1393 bdb_cache_entryinfo_lock( ei );
1394 ei->bei_state ^= CACHE_ENTRY_DELETED;
1395 bdb_cache_entryinfo_unlock( ei );
1396 return DB_LOCK_DEADLOCK;
1399 /* Get write lock on the data */
1400 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1402 bdb_cache_entryinfo_lock( ei );
1403 /* couldn't lock, undo and give up */
1404 ei->bei_state ^= CACHE_ENTRY_DELETED;
1405 bdb_cache_entryinfo_unlock( ei );
1409 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1413 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1415 bdb_cache_entryinfo_lock( ei->bei_parent );
1416 bdb_cache_entryinfo_lock( ei );
1417 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1418 bdb_cache_entryinfo_unlock( ei );
1420 /* free lru mutex */
1421 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1427 bdb_cache_delete_cleanup(
1431 /* Enter with ei locked */
1433 /* already freed? */
1434 if ( !ei->bei_parent ) return;
1437 ei->bei_e->e_private = NULL;
1438 #ifdef SLAP_ZONE_ALLOC
1439 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1441 bdb_entry_return( ei->bei_e );
1446 bdb_cache_entryinfo_unlock( ei );
1447 bdb_cache_entryinfo_free( cache, ei );
1451 bdb_cache_delete_internal(
1456 int rc = 0; /* return code */
1459 /* already freed? */
1460 if ( !e->bei_parent ) {
1466 e->bei_parent->bei_ckids--;
1467 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1470 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1476 if ( e->bei_parent->bei_kids )
1479 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1481 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1489 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1490 bdb_cache_entryinfo_unlock( e->bei_parent );
1494 LRU_DEL( cache, e );
1497 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1499 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1507 bdb_entryinfo_release( void *data )
1509 EntryInfo *ei = (EntryInfo *)data;
1510 if ( ei->bei_kids ) {
1511 avl_free( ei->bei_kids, NULL );
1514 ei->bei_e->e_private = NULL;
1515 #ifdef SLAP_ZONE_ALLOC
1516 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1518 bdb_entry_return( ei->bei_e );
1521 bdb_cache_entryinfo_destroy( ei );
1525 bdb_cache_release_all( Cache *cache )
1527 /* set cache write lock */
1528 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1530 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1532 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1534 avl_free( cache->c_dntree.bei_kids, NULL );
1535 avl_free( cache->c_idtree, bdb_entryinfo_release );
1536 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1537 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1538 bdb_cache_entryinfo_destroy(cache->c_eifree);
1540 cache->c_cursize = 0;
1541 cache->c_eiused = 0;
1542 cache->c_leaves = 0;
1543 cache->c_idtree = NULL;
1544 cache->c_lruhead = NULL;
1545 cache->c_lrutail = NULL;
1546 cache->c_dntree.bei_kids = NULL;
1548 /* free lru mutex */
1549 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1550 /* free cache write lock */
1551 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1556 bdb_lru_count( Cache *cache )
1559 int ei = 0, ent = 0, nc = 0;
1561 for ( e = cache->c_lrutail; ; ) {
1565 if ( e->bei_state & CACHE_ENTRY_NOT_CACHED )
1567 fprintf( stderr, "ei %d entry %p dn %s\n", ei, (void *) e->bei_e, e->bei_e->e_name.bv_val );
1570 if ( e == cache->c_lrutail )
1573 fprintf( stderr, "counted %d entryInfos and %d entries, %d notcached\n",
1576 for ( e = cache->c_lrutail; ; ) {
1579 if ( e == cache->c_lrutail )
1582 fprintf( stderr, "counted %d entryInfos (on lruprev)\n", ei );
1587 bdb_lru_print( Cache *cache )
1591 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1592 fprintf( stderr, "LRU circle (tail forward):\n" );
1593 for ( e = cache->c_lrutail; ; ) {
1594 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1595 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1597 if ( e == cache->c_lrutail )
1600 fprintf( stderr, "LRU circle (tail backward):\n" );
1601 for ( e = cache->c_lrutail; ; ) {
1602 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1603 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1605 if ( e == cache->c_lrutail )
1611 bdb_entryinfo_print(void *data, void *arg)
1613 EntryInfo *e = data;
1614 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1615 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1620 bdb_idtree_print(Cache *cache)
1622 avl_apply( cache->c_idtree, bdb_entryinfo_print, NULL, -1, AVL_INORDER );
1628 bdb_reader_free( void *key, void *data )
1630 /* DB_ENV *env = key; */
1633 if ( txn ) TXN_ABORT( txn );
1636 /* free up any keys used by the main thread */
1638 bdb_reader_flush( DB_ENV *env )
1641 void *ctx = ldap_pvt_thread_pool_context();
1643 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1644 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1645 bdb_reader_free( env, data );
1650 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1656 if ( !env || !txn ) return -1;
1658 /* If no op was provided, try to find the ctx anyway... */
1660 ctx = op->o_threadctx;
1662 ctx = ldap_pvt_thread_pool_context();
1665 /* Shouldn't happen unless we're single-threaded */
1671 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1672 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1673 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1674 if (rc) ldap_pvt_thread_yield();
1680 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1681 data, bdb_reader_free, NULL, NULL ) ) ) {
1683 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1684 db_strerror(rc), rc, 0 );