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-2005 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>
30 #define bdb_cache_lru_add hdb_cache_lru_add
33 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
35 static void bdb_lru_print(Cache *cache);
39 bdb_cache_entryinfo_new( Cache *cache )
43 if ( cache->c_eifree ) {
44 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
45 if ( cache->c_eifree ) {
47 cache->c_eifree = ei->bei_lrunext;
49 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
52 ei->bei_lrunext = NULL;
55 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
56 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
62 /* Atomically release and reacquire a lock */
64 bdb_cache_entry_db_relock(
79 if ( !lock ) return 0;
81 lockobj.data = &ei->bei_id;
82 lockobj.size = sizeof(ei->bei_id) + 1;
84 list[0].op = DB_LOCK_PUT;
86 list[1].op = DB_LOCK_GET;
88 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
89 list[1].obj = &lockobj;
90 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
94 Debug( LDAP_DEBUG_TRACE,
95 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
105 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
106 int rw, int tryOnly, DB_LOCK *lock )
115 if ( !lock ) return 0;
118 db_rw = DB_LOCK_WRITE;
120 db_rw = DB_LOCK_READ;
122 lockobj.data = &ei->bei_id;
123 lockobj.size = sizeof(ei->bei_id) + 1;
125 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
126 &lockobj, db_rw, lock);
127 if (rc && !tryOnly) {
128 Debug( LDAP_DEBUG_TRACE,
129 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
130 ei->bei_id, rw, rc );
133 #endif /* NO_THREADS */
137 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
144 if ( !lock ) return 0;
146 rc = LOCK_PUT ( env, lock );
152 bdb_cache_entryinfo_destroy( EntryInfo *e )
154 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
155 free( e->bei_nrdn.bv_val );
157 free( e->bei_rdn.bv_val );
163 #define LRU_DELETE( cache, ei ) do { \
164 if ( (ei)->bei_lruprev != NULL ) { \
165 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
167 (cache)->c_lruhead = (ei)->bei_lrunext; \
169 if ( (ei)->bei_lrunext != NULL ) { \
170 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
172 (cache)->c_lrutail = (ei)->bei_lruprev; \
174 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
177 #define LRU_ADD( cache, ei ) do { \
178 (ei)->bei_lrunext = (cache)->c_lruhead; \
179 if ( (ei)->bei_lrunext != NULL ) { \
180 (ei)->bei_lrunext->bei_lruprev = (ei); \
182 (cache)->c_lruhead = (ei); \
183 (ei)->bei_lruprev = NULL; \
184 if ( (cache)->c_lrutail == NULL ) { \
185 (cache)->c_lrutail = (ei); \
189 /* Do a length-ordered sort on normalized RDNs */
191 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
193 const EntryInfo *e1 = v_e1, *e2 = v_e2;
194 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
196 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
197 e1->bei_nrdn.bv_len );
203 bdb_id_cmp( const void *v_e1, const void *v_e2 )
205 const EntryInfo *e1 = v_e1, *e2 = v_e2;
206 return e1->bei_id - e2->bei_id;
209 /* Create an entryinfo in the cache. Caller must release the locks later.
212 bdb_entryinfo_add_internal(
213 struct bdb_info *bdb,
217 EntryInfo *ei2 = NULL;
221 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
223 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
224 bdb_cache_entryinfo_lock( ei->bei_parent );
226 ei2->bei_id = ei->bei_id;
227 ei2->bei_parent = ei->bei_parent;
229 ei2->bei_rdn = ei->bei_rdn;
231 #ifdef SLAP_ZONE_ALLOC
235 /* Add to cache ID tree */
236 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
238 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
239 bdb_cache_entryinfo_destroy( ei2 );
242 /* It got freed above because its value was
245 ei->bei_rdn.bv_val = NULL;
248 bdb->bi_cache.c_eiused++;
249 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
251 /* This is a new leaf node. But if parent had no kids, then it was
252 * a leaf and we would be decrementing that. So, only increment if
253 * the parent already has kids.
255 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
256 bdb->bi_cache.c_leaves++;
257 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
260 ei->bei_parent->bei_ckids++;
268 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
269 * the info for its closest ancestor. *res should be NULL to process a
270 * complete DN starting from the tree root. Otherwise *res must be the
271 * immediate parent of the requested DN, and only the RDN will be searched.
272 * The EntryInfo is locked upon return and must be unlocked by the caller.
281 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
282 EntryInfo ei, *eip, *ei2;
286 /* this function is always called with normalized DN */
288 /* we're doing a onelevel search for an RDN */
289 ei.bei_nrdn.bv_val = ndn->bv_val;
290 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
293 /* we're searching a full DN from the root */
294 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
295 ei.bei_nrdn.bv_val = ptr;
296 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
297 /* Skip to next rdn if suffix is empty */
298 if ( ei.bei_nrdn.bv_len == 0 ) {
299 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
300 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
301 if ( ptr >= ndn->bv_val ) {
302 if (DN_SEPARATOR(*ptr)) ptr++;
303 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
304 ei.bei_nrdn.bv_val = ptr;
307 eip = &bdb->bi_cache.c_dntree;
310 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
312 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
314 int len = ei.bei_nrdn.bv_len;
316 if ( BER_BVISEMPTY( ndn )) {
321 ei.bei_nrdn.bv_len = ndn->bv_len -
322 (ei.bei_nrdn.bv_val - ndn->bv_val);
323 bdb_cache_entryinfo_unlock( eip );
325 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
327 bdb_cache_entryinfo_lock( eip );
332 /* DN exists but needs to be added to cache */
333 ei.bei_nrdn.bv_len = len;
334 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
335 /* add_internal left eip and c_rwlock locked */
336 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
341 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
342 /* In the midst of deleting? Give it a chance to
345 bdb_cache_entryinfo_unlock( eip );
346 ldap_pvt_thread_yield();
347 bdb_cache_entryinfo_lock( eip );
351 bdb_cache_entryinfo_unlock( eip );
352 bdb_cache_entryinfo_lock( ei2 );
356 /* Advance to next lower RDN */
357 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
358 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
359 if ( ptr >= ndn->bv_val ) {
360 if (DN_SEPARATOR(*ptr)) ptr++;
361 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
362 ei.bei_nrdn.bv_val = ptr;
364 if ( ptr < ndn->bv_val ) {
374 /* Walk up the tree from a child node, looking for an ID that's already
375 * been linked into the cache.
378 hdb_cache_find_parent(
385 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
386 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
395 rc = hdb_dn2id_parent( op, txn, locker, &ei, &eip.bei_id );
398 /* Save the previous node, if any */
401 /* Create a new node for the current ID */
402 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
403 ein->bei_id = ei.bei_id;
404 ein->bei_kids = ei.bei_kids;
405 ein->bei_nrdn = ei.bei_nrdn;
406 ein->bei_rdn = ei.bei_rdn;
407 ein->bei_ckids = ei.bei_ckids;
408 #ifdef SLAP_ZONE_ALLOC
413 /* This node is not fully connected yet */
414 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
416 /* Insert this node into the ID tree */
417 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
418 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
419 bdb_id_cmp, avl_dup_error ) ) {
421 /* Someone else created this node just before us.
422 * Free our new copy and use the existing one.
424 bdb_cache_entryinfo_destroy( ein );
425 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
426 (caddr_t) &ei, bdb_id_cmp );
428 /* Link in any kids we've already processed */
430 bdb_cache_entryinfo_lock( ein );
431 avl_insert( &ein->bei_kids, (caddr_t)ei2,
432 bdb_rdn_cmp, avl_dup_error );
434 bdb_cache_entryinfo_unlock( ein );
440 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
441 LRU_ADD( &bdb->bi_cache, ein );
442 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
446 /* If this is the first time, save this node
447 * to be returned later.
449 if ( eir == NULL ) eir = ein;
451 /* If there was a previous node, link it to this one */
452 if ( ei2 ) ei2->bei_parent = ein;
454 /* Look for this node's parent */
456 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
457 (caddr_t) &eip, bdb_id_cmp );
459 ei2 = &bdb->bi_cache.c_dntree;
461 bdb->bi_cache.c_eiused++;
462 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
463 bdb->bi_cache.c_leaves++;
464 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
466 /* Got the parent, link in and we're done. */
468 bdb_cache_entryinfo_lock( ei2 );
469 ein->bei_parent = ei2;
470 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
473 bdb_cache_entryinfo_unlock( ei2 );
474 bdb_cache_entryinfo_lock( eir );
476 /* Reset all the state info */
477 for (ein = eir; ein != ei2; ein=ein->bei_parent)
478 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
483 ei.bei_id = eip.bei_id;
485 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
491 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
495 struct bdb_info *bdb,
502 /* See if we already have this one */
503 bdb_cache_entryinfo_lock( ei->bei_parent );
504 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
505 bdb_cache_entryinfo_unlock( ei->bei_parent );
508 /* Not found, add it */
511 /* bei_rdn was not malloc'd before, do it now */
512 ber_dupbv( &bv, &ei->bei_rdn );
515 rc = bdb_entryinfo_add_internal( bdb, ei, res );
516 bdb_cache_entryinfo_unlock( ei->bei_parent );
517 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
519 /* Found, return it */
527 /* caller must have lru_mutex locked. mutex
528 * will be unlocked on return.
532 struct bdb_info *bdb,
536 DB_LOCK lock, *lockp;
544 /* See if we're above the cache size limit */
545 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
546 EntryInfo *elru, *elprev;
549 /* Look for an unused entry to remove */
550 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
551 elprev = elru->bei_lruprev;
553 /* Too many probes, not enough idle, give up */
557 /* If we can successfully writelock it, then
558 * the object is idle.
560 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv,
561 bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
563 int stop = 0, decr = 0;
565 /* If this node is in the process of linking into the cache,
566 * or this node is being deleted, skip it.
568 if ( elru->bei_state &
569 ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED )) {
570 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
573 /* Free entry for this node if it's present */
575 elru->bei_e->e_private = NULL;
576 #ifdef SLAP_ZONE_ALLOC
577 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
579 bdb_entry_return( elru->bei_e );
584 /* ITS#4010 if we're in slapcat, and this node is a leaf
587 * FIXME: we need to do this for slapd as well, (which is
588 * why we compute bi_cache.c_leaves now) but at the moment
589 * we can't because it causes unresolvable deadlocks.
591 if ( slapMode & SLAP_TOOL_READONLY ) {
592 if ( !elru->bei_kids ) {
593 /* This does LRU_DELETE for us */
594 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
595 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
597 /* Leave node on LRU list for a future pass */
599 LRU_DELETE( &bdb->bi_cache, elru );
601 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
603 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
605 --bdb->bi_cache.c_cursize;
606 if (bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize)
608 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
613 LRU_ADD( &bdb->bi_cache, ei );
614 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
619 struct bdb_info *bdb,
622 EntryInfo ei = { 0 },
627 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
628 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
629 (caddr_t) &ei, bdb_id_cmp );
630 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
635 * cache_find_id - find an entry in the cache, given id.
636 * The entry is locked for Read upon return. Call with islocked TRUE if
637 * the supplied *eip was already locked.
650 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
652 int rc = 0, load = 0;
653 EntryInfo ei = { 0 };
657 #ifdef SLAP_ZONE_ALLOC
658 slap_zh_rlock(bdb->bi_cache.c_zctx);
660 /* If we weren't given any info, see if we have it already cached */
662 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
663 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
664 (caddr_t) &ei, bdb_id_cmp );
666 /* If the lock attempt fails, the info is in use */
667 if ( ldap_pvt_thread_mutex_trylock(
668 &(*eip)->bei_kids_mutex )) {
669 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
670 /* If this node is being deleted, treat
671 * as if the delete has already finished
673 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
676 /* otherwise, wait for the info to free up */
677 ldap_pvt_thread_yield();
680 /* If this info isn't hooked up to its parent yet,
681 * unlock and wait for it to be fully initialized
683 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
684 bdb_cache_entryinfo_unlock( *eip );
685 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
686 ldap_pvt_thread_yield();
691 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
694 /* See if the ID exists in the database; add it to the cache if so */
697 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
699 rc = bdb_cache_find_ndn( op, tid,
701 if ( *eip ) islocked = 1;
703 #ifdef SLAP_ZONE_ALLOC
704 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
706 bdb_entry_return( ep );
712 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
713 if ( rc == 0 && *eip ) islocked = 1;
717 /* Ok, we found the info, do we have the entry? */
718 if ( *eip && rc == 0 ) {
719 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
722 /* Make sure only one thread tries to load the entry */
724 #ifdef SLAP_ZONE_ALLOC
725 if ((*eip)->bei_e && !slap_zn_validate(
726 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
727 (*eip)->bei_e = NULL;
728 (*eip)->bei_zseq = 0;
731 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
733 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
736 bdb_cache_entryinfo_unlock( *eip );
739 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
740 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
742 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
743 } else if ( rc == 0 ) {
745 /* Give up original read lock, obtain write lock
748 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
751 if ( rc == 0 && !ep) {
752 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
755 ep->e_private = *eip;
760 #ifdef SLAP_ZONE_ALLOC
761 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
765 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
767 /* If we succeeded, downgrade back to a readlock. */
768 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
771 /* Otherwise, release the lock. */
772 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
774 } else if ( !(*eip)->bei_e ) {
775 /* Some other thread is trying to load the entry,
776 * give it a chance to finish.
778 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
779 ldap_pvt_thread_yield();
780 bdb_cache_entryinfo_lock( *eip );
785 /* Check for subtree renames
787 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
789 bdb_cache_entry_db_relock( bdb->bi_dbenv,
790 locker, *eip, 1, 0, lock );
791 /* check again in case other modifier did it already */
792 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
793 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
794 bdb_cache_entry_db_relock( bdb->bi_dbenv,
795 locker, *eip, 0, 0, lock );
804 bdb_cache_entryinfo_unlock( *eip );
807 #ifdef SLAP_ZONE_ALLOC
808 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
810 bdb_entry_return( ep );
816 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
817 bdb->bi_cache.c_cursize++;
818 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
821 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
823 /* If the LRU list has only one entry and this is it, it
824 * doesn't need to be added again.
826 if ( bdb->bi_cache.c_lruhead == bdb->bi_cache.c_lrutail &&
827 bdb->bi_cache.c_lruhead == *eip ) {
828 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
831 /* if entry is on LRU list, remove from old spot */
832 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
833 LRU_DELETE( &bdb->bi_cache, *eip );
835 /* lru_mutex is unlocked for us */
836 bdb_cache_lru_add( bdb, locker, *eip );
840 #ifdef SLAP_ZONE_ALLOC
841 if (rc == 0 && (*eip)->bei_e) {
842 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
844 slap_zh_runlock(bdb->bi_cache.c_zctx);
857 if ( BEI(e)->bei_kids ) {
860 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
863 rc = bdb_dn2id_children( op, txn, e );
864 if ( rc == DB_NOTFOUND ) {
865 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
870 /* Update the cache after a successful database Add. */
873 struct bdb_info *bdb,
883 struct berval rdn = e->e_name;
891 /* Lock this entry so that bdb_add can run to completion.
892 * It can only fail if BDB has run out of lock resources.
894 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
896 bdb_cache_entryinfo_unlock( eip );
901 if ( nrdn->bv_len != e->e_nname.bv_len ) {
902 char *ptr = ber_bvchr( &rdn, ',' );
903 assert( ptr != NULL );
904 rdn.bv_len = ptr - rdn.bv_val;
906 ber_dupbv( &ei.bei_rdn, &rdn );
907 if ( eip->bei_dkids ) eip->bei_dkids++;
910 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
911 /* bdb_csn_commit can cause this when adding the database root entry */
913 new->bei_e->e_private = NULL;
914 #ifdef SLAP_ZONE_ALLOC
915 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
917 bdb_entry_return( new->bei_e );
922 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
923 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
924 if (eip->bei_parent) {
925 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
927 bdb_cache_entryinfo_unlock( eip );
929 ++bdb->bi_cache.c_cursize;
930 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
933 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
935 /* lru_mutex is unlocked for us */
936 bdb_cache_lru_add( bdb, locker, new );
949 EntryInfo *ei = BEI(e);
951 /* Get write lock on data */
952 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
954 /* If we've done repeated mods on a cached entry, then e_attrs
955 * is no longer contiguous with the entry, and must be freed.
958 if ( (void *)e->e_attrs != (void *)(e+1) ) {
959 attrs_free( e->e_attrs );
961 e->e_attrs = newAttrs;
967 * Change the rdn in the entryinfo. Also move to a new parent if needed.
971 struct bdb_info *bdb,
979 EntryInfo *ei = BEI(e), *pei;
985 /* Get write lock on data */
986 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
989 /* If we've done repeated mods on a cached entry, then e_attrs
990 * is no longer contiguous with the entry, and must be freed.
992 if ( (void *)e->e_attrs != (void *)(e+1) ) {
993 attrs_free( e->e_attrs );
995 e->e_attrs = new->e_attrs;
996 if( e->e_nname.bv_val < e->e_bv.bv_val ||
997 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
999 ch_free(e->e_name.bv_val);
1000 ch_free(e->e_nname.bv_val);
1002 e->e_name = new->e_name;
1003 e->e_nname = new->e_nname;
1005 /* Lock the parent's kids AVL tree */
1006 pei = ei->bei_parent;
1007 bdb_cache_entryinfo_lock( pei );
1008 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1009 free( ei->bei_nrdn.bv_val );
1010 ber_dupbv( &ei->bei_nrdn, nrdn );
1012 free( ei->bei_rdn.bv_val );
1015 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1016 char *ptr = ber_bvchr(&rdn, ',');
1017 assert( ptr != NULL );
1018 rdn.bv_len = ptr - rdn.bv_val;
1020 ber_dupbv( &ei->bei_rdn, &rdn );
1024 ein = ei->bei_parent;
1026 ei->bei_parent = ein;
1027 bdb_cache_entryinfo_unlock( pei );
1028 bdb_cache_entryinfo_lock( ein );
1032 /* Record the generation number of this change */
1033 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1035 ei->bei_modrdns = bdb->bi_modrdns;
1036 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1039 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1040 bdb_cache_entryinfo_unlock( ein );
1044 * cache_delete - delete the entry e from the cache.
1046 * returns: 0 e was deleted ok
1047 * 1 e was not in the cache
1048 * -1 something bad happened
1058 EntryInfo *ei = BEI(e);
1061 assert( e->e_private != NULL );
1063 /* Set this early, warn off any queriers */
1064 ei->bei_state |= CACHE_ENTRY_DELETED;
1066 /* Lock the entry's info */
1067 bdb_cache_entryinfo_lock( ei );
1069 /* Get write lock on the data */
1070 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1072 /* couldn't lock, undo and give up */
1073 ei->bei_state ^= CACHE_ENTRY_DELETED;
1074 bdb_cache_entryinfo_unlock( ei );
1078 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1082 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1084 /* set cache write lock */
1085 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1087 rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1089 /* free cache write lock */
1090 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1092 /* free lru mutex */
1093 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1095 /* Leave entry info locked */
1101 bdb_cache_delete_cleanup(
1106 ei->bei_e->e_private = NULL;
1107 #ifdef SLAP_ZONE_ALLOC
1108 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1110 bdb_entry_return( ei->bei_e );
1115 free( ei->bei_nrdn.bv_val );
1116 ei->bei_nrdn.bv_val = NULL;
1118 free( ei->bei_rdn.bv_val );
1119 ei->bei_rdn.bv_val = NULL;
1120 ei->bei_modrdns = 0;
1124 ei->bei_parent = NULL;
1125 ei->bei_kids = NULL;
1126 ei->bei_lruprev = NULL;
1128 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1129 ei->bei_lrunext = cache->c_eifree;
1130 cache->c_eifree = ei;
1131 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1132 bdb_cache_entryinfo_unlock( ei );
1136 bdb_cache_delete_internal(
1141 int rc = 0; /* return code */
1143 /* Lock the parent's kids tree */
1144 bdb_cache_entryinfo_lock( e->bei_parent );
1147 e->bei_parent->bei_ckids--;
1148 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1151 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1156 if ( e->bei_parent->bei_kids )
1160 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1168 LRU_DELETE( cache, e );
1169 if ( e->bei_e ) cache->c_cursize--;
1172 bdb_cache_entryinfo_unlock( e->bei_parent );
1178 bdb_entryinfo_release( void *data )
1180 EntryInfo *ei = (EntryInfo *)data;
1181 if ( ei->bei_kids ) {
1182 avl_free( ei->bei_kids, NULL );
1185 ei->bei_e->e_private = NULL;
1186 #ifdef SLAP_ZONE_ALLOC
1187 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1189 bdb_entry_return( ei->bei_e );
1192 bdb_cache_entryinfo_destroy( ei );
1196 bdb_cache_release_all( Cache *cache )
1198 /* set cache write lock */
1199 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1201 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1203 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1205 avl_free( cache->c_dntree.bei_kids, NULL );
1206 avl_free( cache->c_idtree, bdb_entryinfo_release );
1207 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1208 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1209 bdb_cache_entryinfo_destroy(cache->c_eifree);
1211 cache->c_cursize = 0;
1212 cache->c_eiused = 0;
1213 cache->c_leaves = 0;
1214 cache->c_idtree = NULL;
1215 cache->c_lruhead = NULL;
1216 cache->c_lrutail = NULL;
1217 cache->c_dntree.bei_kids = NULL;
1219 /* free lru mutex */
1220 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1221 /* free cache write lock */
1222 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1227 bdb_lru_print( Cache *cache )
1231 fprintf( stderr, "LRU queue (head to tail):\n" );
1232 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1233 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1234 e->bei_nrdn.bv_val, e->bei_id );
1236 fprintf( stderr, "LRU queue (tail to head):\n" );
1237 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1238 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1239 e->bei_nrdn.bv_val, e->bei_id );
1244 #ifdef BDB_REUSE_LOCKERS
1246 bdb_locker_id_free( void *key, void *data )
1249 u_int32_t lockid = (long)data;
1252 rc = XLOCK_ID_FREE( env, lockid );
1253 if ( rc == EINVAL ) {
1255 Debug( LDAP_DEBUG_ANY,
1256 "bdb_locker_id_free: %lu err %s(%d)\n",
1257 (unsigned long) lockid, db_strerror(rc), rc );
1258 /* release all locks held by this locker. */
1259 lr.op = DB_LOCK_PUT_ALL;
1261 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1262 XLOCK_ID_FREE( env, lockid );
1267 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1274 if ( !env || !locker ) return -1;
1276 /* If no op was provided, try to find the ctx anyway... */
1278 ctx = op->o_threadctx;
1280 ctx = ldap_pvt_thread_pool_context();
1283 /* Shouldn't happen unless we're single-threaded */
1289 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1290 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1291 rc = XLOCK_ID( env, &lockid );
1292 if (rc) ldap_pvt_thread_yield();
1297 data = (void *)((long)lockid);
1298 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1299 data, bdb_locker_id_free ) ) ) {
1300 XLOCK_ID_FREE( env, lockid );
1301 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1302 db_strerror(rc), rc, 0 );
1307 lockid = (long)data;
1312 #endif /* BDB_REUSE_LOCKERS */
1315 bdb_cache_delete_entry(
1316 struct bdb_info *bdb,
1321 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1322 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1324 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1325 LRU_DELETE( &bdb->bi_cache, ei );
1326 ei->bei_e->e_private = NULL;
1327 #ifdef SLAP_ZONE_ALLOC
1328 bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1330 bdb_entry_return( ei->bei_e );
1333 --bdb->bi_cache.c_cursize;
1335 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1337 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );