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 );
144 #define LRU_DELETE( cache, ei ) do { \
145 if ( (ei)->bei_lruprev != NULL ) { \
146 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
148 (cache)->c_lruhead = (ei)->bei_lrunext; \
150 if ( (ei)->bei_lrunext != NULL ) { \
151 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
153 (cache)->c_lrutail = (ei)->bei_lruprev; \
157 #define LRU_ADD( cache, ei ) do { \
158 (ei)->bei_lrunext = (cache)->c_lruhead; \
159 if ( (ei)->bei_lrunext != NULL ) { \
160 (ei)->bei_lrunext->bei_lruprev = (ei); \
162 (cache)->c_lruhead = (ei); \
163 (ei)->bei_lruprev = NULL; \
164 if ( (cache)->c_lrutail == NULL ) { \
165 (cache)->c_lrutail = (ei); \
169 /* Do a lexical sort on normalized RDNs */
171 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
173 const EntryInfo *e1 = v_e1, *e2 = v_e2;
174 int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
175 if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
180 bdb_id_cmp( const void *v_e1, const void *v_e2 )
182 const EntryInfo *e1 = v_e1, *e2 = v_e2;
183 return e1->bei_id - e2->bei_id;
186 /* Create an entryinfo in the cache. Caller must release the locks later.
189 bdb_entryinfo_add_internal(
190 struct bdb_info *bdb,
196 Cache *cache = &bdb->bi_cache;
197 DB_ENV *env = bdb->bi_dbenv;
198 EntryInfo *ei2 = NULL;
206 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
207 bdb_cache_entryinfo_lock( ei->bei_parent );
209 /* if parent was previously considered a leaf node,
210 * it was on the LRU list. Now it's going to have
211 * kids, take it off the LRU list.
213 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
214 if ( ei->bei_parent->bei_id && !ei->bei_parent->bei_kids ) {
215 LRU_DELETE( cache, ei->bei_parent );
219 cache->c_cursize += incr;
221 /* See if we're above the cache size limit */
222 if ( cache->c_cursize > cache->c_maxsize ) {
223 EntryInfo *elru, *elprev;
226 /* Look for an unused entry to remove */
227 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
228 elprev = elru->bei_lruprev;
230 /* Too many probes, not enough idle, give up */
233 /* If we can successfully writelock it, then
234 * the object is idle.
236 if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
238 /* Need to lock parent to delete child */
239 if ( ldap_pvt_thread_mutex_trylock(
240 &elru->bei_parent->bei_kids_mutex )) {
241 bdb_cache_entry_db_unlock( env, &lock );
244 bdb_cache_delete_entry_internal( cache, elru );
245 bdb_cache_entryinfo_unlock( elru->bei_parent );
246 elru->bei_e->e_private = NULL;
247 bdb_entry_return( elru->bei_e );
248 bdb_cache_entry_db_unlock( env, &lock );
250 bdb_cache_entryinfo_destroy( elru );
252 /* re-use this one */
253 ch_free(elru->bei_nrdn.bv_val);
254 elru->bei_nrdn.bv_val = NULL;
256 elru->bei_kids = NULL;
257 elru->bei_lrunext = NULL;
258 elru->bei_lruprev = NULL;
262 if (cache->c_cursize < cache->c_maxsize)
268 ei2 = bdb_cache_entryinfo_new();
270 ei2->bei_id = ei->bei_id;
271 ei2->bei_parent = ei->bei_parent;
272 ei2->bei_rdn = ei->bei_rdn;
274 /* Add to cache ID tree */
275 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
277 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
278 bdb_cache_entryinfo_destroy( ei2 );
281 cache->c_cursize -= incr;
282 if ( ei->bei_rdn.bv_val )
283 ber_memfree_x( ei->bei_rdn.bv_val, NULL );
285 LRU_ADD( cache, ei2 );
286 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
290 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
294 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
300 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
301 * the info for its closest ancestor. *res should be NULL to process a
302 * complete DN starting from the tree root. Otherwise *res must be the
303 * immediate parent of the requested DN, and only the RDN will be searched.
304 * The EntryInfo is locked upon return and must be unlocked by the caller.
307 bdb_cache_find_entry_ndn2id(
316 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
317 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; ) {
337 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
339 int len = ei.bei_nrdn.bv_len;
341 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
342 bdb_cache_entryinfo_unlock( eip );
344 rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
346 bdb_cache_entryinfo_lock( eip );
351 /* DN exists but needs to be added to cache */
352 ei.bei_nrdn.bv_len = len;
353 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
355 /* add_internal left eip and c_rwlock locked */
356 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
361 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
362 /* In the midst of deleting? Give it a chance to
365 bdb_cache_entryinfo_unlock( eip );
366 ldap_pvt_thread_yield();
367 bdb_cache_entryinfo_lock( eip );
371 bdb_cache_entryinfo_unlock( eip );
372 bdb_cache_entryinfo_lock( ei2 );
376 /* Advance to next lower RDN */
377 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
378 && !DN_SEPARATOR(*ptr); ptr--);
379 if ( ptr >= ndn->bv_val ) {
380 if (DN_SEPARATOR(*ptr)) ptr++;
381 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
382 ei.bei_nrdn.bv_val = ptr;
384 if ( ptr < ndn->bv_val ) {
394 /* Walk up the tree from a child node, looking for an ID that's already
395 * been linked into the cache.
398 bdb_cache_find_parent(
406 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
407 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
415 rc = bdb_dn2id_parent( be, txn, &ei, &eip.bei_id, ctx );
418 /* Save the previous node, if any */
421 /* Create a new node for the current ID */
422 ein = bdb_cache_entryinfo_new();
423 ein->bei_id = ei.bei_id;
424 ein->bei_nrdn = ei.bei_nrdn;
425 ein->bei_rdn = ei.bei_rdn;
426 ein->bei_kids = ei.bei_kids;
428 /* This node is not fully connected yet */
429 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
431 /* If this is the first time, save this node
432 * to be returned later.
434 if ( eir == NULL ) eir = ein;
436 /* Insert this node into the ID tree */
437 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
438 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
439 bdb_id_cmp, avl_dup_error ) ) {
441 /* Hm, can this really happen? */
442 bdb_cache_entryinfo_destroy( ein );
443 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
444 (caddr_t) &ei, bdb_id_cmp );
445 bdb_cache_entryinfo_lock( ein );
446 avl_insert( &ein->bei_kids, (caddr_t)ei2, bdb_rdn_cmp,
448 bdb_cache_entryinfo_unlock( ein );
451 /* If there was a previous node, link it to this one */
452 if ( ei2 ) ei2->bei_parent = ein;
455 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
456 (caddr_t) &eip, bdb_id_cmp );
458 ei2 = &bdb->bi_cache.c_dntree;
462 ein->bei_parent = ei2;
463 bdb_cache_entryinfo_lock( ei2 );
464 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
466 bdb_cache_entryinfo_unlock( ei2 );
468 bdb_cache_entryinfo_lock( eir );
470 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
472 /* Found a link. Reset all the state info */
473 for (ein = eir; ein != ei2; ein=ein->bei_parent)
474 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
478 ei.bei_id = eip.bei_id;
479 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
487 * cache_find_entry_id - find an entry in the cache, given id.
488 * The entry is locked for Read upon return. Call with islocked TRUE if
489 * the supplied *eip was already locked.
493 bdb_cache_find_entry_id(
504 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
511 /* If we weren't given any info, see if we have it already cached */
513 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
514 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
515 (caddr_t) &ei, bdb_id_cmp );
517 bdb_cache_entryinfo_lock( *eip );
520 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
523 /* See if the ID exists in the database; add it to the cache if so */
526 rc = bdb_id2entry( be, tid, id, &ep );
528 rc = bdb_cache_find_entry_ndn2id( be, tid,
529 &ep->e_nname, eip, locker, ctx );
533 bdb_entry_return( ep );
538 rc = bdb_cache_find_parent(be, tid, id, eip, ctx );
539 if ( rc == 0 && *eip )
544 /* Ok, we found the info, do we have the entry? */
545 if ( *eip && rc == 0 ) {
546 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
548 } else if (!(*eip)->bei_e ) {
550 rc = bdb_id2entry( be, tid, id, &ep );
553 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
555 ep->e_private = *eip;
560 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
564 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
568 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
570 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
571 LRU_DELETE( &bdb->bi_cache, *eip );
572 LRU_ADD( &bdb->bi_cache, *eip );
573 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
577 bdb_cache_entryinfo_unlock( *eip );
591 if ( BEI(e)->bei_kids ) {
594 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
597 rc = bdb_dn2id_children( op, txn, e );
598 if ( rc == DB_NOTFOUND ) {
599 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
604 /* Update the cache after a successful database Add. */
607 struct bdb_info *bdb,
615 struct berval rdn = e->e_name;
621 if ( nrdn->bv_len != e->e_nname.bv_len ) {
622 char *ptr = strchr( rdn.bv_val, ',' );
623 rdn.bv_len = ptr - rdn.bv_val;
625 ber_dupbv( &ei.bei_rdn, &rdn );
626 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
629 new->bei_state = CACHE_ENTRY_NO_KIDS;
630 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
631 bdb_cache_entryinfo_unlock( eip );
632 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
645 EntryInfo *ei = BEI(e);
647 /* Get write lock on data */
648 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
650 /* If we've done repeated mods on a cached entry, then e_attrs
651 * is no longer contiguous with the entry, and must be freed.
653 if ( (void *)e->e_attrs != (void *)(e+1) ) {
654 attrs_free( e->e_attrs );
656 e->e_attrs = newAttrs;
662 * Change the rdn in the entryinfo. Also move to a new parent if needed.
675 EntryInfo *ei = BEI(e), *pei;
679 /* Get write lock on data */
680 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
682 /* If we've done repeated mods on a cached entry, then e_attrs
683 * is no longer contiguous with the entry, and must be freed.
685 if ( (void *)e->e_attrs != (void *)(e+1) ) {
686 attrs_free( e->e_attrs );
688 e->e_attrs = new->e_attrs;
690 ch_free(e->e_name.bv_val);
692 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
693 e->e_bv.bv_val + e->e_bv.bv_len ) {
694 ch_free(e->e_name.bv_val);
695 ch_free(e->e_nname.bv_val);
698 e->e_name = new->e_name;
699 e->e_nname = new->e_nname;
701 /* Lock the parent's kids AVL tree */
702 pei = ei->bei_parent;
703 bdb_cache_entryinfo_lock( pei );
704 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
705 free( ei->bei_nrdn.bv_val );
706 free( ei->bei_rdn.bv_val );
707 ber_dupbv( &ei->bei_nrdn, nrdn );
709 if ( nrdn->bv_len != e->e_nname.bv_len ) {
710 char *ptr = strchr(rdn.bv_val, ',');
711 rdn.bv_len = ptr - rdn.bv_val;
713 ber_dupbv( &ei->bei_rdn, &rdn );
716 ein = ei->bei_parent;
718 ei->bei_parent = ein;
719 bdb_cache_entryinfo_unlock( pei );
720 bdb_cache_entryinfo_lock( ein );
722 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
723 bdb_cache_entryinfo_unlock( ein );
727 * cache_delete_entry - delete the entry e from the cache.
729 * returns: 0 e was deleted ok
730 * 1 e was not in the cache
731 * -1 something bad happened
734 bdb_cache_delete_entry(
742 EntryInfo *ei = BEI(e);
745 assert( e->e_private );
747 /* Set this early, warn off any queriers */
748 ei->bei_state |= CACHE_ENTRY_DELETED;
750 /* Get write lock on the data */
751 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
753 /* set cache write lock */
754 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
756 /* Lock the parent's kids tree */
757 bdb_cache_entryinfo_lock( ei->bei_parent );
760 LDAP_LOG( CACHE, ENTRY,
761 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
763 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
768 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
769 rc = bdb_cache_delete_entry_internal( cache, e->e_private );
771 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
773 /* free cache write lock */
774 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
775 bdb_cache_entryinfo_unlock( ei->bei_parent );
776 bdb_cache_entryinfo_destroy( ei );
782 bdb_cache_delete_entry_internal(
787 int rc = 0; /* return code */
790 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
795 /* If parent has no more kids, put in on LRU list */
796 if ( e->bei_parent->bei_kids == NULL ) {
797 LRU_ADD( cache, e->bei_parent );
802 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
812 LRU_DELETE( cache, e );
816 * flag entry to be freed later by a call to cache_return_entry()
818 e->bei_state |= CACHE_ENTRY_DELETED;
824 bdb_entryinfo_release( void *data )
826 EntryInfo *ei = (EntryInfo *)data;
827 if ( ei->bei_kids ) {
828 avl_free( ei->bei_kids, NULL );
831 ei->bei_e->e_private = NULL;
832 bdb_entry_return( ei->bei_e );
834 bdb_cache_entryinfo_destroy( ei );
838 bdb_cache_release_all( Cache *cache )
840 /* set cache write lock */
841 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
843 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
846 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
848 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
851 avl_free( cache->c_dntree.bei_kids, NULL );
852 avl_free( cache->c_idtree, bdb_entryinfo_release );
853 cache->c_lruhead = NULL;
854 cache->c_lrutail = NULL;
857 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
858 /* free cache write lock */
859 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
864 bdb_lru_print( Cache *cache )
868 fprintf( stderr, "LRU queue (head to tail):\n" );
869 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
870 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
871 e->bei_nrdn.bv_val, e->bei_id );
873 fprintf( stderr, "LRU queue (tail to head):\n" );
874 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
875 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
876 e->bei_nrdn.bv_val, e->bei_id );
881 #ifdef BDB_REUSE_LOCKERS
883 bdb_locker_id_free( void *key, void *data )
886 int lockid = (int) data;
888 XLOCK_ID_FREE( env, lockid );
892 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
898 if ( !env || !locker ) return -1;
900 /* If no op was provided, try to find the ctx anyway... */
902 ctx = op->o_threadctx;
904 ctx = ldap_pvt_thread_pool_context();
907 /* Shouldn't happen unless we're single-threaded */
913 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
914 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
915 rc = XLOCK_ID( env, &lockid );
916 if (rc) ldap_pvt_thread_yield();
921 data = (void *)lockid;
922 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
923 data, bdb_locker_id_free ) ) ) {
924 XLOCK_ID_FREE( env, lockid );
926 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
927 db_strerror(rc), rc, 0 );
929 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
930 db_strerror(rc), rc, 0 );