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 = strchr( rdn.bv_val, ',' );
903 rdn.bv_len = ptr - rdn.bv_val;
905 ber_dupbv( &ei.bei_rdn, &rdn );
906 if ( eip->bei_dkids ) eip->bei_dkids++;
909 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
910 /* bdb_csn_commit can cause this when adding the database root entry */
912 new->bei_e->e_private = NULL;
913 #ifdef SLAP_ZONE_ALLOC
914 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
916 bdb_entry_return( new->bei_e );
921 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
922 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
923 if (eip->bei_parent) {
924 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
926 bdb_cache_entryinfo_unlock( eip );
928 ++bdb->bi_cache.c_cursize;
929 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
932 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
934 /* lru_mutex is unlocked for us */
935 bdb_cache_lru_add( bdb, locker, new );
948 EntryInfo *ei = BEI(e);
950 /* Get write lock on data */
951 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
953 /* If we've done repeated mods on a cached entry, then e_attrs
954 * is no longer contiguous with the entry, and must be freed.
957 if ( (void *)e->e_attrs != (void *)(e+1) ) {
958 attrs_free( e->e_attrs );
960 e->e_attrs = newAttrs;
966 * Change the rdn in the entryinfo. Also move to a new parent if needed.
970 struct bdb_info *bdb,
978 EntryInfo *ei = BEI(e), *pei;
984 /* Get write lock on data */
985 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
988 /* If we've done repeated mods on a cached entry, then e_attrs
989 * is no longer contiguous with the entry, and must be freed.
991 if ( (void *)e->e_attrs != (void *)(e+1) ) {
992 attrs_free( e->e_attrs );
994 e->e_attrs = new->e_attrs;
995 if( e->e_nname.bv_val < e->e_bv.bv_val ||
996 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
998 ch_free(e->e_name.bv_val);
999 ch_free(e->e_nname.bv_val);
1001 e->e_name = new->e_name;
1002 e->e_nname = new->e_nname;
1004 /* Lock the parent's kids AVL tree */
1005 pei = ei->bei_parent;
1006 bdb_cache_entryinfo_lock( pei );
1007 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1008 free( ei->bei_nrdn.bv_val );
1009 ber_dupbv( &ei->bei_nrdn, nrdn );
1011 free( ei->bei_rdn.bv_val );
1014 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1015 char *ptr = strchr(rdn.bv_val, ',');
1016 rdn.bv_len = ptr - rdn.bv_val;
1018 ber_dupbv( &ei->bei_rdn, &rdn );
1022 ein = ei->bei_parent;
1024 ei->bei_parent = ein;
1025 bdb_cache_entryinfo_unlock( pei );
1026 bdb_cache_entryinfo_lock( ein );
1030 /* Record the generation number of this change */
1031 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1033 ei->bei_modrdns = bdb->bi_modrdns;
1034 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1037 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1038 bdb_cache_entryinfo_unlock( ein );
1042 * cache_delete - delete the entry e from the cache.
1044 * returns: 0 e was deleted ok
1045 * 1 e was not in the cache
1046 * -1 something bad happened
1056 EntryInfo *ei = BEI(e);
1059 assert( e->e_private != NULL );
1061 /* Set this early, warn off any queriers */
1062 ei->bei_state |= CACHE_ENTRY_DELETED;
1064 /* Lock the entry's info */
1065 bdb_cache_entryinfo_lock( ei );
1067 /* Get write lock on the data */
1068 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1070 /* couldn't lock, undo and give up */
1071 ei->bei_state ^= CACHE_ENTRY_DELETED;
1072 bdb_cache_entryinfo_unlock( ei );
1076 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1080 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1082 /* set cache write lock */
1083 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1085 rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1087 /* free cache write lock */
1088 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1090 /* free lru mutex */
1091 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1093 /* Leave entry info locked */
1099 bdb_cache_delete_cleanup(
1104 ei->bei_e->e_private = NULL;
1105 #ifdef SLAP_ZONE_ALLOC
1106 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1108 bdb_entry_return( ei->bei_e );
1113 free( ei->bei_nrdn.bv_val );
1114 ei->bei_nrdn.bv_val = NULL;
1116 free( ei->bei_rdn.bv_val );
1117 ei->bei_rdn.bv_val = NULL;
1118 ei->bei_modrdns = 0;
1122 ei->bei_parent = NULL;
1123 ei->bei_kids = NULL;
1124 ei->bei_lruprev = NULL;
1126 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1127 ei->bei_lrunext = cache->c_eifree;
1128 cache->c_eifree = ei;
1129 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1130 bdb_cache_entryinfo_unlock( ei );
1134 bdb_cache_delete_internal(
1139 int rc = 0; /* return code */
1141 /* Lock the parent's kids tree */
1142 bdb_cache_entryinfo_lock( e->bei_parent );
1145 e->bei_parent->bei_ckids--;
1146 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1149 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1154 if ( e->bei_parent->bei_kids )
1158 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1166 LRU_DELETE( cache, e );
1167 if ( e->bei_e ) cache->c_cursize--;
1170 bdb_cache_entryinfo_unlock( e->bei_parent );
1176 bdb_entryinfo_release( void *data )
1178 EntryInfo *ei = (EntryInfo *)data;
1179 if ( ei->bei_kids ) {
1180 avl_free( ei->bei_kids, NULL );
1183 ei->bei_e->e_private = NULL;
1184 #ifdef SLAP_ZONE_ALLOC
1185 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1187 bdb_entry_return( ei->bei_e );
1190 bdb_cache_entryinfo_destroy( ei );
1194 bdb_cache_release_all( Cache *cache )
1196 /* set cache write lock */
1197 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1199 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1201 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1203 avl_free( cache->c_dntree.bei_kids, NULL );
1204 avl_free( cache->c_idtree, bdb_entryinfo_release );
1205 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1206 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1207 bdb_cache_entryinfo_destroy(cache->c_eifree);
1209 cache->c_cursize = 0;
1210 cache->c_eiused = 0;
1211 cache->c_leaves = 0;
1212 cache->c_idtree = NULL;
1213 cache->c_lruhead = NULL;
1214 cache->c_lrutail = NULL;
1215 cache->c_dntree.bei_kids = NULL;
1217 /* free lru mutex */
1218 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1219 /* free cache write lock */
1220 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1225 bdb_lru_print( Cache *cache )
1229 fprintf( stderr, "LRU queue (head to tail):\n" );
1230 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1231 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1232 e->bei_nrdn.bv_val, e->bei_id );
1234 fprintf( stderr, "LRU queue (tail to head):\n" );
1235 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1236 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1237 e->bei_nrdn.bv_val, e->bei_id );
1242 #ifdef BDB_REUSE_LOCKERS
1244 bdb_locker_id_free( void *key, void *data )
1247 u_int32_t lockid = (long)data;
1250 rc = XLOCK_ID_FREE( env, lockid );
1251 if ( rc == EINVAL ) {
1253 Debug( LDAP_DEBUG_ANY,
1254 "bdb_locker_id_free: %lu err %s(%d)\n",
1255 (unsigned long) lockid, db_strerror(rc), rc );
1256 /* release all locks held by this locker. */
1257 lr.op = DB_LOCK_PUT_ALL;
1259 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1260 XLOCK_ID_FREE( env, lockid );
1265 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1272 if ( !env || !locker ) return -1;
1274 /* If no op was provided, try to find the ctx anyway... */
1276 ctx = op->o_threadctx;
1278 ctx = ldap_pvt_thread_pool_context();
1281 /* Shouldn't happen unless we're single-threaded */
1287 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1288 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1289 rc = XLOCK_ID( env, &lockid );
1290 if (rc) ldap_pvt_thread_yield();
1295 data = (void *)((long)lockid);
1296 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1297 data, bdb_locker_id_free ) ) ) {
1298 XLOCK_ID_FREE( env, lockid );
1299 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1300 db_strerror(rc), rc, 0 );
1305 lockid = (long)data;
1310 #endif /* BDB_REUSE_LOCKERS */
1313 bdb_cache_delete_entry(
1314 struct bdb_info *bdb,
1319 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1320 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1322 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1323 LRU_DELETE( &bdb->bi_cache, ei );
1324 ei->bei_e->e_private = NULL;
1325 #ifdef SLAP_ZONE_ALLOC
1326 bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1328 bdb_entry_return( ei->bei_e );
1331 --bdb->bi_cache.c_cursize;
1333 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1335 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );