1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 /* BDB backend specific entry info -- visible only to the cache */
21 typedef struct bdb_entry_info {
22 ldap_pvt_thread_rdwr_t bei_rdwr; /* reader/writer lock */
25 * remaining fields require backend cache lock to access
26 * These items are specific to the BDB backend and should
29 int bei_state; /* for the cache */
30 #define CACHE_ENTRY_UNDEFINED 0
31 #define CACHE_ENTRY_CREATING 1
32 #define CACHE_ENTRY_READY 2
33 #define CACHE_ENTRY_DELETED 3
34 #define CACHE_ENTRY_COMMITTED 4
36 int bei_refcnt; /* # threads ref'ing this entry */
37 Entry *bei_lrunext; /* for cache lru list */
41 #define BEI(e) ((EntryInfo *) ((e)->e_private))
43 static int bdb_cache_delete_entry_internal(Cache *cache, Entry *e);
45 static void bdb_lru_print(Cache *cache);
49 bdb_cache_entry_rdwr_lock(Entry *e, int rw)
52 LDAP_LOG( CACHE, ENTRY,
53 "bdb_cache_entry_rdwr_lock: %s lock on ID %ld\n",
54 rw ? "w" : "r", e->e_id, 0 );
56 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
57 rw ? "w" : "r", e->e_id, 0);
61 return ldap_pvt_thread_rdwr_wlock(&BEI(e)->bei_rdwr);
63 return ldap_pvt_thread_rdwr_rlock(&BEI(e)->bei_rdwr);
67 bdb_cache_entry_rdwr_trylock(Entry *e, int rw)
70 LDAP_LOG( CACHE, ENTRY,
71 "bdb_cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
72 rw ? "w" : "r", e->e_id, 0 );
74 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
75 rw ? "w" : "r", e->e_id, 0);
79 return ldap_pvt_thread_rdwr_wtrylock(&BEI(e)->bei_rdwr);
81 return ldap_pvt_thread_rdwr_rtrylock(&BEI(e)->bei_rdwr);
85 bdb_cache_entry_rdwr_unlock(Entry *e, int rw)
88 LDAP_LOG( CACHE, ENTRY,
89 "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
90 rw ? "w" : "r", e->e_id, 0 );
92 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
93 rw ? "w" : "r", e->e_id, 0);
97 return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr);
99 return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr);
103 bdb_cache_entry_rdwr_init(Entry *e)
105 return ldap_pvt_thread_rdwr_init( &BEI(e)->bei_rdwr );
109 bdb_cache_entry_rdwr_destroy(Entry *e)
111 return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr );
115 bdb_cache_entry_private_init( Entry *e )
117 assert( e->e_private == NULL );
119 if( e->e_private != NULL ) {
120 /* this should never happen */
124 e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info));
126 if( bdb_cache_entry_rdwr_init( e ) != 0 ) {
136 bdb_cache_entry_db_lock
137 ( DB_ENV *env, u_int32_t locker, Entry *e, int rw, u_int32_t flags, DB_LOCK *lock )
147 db_rw = DB_LOCK_WRITE;
149 db_rw = DB_LOCK_READ;
151 lockobj.data = e->e_nname.bv_val;
152 lockobj.size = e->e_nname.bv_len;
153 rc = LOCK_GET(env, locker, flags | DB_LOCK_NOWAIT,
154 &lockobj, db_rw, lock);
157 LDAP_LOG( CACHE, DETAIL1,
158 "bdb_cache_entry_db_lock: entry %s, rw %d, rc %d\n",
159 e->e_nname.bv_val, rw, rc );
161 Debug( LDAP_DEBUG_TRACE,
162 "bdb_cache_entry_db_lock: entry %s, rw %d, rc %d\n",
163 e->e_nname.bv_val, rw, rc );
167 #endif /* NO_THREADS */
171 bdb_cache_entry_db_unlock
172 ( DB_ENV *env, DB_LOCK *lock )
179 rc = LOCK_PUT ( env, lock );
185 * marks an entry in CREATING state as committed, so it is really returned
186 * to the cache. Otherwise an entry in CREATING state is removed.
187 * Makes e_private be destroyed at the following cache_return_entry_w,
188 * but lets the entry untouched (owned by someone else)
191 bdb_cache_entry_commit( Entry *e )
194 assert( e->e_private );
195 assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING );
196 /* assert( BEI(e)->bei_refcnt == 1 ); */
198 BEI(e)->bei_state = CACHE_ENTRY_COMMITTED;
202 bdb_cache_entry_private_destroy( Entry *e )
204 assert( e->e_private );
206 bdb_cache_entry_rdwr_destroy( e );
208 free( e->e_private );
214 bdb_unlocked_cache_return_entry_rw( Cache *cache, Entry *e, int rw )
218 int refcnt, freeit = 1;
220 /* set cache write lock */
221 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
223 assert( e->e_private );
226 bdb_cache_entry_rdwr_unlock(e, rw);
230 refcnt = --BEI(e)->bei_refcnt;
233 * if the entry is returned when in CREATING state, it is deleted
234 * but not freed because it may belong to someone else (do_add,
237 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
239 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
240 bdb_cache_delete_entry_internal( cache, e );
242 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
244 /* now the entry is in DELETED state */
247 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
248 BEI(e)->bei_state = CACHE_ENTRY_READY;
250 /* free cache write lock */
251 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
254 LDAP_LOG( CACHE, DETAIL1,
255 "bdb_unlocked_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
256 id, rw ? "w" : "r", refcnt );
258 Debug( LDAP_DEBUG_TRACE,
259 "====> bdb_unlocked_cache_return_entry_%s( %ld ): created (%d)\n",
260 rw ? "w" : "r", id, refcnt );
264 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
266 /* free cache write lock */
267 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
270 LDAP_LOG( CACHE, DETAIL1,
271 "bdb_unlocked_cache_return_entry_rw: %ld, delete pending (%d).\n",
274 Debug( LDAP_DEBUG_TRACE,
275 "====> bdb_unlocked_cache_return_entry_%s( %ld ): delete pending (%d)\n",
276 rw ? "w" : "r", id, refcnt );
280 bdb_cache_entry_private_destroy( e );
282 bdb_entry_return( e );
285 /* free cache write lock */
286 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
289 LDAP_LOG( CACHE, DETAIL1,
290 "bdb_unlocked_cache_return_entry_rw: (%ld): deleted (%d)\n",
293 Debug( LDAP_DEBUG_TRACE,
294 "====> bdb_unlocked_cache_return_entry_%s( %ld ): deleted (%d)\n",
295 rw ? "w" : "r", id, refcnt );
300 /* free cache write lock */
301 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
304 LDAP_LOG( CACHE, DETAIL1,
305 "bdb_unlocked_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
306 id, rw ? "w": "r", refcnt );
308 Debug( LDAP_DEBUG_TRACE,
309 "====> bdb_unlocked_cache_return_entry_%s( %ld ): returned (%d)\n",
310 rw ? "w" : "r", id, refcnt);
316 bdb_cache_return_entry_rw
317 ( DB_ENV *env, Cache *cache, Entry *e, int rw, DB_LOCK *lock )
320 int refcnt, freeit = 1;
322 /* set cache write lock */
323 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
325 assert( e->e_private );
327 bdb_cache_entry_db_unlock( env, lock );
329 bdb_cache_entry_rdwr_unlock(e, rw);
333 refcnt = --BEI(e)->bei_refcnt;
336 * if the entry is returned when in CREATING state, it is deleted
337 * but not freed because it may belong to someone else (do_add,
340 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
342 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
343 bdb_cache_delete_entry_internal( cache, e );
345 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
347 /* now the entry is in DELETED state */
350 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
351 BEI(e)->bei_state = CACHE_ENTRY_READY;
353 /* free cache write lock */
354 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
357 LDAP_LOG( CACHE, DETAIL1,
358 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
359 id, rw ? "w" : "r", refcnt );
361 Debug( LDAP_DEBUG_TRACE,
362 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
363 rw ? "w" : "r", id, refcnt );
367 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
369 /* free cache write lock */
370 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
373 LDAP_LOG( CACHE, DETAIL1,
374 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
377 Debug( LDAP_DEBUG_TRACE,
378 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
379 rw ? "w" : "r", id, refcnt );
383 bdb_cache_entry_private_destroy( e );
385 bdb_entry_return( e );
388 /* free cache write lock */
389 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
392 LDAP_LOG( CACHE, DETAIL1,
393 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
396 Debug( LDAP_DEBUG_TRACE,
397 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
398 rw ? "w" : "r", id, refcnt );
403 /* free cache write lock */
404 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
407 LDAP_LOG( CACHE, DETAIL1,
408 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
409 id, rw ? "w": "r", refcnt );
411 Debug( LDAP_DEBUG_TRACE,
412 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
413 rw ? "w" : "r", id, refcnt);
418 #define LRU_DELETE( cache, e ) do { \
419 if ( BEI(e)->bei_lruprev != NULL ) { \
420 BEI(BEI(e)->bei_lruprev)->bei_lrunext = BEI(e)->bei_lrunext; \
422 (cache)->c_lruhead = BEI(e)->bei_lrunext; \
424 if ( BEI(e)->bei_lrunext != NULL ) { \
425 BEI(BEI(e)->bei_lrunext)->bei_lruprev = BEI(e)->bei_lruprev; \
427 (cache)->c_lrutail = BEI(e)->bei_lruprev; \
431 #define LRU_ADD( cache, e ) do { \
432 BEI(e)->bei_lrunext = (cache)->c_lruhead; \
433 if ( BEI(e)->bei_lrunext != NULL ) { \
434 BEI(BEI(e)->bei_lrunext)->bei_lruprev = (e); \
436 (cache)->c_lruhead = (e); \
437 BEI(e)->bei_lruprev = NULL; \
438 if ( (cache)->c_lrutail == NULL ) { \
439 (cache)->c_lrutail = (e); \
444 * cache_add_entry_rw - create and lock an entry in the cache
445 * returns: 0 entry has been created and locked
446 * 1 entry already existed
447 * -1 something bad happened
448 * other Berkeley DB locking error code
451 bdb_cache_add_entry_rw(
464 LDAP_LOG( CACHE, ENTRY,
465 "bdb_cache_add_entry_rw: add (%s):%s to cache\n",
466 e->e_dn, rw ? "w" : "r", 0 );
468 /* set cache write lock */
469 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
471 assert( e->e_private == NULL );
473 if( bdb_cache_entry_private_init(e) != 0 ) {
474 /* free cache write lock */
475 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
478 LDAP_LOG( CACHE, ERR,
479 "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n",
480 e->e_dn, e->e_id, 0 );
482 Debug( LDAP_DEBUG_ANY,
483 "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n",
484 e->e_id, e->e_dn, 0 );
491 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
492 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
494 /* free cache write lock */
495 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
498 LDAP_LOG( CACHE, DETAIL1,
499 "bdb_cache_add_entry: (%s):%ld already in cache.\n",
500 e->e_dn, e->e_id, 0 );
502 Debug( LDAP_DEBUG_TRACE,
503 "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
504 e->e_id, e->e_dn, 0 );
507 bdb_cache_entry_private_destroy(e);
513 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
514 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
517 LDAP_LOG( CACHE, DETAIL1,
518 "bdb_cache_add_entry: (%s):%ls already in cache.\n",
519 e->e_dn, e->e_id, 0 );
521 Debug( LDAP_DEBUG_ANY,
522 "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n",
523 e->e_id, e->e_dn, 0 );
526 /* delete from dn tree inserted above */
527 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
528 (AVL_CMP) entry_dn_cmp ) == NULL )
531 LDAP_LOG( CACHE, INFO,
532 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
535 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
540 bdb_cache_entry_private_destroy(e);
542 /* free cache write lock */
543 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
547 rc = bdb_cache_entry_db_lock( env, locker, e, rw, 0, lock );
551 case DB_LOCK_DEADLOCK :
552 case DB_LOCK_NOTGRANTED :
553 /* undo avl changes immediately */
554 if ( avl_delete( &cache->c_idtree, (caddr_t) e,
555 (AVL_CMP) entry_id_cmp ) == NULL ) {
557 LDAP_LOG( CACHE, INFO,
558 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
561 Debug( LDAP_DEBUG_ANY, "====> can't delete from id cache\n", 0, 0, 0 );
564 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
565 (AVL_CMP) entry_dn_cmp ) == NULL ) {
567 LDAP_LOG( CACHE, INFO,
568 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
571 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 );
576 bdb_cache_entry_private_destroy(e);
577 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
581 /* put the entry into 'CREATING' state */
582 /* will be marked after when entry is returned */
583 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
584 BEI(e)->bei_refcnt = 1;
587 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
590 if ( ++cache->c_cursize > cache->c_maxsize ) {
592 * find the lru entry not currently in use and delete it.
593 * in case a lot of entries are in use, only look at the
594 * first 10 on the tail of the list.
597 while ( cache->c_lrutail != NULL &&
598 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
601 /* move this in-use entry to the front of the q */
602 ee = cache->c_lrutail;
603 LRU_DELETE( cache, ee );
604 LRU_ADD( cache, ee );
609 * found at least one to delete - try to get back under
610 * the max cache size.
612 while ( cache->c_lrutail != NULL &&
613 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
614 cache->c_cursize > cache->c_maxsize )
616 e = cache->c_lrutail;
618 /* delete from cache and lru q */
619 /* XXX do we need rc ? */
620 rc = bdb_cache_delete_entry_internal( cache, e );
621 bdb_cache_entry_private_destroy( e );
622 bdb_entry_return( e );
627 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
628 /* free cache write lock */
629 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
634 * cache_update_entry - update a LOCKED entry which has been deleted.
635 * returns: 0 entry has been created and locked
636 * 1 entry already existed
637 * -1 something bad happened
640 bdb_cache_update_entry(
648 /* set cache write lock */
649 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
651 assert( e->e_private );
653 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
654 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
657 LDAP_LOG( CACHE, DETAIL1,
658 "bdb_cache_update_entry: (%s):%ld already in dn cache\n",
659 e->e_dn, e->e_id, 0 );
661 Debug( LDAP_DEBUG_TRACE,
662 "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n",
663 e->e_id, e->e_dn, 0 );
666 /* free cache write lock */
667 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
672 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
673 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
676 LDAP_LOG( CACHE, DETAIL1,
677 "bdb_cache_update_entry: (%s)%ld already in id cache\n",
678 e->e_dn, e->e_id, 0 );
680 Debug( LDAP_DEBUG_ANY,
681 "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n",
682 e->e_id, e->e_dn, 0 );
685 /* delete from dn tree inserted above */
686 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
687 (AVL_CMP) entry_dn_cmp ) == NULL )
690 LDAP_LOG( CACHE, INFO,
691 "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n",
692 e->e_dn, e->e_id, 0 );
694 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
699 /* free cache write lock */
700 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
705 /* put the entry into 'CREATING' state */
706 /* will be marked after when entry is returned */
707 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
710 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
713 if ( ++cache->c_cursize > cache->c_maxsize ) {
715 * find the lru entry not currently in use and delete it.
716 * in case a lot of entries are in use, only look at the
717 * first 10 on the tail of the list.
720 while ( cache->c_lrutail != NULL &&
721 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
724 /* move this in-use entry to the front of the q */
725 ee = cache->c_lrutail;
726 LRU_DELETE( cache, ee );
727 LRU_ADD( cache, ee );
732 * found at least one to delete - try to get back under
733 * the max cache size.
735 while ( cache->c_lrutail != NULL &&
736 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
737 cache->c_cursize > cache->c_maxsize )
739 e = cache->c_lrutail;
741 /* delete from cache and lru q */
742 /* XXX do we need rc ? */
743 rc = bdb_cache_delete_entry_internal( cache, e );
744 bdb_cache_entry_private_destroy( e );
745 bdb_entry_return( e );
750 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
751 /* free cache write lock */
752 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
757 bdb_cache_find_entry_ndn2id(
767 /* this function is always called with normalized DN */
771 /* set cache read lock */
772 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
774 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
775 (AVL_CMP) entry_dn_cmp )) != NULL )
781 * ep now points to an unlocked entry
782 * we do not need to lock the entry if we only
783 * check the state, refcnt, LRU, and id.
786 assert( ep->e_private );
790 state = BEI(ep)->bei_state;
793 * entry is deleted or not fully created yet
795 if ( state != CACHE_ENTRY_READY ) {
796 assert(state != CACHE_ENTRY_UNDEFINED);
798 /* free cache read lock */
799 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
802 LDAP_LOG( CACHE, INFO,
803 "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
804 ndn->bv_val, id, state );
806 Debug(LDAP_DEBUG_TRACE,
807 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
808 ndn->bv_val, id, state);
812 ldap_pvt_thread_yield();
816 /* free cache read lock */
817 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
820 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
823 LRU_DELETE( cache, ep );
824 LRU_ADD( cache, ep );
827 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
830 LDAP_LOG( CACHE, DETAIL1,
831 "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n",
832 ndn->bv_val, id, count );
834 Debug(LDAP_DEBUG_TRACE,
835 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
836 ndn->bv_val, id, count);
840 /* free cache read lock */
841 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
850 * cache_find_entry_id - find an entry in the cache, given id
854 bdb_cache_find_entry_id(
871 /* set cache read lock */
872 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
874 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
875 (AVL_CMP) entry_id_cmp )) != NULL )
882 assert( ep->e_private );
885 state = BEI(ep)->bei_state;
888 * entry is deleted or not fully created yet
890 if ( state != CACHE_ENTRY_READY ) {
892 assert(state != CACHE_ENTRY_UNDEFINED);
894 /* free cache read lock */
895 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
898 LDAP_LOG( CACHE, INFO,
899 "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
903 Debug(LDAP_DEBUG_TRACE,
904 "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
908 ldap_pvt_thread_yield();
912 /* acquire reader lock */
913 rc = bdb_cache_entry_db_lock ( env, locker, ep, rw, 0, lock );
916 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
919 if ( rc ) { /* will be changed to retry beyond threshold */
920 /* could not acquire entry lock...
921 * owner cannot free as we have the cache locked.
922 * so, unlock the cache, yield, and try again.
925 /* free cache read lock */
926 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
929 LDAP_LOG( CACHE, INFO,
930 "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n",
933 Debug(LDAP_DEBUG_TRACE,
934 "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n",
936 Debug(LDAP_DEBUG_TRACE,
941 ldap_pvt_thread_yield();
945 /* free cache read lock */
946 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
948 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
950 LRU_DELETE( cache, ep );
951 LRU_ADD( cache, ep );
953 BEI(ep)->bei_refcnt++;
956 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
959 LDAP_LOG( CACHE, DETAIL1,
960 "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n",
961 ep_id, ep->e_dn, count );
963 Debug(LDAP_DEBUG_TRACE,
964 "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
965 ep_id, ep->e_dn, count);
972 /* free cache read lock */
973 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
979 * cache_delete_entry - delete the entry e from the cache. the caller
980 * should have obtained e (increasing its ref count) via a call to one
981 * of the cache_find_* routines. the caller should *not* call the
982 * cache_return_entry() routine prior to calling cache_delete_entry().
983 * it performs this function.
985 * returns: 0 e was deleted ok
986 * 1 e was not in the cache
987 * -1 something bad happened
990 bdb_cache_delete_entry(
997 /* set cache write lock */
998 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1000 assert( e->e_private );
1003 LDAP_LOG( CACHE, ENTRY,
1004 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
1006 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
1011 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1012 rc = bdb_cache_delete_entry_internal( cache, e );
1013 /* free lru mutex */
1014 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1016 /* free cache write lock */
1017 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1022 bdb_cache_delete_entry_internal(
1027 int rc = 0; /* return code */
1030 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
1037 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
1048 LRU_DELETE( cache, e );
1052 * flag entry to be freed later by a call to cache_return_entry()
1054 BEI(e)->bei_state = CACHE_ENTRY_DELETED;
1060 bdb_cache_release_all( Cache *cache )
1065 /* set cache write lock */
1066 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1068 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1071 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1073 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1076 while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) {
1077 #ifdef LDAP_RDWR_DEBUG
1078 assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr));
1081 /* delete from cache and lru q */
1082 /* XXX do we need rc ? */
1083 rc = bdb_cache_delete_entry_internal( cache, e );
1084 bdb_cache_entry_private_destroy( e );
1085 bdb_entry_return( e );
1088 if ( cache->c_cursize ) {
1090 LDAP_LOG( CACHE, INFO,
1091 "bdb_cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0 );
1093 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
1098 /* free lru mutex */
1099 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1100 /* free cache write lock */
1101 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1106 bdb_lru_print( Cache *cache )
1110 fprintf( stderr, "LRU queue (head to tail):\n" );
1111 for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) {
1112 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1113 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
1115 fprintf( stderr, "LRU queue (tail to head):\n" );
1116 for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) {
1117 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1118 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
1123 #ifdef BDB_REUSE_LOCKERS
1125 bdb_locker_id_free( void *key, void *data )
1128 int lockid = (int) data;
1130 XLOCK_ID_FREE( env, lockid );
1134 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1139 if ( !env || !op || !locker ) return -1;
1141 /* Shouldn't happen unless we're single-threaded */
1142 if ( !op->o_threadctx ) {
1147 if ( ldap_pvt_thread_pool_getkey( op->o_threadctx, env, &data, NULL ) ) {
1148 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1149 rc = XLOCK_ID( env, &lockid );
1150 if (rc) ldap_pvt_thread_yield();
1155 data = (void *)lockid;
1156 if ( ( rc = ldap_pvt_thread_pool_setkey( op->o_threadctx, env,
1157 data, bdb_locker_id_free ) ) ) {
1158 XLOCK_ID_FREE( env, lockid );
1160 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1161 db_strerror(rc), rc, 0 );
1163 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1164 db_strerror(rc), rc, 0 );