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);
35 bdb_cache_entryinfo_new( Cache *cache )
39 if ( cache->c_eifree ) {
40 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
41 if ( cache->c_eifree ) {
43 cache->c_eifree = ei->bei_lrunext;
45 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
48 ei->bei_lrunext = NULL;
51 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
52 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
58 /* Atomically release and reacquire a lock */
60 bdb_cache_entry_db_relock(
75 if ( !lock ) return 0;
77 lockobj.data = &ei->bei_id;
78 lockobj.size = sizeof(ei->bei_id) + 1;
80 list[0].op = DB_LOCK_PUT;
82 list[1].op = DB_LOCK_GET;
84 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
85 list[1].obj = &lockobj;
86 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
91 LDAP_LOG( CACHE, DETAIL1,
92 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
95 Debug( LDAP_DEBUG_TRACE,
96 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
100 *lock = list[1].lock;
107 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
108 int rw, int tryOnly, DB_LOCK *lock )
117 if ( !lock ) return 0;
120 db_rw = DB_LOCK_WRITE;
122 db_rw = DB_LOCK_READ;
124 lockobj.data = &ei->bei_id;
125 lockobj.size = sizeof(ei->bei_id) + 1;
127 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
128 &lockobj, db_rw, lock);
131 LDAP_LOG( CACHE, DETAIL1,
132 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
133 ei->bei_id, rw, rc );
135 Debug( LDAP_DEBUG_TRACE,
136 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
137 ei->bei_id, rw, rc );
141 #endif /* NO_THREADS */
145 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
152 if ( !lock ) return 0;
154 rc = LOCK_PUT ( env, lock );
160 bdb_cache_entryinfo_destroy( EntryInfo *e )
162 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
163 free( e->bei_nrdn.bv_val );
165 free( e->bei_rdn.bv_val );
171 #define LRU_DELETE( cache, ei ) do { \
172 if ( (ei)->bei_lruprev != NULL ) { \
173 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
175 (cache)->c_lruhead = (ei)->bei_lrunext; \
177 if ( (ei)->bei_lrunext != NULL ) { \
178 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
180 (cache)->c_lrutail = (ei)->bei_lruprev; \
182 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
185 #define LRU_ADD( cache, ei ) do { \
186 (ei)->bei_lrunext = (cache)->c_lruhead; \
187 if ( (ei)->bei_lrunext != NULL ) { \
188 (ei)->bei_lrunext->bei_lruprev = (ei); \
190 (cache)->c_lruhead = (ei); \
191 (ei)->bei_lruprev = NULL; \
192 if ( (cache)->c_lrutail == NULL ) { \
193 (cache)->c_lrutail = (ei); \
197 /* Do a length-ordered sort on normalized RDNs */
199 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
201 const EntryInfo *e1 = v_e1, *e2 = v_e2;
202 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
204 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
205 e1->bei_nrdn.bv_len );
211 bdb_id_cmp( const void *v_e1, const void *v_e2 )
213 const EntryInfo *e1 = v_e1, *e2 = v_e2;
214 return e1->bei_id - e2->bei_id;
217 /* Create an entryinfo in the cache. Caller must release the locks later.
220 bdb_entryinfo_add_internal(
221 struct bdb_info *bdb,
225 EntryInfo *ei2 = NULL;
229 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
231 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
232 bdb_cache_entryinfo_lock( ei->bei_parent );
234 ei2->bei_id = ei->bei_id;
235 ei2->bei_parent = ei->bei_parent;
237 ei2->bei_rdn = ei->bei_rdn;
240 /* Add to cache ID tree */
241 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
243 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
244 bdb_cache_entryinfo_destroy( ei2 );
247 /* It got freed above because its value was
250 ei->bei_rdn.bv_val = NULL;
253 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
254 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
257 ei->bei_parent->bei_ckids++;
265 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
266 * the info for its closest ancestor. *res should be NULL to process a
267 * complete DN starting from the tree root. Otherwise *res must be the
268 * immediate parent of the requested DN, and only the RDN will be searched.
269 * The EntryInfo is locked upon return and must be unlocked by the caller.
278 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
279 EntryInfo ei, *eip, *ei2;
283 /* this function is always called with normalized DN */
285 /* we're doing a onelevel search for an RDN */
286 ei.bei_nrdn.bv_val = ndn->bv_val;
287 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
290 /* we're searching a full DN from the root */
291 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
292 ei.bei_nrdn.bv_val = ptr;
293 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
294 /* Skip to next rdn if suffix is empty */
295 if ( ei.bei_nrdn.bv_len == 0 ) {
296 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
297 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
298 if ( ptr >= ndn->bv_val ) {
299 if (DN_SEPARATOR(*ptr)) ptr++;
300 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
301 ei.bei_nrdn.bv_val = ptr;
304 eip = &bdb->bi_cache.c_dntree;
307 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
309 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
311 int len = ei.bei_nrdn.bv_len;
313 ei.bei_nrdn.bv_len = ndn->bv_len -
314 (ei.bei_nrdn.bv_val - ndn->bv_val);
315 bdb_cache_entryinfo_unlock( eip );
317 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
319 bdb_cache_entryinfo_lock( eip );
324 /* DN exists but needs to be added to cache */
325 ei.bei_nrdn.bv_len = len;
326 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
327 /* add_internal left eip and c_rwlock locked */
328 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
333 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
334 /* In the midst of deleting? Give it a chance to
337 bdb_cache_entryinfo_unlock( eip );
338 ldap_pvt_thread_yield();
339 bdb_cache_entryinfo_lock( eip );
343 bdb_cache_entryinfo_unlock( eip );
344 bdb_cache_entryinfo_lock( ei2 );
348 /* Advance to next lower RDN */
349 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
350 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
351 if ( ptr >= ndn->bv_val ) {
352 if (DN_SEPARATOR(*ptr)) ptr++;
353 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
354 ei.bei_nrdn.bv_val = ptr;
356 if ( ptr < ndn->bv_val ) {
366 /* Walk up the tree from a child node, looking for an ID that's already
367 * been linked into the cache.
370 hdb_cache_find_parent(
376 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
377 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
378 char ndn[SLAP_LDAPDN_MAXLEN];
387 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
390 /* Save the previous node, if any */
393 /* Create a new node for the current ID */
394 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
395 ein->bei_id = ei.bei_id;
396 ein->bei_kids = ei.bei_kids;
397 ein->bei_nrdn = ei.bei_nrdn;
398 ein->bei_rdn = ei.bei_rdn;
400 /* This node is not fully connected yet */
401 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
403 /* Insert this node into the ID tree */
404 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
405 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
406 bdb_id_cmp, avl_dup_error ) ) {
408 /* Someone else created this node just before us.
409 * Free our new copy and use the existing one.
411 bdb_cache_entryinfo_destroy( ein );
412 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
413 (caddr_t) &ei, bdb_id_cmp );
415 /* Link in any kids we've already processed */
417 bdb_cache_entryinfo_lock( ein );
418 avl_insert( &ein->bei_kids, (caddr_t)ei2,
419 bdb_rdn_cmp, avl_dup_error );
420 bdb_cache_entryinfo_unlock( ein );
428 /* If this is the first time, save this node
429 * to be returned later.
431 if ( eir == NULL ) eir = ein;
433 /* If there was a previous node, link it to this one */
434 if ( ei2 ) ei2->bei_parent = ein;
436 /* Look for this node's parent */
438 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
439 (caddr_t) &eip, bdb_id_cmp );
441 ei2 = &bdb->bi_cache.c_dntree;
443 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
445 /* Got the parent, link in and we're done. */
447 bdb_cache_entryinfo_lock( ei2 );
448 ein->bei_parent = ei2;
449 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
451 bdb_cache_entryinfo_unlock( ei2 );
452 bdb_cache_entryinfo_lock( eir );
454 /* Reset all the state info */
455 for (ein = eir; ein != ei2; ein=ein->bei_parent)
456 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
461 ei.bei_id = eip.bei_id;
462 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
468 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
472 struct bdb_info *bdb,
479 /* See if we already have this one */
480 bdb_cache_entryinfo_lock( ei->bei_parent );
481 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
482 bdb_cache_entryinfo_unlock( ei->bei_parent );
485 /* Not found, add it */
488 /* bei_rdn was not malloc'd before, do it now */
489 ber_dupbv( &bv, &ei->bei_rdn );
492 rc = bdb_entryinfo_add_internal( bdb, ei, res );
493 bdb_cache_entryinfo_unlock( ei->bei_parent );
494 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
496 /* Found, return it */
504 /* caller must have lru_mutex locked. mutex
505 * will be unlocked on return.
509 struct bdb_info *bdb,
513 DB_LOCK lock, *lockp;
521 /* See if we're above the cache size limit */
522 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
523 EntryInfo *elru, *elprev;
526 /* Look for an unused entry to remove */
527 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
528 elprev = elru->bei_lruprev;
530 /* Too many probes, not enough idle, give up */
533 /* If we can successfully writelock it, then
534 * the object is idle.
536 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, elru, 1, 1,
538 /* If there's no entry, or this node is in
539 * the process of linking into the cache,
542 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
543 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
546 LRU_DELETE( &bdb->bi_cache, elru );
547 elru->bei_e->e_private = NULL;
548 bdb_entry_return( elru->bei_e );
550 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
551 --bdb->bi_cache.c_cursize;
552 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
557 LRU_ADD( &bdb->bi_cache, ei );
558 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
563 struct bdb_info *bdb,
570 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
571 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
572 (caddr_t) &ei, bdb_id_cmp );
573 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
578 * cache_find_id - find an entry in the cache, given id.
579 * The entry is locked for Read upon return. Call with islocked TRUE if
580 * the supplied *eip was already locked.
593 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
600 /* If we weren't given any info, see if we have it already cached */
602 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
603 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
604 (caddr_t) &ei, bdb_id_cmp );
606 /* If the lock attempt fails, the info is in use */
607 if ( ldap_pvt_thread_mutex_trylock(
608 &(*eip)->bei_kids_mutex )) {
609 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
610 /* If this node is being deleted, treat
611 * as if the delete has already finished
613 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
616 /* otherwise, wait for the info to free up */
617 ldap_pvt_thread_yield();
620 /* If this info isn't hooked up to its parent yet,
621 * unlock and wait for it to be fully initialized
623 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
624 bdb_cache_entryinfo_unlock( *eip );
625 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
626 ldap_pvt_thread_yield();
631 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
634 /* See if the ID exists in the database; add it to the cache if so */
637 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
639 rc = bdb_cache_find_ndn( op, tid,
641 if ( *eip ) islocked = 1;
643 bdb_entry_return( ep );
648 rc = hdb_cache_find_parent(op, tid, id, eip );
649 if ( rc == 0 && *eip ) islocked = 1;
653 /* Ok, we found the info, do we have the entry? */
654 if ( *eip && rc == 0 ) {
655 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
659 bdb_cache_entryinfo_unlock( *eip );
662 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
663 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
665 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
666 } else if ( rc == 0 ) {
667 if ( !(*eip)->bei_e ) {
668 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
671 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
674 /* Make sure no other modifier beat us to it */
675 if ( (*eip)->bei_e ) {
676 bdb_entry_return( ep );
679 /* Check for subtree renames */
680 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
681 if ( rc ) rc = bdb_fix_dn( (*eip)->bei_e, 2 );
684 ep->e_private = *eip;
692 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
696 /* Check for subtree renames
698 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
700 bdb_cache_entry_db_relock( bdb->bi_dbenv,
701 locker, *eip, 1, 0, lock );
702 /* check again in case other modifier did it already */
703 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
704 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
705 bdb_cache_entry_db_relock( bdb->bi_dbenv,
706 locker, *eip, 0, 0, lock );
715 bdb_cache_entryinfo_unlock( *eip );
718 bdb_entry_return( ep );
722 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
723 /* if entry is on LRU list, remove from old spot */
724 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
725 LRU_DELETE( &bdb->bi_cache, *eip );
727 /* if entry is new, bump cache size */
728 bdb->bi_cache.c_cursize++;
730 /* lru_mutex is unlocked for us */
731 bdb_cache_lru_add( bdb, locker, *eip );
745 if ( BEI(e)->bei_kids ) {
748 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
751 rc = bdb_dn2id_children( op, txn, e );
752 if ( rc == DB_NOTFOUND ) {
753 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
758 /* Update the cache after a successful database Add. */
761 struct bdb_info *bdb,
768 struct berval rdn = e->e_name;
777 /* Lock this entry so that bdb_add can run to completion.
778 * It can only fail if BDB has run out of lock resources.
780 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
782 bdb_cache_entryinfo_unlock( eip );
787 if ( nrdn->bv_len != e->e_nname.bv_len ) {
788 char *ptr = strchr( rdn.bv_val, ',' );
789 rdn.bv_len = ptr - rdn.bv_val;
791 ber_dupbv( &ei.bei_rdn, &rdn );
792 if ( eip->bei_dkids ) eip->bei_dkids++;
795 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
796 /* bdb_csn_commit can cause this when adding the database root entry */
798 new->bei_e->e_private = NULL;
799 bdb_entry_return( new->bei_e );
803 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
804 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
805 if (eip->bei_parent) {
806 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
810 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
811 ++bdb->bi_cache.c_cursize;
812 /* lru_mutex is unlocked for us */
813 bdb_cache_lru_add( bdb, locker, new );
815 bdb_cache_entryinfo_unlock( eip );
816 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
828 EntryInfo *ei = BEI(e);
830 /* Get write lock on data */
831 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
833 /* If we've done repeated mods on a cached entry, then e_attrs
834 * is no longer contiguous with the entry, and must be freed.
837 if ( (void *)e->e_attrs != (void *)(e+1) ) {
838 attrs_free( e->e_attrs );
840 e->e_attrs = newAttrs;
846 * Change the rdn in the entryinfo. Also move to a new parent if needed.
858 EntryInfo *ei = BEI(e), *pei;
862 /* Get write lock on data */
863 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
866 /* If we've done repeated mods on a cached entry, then e_attrs
867 * is no longer contiguous with the entry, and must be freed.
869 if ( (void *)e->e_attrs != (void *)(e+1) ) {
870 attrs_free( e->e_attrs );
872 e->e_attrs = new->e_attrs;
873 if( e->e_nname.bv_val < e->e_bv.bv_val ||
874 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
876 ch_free(e->e_name.bv_val);
877 ch_free(e->e_nname.bv_val);
879 e->e_name = new->e_name;
880 e->e_nname = new->e_nname;
882 /* Lock the parent's kids AVL tree */
883 pei = ei->bei_parent;
884 bdb_cache_entryinfo_lock( pei );
885 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
886 free( ei->bei_nrdn.bv_val );
887 ber_dupbv( &ei->bei_nrdn, nrdn );
889 free( ei->bei_rdn.bv_val );
892 if ( nrdn->bv_len != e->e_nname.bv_len ) {
893 char *ptr = strchr(rdn.bv_val, ',');
894 rdn.bv_len = ptr - rdn.bv_val;
896 ber_dupbv( &ei->bei_rdn, &rdn );
900 ein = ei->bei_parent;
902 ei->bei_parent = ein;
903 bdb_cache_entryinfo_unlock( pei );
904 bdb_cache_entryinfo_lock( ein );
908 int max = ei->bei_modrdns;
909 /* Record the generation number of this change */
910 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
911 if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
913 ei->bei_modrdns = max + 1;
916 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
917 bdb_cache_entryinfo_unlock( ein );
921 * cache_delete - delete the entry e from the cache.
923 * returns: 0 e was deleted ok
924 * 1 e was not in the cache
925 * -1 something bad happened
935 EntryInfo *ei = BEI(e);
938 assert( e->e_private );
940 /* Set this early, warn off any queriers */
941 ei->bei_state |= CACHE_ENTRY_DELETED;
943 /* Lock the entry's info */
944 bdb_cache_entryinfo_lock( ei );
946 /* Get write lock on the data */
947 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
949 /* couldn't lock, undo and give up */
950 ei->bei_state ^= CACHE_ENTRY_DELETED;
951 bdb_cache_entryinfo_unlock( ei );
955 /* set cache write lock */
956 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
958 /* Lock the parent's kids tree */
959 bdb_cache_entryinfo_lock( ei->bei_parent );
962 LDAP_LOG( CACHE, ENTRY,
963 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
965 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
970 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
971 rc = bdb_cache_delete_internal( cache, e->e_private );
973 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
975 /* free cache write lock */
976 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
977 bdb_cache_entryinfo_unlock( ei->bei_parent );
979 /* Leave entry info locked */
985 bdb_cache_delete_cleanup(
989 EntryInfo *ei = BEI(e);
993 bdb_entry_return( e );
995 free( ei->bei_nrdn.bv_val );
996 ei->bei_nrdn.bv_val = NULL;
998 free( ei->bei_rdn.bv_val );
999 ei->bei_rdn.bv_val = NULL;
1000 ei->bei_modrdns = 0;
1004 ei->bei_parent = NULL;
1005 ei->bei_kids = NULL;
1006 ei->bei_lruprev = NULL;
1008 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1009 ei->bei_lrunext = cache->c_eifree;
1010 cache->c_eifree = ei;
1011 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1012 bdb_cache_entryinfo_unlock( ei );
1016 bdb_cache_delete_internal(
1020 int rc = 0; /* return code */
1023 e->bei_parent->bei_ckids--;
1024 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1027 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1034 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1043 LRU_DELETE( cache, e );
1047 * flag entry to be freed later by a call to cache_return_entry()
1049 e->bei_state |= CACHE_ENTRY_DELETED;
1055 bdb_entryinfo_release( void *data )
1057 EntryInfo *ei = (EntryInfo *)data;
1058 if ( ei->bei_kids ) {
1059 avl_free( ei->bei_kids, NULL );
1062 ei->bei_e->e_private = NULL;
1063 bdb_entry_return( ei->bei_e );
1065 bdb_cache_entryinfo_destroy( ei );
1069 bdb_cache_release_all( Cache *cache )
1071 /* set cache write lock */
1072 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1074 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1077 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1079 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1082 avl_free( cache->c_dntree.bei_kids, NULL );
1083 avl_free( cache->c_idtree, bdb_entryinfo_release );
1084 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1085 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1086 bdb_cache_entryinfo_destroy(cache->c_eifree);
1088 cache->c_lruhead = NULL;
1089 cache->c_lrutail = NULL;
1091 /* free lru mutex */
1092 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1093 /* free cache write lock */
1094 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1099 bdb_lru_print( Cache *cache )
1103 fprintf( stderr, "LRU queue (head to tail):\n" );
1104 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1105 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1106 e->bei_nrdn.bv_val, e->bei_id );
1108 fprintf( stderr, "LRU queue (tail to head):\n" );
1109 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1110 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1111 e->bei_nrdn.bv_val, e->bei_id );
1116 #ifdef BDB_REUSE_LOCKERS
1118 bdb_locker_id_free( void *key, void *data )
1121 int lockid = (int) data;
1124 rc = XLOCK_ID_FREE( env, lockid );
1125 if ( rc == EINVAL ) {
1128 LDAP_LOG( BACK_BDB, ERR,
1129 "bdb_locker_id_free: %d err %s(%d)\n",
1130 lockid, db_strerror(rc), rc );
1132 Debug( LDAP_DEBUG_ANY,
1133 "bdb_locker_id_free: %d err %s(%d)\n",
1134 lockid, db_strerror(rc), rc );
1136 memset( &lr, 0, sizeof(lr) );
1138 /* release all locks held by this locker. */
1139 lr.op = DB_LOCK_PUT_ALL;
1140 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1141 XLOCK_ID_FREE( env, lockid );
1146 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1152 if ( !env || !locker ) return -1;
1154 /* If no op was provided, try to find the ctx anyway... */
1156 ctx = op->o_threadctx;
1158 ctx = ldap_pvt_thread_pool_context();
1161 /* Shouldn't happen unless we're single-threaded */
1167 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1168 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1169 rc = XLOCK_ID( env, &lockid );
1170 if (rc) ldap_pvt_thread_yield();
1175 data = (void *)lockid;
1176 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1177 data, bdb_locker_id_free ) ) ) {
1178 XLOCK_ID_FREE( env, lockid );
1180 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1181 db_strerror(rc), rc, 0 );
1183 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1184 db_strerror(rc), rc, 0 );
1198 bdb_cache_delete_entry(
1199 struct bdb_info *bdb,
1204 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1205 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1207 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1208 LRU_DELETE( &bdb->bi_cache, ei );
1209 ei->bei_e->e_private = NULL;
1210 bdb_entry_return( ei->bei_e );
1212 --bdb->bi_cache.c_cursize;
1214 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1216 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );