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-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
32 #define bdb_cache_lru_purge hdb_cache_lru_purge
34 static void bdb_cache_lru_purge( struct bdb_info *bdb );
36 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
40 static void bdb_lru_print(Cache *cache);
44 /* For concurrency experiments only! */
46 #define ldap_pvt_thread_rdwr_wlock(a) 0
47 #define ldap_pvt_thread_rdwr_wunlock(a) 0
48 #define ldap_pvt_thread_rdwr_rlock(a) 0
49 #define ldap_pvt_thread_rdwr_runlock(a) 0
53 #define ldap_pvt_thread_mutex_trylock(a) 0
57 bdb_cache_entryinfo_new( Cache *cache )
61 if ( cache->c_eifree ) {
62 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
63 if ( cache->c_eifree ) {
65 cache->c_eifree = ei->bei_lrunext;
67 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
71 ei = ch_calloc(1, sizeof(EntryInfo));
72 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
75 ei->bei_state = CACHE_ENTRY_REFERENCED;
81 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
83 free( ei->bei_nrdn.bv_val );
84 ei->bei_nrdn.bv_val = NULL;
86 free( ei->bei_rdn.bv_val );
87 ei->bei_rdn.bv_val = NULL;
92 ei->bei_parent = NULL;
94 ei->bei_lruprev = NULL;
96 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
97 ei->bei_lrunext = cache->c_eifree;
99 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
102 #define LRU_DEL( c, e ) do { \
103 if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
104 if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
105 e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
106 e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
107 e->bei_lruprev = NULL; \
110 /* Note - we now use a Second-Chance / Clock algorithm instead of
111 * Least-Recently-Used. This tremendously improves concurrency
112 * because we no longer need to manipulate the lists every time an
113 * entry is touched. We only need to lock the lists when adding
114 * or deleting an entry. It's now a circular doubly-linked list.
115 * We always append to the tail, but the head traverses the circle
116 * during a purge operation.
119 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
122 /* Insert into circular LRU list */
123 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
125 /* Still linked, remove */
126 if ( ei->bei_lruprev ) {
127 LRU_DEL( &bdb->bi_cache, ei );
129 ei->bei_lruprev = bdb->bi_cache.c_lrutail;
130 if ( bdb->bi_cache.c_lrutail ) {
131 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
132 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
133 if ( ei->bei_lrunext )
134 ei->bei_lrunext->bei_lruprev = ei;
136 ei->bei_lrunext = ei->bei_lruprev = ei;
137 bdb->bi_cache.c_lruhead = ei;
139 bdb->bi_cache.c_lrutail = ei;
140 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
147 /* #define NO_DB_LOCK 1 */
148 /* Note: The BerkeleyDB locks are much slower than regular
149 * mutexes or rdwr locks. But the BDB implementation has the
150 * advantage of using a fixed size lock table, instead of
151 * allocating a lock object per entry in the DB. That's a
152 * key benefit for scaling. It also frees us from worrying
153 * about undetectable deadlocks between BDB activity and our
154 * own cache activity. It's still worth exploring faster
155 * alternatives though.
158 /* Atomically release and reacquire a lock */
160 bdb_cache_entry_db_relock(
161 struct bdb_info *bdb,
175 if ( !lock ) return 0;
177 lockobj.data = &ei->bei_id;
178 lockobj.size = sizeof(ei->bei_id) + 1;
180 list[0].op = DB_LOCK_PUT;
181 list[0].lock = *lock;
182 list[1].op = DB_LOCK_GET;
183 list[1].lock = *lock;
184 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
185 list[1].obj = &lockobj;
186 rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
189 if (rc && !tryOnly) {
190 Debug( LDAP_DEBUG_TRACE,
191 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
192 ei->bei_id, rw, rc );
194 *lock = list[1].lock;
201 bdb_cache_entry_db_lock( struct bdb_info *bdb, u_int32_t locker, EntryInfo *ei,
202 int rw, int tryOnly, DB_LOCK *lock )
211 if ( !lock ) return 0;
214 db_rw = DB_LOCK_WRITE;
216 db_rw = DB_LOCK_READ;
218 lockobj.data = &ei->bei_id;
219 lockobj.size = sizeof(ei->bei_id) + 1;
221 rc = LOCK_GET(bdb->bi_dbenv, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
222 &lockobj, db_rw, lock);
223 if (rc && !tryOnly) {
224 Debug( LDAP_DEBUG_TRACE,
225 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
226 ei->bei_id, rw, rc );
229 #endif /* NO_DB_LOCK */
233 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
240 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
242 rc = LOCK_PUT ( bdb->bi_dbenv, lock );
248 bdb_cache_entryinfo_destroy( EntryInfo *e )
250 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
251 free( e->bei_nrdn.bv_val );
253 free( e->bei_rdn.bv_val );
259 /* Do a length-ordered sort on normalized RDNs */
261 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
263 const EntryInfo *e1 = v_e1, *e2 = v_e2;
264 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
266 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
267 e1->bei_nrdn.bv_len );
273 bdb_id_cmp( const void *v_e1, const void *v_e2 )
275 const EntryInfo *e1 = v_e1, *e2 = v_e2;
276 return e1->bei_id - e2->bei_id;
280 bdb_id_dup_err( void *v1, void *v2 )
287 /* Create an entryinfo in the cache. Caller must release the locks later.
290 bdb_entryinfo_add_internal(
291 struct bdb_info *bdb,
295 EntryInfo *ei2 = NULL;
299 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
301 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
302 bdb_cache_entryinfo_lock( ei->bei_parent );
304 ei2->bei_id = ei->bei_id;
305 ei2->bei_parent = ei->bei_parent;
307 ei2->bei_rdn = ei->bei_rdn;
309 #ifdef SLAP_ZONE_ALLOC
313 /* Add to cache ID tree */
314 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
316 EntryInfo *eix = (EntryInfo *)ei2->bei_e;
317 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
320 /* It got freed above because its value was
323 ei->bei_rdn.bv_val = NULL;
326 bdb->bi_cache.c_eiused++;
327 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
329 /* This is a new leaf node. But if parent had no kids, then it was
330 * a leaf and we would be decrementing that. So, only increment if
331 * the parent already has kids.
333 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
334 bdb->bi_cache.c_leaves++;
335 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
338 ei->bei_parent->bei_ckids++;
346 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
347 * the info for its closest ancestor. *res should be NULL to process a
348 * complete DN starting from the tree root. Otherwise *res must be the
349 * immediate parent of the requested DN, and only the RDN will be searched.
350 * The EntryInfo is locked upon return and must be unlocked by the caller.
359 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
360 EntryInfo ei, *eip, *ei2;
364 /* this function is always called with normalized DN */
366 /* we're doing a onelevel search for an RDN */
367 ei.bei_nrdn.bv_val = ndn->bv_val;
368 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
371 /* we're searching a full DN from the root */
372 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
373 ei.bei_nrdn.bv_val = ptr;
374 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
375 /* Skip to next rdn if suffix is empty */
376 if ( ei.bei_nrdn.bv_len == 0 ) {
377 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
378 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
379 if ( ptr >= ndn->bv_val ) {
380 if (DN_SEPARATOR(*ptr)) ptr++;
381 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
382 ei.bei_nrdn.bv_val = ptr;
385 eip = &bdb->bi_cache.c_dntree;
388 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
389 eip->bei_state |= CACHE_ENTRY_REFERENCED;
391 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
393 int len = ei.bei_nrdn.bv_len;
395 if ( BER_BVISEMPTY( ndn )) {
400 ei.bei_nrdn.bv_len = ndn->bv_len -
401 (ei.bei_nrdn.bv_val - ndn->bv_val);
402 bdb_cache_entryinfo_unlock( eip );
404 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
406 bdb_cache_entryinfo_lock( eip );
411 /* DN exists but needs to be added to cache */
412 ei.bei_nrdn.bv_len = len;
413 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
414 /* add_internal left eip and c_rwlock locked */
415 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
420 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
421 /* In the midst of deleting? Give it a chance to
424 bdb_cache_entryinfo_unlock( eip );
425 ldap_pvt_thread_yield();
426 bdb_cache_entryinfo_lock( eip );
430 bdb_cache_entryinfo_unlock( eip );
431 bdb_cache_entryinfo_lock( ei2 );
435 /* Advance to next lower RDN */
436 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
437 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
438 if ( ptr >= ndn->bv_val ) {
439 if (DN_SEPARATOR(*ptr)) ptr++;
440 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
441 ei.bei_nrdn.bv_val = ptr;
443 if ( ptr < ndn->bv_val ) {
453 /* Walk up the tree from a child node, looking for an ID that's already
454 * been linked into the cache.
457 hdb_cache_find_parent(
464 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
465 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
473 rc = hdb_dn2id_parent( op, txn, locker, &ei, &eip.bei_id );
476 /* Save the previous node, if any */
479 /* Create a new node for the current ID */
480 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
481 ein->bei_id = ei.bei_id;
482 ein->bei_kids = ei.bei_kids;
483 ein->bei_nrdn = ei.bei_nrdn;
484 ein->bei_rdn = ei.bei_rdn;
485 ein->bei_ckids = ei.bei_ckids;
486 #ifdef SLAP_ZONE_ALLOC
491 /* This node is not fully connected yet */
492 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
494 /* Insert this node into the ID tree */
495 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
496 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
497 bdb_id_cmp, bdb_id_dup_err ) ) {
498 EntryInfo *eix = (EntryInfo *)ein->bei_e;
500 /* Someone else created this node just before us.
501 * Free our new copy and use the existing one.
503 bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
506 /* Link in any kids we've already processed */
508 bdb_cache_entryinfo_lock( ein );
509 avl_insert( &ein->bei_kids, (caddr_t)ei2,
510 bdb_rdn_cmp, avl_dup_error );
512 bdb_cache_entryinfo_unlock( ein );
516 /* If this is the first time, save this node
517 * to be returned later.
519 if ( eir == NULL ) eir = ein;
521 /* If there was a previous node, link it to this one */
522 if ( ei2 ) ei2->bei_parent = ein;
524 /* Look for this node's parent */
526 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
527 (caddr_t) &eip, bdb_id_cmp );
529 ei2 = &bdb->bi_cache.c_dntree;
531 bdb->bi_cache.c_eiused++;
532 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
533 bdb->bi_cache.c_leaves++;
534 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
536 /* Got the parent, link in and we're done. */
538 bdb_cache_entryinfo_lock( ei2 );
539 ein->bei_parent = ei2;
541 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
545 /* Reset all the state info */
546 for (ein = eir; ein != ei2; ein=ein->bei_parent)
547 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
549 bdb_cache_entryinfo_unlock( ei2 );
550 bdb_cache_entryinfo_lock( eir );
556 ei.bei_id = eip.bei_id;
558 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
564 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
568 struct bdb_info *bdb,
575 /* See if we already have this one */
576 bdb_cache_entryinfo_lock( ei->bei_parent );
577 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
578 bdb_cache_entryinfo_unlock( ei->bei_parent );
581 /* Not found, add it */
584 /* bei_rdn was not malloc'd before, do it now */
585 ber_dupbv( &bv, &ei->bei_rdn );
588 rc = bdb_entryinfo_add_internal( bdb, ei, res );
589 bdb_cache_entryinfo_unlock( ei->bei_parent );
590 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
592 /* Found, return it */
600 /* This is best-effort only. If all entries in the cache are
601 * busy, they will all be kept. This is unlikely to happen
602 * unless the cache is very much smaller than the working set.
605 bdb_cache_lru_purge( struct bdb_info *bdb )
607 DB_LOCK lock, *lockp;
608 EntryInfo *elru, *elnext = NULL;
609 int count, islocked, eimax;
611 /* Wait for the mutex; we're the only one trying to purge. */
612 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
614 if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
615 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
616 bdb->bi_cache.c_purging = 0;
620 if ( bdb->bi_cache.c_locker ) {
628 /* maximum number of EntryInfo leaves to cache. In slapcat
629 * we always free all leaf nodes.
631 if ( slapMode & SLAP_TOOL_READONLY )
634 eimax = bdb->bi_cache.c_maxsize * 4;
636 /* Look for an unused entry to remove */
637 for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
638 elnext = elru->bei_lrunext;
640 if ( bdb_cache_entryinfo_trylock( elru ))
643 /* This flag implements the clock replacement behavior */
644 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
645 elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
646 bdb_cache_entryinfo_unlock( elru );
650 /* If this node is in the process of linking into the cache,
651 * or this node is being deleted, skip it.
653 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
654 CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
655 elru->bei_finders > 0 ) {
656 bdb_cache_entryinfo_unlock( elru );
660 /* entryinfo is locked */
663 /* If we can successfully writelock it, then
664 * the object is idle.
666 if ( bdb_cache_entry_db_lock( bdb,
667 bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
669 /* Free entry for this node if it's present */
671 elru->bei_e->e_private = NULL;
672 #ifdef SLAP_ZONE_ALLOC
673 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
675 bdb_entry_return( elru->bei_e );
680 bdb_cache_entry_db_unlock( bdb, lockp );
683 * If it is a leaf node, and we're over the limit, free it.
685 if ( elru->bei_kids ) {
686 /* Drop from list, we ignore it... */
687 LRU_DEL( &bdb->bi_cache, elru );
688 } else if ( bdb->bi_cache.c_leaves > eimax ) {
689 /* Too many leaf nodes, free this one */
690 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
691 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
693 } /* Leave on list until we need to free it */
697 bdb_cache_entryinfo_unlock( elru );
699 if ( count >= bdb->bi_cache.c_minfree ) {
700 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
701 bdb->bi_cache.c_cursize -= count;
702 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
706 if ( elnext == bdb->bi_cache.c_lruhead )
710 bdb->bi_cache.c_lruhead = elnext;
711 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
712 bdb->bi_cache.c_purging = 0;
717 struct bdb_info *bdb,
720 EntryInfo ei = { 0 },
725 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
726 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
727 (caddr_t) &ei, bdb_id_cmp );
728 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
733 * cache_find_id - find an entry in the cache, given id.
734 * The entry is locked for Read upon return. Call with islocked TRUE if
735 * the supplied *eip was already locked.
748 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
750 int rc = 0, load = 0;
751 EntryInfo ei = { 0 };
755 #ifdef SLAP_ZONE_ALLOC
756 slap_zh_rlock(bdb->bi_cache.c_zctx);
758 /* If we weren't given any info, see if we have it already cached */
760 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
761 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
762 (caddr_t) &ei, bdb_id_cmp );
764 /* If the lock attempt fails, the info is in use */
765 if ( bdb_cache_entryinfo_trylock( *eip )) {
766 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
767 /* If this node is being deleted, treat
768 * as if the delete has already finished
770 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
773 /* otherwise, wait for the info to free up */
774 ldap_pvt_thread_yield();
777 /* If this info isn't hooked up to its parent yet,
778 * unlock and wait for it to be fully initialized
780 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
781 bdb_cache_entryinfo_unlock( *eip );
782 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
783 ldap_pvt_thread_yield();
788 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
791 /* See if the ID exists in the database; add it to the cache if so */
794 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
796 rc = bdb_cache_find_ndn( op, tid,
798 if ( *eip ) islocked = 1;
800 ep->e_private = NULL;
801 #ifdef SLAP_ZONE_ALLOC
802 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
804 bdb_entry_return( ep );
810 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
811 if ( rc == 0 ) islocked = 1;
815 /* Ok, we found the info, do we have the entry? */
817 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
820 (*eip)->bei_finders++;
821 /* Make sure only one thread tries to load the entry */
823 #ifdef SLAP_ZONE_ALLOC
824 if ((*eip)->bei_e && !slap_zn_validate(
825 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
826 (*eip)->bei_e = NULL;
827 (*eip)->bei_zseq = 0;
830 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
832 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
836 bdb_cache_entryinfo_unlock( *eip );
839 rc = bdb_cache_entry_db_lock( bdb, locker, *eip, load, 0, lock );
840 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
842 bdb_cache_entry_db_unlock( bdb, lock );
843 } else if ( rc == 0 ) {
846 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
849 ep->e_private = *eip;
854 #ifdef SLAP_ZONE_ALLOC
855 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
858 bdb_cache_lru_link( bdb, *eip );
861 /* If we succeeded, downgrade back to a readlock. */
862 rc = bdb_cache_entry_db_relock( bdb, locker,
865 /* Otherwise, release the lock. */
866 bdb_cache_entry_db_unlock( bdb, lock );
868 } else if ( !(*eip)->bei_e ) {
869 /* Some other thread is trying to load the entry,
870 * wait for it to finish.
872 bdb_cache_entry_db_unlock( bdb, lock );
873 bdb_cache_entryinfo_lock( *eip );
878 /* Check for subtree renames
880 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
882 bdb_cache_entry_db_relock( bdb,
883 locker, *eip, 1, 0, lock );
884 /* check again in case other modifier did it already */
885 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
886 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
887 bdb_cache_entry_db_relock( bdb,
888 locker, *eip, 0, 0, lock );
892 bdb_cache_entryinfo_lock( *eip );
893 (*eip)->bei_finders--;
895 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
896 bdb_cache_entryinfo_unlock( *eip );
901 bdb_cache_entryinfo_unlock( *eip );
904 ep->e_private = NULL;
905 #ifdef SLAP_ZONE_ALLOC
906 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
908 bdb_entry_return( ep );
915 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
916 bdb->bi_cache.c_cursize++;
917 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
918 !bdb->bi_cache.c_purging ) {
920 bdb->bi_cache.c_purging = 1;
922 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
925 bdb_cache_lru_purge( bdb );
928 #ifdef SLAP_ZONE_ALLOC
929 if (rc == 0 && (*eip)->bei_e) {
930 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
932 slap_zh_runlock(bdb->bi_cache.c_zctx);
945 if ( BEI(e)->bei_kids ) {
948 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
951 rc = bdb_dn2id_children( op, txn, e );
952 if ( rc == DB_NOTFOUND ) {
953 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
958 /* Update the cache after a successful database Add. */
961 struct bdb_info *bdb,
971 struct berval rdn = e->e_name;
979 /* Lock this entry so that bdb_add can run to completion.
980 * It can only fail if BDB has run out of lock resources.
982 rc = bdb_cache_entry_db_lock( bdb, locker, &ei, 0, 0, lock );
984 bdb_cache_entryinfo_unlock( eip );
989 if ( nrdn->bv_len != e->e_nname.bv_len ) {
990 char *ptr = ber_bvchr( &rdn, ',' );
991 assert( ptr != NULL );
992 rdn.bv_len = ptr - rdn.bv_val;
994 ber_dupbv( &ei.bei_rdn, &rdn );
995 if ( eip->bei_dkids ) eip->bei_dkids++;
998 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
999 /* bdb_csn_commit can cause this when adding the database root entry */
1001 new->bei_e->e_private = NULL;
1002 #ifdef SLAP_ZONE_ALLOC
1003 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1005 bdb_entry_return( new->bei_e );
1010 new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1011 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1012 if (eip->bei_parent) {
1013 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1015 bdb_cache_entryinfo_unlock( eip );
1017 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1018 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1019 ++bdb->bi_cache.c_cursize;
1020 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1021 !bdb->bi_cache.c_purging ) {
1023 bdb->bi_cache.c_purging = 1;
1025 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1027 bdb_cache_lru_link( bdb, new );
1030 bdb_cache_lru_purge( bdb );
1037 struct bdb_info *bdb,
1039 Attribute *newAttrs,
1043 EntryInfo *ei = BEI(e);
1045 /* Get write lock on data */
1046 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1048 /* If we've done repeated mods on a cached entry, then e_attrs
1049 * is no longer contiguous with the entry, and must be freed.
1052 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1053 attrs_free( e->e_attrs );
1055 e->e_attrs = newAttrs;
1061 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1065 struct bdb_info *bdb,
1067 struct berval *nrdn,
1073 EntryInfo *ei = BEI(e), *pei;
1079 /* Get write lock on data */
1080 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1081 if ( rc ) return rc;
1083 /* If we've done repeated mods on a cached entry, then e_attrs
1084 * is no longer contiguous with the entry, and must be freed.
1086 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1087 attrs_free( e->e_attrs );
1089 e->e_attrs = new->e_attrs;
1090 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1091 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1093 ch_free(e->e_name.bv_val);
1094 ch_free(e->e_nname.bv_val);
1096 e->e_name = new->e_name;
1097 e->e_nname = new->e_nname;
1099 /* Lock the parent's kids AVL tree */
1100 pei = ei->bei_parent;
1101 bdb_cache_entryinfo_lock( pei );
1102 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1103 free( ei->bei_nrdn.bv_val );
1104 ber_dupbv( &ei->bei_nrdn, nrdn );
1106 if ( !pei->bei_kids )
1107 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1110 free( ei->bei_rdn.bv_val );
1113 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1114 char *ptr = ber_bvchr(&rdn, ',');
1115 assert( ptr != NULL );
1116 rdn.bv_len = ptr - rdn.bv_val;
1118 ber_dupbv( &ei->bei_rdn, &rdn );
1120 if ( pei->bei_dkids ) pei->bei_dkids--;
1124 ein = ei->bei_parent;
1126 ei->bei_parent = ein;
1127 bdb_cache_entryinfo_unlock( pei );
1128 bdb_cache_entryinfo_lock( ein );
1130 /* parent now has kids */
1131 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1132 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1135 /* parent might now have grandkids */
1136 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1137 !(ei->bei_state & (CACHE_ENTRY_NO_KIDS)))
1138 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1141 /* Record the generation number of this change */
1142 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1144 ei->bei_modrdns = bdb->bi_modrdns;
1145 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1148 if ( ein->bei_dkids ) ein->bei_dkids++;
1150 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1151 bdb_cache_entryinfo_unlock( ein );
1155 * cache_delete - delete the entry e from the cache.
1157 * returns: 0 e was deleted ok
1158 * 1 e was not in the cache
1159 * -1 something bad happened
1163 struct bdb_info *bdb,
1168 EntryInfo *ei = BEI(e);
1171 assert( e->e_private != NULL );
1173 /* Set this early, warn off any queriers */
1174 ei->bei_state |= CACHE_ENTRY_DELETED;
1176 /* Lock the entry's info */
1177 bdb_cache_entryinfo_lock( ei );
1179 /* Get write lock on the data */
1180 rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1182 /* couldn't lock, undo and give up */
1183 ei->bei_state ^= CACHE_ENTRY_DELETED;
1184 bdb_cache_entryinfo_unlock( ei );
1188 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1192 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1194 rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1196 /* free lru mutex */
1197 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1199 /* Leave entry info locked */
1205 bdb_cache_delete_cleanup(
1210 ei->bei_e->e_private = NULL;
1211 #ifdef SLAP_ZONE_ALLOC
1212 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1214 bdb_entry_return( ei->bei_e );
1219 bdb_cache_entryinfo_free( cache, ei );
1220 bdb_cache_entryinfo_unlock( ei );
1224 bdb_cache_delete_internal(
1229 int rc = 0; /* return code */
1232 /* Lock the parent's kids tree */
1233 bdb_cache_entryinfo_lock( e->bei_parent );
1236 e->bei_parent->bei_ckids--;
1237 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1240 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1245 if ( e->bei_parent->bei_kids )
1248 bdb_cache_entryinfo_unlock( e->bei_parent );
1250 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1252 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1259 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1263 LRU_DEL( cache, e );
1266 ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1268 ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1276 bdb_entryinfo_release( void *data )
1278 EntryInfo *ei = (EntryInfo *)data;
1279 if ( ei->bei_kids ) {
1280 avl_free( ei->bei_kids, NULL );
1283 ei->bei_e->e_private = NULL;
1284 #ifdef SLAP_ZONE_ALLOC
1285 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1287 bdb_entry_return( ei->bei_e );
1290 bdb_cache_entryinfo_destroy( ei );
1294 bdb_cache_release_all( Cache *cache )
1296 /* set cache write lock */
1297 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1299 ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1301 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1303 avl_free( cache->c_dntree.bei_kids, NULL );
1304 avl_free( cache->c_idtree, bdb_entryinfo_release );
1305 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1306 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1307 bdb_cache_entryinfo_destroy(cache->c_eifree);
1309 cache->c_cursize = 0;
1310 cache->c_eiused = 0;
1311 cache->c_leaves = 0;
1312 cache->c_idtree = NULL;
1313 cache->c_lruhead = NULL;
1314 cache->c_lrutail = NULL;
1315 cache->c_dntree.bei_kids = NULL;
1317 /* free lru mutex */
1318 ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1319 /* free cache write lock */
1320 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1326 bdb_lru_print( Cache *cache )
1330 fprintf( stderr, "LRU circle head: %p\n", cache->c_lruhead );
1331 fprintf( stderr, "LRU circle (tail forward):\n" );
1332 for ( e = cache->c_lrutail; ; ) {
1333 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1334 e, e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1336 if ( e == cache->c_lrutail )
1339 fprintf( stderr, "LRU circle (tail backward):\n" );
1340 for ( e = cache->c_lrutail; ; ) {
1341 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1342 e, e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1344 if ( e == cache->c_lrutail )
1351 #ifdef BDB_REUSE_LOCKERS
1353 bdb_locker_id_free( void *key, void *data )
1356 u_int32_t lockid = (long)data;
1359 rc = XLOCK_ID_FREE( env, lockid );
1360 if ( rc == EINVAL ) {
1362 Debug( LDAP_DEBUG_ANY,
1363 "bdb_locker_id_free: %lu err %s(%d)\n",
1364 (unsigned long) lockid, db_strerror(rc), rc );
1365 /* release all locks held by this locker. */
1366 lr.op = DB_LOCK_PUT_ALL;
1368 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1369 XLOCK_ID_FREE( env, lockid );
1374 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1381 if ( !env || !locker ) return -1;
1383 /* If no op was provided, try to find the ctx anyway... */
1385 ctx = op->o_threadctx;
1387 ctx = ldap_pvt_thread_pool_context();
1390 /* Shouldn't happen unless we're single-threaded */
1396 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1397 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1398 rc = XLOCK_ID( env, &lockid );
1399 if (rc) ldap_pvt_thread_yield();
1404 data = (void *)((long)lockid);
1405 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1406 data, bdb_locker_id_free ) ) ) {
1407 XLOCK_ID_FREE( env, lockid );
1408 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1409 db_strerror(rc), rc, 0 );
1414 lockid = (long)data;
1419 #endif /* BDB_REUSE_LOCKERS */