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;
42 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
43 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
49 /* Atomically release and reacquire a lock */
51 bdb_cache_entry_db_relock(
66 if ( !lock ) return 0;
69 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
71 list[0].op = DB_LOCK_PUT;
73 list[1].op = DB_LOCK_GET;
75 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
76 list[1].obj = &lockobj;
77 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
82 LDAP_LOG( CACHE, DETAIL1,
83 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
86 Debug( LDAP_DEBUG_TRACE,
87 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
97 bdb_cache_entry_db_lock
98 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
107 if ( !lock ) return 0;
110 db_rw = DB_LOCK_WRITE;
112 db_rw = DB_LOCK_READ;
115 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
117 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
118 &lockobj, db_rw, lock);
121 LDAP_LOG( CACHE, DETAIL1,
122 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
123 ei->bei_id, rw, rc );
125 Debug( LDAP_DEBUG_TRACE,
126 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
127 ei->bei_id, rw, rc );
131 #endif /* NO_THREADS */
135 bdb_cache_entry_db_unlock
136 ( DB_ENV *env, DB_LOCK *lock )
143 rc = LOCK_PUT ( env, lock );
149 bdb_cache_entryinfo_destroy( EntryInfo *e )
151 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
152 free( e->bei_nrdn.bv_val );
154 free( e->bei_rdn.bv_val );
160 #define LRU_DELETE( cache, ei ) do { \
161 if ( (ei)->bei_lruprev != NULL ) { \
162 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
164 (cache)->c_lruhead = (ei)->bei_lrunext; \
166 if ( (ei)->bei_lrunext != NULL ) { \
167 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
169 (cache)->c_lrutail = (ei)->bei_lruprev; \
173 #define LRU_ADD( cache, ei ) do { \
174 (ei)->bei_lrunext = (cache)->c_lruhead; \
175 if ( (ei)->bei_lrunext != NULL ) { \
176 (ei)->bei_lrunext->bei_lruprev = (ei); \
178 (cache)->c_lruhead = (ei); \
179 (ei)->bei_lruprev = NULL; \
180 if ( (cache)->c_lrutail == NULL ) { \
181 (cache)->c_lrutail = (ei); \
185 /* Do a length-ordered sort on normalized RDNs */
187 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
189 const EntryInfo *e1 = v_e1, *e2 = v_e2;
190 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
191 if (rc == 0) rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
192 e1->bei_nrdn.bv_len );
197 bdb_id_cmp( const void *v_e1, const void *v_e2 )
199 const EntryInfo *e1 = v_e1, *e2 = v_e2;
200 return e1->bei_id - e2->bei_id;
203 /* Create an entryinfo in the cache. Caller must release the locks later.
206 bdb_entryinfo_add_internal(
207 struct bdb_info *bdb,
212 EntryInfo *ei2 = NULL;
216 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
218 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
219 bdb_cache_entryinfo_lock( ei->bei_parent );
221 ei2->bei_id = ei->bei_id;
222 ei2->bei_parent = ei->bei_parent;
224 ei2->bei_rdn = ei->bei_rdn;
227 /* Add to cache ID tree */
228 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
230 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
231 bdb_cache_entryinfo_destroy( ei2 );
234 /* It got freed above because its value was
237 ei->bei_rdn.bv_val = NULL;
240 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
241 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
244 ei->bei_parent->bei_ckids++;
252 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
253 * the info for its closest ancestor. *res should be NULL to process a
254 * complete DN starting from the tree root. Otherwise *res must be the
255 * immediate parent of the requested DN, and only the RDN will be searched.
256 * The EntryInfo is locked upon return and must be unlocked by the caller.
266 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
267 EntryInfo ei, *eip, *ei2;
271 /* this function is always called with normalized DN */
273 /* we're doing a onelevel search for an RDN */
274 ei.bei_nrdn.bv_val = ndn->bv_val;
275 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
278 /* we're searching a full DN from the root */
279 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
280 ei.bei_nrdn.bv_val = ptr;
281 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
282 eip = &bdb->bi_cache.c_dntree;
285 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
287 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
289 int len = ei.bei_nrdn.bv_len;
291 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
292 bdb_cache_entryinfo_unlock( eip );
294 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
296 bdb_cache_entryinfo_lock( eip );
301 /* DN exists but needs to be added to cache */
302 ei.bei_nrdn.bv_len = len;
303 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
304 /* add_internal left eip and c_rwlock locked */
305 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
310 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
311 /* In the midst of deleting? Give it a chance to
314 bdb_cache_entryinfo_unlock( eip );
315 ldap_pvt_thread_yield();
316 bdb_cache_entryinfo_lock( eip );
320 bdb_cache_entryinfo_unlock( eip );
321 bdb_cache_entryinfo_lock( ei2 );
325 /* Advance to next lower RDN */
326 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
327 && !DN_SEPARATOR(*ptr); ptr--);
328 if ( ptr >= ndn->bv_val ) {
329 if (DN_SEPARATOR(*ptr)) ptr++;
330 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
331 ei.bei_nrdn.bv_val = ptr;
333 if ( ptr < ndn->bv_val ) {
343 /* Walk up the tree from a child node, looking for an ID that's already
344 * been linked into the cache.
347 hdb_cache_find_parent(
354 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
355 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
356 char ndn[SLAP_LDAPDN_MAXLEN];
365 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
368 /* Save the previous node, if any */
371 /* Create a new node for the current ID */
372 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
373 ein->bei_id = ei.bei_id;
374 ein->bei_kids = ei.bei_kids;
375 ein->bei_nrdn = ei.bei_nrdn;
376 ein->bei_rdn = ei.bei_rdn;
378 /* This node is not fully connected yet */
379 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
381 /* Insert this node into the ID tree */
382 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
383 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
384 bdb_id_cmp, avl_dup_error ) ) {
386 /* Someone else created this node just before us.
387 * Free our new copy and use the existing one.
389 bdb_cache_entryinfo_destroy( ein );
390 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
391 (caddr_t) &ei, bdb_id_cmp );
393 /* Link in any kids we've already processed */
395 bdb_cache_entryinfo_lock( ein );
396 avl_insert( &ein->bei_kids, (caddr_t)ei2,
397 bdb_rdn_cmp, avl_dup_error );
398 bdb_cache_entryinfo_unlock( ein );
406 /* If this is the first time, save this node
407 * to be returned later.
409 if ( eir == NULL ) eir = ein;
411 /* If there was a previous node, link it to this one */
412 if ( ei2 ) ei2->bei_parent = ein;
414 /* Look for this node's parent */
416 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
417 (caddr_t) &eip, bdb_id_cmp );
419 ei2 = &bdb->bi_cache.c_dntree;
421 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
423 /* Got the parent, link in and we're done. */
425 bdb_cache_entryinfo_lock( ei2 );
426 ein->bei_parent = ei2;
427 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
429 bdb_cache_entryinfo_unlock( ei2 );
430 bdb_cache_entryinfo_lock( eir );
432 /* Reset all the state info */
433 for (ein = eir; ein != ei2; ein=ein->bei_parent)
434 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
439 ei.bei_id = eip.bei_id;
440 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
446 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
450 struct bdb_info *bdb,
457 /* See if we already have this one */
458 bdb_cache_entryinfo_lock( ei->bei_parent );
459 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
460 bdb_cache_entryinfo_unlock( ei->bei_parent );
463 /* Not found, add it */
466 /* bei_rdn was not malloc'd before, do it now */
467 ber_dupbv( &bv, &ei->bei_rdn );
470 rc = bdb_entryinfo_add_internal( bdb, ei, res );
471 bdb_cache_entryinfo_unlock( ei->bei_parent );
472 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
474 /* Found, return it */
482 /* caller must have lru_mutex locked. mutex
483 * will be unlocked on return.
487 struct bdb_info *bdb,
494 /* See if we're above the cache size limit */
495 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
496 EntryInfo *elru, *elprev;
499 /* Look for an unused entry to remove */
500 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
501 elprev = elru->bei_lruprev;
503 /* Too many probes, not enough idle, give up */
506 /* If we can successfully writelock it, then
507 * the object is idle.
509 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, elru, 1, 1,
511 /* If there's no entry, or this node is in
512 * the process of linking into the cache,
515 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
516 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
519 LRU_DELETE( &bdb->bi_cache, elru );
520 elru->bei_e->e_private = NULL;
521 bdb_entry_return( elru->bei_e );
523 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
524 --bdb->bi_cache.c_cursize;
525 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
530 LRU_ADD( &bdb->bi_cache, ei );
531 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
535 * cache_find_id - find an entry in the cache, given id.
536 * The entry is locked for Read upon return. Call with islocked TRUE if
537 * the supplied *eip was already locked.
551 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
559 /* If we weren't given any info, see if we have it already cached */
561 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
562 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
563 (caddr_t) &ei, bdb_id_cmp );
565 /* If the lock attempt fails, the info is in use */
566 if ( ldap_pvt_thread_mutex_trylock(
567 &(*eip)->bei_kids_mutex )) {
568 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
569 /* If this node is being deleted, treat
570 * as if the delete has already finished
572 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
575 /* otherwise, wait for the info to free up */
576 ldap_pvt_thread_yield();
579 /* If this info isn't hooked up to its parent yet,
580 * unlock and wait for it to be fully initialized
582 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
583 bdb_cache_entryinfo_unlock( *eip );
584 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
585 ldap_pvt_thread_yield();
590 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
593 /* See if the ID exists in the database; add it to the cache if so */
596 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
598 rc = bdb_cache_find_ndn( op, tid,
603 bdb_entry_return( ep );
608 rc = hdb_cache_find_parent(op, tid, id, eip );
609 if ( rc == 0 && *eip )
614 /* Ok, we found the info, do we have the entry? */
615 if ( *eip && rc == 0 ) {
616 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
619 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
621 if ( !(*eip)->bei_e ) {
623 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
626 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
628 /* Make sure no other modifier beat us to it */
629 if ( (*eip)->bei_e ) {
630 bdb_entry_return( ep );
633 ep->e_private = *eip;
639 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
643 /* If we had the entry already, this item
644 * is on the LRU list.
648 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
650 bdb_cache_entry_db_relock( bdb->bi_dbenv,
651 locker, *eip, 1, 0, lock );
652 /* check again in case other modifier did it already */
653 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
654 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
655 bdb_cache_entry_db_relock( bdb->bi_dbenv,
656 locker, *eip, 0, 0, lock );
664 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
665 /* if entry is old, remove from old spot on LRU list */
667 LRU_DELETE( &bdb->bi_cache, *eip );
669 /* if entry is new, bump cache size */
670 bdb->bi_cache.c_cursize++;
672 /* lru_mutex is unlocked for us */
673 bdb_cache_lru_add( bdb, locker, *eip );
677 bdb_cache_entryinfo_unlock( *eip );
691 if ( BEI(e)->bei_kids ) {
694 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
697 rc = bdb_dn2id_children( op, txn, e );
698 if ( rc == DB_NOTFOUND ) {
699 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
704 /* Update the cache after a successful database Add. */
707 struct bdb_info *bdb,
715 struct berval rdn = e->e_name;
722 if ( nrdn->bv_len != e->e_nname.bv_len ) {
723 char *ptr = strchr( rdn.bv_val, ',' );
724 rdn.bv_len = ptr - rdn.bv_val;
726 ber_dupbv( &ei.bei_rdn, &rdn );
727 if ( eip->bei_dkids ) eip->bei_dkids++;
729 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
732 new->bei_state = CACHE_ENTRY_NO_KIDS;
733 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
736 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
737 ++bdb->bi_cache.c_cursize;
738 /* lru_mutex is unlocked for us */
739 bdb_cache_lru_add( bdb, locker, new );
741 bdb_cache_entryinfo_unlock( eip );
742 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
755 EntryInfo *ei = BEI(e);
757 /* Get write lock on data */
758 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
760 /* If we've done repeated mods on a cached entry, then e_attrs
761 * is no longer contiguous with the entry, and must be freed.
763 if ( (void *)e->e_attrs != (void *)(e+1) ) {
764 attrs_free( e->e_attrs );
766 e->e_attrs = newAttrs;
772 * Change the rdn in the entryinfo. Also move to a new parent if needed.
785 EntryInfo *ei = BEI(e), *pei;
789 /* Get write lock on data */
790 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
792 /* If we've done repeated mods on a cached entry, then e_attrs
793 * is no longer contiguous with the entry, and must be freed.
795 if ( (void *)e->e_attrs != (void *)(e+1) ) {
796 attrs_free( e->e_attrs );
798 e->e_attrs = new->e_attrs;
799 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
800 e->e_bv.bv_val + e->e_bv.bv_len ) {
801 ch_free(e->e_name.bv_val);
802 ch_free(e->e_nname.bv_val);
804 e->e_name = new->e_name;
805 e->e_nname = new->e_nname;
807 /* Lock the parent's kids AVL tree */
808 pei = ei->bei_parent;
809 bdb_cache_entryinfo_lock( pei );
810 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
811 free( ei->bei_nrdn.bv_val );
812 ber_dupbv( &ei->bei_nrdn, nrdn );
814 free( ei->bei_rdn.bv_val );
817 if ( nrdn->bv_len != e->e_nname.bv_len ) {
818 char *ptr = strchr(rdn.bv_val, ',');
819 rdn.bv_len = ptr - rdn.bv_val;
821 ber_dupbv( &ei->bei_rdn, &rdn );
825 ein = ei->bei_parent;
827 ei->bei_parent = ein;
828 bdb_cache_entryinfo_unlock( pei );
829 bdb_cache_entryinfo_lock( ein );
832 { int max = ei->bei_modrdns;
833 /* Record the generation number of this change */
834 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
835 if ( pei->bei_modrdns > max )
836 max = pei->bei_modrdns;
838 ei->bei_modrdns = max + 1;
841 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
842 bdb_cache_entryinfo_unlock( ein );
846 * cache_delete - delete the entry e from the cache.
848 * returns: 0 e was deleted ok
849 * 1 e was not in the cache
850 * -1 something bad happened
861 EntryInfo *ei = BEI(e);
864 assert( e->e_private );
866 /* Set this early, warn off any queriers */
867 ei->bei_state |= CACHE_ENTRY_DELETED;
869 /* Lock the entry's info */
870 bdb_cache_entryinfo_lock( ei );
872 /* Get write lock on the data */
873 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
875 /* set cache write lock */
876 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
878 /* Lock the parent's kids tree */
879 bdb_cache_entryinfo_lock( ei->bei_parent );
882 LDAP_LOG( CACHE, ENTRY,
883 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
885 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
890 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
891 rc = bdb_cache_delete_internal( cache, e->e_private );
893 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
895 /* free cache write lock */
896 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
897 bdb_cache_entryinfo_unlock( ei->bei_parent );
899 /* Leave entry info locked */
905 bdb_cache_delete_cleanup(
910 EntryInfo *ei = BEI(e);
914 bdb_entry_return( e );
916 free( ei->bei_nrdn.bv_val );
917 ei->bei_nrdn.bv_val = NULL;
919 free( ei->bei_rdn.bv_val );
920 ei->bei_rdn.bv_val = NULL;
925 ei->bei_parent = NULL;
927 ei->bei_lruprev = NULL;
929 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
930 ei->bei_lrunext = cache->c_eifree;
931 cache->c_eifree = ei;
932 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
933 bdb_cache_entryinfo_unlock( ei );
937 bdb_cache_delete_internal(
942 int rc = 0; /* return code */
945 e->bei_parent->bei_ckids--;
946 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
949 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
955 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
965 LRU_DELETE( cache, e );
969 * flag entry to be freed later by a call to cache_return_entry()
971 e->bei_state |= CACHE_ENTRY_DELETED;
977 bdb_entryinfo_release( void *data )
979 EntryInfo *ei = (EntryInfo *)data;
980 if ( ei->bei_kids ) {
981 avl_free( ei->bei_kids, NULL );
984 ei->bei_e->e_private = NULL;
985 bdb_entry_return( ei->bei_e );
987 bdb_cache_entryinfo_destroy( ei );
991 bdb_cache_release_all( Cache *cache )
993 /* set cache write lock */
994 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
996 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
999 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1001 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1004 avl_free( cache->c_dntree.bei_kids, NULL );
1005 avl_free( cache->c_idtree, bdb_entryinfo_release );
1006 cache->c_lruhead = NULL;
1007 cache->c_lrutail = NULL;
1009 /* free lru mutex */
1010 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1011 /* free cache write lock */
1012 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1017 bdb_lru_print( Cache *cache )
1021 fprintf( stderr, "LRU queue (head to tail):\n" );
1022 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1023 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1024 e->bei_nrdn.bv_val, e->bei_id );
1026 fprintf( stderr, "LRU queue (tail to head):\n" );
1027 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1028 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1029 e->bei_nrdn.bv_val, e->bei_id );
1034 #ifdef BDB_REUSE_LOCKERS
1036 bdb_locker_id_free( void *key, void *data )
1039 int lockid = (int) data;
1043 rc = XLOCK_ID_FREE( env, lockid );
1044 if ( rc == EINVAL ) {
1047 LDAP_LOG( BACK_BDB, ERR,
1048 "bdb_locker_id_free: %d err %s(%d)\n",
1049 lockid, db_strerror(rc), rc );
1051 Debug( LDAP_DEBUG_ANY,
1052 "bdb_locker_id_free: %d err %s(%d)\n",
1053 lockid, db_strerror(rc), rc );
1055 memset( &lr, 0, sizeof(lr) );
1057 /* release all locks held by this locker. */
1058 lr.op = DB_LOCK_PUT_ALL;
1059 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1060 XLOCK_ID_FREE( env, lockid );
1065 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1071 if ( !env || !locker ) return -1;
1073 /* If no op was provided, try to find the ctx anyway... */
1075 ctx = op->o_threadctx;
1077 ctx = ldap_pvt_thread_pool_context();
1080 /* Shouldn't happen unless we're single-threaded */
1086 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1087 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1088 rc = XLOCK_ID( env, &lockid );
1089 if (rc) ldap_pvt_thread_yield();
1094 data = (void *)lockid;
1095 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1096 data, bdb_locker_id_free ) ) ) {
1097 XLOCK_ID_FREE( env, lockid );
1099 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1100 db_strerror(rc), rc, 0 );
1102 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1103 db_strerror(rc), rc, 0 );
1117 bdb_cache_delete_entry(
1118 struct bdb_info *bdb,
1123 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1124 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 ) {
1125 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1126 LRU_DELETE( &bdb->bi_cache, ei );
1127 ei->bei_e->e_private = NULL;
1128 bdb_entry_return( ei->bei_e );
1130 --bdb->bi_cache.c_cursize;
1132 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1134 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );