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 );
536 struct bdb_info *bdb,
544 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
545 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
546 (caddr_t) &ei, bdb_id_cmp );
547 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
552 * cache_find_id - find an entry in the cache, given id.
553 * The entry is locked for Read upon return. Call with islocked TRUE if
554 * the supplied *eip was already locked.
568 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
576 /* If we weren't given any info, see if we have it already cached */
578 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
579 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
580 (caddr_t) &ei, bdb_id_cmp );
582 /* If the lock attempt fails, the info is in use */
583 if ( ldap_pvt_thread_mutex_trylock(
584 &(*eip)->bei_kids_mutex )) {
585 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
586 /* If this node is being deleted, treat
587 * as if the delete has already finished
589 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
592 /* otherwise, wait for the info to free up */
593 ldap_pvt_thread_yield();
596 /* If this info isn't hooked up to its parent yet,
597 * unlock and wait for it to be fully initialized
599 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
600 bdb_cache_entryinfo_unlock( *eip );
601 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
602 ldap_pvt_thread_yield();
607 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
610 /* See if the ID exists in the database; add it to the cache if so */
613 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
615 rc = bdb_cache_find_ndn( op, tid,
620 bdb_entry_return( ep );
625 rc = hdb_cache_find_parent(op, tid, id, eip );
626 if ( rc == 0 && *eip )
631 /* Ok, we found the info, do we have the entry? */
632 if ( *eip && rc == 0 ) {
633 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
636 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
638 if ( !(*eip)->bei_e ) {
640 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
643 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
645 /* Make sure no other modifier beat us to it */
646 if ( (*eip)->bei_e ) {
647 bdb_entry_return( ep );
650 ep->e_private = *eip;
656 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
660 /* If we had the entry already, this item
661 * is on the LRU list.
665 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
667 bdb_cache_entry_db_relock( bdb->bi_dbenv,
668 locker, *eip, 1, 0, lock );
669 /* check again in case other modifier did it already */
670 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
671 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
672 bdb_cache_entry_db_relock( bdb->bi_dbenv,
673 locker, *eip, 0, 0, lock );
681 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
682 /* if entry is old, remove from old spot on LRU list */
684 LRU_DELETE( &bdb->bi_cache, *eip );
686 /* if entry is new, bump cache size */
687 bdb->bi_cache.c_cursize++;
689 /* lru_mutex is unlocked for us */
690 bdb_cache_lru_add( bdb, locker, *eip );
694 bdb_cache_entryinfo_unlock( *eip );
708 if ( BEI(e)->bei_kids ) {
711 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
714 rc = bdb_dn2id_children( op, txn, e );
715 if ( rc == DB_NOTFOUND ) {
716 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
721 /* Update the cache after a successful database Add. */
724 struct bdb_info *bdb,
732 struct berval rdn = e->e_name;
739 if ( nrdn->bv_len != e->e_nname.bv_len ) {
740 char *ptr = strchr( rdn.bv_val, ',' );
741 rdn.bv_len = ptr - rdn.bv_val;
743 ber_dupbv( &ei.bei_rdn, &rdn );
744 if ( eip->bei_dkids ) eip->bei_dkids++;
746 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
747 /* bdb_csn_commit can cause this when adding the database root entry */
748 if ( new->bei_e ) bdb_entry_return( new->bei_e );
751 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
752 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
753 if (eip->bei_parent) eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
756 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
757 ++bdb->bi_cache.c_cursize;
758 /* lru_mutex is unlocked for us */
759 bdb_cache_lru_add( bdb, locker, new );
761 bdb_cache_entryinfo_unlock( eip );
762 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
775 EntryInfo *ei = BEI(e);
777 /* Get write lock on data */
778 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
780 /* If we've done repeated mods on a cached entry, then e_attrs
781 * is no longer contiguous with the entry, and must be freed.
783 if ( (void *)e->e_attrs != (void *)(e+1) ) {
784 attrs_free( e->e_attrs );
786 e->e_attrs = newAttrs;
792 * Change the rdn in the entryinfo. Also move to a new parent if needed.
805 EntryInfo *ei = BEI(e), *pei;
809 /* Get write lock on data */
810 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
812 /* If we've done repeated mods on a cached entry, then e_attrs
813 * is no longer contiguous with the entry, and must be freed.
815 if ( (void *)e->e_attrs != (void *)(e+1) ) {
816 attrs_free( e->e_attrs );
818 e->e_attrs = new->e_attrs;
819 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
820 e->e_bv.bv_val + e->e_bv.bv_len ) {
821 ch_free(e->e_name.bv_val);
822 ch_free(e->e_nname.bv_val);
824 e->e_name = new->e_name;
825 e->e_nname = new->e_nname;
827 /* Lock the parent's kids AVL tree */
828 pei = ei->bei_parent;
829 bdb_cache_entryinfo_lock( pei );
830 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
831 free( ei->bei_nrdn.bv_val );
832 ber_dupbv( &ei->bei_nrdn, nrdn );
834 free( ei->bei_rdn.bv_val );
837 if ( nrdn->bv_len != e->e_nname.bv_len ) {
838 char *ptr = strchr(rdn.bv_val, ',');
839 rdn.bv_len = ptr - rdn.bv_val;
841 ber_dupbv( &ei->bei_rdn, &rdn );
845 ein = ei->bei_parent;
847 ei->bei_parent = ein;
848 bdb_cache_entryinfo_unlock( pei );
849 bdb_cache_entryinfo_lock( ein );
852 { int max = ei->bei_modrdns;
853 /* Record the generation number of this change */
854 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
855 if ( pei->bei_modrdns > max )
856 max = pei->bei_modrdns;
858 ei->bei_modrdns = max + 1;
861 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
862 bdb_cache_entryinfo_unlock( ein );
866 * cache_delete - delete the entry e from the cache.
868 * returns: 0 e was deleted ok
869 * 1 e was not in the cache
870 * -1 something bad happened
881 EntryInfo *ei = BEI(e);
884 assert( e->e_private );
886 /* Set this early, warn off any queriers */
887 ei->bei_state |= CACHE_ENTRY_DELETED;
889 /* Lock the entry's info */
890 bdb_cache_entryinfo_lock( ei );
892 /* Get write lock on the data */
893 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
895 /* set cache write lock */
896 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
898 /* Lock the parent's kids tree */
899 bdb_cache_entryinfo_lock( ei->bei_parent );
902 LDAP_LOG( CACHE, ENTRY,
903 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
905 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
910 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
911 rc = bdb_cache_delete_internal( cache, e->e_private );
913 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
915 /* free cache write lock */
916 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
917 bdb_cache_entryinfo_unlock( ei->bei_parent );
919 /* Leave entry info locked */
925 bdb_cache_delete_cleanup(
930 EntryInfo *ei = BEI(e);
934 bdb_entry_return( e );
936 free( ei->bei_nrdn.bv_val );
937 ei->bei_nrdn.bv_val = NULL;
939 free( ei->bei_rdn.bv_val );
940 ei->bei_rdn.bv_val = NULL;
945 ei->bei_parent = NULL;
947 ei->bei_lruprev = NULL;
949 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
950 ei->bei_lrunext = cache->c_eifree;
951 cache->c_eifree = ei;
952 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
953 bdb_cache_entryinfo_unlock( ei );
957 bdb_cache_delete_internal(
962 int rc = 0; /* return code */
965 e->bei_parent->bei_ckids--;
966 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
969 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
975 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
985 LRU_DELETE( cache, e );
989 * flag entry to be freed later by a call to cache_return_entry()
991 e->bei_state |= CACHE_ENTRY_DELETED;
997 bdb_entryinfo_release( void *data )
999 EntryInfo *ei = (EntryInfo *)data;
1000 if ( ei->bei_kids ) {
1001 avl_free( ei->bei_kids, NULL );
1004 ei->bei_e->e_private = NULL;
1005 bdb_entry_return( ei->bei_e );
1007 bdb_cache_entryinfo_destroy( ei );
1011 bdb_cache_release_all( Cache *cache )
1013 /* set cache write lock */
1014 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1016 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1019 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1021 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1024 avl_free( cache->c_dntree.bei_kids, NULL );
1025 avl_free( cache->c_idtree, bdb_entryinfo_release );
1026 cache->c_lruhead = NULL;
1027 cache->c_lrutail = NULL;
1029 /* free lru mutex */
1030 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1031 /* free cache write lock */
1032 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1037 bdb_lru_print( Cache *cache )
1041 fprintf( stderr, "LRU queue (head to tail):\n" );
1042 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1043 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1044 e->bei_nrdn.bv_val, e->bei_id );
1046 fprintf( stderr, "LRU queue (tail to head):\n" );
1047 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1048 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1049 e->bei_nrdn.bv_val, e->bei_id );
1054 #ifdef BDB_REUSE_LOCKERS
1056 bdb_locker_id_free( void *key, void *data )
1059 int lockid = (int) data;
1063 rc = XLOCK_ID_FREE( env, lockid );
1064 if ( rc == EINVAL ) {
1067 LDAP_LOG( BACK_BDB, ERR,
1068 "bdb_locker_id_free: %d err %s(%d)\n",
1069 lockid, db_strerror(rc), rc );
1071 Debug( LDAP_DEBUG_ANY,
1072 "bdb_locker_id_free: %d err %s(%d)\n",
1073 lockid, db_strerror(rc), rc );
1075 memset( &lr, 0, sizeof(lr) );
1077 /* release all locks held by this locker. */
1078 lr.op = DB_LOCK_PUT_ALL;
1079 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1080 XLOCK_ID_FREE( env, lockid );
1085 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1091 if ( !env || !locker ) return -1;
1093 /* If no op was provided, try to find the ctx anyway... */
1095 ctx = op->o_threadctx;
1097 ctx = ldap_pvt_thread_pool_context();
1100 /* Shouldn't happen unless we're single-threaded */
1106 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1107 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1108 rc = XLOCK_ID( env, &lockid );
1109 if (rc) ldap_pvt_thread_yield();
1114 data = (void *)lockid;
1115 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1116 data, bdb_locker_id_free ) ) ) {
1117 XLOCK_ID_FREE( env, lockid );
1119 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1120 db_strerror(rc), rc, 0 );
1122 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1123 db_strerror(rc), rc, 0 );
1137 bdb_cache_delete_entry(
1138 struct bdb_info *bdb,
1143 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1144 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 ) {
1145 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1146 LRU_DELETE( &bdb->bi_cache, ei );
1147 ei->bei_e->e_private = NULL;
1148 bdb_entry_return( ei->bei_e );
1150 --bdb->bi_cache.c_cursize;
1152 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1154 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );