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-2006 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_add hdb_cache_lru_add
34 static void bdb_cache_lru_add( struct bdb_info *bdb, EntryInfo *ei );
36 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
39 static void bdb_lru_print(Cache *cache);
44 bdb_cache_entryinfo_new( Cache *cache )
48 if ( cache->c_eifree ) {
49 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
50 if ( cache->c_eifree ) {
52 cache->c_eifree = ei->bei_lrunext;
54 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
57 ei->bei_lrunext = NULL;
60 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
61 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
67 /* Atomically release and reacquire a lock */
69 bdb_cache_entry_db_relock(
84 if ( !lock ) return 0;
86 lockobj.data = &ei->bei_id;
87 lockobj.size = sizeof(ei->bei_id) + 1;
89 list[0].op = DB_LOCK_PUT;
91 list[1].op = DB_LOCK_GET;
93 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
94 list[1].obj = &lockobj;
95 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
99 Debug( LDAP_DEBUG_TRACE,
100 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
101 ei->bei_id, rw, rc );
103 *lock = list[1].lock;
110 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
111 int rw, int tryOnly, DB_LOCK *lock )
120 if ( !lock ) return 0;
123 db_rw = DB_LOCK_WRITE;
125 db_rw = DB_LOCK_READ;
127 lockobj.data = &ei->bei_id;
128 lockobj.size = sizeof(ei->bei_id) + 1;
130 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
131 &lockobj, db_rw, lock);
132 if (rc && !tryOnly) {
133 Debug( LDAP_DEBUG_TRACE,
134 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
135 ei->bei_id, rw, rc );
138 #endif /* NO_THREADS */
142 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
149 if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
151 rc = LOCK_PUT ( env, lock );
157 bdb_cache_entryinfo_destroy( EntryInfo *e )
159 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
160 free( e->bei_nrdn.bv_val );
162 free( e->bei_rdn.bv_val );
168 #define LRU_DELETE( cache, ei ) do { \
169 if ( (ei)->bei_lruprev != NULL ) { \
170 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
172 (cache)->c_lruhead = (ei)->bei_lrunext; \
174 if ( (ei)->bei_lrunext != NULL ) { \
175 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
177 (cache)->c_lrutail = (ei)->bei_lruprev; \
179 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
182 #define LRU_ADD( cache, ei ) do { \
183 (ei)->bei_lrunext = (cache)->c_lruhead; \
184 if ( (ei)->bei_lrunext != NULL ) { \
185 (ei)->bei_lrunext->bei_lruprev = (ei); \
187 (cache)->c_lruhead = (ei); \
188 (ei)->bei_lruprev = NULL; \
189 if ( !ldap_pvt_thread_mutex_trylock( &(cache)->lru_tail_mutex )) { \
190 if ( (cache)->c_lrutail == NULL ) \
191 (cache)->c_lrutail = (ei); \
192 ldap_pvt_thread_mutex_unlock( &(cache)->lru_tail_mutex ); \
196 /* Do a length-ordered sort on normalized RDNs */
198 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
200 const EntryInfo *e1 = v_e1, *e2 = v_e2;
201 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
203 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
204 e1->bei_nrdn.bv_len );
210 bdb_id_cmp( const void *v_e1, const void *v_e2 )
212 const EntryInfo *e1 = v_e1, *e2 = v_e2;
213 return e1->bei_id - e2->bei_id;
216 /* Create an entryinfo in the cache. Caller must release the locks later.
219 bdb_entryinfo_add_internal(
220 struct bdb_info *bdb,
224 EntryInfo *ei2 = NULL;
228 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
230 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
231 bdb_cache_entryinfo_lock( ei->bei_parent );
233 ei2->bei_id = ei->bei_id;
234 ei2->bei_parent = ei->bei_parent;
236 ei2->bei_rdn = ei->bei_rdn;
238 #ifdef SLAP_ZONE_ALLOC
242 /* Add to cache ID tree */
243 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
245 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
246 bdb_cache_entryinfo_destroy( ei2 );
249 /* It got freed above because its value was
252 ei->bei_rdn.bv_val = NULL;
255 bdb->bi_cache.c_eiused++;
256 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
258 /* This is a new leaf node. But if parent had no kids, then it was
259 * a leaf and we would be decrementing that. So, only increment if
260 * the parent already has kids.
262 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
263 bdb->bi_cache.c_leaves++;
264 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
267 ei->bei_parent->bei_ckids++;
275 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
276 * the info for its closest ancestor. *res should be NULL to process a
277 * complete DN starting from the tree root. Otherwise *res must be the
278 * immediate parent of the requested DN, and only the RDN will be searched.
279 * The EntryInfo is locked upon return and must be unlocked by the caller.
288 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
289 EntryInfo ei, *eip, *ei2;
293 /* this function is always called with normalized DN */
295 /* we're doing a onelevel search for an RDN */
296 ei.bei_nrdn.bv_val = ndn->bv_val;
297 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
300 /* we're searching a full DN from the root */
301 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
302 ei.bei_nrdn.bv_val = ptr;
303 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
304 /* Skip to next rdn if suffix is empty */
305 if ( ei.bei_nrdn.bv_len == 0 ) {
306 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
307 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
308 if ( ptr >= ndn->bv_val ) {
309 if (DN_SEPARATOR(*ptr)) ptr++;
310 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
311 ei.bei_nrdn.bv_val = ptr;
314 eip = &bdb->bi_cache.c_dntree;
317 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
319 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
321 int len = ei.bei_nrdn.bv_len;
323 if ( BER_BVISEMPTY( ndn )) {
328 ei.bei_nrdn.bv_len = ndn->bv_len -
329 (ei.bei_nrdn.bv_val - ndn->bv_val);
330 bdb_cache_entryinfo_unlock( eip );
332 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
334 bdb_cache_entryinfo_lock( eip );
339 /* DN exists but needs to be added to cache */
340 ei.bei_nrdn.bv_len = len;
341 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
342 /* add_internal left eip and c_rwlock locked */
343 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
348 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
349 /* In the midst of deleting? Give it a chance to
352 bdb_cache_entryinfo_unlock( eip );
353 ldap_pvt_thread_yield();
354 bdb_cache_entryinfo_lock( eip );
358 bdb_cache_entryinfo_unlock( eip );
359 bdb_cache_entryinfo_lock( ei2 );
363 /* Advance to next lower RDN */
364 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
365 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
366 if ( ptr >= ndn->bv_val ) {
367 if (DN_SEPARATOR(*ptr)) ptr++;
368 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
369 ei.bei_nrdn.bv_val = ptr;
371 if ( ptr < ndn->bv_val ) {
381 /* Walk up the tree from a child node, looking for an ID that's already
382 * been linked into the cache.
385 hdb_cache_find_parent(
392 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
393 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
402 rc = hdb_dn2id_parent( op, txn, locker, &ei, &eip.bei_id );
405 /* Save the previous node, if any */
408 /* Create a new node for the current ID */
409 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
410 ein->bei_id = ei.bei_id;
411 ein->bei_kids = ei.bei_kids;
412 ein->bei_nrdn = ei.bei_nrdn;
413 ein->bei_rdn = ei.bei_rdn;
414 ein->bei_ckids = ei.bei_ckids;
415 #ifdef SLAP_ZONE_ALLOC
420 /* This node is not fully connected yet */
421 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
423 /* Insert this node into the ID tree */
424 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
425 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
426 bdb_id_cmp, avl_dup_error ) ) {
428 /* Someone else created this node just before us.
429 * Free our new copy and use the existing one.
431 bdb_cache_entryinfo_destroy( ein );
432 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
433 (caddr_t) &ei, bdb_id_cmp );
435 /* Link in any kids we've already processed */
437 bdb_cache_entryinfo_lock( ein );
438 avl_insert( &ein->bei_kids, (caddr_t)ei2,
439 bdb_rdn_cmp, avl_dup_error );
441 bdb_cache_entryinfo_unlock( ein );
447 /* If this is the first time, save this node
448 * to be returned later.
450 if ( eir == NULL ) eir = ein;
452 /* If there was a previous node, link it to this one */
453 if ( ei2 ) ei2->bei_parent = ein;
455 /* Look for this node's parent */
457 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
458 (caddr_t) &eip, bdb_id_cmp );
460 ei2 = &bdb->bi_cache.c_dntree;
462 bdb->bi_cache.c_eiused++;
463 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
464 bdb->bi_cache.c_leaves++;
465 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
468 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
469 bdb_cache_lru_add( bdb, ein );
473 /* Got the parent, link in and we're done. */
475 bdb_cache_entryinfo_lock( ei2 );
476 ein->bei_parent = ei2;
477 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
480 bdb_cache_entryinfo_unlock( ei2 );
481 bdb_cache_entryinfo_lock( eir );
483 /* Reset all the state info */
484 for (ein = eir; ein != ei2; ein=ein->bei_parent)
485 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
490 ei.bei_id = eip.bei_id;
492 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
498 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
502 struct bdb_info *bdb,
509 /* See if we already have this one */
510 bdb_cache_entryinfo_lock( ei->bei_parent );
511 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
512 bdb_cache_entryinfo_unlock( ei->bei_parent );
515 /* Not found, add it */
518 /* bei_rdn was not malloc'd before, do it now */
519 ber_dupbv( &bv, &ei->bei_rdn );
522 rc = bdb_entryinfo_add_internal( bdb, ei, res );
523 bdb_cache_entryinfo_unlock( ei->bei_parent );
524 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
526 /* Found, return it */
534 /* caller must have lru_head_mutex locked. mutex
535 * will be unlocked on return.
539 struct bdb_info *bdb,
542 DB_LOCK lock, *lockp;
543 EntryInfo *elru, *elprev;
546 LRU_ADD( &bdb->bi_cache, ei );
547 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_head_mutex );
549 /* See if we're above the cache size limit */
550 if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize )
553 if ( bdb->bi_cache.c_locker ) {
559 /* Don't bother if we can't get the lock */
560 if ( ldap_pvt_thread_mutex_trylock( &bdb->bi_cache.lru_tail_mutex ) )
563 /* Look for an unused entry to remove */
564 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev ) {
565 elprev = elru->bei_lruprev;
567 /* If we can successfully writelock it, then
568 * the object is idle.
570 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv,
571 bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
574 /* If this node is in the process of linking into the cache,
575 * or this node is being deleted, skip it.
577 if ( elru->bei_state &
578 ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED )) {
579 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
582 /* Free entry for this node if it's present */
584 elru->bei_e->e_private = NULL;
585 #ifdef SLAP_ZONE_ALLOC
586 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
588 bdb_entry_return( elru->bei_e );
593 /* ITS#4010 if we're in slapcat, and this node is a leaf
596 * FIXME: we need to do this for slapd as well, (which is
597 * why we compute bi_cache.c_leaves now) but at the moment
598 * we can't because it causes unresolvable deadlocks.
600 if ( slapMode & SLAP_TOOL_READONLY ) {
601 if ( !elru->bei_kids ) {
602 /* This does LRU_DELETE for us */
603 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
604 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
606 /* Leave node on LRU list for a future pass */
608 LRU_DELETE( &bdb->bi_cache, elru );
610 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
612 if ( count >= bdb->bi_cache.c_minfree ) {
613 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
614 bdb->bi_cache.c_cursize -= count;
615 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
621 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_tail_mutex );
626 struct bdb_info *bdb,
629 EntryInfo ei = { 0 },
634 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
635 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
636 (caddr_t) &ei, bdb_id_cmp );
637 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
642 * cache_find_id - find an entry in the cache, given id.
643 * The entry is locked for Read upon return. Call with islocked TRUE if
644 * the supplied *eip was already locked.
657 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
659 int rc = 0, load = 0;
660 EntryInfo ei = { 0 };
664 #ifdef SLAP_ZONE_ALLOC
665 slap_zh_rlock(bdb->bi_cache.c_zctx);
667 /* If we weren't given any info, see if we have it already cached */
669 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
670 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
671 (caddr_t) &ei, bdb_id_cmp );
673 /* If the lock attempt fails, the info is in use */
674 if ( ldap_pvt_thread_mutex_trylock(
675 &(*eip)->bei_kids_mutex )) {
676 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
677 /* If this node is being deleted, treat
678 * as if the delete has already finished
680 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
683 /* otherwise, wait for the info to free up */
684 ldap_pvt_thread_yield();
687 /* If this info isn't hooked up to its parent yet,
688 * unlock and wait for it to be fully initialized
690 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
691 bdb_cache_entryinfo_unlock( *eip );
692 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
693 ldap_pvt_thread_yield();
698 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
701 /* See if the ID exists in the database; add it to the cache if so */
704 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
706 rc = bdb_cache_find_ndn( op, tid,
708 if ( *eip ) islocked = 1;
710 #ifdef SLAP_ZONE_ALLOC
711 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
713 bdb_entry_return( ep );
719 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
720 if ( rc == 0 ) islocked = 1;
724 /* Ok, we found the info, do we have the entry? */
726 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
729 /* Make sure only one thread tries to load the entry */
731 #ifdef SLAP_ZONE_ALLOC
732 if ((*eip)->bei_e && !slap_zn_validate(
733 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
734 (*eip)->bei_e = NULL;
735 (*eip)->bei_zseq = 0;
738 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
740 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
743 bdb_cache_entryinfo_unlock( *eip );
746 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
747 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
749 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
750 } else if ( rc == 0 ) {
752 /* Give up original read lock, obtain write lock
755 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
758 if ( rc == 0 && !ep) {
759 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
762 ep->e_private = *eip;
767 #ifdef SLAP_ZONE_ALLOC
768 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
772 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
774 /* If we succeeded, downgrade back to a readlock. */
775 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
778 /* Otherwise, release the lock. */
779 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
781 } else if ( !(*eip)->bei_e ) {
782 /* Some other thread is trying to load the entry,
783 * give it a chance to finish.
785 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
786 ldap_pvt_thread_yield();
787 bdb_cache_entryinfo_lock( *eip );
792 /* Check for subtree renames
794 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
796 bdb_cache_entry_db_relock( bdb->bi_dbenv,
797 locker, *eip, 1, 0, lock );
798 /* check again in case other modifier did it already */
799 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
800 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
801 bdb_cache_entry_db_relock( bdb->bi_dbenv,
802 locker, *eip, 0, 0, lock );
811 bdb_cache_entryinfo_unlock( *eip );
814 #ifdef SLAP_ZONE_ALLOC
815 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
817 bdb_entry_return( ep );
823 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
824 bdb->bi_cache.c_cursize++;
825 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
828 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
830 /* If the LRU list has only one entry and this is it, it
831 * doesn't need to be added again.
833 if ( bdb->bi_cache.c_lruhead == bdb->bi_cache.c_lrutail &&
834 bdb->bi_cache.c_lruhead == *eip ) {
835 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_head_mutex );
837 /* if entry is on LRU list, remove from old spot */
838 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
839 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_tail_mutex );
840 LRU_DELETE( &bdb->bi_cache, *eip );
841 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_tail_mutex );
843 /* lru_head_mutex is unlocked for us */
844 bdb_cache_lru_add( bdb, *eip );
848 #ifdef SLAP_ZONE_ALLOC
849 if (rc == 0 && (*eip)->bei_e) {
850 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
852 slap_zh_runlock(bdb->bi_cache.c_zctx);
865 if ( BEI(e)->bei_kids ) {
868 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
871 rc = bdb_dn2id_children( op, txn, e );
872 if ( rc == DB_NOTFOUND ) {
873 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
878 /* Update the cache after a successful database Add. */
881 struct bdb_info *bdb,
891 struct berval rdn = e->e_name;
899 /* Lock this entry so that bdb_add can run to completion.
900 * It can only fail if BDB has run out of lock resources.
902 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
904 bdb_cache_entryinfo_unlock( eip );
909 if ( nrdn->bv_len != e->e_nname.bv_len ) {
910 char *ptr = ber_bvchr( &rdn, ',' );
911 assert( ptr != NULL );
912 rdn.bv_len = ptr - rdn.bv_val;
914 ber_dupbv( &ei.bei_rdn, &rdn );
915 if ( eip->bei_dkids ) eip->bei_dkids++;
918 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
919 /* bdb_csn_commit can cause this when adding the database root entry */
921 new->bei_e->e_private = NULL;
922 #ifdef SLAP_ZONE_ALLOC
923 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
925 bdb_entry_return( new->bei_e );
930 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
931 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
932 if (eip->bei_parent) {
933 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
935 bdb_cache_entryinfo_unlock( eip );
937 ++bdb->bi_cache.c_cursize;
938 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
941 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
943 /* lru_head_mutex is unlocked for us */
944 bdb_cache_lru_add( bdb, new );
957 EntryInfo *ei = BEI(e);
959 /* Get write lock on data */
960 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
962 /* If we've done repeated mods on a cached entry, then e_attrs
963 * is no longer contiguous with the entry, and must be freed.
966 if ( (void *)e->e_attrs != (void *)(e+1) ) {
967 attrs_free( e->e_attrs );
969 e->e_attrs = newAttrs;
975 * Change the rdn in the entryinfo. Also move to a new parent if needed.
979 struct bdb_info *bdb,
987 EntryInfo *ei = BEI(e), *pei;
993 /* Get write lock on data */
994 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
997 /* If we've done repeated mods on a cached entry, then e_attrs
998 * is no longer contiguous with the entry, and must be freed.
1000 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1001 attrs_free( e->e_attrs );
1003 e->e_attrs = new->e_attrs;
1004 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1005 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1007 ch_free(e->e_name.bv_val);
1008 ch_free(e->e_nname.bv_val);
1010 e->e_name = new->e_name;
1011 e->e_nname = new->e_nname;
1013 /* Lock the parent's kids AVL tree */
1014 pei = ei->bei_parent;
1015 bdb_cache_entryinfo_lock( pei );
1016 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1017 free( ei->bei_nrdn.bv_val );
1018 ber_dupbv( &ei->bei_nrdn, nrdn );
1020 if ( !pei->bei_kids )
1021 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1024 free( ei->bei_rdn.bv_val );
1027 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1028 char *ptr = ber_bvchr(&rdn, ',');
1029 assert( ptr != NULL );
1030 rdn.bv_len = ptr - rdn.bv_val;
1032 ber_dupbv( &ei->bei_rdn, &rdn );
1034 if ( pei->bei_dkids ) pei->bei_dkids--;
1038 ein = ei->bei_parent;
1040 ei->bei_parent = ein;
1041 bdb_cache_entryinfo_unlock( pei );
1042 bdb_cache_entryinfo_lock( ein );
1044 /* parent now has kids */
1045 if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1046 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1049 /* parent might now have grandkids */
1050 if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1051 !(ei->bei_state & (CACHE_ENTRY_NO_KIDS)))
1052 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1055 /* Record the generation number of this change */
1056 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1058 ei->bei_modrdns = bdb->bi_modrdns;
1059 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1062 if ( ein->bei_dkids ) ein->bei_dkids++;
1064 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1065 bdb_cache_entryinfo_unlock( ein );
1069 * cache_delete - delete the entry e from the cache.
1071 * returns: 0 e was deleted ok
1072 * 1 e was not in the cache
1073 * -1 something bad happened
1083 EntryInfo *ei = BEI(e);
1086 assert( e->e_private != NULL );
1088 /* Set this early, warn off any queriers */
1089 ei->bei_state |= CACHE_ENTRY_DELETED;
1091 /* Lock the entry's info */
1092 bdb_cache_entryinfo_lock( ei );
1094 /* Get write lock on the data */
1095 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1097 /* couldn't lock, undo and give up */
1098 ei->bei_state ^= CACHE_ENTRY_DELETED;
1099 bdb_cache_entryinfo_unlock( ei );
1103 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1107 ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex );
1109 /* set cache write lock */
1110 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1112 rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1114 /* free cache write lock */
1115 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1117 /* free lru mutex */
1118 ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex );
1120 /* Leave entry info locked */
1126 bdb_cache_delete_cleanup(
1131 ei->bei_e->e_private = NULL;
1132 #ifdef SLAP_ZONE_ALLOC
1133 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1135 bdb_entry_return( ei->bei_e );
1140 free( ei->bei_nrdn.bv_val );
1141 ei->bei_nrdn.bv_val = NULL;
1143 free( ei->bei_rdn.bv_val );
1144 ei->bei_rdn.bv_val = NULL;
1145 ei->bei_modrdns = 0;
1149 ei->bei_parent = NULL;
1150 ei->bei_kids = NULL;
1151 ei->bei_lruprev = NULL;
1153 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1154 ei->bei_lrunext = cache->c_eifree;
1155 cache->c_eifree = ei;
1156 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1157 bdb_cache_entryinfo_unlock( ei );
1161 bdb_cache_delete_internal(
1166 int rc = 0; /* return code */
1168 /* Lock the parent's kids tree */
1169 bdb_cache_entryinfo_lock( e->bei_parent );
1172 e->bei_parent->bei_ckids--;
1173 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1176 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1181 if ( e->bei_parent->bei_kids )
1185 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1193 LRU_DELETE( cache, e );
1194 if ( e->bei_e ) cache->c_cursize--;
1197 bdb_cache_entryinfo_unlock( e->bei_parent );
1203 bdb_entryinfo_release( void *data )
1205 EntryInfo *ei = (EntryInfo *)data;
1206 if ( ei->bei_kids ) {
1207 avl_free( ei->bei_kids, NULL );
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 );
1217 bdb_cache_entryinfo_destroy( ei );
1221 bdb_cache_release_all( Cache *cache )
1223 /* set cache write lock */
1224 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1226 ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex );
1228 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1230 avl_free( cache->c_dntree.bei_kids, NULL );
1231 avl_free( cache->c_idtree, bdb_entryinfo_release );
1232 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1233 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1234 bdb_cache_entryinfo_destroy(cache->c_eifree);
1236 cache->c_cursize = 0;
1237 cache->c_eiused = 0;
1238 cache->c_leaves = 0;
1239 cache->c_idtree = NULL;
1240 cache->c_lruhead = NULL;
1241 cache->c_lrutail = NULL;
1242 cache->c_dntree.bei_kids = NULL;
1244 /* free lru mutex */
1245 ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex );
1246 /* free cache write lock */
1247 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1253 bdb_lru_print( Cache *cache )
1257 fprintf( stderr, "LRU queue (head to tail):\n" );
1258 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1259 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1260 e->bei_nrdn.bv_val, e->bei_id );
1262 fprintf( stderr, "LRU queue (tail to head):\n" );
1263 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1264 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1265 e->bei_nrdn.bv_val, e->bei_id );
1271 #ifdef BDB_REUSE_LOCKERS
1273 bdb_locker_id_free( void *key, void *data )
1276 u_int32_t lockid = (long)data;
1279 rc = XLOCK_ID_FREE( env, lockid );
1280 if ( rc == EINVAL ) {
1282 Debug( LDAP_DEBUG_ANY,
1283 "bdb_locker_id_free: %lu err %s(%d)\n",
1284 (unsigned long) lockid, db_strerror(rc), rc );
1285 /* release all locks held by this locker. */
1286 lr.op = DB_LOCK_PUT_ALL;
1288 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1289 XLOCK_ID_FREE( env, lockid );
1294 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1301 if ( !env || !locker ) return -1;
1303 /* If no op was provided, try to find the ctx anyway... */
1305 ctx = op->o_threadctx;
1307 ctx = ldap_pvt_thread_pool_context();
1310 /* Shouldn't happen unless we're single-threaded */
1316 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1317 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1318 rc = XLOCK_ID( env, &lockid );
1319 if (rc) ldap_pvt_thread_yield();
1324 data = (void *)((long)lockid);
1325 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1326 data, bdb_locker_id_free ) ) ) {
1327 XLOCK_ID_FREE( env, lockid );
1328 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1329 db_strerror(rc), rc, 0 );
1334 lockid = (long)data;
1339 #endif /* BDB_REUSE_LOCKERS */
1342 bdb_cache_delete_entry(
1343 struct bdb_info *bdb,
1348 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1349 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1351 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1352 LRU_DELETE( &bdb->bi_cache, ei );
1353 ei->bei_e->e_private = NULL;
1354 #ifdef SLAP_ZONE_ALLOC
1355 bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1357 bdb_entry_return( ei->bei_e );
1360 --bdb->bi_cache.c_cursize;
1362 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1364 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );