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, int reset );
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,
92 Debug( LDAP_DEBUG_TRACE,
93 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
103 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
104 int rw, int tryOnly, DB_LOCK *lock )
113 if ( !lock ) return 0;
116 db_rw = DB_LOCK_WRITE;
118 db_rw = DB_LOCK_READ;
120 lockobj.data = &ei->bei_id;
121 lockobj.size = sizeof(ei->bei_id) + 1;
123 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
124 &lockobj, db_rw, lock);
125 if (rc && !tryOnly) {
126 Debug( LDAP_DEBUG_TRACE,
127 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
128 ei->bei_id, rw, rc );
131 #endif /* NO_THREADS */
135 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
142 if ( !lock ) return 0;
144 rc = LOCK_PUT ( env, lock );
150 bdb_cache_entryinfo_destroy( EntryInfo *e )
152 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
153 free( e->bei_nrdn.bv_val );
155 free( e->bei_rdn.bv_val );
161 #define LRU_DELETE( cache, ei ) do { \
162 if ( (ei)->bei_lruprev != NULL ) { \
163 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
165 (cache)->c_lruhead = (ei)->bei_lrunext; \
167 if ( (ei)->bei_lrunext != NULL ) { \
168 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
170 (cache)->c_lrutail = (ei)->bei_lruprev; \
172 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
175 #define LRU_ADD( cache, ei ) do { \
176 (ei)->bei_lrunext = (cache)->c_lruhead; \
177 if ( (ei)->bei_lrunext != NULL ) { \
178 (ei)->bei_lrunext->bei_lruprev = (ei); \
180 (cache)->c_lruhead = (ei); \
181 (ei)->bei_lruprev = NULL; \
182 if ( (cache)->c_lrutail == NULL ) { \
183 (cache)->c_lrutail = (ei); \
187 /* Do a length-ordered sort on normalized RDNs */
189 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
191 const EntryInfo *e1 = v_e1, *e2 = v_e2;
192 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
194 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
195 e1->bei_nrdn.bv_len );
201 bdb_id_cmp( const void *v_e1, const void *v_e2 )
203 const EntryInfo *e1 = v_e1, *e2 = v_e2;
204 return e1->bei_id - e2->bei_id;
207 /* Create an entryinfo in the cache. Caller must release the locks later.
210 bdb_entryinfo_add_internal(
211 struct bdb_info *bdb,
215 EntryInfo *ei2 = NULL;
219 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
221 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
222 bdb_cache_entryinfo_lock( ei->bei_parent );
224 ei2->bei_id = ei->bei_id;
225 ei2->bei_parent = ei->bei_parent;
227 ei2->bei_rdn = ei->bei_rdn;
230 /* Add to cache ID tree */
231 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
233 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
234 bdb_cache_entryinfo_destroy( ei2 );
237 /* It got freed above because its value was
240 ei->bei_rdn.bv_val = NULL;
243 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
244 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
247 ei->bei_parent->bei_ckids++;
255 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
256 * the info for its closest ancestor. *res should be NULL to process a
257 * complete DN starting from the tree root. Otherwise *res must be the
258 * immediate parent of the requested DN, and only the RDN will be searched.
259 * The EntryInfo is locked upon return and must be unlocked by the caller.
268 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
269 EntryInfo ei, *eip, *ei2;
273 /* this function is always called with normalized DN */
275 /* we're doing a onelevel search for an RDN */
276 ei.bei_nrdn.bv_val = ndn->bv_val;
277 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
280 /* we're searching a full DN from the root */
281 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
282 ei.bei_nrdn.bv_val = ptr;
283 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
284 /* Skip to next rdn if suffix is empty */
285 if ( ei.bei_nrdn.bv_len == 0 ) {
286 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
287 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
288 if ( ptr >= ndn->bv_val ) {
289 if (DN_SEPARATOR(*ptr)) ptr++;
290 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
291 ei.bei_nrdn.bv_val = ptr;
294 eip = &bdb->bi_cache.c_dntree;
297 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
299 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
301 int len = ei.bei_nrdn.bv_len;
303 ei.bei_nrdn.bv_len = ndn->bv_len -
304 (ei.bei_nrdn.bv_val - ndn->bv_val);
305 bdb_cache_entryinfo_unlock( eip );
307 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
309 bdb_cache_entryinfo_lock( eip );
314 /* DN exists but needs to be added to cache */
315 ei.bei_nrdn.bv_len = len;
316 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
317 /* add_internal left eip and c_rwlock locked */
318 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
323 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
324 /* In the midst of deleting? Give it a chance to
327 bdb_cache_entryinfo_unlock( eip );
328 ldap_pvt_thread_yield();
329 bdb_cache_entryinfo_lock( eip );
333 bdb_cache_entryinfo_unlock( eip );
334 bdb_cache_entryinfo_lock( ei2 );
338 /* Advance to next lower RDN */
339 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
340 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
341 if ( ptr >= ndn->bv_val ) {
342 if (DN_SEPARATOR(*ptr)) ptr++;
343 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
344 ei.bei_nrdn.bv_val = ptr;
346 if ( ptr < ndn->bv_val ) {
356 /* Walk up the tree from a child node, looking for an ID that's already
357 * been linked into the cache.
360 hdb_cache_find_parent(
366 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
367 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
368 char ndn[SLAP_LDAPDN_MAXLEN];
377 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
380 /* Save the previous node, if any */
383 /* Create a new node for the current ID */
384 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
385 ein->bei_id = ei.bei_id;
386 ein->bei_kids = ei.bei_kids;
387 ein->bei_nrdn = ei.bei_nrdn;
388 ein->bei_rdn = ei.bei_rdn;
390 /* This node is not fully connected yet */
391 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
393 /* Insert this node into the ID tree */
394 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
395 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
396 bdb_id_cmp, avl_dup_error ) ) {
398 /* Someone else created this node just before us.
399 * Free our new copy and use the existing one.
401 bdb_cache_entryinfo_destroy( ein );
402 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
403 (caddr_t) &ei, bdb_id_cmp );
405 /* Link in any kids we've already processed */
407 bdb_cache_entryinfo_lock( ein );
408 avl_insert( &ein->bei_kids, (caddr_t)ei2,
409 bdb_rdn_cmp, avl_dup_error );
410 bdb_cache_entryinfo_unlock( ein );
418 /* If this is the first time, save this node
419 * to be returned later.
421 if ( eir == NULL ) eir = ein;
423 /* If there was a previous node, link it to this one */
424 if ( ei2 ) ei2->bei_parent = ein;
426 /* Look for this node's parent */
428 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
429 (caddr_t) &eip, bdb_id_cmp );
431 ei2 = &bdb->bi_cache.c_dntree;
433 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
435 /* Got the parent, link in and we're done. */
437 bdb_cache_entryinfo_lock( ei2 );
438 ein->bei_parent = ei2;
439 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
441 bdb_cache_entryinfo_unlock( ei2 );
442 bdb_cache_entryinfo_lock( eir );
444 /* Reset all the state info */
445 for (ein = eir; ein != ei2; ein=ein->bei_parent)
446 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
451 ei.bei_id = eip.bei_id;
452 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
458 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
462 struct bdb_info *bdb,
469 /* See if we already have this one */
470 bdb_cache_entryinfo_lock( ei->bei_parent );
471 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
472 bdb_cache_entryinfo_unlock( ei->bei_parent );
475 /* Not found, add it */
478 /* bei_rdn was not malloc'd before, do it now */
479 ber_dupbv( &bv, &ei->bei_rdn );
482 rc = bdb_entryinfo_add_internal( bdb, ei, res );
483 bdb_cache_entryinfo_unlock( ei->bei_parent );
484 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
486 /* Found, return it */
494 /* caller must have lru_mutex locked. mutex
495 * will be unlocked on return.
499 struct bdb_info *bdb,
503 DB_LOCK lock, *lockp;
511 /* See if we're above the cache size limit */
512 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
513 EntryInfo *elru, *elprev;
516 /* Look for an unused entry to remove */
517 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
518 elprev = elru->bei_lruprev;
520 /* Too many probes, not enough idle, give up */
523 /* If we can successfully writelock it, then
524 * the object is idle.
526 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, elru, 1, 1,
528 /* If there's no entry, or this node is in
529 * the process of linking into the cache,
530 * or this node is being deleted, skip it.
532 if ( !elru->bei_e || (elru->bei_state &
533 ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED ))) {
534 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
537 LRU_DELETE( &bdb->bi_cache, elru );
538 elru->bei_e->e_private = NULL;
539 bdb_entry_return( elru->bei_e );
541 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
542 --bdb->bi_cache.c_cursize;
543 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
548 LRU_ADD( &bdb->bi_cache, ei );
549 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
554 struct bdb_info *bdb,
557 EntryInfo ei = { 0 },
562 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
563 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
564 (caddr_t) &ei, bdb_id_cmp );
565 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
570 * cache_find_id - find an entry in the cache, given id.
571 * The entry is locked for Read upon return. Call with islocked TRUE if
572 * the supplied *eip was already locked.
585 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
588 EntryInfo ei = { 0 };
592 /* If we weren't given any info, see if we have it already cached */
594 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
595 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
596 (caddr_t) &ei, bdb_id_cmp );
598 /* If the lock attempt fails, the info is in use */
599 if ( ldap_pvt_thread_mutex_trylock(
600 &(*eip)->bei_kids_mutex )) {
601 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
602 /* If this node is being deleted, treat
603 * as if the delete has already finished
605 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
608 /* otherwise, wait for the info to free up */
609 ldap_pvt_thread_yield();
612 /* If this info isn't hooked up to its parent yet,
613 * unlock and wait for it to be fully initialized
615 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
616 bdb_cache_entryinfo_unlock( *eip );
617 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
618 ldap_pvt_thread_yield();
623 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
626 /* See if the ID exists in the database; add it to the cache if so */
629 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
631 rc = bdb_cache_find_ndn( op, tid,
633 if ( *eip ) islocked = 1;
635 bdb_entry_return( ep );
640 rc = hdb_cache_find_parent(op, tid, id, eip );
641 if ( rc == 0 && *eip ) islocked = 1;
645 /* Ok, we found the info, do we have the entry? */
646 if ( *eip && rc == 0 ) {
647 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
651 /* Make sure only one thread tries to load the entry */
652 load1: if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
654 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
657 bdb_cache_entryinfo_unlock( *eip );
660 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
661 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
663 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
664 } else if ( rc == 0 ) {
667 u_int32_t locker2 = locker;
669 /* We don't wrap entire read operations in txn's, but
670 * we need our cache entry lock and any DB page locks
671 * to be associated, in order for deadlock detection
672 * to work properly. So if we need to read from the DB,
673 * we use a long-lived per-thread txn for this step.
676 rc = bdb_txn_get( op, bdb->bi_dbenv, <id, 0 );
678 locker2 = TXN_ID( ltid );
682 /* Give up original read lock, obtain write lock with
683 * (possibly) new locker ID.
686 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker2,
689 if ( rc == 0 && !ep) {
690 rc = bdb_id2entry( op->o_bd, ltid, id, &ep );
693 ep->e_private = *eip;
700 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
702 /* If we succeeded, downgrade back to a readlock. */
703 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
706 /* Otherwise, release the lock. */
707 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
709 if ( locker2 != locker ) {
710 /* If we're using the per-thread txn, release all
711 * of its page locks now.
714 list.op = DB_LOCK_PUT_ALL;
716 bdb->bi_dbenv->lock_vec( bdb->bi_dbenv, locker2,
718 /* If this txn was deadlocked, we must abort it
719 * and invalidate this per-thread txn.
721 if ( rc == DB_LOCK_DEADLOCK ) {
722 bdb_txn_get( op, bdb->bi_dbenv, <id, 1 );
725 } else if ( !(*eip)->bei_e ) {
726 /* Some other thread is trying to load the entry,
727 * give it a chance to finish.
729 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
730 ldap_pvt_thread_yield();
731 bdb_cache_entryinfo_lock( *eip );
736 /* Check for subtree renames
738 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
740 bdb_cache_entry_db_relock( bdb->bi_dbenv,
741 locker, *eip, 1, 0, lock );
742 /* check again in case other modifier did it already */
743 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
744 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
745 bdb_cache_entry_db_relock( bdb->bi_dbenv,
746 locker, *eip, 0, 0, lock );
755 bdb_cache_entryinfo_unlock( *eip );
758 bdb_entry_return( ep );
762 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
763 /* if entry is on LRU list, remove from old spot */
764 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
765 LRU_DELETE( &bdb->bi_cache, *eip );
767 /* if entry is new, bump cache size */
768 bdb->bi_cache.c_cursize++;
770 /* lru_mutex is unlocked for us */
771 bdb_cache_lru_add( bdb, locker, *eip );
785 if ( BEI(e)->bei_kids ) {
788 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
791 rc = bdb_dn2id_children( op, txn, e );
792 if ( rc == DB_NOTFOUND ) {
793 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
798 /* Update the cache after a successful database Add. */
801 struct bdb_info *bdb,
808 struct berval rdn = e->e_name;
817 /* Lock this entry so that bdb_add can run to completion.
818 * It can only fail if BDB has run out of lock resources.
820 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
822 bdb_cache_entryinfo_unlock( eip );
827 if ( nrdn->bv_len != e->e_nname.bv_len ) {
828 char *ptr = strchr( rdn.bv_val, ',' );
829 rdn.bv_len = ptr - rdn.bv_val;
831 ber_dupbv( &ei.bei_rdn, &rdn );
832 if ( eip->bei_dkids ) eip->bei_dkids++;
835 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
836 /* bdb_csn_commit can cause this when adding the database root entry */
838 new->bei_e->e_private = NULL;
839 bdb_entry_return( new->bei_e );
843 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
844 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
845 if (eip->bei_parent) {
846 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
850 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
851 ++bdb->bi_cache.c_cursize;
852 /* lru_mutex is unlocked for us */
853 bdb_cache_lru_add( bdb, locker, new );
855 bdb_cache_entryinfo_unlock( eip );
856 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
868 EntryInfo *ei = BEI(e);
870 /* Get write lock on data */
871 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
873 /* If we've done repeated mods on a cached entry, then e_attrs
874 * is no longer contiguous with the entry, and must be freed.
877 if ( (void *)e->e_attrs != (void *)(e+1) ) {
878 attrs_free( e->e_attrs );
880 e->e_attrs = newAttrs;
886 * Change the rdn in the entryinfo. Also move to a new parent if needed.
898 EntryInfo *ei = BEI(e), *pei;
902 /* Get write lock on data */
903 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
906 /* If we've done repeated mods on a cached entry, then e_attrs
907 * is no longer contiguous with the entry, and must be freed.
909 if ( (void *)e->e_attrs != (void *)(e+1) ) {
910 attrs_free( e->e_attrs );
912 e->e_attrs = new->e_attrs;
913 if( e->e_nname.bv_val < e->e_bv.bv_val ||
914 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
916 ch_free(e->e_name.bv_val);
917 ch_free(e->e_nname.bv_val);
919 e->e_name = new->e_name;
920 e->e_nname = new->e_nname;
922 /* Lock the parent's kids AVL tree */
923 pei = ei->bei_parent;
924 bdb_cache_entryinfo_lock( pei );
925 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
926 free( ei->bei_nrdn.bv_val );
927 ber_dupbv( &ei->bei_nrdn, nrdn );
929 free( ei->bei_rdn.bv_val );
932 if ( nrdn->bv_len != e->e_nname.bv_len ) {
933 char *ptr = strchr(rdn.bv_val, ',');
934 rdn.bv_len = ptr - rdn.bv_val;
936 ber_dupbv( &ei->bei_rdn, &rdn );
940 ein = ei->bei_parent;
942 ei->bei_parent = ein;
943 bdb_cache_entryinfo_unlock( pei );
944 bdb_cache_entryinfo_lock( ein );
948 int max = ei->bei_modrdns;
949 /* Record the generation number of this change */
950 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
951 if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
953 ei->bei_modrdns = max + 1;
956 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
957 bdb_cache_entryinfo_unlock( ein );
961 * cache_delete - delete the entry e from the cache.
963 * returns: 0 e was deleted ok
964 * 1 e was not in the cache
965 * -1 something bad happened
975 EntryInfo *ei = BEI(e);
978 assert( e->e_private );
980 /* Set this early, warn off any queriers */
981 ei->bei_state |= CACHE_ENTRY_DELETED;
983 /* Lock the entry's info */
984 bdb_cache_entryinfo_lock( ei );
986 /* Get write lock on the data */
987 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
989 /* couldn't lock, undo and give up */
990 ei->bei_state ^= CACHE_ENTRY_DELETED;
991 bdb_cache_entryinfo_unlock( ei );
995 /* set cache write lock */
996 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
998 /* Lock the parent's kids tree */
999 bdb_cache_entryinfo_lock( ei->bei_parent );
1001 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1005 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1006 rc = bdb_cache_delete_internal( cache, e->e_private );
1007 /* free lru mutex */
1008 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1010 /* free cache write lock */
1011 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1012 bdb_cache_entryinfo_unlock( ei->bei_parent );
1014 /* Leave entry info locked */
1020 bdb_cache_delete_cleanup(
1024 EntryInfo *ei = BEI(e);
1027 e->e_private = NULL;
1028 bdb_entry_return( e );
1030 free( ei->bei_nrdn.bv_val );
1031 ei->bei_nrdn.bv_val = NULL;
1033 free( ei->bei_rdn.bv_val );
1034 ei->bei_rdn.bv_val = NULL;
1035 ei->bei_modrdns = 0;
1039 ei->bei_parent = NULL;
1040 ei->bei_kids = NULL;
1041 ei->bei_lruprev = NULL;
1043 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1044 ei->bei_lrunext = cache->c_eifree;
1045 cache->c_eifree = ei;
1046 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1047 bdb_cache_entryinfo_unlock( ei );
1051 bdb_cache_delete_internal(
1055 int rc = 0; /* return code */
1058 e->bei_parent->bei_ckids--;
1059 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1062 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1069 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1078 LRU_DELETE( cache, e );
1082 * flag entry to be freed later by a call to cache_return_entry()
1084 e->bei_state |= CACHE_ENTRY_DELETED;
1090 bdb_entryinfo_release( void *data )
1092 EntryInfo *ei = (EntryInfo *)data;
1093 if ( ei->bei_kids ) {
1094 avl_free( ei->bei_kids, NULL );
1097 ei->bei_e->e_private = NULL;
1098 bdb_entry_return( ei->bei_e );
1100 bdb_cache_entryinfo_destroy( ei );
1104 bdb_cache_release_all( Cache *cache )
1106 /* set cache write lock */
1107 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1109 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1111 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1113 avl_free( cache->c_dntree.bei_kids, NULL );
1114 avl_free( cache->c_idtree, bdb_entryinfo_release );
1115 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1116 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1117 bdb_cache_entryinfo_destroy(cache->c_eifree);
1119 cache->c_lruhead = NULL;
1120 cache->c_lrutail = NULL;
1122 /* free lru mutex */
1123 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1124 /* free cache write lock */
1125 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1130 bdb_lru_print( Cache *cache )
1134 fprintf( stderr, "LRU queue (head to tail):\n" );
1135 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1136 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1137 e->bei_nrdn.bv_val, e->bei_id );
1139 fprintf( stderr, "LRU queue (tail to head):\n" );
1140 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1141 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1142 e->bei_nrdn.bv_val, e->bei_id );
1148 bdb_txn_free( void *key, void *data )
1154 /* Obtain a long-lived transaction for the current thread.
1155 * If reset == 1, remove the current transaction. */
1157 bdb_txn_get( Operation *op, DB_ENV *env, DB_TXN **txn, int reset )
1160 void *ctx, *data = NULL;
1162 /* If no op was provided, try to find the ctx anyway... */
1164 ctx = op->o_threadctx;
1166 ctx = ldap_pvt_thread_pool_context();
1169 /* Shouldn't happen unless we're single-threaded */
1177 return ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1, NULL, NULL );
1180 if ( ldap_pvt_thread_pool_getkey( ctx, ((char *)env)+1, &data, NULL ) ||
1182 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1183 rc = TXN_BEGIN( env, NULL, txn, 0 );
1184 if (rc) ldap_pvt_thread_yield();
1189 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1,
1190 *txn, bdb_txn_free ) ) ) {
1192 Debug( LDAP_DEBUG_ANY, "bdb_txn_get: err %s(%d)\n",
1193 db_strerror(rc), rc, 0 );
1203 #ifdef BDB_REUSE_LOCKERS
1205 bdb_locker_id_free( void *key, void *data )
1208 int lockid = (int) data;
1211 rc = XLOCK_ID_FREE( env, lockid );
1212 if ( rc == EINVAL ) {
1214 Debug( LDAP_DEBUG_ANY,
1215 "bdb_locker_id_free: %d err %s(%d)\n",
1216 lockid, db_strerror(rc), rc );
1217 /* release all locks held by this locker. */
1218 lr.op = DB_LOCK_PUT_ALL;
1220 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1221 XLOCK_ID_FREE( env, lockid );
1226 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1232 if ( !env || !locker ) return -1;
1234 /* If no op was provided, try to find the ctx anyway... */
1236 ctx = op->o_threadctx;
1238 ctx = ldap_pvt_thread_pool_context();
1241 /* Shouldn't happen unless we're single-threaded */
1247 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1248 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1249 rc = XLOCK_ID( env, &lockid );
1250 if (rc) ldap_pvt_thread_yield();
1255 data = (void *)lockid;
1256 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1257 data, bdb_locker_id_free ) ) ) {
1258 XLOCK_ID_FREE( env, lockid );
1259 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1260 db_strerror(rc), rc, 0 );
1273 bdb_cache_delete_entry(
1274 struct bdb_info *bdb,
1279 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1280 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1282 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1283 LRU_DELETE( &bdb->bi_cache, ei );
1284 ei->bei_e->e_private = NULL;
1285 bdb_entry_return( ei->bei_e );
1287 --bdb->bi_cache.c_cursize;
1289 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1291 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );