1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e);
22 static void bdb_lru_print(Cache *cache);
26 bdb_cache_entryinfo_new( Cache *cache )
30 if ( cache->c_eifree ) {
31 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
32 if ( cache->c_eifree ) {
34 cache->c_eifree = ei->bei_lrunext;
36 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
39 ei->bei_lrunext = NULL;
41 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
42 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
48 /* Atomically release and reacquire a lock */
50 bdb_cache_entry_db_relock(
65 if ( !lock ) return 0;
68 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
70 list[0].op = DB_LOCK_PUT;
72 list[1].op = DB_LOCK_GET;
74 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
75 list[1].obj = &lockobj;
76 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
81 LDAP_LOG( CACHE, DETAIL1,
82 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
85 Debug( LDAP_DEBUG_TRACE,
86 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
96 bdb_cache_entry_db_lock
97 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
106 if ( !lock ) return 0;
109 db_rw = DB_LOCK_WRITE;
111 db_rw = DB_LOCK_READ;
114 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
116 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
117 &lockobj, db_rw, lock);
120 LDAP_LOG( CACHE, DETAIL1,
121 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
122 ei->bei_id, rw, rc );
124 Debug( LDAP_DEBUG_TRACE,
125 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
126 ei->bei_id, rw, rc );
130 #endif /* NO_THREADS */
134 bdb_cache_entry_db_unlock
135 ( DB_ENV *env, DB_LOCK *lock )
142 rc = LOCK_PUT ( env, lock );
148 bdb_cache_entryinfo_destroy( EntryInfo *e )
150 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
151 free( e->bei_nrdn.bv_val );
153 free( e->bei_rdn.bv_val );
159 #define LRU_DELETE( cache, ei ) do { \
160 if ( (ei)->bei_lruprev != NULL ) { \
161 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
163 (cache)->c_lruhead = (ei)->bei_lrunext; \
165 if ( (ei)->bei_lrunext != NULL ) { \
166 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
168 (cache)->c_lrutail = (ei)->bei_lruprev; \
172 #define LRU_ADD( cache, ei ) do { \
173 (ei)->bei_lrunext = (cache)->c_lruhead; \
174 if ( (ei)->bei_lrunext != NULL ) { \
175 (ei)->bei_lrunext->bei_lruprev = (ei); \
177 (cache)->c_lruhead = (ei); \
178 (ei)->bei_lruprev = NULL; \
179 if ( (cache)->c_lrutail == NULL ) { \
180 (cache)->c_lrutail = (ei); \
184 /* Do a length-ordered sort on normalized RDNs */
186 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
188 const EntryInfo *e1 = v_e1, *e2 = v_e2;
189 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
190 if (rc == 0) rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
191 e1->bei_nrdn.bv_len );
196 bdb_id_cmp( const void *v_e1, const void *v_e2 )
198 const EntryInfo *e1 = v_e1, *e2 = v_e2;
199 return e1->bei_id - e2->bei_id;
202 /* Create an entryinfo in the cache. Caller must release the locks later.
205 bdb_entryinfo_add_internal(
206 struct bdb_info *bdb,
211 EntryInfo *ei2 = NULL;
215 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
217 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
218 bdb_cache_entryinfo_lock( ei->bei_parent );
220 ei2->bei_id = ei->bei_id;
221 ei2->bei_parent = ei->bei_parent;
223 ei2->bei_rdn = ei->bei_rdn;
226 /* Add to cache ID tree */
227 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
229 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
230 bdb_cache_entryinfo_destroy( ei2 );
233 /* It got freed above because its value was
236 ei->bei_rdn.bv_val = NULL;
239 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
240 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
243 ei->bei_parent->bei_ckids++;
251 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
252 * the info for its closest ancestor. *res should be NULL to process a
253 * complete DN starting from the tree root. Otherwise *res must be the
254 * immediate parent of the requested DN, and only the RDN will be searched.
255 * The EntryInfo is locked upon return and must be unlocked by the caller.
265 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
266 EntryInfo ei, *eip, *ei2;
270 /* this function is always called with normalized DN */
272 /* we're doing a onelevel search for an RDN */
273 ei.bei_nrdn.bv_val = ndn->bv_val;
274 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
277 /* we're searching a full DN from the root */
278 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
279 ei.bei_nrdn.bv_val = ptr;
280 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
281 eip = &bdb->bi_cache.c_dntree;
284 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
286 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
288 int len = ei.bei_nrdn.bv_len;
290 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
291 bdb_cache_entryinfo_unlock( eip );
293 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
295 bdb_cache_entryinfo_lock( eip );
300 /* DN exists but needs to be added to cache */
301 ei.bei_nrdn.bv_len = len;
302 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
303 /* add_internal left eip and c_rwlock locked */
304 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
309 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
310 /* In the midst of deleting? Give it a chance to
313 bdb_cache_entryinfo_unlock( eip );
314 ldap_pvt_thread_yield();
315 bdb_cache_entryinfo_lock( eip );
319 bdb_cache_entryinfo_unlock( eip );
320 bdb_cache_entryinfo_lock( ei2 );
324 /* Advance to next lower RDN */
325 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
326 && !DN_SEPARATOR(*ptr); ptr--);
327 if ( ptr >= ndn->bv_val ) {
328 if (DN_SEPARATOR(*ptr)) ptr++;
329 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
330 ei.bei_nrdn.bv_val = ptr;
332 if ( ptr < ndn->bv_val ) {
342 /* Walk up the tree from a child node, looking for an ID that's already
343 * been linked into the cache.
346 hdb_cache_find_parent(
353 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
354 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
355 char ndn[SLAP_LDAPDN_MAXLEN];
364 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
367 /* Save the previous node, if any */
370 /* Create a new node for the current ID */
371 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
372 ein->bei_id = ei.bei_id;
373 ein->bei_kids = ei.bei_kids;
374 ein->bei_nrdn = ei.bei_nrdn;
375 ein->bei_rdn = ei.bei_rdn;
377 /* This node is not fully connected yet */
378 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
380 /* Insert this node into the ID tree */
381 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
382 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
383 bdb_id_cmp, avl_dup_error ) ) {
385 /* Someone else created this node just before us.
386 * Free our new copy and use the existing one.
388 bdb_cache_entryinfo_destroy( ein );
389 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
390 (caddr_t) &ei, bdb_id_cmp );
392 /* Link in any kids we've already processed */
394 bdb_cache_entryinfo_lock( ein );
395 avl_insert( &ein->bei_kids, (caddr_t)ei2,
396 bdb_rdn_cmp, avl_dup_error );
397 bdb_cache_entryinfo_unlock( ein );
405 /* If this is the first time, save this node
406 * to be returned later.
408 if ( eir == NULL ) eir = ein;
410 /* If there was a previous node, link it to this one */
411 if ( ei2 ) ei2->bei_parent = ein;
413 /* Look for this node's parent */
415 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
416 (caddr_t) &eip, bdb_id_cmp );
418 ei2 = &bdb->bi_cache.c_dntree;
420 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
422 /* Got the parent, link in and we're done. */
424 bdb_cache_entryinfo_lock( ei2 );
425 ein->bei_parent = ei2;
426 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
428 bdb_cache_entryinfo_unlock( ei2 );
429 bdb_cache_entryinfo_lock( eir );
431 /* Reset all the state info */
432 for (ein = eir; ein != ei2; ein=ein->bei_parent)
433 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
438 ei.bei_id = eip.bei_id;
439 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
445 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
449 struct bdb_info *bdb,
456 /* See if we already have this one */
457 bdb_cache_entryinfo_lock( ei->bei_parent );
458 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
459 bdb_cache_entryinfo_unlock( ei->bei_parent );
462 /* Not found, add it */
465 /* bei_rdn was not malloc'd before, do it now */
466 ber_dupbv( &bv, &ei->bei_rdn );
469 rc = bdb_entryinfo_add_internal( bdb, ei, res );
470 bdb_cache_entryinfo_unlock( ei->bei_parent );
471 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
473 /* Found, return it */
481 /* caller must have lru_mutex locked. mutex
482 * will be unlocked on return.
486 struct bdb_info *bdb,
493 /* See if we're above the cache size limit */
494 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
495 EntryInfo *elru, *elprev;
498 /* Look for an unused entry to remove */
499 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
500 elprev = elru->bei_lruprev;
502 /* Too many probes, not enough idle, give up */
505 /* If we can successfully writelock it, then
506 * the object is idle.
508 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, elru, 1, 1,
510 /* If there's no entry, or this node is in
511 * the process of linking into the cache,
514 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
515 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
518 LRU_DELETE( &bdb->bi_cache, elru );
519 elru->bei_e->e_private = NULL;
520 bdb_entry_return( elru->bei_e );
522 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
523 --bdb->bi_cache.c_cursize;
524 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
529 LRU_ADD( &bdb->bi_cache, ei );
530 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
534 * cache_find_id - find an entry in the cache, given id.
535 * The entry is locked for Read upon return. Call with islocked TRUE if
536 * the supplied *eip was already locked.
550 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
558 /* If we weren't given any info, see if we have it already cached */
560 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
561 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
562 (caddr_t) &ei, bdb_id_cmp );
564 /* If the lock attempt fails, the info is in use */
565 if ( ldap_pvt_thread_mutex_trylock(
566 &(*eip)->bei_kids_mutex )) {
567 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
568 /* If this node is being deleted, treat
569 * as if the delete has already finished
571 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
574 /* otherwise, wait for the info to free up */
575 ldap_pvt_thread_yield();
578 /* If this info isn't hooked up to its parent yet,
579 * unlock and wait for it to be fully initialized
581 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
582 bdb_cache_entryinfo_unlock( *eip );
583 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
584 ldap_pvt_thread_yield();
589 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
592 /* See if the ID exists in the database; add it to the cache if so */
595 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
597 rc = bdb_cache_find_ndn( op, tid,
602 bdb_entry_return( ep );
607 rc = hdb_cache_find_parent(op, tid, id, eip );
608 if ( rc == 0 && *eip )
613 /* Ok, we found the info, do we have the entry? */
614 if ( *eip && rc == 0 ) {
615 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
618 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
620 if ( !(*eip)->bei_e ) {
622 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
625 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
627 /* Make sure no other modifier beat us to it */
628 if ( (*eip)->bei_e ) {
629 bdb_entry_return( ep );
632 ep->e_private = *eip;
638 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
642 /* If we had the entry already, this item
643 * is on the LRU list.
647 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
649 bdb_cache_entry_db_relock( bdb->bi_dbenv,
650 locker, *eip, 1, 0, lock );
651 /* check again in case other modifier did it already */
652 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
653 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
654 bdb_cache_entry_db_relock( bdb->bi_dbenv,
655 locker, *eip, 0, 0, lock );
663 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
664 /* if entry is old, remove from old spot on LRU list */
666 LRU_DELETE( &bdb->bi_cache, *eip );
668 /* if entry is new, bump cache size */
669 bdb->bi_cache.c_cursize++;
671 /* lru_mutex is unlocked for us */
672 bdb_cache_lru_add( bdb, locker, *eip );
676 bdb_cache_entryinfo_unlock( *eip );
690 if ( BEI(e)->bei_kids ) {
693 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
696 rc = bdb_dn2id_children( op, txn, e );
697 if ( rc == DB_NOTFOUND ) {
698 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
703 /* Update the cache after a successful database Add. */
706 struct bdb_info *bdb,
714 struct berval rdn = e->e_name;
721 if ( nrdn->bv_len != e->e_nname.bv_len ) {
722 char *ptr = strchr( rdn.bv_val, ',' );
723 rdn.bv_len = ptr - rdn.bv_val;
725 ber_dupbv( &ei.bei_rdn, &rdn );
726 if ( eip->bei_dkids ) eip->bei_dkids++;
728 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
731 new->bei_state = CACHE_ENTRY_NO_KIDS;
732 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
735 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
736 ++bdb->bi_cache.c_cursize;
737 /* lru_mutex is unlocked for us */
738 bdb_cache_lru_add( bdb, locker, new );
740 bdb_cache_entryinfo_unlock( eip );
741 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
754 EntryInfo *ei = BEI(e);
756 /* Get write lock on data */
757 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
759 /* If we've done repeated mods on a cached entry, then e_attrs
760 * is no longer contiguous with the entry, and must be freed.
762 if ( (void *)e->e_attrs != (void *)(e+1) ) {
763 attrs_free( e->e_attrs );
765 e->e_attrs = newAttrs;
771 * Change the rdn in the entryinfo. Also move to a new parent if needed.
784 EntryInfo *ei = BEI(e), *pei;
788 /* Get write lock on data */
789 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
791 /* If we've done repeated mods on a cached entry, then e_attrs
792 * is no longer contiguous with the entry, and must be freed.
794 if ( (void *)e->e_attrs != (void *)(e+1) ) {
795 attrs_free( e->e_attrs );
797 e->e_attrs = new->e_attrs;
798 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
799 e->e_bv.bv_val + e->e_bv.bv_len ) {
800 ch_free(e->e_name.bv_val);
801 ch_free(e->e_nname.bv_val);
803 e->e_name = new->e_name;
804 e->e_nname = new->e_nname;
806 /* Lock the parent's kids AVL tree */
807 pei = ei->bei_parent;
808 bdb_cache_entryinfo_lock( pei );
809 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
810 free( ei->bei_nrdn.bv_val );
811 ber_dupbv( &ei->bei_nrdn, nrdn );
813 free( ei->bei_rdn.bv_val );
816 if ( nrdn->bv_len != e->e_nname.bv_len ) {
817 char *ptr = strchr(rdn.bv_val, ',');
818 rdn.bv_len = ptr - rdn.bv_val;
820 ber_dupbv( &ei->bei_rdn, &rdn );
824 ein = ei->bei_parent;
826 ei->bei_parent = ein;
827 bdb_cache_entryinfo_unlock( pei );
828 bdb_cache_entryinfo_lock( ein );
831 { int max = ei->bei_modrdns;
832 /* Record the generation number of this change */
833 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
834 if ( pei->bei_modrdns > max )
835 max = pei->bei_modrdns;
837 ei->bei_modrdns = max + 1;
840 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
841 bdb_cache_entryinfo_unlock( ein );
845 * cache_delete - delete the entry e from the cache.
847 * returns: 0 e was deleted ok
848 * 1 e was not in the cache
849 * -1 something bad happened
860 EntryInfo *ei = BEI(e);
863 assert( e->e_private );
865 /* Set this early, warn off any queriers */
866 ei->bei_state |= CACHE_ENTRY_DELETED;
868 /* Lock the entry's info */
869 bdb_cache_entryinfo_lock( ei );
871 /* Get write lock on the data */
872 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
874 /* set cache write lock */
875 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
877 /* Lock the parent's kids tree */
878 bdb_cache_entryinfo_lock( ei->bei_parent );
881 LDAP_LOG( CACHE, ENTRY,
882 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
884 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
889 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
890 rc = bdb_cache_delete_internal( cache, e->e_private );
892 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
894 /* free cache write lock */
895 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
896 bdb_cache_entryinfo_unlock( ei->bei_parent );
898 /* Leave entry info locked */
904 bdb_cache_delete_cleanup(
909 EntryInfo *ei = BEI(e);
913 bdb_entry_return( e );
915 free( ei->bei_nrdn.bv_val );
916 ei->bei_nrdn.bv_val = NULL;
918 free( ei->bei_rdn.bv_val );
919 ei->bei_rdn.bv_val = NULL;
924 ei->bei_parent = NULL;
926 ei->bei_lruprev = NULL;
928 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
929 ei->bei_lrunext = cache->c_eifree;
930 cache->c_eifree = ei;
931 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
932 bdb_cache_entryinfo_unlock( ei );
936 bdb_cache_delete_internal(
941 int rc = 0; /* return code */
944 e->bei_parent->bei_ckids--;
945 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
948 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
954 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
964 LRU_DELETE( cache, e );
968 * flag entry to be freed later by a call to cache_return_entry()
970 e->bei_state |= CACHE_ENTRY_DELETED;
976 bdb_entryinfo_release( void *data )
978 EntryInfo *ei = (EntryInfo *)data;
979 if ( ei->bei_kids ) {
980 avl_free( ei->bei_kids, NULL );
983 ei->bei_e->e_private = NULL;
984 bdb_entry_return( ei->bei_e );
986 bdb_cache_entryinfo_destroy( ei );
990 bdb_cache_release_all( Cache *cache )
992 /* set cache write lock */
993 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
995 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
998 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1000 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1003 avl_free( cache->c_dntree.bei_kids, NULL );
1004 avl_free( cache->c_idtree, bdb_entryinfo_release );
1005 cache->c_lruhead = NULL;
1006 cache->c_lrutail = NULL;
1008 /* free lru mutex */
1009 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1010 /* free cache write lock */
1011 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1016 bdb_lru_print( Cache *cache )
1020 fprintf( stderr, "LRU queue (head to tail):\n" );
1021 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1022 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1023 e->bei_nrdn.bv_val, e->bei_id );
1025 fprintf( stderr, "LRU queue (tail to head):\n" );
1026 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1027 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1028 e->bei_nrdn.bv_val, e->bei_id );
1033 #ifdef BDB_REUSE_LOCKERS
1035 bdb_locker_id_free( void *key, void *data )
1038 int lockid = (int) data;
1042 rc = XLOCK_ID_FREE( env, lockid );
1043 if ( rc == EINVAL ) {
1046 LDAP_LOG( BACK_BDB, ERR,
1047 "bdb_locker_id_free: %d err %s(%d)\n",
1048 lockid, db_strerror(rc), rc );
1050 Debug( LDAP_DEBUG_ANY,
1051 "bdb_locker_id_free: %d err %s(%d)\n",
1052 lockid, db_strerror(rc), rc );
1054 memset( &lr, 0, sizeof(lr) );
1056 /* release all locks held by this locker. */
1057 lr.op = DB_LOCK_PUT_ALL;
1058 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1059 XLOCK_ID_FREE( env, lockid );
1064 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1070 if ( !env || !locker ) return -1;
1072 /* If no op was provided, try to find the ctx anyway... */
1074 ctx = op->o_threadctx;
1076 ctx = ldap_pvt_thread_pool_context();
1079 /* Shouldn't happen unless we're single-threaded */
1085 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1086 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1087 rc = XLOCK_ID( env, &lockid );
1088 if (rc) ldap_pvt_thread_yield();
1093 data = (void *)lockid;
1094 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1095 data, bdb_locker_id_free ) ) ) {
1096 XLOCK_ID_FREE( env, lockid );
1098 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1099 db_strerror(rc), rc, 0 );
1101 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1102 db_strerror(rc), rc, 0 );
1116 bdb_cache_delete_entry(
1117 struct bdb_info *bdb,
1122 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1123 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 ) {
1124 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1125 LRU_DELETE( &bdb->bi_cache, ei );
1126 ei->bei_e->e_private = NULL;
1127 bdb_entry_return( ei->bei_e );
1129 --bdb->bi_cache.c_cursize;
1131 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1133 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );