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_entry_internal(Cache *cache, EntryInfo *e);
22 static void bdb_lru_print(Cache *cache);
26 bdb_cache_entryinfo_new( )
30 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
31 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
36 /* Atomically release and reacquire a lock */
38 bdb_cache_entry_db_relock(
54 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
56 list[0].op = DB_LOCK_PUT;
58 list[1].op = DB_LOCK_GET;
60 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
61 list[1].obj = &lockobj;
62 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
67 LDAP_LOG( CACHE, DETAIL1,
68 "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
71 Debug( LDAP_DEBUG_TRACE,
72 "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
82 bdb_cache_entry_db_lock
83 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
93 db_rw = DB_LOCK_WRITE;
98 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
100 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
101 &lockobj, db_rw, lock);
104 LDAP_LOG( CACHE, DETAIL1,
105 "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
106 ei->bei_id, rw, rc );
108 Debug( LDAP_DEBUG_TRACE,
109 "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
110 ei->bei_id, rw, rc );
114 #endif /* NO_THREADS */
118 bdb_cache_entry_db_unlock
119 ( DB_ENV *env, DB_LOCK *lock )
126 rc = LOCK_PUT ( env, lock );
132 bdb_cache_entryinfo_destroy( EntryInfo *e )
134 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
135 free( e->bei_nrdn.bv_val );
140 #define LRU_DELETE( cache, ei ) do { \
141 if ( (ei)->bei_lruprev != NULL ) { \
142 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
144 (cache)->c_lruhead = (ei)->bei_lrunext; \
146 if ( (ei)->bei_lrunext != NULL ) { \
147 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
149 (cache)->c_lrutail = (ei)->bei_lruprev; \
153 #define LRU_ADD( cache, ei ) do { \
154 (ei)->bei_lrunext = (cache)->c_lruhead; \
155 if ( (ei)->bei_lrunext != NULL ) { \
156 (ei)->bei_lrunext->bei_lruprev = (ei); \
158 (cache)->c_lruhead = (ei); \
159 (ei)->bei_lruprev = NULL; \
160 if ( (cache)->c_lrutail == NULL ) { \
161 (cache)->c_lrutail = (ei); \
165 /* Do a lexical sort on normalized RDNs */
167 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
169 const EntryInfo *e1 = v_e1, *e2 = v_e2;
170 int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
171 if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
176 bdb_id_cmp( const void *v_e1, const void *v_e2 )
178 const EntryInfo *e1 = v_e1, *e2 = v_e2;
179 return e1->bei_id - e2->bei_id;
182 /* Create an entryinfo in the cache. Caller must release the locks later.
185 bdb_entryinfo_add_internal(
186 struct bdb_info *bdb,
194 Cache *cache = &bdb->bi_cache;
195 DB_ENV *env = bdb->bi_dbenv;
196 EntryInfo *ei2 = NULL;
204 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
205 bdb_cache_entryinfo_lock( eip );
207 /* if parent was previously considered a leaf node,
208 * it was on the LRU list. Now it's going to have
209 * kids, take it off the LRU list.
211 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
212 if ( eip->bei_id && !eip->bei_kids ) {
213 LRU_DELETE( cache, eip );
217 cache->c_cursize += incr;
219 /* See if we're above the cache size limit */
220 if ( cache->c_cursize > cache->c_maxsize ) {
221 EntryInfo *elru, *elprev;
224 /* Look for an unused entry to remove */
225 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
226 elprev = elru->bei_lruprev;
228 /* Too many probes, not enough idle, give up */
231 /* If we can successfully writelock it, then
232 * the object is idle.
234 if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
236 /* Need to lock parent to delete child */
237 if ( ldap_pvt_thread_mutex_trylock(
238 &elru->bei_parent->bei_kids_mutex )) {
239 bdb_cache_entry_db_unlock( env, &lock );
242 bdb_cache_delete_entry_internal( cache, elru );
243 bdb_cache_entryinfo_unlock( elru->bei_parent );
244 elru->bei_e->e_private = NULL;
245 bdb_entry_return( elru->bei_e );
246 bdb_cache_entry_db_unlock( env, &lock );
248 bdb_cache_entryinfo_destroy( elru );
250 /* re-use this one */
251 ch_free(elru->bei_nrdn.bv_val);
252 elru->bei_nrdn.bv_val = NULL;
254 elru->bei_kids = NULL;
255 elru->bei_lrunext = NULL;
256 elru->bei_lruprev = NULL;
260 if (cache->c_cursize < cache->c_maxsize)
266 ei2 = bdb_cache_entryinfo_new();
269 ei2->bei_parent = eip;
271 /* Add to cache ID tree */
272 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
274 ei = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
275 bdb_cache_entryinfo_destroy( ei2 );
278 cache->c_cursize -= incr;
280 LRU_ADD( cache, ei2 );
281 ber_dupbv( &ei2->bei_nrdn, nrdn );
285 avl_insert( &eip->bei_kids, ei2, bdb_rdn_cmp, avl_dup_error );
288 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
290 #if 0 /* caller must do these frees */
291 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
292 bdb_cache_entryinfo_unlock( eip );
299 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
300 * the info for its closest ancestor. *res should be NULL to process a
301 * complete DN starting from the tree root. Otherwise *res must be the
302 * immediate parent of the requested DN, and only the RDN will be searched.
303 * The EntryInfo is locked upon return and must be unlocked by the caller.
306 bdb_cache_find_entry_ndn2id(
315 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
316 EntryInfo ei, *eip, *ei2;
320 /* this function is always called with normalized DN */
322 /* we're doing a onelevel search for an RDN */
323 ei.bei_nrdn.bv_val = ndn->bv_val;
324 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
327 /* we're searching a full DN from the root */
328 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
329 ei.bei_nrdn.bv_val = ptr;
330 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
331 eip = &bdb->bi_cache.c_dntree;
334 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
335 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
337 int len = ei.bei_nrdn.bv_len;
339 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
340 bdb_cache_entryinfo_unlock( eip );
342 rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
344 bdb_cache_entryinfo_lock( eip );
349 /* DN exists but needs to be added to cache */
350 ei.bei_nrdn.bv_len = len;
351 rc = bdb_entryinfo_add_internal( bdb,
352 eip, ei.bei_id, &ei.bei_nrdn, &ei2, locker );
353 /* add_internal left eip and c_rwlock locked */
354 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
359 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
360 /* In the midst of deleting? Give it a chance to
363 bdb_cache_entryinfo_unlock( eip );
364 ldap_pvt_thread_yield();
365 bdb_cache_entryinfo_lock( eip );
369 bdb_cache_entryinfo_unlock( eip );
370 bdb_cache_entryinfo_lock( ei2 );
374 /* Advance to next lower RDN */
375 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
376 && !DN_SEPARATOR(*ptr); ptr--);
377 if ( ptr >= ndn->bv_val ) {
378 if (DN_SEPARATOR(*ptr)) ptr++;
379 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
380 ei.bei_nrdn.bv_val = ptr;
382 if ( ptr < ndn->bv_val ) {
392 * cache_find_entry_id - find an entry in the cache, given id.
393 * The entry is locked for Read upon return. Call with islocked TRUE if
394 * the supplied *eip was already locked.
398 bdb_cache_find_entry_id(
409 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
416 /* If we weren't given any info, see if we have it already cached */
418 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
419 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
420 (caddr_t) &ei, bdb_id_cmp );
422 bdb_cache_entryinfo_lock( *eip );
425 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
428 /* See if the ID exists in the database; add it to the cache if so */
430 rc = bdb_id2entry( be, tid, id, &ep );
432 rc = bdb_cache_find_entry_ndn2id( be, tid,
433 &ep->e_nname, eip, locker, ctx );
437 bdb_entry_return( ep );
443 /* Ok, we found the info, do we have the entry? */
444 if ( *eip && rc == 0 ) {
445 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
447 } else if (!(*eip)->bei_e ) {
449 rc = bdb_id2entry( be, tid, id, &ep );
452 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
455 ep->e_private = *eip;
456 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
460 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
464 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
466 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
467 LRU_DELETE( &bdb->bi_cache, *eip );
468 LRU_ADD( &bdb->bi_cache, *eip );
469 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
473 bdb_cache_entryinfo_unlock( *eip );
487 if ( BEI(e)->bei_kids ) {
490 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
493 rc = bdb_dn2id_children( op, txn, e );
494 if ( rc == DB_NOTFOUND ) {
495 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
500 /* Update the cache after a successful database Add. */
503 struct bdb_info *bdb,
513 rc = bdb_entryinfo_add_internal( bdb, ei, e->e_id, nrdn, &new, locker );
516 new->bei_state = CACHE_ENTRY_NO_KIDS;
517 ei->bei_state &= ~CACHE_ENTRY_NO_KIDS;
518 bdb_cache_entryinfo_unlock( ei );
519 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
532 EntryInfo *ei = BEI(e);
534 /* Get write lock on data */
535 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
537 /* If we've done repeated mods on a cached entry, then e_attrs
538 * is no longer contiguous with the entry, and must be freed.
540 if ( (void *)e->e_attrs != (void *)(e+1) ) {
541 attrs_free( e->e_attrs );
543 e->e_attrs = newAttrs;
549 * Change the rdn in the entryinfo. Also move to a new parent if needed.
562 EntryInfo *ei = BEI(e), *pei;
565 /* Get write lock on data */
566 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
568 /* If we've done repeated mods on a cached entry, then e_attrs
569 * is no longer contiguous with the entry, and must be freed.
571 if ( (void *)e->e_attrs != (void *)(e+1) ) {
572 attrs_free( e->e_attrs );
574 e->e_attrs = new->e_attrs;
576 ch_free(e->e_name.bv_val);
578 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
579 e->e_bv.bv_val + e->e_bv.bv_len ) {
580 ch_free(e->e_name.bv_val);
581 ch_free(e->e_nname.bv_val);
584 e->e_name = new->e_name;
585 e->e_nname = new->e_nname;
587 /* Lock the parent's kids AVL tree */
588 pei = ei->bei_parent;
589 bdb_cache_entryinfo_lock( pei );
590 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
591 free( ei->bei_nrdn.bv_val );
592 ber_dupbv( &ei->bei_nrdn, nrdn );
594 ein = ei->bei_parent;
596 ei->bei_parent = ein;
597 bdb_cache_entryinfo_unlock( pei );
598 bdb_cache_entryinfo_lock( ein );
600 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
601 bdb_cache_entryinfo_unlock( ein );
605 * cache_delete_entry - delete the entry e from the cache.
607 * returns: 0 e was deleted ok
608 * 1 e was not in the cache
609 * -1 something bad happened
612 bdb_cache_delete_entry(
620 EntryInfo *ei = BEI(e);
623 assert( e->e_private );
625 /* Set this early, warn off any queriers */
626 ei->bei_state |= CACHE_ENTRY_DELETED;
628 /* Get write lock on the data */
629 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
631 /* set cache write lock */
632 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
634 /* Lock the parent's kids tree */
635 bdb_cache_entryinfo_lock( ei->bei_parent );
638 LDAP_LOG( CACHE, ENTRY,
639 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
641 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
646 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
647 rc = bdb_cache_delete_entry_internal( cache, e->e_private );
649 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
651 /* free cache write lock */
652 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
653 bdb_cache_entryinfo_unlock( ei->bei_parent );
654 bdb_cache_entryinfo_destroy( ei );
660 bdb_cache_delete_entry_internal(
665 int rc = 0; /* return code */
668 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
673 /* If parent has no more kids, put in on LRU list */
674 if ( e->bei_parent->bei_kids == NULL ) {
675 LRU_ADD( cache, e->bei_parent );
680 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
690 LRU_DELETE( cache, e );
694 * flag entry to be freed later by a call to cache_return_entry()
696 e->bei_state |= CACHE_ENTRY_DELETED;
702 bdb_entryinfo_release( void *data )
704 EntryInfo *ei = (EntryInfo *)data;
705 avl_free( ei->bei_kids, NULL );
707 ei->bei_e->e_private = NULL;
708 bdb_entry_return( ei->bei_e );
710 bdb_cache_entryinfo_destroy( ei );
714 bdb_cache_release_all( Cache *cache )
716 /* set cache write lock */
717 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
719 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
722 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
724 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
727 avl_free( cache->c_dntree.bei_kids, NULL );
728 avl_free( cache->c_idtree, bdb_entryinfo_release );
729 cache->c_lruhead = NULL;
730 cache->c_lrutail = NULL;
733 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
734 /* free cache write lock */
735 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
740 bdb_lru_print( Cache *cache )
744 fprintf( stderr, "LRU queue (head to tail):\n" );
745 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
746 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
747 e->bei_nrdn.bv_val, e->bei_id );
749 fprintf( stderr, "LRU queue (tail to head):\n" );
750 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
751 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
752 e->bei_nrdn.bv_val, e->bei_id );
757 #ifdef BDB_REUSE_LOCKERS
759 bdb_locker_id_free( void *key, void *data )
762 int lockid = (int) data;
764 XLOCK_ID_FREE( env, lockid );
768 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
774 if ( !env || !locker ) return -1;
776 /* If no op was provided, try to find the ctx anyway... */
778 ctx = op->o_threadctx;
780 ctx = ldap_pvt_thread_pool_context();
783 /* Shouldn't happen unless we're single-threaded */
789 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
790 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
791 rc = XLOCK_ID( env, &lockid );
792 if (rc) ldap_pvt_thread_yield();
797 data = (void *)lockid;
798 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
799 data, bdb_locker_id_free ) ) ) {
800 XLOCK_ID_FREE( env, lockid );
802 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
803 db_strerror(rc), rc, 0 );
805 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
806 db_strerror(rc), rc, 0 );