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 if ( !lock ) return 0;
145 rc = LOCK_PUT ( env, lock );
151 bdb_cache_entryinfo_destroy( EntryInfo *e )
153 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
154 free( e->bei_nrdn.bv_val );
156 free( e->bei_rdn.bv_val );
162 #define LRU_DELETE( cache, ei ) do { \
163 if ( (ei)->bei_lruprev != NULL ) { \
164 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
166 (cache)->c_lruhead = (ei)->bei_lrunext; \
168 if ( (ei)->bei_lrunext != NULL ) { \
169 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
171 (cache)->c_lrutail = (ei)->bei_lruprev; \
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;
193 if (rc == 0) rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
194 e1->bei_nrdn.bv_len );
199 bdb_id_cmp( const void *v_e1, const void *v_e2 )
201 const EntryInfo *e1 = v_e1, *e2 = v_e2;
202 return e1->bei_id - e2->bei_id;
205 /* Create an entryinfo in the cache. Caller must release the locks later.
208 bdb_entryinfo_add_internal(
209 struct bdb_info *bdb,
214 EntryInfo *ei2 = NULL;
218 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
220 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
221 bdb_cache_entryinfo_lock( ei->bei_parent );
223 ei2->bei_id = ei->bei_id;
224 ei2->bei_parent = ei->bei_parent;
226 ei2->bei_rdn = ei->bei_rdn;
229 /* Add to cache ID tree */
230 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
232 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
233 bdb_cache_entryinfo_destroy( ei2 );
236 /* It got freed above because its value was
239 ei->bei_rdn.bv_val = NULL;
242 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
243 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
246 ei->bei_parent->bei_ckids++;
254 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
255 * the info for its closest ancestor. *res should be NULL to process a
256 * complete DN starting from the tree root. Otherwise *res must be the
257 * immediate parent of the requested DN, and only the RDN will be searched.
258 * 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 eip = &bdb->bi_cache.c_dntree;
287 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
289 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
291 int len = ei.bei_nrdn.bv_len;
293 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
294 bdb_cache_entryinfo_unlock( eip );
296 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
298 bdb_cache_entryinfo_lock( eip );
303 /* DN exists but needs to be added to cache */
304 ei.bei_nrdn.bv_len = len;
305 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
306 /* add_internal left eip and c_rwlock locked */
307 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
312 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
313 /* In the midst of deleting? Give it a chance to
316 bdb_cache_entryinfo_unlock( eip );
317 ldap_pvt_thread_yield();
318 bdb_cache_entryinfo_lock( eip );
322 bdb_cache_entryinfo_unlock( eip );
323 bdb_cache_entryinfo_lock( ei2 );
327 /* Advance to next lower RDN */
328 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
329 && !DN_SEPARATOR(*ptr); ptr--);
330 if ( ptr >= ndn->bv_val ) {
331 if (DN_SEPARATOR(*ptr)) ptr++;
332 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
333 ei.bei_nrdn.bv_val = ptr;
335 if ( ptr < ndn->bv_val ) {
345 /* Walk up the tree from a child node, looking for an ID that's already
346 * been linked into the cache.
349 hdb_cache_find_parent(
356 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
357 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
358 char ndn[SLAP_LDAPDN_MAXLEN];
367 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
370 /* Save the previous node, if any */
373 /* Create a new node for the current ID */
374 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
375 ein->bei_id = ei.bei_id;
376 ein->bei_kids = ei.bei_kids;
377 ein->bei_nrdn = ei.bei_nrdn;
378 ein->bei_rdn = ei.bei_rdn;
380 /* This node is not fully connected yet */
381 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
383 /* Insert this node into the ID tree */
384 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
385 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
386 bdb_id_cmp, avl_dup_error ) ) {
388 /* Someone else created this node just before us.
389 * Free our new copy and use the existing one.
391 bdb_cache_entryinfo_destroy( ein );
392 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
393 (caddr_t) &ei, bdb_id_cmp );
395 /* Link in any kids we've already processed */
397 bdb_cache_entryinfo_lock( ein );
398 avl_insert( &ein->bei_kids, (caddr_t)ei2,
399 bdb_rdn_cmp, avl_dup_error );
400 bdb_cache_entryinfo_unlock( ein );
408 /* If this is the first time, save this node
409 * to be returned later.
411 if ( eir == NULL ) eir = ein;
413 /* If there was a previous node, link it to this one */
414 if ( ei2 ) ei2->bei_parent = ein;
416 /* Look for this node's parent */
418 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
419 (caddr_t) &eip, bdb_id_cmp );
421 ei2 = &bdb->bi_cache.c_dntree;
423 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
425 /* Got the parent, link in and we're done. */
427 bdb_cache_entryinfo_lock( ei2 );
428 ein->bei_parent = ei2;
429 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
431 bdb_cache_entryinfo_unlock( ei2 );
432 bdb_cache_entryinfo_lock( eir );
434 /* Reset all the state info */
435 for (ein = eir; ein != ei2; ein=ein->bei_parent)
436 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
441 ei.bei_id = eip.bei_id;
442 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
448 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
452 struct bdb_info *bdb,
459 /* See if we already have this one */
460 bdb_cache_entryinfo_lock( ei->bei_parent );
461 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
462 bdb_cache_entryinfo_unlock( ei->bei_parent );
465 /* Not found, add it */
468 /* bei_rdn was not malloc'd before, do it now */
469 ber_dupbv( &bv, &ei->bei_rdn );
472 rc = bdb_entryinfo_add_internal( bdb, ei, res );
473 bdb_cache_entryinfo_unlock( ei->bei_parent );
474 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
476 /* Found, return it */
484 /* caller must have lru_mutex locked. mutex
485 * will be unlocked on return.
489 struct bdb_info *bdb,
494 DB_LOCK lock, *lockp;
502 /* See if we're above the cache size limit */
503 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
504 EntryInfo *elru, *elprev;
507 /* Look for an unused entry to remove */
508 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
509 elprev = elru->bei_lruprev;
511 /* Too many probes, not enough idle, give up */
514 /* If we can successfully writelock it, then
515 * the object is idle.
517 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, elru, 1, 1,
519 /* If there's no entry, or this node is in
520 * the process of linking into the cache,
523 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
524 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
527 LRU_DELETE( &bdb->bi_cache, elru );
528 elru->bei_e->e_private = NULL;
529 bdb_entry_return( elru->bei_e );
531 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
532 --bdb->bi_cache.c_cursize;
533 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
538 LRU_ADD( &bdb->bi_cache, ei );
539 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
544 struct bdb_info *bdb,
552 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
553 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
554 (caddr_t) &ei, bdb_id_cmp );
555 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
560 * cache_find_id - find an entry in the cache, given id.
561 * The entry is locked for Read upon return. Call with islocked TRUE if
562 * the supplied *eip was already locked.
576 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
584 /* If we weren't given any info, see if we have it already cached */
586 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
587 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
588 (caddr_t) &ei, bdb_id_cmp );
590 /* If the lock attempt fails, the info is in use */
591 if ( ldap_pvt_thread_mutex_trylock(
592 &(*eip)->bei_kids_mutex )) {
593 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
594 /* If this node is being deleted, treat
595 * as if the delete has already finished
597 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
600 /* otherwise, wait for the info to free up */
601 ldap_pvt_thread_yield();
604 /* If this info isn't hooked up to its parent yet,
605 * unlock and wait for it to be fully initialized
607 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
608 bdb_cache_entryinfo_unlock( *eip );
609 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
610 ldap_pvt_thread_yield();
615 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
618 /* See if the ID exists in the database; add it to the cache if so */
621 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
623 rc = bdb_cache_find_ndn( op, tid,
628 bdb_entry_return( ep );
633 rc = hdb_cache_find_parent(op, tid, id, eip );
634 if ( rc == 0 && *eip )
639 /* Ok, we found the info, do we have the entry? */
640 if ( *eip && rc == 0 ) {
641 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
644 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
646 if ( !(*eip)->bei_e ) {
648 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
651 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
653 /* Make sure no other modifier beat us to it */
654 if ( (*eip)->bei_e ) {
655 bdb_entry_return( ep );
658 ep->e_private = *eip;
664 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
668 /* If we had the entry already, this item
669 * is on the LRU list.
673 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
675 bdb_cache_entry_db_relock( bdb->bi_dbenv,
676 locker, *eip, 1, 0, lock );
677 /* check again in case other modifier did it already */
678 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
679 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
680 bdb_cache_entry_db_relock( bdb->bi_dbenv,
681 locker, *eip, 0, 0, lock );
689 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
690 /* if entry is old, remove from old spot on LRU list */
692 LRU_DELETE( &bdb->bi_cache, *eip );
694 /* if entry is new, bump cache size */
695 bdb->bi_cache.c_cursize++;
697 /* lru_mutex is unlocked for us */
698 bdb_cache_lru_add( bdb, locker, *eip );
702 bdb_cache_entryinfo_unlock( *eip );
716 if ( BEI(e)->bei_kids ) {
719 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
722 rc = bdb_dn2id_children( op, txn, e );
723 if ( rc == DB_NOTFOUND ) {
724 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
729 /* Update the cache after a successful database Add. */
732 struct bdb_info *bdb,
740 struct berval rdn = e->e_name;
747 if ( nrdn->bv_len != e->e_nname.bv_len ) {
748 char *ptr = strchr( rdn.bv_val, ',' );
749 rdn.bv_len = ptr - rdn.bv_val;
751 ber_dupbv( &ei.bei_rdn, &rdn );
752 if ( eip->bei_dkids ) eip->bei_dkids++;
754 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
755 /* bdb_csn_commit can cause this when adding the database root entry */
756 if ( new->bei_e ) bdb_entry_return( new->bei_e );
759 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
760 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
761 if (eip->bei_parent) eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
764 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
765 ++bdb->bi_cache.c_cursize;
766 /* lru_mutex is unlocked for us */
767 bdb_cache_lru_add( bdb, locker, new );
769 bdb_cache_entryinfo_unlock( eip );
770 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
783 EntryInfo *ei = BEI(e);
785 /* Get write lock on data */
786 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
788 /* If we've done repeated mods on a cached entry, then e_attrs
789 * is no longer contiguous with the entry, and must be freed.
791 if ( (void *)e->e_attrs != (void *)(e+1) ) {
792 attrs_free( e->e_attrs );
794 e->e_attrs = newAttrs;
800 * Change the rdn in the entryinfo. Also move to a new parent if needed.
813 EntryInfo *ei = BEI(e), *pei;
817 /* Get write lock on data */
818 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
820 /* If we've done repeated mods on a cached entry, then e_attrs
821 * is no longer contiguous with the entry, and must be freed.
823 if ( (void *)e->e_attrs != (void *)(e+1) ) {
824 attrs_free( e->e_attrs );
826 e->e_attrs = new->e_attrs;
827 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
828 e->e_bv.bv_val + e->e_bv.bv_len ) {
829 ch_free(e->e_name.bv_val);
830 ch_free(e->e_nname.bv_val);
832 e->e_name = new->e_name;
833 e->e_nname = new->e_nname;
835 /* Lock the parent's kids AVL tree */
836 pei = ei->bei_parent;
837 bdb_cache_entryinfo_lock( pei );
838 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
839 free( ei->bei_nrdn.bv_val );
840 ber_dupbv( &ei->bei_nrdn, nrdn );
842 free( ei->bei_rdn.bv_val );
845 if ( nrdn->bv_len != e->e_nname.bv_len ) {
846 char *ptr = strchr(rdn.bv_val, ',');
847 rdn.bv_len = ptr - rdn.bv_val;
849 ber_dupbv( &ei->bei_rdn, &rdn );
853 ein = ei->bei_parent;
855 ei->bei_parent = ein;
856 bdb_cache_entryinfo_unlock( pei );
857 bdb_cache_entryinfo_lock( ein );
860 { int max = ei->bei_modrdns;
861 /* Record the generation number of this change */
862 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
863 if ( pei->bei_modrdns > max )
864 max = pei->bei_modrdns;
866 ei->bei_modrdns = max + 1;
869 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
870 bdb_cache_entryinfo_unlock( ein );
874 * cache_delete - delete the entry e from the cache.
876 * returns: 0 e was deleted ok
877 * 1 e was not in the cache
878 * -1 something bad happened
889 EntryInfo *ei = BEI(e);
892 assert( e->e_private );
894 /* Set this early, warn off any queriers */
895 ei->bei_state |= CACHE_ENTRY_DELETED;
897 /* Lock the entry's info */
898 bdb_cache_entryinfo_lock( ei );
900 /* Get write lock on the data */
901 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
903 /* set cache write lock */
904 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
906 /* Lock the parent's kids tree */
907 bdb_cache_entryinfo_lock( ei->bei_parent );
910 LDAP_LOG( CACHE, ENTRY,
911 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
913 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
918 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
919 rc = bdb_cache_delete_internal( cache, e->e_private );
921 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
923 /* free cache write lock */
924 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
925 bdb_cache_entryinfo_unlock( ei->bei_parent );
927 /* Leave entry info locked */
933 bdb_cache_delete_cleanup(
938 EntryInfo *ei = BEI(e);
942 bdb_entry_return( e );
944 free( ei->bei_nrdn.bv_val );
945 ei->bei_nrdn.bv_val = NULL;
947 free( ei->bei_rdn.bv_val );
948 ei->bei_rdn.bv_val = NULL;
953 ei->bei_parent = NULL;
955 ei->bei_lruprev = NULL;
957 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
958 ei->bei_lrunext = cache->c_eifree;
959 cache->c_eifree = ei;
960 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
961 bdb_cache_entryinfo_unlock( ei );
965 bdb_cache_delete_internal(
970 int rc = 0; /* return code */
973 e->bei_parent->bei_ckids--;
974 if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
977 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
983 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
993 LRU_DELETE( cache, e );
997 * flag entry to be freed later by a call to cache_return_entry()
999 e->bei_state |= CACHE_ENTRY_DELETED;
1005 bdb_entryinfo_release( void *data )
1007 EntryInfo *ei = (EntryInfo *)data;
1008 if ( ei->bei_kids ) {
1009 avl_free( ei->bei_kids, NULL );
1012 ei->bei_e->e_private = NULL;
1013 bdb_entry_return( ei->bei_e );
1015 bdb_cache_entryinfo_destroy( ei );
1019 bdb_cache_release_all( Cache *cache )
1021 /* set cache write lock */
1022 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1024 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1027 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1029 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1032 avl_free( cache->c_dntree.bei_kids, NULL );
1033 avl_free( cache->c_idtree, bdb_entryinfo_release );
1034 cache->c_lruhead = NULL;
1035 cache->c_lrutail = NULL;
1037 /* free lru mutex */
1038 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1039 /* free cache write lock */
1040 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1045 bdb_lru_print( Cache *cache )
1049 fprintf( stderr, "LRU queue (head to tail):\n" );
1050 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1051 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1052 e->bei_nrdn.bv_val, e->bei_id );
1054 fprintf( stderr, "LRU queue (tail to head):\n" );
1055 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1056 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1057 e->bei_nrdn.bv_val, e->bei_id );
1062 #ifdef BDB_REUSE_LOCKERS
1064 bdb_locker_id_free( void *key, void *data )
1067 int lockid = (int) data;
1071 rc = XLOCK_ID_FREE( env, lockid );
1072 if ( rc == EINVAL ) {
1075 LDAP_LOG( BACK_BDB, ERR,
1076 "bdb_locker_id_free: %d err %s(%d)\n",
1077 lockid, db_strerror(rc), rc );
1079 Debug( LDAP_DEBUG_ANY,
1080 "bdb_locker_id_free: %d err %s(%d)\n",
1081 lockid, db_strerror(rc), rc );
1083 memset( &lr, 0, sizeof(lr) );
1085 /* release all locks held by this locker. */
1086 lr.op = DB_LOCK_PUT_ALL;
1087 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1088 XLOCK_ID_FREE( env, lockid );
1093 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1099 if ( !env || !locker ) return -1;
1101 /* If no op was provided, try to find the ctx anyway... */
1103 ctx = op->o_threadctx;
1105 ctx = ldap_pvt_thread_pool_context();
1108 /* Shouldn't happen unless we're single-threaded */
1114 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1115 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1116 rc = XLOCK_ID( env, &lockid );
1117 if (rc) ldap_pvt_thread_yield();
1122 data = (void *)lockid;
1123 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1124 data, bdb_locker_id_free ) ) ) {
1125 XLOCK_ID_FREE( env, lockid );
1127 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1128 db_strerror(rc), rc, 0 );
1130 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1131 db_strerror(rc), rc, 0 );
1145 bdb_cache_delete_entry(
1146 struct bdb_info *bdb,
1151 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1152 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 ) {
1153 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1154 LRU_DELETE( &bdb->bi_cache, ei );
1155 ei->bei_e->e_private = NULL;
1156 bdb_entry_return( ei->bei_e );
1158 --bdb->bi_cache.c_cursize;
1160 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1162 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );