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-2004 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>
29 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e);
31 static void bdb_lru_print(Cache *cache);
34 static int bdb_txn_get( Operation *op, DB_ENV *env, DB_TXN **txn );
37 bdb_cache_entryinfo_new( Cache *cache )
41 if ( cache->c_eifree ) {
42 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
43 if ( cache->c_eifree ) {
45 cache->c_eifree = ei->bei_lrunext;
47 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
50 ei->bei_lrunext = NULL;
53 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
54 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
60 /* Atomically release and reacquire a lock */
62 bdb_cache_entry_db_relock(
77 if ( !lock ) return 0;
79 lockobj.data = &ei->bei_id;
80 lockobj.size = sizeof(ei->bei_id) + 1;
82 list[0].op = DB_LOCK_PUT;
84 list[1].op = DB_LOCK_GET;
86 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
87 list[1].obj = &lockobj;
88 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
93 LDAP_LOG( CACHE, DETAIL1,
94 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
97 Debug( LDAP_DEBUG_TRACE,
98 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
102 *lock = list[1].lock;
109 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
110 int rw, int tryOnly, DB_LOCK *lock )
119 if ( !lock ) return 0;
122 db_rw = DB_LOCK_WRITE;
124 db_rw = DB_LOCK_READ;
126 lockobj.data = &ei->bei_id;
127 lockobj.size = sizeof(ei->bei_id) + 1;
129 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
130 &lockobj, db_rw, lock);
131 if (rc && !tryOnly) {
133 LDAP_LOG( CACHE, DETAIL1,
134 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
135 ei->bei_id, rw, rc );
137 Debug( LDAP_DEBUG_TRACE,
138 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
139 ei->bei_id, rw, rc );
143 #endif /* NO_THREADS */
147 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
154 if ( !lock ) return 0;
156 rc = LOCK_PUT ( env, lock );
162 bdb_cache_entryinfo_destroy( EntryInfo *e )
164 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
165 free( e->bei_nrdn.bv_val );
167 free( e->bei_rdn.bv_val );
173 #define LRU_DELETE( cache, ei ) do { \
174 if ( (ei)->bei_lruprev != NULL ) { \
175 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
177 (cache)->c_lruhead = (ei)->bei_lrunext; \
179 if ( (ei)->bei_lrunext != NULL ) { \
180 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
182 (cache)->c_lrutail = (ei)->bei_lruprev; \
184 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
187 #define LRU_ADD( cache, ei ) do { \
188 (ei)->bei_lrunext = (cache)->c_lruhead; \
189 if ( (ei)->bei_lrunext != NULL ) { \
190 (ei)->bei_lrunext->bei_lruprev = (ei); \
192 (cache)->c_lruhead = (ei); \
193 (ei)->bei_lruprev = NULL; \
194 if ( (cache)->c_lrutail == NULL ) { \
195 (cache)->c_lrutail = (ei); \
199 /* Do a length-ordered sort on normalized RDNs */
201 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
203 const EntryInfo *e1 = v_e1, *e2 = v_e2;
204 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
206 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
207 e1->bei_nrdn.bv_len );
213 bdb_id_cmp( const void *v_e1, const void *v_e2 )
215 const EntryInfo *e1 = v_e1, *e2 = v_e2;
216 return e1->bei_id - e2->bei_id;
219 /* Create an entryinfo in the cache. Caller must release the locks later.
222 bdb_entryinfo_add_internal(
223 struct bdb_info *bdb,
227 EntryInfo *ei2 = NULL;
231 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
233 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
234 bdb_cache_entryinfo_lock( ei->bei_parent );
236 ei2->bei_id = ei->bei_id;
237 ei2->bei_parent = ei->bei_parent;
239 ei2->bei_rdn = ei->bei_rdn;
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 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
256 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
259 ei->bei_parent->bei_ckids++;
267 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
268 * the info for its closest ancestor. *res should be NULL to process a
269 * complete DN starting from the tree root. Otherwise *res must be the
270 * immediate parent of the requested DN, and only the RDN will be searched.
271 * The EntryInfo is locked upon return and must be unlocked by the caller.
280 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
281 EntryInfo ei, *eip, *ei2;
285 /* this function is always called with normalized DN */
287 /* we're doing a onelevel search for an RDN */
288 ei.bei_nrdn.bv_val = ndn->bv_val;
289 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
292 /* we're searching a full DN from the root */
293 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
294 ei.bei_nrdn.bv_val = ptr;
295 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
296 /* Skip to next rdn if suffix is empty */
297 if ( ei.bei_nrdn.bv_len == 0 ) {
298 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
299 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
300 if ( ptr >= ndn->bv_val ) {
301 if (DN_SEPARATOR(*ptr)) ptr++;
302 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
303 ei.bei_nrdn.bv_val = ptr;
306 eip = &bdb->bi_cache.c_dntree;
309 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
311 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
313 int len = ei.bei_nrdn.bv_len;
315 ei.bei_nrdn.bv_len = ndn->bv_len -
316 (ei.bei_nrdn.bv_val - ndn->bv_val);
317 bdb_cache_entryinfo_unlock( eip );
319 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
321 bdb_cache_entryinfo_lock( eip );
326 /* DN exists but needs to be added to cache */
327 ei.bei_nrdn.bv_len = len;
328 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
329 /* add_internal left eip and c_rwlock locked */
330 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
335 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
336 /* In the midst of deleting? Give it a chance to
339 bdb_cache_entryinfo_unlock( eip );
340 ldap_pvt_thread_yield();
341 bdb_cache_entryinfo_lock( eip );
345 bdb_cache_entryinfo_unlock( eip );
346 bdb_cache_entryinfo_lock( ei2 );
350 /* Advance to next lower RDN */
351 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
352 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
353 if ( ptr >= ndn->bv_val ) {
354 if (DN_SEPARATOR(*ptr)) ptr++;
355 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
356 ei.bei_nrdn.bv_val = ptr;
358 if ( ptr < ndn->bv_val ) {
368 /* Walk up the tree from a child node, looking for an ID that's already
369 * been linked into the cache.
372 hdb_cache_find_parent(
378 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
379 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
380 char ndn[SLAP_LDAPDN_MAXLEN];
389 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
392 /* Save the previous node, if any */
395 /* Create a new node for the current ID */
396 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
397 ein->bei_id = ei.bei_id;
398 ein->bei_kids = ei.bei_kids;
399 ein->bei_nrdn = ei.bei_nrdn;
400 ein->bei_rdn = ei.bei_rdn;
402 /* This node is not fully connected yet */
403 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
405 /* Insert this node into the ID tree */
406 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
407 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
408 bdb_id_cmp, avl_dup_error ) ) {
410 /* Someone else created this node just before us.
411 * Free our new copy and use the existing one.
413 bdb_cache_entryinfo_destroy( ein );
414 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
415 (caddr_t) &ei, bdb_id_cmp );
417 /* Link in any kids we've already processed */
419 bdb_cache_entryinfo_lock( ein );
420 avl_insert( &ein->bei_kids, (caddr_t)ei2,
421 bdb_rdn_cmp, avl_dup_error );
422 bdb_cache_entryinfo_unlock( ein );
430 /* If this is the first time, save this node
431 * to be returned later.
433 if ( eir == NULL ) eir = ein;
435 /* If there was a previous node, link it to this one */
436 if ( ei2 ) ei2->bei_parent = ein;
438 /* Look for this node's parent */
440 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
441 (caddr_t) &eip, bdb_id_cmp );
443 ei2 = &bdb->bi_cache.c_dntree;
445 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
447 /* Got the parent, link in and we're done. */
449 bdb_cache_entryinfo_lock( ei2 );
450 ein->bei_parent = ei2;
451 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
453 bdb_cache_entryinfo_unlock( ei2 );
454 bdb_cache_entryinfo_lock( eir );
456 /* Reset all the state info */
457 for (ein = eir; ein != ei2; ein=ein->bei_parent)
458 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
463 ei.bei_id = eip.bei_id;
464 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
470 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
474 struct bdb_info *bdb,
481 /* See if we already have this one */
482 bdb_cache_entryinfo_lock( ei->bei_parent );
483 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
484 bdb_cache_entryinfo_unlock( ei->bei_parent );
487 /* Not found, add it */
490 /* bei_rdn was not malloc'd before, do it now */
491 ber_dupbv( &bv, &ei->bei_rdn );
494 rc = bdb_entryinfo_add_internal( bdb, ei, res );
495 bdb_cache_entryinfo_unlock( ei->bei_parent );
496 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
498 /* Found, return it */
506 /* caller must have lru_mutex locked. mutex
507 * will be unlocked on return.
511 struct bdb_info *bdb,
515 DB_LOCK lock, *lockp;
523 /* See if we're above the cache size limit */
524 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
525 EntryInfo *elru, *elprev;
528 /* Look for an unused entry to remove */
529 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
530 elprev = elru->bei_lruprev;
532 /* Too many probes, not enough idle, give up */
535 /* If we can successfully writelock it, then
536 * the object is idle.
538 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, elru, 1, 1,
540 /* If there's no entry, or this node is in
541 * the process of linking into the cache,
544 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
545 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
548 LRU_DELETE( &bdb->bi_cache, elru );
549 elru->bei_e->e_private = NULL;
550 bdb_entry_return( elru->bei_e );
552 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
553 --bdb->bi_cache.c_cursize;
554 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
559 LRU_ADD( &bdb->bi_cache, ei );
560 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
565 struct bdb_info *bdb,
572 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
573 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
574 (caddr_t) &ei, bdb_id_cmp );
575 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
580 * cache_find_id - find an entry in the cache, given id.
581 * The entry is locked for Read upon return. Call with islocked TRUE if
582 * the supplied *eip was already locked.
595 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
602 /* If we weren't given any info, see if we have it already cached */
604 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
605 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
606 (caddr_t) &ei, bdb_id_cmp );
608 /* If the lock attempt fails, the info is in use */
609 if ( ldap_pvt_thread_mutex_trylock(
610 &(*eip)->bei_kids_mutex )) {
611 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
612 /* If this node is being deleted, treat
613 * as if the delete has already finished
615 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
618 /* otherwise, wait for the info to free up */
619 ldap_pvt_thread_yield();
622 /* If this info isn't hooked up to its parent yet,
623 * unlock and wait for it to be fully initialized
625 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
626 bdb_cache_entryinfo_unlock( *eip );
627 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
628 ldap_pvt_thread_yield();
633 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
636 /* See if the ID exists in the database; add it to the cache if so */
639 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
641 rc = bdb_cache_find_ndn( op, tid,
643 if ( *eip ) islocked = 1;
645 bdb_entry_return( ep );
650 rc = hdb_cache_find_parent(op, tid, id, eip );
651 if ( rc == 0 && *eip ) islocked = 1;
655 /* Ok, we found the info, do we have the entry? */
656 if ( *eip && rc == 0 ) {
657 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
661 /* Make sure only one thread tries to load the entry */
662 load1: if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
664 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
667 bdb_cache_entryinfo_unlock( *eip );
670 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
671 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
673 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
674 } else if ( rc == 0 ) {
677 u_int32_t locker2 = locker;
679 /* We don't wrap entire read operations in txn's, but
680 * we need our cache entry lock and any DB page locks
681 * to be associated, in order for deadlock detection
682 * to work properly. So if we need to read from the DB,
683 * we use a long-lived per-thread txn for this step.
686 rc = bdb_txn_get( op, bdb->bi_dbenv, <id );
688 locker2 = TXN_ID( ltid );
692 /* Give up original read lock, obtain write lock with
693 * (possibly) new locker ID.
696 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker2,
699 if ( rc == 0 && !ep) {
700 rc = bdb_id2entry( op->o_bd, ltid, id, &ep );
703 ep->e_private = *eip;
710 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
712 /* If we succeeded, downgrade back to a readlock. */
713 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
716 /* Otherwise, release the lock. */
717 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
719 if ( locker2 != locker ) {
720 /* If we're using the per-thread txn, release all
721 * of its page locks now.
724 list.op = DB_LOCK_PUT_ALL;
726 bdb->bi_dbenv->lock_vec( bdb->bi_dbenv, locker2,
729 } else if ( !(*eip)->bei_e ) {
730 /* Some other thread is trying to load the entry,
731 * give it a chance to finish.
733 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
734 ldap_pvt_thread_yield();
735 bdb_cache_entryinfo_lock( *eip );
740 /* Check for subtree renames
742 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
744 bdb_cache_entry_db_relock( bdb->bi_dbenv,
745 locker, *eip, 1, 0, lock );
746 /* check again in case other modifier did it already */
747 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
748 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
749 bdb_cache_entry_db_relock( bdb->bi_dbenv,
750 locker, *eip, 0, 0, lock );
759 bdb_cache_entryinfo_unlock( *eip );
762 bdb_entry_return( ep );
766 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
767 /* if entry is on LRU list, remove from old spot */
768 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
769 LRU_DELETE( &bdb->bi_cache, *eip );
771 /* if entry is new, bump cache size */
772 bdb->bi_cache.c_cursize++;
774 /* lru_mutex is unlocked for us */
775 bdb_cache_lru_add( bdb, locker, *eip );
789 if ( BEI(e)->bei_kids ) {
792 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
795 rc = bdb_dn2id_children( op, txn, e );
796 if ( rc == DB_NOTFOUND ) {
797 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
802 /* Update the cache after a successful database Add. */
805 struct bdb_info *bdb,
812 struct berval rdn = e->e_name;
821 /* Lock this entry so that bdb_add can run to completion.
822 * It can only fail if BDB has run out of lock resources.
824 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
826 bdb_cache_entryinfo_unlock( eip );
831 if ( nrdn->bv_len != e->e_nname.bv_len ) {
832 char *ptr = strchr( rdn.bv_val, ',' );
833 rdn.bv_len = ptr - rdn.bv_val;
835 ber_dupbv( &ei.bei_rdn, &rdn );
836 if ( eip->bei_dkids ) eip->bei_dkids++;
839 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
840 /* bdb_csn_commit can cause this when adding the database root entry */
842 new->bei_e->e_private = NULL;
843 bdb_entry_return( new->bei_e );
847 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
848 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
849 if (eip->bei_parent) {
850 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
854 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
855 ++bdb->bi_cache.c_cursize;
856 /* lru_mutex is unlocked for us */
857 bdb_cache_lru_add( bdb, locker, new );
859 bdb_cache_entryinfo_unlock( eip );
860 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
872 EntryInfo *ei = BEI(e);
874 /* Get write lock on data */
875 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
877 /* If we've done repeated mods on a cached entry, then e_attrs
878 * is no longer contiguous with the entry, and must be freed.
881 if ( (void *)e->e_attrs != (void *)(e+1) ) {
882 attrs_free( e->e_attrs );
884 e->e_attrs = newAttrs;
890 * Change the rdn in the entryinfo. Also move to a new parent if needed.
902 EntryInfo *ei = BEI(e), *pei;
906 /* Get write lock on data */
907 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
910 /* If we've done repeated mods on a cached entry, then e_attrs
911 * is no longer contiguous with the entry, and must be freed.
913 if ( (void *)e->e_attrs != (void *)(e+1) ) {
914 attrs_free( e->e_attrs );
916 e->e_attrs = new->e_attrs;
917 if( e->e_nname.bv_val < e->e_bv.bv_val ||
918 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
920 ch_free(e->e_name.bv_val);
921 ch_free(e->e_nname.bv_val);
923 e->e_name = new->e_name;
924 e->e_nname = new->e_nname;
926 /* Lock the parent's kids AVL tree */
927 pei = ei->bei_parent;
928 bdb_cache_entryinfo_lock( pei );
929 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
930 free( ei->bei_nrdn.bv_val );
931 ber_dupbv( &ei->bei_nrdn, nrdn );
933 free( ei->bei_rdn.bv_val );
936 if ( nrdn->bv_len != e->e_nname.bv_len ) {
937 char *ptr = strchr(rdn.bv_val, ',');
938 rdn.bv_len = ptr - rdn.bv_val;
940 ber_dupbv( &ei->bei_rdn, &rdn );
944 ein = ei->bei_parent;
946 ei->bei_parent = ein;
947 bdb_cache_entryinfo_unlock( pei );
948 bdb_cache_entryinfo_lock( ein );
952 int max = ei->bei_modrdns;
953 /* Record the generation number of this change */
954 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
955 if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
957 ei->bei_modrdns = max + 1;
960 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
961 bdb_cache_entryinfo_unlock( ein );
965 * cache_delete - delete the entry e from the cache.
967 * returns: 0 e was deleted ok
968 * 1 e was not in the cache
969 * -1 something bad happened
979 EntryInfo *ei = BEI(e);
982 assert( e->e_private );
984 /* Set this early, warn off any queriers */
985 ei->bei_state |= CACHE_ENTRY_DELETED;
987 /* Lock the entry's info */
988 bdb_cache_entryinfo_lock( ei );
990 /* Get write lock on the data */
991 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
993 /* couldn't lock, undo and give up */
994 ei->bei_state ^= CACHE_ENTRY_DELETED;
995 bdb_cache_entryinfo_unlock( ei );
999 /* set cache write lock */
1000 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1002 /* Lock the parent's kids tree */
1003 bdb_cache_entryinfo_lock( ei->bei_parent );
1006 LDAP_LOG( CACHE, ENTRY,
1007 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
1009 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1014 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1015 rc = bdb_cache_delete_internal( cache, e->e_private );
1016 /* free lru mutex */
1017 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1019 /* free cache write lock */
1020 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1021 bdb_cache_entryinfo_unlock( ei->bei_parent );
1023 /* Leave entry info locked */
1029 bdb_cache_delete_cleanup(
1033 EntryInfo *ei = BEI(e);
1036 e->e_private = NULL;
1037 bdb_entry_return( e );
1039 free( ei->bei_nrdn.bv_val );
1040 ei->bei_nrdn.bv_val = NULL;
1042 free( ei->bei_rdn.bv_val );
1043 ei->bei_rdn.bv_val = NULL;
1044 ei->bei_modrdns = 0;
1048 ei->bei_parent = NULL;
1049 ei->bei_kids = NULL;
1050 ei->bei_lruprev = NULL;
1052 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1053 ei->bei_lrunext = cache->c_eifree;
1054 cache->c_eifree = ei;
1055 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1056 bdb_cache_entryinfo_unlock( ei );
1060 bdb_cache_delete_internal(
1064 int rc = 0; /* return code */
1067 e->bei_parent->bei_ckids--;
1068 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1071 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1078 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1087 LRU_DELETE( cache, e );
1091 * flag entry to be freed later by a call to cache_return_entry()
1093 e->bei_state |= CACHE_ENTRY_DELETED;
1099 bdb_entryinfo_release( void *data )
1101 EntryInfo *ei = (EntryInfo *)data;
1102 if ( ei->bei_kids ) {
1103 avl_free( ei->bei_kids, NULL );
1106 ei->bei_e->e_private = NULL;
1107 bdb_entry_return( ei->bei_e );
1109 bdb_cache_entryinfo_destroy( ei );
1113 bdb_cache_release_all( Cache *cache )
1115 /* set cache write lock */
1116 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1118 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1121 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1123 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1126 avl_free( cache->c_dntree.bei_kids, NULL );
1127 avl_free( cache->c_idtree, bdb_entryinfo_release );
1128 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1129 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1130 bdb_cache_entryinfo_destroy(cache->c_eifree);
1132 cache->c_lruhead = NULL;
1133 cache->c_lrutail = NULL;
1135 /* free lru mutex */
1136 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1137 /* free cache write lock */
1138 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1143 bdb_lru_print( Cache *cache )
1147 fprintf( stderr, "LRU queue (head to tail):\n" );
1148 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1149 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1150 e->bei_nrdn.bv_val, e->bei_id );
1152 fprintf( stderr, "LRU queue (tail to head):\n" );
1153 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1154 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1155 e->bei_nrdn.bv_val, e->bei_id );
1161 bdb_txn_free( void *key, void *data )
1167 /* Obtain a long-lived transaction for the current thread */
1169 bdb_txn_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1174 /* If no op was provided, try to find the ctx anyway... */
1176 ctx = op->o_threadctx;
1178 ctx = ldap_pvt_thread_pool_context();
1181 /* Shouldn't happen unless we're single-threaded */
1187 if ( ldap_pvt_thread_pool_getkey( ctx, ((char *)env)+1, &data, NULL ) ) {
1188 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1189 rc = TXN_BEGIN( env, NULL, txn, 0 );
1190 if (rc) ldap_pvt_thread_yield();
1195 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1,
1196 *txn, bdb_txn_free ) ) ) {
1199 LDAP_LOG( BACK_BDB, ERR, "bdb_txn_get: err %s(%d)\n",
1200 db_strerror(rc), rc, 0 );
1202 Debug( LDAP_DEBUG_ANY, "bdb_txn_get: err %s(%d)\n",
1203 db_strerror(rc), rc, 0 );
1214 #ifdef BDB_REUSE_LOCKERS
1216 bdb_locker_id_free( void *key, void *data )
1219 int lockid = (int) data;
1222 rc = XLOCK_ID_FREE( env, lockid );
1223 if ( rc == EINVAL ) {
1226 LDAP_LOG( BACK_BDB, ERR,
1227 "bdb_locker_id_free: %d err %s(%d)\n",
1228 lockid, db_strerror(rc), rc );
1230 Debug( LDAP_DEBUG_ANY,
1231 "bdb_locker_id_free: %d err %s(%d)\n",
1232 lockid, db_strerror(rc), rc );
1234 /* release all locks held by this locker. */
1235 lr.op = DB_LOCK_PUT_ALL;
1237 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1238 XLOCK_ID_FREE( env, lockid );
1243 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1249 if ( !env || !locker ) return -1;
1251 /* If no op was provided, try to find the ctx anyway... */
1253 ctx = op->o_threadctx;
1255 ctx = ldap_pvt_thread_pool_context();
1258 /* Shouldn't happen unless we're single-threaded */
1264 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1265 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1266 rc = XLOCK_ID( env, &lockid );
1267 if (rc) ldap_pvt_thread_yield();
1272 data = (void *)lockid;
1273 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1274 data, bdb_locker_id_free ) ) ) {
1275 XLOCK_ID_FREE( env, lockid );
1277 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1278 db_strerror(rc), rc, 0 );
1280 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1281 db_strerror(rc), rc, 0 );
1295 bdb_cache_delete_entry(
1296 struct bdb_info *bdb,
1301 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1302 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1304 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1305 LRU_DELETE( &bdb->bi_cache, ei );
1306 ei->bei_e->e_private = NULL;
1307 bdb_entry_return( ei->bei_e );
1309 --bdb->bi_cache.c_cursize;
1311 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1313 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );