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(
53 if ( !lock ) return 0;
56 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
58 list[0].op = DB_LOCK_PUT;
60 list[1].op = DB_LOCK_GET;
62 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
63 list[1].obj = &lockobj;
64 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
69 LDAP_LOG( CACHE, DETAIL1,
70 "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
73 Debug( LDAP_DEBUG_TRACE,
74 "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
84 bdb_cache_entry_db_lock
85 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
94 if ( !lock ) return 0;
97 db_rw = DB_LOCK_WRITE;
102 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
104 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
105 &lockobj, db_rw, lock);
108 LDAP_LOG( CACHE, DETAIL1,
109 "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
110 ei->bei_id, rw, rc );
112 Debug( LDAP_DEBUG_TRACE,
113 "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
114 ei->bei_id, rw, rc );
118 #endif /* NO_THREADS */
122 bdb_cache_entry_db_unlock
123 ( DB_ENV *env, DB_LOCK *lock )
130 rc = LOCK_PUT ( env, lock );
136 bdb_cache_entryinfo_destroy( EntryInfo *e )
138 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
139 free( e->bei_nrdn.bv_val );
141 free( e->bei_rdn.bv_val );
147 #define LRU_DELETE( cache, ei ) do { \
148 if ( (ei)->bei_lruprev != NULL ) { \
149 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
151 (cache)->c_lruhead = (ei)->bei_lrunext; \
153 if ( (ei)->bei_lrunext != NULL ) { \
154 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
156 (cache)->c_lrutail = (ei)->bei_lruprev; \
160 #define LRU_ADD( cache, ei ) do { \
161 (ei)->bei_lrunext = (cache)->c_lruhead; \
162 if ( (ei)->bei_lrunext != NULL ) { \
163 (ei)->bei_lrunext->bei_lruprev = (ei); \
165 (cache)->c_lruhead = (ei); \
166 (ei)->bei_lruprev = NULL; \
167 if ( (cache)->c_lrutail == NULL ) { \
168 (cache)->c_lrutail = (ei); \
172 /* Do a lexical sort on normalized RDNs */
174 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
176 const EntryInfo *e1 = v_e1, *e2 = v_e2;
177 int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
178 if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
183 bdb_id_cmp( const void *v_e1, const void *v_e2 )
185 const EntryInfo *e1 = v_e1, *e2 = v_e2;
186 return e1->bei_id - e2->bei_id;
189 /* Create an entryinfo in the cache. Caller must release the locks later.
192 bdb_entryinfo_add_internal(
193 struct bdb_info *bdb,
199 Cache *cache = &bdb->bi_cache;
200 DB_ENV *env = bdb->bi_dbenv;
201 EntryInfo *ei2 = NULL;
209 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
210 bdb_cache_entryinfo_lock( ei->bei_parent );
212 /* if parent was previously considered a leaf node,
213 * it was on the LRU list. Now it's going to have
214 * kids, take it off the LRU list.
216 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
217 if ( ei->bei_parent->bei_id && !ei->bei_parent->bei_kids ) {
218 LRU_DELETE( cache, ei->bei_parent );
222 cache->c_cursize += incr;
224 /* See if we're above the cache size limit */
225 if ( cache->c_cursize > cache->c_maxsize ) {
226 EntryInfo *elru, *elprev;
229 /* Look for an unused entry to remove */
230 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
231 elprev = elru->bei_lruprev;
233 /* Too many probes, not enough idle, give up */
236 /* If we can successfully writelock it, then
237 * the object is idle.
239 if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
241 /* Need to lock parent to delete child */
242 if ( ldap_pvt_thread_mutex_trylock(
243 &elru->bei_parent->bei_kids_mutex )) {
244 bdb_cache_entry_db_unlock( env, &lock );
247 bdb_cache_delete_entry_internal( cache, elru );
248 bdb_cache_entryinfo_unlock( elru->bei_parent );
249 elru->bei_e->e_private = NULL;
250 bdb_entry_return( elru->bei_e );
251 bdb_cache_entry_db_unlock( env, &lock );
253 bdb_cache_entryinfo_destroy( elru );
255 /* re-use this one */
256 ch_free(elru->bei_nrdn.bv_val);
257 elru->bei_nrdn.bv_val = NULL;
259 elru->bei_kids = NULL;
260 elru->bei_lrunext = NULL;
261 elru->bei_lruprev = NULL;
265 if (cache->c_cursize < cache->c_maxsize)
271 ei2 = bdb_cache_entryinfo_new();
273 ei2->bei_id = ei->bei_id;
274 ei2->bei_parent = ei->bei_parent;
276 ei2->bei_rdn = ei->bei_rdn;
279 /* Add to cache ID tree */
280 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
282 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
283 bdb_cache_entryinfo_destroy( ei2 );
286 cache->c_cursize -= incr;
288 if ( ei->bei_rdn.bv_val )
289 ber_memfree_x( ei->bei_rdn.bv_val, NULL );
292 LRU_ADD( cache, ei2 );
293 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
297 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
301 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
307 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
308 * the info for its closest ancestor. *res should be NULL to process a
309 * complete DN starting from the tree root. Otherwise *res must be the
310 * immediate parent of the requested DN, and only the RDN will be searched.
311 * The EntryInfo is locked upon return and must be unlocked by the caller.
314 bdb_cache_find_entry_ndn2id(
323 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
324 EntryInfo ei, *eip, *ei2;
328 /* this function is always called with normalized DN */
330 /* we're doing a onelevel search for an RDN */
331 ei.bei_nrdn.bv_val = ndn->bv_val;
332 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
335 /* we're searching a full DN from the root */
336 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
337 ei.bei_nrdn.bv_val = ptr;
338 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
339 eip = &bdb->bi_cache.c_dntree;
342 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
344 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
346 int len = ei.bei_nrdn.bv_len;
348 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
349 bdb_cache_entryinfo_unlock( eip );
351 rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
353 bdb_cache_entryinfo_lock( eip );
358 /* DN exists but needs to be added to cache */
359 ei.bei_nrdn.bv_len = len;
360 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
362 /* add_internal left eip and c_rwlock locked */
363 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
368 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
369 /* In the midst of deleting? Give it a chance to
372 bdb_cache_entryinfo_unlock( eip );
373 ldap_pvt_thread_yield();
374 bdb_cache_entryinfo_lock( eip );
378 bdb_cache_entryinfo_unlock( eip );
379 bdb_cache_entryinfo_lock( ei2 );
383 /* Advance to next lower RDN */
384 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
385 && !DN_SEPARATOR(*ptr); ptr--);
386 if ( ptr >= ndn->bv_val ) {
387 if (DN_SEPARATOR(*ptr)) ptr++;
388 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
389 ei.bei_nrdn.bv_val = ptr;
391 if ( ptr < ndn->bv_val ) {
401 /* Walk up the tree from a child node, looking for an ID that's already
402 * been linked into the cache.
405 bdb_cache_find_parent(
413 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
414 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
422 rc = bdb_dn2id_parent( be, txn, &ei, &eip.bei_id, ctx );
425 /* Save the previous node, if any */
428 /* Create a new node for the current ID */
429 ein = bdb_cache_entryinfo_new();
430 ein->bei_id = ei.bei_id;
431 ein->bei_kids = ei.bei_kids;
432 ein->bei_nrdn = ei.bei_nrdn;
434 ein->bei_rdn = ei.bei_rdn;
437 /* This node is not fully connected yet */
438 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
440 /* If this is the first time, save this node
441 * to be returned later.
443 if ( eir == NULL ) eir = ein;
445 /* Insert this node into the ID tree */
446 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
447 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
448 bdb_id_cmp, avl_dup_error ) ) {
450 /* Hm, can this really happen? */
451 bdb_cache_entryinfo_destroy( ein );
452 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
453 (caddr_t) &ei, bdb_id_cmp );
454 bdb_cache_entryinfo_lock( ein );
455 avl_insert( &ein->bei_kids, (caddr_t)ei2, bdb_rdn_cmp,
457 bdb_cache_entryinfo_unlock( ein );
460 /* If there was a previous node, link it to this one */
461 if ( ei2 ) ei2->bei_parent = ein;
464 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
465 (caddr_t) &eip, bdb_id_cmp );
467 ei2 = &bdb->bi_cache.c_dntree;
471 ein->bei_parent = ei2;
472 bdb_cache_entryinfo_lock( ei2 );
473 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
475 bdb_cache_entryinfo_unlock( ei2 );
477 bdb_cache_entryinfo_lock( eir );
479 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
481 /* Found a link. Reset all the state info */
482 for (ein = eir; ein != ei2; ein=ein->bei_parent)
483 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
487 ei.bei_id = eip.bei_id;
488 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
496 * cache_find_entry_id - find an entry in the cache, given id.
497 * The entry is locked for Read upon return. Call with islocked TRUE if
498 * the supplied *eip was already locked.
502 bdb_cache_find_entry_id(
513 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
520 /* If we weren't given any info, see if we have it already cached */
522 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
523 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
524 (caddr_t) &ei, bdb_id_cmp );
526 bdb_cache_entryinfo_lock( *eip );
529 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
532 /* See if the ID exists in the database; add it to the cache if so */
535 rc = bdb_id2entry( be, tid, id, &ep );
537 rc = bdb_cache_find_entry_ndn2id( be, tid,
538 &ep->e_nname, eip, locker, ctx );
542 bdb_entry_return( ep );
547 rc = bdb_cache_find_parent(be, tid, id, eip, ctx );
548 if ( rc == 0 && *eip )
553 /* Ok, we found the info, do we have the entry? */
554 if ( *eip && rc == 0 ) {
555 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
557 } else if (!(*eip)->bei_e ) {
559 rc = bdb_id2entry( be, tid, id, &ep );
562 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
564 ep->e_private = *eip;
569 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
573 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
577 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
579 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
580 LRU_DELETE( &bdb->bi_cache, *eip );
581 LRU_ADD( &bdb->bi_cache, *eip );
582 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
586 bdb_cache_entryinfo_unlock( *eip );
600 if ( BEI(e)->bei_kids ) {
603 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
606 rc = bdb_dn2id_children( op, txn, e );
607 if ( rc == DB_NOTFOUND ) {
608 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
613 /* Update the cache after a successful database Add. */
616 struct bdb_info *bdb,
624 struct berval rdn = e->e_name;
631 if ( nrdn->bv_len != e->e_nname.bv_len ) {
632 char *ptr = strchr( rdn.bv_val, ',' );
633 rdn.bv_len = ptr - rdn.bv_val;
635 ber_dupbv( &ei.bei_rdn, &rdn );
637 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
640 new->bei_state = CACHE_ENTRY_NO_KIDS;
641 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
642 bdb_cache_entryinfo_unlock( eip );
643 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
656 EntryInfo *ei = BEI(e);
658 /* Get write lock on data */
659 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
661 /* If we've done repeated mods on a cached entry, then e_attrs
662 * is no longer contiguous with the entry, and must be freed.
664 if ( (void *)e->e_attrs != (void *)(e+1) ) {
665 attrs_free( e->e_attrs );
667 e->e_attrs = newAttrs;
673 * Change the rdn in the entryinfo. Also move to a new parent if needed.
686 EntryInfo *ei = BEI(e), *pei;
690 /* Get write lock on data */
691 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
693 /* If we've done repeated mods on a cached entry, then e_attrs
694 * is no longer contiguous with the entry, and must be freed.
696 if ( (void *)e->e_attrs != (void *)(e+1) ) {
697 attrs_free( e->e_attrs );
699 e->e_attrs = new->e_attrs;
701 ch_free(e->e_name.bv_val);
703 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
704 e->e_bv.bv_val + e->e_bv.bv_len ) {
705 ch_free(e->e_name.bv_val);
706 ch_free(e->e_nname.bv_val);
709 e->e_name = new->e_name;
710 e->e_nname = new->e_nname;
712 /* Lock the parent's kids AVL tree */
713 pei = ei->bei_parent;
714 bdb_cache_entryinfo_lock( pei );
715 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
716 free( ei->bei_nrdn.bv_val );
717 ber_dupbv( &ei->bei_nrdn, nrdn );
719 free( ei->bei_rdn.bv_val );
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 );
730 ein = ei->bei_parent;
732 ei->bei_parent = ein;
733 bdb_cache_entryinfo_unlock( pei );
734 bdb_cache_entryinfo_lock( ein );
736 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
737 bdb_cache_entryinfo_unlock( ein );
741 * cache_delete_entry - delete the entry e from the cache.
743 * returns: 0 e was deleted ok
744 * 1 e was not in the cache
745 * -1 something bad happened
748 bdb_cache_delete_entry(
756 EntryInfo *ei = BEI(e);
759 assert( e->e_private );
761 /* Set this early, warn off any queriers */
762 ei->bei_state |= CACHE_ENTRY_DELETED;
764 /* Get write lock on the data */
765 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
767 /* set cache write lock */
768 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
770 /* Lock the parent's kids tree */
771 bdb_cache_entryinfo_lock( ei->bei_parent );
774 LDAP_LOG( CACHE, ENTRY,
775 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
777 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
782 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
783 rc = bdb_cache_delete_entry_internal( cache, e->e_private );
785 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
787 /* free cache write lock */
788 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
789 bdb_cache_entryinfo_unlock( ei->bei_parent );
790 bdb_cache_entryinfo_destroy( ei );
796 bdb_cache_delete_entry_internal(
801 int rc = 0; /* return code */
804 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
809 /* If parent has no more kids, put in on LRU list */
810 if ( e->bei_parent->bei_kids == NULL ) {
811 LRU_ADD( cache, e->bei_parent );
816 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
826 LRU_DELETE( cache, e );
830 * flag entry to be freed later by a call to cache_return_entry()
832 e->bei_state |= CACHE_ENTRY_DELETED;
838 bdb_entryinfo_release( void *data )
840 EntryInfo *ei = (EntryInfo *)data;
841 if ( ei->bei_kids ) {
842 avl_free( ei->bei_kids, NULL );
845 ei->bei_e->e_private = NULL;
846 bdb_entry_return( ei->bei_e );
848 bdb_cache_entryinfo_destroy( ei );
852 bdb_cache_release_all( Cache *cache )
854 /* set cache write lock */
855 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
857 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
860 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
862 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
865 avl_free( cache->c_dntree.bei_kids, NULL );
866 avl_free( cache->c_idtree, bdb_entryinfo_release );
867 cache->c_lruhead = NULL;
868 cache->c_lrutail = NULL;
871 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
872 /* free cache write lock */
873 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
878 bdb_lru_print( Cache *cache )
882 fprintf( stderr, "LRU queue (head to tail):\n" );
883 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
884 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
885 e->bei_nrdn.bv_val, e->bei_id );
887 fprintf( stderr, "LRU queue (tail to head):\n" );
888 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
889 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
890 e->bei_nrdn.bv_val, e->bei_id );
895 #ifdef BDB_REUSE_LOCKERS
897 bdb_locker_id_free( void *key, void *data )
900 int lockid = (int) data;
902 XLOCK_ID_FREE( env, lockid );
906 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
912 if ( !env || !locker ) return -1;
914 /* If no op was provided, try to find the ctx anyway... */
916 ctx = op->o_threadctx;
918 ctx = ldap_pvt_thread_pool_context();
921 /* Shouldn't happen unless we're single-threaded */
927 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
928 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
929 rc = XLOCK_ID( env, &lockid );
930 if (rc) ldap_pvt_thread_yield();
935 data = (void *)lockid;
936 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
937 data, bdb_locker_id_free ) ) ) {
938 XLOCK_ID_FREE( env, lockid );
940 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
941 db_strerror(rc), rc, 0 );
943 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
944 db_strerror(rc), rc, 0 );