1 /* cache.c - routines to maintain an in-core cache of entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
32 #define bdb_cache_lru_add hdb_cache_lru_add
34 static void bdb_cache_lru_add( struct bdb_info *bdb, EntryInfo *ei );
36 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
39 static void bdb_lru_print(Cache *cache);
44 bdb_cache_entryinfo_new( Cache *cache )
48 if ( cache->c_eifree ) {
49 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
50 if ( cache->c_eifree ) {
52 cache->c_eifree = ei->bei_lrunext;
54 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
57 ei->bei_lrunext = NULL;
60 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
61 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
67 /* Atomically release and reacquire a lock */
69 bdb_cache_entry_db_relock(
84 if ( !lock ) return 0;
86 lockobj.data = &ei->bei_id;
87 lockobj.size = sizeof(ei->bei_id) + 1;
89 list[0].op = DB_LOCK_PUT;
91 list[1].op = DB_LOCK_GET;
93 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
94 list[1].obj = &lockobj;
95 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
99 Debug( LDAP_DEBUG_TRACE,
100 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
101 ei->bei_id, rw, rc );
103 *lock = list[1].lock;
110 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
111 int rw, int tryOnly, DB_LOCK *lock )
120 if ( !lock ) return 0;
123 db_rw = DB_LOCK_WRITE;
125 db_rw = DB_LOCK_READ;
127 lockobj.data = &ei->bei_id;
128 lockobj.size = sizeof(ei->bei_id) + 1;
130 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
131 &lockobj, db_rw, lock);
132 if (rc && !tryOnly) {
133 Debug( LDAP_DEBUG_TRACE,
134 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
135 ei->bei_id, rw, rc );
138 #endif /* NO_THREADS */
142 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
149 if ( !lock ) return 0;
151 rc = LOCK_PUT ( env, lock );
157 bdb_cache_entryinfo_destroy( EntryInfo *e )
159 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
160 free( e->bei_nrdn.bv_val );
162 free( e->bei_rdn.bv_val );
168 #define LRU_DELETE( cache, ei ) do { \
169 if ( (ei)->bei_lruprev != NULL ) { \
170 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
172 (cache)->c_lruhead = (ei)->bei_lrunext; \
174 if ( (ei)->bei_lrunext != NULL ) { \
175 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
177 (cache)->c_lrutail = (ei)->bei_lruprev; \
179 (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
182 #define LRU_ADD( cache, ei ) do { \
183 (ei)->bei_lrunext = (cache)->c_lruhead; \
184 if ( (ei)->bei_lrunext != NULL ) { \
185 (ei)->bei_lrunext->bei_lruprev = (ei); \
187 (cache)->c_lruhead = (ei); \
188 (ei)->bei_lruprev = NULL; \
189 if ( (cache)->c_lrutail == NULL ) { \
190 (cache)->c_lrutail = (ei); \
194 /* Do a length-ordered sort on normalized RDNs */
196 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
198 const EntryInfo *e1 = v_e1, *e2 = v_e2;
199 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
201 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
202 e1->bei_nrdn.bv_len );
208 bdb_id_cmp( const void *v_e1, const void *v_e2 )
210 const EntryInfo *e1 = v_e1, *e2 = v_e2;
211 return e1->bei_id - e2->bei_id;
214 /* Create an entryinfo in the cache. Caller must release the locks later.
217 bdb_entryinfo_add_internal(
218 struct bdb_info *bdb,
222 EntryInfo *ei2 = NULL;
226 ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
228 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
229 bdb_cache_entryinfo_lock( ei->bei_parent );
231 ei2->bei_id = ei->bei_id;
232 ei2->bei_parent = ei->bei_parent;
234 ei2->bei_rdn = ei->bei_rdn;
236 #ifdef SLAP_ZONE_ALLOC
240 /* Add to cache ID tree */
241 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
243 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
244 bdb_cache_entryinfo_destroy( ei2 );
247 /* It got freed above because its value was
250 ei->bei_rdn.bv_val = NULL;
253 bdb->bi_cache.c_eiused++;
254 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
256 /* This is a new leaf node. But if parent had no kids, then it was
257 * a leaf and we would be decrementing that. So, only increment if
258 * the parent already has kids.
260 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
261 bdb->bi_cache.c_leaves++;
262 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
265 ei->bei_parent->bei_ckids++;
273 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
274 * the info for its closest ancestor. *res should be NULL to process a
275 * complete DN starting from the tree root. Otherwise *res must be the
276 * immediate parent of the requested DN, and only the RDN will be searched.
277 * The EntryInfo is locked upon return and must be unlocked by the caller.
286 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
287 EntryInfo ei, *eip, *ei2;
291 /* this function is always called with normalized DN */
293 /* we're doing a onelevel search for an RDN */
294 ei.bei_nrdn.bv_val = ndn->bv_val;
295 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
298 /* we're searching a full DN from the root */
299 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
300 ei.bei_nrdn.bv_val = ptr;
301 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
302 /* Skip to next rdn if suffix is empty */
303 if ( ei.bei_nrdn.bv_len == 0 ) {
304 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
305 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
306 if ( ptr >= ndn->bv_val ) {
307 if (DN_SEPARATOR(*ptr)) ptr++;
308 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
309 ei.bei_nrdn.bv_val = ptr;
312 eip = &bdb->bi_cache.c_dntree;
315 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
317 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
319 int len = ei.bei_nrdn.bv_len;
321 if ( BER_BVISEMPTY( ndn )) {
326 ei.bei_nrdn.bv_len = ndn->bv_len -
327 (ei.bei_nrdn.bv_val - ndn->bv_val);
328 bdb_cache_entryinfo_unlock( eip );
330 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
332 bdb_cache_entryinfo_lock( eip );
337 /* DN exists but needs to be added to cache */
338 ei.bei_nrdn.bv_len = len;
339 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
340 /* add_internal left eip and c_rwlock locked */
341 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
346 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
347 /* In the midst of deleting? Give it a chance to
350 bdb_cache_entryinfo_unlock( eip );
351 ldap_pvt_thread_yield();
352 bdb_cache_entryinfo_lock( eip );
356 bdb_cache_entryinfo_unlock( eip );
357 bdb_cache_entryinfo_lock( ei2 );
361 /* Advance to next lower RDN */
362 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
363 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
364 if ( ptr >= ndn->bv_val ) {
365 if (DN_SEPARATOR(*ptr)) ptr++;
366 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
367 ei.bei_nrdn.bv_val = ptr;
369 if ( ptr < ndn->bv_val ) {
379 /* Walk up the tree from a child node, looking for an ID that's already
380 * been linked into the cache.
383 hdb_cache_find_parent(
390 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
391 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
400 rc = hdb_dn2id_parent( op, txn, locker, &ei, &eip.bei_id );
403 /* Save the previous node, if any */
406 /* Create a new node for the current ID */
407 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
408 ein->bei_id = ei.bei_id;
409 ein->bei_kids = ei.bei_kids;
410 ein->bei_nrdn = ei.bei_nrdn;
411 ein->bei_rdn = ei.bei_rdn;
412 ein->bei_ckids = ei.bei_ckids;
413 #ifdef SLAP_ZONE_ALLOC
418 /* This node is not fully connected yet */
419 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
421 /* Insert this node into the ID tree */
422 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
423 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
424 bdb_id_cmp, avl_dup_error ) ) {
426 /* Someone else created this node just before us.
427 * Free our new copy and use the existing one.
429 bdb_cache_entryinfo_destroy( ein );
430 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
431 (caddr_t) &ei, bdb_id_cmp );
433 /* Link in any kids we've already processed */
435 bdb_cache_entryinfo_lock( ein );
436 avl_insert( &ein->bei_kids, (caddr_t)ei2,
437 bdb_rdn_cmp, avl_dup_error );
439 bdb_cache_entryinfo_unlock( ein );
445 /* If this is the first time, save this node
446 * to be returned later.
448 if ( eir == NULL ) eir = ein;
450 /* If there was a previous node, link it to this one */
451 if ( ei2 ) ei2->bei_parent = ein;
453 /* Look for this node's parent */
455 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
456 (caddr_t) &eip, bdb_id_cmp );
458 ei2 = &bdb->bi_cache.c_dntree;
460 bdb->bi_cache.c_eiused++;
461 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
462 bdb->bi_cache.c_leaves++;
463 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
466 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
467 bdb_cache_lru_add( bdb, ein );
471 /* Got the parent, link in and we're done. */
473 bdb_cache_entryinfo_lock( ei2 );
474 ein->bei_parent = ei2;
475 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
478 bdb_cache_entryinfo_unlock( ei2 );
479 bdb_cache_entryinfo_lock( eir );
481 /* Reset all the state info */
482 for (ein = eir; ein != ei2; ein=ein->bei_parent)
483 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
488 ei.bei_id = eip.bei_id;
490 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
496 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
500 struct bdb_info *bdb,
507 /* See if we already have this one */
508 bdb_cache_entryinfo_lock( ei->bei_parent );
509 ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
510 bdb_cache_entryinfo_unlock( ei->bei_parent );
513 /* Not found, add it */
516 /* bei_rdn was not malloc'd before, do it now */
517 ber_dupbv( &bv, &ei->bei_rdn );
520 rc = bdb_entryinfo_add_internal( bdb, ei, res );
521 bdb_cache_entryinfo_unlock( ei->bei_parent );
522 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
524 /* Found, return it */
533 bdb_cache_lru_purge(void *ctx, void *arg)
535 struct re_s *rtask = arg;
536 struct bdb_info *bdb = rtask->arg;
537 DB_LOCK lock, *lockp;
538 EntryInfo *elru, *elprev;
541 if ( bdb->bi_cache.c_locker ) {
547 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_tail_mutex );
549 /* Look for an unused entry to remove */
550 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev ) {
551 elprev = elru->bei_lruprev;
553 /* If we can successfully writelock it, then
554 * the object is idle.
556 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv,
557 bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
561 /* If this node is in the process of linking into the cache,
562 * or this node is being deleted, skip it.
564 if ( elru->bei_state &
565 ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED )) {
566 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
569 /* Free entry for this node if it's present */
571 elru->bei_e->e_private = NULL;
572 #ifdef SLAP_ZONE_ALLOC
573 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
575 bdb_entry_return( elru->bei_e );
580 /* ITS#4010 if we're in slapcat, and this node is a leaf
583 * FIXME: we need to do this for slapd as well, (which is
584 * why we compute bi_cache.c_leaves now) but at the moment
585 * we can't because it causes unresolvable deadlocks.
587 if ( slapMode & SLAP_TOOL_READONLY ) {
588 if ( !elru->bei_kids ) {
589 /* This does LRU_DELETE for us */
590 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
591 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
593 /* Leave node on LRU list for a future pass */
595 LRU_DELETE( &bdb->bi_cache, elru );
597 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
599 if ( count == bdb->bi_cache.c_minfree ) {
600 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
601 bdb->bi_cache.c_cursize -= bdb->bi_cache.c_minfree;
602 if ( bdb->bi_cache.c_maxsize - bdb->bi_cache.c_cursize >=
603 bdb->bi_cache.c_minfree )
606 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
612 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_tail_mutex );
614 /* If we're running as a task, drop the task */
616 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
617 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
618 /* Defer processing till we're needed again */
619 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 1 );
620 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
624 /* caller must have lru_head_mutex locked. mutex
625 * will be unlocked on return.
629 struct bdb_info *bdb,
632 LRU_ADD( &bdb->bi_cache, ei );
633 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_head_mutex );
635 /* See if we're above the cache size limit */
636 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
637 if ( slapMode & SLAP_TOOL_MODE ) {
641 bdb_cache_lru_purge( NULL, &rtask );
643 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
644 if ( bdb->bi_cache_task ) {
645 if ( !ldap_pvt_runqueue_isrunning( &slapd_rq,
646 bdb->bi_cache_task ))
647 ldap_pvt_runqueue_resched( &slapd_rq, bdb->bi_cache_task,
650 bdb->bi_cache_task = ldap_pvt_runqueue_insert( &slapd_rq, 0,
651 bdb_cache_lru_purge, bdb, "bdb_cache_lru_purge",
652 bdb->bi_dbenv_home );
654 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
661 struct bdb_info *bdb,
664 EntryInfo ei = { 0 },
669 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
670 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
671 (caddr_t) &ei, bdb_id_cmp );
672 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
677 * cache_find_id - find an entry in the cache, given id.
678 * The entry is locked for Read upon return. Call with islocked TRUE if
679 * the supplied *eip was already locked.
692 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
694 int rc = 0, load = 0;
695 EntryInfo ei = { 0 };
699 #ifdef SLAP_ZONE_ALLOC
700 slap_zh_rlock(bdb->bi_cache.c_zctx);
702 /* If we weren't given any info, see if we have it already cached */
704 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
705 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
706 (caddr_t) &ei, bdb_id_cmp );
708 /* If the lock attempt fails, the info is in use */
709 if ( ldap_pvt_thread_mutex_trylock(
710 &(*eip)->bei_kids_mutex )) {
711 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
712 /* If this node is being deleted, treat
713 * as if the delete has already finished
715 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
718 /* otherwise, wait for the info to free up */
719 ldap_pvt_thread_yield();
722 /* If this info isn't hooked up to its parent yet,
723 * unlock and wait for it to be fully initialized
725 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
726 bdb_cache_entryinfo_unlock( *eip );
727 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
728 ldap_pvt_thread_yield();
733 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
736 /* See if the ID exists in the database; add it to the cache if so */
739 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
741 rc = bdb_cache_find_ndn( op, tid,
743 if ( *eip ) islocked = 1;
745 #ifdef SLAP_ZONE_ALLOC
746 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
748 bdb_entry_return( ep );
754 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
755 if ( rc == 0 && *eip ) islocked = 1;
759 /* Ok, we found the info, do we have the entry? */
760 if ( *eip && rc == 0 ) {
761 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
764 /* Make sure only one thread tries to load the entry */
766 #ifdef SLAP_ZONE_ALLOC
767 if ((*eip)->bei_e && !slap_zn_validate(
768 bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
769 (*eip)->bei_e = NULL;
770 (*eip)->bei_zseq = 0;
773 if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
775 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
778 bdb_cache_entryinfo_unlock( *eip );
781 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
782 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
784 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
785 } else if ( rc == 0 ) {
787 /* Give up original read lock, obtain write lock
790 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
793 if ( rc == 0 && !ep) {
794 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
797 ep->e_private = *eip;
802 #ifdef SLAP_ZONE_ALLOC
803 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
807 (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
809 /* If we succeeded, downgrade back to a readlock. */
810 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
813 /* Otherwise, release the lock. */
814 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
816 } else if ( !(*eip)->bei_e ) {
817 /* Some other thread is trying to load the entry,
818 * give it a chance to finish.
820 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
821 ldap_pvt_thread_yield();
822 bdb_cache_entryinfo_lock( *eip );
827 /* Check for subtree renames
829 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
831 bdb_cache_entry_db_relock( bdb->bi_dbenv,
832 locker, *eip, 1, 0, lock );
833 /* check again in case other modifier did it already */
834 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
835 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
836 bdb_cache_entry_db_relock( bdb->bi_dbenv,
837 locker, *eip, 0, 0, lock );
846 bdb_cache_entryinfo_unlock( *eip );
849 #ifdef SLAP_ZONE_ALLOC
850 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
852 bdb_entry_return( ep );
858 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
859 bdb->bi_cache.c_cursize++;
860 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
863 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
865 /* If the LRU list has only one entry and this is it, it
866 * doesn't need to be added again.
868 if ( bdb->bi_cache.c_lruhead == bdb->bi_cache.c_lrutail &&
869 bdb->bi_cache.c_lruhead == *eip ) {
870 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_head_mutex );
872 /* if entry is on LRU list, remove from old spot */
873 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
874 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_tail_mutex );
875 LRU_DELETE( &bdb->bi_cache, *eip );
876 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_tail_mutex );
878 /* lru_head_mutex is unlocked for us */
879 bdb_cache_lru_add( bdb, *eip );
883 #ifdef SLAP_ZONE_ALLOC
884 if (rc == 0 && (*eip)->bei_e) {
885 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
887 slap_zh_runlock(bdb->bi_cache.c_zctx);
900 if ( BEI(e)->bei_kids ) {
903 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
906 rc = bdb_dn2id_children( op, txn, e );
907 if ( rc == DB_NOTFOUND ) {
908 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
913 /* Update the cache after a successful database Add. */
916 struct bdb_info *bdb,
926 struct berval rdn = e->e_name;
934 /* Lock this entry so that bdb_add can run to completion.
935 * It can only fail if BDB has run out of lock resources.
937 rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
939 bdb_cache_entryinfo_unlock( eip );
944 if ( nrdn->bv_len != e->e_nname.bv_len ) {
945 char *ptr = ber_bvchr( &rdn, ',' );
946 assert( ptr != NULL );
947 rdn.bv_len = ptr - rdn.bv_val;
949 ber_dupbv( &ei.bei_rdn, &rdn );
950 if ( eip->bei_dkids ) eip->bei_dkids++;
953 rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
954 /* bdb_csn_commit can cause this when adding the database root entry */
956 new->bei_e->e_private = NULL;
957 #ifdef SLAP_ZONE_ALLOC
958 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
960 bdb_entry_return( new->bei_e );
965 new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
966 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
967 if (eip->bei_parent) {
968 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
970 bdb_cache_entryinfo_unlock( eip );
972 ++bdb->bi_cache.c_cursize;
973 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
976 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex );
978 /* lru_head_mutex is unlocked for us */
979 bdb_cache_lru_add( bdb, new );
992 EntryInfo *ei = BEI(e);
994 /* Get write lock on data */
995 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
997 /* If we've done repeated mods on a cached entry, then e_attrs
998 * is no longer contiguous with the entry, and must be freed.
1001 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1002 attrs_free( e->e_attrs );
1004 e->e_attrs = newAttrs;
1010 * Change the rdn in the entryinfo. Also move to a new parent if needed.
1014 struct bdb_info *bdb,
1016 struct berval *nrdn,
1022 EntryInfo *ei = BEI(e), *pei;
1028 /* Get write lock on data */
1029 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
1030 if ( rc ) return rc;
1032 /* If we've done repeated mods on a cached entry, then e_attrs
1033 * is no longer contiguous with the entry, and must be freed.
1035 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1036 attrs_free( e->e_attrs );
1038 e->e_attrs = new->e_attrs;
1039 if( e->e_nname.bv_val < e->e_bv.bv_val ||
1040 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1042 ch_free(e->e_name.bv_val);
1043 ch_free(e->e_nname.bv_val);
1045 e->e_name = new->e_name;
1046 e->e_nname = new->e_nname;
1048 /* Lock the parent's kids AVL tree */
1049 pei = ei->bei_parent;
1050 bdb_cache_entryinfo_lock( pei );
1051 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1052 free( ei->bei_nrdn.bv_val );
1053 ber_dupbv( &ei->bei_nrdn, nrdn );
1055 free( ei->bei_rdn.bv_val );
1058 if ( nrdn->bv_len != e->e_nname.bv_len ) {
1059 char *ptr = ber_bvchr(&rdn, ',');
1060 assert( ptr != NULL );
1061 rdn.bv_len = ptr - rdn.bv_val;
1063 ber_dupbv( &ei->bei_rdn, &rdn );
1067 ein = ei->bei_parent;
1069 ei->bei_parent = ein;
1070 bdb_cache_entryinfo_unlock( pei );
1071 bdb_cache_entryinfo_lock( ein );
1075 /* Record the generation number of this change */
1076 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1078 ei->bei_modrdns = bdb->bi_modrdns;
1079 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1082 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1083 bdb_cache_entryinfo_unlock( ein );
1087 * cache_delete - delete the entry e from the cache.
1089 * returns: 0 e was deleted ok
1090 * 1 e was not in the cache
1091 * -1 something bad happened
1101 EntryInfo *ei = BEI(e);
1104 assert( e->e_private != NULL );
1106 /* Set this early, warn off any queriers */
1107 ei->bei_state |= CACHE_ENTRY_DELETED;
1109 /* Lock the entry's info */
1110 bdb_cache_entryinfo_lock( ei );
1112 /* Get write lock on the data */
1113 rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1115 /* couldn't lock, undo and give up */
1116 ei->bei_state ^= CACHE_ENTRY_DELETED;
1117 bdb_cache_entryinfo_unlock( ei );
1121 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1125 ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex );
1127 /* set cache write lock */
1128 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1130 rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1132 /* free cache write lock */
1133 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1135 /* free lru mutex */
1136 ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex );
1138 /* Leave entry info locked */
1144 bdb_cache_delete_cleanup(
1149 ei->bei_e->e_private = NULL;
1150 #ifdef SLAP_ZONE_ALLOC
1151 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1153 bdb_entry_return( ei->bei_e );
1158 free( ei->bei_nrdn.bv_val );
1159 ei->bei_nrdn.bv_val = NULL;
1161 free( ei->bei_rdn.bv_val );
1162 ei->bei_rdn.bv_val = NULL;
1163 ei->bei_modrdns = 0;
1167 ei->bei_parent = NULL;
1168 ei->bei_kids = NULL;
1169 ei->bei_lruprev = NULL;
1171 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1172 ei->bei_lrunext = cache->c_eifree;
1173 cache->c_eifree = ei;
1174 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1175 bdb_cache_entryinfo_unlock( ei );
1179 bdb_cache_delete_internal(
1184 int rc = 0; /* return code */
1186 /* Lock the parent's kids tree */
1187 bdb_cache_entryinfo_lock( e->bei_parent );
1190 e->bei_parent->bei_ckids--;
1191 if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1194 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1199 if ( e->bei_parent->bei_kids )
1203 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1211 LRU_DELETE( cache, e );
1212 if ( e->bei_e ) cache->c_cursize--;
1215 bdb_cache_entryinfo_unlock( e->bei_parent );
1221 bdb_entryinfo_release( void *data )
1223 EntryInfo *ei = (EntryInfo *)data;
1224 if ( ei->bei_kids ) {
1225 avl_free( ei->bei_kids, NULL );
1228 ei->bei_e->e_private = NULL;
1229 #ifdef SLAP_ZONE_ALLOC
1230 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1232 bdb_entry_return( ei->bei_e );
1235 bdb_cache_entryinfo_destroy( ei );
1239 bdb_cache_release_all( Cache *cache )
1241 /* set cache write lock */
1242 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1244 ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex );
1246 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1248 avl_free( cache->c_dntree.bei_kids, NULL );
1249 avl_free( cache->c_idtree, bdb_entryinfo_release );
1250 for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1251 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1252 bdb_cache_entryinfo_destroy(cache->c_eifree);
1254 cache->c_cursize = 0;
1255 cache->c_eiused = 0;
1256 cache->c_leaves = 0;
1257 cache->c_idtree = NULL;
1258 cache->c_lruhead = NULL;
1259 cache->c_lrutail = NULL;
1260 cache->c_dntree.bei_kids = NULL;
1262 /* free lru mutex */
1263 ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex );
1264 /* free cache write lock */
1265 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1271 bdb_lru_print( Cache *cache )
1275 fprintf( stderr, "LRU queue (head to tail):\n" );
1276 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1277 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1278 e->bei_nrdn.bv_val, e->bei_id );
1280 fprintf( stderr, "LRU queue (tail to head):\n" );
1281 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1282 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1283 e->bei_nrdn.bv_val, e->bei_id );
1289 #ifdef BDB_REUSE_LOCKERS
1291 bdb_locker_id_free( void *key, void *data )
1294 u_int32_t lockid = (long)data;
1297 rc = XLOCK_ID_FREE( env, lockid );
1298 if ( rc == EINVAL ) {
1300 Debug( LDAP_DEBUG_ANY,
1301 "bdb_locker_id_free: %lu err %s(%d)\n",
1302 (unsigned long) lockid, db_strerror(rc), rc );
1303 /* release all locks held by this locker. */
1304 lr.op = DB_LOCK_PUT_ALL;
1306 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1307 XLOCK_ID_FREE( env, lockid );
1312 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1319 if ( !env || !locker ) return -1;
1321 /* If no op was provided, try to find the ctx anyway... */
1323 ctx = op->o_threadctx;
1325 ctx = ldap_pvt_thread_pool_context();
1328 /* Shouldn't happen unless we're single-threaded */
1334 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1335 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1336 rc = XLOCK_ID( env, &lockid );
1337 if (rc) ldap_pvt_thread_yield();
1342 data = (void *)((long)lockid);
1343 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1344 data, bdb_locker_id_free ) ) ) {
1345 XLOCK_ID_FREE( env, lockid );
1346 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1347 db_strerror(rc), rc, 0 );
1352 lockid = (long)data;
1357 #endif /* BDB_REUSE_LOCKERS */
1360 bdb_cache_delete_entry(
1361 struct bdb_info *bdb,
1366 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1367 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1369 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1370 LRU_DELETE( &bdb->bi_cache, ei );
1371 ei->bei_e->e_private = NULL;
1372 #ifdef SLAP_ZONE_ALLOC
1373 bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1375 bdb_entry_return( ei->bei_e );
1378 --bdb->bi_cache.c_cursize;
1380 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1382 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );