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);
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 );
108 #define LRU_DEL( c, e ) do { \
109 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
110 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
111 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
112 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
113 e->bei_lruprev = NULL; \
116 /* Note - we now use a Second-Chance / Clock algorithm instead of
117 * Least-Recently-Used. This tremendously improves concurrency
118 * because we no longer need to manipulate the lists every time an
119 * entry is touched. We only need to lock the lists when adding
120 * or deleting an entry. It's now a circular doubly-linked list.
121 * We always append to the tail, but the head traverses the circle
122 * during a purge operation.
125 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
128 /* Already linked, ignore */
129 if ( ei->bei_lruprev )
132 /* Insert into circular LRU list */
133 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
135 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
136 if ( bdb->bi_cache.c_lrutail ) {
137 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
138 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
139 if ( ei->bei_lrunext )
140 ei->bei_lrunext->bei_lruprev = ei;
142 ei->bei_lrunext = ei->bei_lruprev = ei;
143 bdb->bi_cache.c_lruhead = ei;
145 bdb->bi_cache.c_lrutail = ei;
146 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
153 /* #define NO_DB_LOCK 1 */
154 /* Note: The BerkeleyDB locks are much slower than regular
155 * mutexes or rdwr locks. But the BDB implementation has the
156 * advantage of using a fixed size lock table, instead of
157 * allocating a lock object per entry in the DB. That's a
158 * key benefit for scaling. It also frees us from worrying
159 * about undetectable deadlocks between BDB activity and our
160 * own cache activity. It's still worth exploring faster
161 * alternatives though.
164 /* Atomically release and reacquire a lock */
166 bdb_cache_entry_db_relock(
167 struct bdb_info *bdb,
181 if ( !lock ) return 0;
183 lockobj.data = &ei->bei_id;
184 lockobj.size = sizeof(ei->bei_id) + 1;
186 list[0].op = DB_LOCK_PUT;
187 list[0].lock = *lock;
188 list[1].op = DB_LOCK_GET;
189 list[1].lock = *lock;
190 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
191 list[1].obj = &lockobj;
192 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
195 if (rc && !tryOnly) {
196 Debug( LDAP_DEBUG_TRACE,
197 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
198 ei->bei_id, rw, rc );
200 *lock = list[1].lock;
207 bdb_cache_entry_db_lock( struct bdb_info *bdb, DB_TXN *txn, EntryInfo *ei,
208 int rw, int tryOnly, DB_LOCK *lock )
217 if ( !lock ) return 0;
220 db_rw = DB_LOCK_WRITE;
222 db_rw = DB_LOCK_READ;
224 lockobj.data = &ei->bei_id;
225 lockobj.size = sizeof(ei->bei_id) + 1;
227 rc = LOCK_GET(bdb->bi_dbenv, TXN_ID(txn), tryOnly ? DB_LOCK_NOWAIT : 0,
228 &lockobj, db_rw, lock);
229 if (rc && !tryOnly) {
230 Debug( LDAP_DEBUG_TRACE,
231 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
232 ei->bei_id, rw, rc );
235 #endif /* NO_DB_LOCK */
239 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
246 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
248 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
254 bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
255 int rw, DB_LOCK *lock )
262 ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) &&
263 ( bdb_cache_entryinfo_trylock( ei ) == 0 )) {
264 if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
265 /* Releasing the entry can only be done when
266 * we know that nobody else is using it, i.e we
267 * should have an entry_db writelock. But the
268 * flag is only set by the thread that loads the
269 * entry, and only if no other threads has found
270 * it while it was working. All other threads
271 * clear the flag, which mean that we should be
272 * the only thread using the entry if the flag
276 ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
279 bdb_cache_entryinfo_unlock( ei );
281 bdb_cache_entry_db_unlock( bdb, lock );
284 bdb_entry_return( e );
289 bdb_cache_entryinfo_destroy( EntryInfo *e )
291 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
292 free( e->bei_nrdn.bv_val );
294 free( e->bei_rdn.bv_val );
300 /* Do a length-ordered sort on normalized RDNs */
302 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
304 const EntryInfo *e1 = v_e1, *e2 = v_e2;
305 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
307 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
308 e1->bei_nrdn.bv_len );
314 bdb_id_cmp( const void *v_e1, const void *v_e2 )
316 const EntryInfo *e1 = v_e1, *e2 = v_e2;
317 return e1->bei_id - e2->bei_id;
321 bdb_id_dup_err( void *v1, void *v2 )
324 e2->bei_lrunext = v1;
328 /* Create an entryinfo in the cache. Caller must release the locks later.
331 bdb_entryinfo_add_internal(
332 struct bdb_info *bdb,
336 EntryInfo *ei2 = NULL;
340 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
342 bdb_cache_entryinfo_lock( ei->bei_parent );
343 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
345 ei2->bei_id = ei->bei_id;
346 ei2->bei_parent = ei->bei_parent;
348 ei2->bei_rdn = ei->bei_rdn;
350 #ifdef SLAP_ZONE_ALLOC
354 /* Add to cache ID tree */
355 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
357 EntryInfo *eix = ei2->bei_lrunext;
358 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
361 /* It got freed above because its value was
364 ei->bei_rdn.bv_val = NULL;
369 bdb->bi_cache.c_eiused++;
370 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
372 /* This is a new leaf node. But if parent had no kids, then it was
373 * a leaf and we would be decrementing that. So, only increment if
374 * the parent already has kids.
376 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
377 bdb->bi_cache.c_leaves++;
378 rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
381 /* it's possible for hdb_cache_find_parent to beat us to it */
383 ei->bei_parent->bei_ckids++;
392 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
393 * the info for its closest ancestor. *res should be NULL to process a
394 * complete DN starting from the tree root. Otherwise *res must be the
395 * immediate parent of the requested DN, and only the RDN will be searched.
396 * The EntryInfo is locked upon return and must be unlocked by the caller.
405 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
406 EntryInfo ei, *eip, *ei2;
410 /* this function is always called with normalized DN */
412 /* we're doing a onelevel search for an RDN */
413 ei.bei_nrdn.bv_val = ndn->bv_val;
414 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
417 /* we're searching a full DN from the root */
418 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
419 ei.bei_nrdn.bv_val = ptr;
420 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
421 /* Skip to next rdn if suffix is empty */
422 if ( ei.bei_nrdn.bv_len == 0 ) {
423 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
424 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
425 if ( ptr >= ndn->bv_val ) {
426 if (DN_SEPARATOR(*ptr)) ptr++;
427 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
428 ei.bei_nrdn.bv_val = ptr;
431 eip = &bdb->bi_cache.c_dntree;
434 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
435 eip->bei_state |= CACHE_ENTRY_REFERENCED;
437 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
440 int len = ei.bei_nrdn.bv_len;
442 if ( BER_BVISEMPTY( ndn )) {
447 ei.bei_nrdn.bv_len = ndn->bv_len -
448 (ei.bei_nrdn.bv_val - ndn->bv_val);
450 bdb_cache_entryinfo_unlock( eip );
452 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
453 ei.bei_nrdn.bv_val );
455 lock.mode = DB_LOCK_NG;
456 rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, txn, &lock );
458 bdb_cache_entryinfo_lock( eip );
460 bdb_cache_entry_db_unlock( bdb, &lock );
465 BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
466 ei.bei_nrdn.bv_val, ei.bei_id );
468 /* DN exists but needs to be added to cache */
469 ei.bei_nrdn.bv_len = len;
470 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
471 /* add_internal left eip and c_rwlock locked */
473 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
474 bdb_cache_entry_db_unlock( bdb, &lock );
479 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
480 /* In the midst of deleting? Give it a chance to
483 bdb_cache_entryinfo_unlock( eip );
484 ldap_pvt_thread_yield();
485 bdb_cache_entryinfo_lock( eip );
489 bdb_cache_entryinfo_lock( ei2 );
490 bdb_cache_entryinfo_unlock( eip );
494 /* Advance to next lower RDN */
495 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
496 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
497 if ( ptr >= ndn->bv_val ) {
498 if (DN_SEPARATOR(*ptr)) ptr++;
499 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
500 ei.bei_nrdn.bv_val = ptr;
502 if ( ptr < ndn->bv_val ) {
512 /* Walk up the tree from a child node, looking for an ID that's already
513 * been linked into the cache.
516 hdb_cache_find_parent(
522 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
523 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
531 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
534 /* Save the previous node, if any */
537 /* Create a new node for the current ID */
538 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
539 ein->bei_id = ei.bei_id;
540 ein->bei_kids = ei.bei_kids;
541 ein->bei_nrdn = ei.bei_nrdn;
542 ein->bei_rdn = ei.bei_rdn;
543 ein->bei_ckids = ei.bei_ckids;
544 #ifdef SLAP_ZONE_ALLOC
550 /* This node is not fully connected yet */
551 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
553 /* If this is the first time, save this node
554 * to be returned later.
562 /* Insert this node into the ID tree */
563 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
564 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
565 bdb_id_cmp, bdb_id_dup_err ) ) {
566 EntryInfo *eix = ein->bei_lrunext;
568 if ( bdb_cache_entryinfo_trylock( eix )) {
569 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
570 ldap_pvt_thread_yield();
573 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
575 /* Someone else created this node just before us.
576 * Free our new copy and use the existing one.
578 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
580 /* if it was the node we were looking for, just return it */
591 /* otherwise, link up what we have and return */
595 /* If there was a previous node, link it to this one */
596 if ( ei2 ) ei2->bei_parent = ein;
598 /* Look for this node's parent */
601 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
602 (caddr_t) &eip, bdb_id_cmp );
604 ei2 = &bdb->bi_cache.c_dntree;
606 if ( ei2 && bdb_cache_entryinfo_trylock( ei2 )) {
607 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
608 ldap_pvt_thread_yield();
609 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
613 bdb->bi_cache.c_eiused++;
614 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
615 bdb->bi_cache.c_leaves++;
616 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
619 /* Got the parent, link in and we're done. */
621 bdb_cache_entryinfo_lock( eir );
622 ein->bei_parent = ei2;
624 if ( avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
625 avl_dup_error) == 0 )
628 /* Reset all the state info */
629 for (ein = eir; ein != ei2; ein=ein->bei_parent)
630 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
632 bdb_cache_entryinfo_unlock( ei2 );
639 ei.bei_id = eip.bei_id;
641 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
647 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
651 struct bdb_info *bdb,
658 /* See if we already have this one */
659 bdb_cache_entryinfo_lock( ei->bei_parent );
660 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
661 bdb_cache_entryinfo_unlock( ei->bei_parent );
664 /* Not found, add it */
667 /* bei_rdn was not malloc'd before, do it now */
668 ber_dupbv( &bv, &ei->bei_rdn );
671 rc = bdb_entryinfo_add_internal( bdb, ei, res );
672 bdb_cache_entryinfo_unlock( ei->bei_parent );
673 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
675 /* Found, return it */
683 /* This is best-effort only. If all entries in the cache are
684 * busy, they will all be kept. This is unlikely to happen
685 * unless the cache is very much smaller than the working set.
688 bdb_cache_lru_purge( struct bdb_info *bdb )
690 DB_LOCK lock, *lockp;
691 EntryInfo *elru, *elnext = NULL;
694 ID count, efree, eifree = 0;
699 /* Wait for the mutex; we're the only one trying to purge. */
700 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
702 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
703 efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
704 efree += bdb->bi_cache.c_minfree;
709 /* maximum number of EntryInfo leaves to cache. In slapcat
710 * we always free all leaf nodes.
713 if ( slapMode & SLAP_TOOL_READONLY ) {
714 eifree = bdb->bi_cache.c_leaves;
715 } else if ( bdb->bi_cache.c_eimax &&
716 bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
717 eifree = bdb->bi_cache.c_minfree * 10;
718 if ( eifree >= bdb->bi_cache.c_leaves )
722 if ( !efree && !eifree ) {
723 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
724 bdb->bi_cache.c_purging = 0;
728 if ( bdb->bi_cache.c_txn ) {
741 /* Look for an unused entry to remove */
742 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
743 elnext = elru->bei_lrunext;
745 if ( bdb_cache_entryinfo_trylock( elru ))
748 /* This flag implements the clock replacement behavior */
749 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
750 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
751 bdb_cache_entryinfo_unlock( elru );
755 /* If this node is in the process of linking into the cache,
756 * or this node is being deleted, skip it.
758 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
759 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING |
760 CACHE_ENTRY_ONELEVEL )) ||
761 elru->bei_finders > 0 ) {
762 bdb_cache_entryinfo_unlock( elru );
766 if ( bdb_cache_entryinfo_trylock( elru->bei_parent )) {
767 bdb_cache_entryinfo_unlock( elru );
771 /* entryinfo is locked */
774 /* If we can successfully writelock it, then
775 * the object is idle.
777 if ( bdb_cache_entry_db_lock( bdb,
778 bdb->bi_cache.c_txn, elru, 1, 1, lockp ) == 0 ) {
780 /* Free entry for this node if it's present */
784 /* the cache may have gone over the limit while we
785 * weren't looking, so double check.
787 if ( !efree && ecount > bdb->bi_cache.c_maxsize )
788 efree = bdb->bi_cache.c_minfree;
790 if ( count < efree ) {
791 elru->bei_e->e_private = NULL;
792 #ifdef SLAP_ZONE_ALLOC
793 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
795 bdb_entry_return( elru->bei_e );
800 /* Keep this node cached, skip to next */
801 bdb_cache_entry_db_unlock( bdb, lockp );
805 bdb_cache_entry_db_unlock( bdb, lockp );
808 * If it is a leaf node, and we're over the limit, free it.
810 if ( elru->bei_kids ) {
811 /* Drop from list, we ignore it... */
812 LRU_DEL( &bdb->bi_cache, elru );
813 } else if ( eicount < eifree ) {
814 /* Too many leaf nodes, free this one */
815 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
816 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
819 } /* Leave on list until we need to free it */
824 bdb_cache_entryinfo_unlock( elru );
825 bdb_cache_entryinfo_unlock( elru->bei_parent );
828 if ( count >= efree && eicount >= eifree )
831 if ( elnext == bdb->bi_cache.c_lruhead )
838 if ( count || ecount > bdb->bi_cache.c_cursize ) {
839 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
840 /* HACK: we seem to be losing track, fix up now */
841 if ( ecount > bdb->bi_cache.c_cursize )
842 bdb->bi_cache.c_cursize = ecount;
843 bdb->bi_cache.c_cursize -= count;
844 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
846 bdb->bi_cache.c_lruhead = elnext;
847 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
848 bdb->bi_cache.c_purging = 0;
852 * cache_find_id - find an entry in the cache, given id.
853 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
854 * the supplied *eip was already locked.
866 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
868 int rc = 0, load = 0;
869 EntryInfo ei = { 0 };
873 #ifdef SLAP_ZONE_ALLOC
874 slap_zh_rlock(bdb->bi_cache.c_zctx);
876 /* If we weren't given any info, see if we have it already cached */
878 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
879 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
880 (caddr_t) &ei, bdb_id_cmp );
882 /* If the lock attempt fails, the info is in use */
883 if ( bdb_cache_entryinfo_trylock( *eip )) {
884 int del = (*eip)->bei_state & CACHE_ENTRY_DELETED;
885 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
886 /* If this node is being deleted, treat
887 * as if the delete has already finished
892 /* otherwise, wait for the info to free up */
893 ldap_pvt_thread_yield();
896 /* If this info isn't hooked up to its parent yet,
897 * unlock and wait for it to be fully initialized
899 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
900 bdb_cache_entryinfo_unlock( *eip );
901 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
902 ldap_pvt_thread_yield();
907 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
910 /* See if the ID exists in the database; add it to the cache if so */
913 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
915 rc = bdb_cache_find_ndn( op, tid,
917 if ( *eip ) flag |= ID_LOCKED;
919 ep->e_private = NULL;
920 #ifdef SLAP_ZONE_ALLOC
921 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
923 bdb_entry_return( ep );
929 rc = hdb_cache_find_parent(op, tid, id, eip );
930 if ( rc == 0 ) flag |= ID_LOCKED;
934 /* Ok, we found the info, do we have the entry? */
936 if ( !( flag & ID_LOCKED )) {
937 bdb_cache_entryinfo_lock( *eip );
941 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
944 (*eip)->bei_finders++;
945 (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
946 if ( flag & ID_NOENTRY ) {
947 bdb_cache_entryinfo_unlock( *eip );
950 /* Make sure only one thread tries to load the entry */
952 #ifdef SLAP_ZONE_ALLOC
953 if ((*eip)->bei_e && !slap_zn_validate(
954 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
955 (*eip)->bei_e = NULL;
956 (*eip)->bei_zseq = 0;
959 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
961 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
965 /* Clear the uncached state if we are not
966 * loading it, i.e it is already cached or
967 * another thread is currently loading it.
969 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
970 (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
975 if ( flag & ID_LOCKED ) {
976 bdb_cache_entryinfo_unlock( *eip );
979 rc = bdb_cache_entry_db_lock( bdb, tid, *eip, load, 0, lock );
980 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
982 bdb_cache_entry_db_unlock( bdb, lock );
983 bdb_cache_entryinfo_lock( *eip );
984 (*eip)->bei_finders--;
985 bdb_cache_entryinfo_unlock( *eip );
986 } else if ( rc == 0 ) {
989 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
992 ep->e_private = *eip;
994 while ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED )
995 ldap_pvt_thread_yield();
999 #ifdef SLAP_ZONE_ALLOC
1000 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
1003 bdb_cache_lru_link( bdb, *eip );
1004 if (( flag & ID_NOCACHE ) &&
1005 ( bdb_cache_entryinfo_trylock( *eip ) == 0 )) {
1006 /* Set the cached state only if no other thread
1007 * found the info while we were loading the entry.
1009 if ( (*eip)->bei_finders == 1 )
1010 (*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
1011 bdb_cache_entryinfo_unlock( *eip );
1015 /* If we succeeded, downgrade back to a readlock. */
1016 rc = bdb_cache_entry_db_relock( bdb, tid,
1019 /* Otherwise, release the lock. */
1020 bdb_cache_entry_db_unlock( bdb, lock );
1022 } else if ( !(*eip)->bei_e ) {
1023 /* Some other thread is trying to load the entry,
1024 * wait for it to finish.
1026 bdb_cache_entry_db_unlock( bdb, lock );
1027 bdb_cache_entryinfo_lock( *eip );
1032 /* Check for subtree renames
1034 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
1036 bdb_cache_entry_db_relock( bdb,
1037 tid, *eip, 1, 0, lock );
1038 /* check again in case other modifier did it already */
1039 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
1040 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
1041 bdb_cache_entry_db_relock( bdb,
1042 tid, *eip, 0, 0, lock );
1046 bdb_cache_entryinfo_lock( *eip );
1047 (*eip)->bei_finders--;
1049 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
1050 bdb_cache_entryinfo_unlock( *eip );
1054 if ( flag & ID_LOCKED ) {
1055 bdb_cache_entryinfo_unlock( *eip );
1058 ep->e_private = NULL;
1059 #ifdef SLAP_ZONE_ALLOC
1060 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
1062 bdb_entry_return( ep );
1068 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ||
1069 ( bdb->bi_cache.c_eimax && bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax )) {
1070 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1071 if ( !bdb->bi_cache.c_purging ) {
1072 if ( load && !( flag & ID_NOCACHE )) {
1073 bdb->bi_cache.c_cursize++;
1074 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
1076 bdb->bi_cache.c_purging = 1;
1078 } else if ( bdb->bi_cache.c_eimax && bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
1080 bdb->bi_cache.c_purging = 1;
1083 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1086 bdb_cache_lru_purge( bdb );
1089 #ifdef SLAP_ZONE_ALLOC
1090 if (rc == 0 && (*eip)->bei_e) {
1091 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
1093 slap_zh_runlock(bdb->bi_cache.c_zctx);
1106 if ( BEI(e)->bei_kids ) {
1109 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
1112 rc = bdb_dn2id_children( op, txn, e );
1113 if ( rc == DB_NOTFOUND ) {
1114 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1119 /* Update the cache after a successful database Add. */
1122 struct bdb_info *bdb,
1125 struct berval *nrdn,
1132 struct berval rdn = e->e_name;
1135 ei.bei_id = e->e_id;
1136 ei.bei_parent = eip;
1137 ei.bei_nrdn = *nrdn;
1140 /* Lock this entry so that bdb_add can run to completion.
1141 * It can only fail if BDB has run out of lock resources.
1143 rc = bdb_cache_entry_db_lock( bdb, txn, &ei, 0, 0, lock );
1145 bdb_cache_entryinfo_unlock( eip );
1150 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1151 char *ptr = ber_bvchr( &rdn, ',' );
1152 assert( ptr != NULL );
1153 rdn.bv_len = ptr - rdn.bv_val;
1155 ber_dupbv( &ei.bei_rdn, &rdn );
1156 if ( eip->bei_dkids ) eip->bei_dkids++;
1159 if (eip->bei_parent) {
1160 bdb_cache_entryinfo_lock( eip->bei_parent );
1161 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1162 bdb_cache_entryinfo_unlock( eip->bei_parent );
1165 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1166 /* bdb_csn_commit can cause this when adding the database root entry */
1168 new->bei_e->e_private = NULL;
1169 #ifdef SLAP_ZONE_ALLOC
1170 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1172 bdb_entry_return( new->bei_e );
1177 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1178 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1179 bdb_cache_entryinfo_unlock( eip );
1181 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1182 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1183 ++bdb->bi_cache.c_cursize;
1184 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1185 !bdb->bi_cache.c_purging ) {
1187 bdb->bi_cache.c_purging = 1;
1189 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1191 bdb_cache_lru_link( bdb, new );
1194 bdb_cache_lru_purge( bdb );
1201 struct bdb_info *bdb,
1203 Attribute *newAttrs,
1207 EntryInfo *ei = BEI(e);
1209 /* Get write lock on data */
1210 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
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.
1216 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1217 attrs_free( e->e_attrs );
1219 e->e_attrs = newAttrs;
1225 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1229 struct bdb_info *bdb,
1231 struct berval *nrdn,
1237 EntryInfo *ei = BEI(e), *pei;
1243 /* Get write lock on data */
1244 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1245 if ( rc ) return rc;
1247 /* If we've done repeated mods on a cached entry, then e_attrs
1248 * is no longer contiguous with the entry, and must be freed.
1250 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1251 attrs_free( e->e_attrs );
1253 e->e_attrs = new->e_attrs;
1254 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1255 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1257 ch_free(e->e_name.bv_val);
1258 ch_free(e->e_nname.bv_val);
1260 e->e_name = new->e_name;
1261 e->e_nname = new->e_nname;
1263 /* Lock the parent's kids AVL tree */
1264 pei = ei->bei_parent;
1265 bdb_cache_entryinfo_lock( pei );
1266 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1267 free( ei->bei_nrdn.bv_val );
1268 ber_dupbv( &ei->bei_nrdn, nrdn );
1271 free( ei->bei_rdn.bv_val );
1274 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1275 char *ptr = ber_bvchr(&rdn, ',');
1276 assert( ptr != NULL );
1277 rdn.bv_len = ptr - rdn.bv_val;
1279 ber_dupbv( &ei->bei_rdn, &rdn );
1281 /* If new parent, decrement kid counts */
1284 if ( pei->bei_dkids ) {
1286 if ( pei->bei_dkids < 2 )
1287 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1293 ein = ei->bei_parent;
1295 ei->bei_parent = ein;
1296 bdb_cache_entryinfo_unlock( pei );
1297 bdb_cache_entryinfo_lock( ein );
1299 /* new parent now has kids */
1300 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1301 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1302 /* grandparent has grandkids */
1303 if ( ein->bei_parent )
1304 ein->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1306 /* parent might now have grandkids */
1307 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1308 !(ei->bei_state & CACHE_ENTRY_NO_KIDS))
1309 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1312 if ( ein->bei_dkids ) ein->bei_dkids++;
1317 /* Record the generation number of this change */
1318 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1320 ei->bei_modrdns = bdb->bi_modrdns;
1321 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1324 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1325 bdb_cache_entryinfo_unlock( ein );
1329 * cache_delete - delete the entry e from the cache.
1331 * returns: 0 e was deleted ok
1332 * 1 e was not in the cache
1333 * -1 something bad happened
1337 struct bdb_info *bdb,
1342 EntryInfo *ei = BEI(e);
1345 assert( e->e_private != NULL );
1347 /* Lock the entry's info */
1348 bdb_cache_entryinfo_lock( ei );
1350 /* Set this early, warn off any queriers */
1351 ei->bei_state |= CACHE_ENTRY_DELETED;
1353 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1354 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1355 ei->bei_finders > 0 )
1358 bdb_cache_entryinfo_unlock( ei );
1361 ldap_pvt_thread_yield();
1363 bdb_cache_entryinfo_lock( ei );
1364 if (( ei->bei_state & ( CACHE_ENTRY_NOT_LINKED |
1365 CACHE_ENTRY_LOADING | CACHE_ENTRY_ONELEVEL )) ||
1366 ei->bei_finders > 0 )
1368 bdb_cache_entryinfo_unlock( ei );
1371 /* Get write lock on the data */
1372 rc = bdb_cache_entry_db_relock( bdb, txn, ei, 1, 0, lock );
1374 bdb_cache_entryinfo_lock( ei );
1375 /* couldn't lock, undo and give up */
1376 ei->bei_state ^= CACHE_ENTRY_DELETED;
1377 bdb_cache_entryinfo_unlock( ei );
1381 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1385 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1387 bdb_cache_entryinfo_lock( ei->bei_parent );
1388 bdb_cache_entryinfo_lock( ei );
1389 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1390 bdb_cache_entryinfo_unlock( ei );
1392 /* free lru mutex */
1393 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1399 bdb_cache_delete_cleanup(
1403 /* Enter with ei locked */
1405 /* already freed? */
1406 if ( !ei->bei_parent ) return;
1409 ei->bei_e->e_private = NULL;
1410 #ifdef SLAP_ZONE_ALLOC
1411 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1413 bdb_entry_return( ei->bei_e );
1418 bdb_cache_entryinfo_unlock( ei );
1419 bdb_cache_entryinfo_free( cache, ei );
1423 bdb_cache_delete_internal(
1428 int rc = 0; /* return code */
1431 /* already freed? */
1432 if ( !e->bei_parent ) {
1438 e->bei_parent->bei_ckids--;
1439 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1442 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1448 if ( e->bei_parent->bei_kids )
1451 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1453 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1461 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1462 bdb_cache_entryinfo_unlock( e->bei_parent );
1466 LRU_DEL( cache, e );
1469 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1471 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1479 bdb_entryinfo_release( void *data )
1481 EntryInfo *ei = (EntryInfo *)data;
1482 if ( ei->bei_kids ) {
1483 avl_free( ei->bei_kids, NULL );
1486 ei->bei_e->e_private = NULL;
1487 #ifdef SLAP_ZONE_ALLOC
1488 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1490 bdb_entry_return( ei->bei_e );
1493 bdb_cache_entryinfo_destroy( ei );
1497 bdb_cache_release_all( Cache *cache )
1499 /* set cache write lock */
1500 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1502 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1504 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1506 avl_free( cache->c_dntree.bei_kids, NULL );
1507 avl_free( cache->c_idtree, bdb_entryinfo_release );
1508 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1509 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1510 bdb_cache_entryinfo_destroy(cache->c_eifree);
1512 cache->c_cursize = 0;
1513 cache->c_eiused = 0;
1514 cache->c_leaves = 0;
1515 cache->c_idtree = NULL;
1516 cache->c_lruhead = NULL;
1517 cache->c_lrutail = NULL;
1518 cache->c_dntree.bei_kids = NULL;
1520 /* free lru mutex */
1521 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1522 /* free cache write lock */
1523 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1529 bdb_lru_print( Cache *cache )
1533 fprintf( stderr, "LRU circle head: %p\n", (void *) cache->c_lruhead );
1534 fprintf( stderr, "LRU circle (tail forward):\n" );
1535 for ( e = cache->c_lrutail; ; ) {
1536 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1537 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1539 if ( e == cache->c_lrutail )
1542 fprintf( stderr, "LRU circle (tail backward):\n" );
1543 for ( e = cache->c_lrutail; ; ) {
1544 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1545 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1547 if ( e == cache->c_lrutail )
1553 bdb_entryinfo_print(void *data, void *arg)
1555 EntryInfo *e = data;
1556 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1557 (void *) e, (void *) e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1562 bdb_idtree_print(Cache *cache)
1564 avl_apply( cache->c_idtree, bdb_entryinfo_print, NULL, -1, AVL_INORDER );
1570 bdb_reader_free( void *key, void *data )
1572 /* DB_ENV *env = key; */
1575 if ( txn ) TXN_ABORT( txn );
1578 /* free up any keys used by the main thread */
1580 bdb_reader_flush( DB_ENV *env )
1583 void *ctx = ldap_pvt_thread_pool_context();
1585 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1586 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
1587 bdb_reader_free( env, data );
1592 bdb_reader_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1598 if ( !env || !txn ) return -1;
1600 /* If no op was provided, try to find the ctx anyway... */
1602 ctx = op->o_threadctx;
1604 ctx = ldap_pvt_thread_pool_context();
1607 /* Shouldn't happen unless we're single-threaded */
1613 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1614 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1615 rc = TXN_BEGIN( env, NULL, txn, DB_READ_COMMITTED );
1616 if (rc) ldap_pvt_thread_yield();
1622 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1623 data, bdb_reader_free, NULL, NULL ) ) ) {
1625 Debug( LDAP_DEBUG_ANY, "bdb_reader_get: err %s(%d)\n",
1626 db_strerror(rc), rc, 0 );