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;
321 /* this function is always called with normalized DN */
323 /* we're doing a onelevel search for an RDN */
324 ei.bei_nrdn.bv_val = ndn->bv_val;
325 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
328 /* we're searching a full DN from the root */
329 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
330 ei.bei_nrdn.bv_val = ptr;
331 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
332 eip = &bdb->bi_cache.c_dntree;
335 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
336 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
338 int len = ei.bei_nrdn.bv_len;
340 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
341 bdb_cache_entryinfo_unlock( eip );
343 rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &id, ctx );
345 bdb_cache_entryinfo_lock( eip );
350 /* DN exists but needs to be added to cache */
351 ei.bei_nrdn.bv_len = len;
352 rc = bdb_entryinfo_add_internal( bdb,
353 eip, id, &ei.bei_nrdn, &ei2, locker );
354 /* add_internal left eip and c_rwlock locked */
355 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
360 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
361 /* In the midst of deleting? Give it a chance to
364 bdb_cache_entryinfo_unlock( eip );
365 ldap_pvt_thread_yield();
366 bdb_cache_entryinfo_lock( eip );
370 bdb_cache_entryinfo_unlock( eip );
371 bdb_cache_entryinfo_lock( ei2 );
375 /* Advance to next lower RDN */
376 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
377 && !DN_SEPARATOR(*ptr); ptr--);
378 if ( ptr >= ndn->bv_val ) {
379 if (DN_SEPARATOR(*ptr)) ptr++;
380 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
381 ei.bei_nrdn.bv_val = ptr;
383 if ( ptr < ndn->bv_val ) {
393 * cache_find_entry_id - find an entry in the cache, given id.
394 * The entry is locked for Read upon return. Call with islocked TRUE if
395 * the supplied *eip was already locked.
399 bdb_cache_find_entry_id(
410 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
417 /* If we weren't given any info, see if we have it already cached */
419 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
420 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
421 (caddr_t) &ei, bdb_id_cmp );
423 bdb_cache_entryinfo_lock( *eip );
426 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
429 /* See if the ID exists in the database; add it to the cache if so */
431 rc = bdb_id2entry( be, tid, id, &ep );
433 rc = bdb_cache_find_entry_ndn2id( be, tid,
434 &ep->e_nname, eip, locker, ctx );
438 bdb_entry_return( ep );
444 /* Ok, we found the info, do we have the entry? */
445 if ( *eip && rc == 0 ) {
446 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
448 } else if (!(*eip)->bei_e ) {
450 rc = bdb_id2entry( be, tid, id, &ep );
453 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
456 ep->e_private = *eip;
457 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
461 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
465 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
467 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
468 LRU_DELETE( &bdb->bi_cache, *eip );
469 LRU_ADD( &bdb->bi_cache, *eip );
470 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
474 bdb_cache_entryinfo_unlock( *eip );
479 /* Update the cache after a successful database Add. */
482 struct bdb_info *bdb,
492 rc = bdb_entryinfo_add_internal( bdb, ei, e->e_id, nrdn, &new, locker );
495 new->bei_state = CACHE_ENTRY_NO_KIDS;
496 ei->bei_state &= ~CACHE_ENTRY_NO_KIDS;
497 bdb_cache_entryinfo_unlock( ei );
498 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
511 EntryInfo *ei = BEI(e);
513 /* Get write lock on data */
514 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
516 /* If we've done repeated mods on a cached entry, then e_attrs
517 * is no longer contiguous with the entry, and must be freed.
519 if ( (void *)e->e_attrs != (void *)(e+1) ) {
520 attrs_free( e->e_attrs );
522 e->e_attrs = newAttrs;
528 * Change the rdn in the entryinfo. Also move to a new parent if needed.
541 EntryInfo *ei = BEI(e), *pei;
544 /* Get write lock on data */
545 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
547 /* If we've done repeated mods on a cached entry, then e_attrs
548 * is no longer contiguous with the entry, and must be freed.
550 if ( (void *)e->e_attrs != (void *)(e+1) ) {
551 attrs_free( e->e_attrs );
553 e->e_attrs = new->e_attrs;
555 ch_free(e->e_name.bv_val);
557 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
558 e->e_bv.bv_val + e->e_bv.bv_len ) {
559 ch_free(e->e_name.bv_val);
560 ch_free(e->e_nname.bv_val);
563 e->e_name = new->e_name;
564 e->e_nname = new->e_nname;
566 /* Lock the parent's kids AVL tree */
567 pei = ei->bei_parent;
568 bdb_cache_entryinfo_lock( pei );
569 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
570 free( ei->bei_nrdn.bv_val );
571 ber_dupbv( &ei->bei_nrdn, nrdn );
573 ein = ei->bei_parent;
575 ei->bei_parent = ein;
576 bdb_cache_entryinfo_unlock( pei );
577 bdb_cache_entryinfo_lock( ein );
579 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
580 bdb_cache_entryinfo_unlock( ein );
584 * cache_delete_entry - delete the entry e from the cache.
586 * returns: 0 e was deleted ok
587 * 1 e was not in the cache
588 * -1 something bad happened
591 bdb_cache_delete_entry(
599 EntryInfo *ei = BEI(e);
602 assert( e->e_private );
604 /* Set this early, warn off any queriers */
605 ei->bei_state |= CACHE_ENTRY_DELETED;
607 /* Get write lock on the data */
608 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
610 /* set cache write lock */
611 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
613 /* Lock the parent's kids tree */
614 bdb_cache_entryinfo_lock( ei->bei_parent );
617 LDAP_LOG( CACHE, ENTRY,
618 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
620 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
625 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
626 rc = bdb_cache_delete_entry_internal( cache, e->e_private );
628 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
630 /* free cache write lock */
631 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
632 bdb_cache_entryinfo_unlock( ei->bei_parent );
633 bdb_cache_entryinfo_destroy( ei );
639 bdb_cache_delete_entry_internal(
644 int rc = 0; /* return code */
647 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
652 /* If parent has no more kids, put in on LRU list */
653 if ( e->bei_parent->bei_kids == NULL ) {
654 LRU_ADD( cache, e->bei_parent );
659 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
669 LRU_DELETE( cache, e );
673 * flag entry to be freed later by a call to cache_return_entry()
675 e->bei_state |= CACHE_ENTRY_DELETED;
681 bdb_entryinfo_release( void *data )
683 EntryInfo *ei = (EntryInfo *)data;
684 avl_free( ei->bei_kids, NULL );
686 ei->bei_e->e_private = NULL;
687 bdb_entry_return( ei->bei_e );
689 bdb_cache_entryinfo_destroy( ei );
693 bdb_cache_release_all( Cache *cache )
695 /* set cache write lock */
696 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
698 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
701 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
703 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
706 avl_free( cache->c_dntree.bei_kids, NULL );
707 avl_free( cache->c_idtree, bdb_entryinfo_release );
708 cache->c_lruhead = NULL;
709 cache->c_lrutail = NULL;
712 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
713 /* free cache write lock */
714 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
719 bdb_lru_print( Cache *cache )
723 fprintf( stderr, "LRU queue (head to tail):\n" );
724 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
725 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
726 e->bei_nrdn.bv_val, e->bei_id );
728 fprintf( stderr, "LRU queue (tail to head):\n" );
729 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
730 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
731 e->bei_nrdn.bv_val, e->bei_id );
736 #ifdef BDB_REUSE_LOCKERS
738 bdb_locker_id_free( void *key, void *data )
741 int lockid = (int) data;
743 XLOCK_ID_FREE( env, lockid );
747 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
753 if ( !env || !locker ) return -1;
755 /* If no op was provided, try to find the ctx anyway... */
757 ctx = op->o_threadctx;
759 ctx = ldap_pvt_thread_pool_context();
762 /* Shouldn't happen unless we're single-threaded */
768 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
769 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
770 rc = XLOCK_ID( env, &lockid );
771 if (rc) ldap_pvt_thread_yield();
776 data = (void *)lockid;
777 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
778 data, bdb_locker_id_free ) ) ) {
779 XLOCK_ID_FREE( env, lockid );
781 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
782 db_strerror(rc), rc, 0 );
784 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
785 db_strerror(rc), rc, 0 );