/* * marks an entry in CREATING state as committed, so it is really returned * to the cache. Otherwise an entry in CREATING state is removed. * Makes e_private be destroyed at the following cache_return_entry_w, * but lets the entry untouched (owned by someone else) */ void bdb_cache_entry_commit( Entry *e ) { assert( e ); assert( e->e_private ); assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING ); /* assert( BEI(e)->bei_refcnt == 1 ); */ BEI(e)->bei_state = CACHE_ENTRY_COMMITTED; }
/* This function constructs a full DN for a given entry. */ int hdb_fix_dn( Entry *e, int checkit ) { EntryInfo *ei; int rlen = 0, nrlen = 0; char *ptr, *nptr; int max = 0; if ( !e->e_id ) return 0; /* count length of all DN components */ for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) { rlen += ei->bei_rdn.bv_len + 1; nrlen += ei->bei_nrdn.bv_len + 1; if (ei->bei_modrdns > max) max = ei->bei_modrdns; } /* See if the entry DN was invalidated by a subtree rename */ if ( checkit ) { if ( BEI(e)->bei_modrdns >= max ) { return 0; } /* We found a mismatch, tell the caller to lock it */ if ( checkit == 1 ) { return 1; } /* checkit == 2. do the fix. */ free( e->e_name.bv_val ); free( e->e_nname.bv_val ); } e->e_name.bv_len = rlen - 1; e->e_nname.bv_len = nrlen - 1; e->e_name.bv_val = ch_malloc(rlen); e->e_nname.bv_val = ch_malloc(nrlen); ptr = e->e_name.bv_val; nptr = e->e_nname.bv_val; for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) { ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val); nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val); if ( ei->bei_parent ) { *ptr++ = ','; *nptr++ = ','; } } BEI(e)->bei_modrdns = max; if ( ptr > e->e_name.bv_val ) ptr[-1] = '\0'; if ( nptr > e->e_nname.bv_val ) nptr[-1] = '\0'; return 0; }
static void bdb_lru_print( Cache *cache ) { Entry *e; fprintf( stderr, "LRU queue (head to tail):\n" ); for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) { fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n", e->e_dn, e->e_id, BEI(e)->bei_refcnt ); } fprintf( stderr, "LRU queue (tail to head):\n" ); for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) { fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n", e->e_dn, e->e_id, BEI(e)->bei_refcnt ); } }
static int bdb_cache_delete_entry_internal( Cache *cache, Entry *e ) { int rc = 0; /* return code */ /* dn tree */ if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL ) { rc = -1; } /* id tree */ if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL ) { rc = -1; } if (rc != 0) { return rc; } /* lru */ LRU_DELETE( cache, e ); cache->c_cursize--; /* * flag entry to be freed later by a call to cache_return_entry() */ BEI(e)->bei_state = CACHE_ENTRY_DELETED; return( 0 ); }
static int bdb_cache_entry_rdwr_unlock(Entry *e, int rw) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, ENTRY, "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n", rw ? "w" : "r", e->e_id, 0 ); #else Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n", rw ? "w" : "r", e->e_id, 0); #endif if (rw) return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr); else return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr); }
void bdb_cache_release_all( Cache *cache ) { Entry *e; int rc; /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 ); #endif while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) { #ifdef LDAP_RDWR_DEBUG assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr)); #endif /* delete from cache and lru q */ /* XXX do we need rc ? */ rc = bdb_cache_delete_entry_internal( cache, e ); bdb_cache_entry_private_destroy( e ); bdb_entry_return( e ); } if ( cache->c_cursize ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0 ); #else Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 ); #endif } /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); }
int bdb_cache_children( Operation *op, DB_TXN *txn, Entry *e ) { int rc; if ( BEI(e)->bei_kids ) { return 0; } if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) { return DB_NOTFOUND; } rc = bdb_dn2id_children( op, txn, e ); if ( rc == DB_NOTFOUND ) { BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS; } return rc; }
int hdb_dn2id_children( Operation *op, DB_TXN *txn, Entry *e ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; int rc; ID id; diskNode d; DBTzero(&key); key.size = sizeof(ID); key.data = &e->e_id; key.flags = DB_DBT_USERMEM; BDB_ID2DISK( e->e_id, &id ); /* IDL cache is in host byte order */ if ( bdb->bi_idl_cache_size ) { rc = bdb_idl_cache_get( bdb, db, &key, NULL ); if ( rc != LDAP_NO_SUCH_OBJECT ) { return rc; } } key.data = &id; DBTzero(&data); data.data = &d; data.ulen = sizeof(d); data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; data.dlen = sizeof(d); rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) return rc; rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) { db_recno_t dkids; rc = cursor->c_count( cursor, &dkids, 0 ); if ( rc == 0 ) { BEI(e)->bei_dkids = dkids; if ( dkids < 2 ) rc = DB_NOTFOUND; } } cursor->c_close( cursor ); return rc; }
static int bdb_cache_entry_private_init( Entry *e ) { assert( e->e_private == NULL ); if( e->e_private != NULL ) { /* this should never happen */ return 1; } e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info)); #if 0 if( bdb_cache_entry_rdwr_init( e ) != 0 ) { free( BEI(e) ); e->e_private = NULL; return 1; } #endif return 0; }
/* * cache_update_entry - update a LOCKED entry which has been deleted. * returns: 0 entry has been created and locked * 1 entry already existed * -1 something bad happened */ int bdb_cache_update_entry( Cache *cache, Entry *e ) { int i, rc; Entry *ee; /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); assert( e->e_private ); if ( avl_insert( &cache->c_dntree, (caddr_t) e, entry_dn_cmp, avl_dup_error ) != 0 ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_update_entry: (%s):%ld already in dn cache\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n", e->e_id, e->e_dn, 0 ); #endif /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( 1 ); } /* id tree */ if ( avl_insert( &cache->c_idtree, (caddr_t) e, entry_id_cmp, avl_dup_error ) != 0 ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_update_entry: (%s)%ld already in id cache\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n", e->e_id, e->e_dn, 0 ); #endif /* delete from dn tree inserted above */ if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 ); #endif } /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( -1 ); } /* put the entry into 'CREATING' state */ /* will be marked after when entry is returned */ BEI(e)->bei_state = CACHE_ENTRY_CREATING; /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); /* lru */ LRU_ADD( cache, e ); if ( ++cache->c_cursize > cache->c_maxsize ) { /* * find the lru entry not currently in use and delete it. * in case a lot of entries are in use, only look at the * first 10 on the tail of the list. */ i = 0; while ( cache->c_lrutail != NULL && BEI(cache->c_lrutail)->bei_refcnt != 0 && i < 10 ) { /* move this in-use entry to the front of the q */ ee = cache->c_lrutail; LRU_DELETE( cache, ee ); LRU_ADD( cache, ee ); i++; } /* * found at least one to delete - try to get back under * the max cache size. */ while ( cache->c_lrutail != NULL && BEI(cache->c_lrutail)->bei_refcnt == 0 && cache->c_cursize > cache->c_maxsize ) { e = cache->c_lrutail; /* delete from cache and lru q */ /* XXX do we need rc ? */ rc = bdb_cache_delete_entry_internal( cache, e ); bdb_cache_entry_private_destroy( e ); bdb_entry_return( e ); } } /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( 0 ); }
int hdb_dn2id_delete( Operation *op, DB_TXN *txn, EntryInfo *eip, Entry *e ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; diskNode *d; int rc; ID nid; unsigned char dlen[2]; Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n", e->e_id, e->e_ndn, 0 ); DBTzero(&key); key.size = sizeof(ID); key.ulen = key.size; key.flags = DB_DBT_USERMEM; BDB_ID2DISK( eip->bei_id, &nid ); DBTzero(&data); data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1; data.ulen = data.size; data.dlen = data.size; data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; key.data = &nid; d = op->o_tmpalloc( data.size, op->o_tmpmemctx ); d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff; d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80; dlen[0] = d->nrdnlen[0]; dlen[1] = d->nrdnlen[1]; memcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val, BEI(e)->bei_nrdn.bv_len+1 ); data.data = d; rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) goto func_leave; /* Delete our ID from the parent's list */ rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE ); if ( rc == 0 ) { if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] && !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val )) rc = cursor->c_del( cursor, 0 ); else rc = DB_NOTFOUND; } /* Delete our ID from the tree. With sorted duplicates, this * will leave any child nodes still hanging around. This is OK * for modrdn, which will add our info back in later. */ if ( rc == 0 ) { BDB_ID2DISK( e->e_id, &nid ); rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) rc = cursor->c_del( cursor, 0 ); } cursor->c_close( cursor ); func_leave: op->o_tmpfree( d, op->o_tmpmemctx ); /* Delete IDL cache entries */ if ( rc == 0 && bdb->bi_idl_cache_size ) { ID tmp[2]; char *ptr = ((char *)&tmp[1])-1; key.data = ptr; key.size = sizeof(ID)+1; tmp[1] = eip->bei_id; *ptr = DN_ONE_PREFIX; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); if ( eip ->bei_parent ) { *ptr = DN_SUBTREE_PREFIX; for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) { tmp[1] = eip->bei_id; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); } /* Handle DB with empty suffix */ if ( !op->o_bd->be_suffix[0].bv_len && eip ) { tmp[1] = eip->bei_id; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); } } } Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 ); return rc; }
static int bdb_cache_entry_rdwr_destroy(Entry *e) { return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr ); }
int bdb_delete( Operation *op, SlapReply *rs ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; Entry *matched = NULL; struct berval pdn = {0, NULL}; Entry *e = NULL; Entry *p = NULL; EntryInfo *ei = NULL, *eip = NULL; int manageDSAit = get_manageDSAit( op ); AttributeDescription *children = slap_schema.si_ad_children; AttributeDescription *entry = slap_schema.si_ad_entry; DB_TXN *ltid = NULL, *lt2; struct bdb_op_info opinfo = {{{ 0 }}}; ID eid; DB_LOCK lock, plock; int num_retries = 0; int rc; LDAPControl **preread_ctrl = NULL; LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS]; int num_ctrls = 0; int parent_is_glue = 0; int parent_is_leaf = 0; #ifdef LDAP_X_TXN int settle = 0; #endif Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_delete) ": %s\n", op->o_req_dn.bv_val, 0, 0 ); #ifdef LDAP_X_TXN if( op->o_txnSpec ) { /* acquire connection lock */ ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex ); if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) { rs->sr_text = "invalid transaction identifier"; rs->sr_err = LDAP_X_TXN_ID_INVALID; goto txnReturn; } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) { settle=1; goto txnReturn; } if( op->o_conn->c_txn_backend == NULL ) { op->o_conn->c_txn_backend = op->o_bd; } else if( op->o_conn->c_txn_backend != op->o_bd ) { rs->sr_text = "transaction cannot span multiple database contexts"; rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS; goto txnReturn; } /* insert operation into transaction */ rs->sr_text = "transaction specified"; rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY; txnReturn: /* release connection lock */ ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex ); if( !settle ) { send_ldap_result( op, rs ); return rs->sr_err; } } #endif ctrls[num_ctrls] = 0; /* allocate CSN */ if ( BER_BVISNULL( &op->o_csn ) ) { struct berval csn; char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE]; csn.bv_val = csnbuf; csn.bv_len = sizeof(csnbuf); slap_get_csn( op, &csn, 1 ); } if( 0 ) { retry: /* transaction retry */ if( e != NULL ) { bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e); e = NULL; } if( p != NULL ) { bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p); p = NULL; } Debug( LDAP_DEBUG_TRACE, "==> " LDAP_XSTRING(bdb_delete) ": retrying...\n", 0, 0, 0 ); rs->sr_err = TXN_ABORT( ltid ); ltid = NULL; LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next ); opinfo.boi_oe.oe_key = NULL; op->o_do_not_cache = opinfo.boi_acl_cache; if( rs->sr_err != 0 ) { rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } if ( op->o_abandon ) { rs->sr_err = SLAPD_ABANDON; goto return_results; } parent_is_glue = 0; parent_is_leaf = 0; bdb_trans_backoff( ++num_retries ); } /* begin transaction */ rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id, bdb->bi_db_opflags ); rs->sr_text = NULL; if( rs->sr_err != 0 ) { Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_delete) ": txn_begin failed: " "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } opinfo.boi_oe.oe_key = bdb; opinfo.boi_txn = ltid; opinfo.boi_err = 0; opinfo.boi_acl_cache = op->o_do_not_cache; LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next ); if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) { dnParent( &op->o_req_ndn, &pdn ); } /* get entry */ rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1, &lock ); switch( rs->sr_err ) { case 0: case DB_NOTFOUND: break; case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; case LDAP_BUSY: rs->sr_text = "ldap server busy"; goto return_results; default: rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } if ( rs->sr_err == 0 ) { e = ei->bei_e; eip = ei->bei_parent; } else { matched = ei->bei_e; } /* FIXME : dn2entry() should return non-glue entry */ if ( e == NULL || ( !manageDSAit && is_entry_glue( e ))) { Debug( LDAP_DEBUG_ARGS, "<=- " LDAP_XSTRING(bdb_delete) ": no such object %s\n", op->o_req_dn.bv_val, 0, 0); if ( matched != NULL ) { rs->sr_matched = ch_strdup( matched->e_dn ); rs->sr_ref = is_entry_referral( matched ) ? get_entry_referrals( op, matched ) : NULL; bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched); matched = NULL; } else { rs->sr_ref = referral_rewrite( default_referral, NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT ); } rs->sr_err = LDAP_REFERRAL; rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED; goto return_results; } rc = bdb_cache_find_id( op, ltid, eip->bei_id, &eip, 0, &plock ); switch( rc ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; case 0: case DB_NOTFOUND: break; default: rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } if ( eip ) p = eip->bei_e; if ( pdn.bv_len != 0 ) { if( p == NULL || !bvmatch( &pdn, &p->e_nname )) { Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": parent " "does not exist\n", 0, 0, 0 ); rs->sr_err = LDAP_OTHER; rs->sr_text = "could not locate parent of entry"; goto return_results; } /* check parent for "children" acl */ rs->sr_err = access_allowed( op, p, children, NULL, ACL_WDEL, NULL ); if ( !rs->sr_err ) { switch( opinfo.boi_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": no write " "access to parent\n", 0, 0, 0 ); rs->sr_err = LDAP_INSUFFICIENT_ACCESS; rs->sr_text = "no write access to parent"; goto return_results; } } else { /* no parent, must be root to delete */ if( ! be_isroot( op ) ) { if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_shadow_update( op ) ) { p = (Entry *)&slap_entry_root; /* check parent for "children" acl */ rs->sr_err = access_allowed( op, p, children, NULL, ACL_WDEL, NULL ); p = NULL; if ( !rs->sr_err ) { switch( opinfo.boi_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": no access to parent\n", 0, 0, 0 ); rs->sr_err = LDAP_INSUFFICIENT_ACCESS; rs->sr_text = "no write access to parent"; goto return_results; } } else { Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": no parent and not root\n", 0, 0, 0 ); rs->sr_err = LDAP_INSUFFICIENT_ACCESS; goto return_results; } } } if ( get_assert( op ) && ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE )) { rs->sr_err = LDAP_ASSERTION_FAILED; goto return_results; } rs->sr_err = access_allowed( op, e, entry, NULL, ACL_WDEL, NULL ); if ( !rs->sr_err ) { switch( opinfo.boi_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": no write access " "to entry\n", 0, 0, 0 ); rs->sr_err = LDAP_INSUFFICIENT_ACCESS; rs->sr_text = "no write access to entry"; goto return_results; } if ( !manageDSAit && is_entry_referral( e ) ) { /* entry is a referral, don't allow delete */ rs->sr_ref = get_entry_referrals( op, e ); Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_delete) ": entry is referral\n", 0, 0, 0 ); rs->sr_err = LDAP_REFERRAL; rs->sr_matched = ch_strdup( e->e_name.bv_val ); rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED; goto return_results; } /* pre-read */ if( op->o_preread ) { if( preread_ctrl == NULL ) { preread_ctrl = &ctrls[num_ctrls++]; ctrls[num_ctrls] = NULL; } if( slap_read_controls( op, rs, e, &slap_pre_read_bv, preread_ctrl ) ) { Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": pre-read " "failed!\n", 0, 0, 0 ); if ( op->o_preread & SLAP_CONTROL_CRITICAL ) { /* FIXME: is it correct to abort * operation if control fails? */ goto return_results; } } } /* nested transaction */ rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags ); rs->sr_text = NULL; if( rs->sr_err != 0 ) { Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_delete) ": txn_begin(2) failed: " "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } BDB_LOG_PRINTF( bdb->bi_dbenv, lt2, "slapd Starting delete %s(%d)", e->e_nname.bv_val, e->e_id ); /* Can't do it if we have kids */ rs->sr_err = bdb_cache_children( op, lt2, e ); if( rs->sr_err != DB_NOTFOUND ) { switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; case 0: Debug(LDAP_DEBUG_ARGS, "<=- " LDAP_XSTRING(bdb_delete) ": non-leaf %s\n", op->o_req_dn.bv_val, 0, 0); rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF; rs->sr_text = "subordinate objects must be deleted first"; break; default: Debug(LDAP_DEBUG_ARGS, "<=- " LDAP_XSTRING(bdb_delete) ": has_children failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; } goto return_results; } /* delete from dn2id */ rs->sr_err = bdb_dn2id_delete( op, lt2, eip, e ); if ( rs->sr_err != 0 ) { Debug(LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": dn2id failed: " "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } rs->sr_text = "DN index delete failed"; rs->sr_err = LDAP_OTHER; goto return_results; } /* delete indices for old attributes */ rs->sr_err = bdb_index_entry_del( op, lt2, e ); if ( rs->sr_err != LDAP_SUCCESS ) { Debug(LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": index failed: " "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } rs->sr_text = "entry index delete failed"; rs->sr_err = LDAP_OTHER; goto return_results; } /* fixup delete CSN */ if ( !SLAP_SHADOW( op->o_bd )) { struct berval vals[2]; assert( !BER_BVISNULL( &op->o_csn ) ); vals[0] = op->o_csn; BER_BVZERO( &vals[1] ); rs->sr_err = bdb_index_values( op, lt2, slap_schema.si_ad_entryCSN, vals, 0, SLAP_INDEX_ADD_OP ); if ( rs->sr_err != LDAP_SUCCESS ) { switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } rs->sr_text = "entryCSN index update failed"; rs->sr_err = LDAP_OTHER; goto return_results; } } /* delete from id2entry */ rs->sr_err = bdb_id2entry_delete( op->o_bd, lt2, e ); if ( rs->sr_err != 0 ) { Debug( LDAP_DEBUG_TRACE, "<=- " LDAP_XSTRING(bdb_delete) ": id2entry failed: " "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } rs->sr_text = "entry delete failed"; rs->sr_err = LDAP_OTHER; goto return_results; } if ( pdn.bv_len != 0 ) { parent_is_glue = is_entry_glue(p); rs->sr_err = bdb_cache_children( op, lt2, p ); if ( rs->sr_err != DB_NOTFOUND ) { switch( rs->sr_err ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; case 0: break; default: Debug(LDAP_DEBUG_ARGS, "<=- " LDAP_XSTRING(bdb_delete) ": has_children failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 ); rs->sr_err = LDAP_OTHER; rs->sr_text = "internal error"; goto return_results; } parent_is_leaf = 1; } bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p); p = NULL; } BDB_LOG_PRINTF( bdb->bi_dbenv, lt2, "slapd Commit1 delete %s(%d)", e->e_nname.bv_val, e->e_id ); if ( TXN_COMMIT( lt2, 0 ) != 0 ) { rs->sr_err = LDAP_OTHER; rs->sr_text = "txn_commit(2) failed"; goto return_results; } eid = e->e_id; #if 0 /* Do we want to reclaim deleted IDs? */ ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex ); if ( e->e_id == bdb->bi_lastid ) { bdb_last_id( op->o_bd, ltid ); } ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex ); #endif if( op->o_noop ) { if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) { rs->sr_text = "txn_abort (no-op) failed"; } else { rs->sr_err = LDAP_X_NO_OPERATION; ltid = NULL; goto return_results; } } else { BDB_LOG_PRINTF( bdb->bi_dbenv, ltid, "slapd Cache delete %s(%d)", e->e_nname.bv_val, e->e_id ); rc = bdb_cache_delete( bdb, e, ltid, &lock ); switch( rc ) { case DB_LOCK_DEADLOCK: case DB_LOCK_NOTGRANTED: goto retry; } rs->sr_err = TXN_COMMIT( ltid, 0 ); } ltid = NULL; LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next ); opinfo.boi_oe.oe_key = NULL; BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Committed delete %s(%d)", e->e_nname.bv_val, e->e_id ); if( rs->sr_err != 0 ) { Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_delete) ": txn_%s failed: %s (%d)\n", op->o_noop ? "abort (no-op)" : "commit", db_strerror(rs->sr_err), rs->sr_err ); rs->sr_err = LDAP_OTHER; rs->sr_text = "commit failed"; goto return_results; } Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_delete) ": deleted%s id=%08lx dn=\"%s\"\n", op->o_noop ? " (no-op)" : "", eid, op->o_req_dn.bv_val ); rs->sr_err = LDAP_SUCCESS; rs->sr_text = NULL; if( num_ctrls ) rs->sr_ctrls = ctrls; return_results: if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) { op->o_delete_glue_parent = 1; } if ( p ) bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p); /* free entry */ if( e != NULL ) { if ( rs->sr_err == LDAP_SUCCESS ) { /* Free the EntryInfo and the Entry */ bdb_cache_entryinfo_lock( BEI(e) ); bdb_cache_delete_cleanup( &bdb->bi_cache, BEI(e) ); } else { bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e); } } if( ltid != NULL ) { TXN_ABORT( ltid ); } if ( opinfo.boi_oe.oe_key ) { LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next ); } send_ldap_result( op, rs ); slap_graduate_commit_csn( op ); if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) { slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx ); slap_sl_free( *preread_ctrl, op->o_tmpmemctx ); } if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) { TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 ); } return rs->sr_err; }
Entry * bdb_cache_find_entry_id( DB_ENV *env, Cache *cache, ID id, int rw, u_int32_t locker, DB_LOCK *lock ) { Entry e; Entry *ep; int count = 0; int rc; e.e_id = id; try_again: /* set cache read lock */ ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock ); if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e, entry_id_cmp )) != NULL ) { int state; ID ep_id; count++; assert( ep->e_private ); ep_id = ep->e_id; state = BEI(ep)->bei_state; /* * entry is deleted or not fully created yet */ if ( state != CACHE_ENTRY_READY && state != CACHE_ENTRY_COMMITTED ) { assert(state != CACHE_ENTRY_UNDEFINED); /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n", id, ep_id, state ); #else Debug(LDAP_DEBUG_TRACE, "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n", id, ep_id, state); #endif ldap_pvt_thread_yield(); goto try_again; } /* acquire reader lock */ rc = bdb_cache_entry_db_lock ( env, locker, ep, rw, 0, lock ); #if 0 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) { #endif if ( rc ) { /* will be changed to retry beyond threshold */ /* could not acquire entry lock... * owner cannot free as we have the cache locked. * so, unlock the cache, yield, and try again. */ /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n", id, ep_id, state ); #else Debug(LDAP_DEBUG_TRACE, "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n", id, ep_id, state); Debug(LDAP_DEBUG_TRACE, "locker = %d\n", locker, 0, 0); #endif ldap_pvt_thread_yield(); goto try_again; } /* Mark entry in-use */ BEI(ep)->bei_refcnt++; /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); /* lru */ LRU_DELETE( cache, ep ); LRU_ADD( cache, ep ); /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n", ep_id, ep->e_dn, count ); #else Debug(LDAP_DEBUG_TRACE, "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n", ep_id, ep->e_dn, count); #endif return( ep ); } /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); return( NULL ); } /* * cache_delete_entry - delete the entry e from the cache. the caller * should have obtained e (increasing its ref count) via a call to one * of the cache_find_* routines. the caller should *not* call the * cache_return_entry() routine prior to calling cache_delete_entry(). * it performs this function. * * returns: 0 e was deleted ok * 1 e was not in the cache * -1 something bad happened */ int bdb_cache_delete_entry( Cache *cache, Entry *e ) { int rc; /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); assert( e->e_private ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, ENTRY, "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n", e->e_id, 0, 0 ); #endif /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); rc = bdb_cache_delete_entry_internal( cache, e ); /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( rc ); }
void bdb_cache_return_entry_rw ( DB_ENV *env, Cache *cache, Entry *e, int rw, DB_LOCK *lock ) { ID id; int refcnt, freeit = 1; /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); assert( e->e_private ); bdb_cache_entry_db_unlock( env, lock ); #if 0 bdb_cache_entry_rdwr_unlock(e, rw); #endif id = e->e_id; refcnt = --BEI(e)->bei_refcnt; /* * if the entry is returned when in CREATING state, it is deleted * but not freed because it may belong to someone else (do_add, * for instance) */ if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) { /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); bdb_cache_delete_entry_internal( cache, e ); /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); freeit = 0; /* now the entry is in DELETED state */ } if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) { BEI(e)->bei_state = CACHE_ENTRY_READY; /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n", id, rw ? "w" : "r", refcnt ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n", rw ? "w" : "r", id, refcnt ); #endif } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) { if( refcnt > 0 ) { /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n", id, refcnt, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n", rw ? "w" : "r", id, refcnt ); #endif } else { bdb_cache_entry_private_destroy( e ); if ( freeit ) { bdb_entry_return( e ); } /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n", id, refcnt, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n", rw ? "w" : "r", id, refcnt ); #endif } } else { /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n", id, rw ? "w": "r", refcnt ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n", rw ? "w" : "r", id, refcnt); #endif } }
ID bdb_cache_find_entry_ndn2id( Backend *be, Cache *cache, struct berval *ndn ) { Entry e, *ep; ID id; int count = 0; /* this function is always called with normalized DN */ e.e_nname = *ndn; try_again: /* set cache read lock */ ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock ); if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e, entry_dn_cmp )) != NULL ) { int state; count++; /* * ep now points to an unlocked entry * we do not need to lock the entry if we only * check the state, refcnt, LRU, and id. */ assert( ep->e_private ); /* save id */ id = ep->e_id; state = BEI(ep)->bei_state; /* * entry is deleted or not fully created yet */ if ( state != CACHE_ENTRY_READY && state != CACHE_ENTRY_COMMITTED ) { assert(state != CACHE_ENTRY_UNDEFINED); /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n", ndn->bv_val, id, state ); #else Debug(LDAP_DEBUG_TRACE, "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n", ndn->bv_val, id, state); #endif ldap_pvt_thread_yield(); goto try_again; } /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); /* lru */ LRU_DELETE( cache, ep ); LRU_ADD( cache, ep ); /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n", ndn->bv_val, id, count ); #else Debug(LDAP_DEBUG_TRACE, "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n", ndn->bv_val, id, count); #endif } else { /* free cache read lock */ ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock ); id = NOID; } return( id ); }
/* * cache_add_entry_rw - create and lock an entry in the cache * returns: 0 entry has been created and locked * 1 entry already existed * -1 something bad happened * other Berkeley DB locking error code */ int bdb_cache_add_entry_rw( DB_ENV *env, Cache *cache, Entry *e, int rw, u_int32_t locker, DB_LOCK *lock ) { int i, rc; Entry *ee; #ifdef NEW_LOGGING LDAP_LOG( CACHE, ENTRY, "bdb_cache_add_entry_rw: add (%s):%s to cache\n", e->e_dn, rw ? "w" : "r", 0 ); #endif /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); assert( e->e_private == NULL ); if( bdb_cache_entry_private_init(e) != 0 ) { /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, ERR, "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n", e->e_id, e->e_dn, 0 ); #endif return( -1 ); } if ( avl_insert( &cache->c_dntree, (caddr_t) e, entry_dn_cmp, avl_dup_error ) != 0 ) { /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_add_entry: (%s):%ld already in cache.\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n", e->e_id, e->e_dn, 0 ); #endif bdb_cache_entry_private_destroy(e); return( 1 ); } /* id tree */ if ( avl_insert( &cache->c_idtree, (caddr_t) e, entry_id_cmp, avl_dup_error ) != 0 ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, DETAIL1, "bdb_cache_add_entry: (%s):%ls already in cache.\n", e->e_dn, e->e_id, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n", e->e_id, e->e_dn, 0 ); #endif /* delete from dn tree inserted above */ if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_add_entry: can't delete (%s) from cache.\n", e->e_dn, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 ); #endif } bdb_cache_entry_private_destroy(e); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( -1 ); } rc = bdb_cache_entry_db_lock( env, locker, e, rw, 0, lock ); switch ( rc ) { case 0 : break; case DB_LOCK_DEADLOCK : case DB_LOCK_NOTGRANTED : /* undo avl changes immediately */ if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_add_entry: can't delete (%s) from cache.\n", e->e_dn, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> can't delete from id cache\n", 0, 0, 0 ); #endif } if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( CACHE, INFO, "bdb_cache_add_entry: can't delete (%s) from cache.\n", e->e_dn, 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 ); #endif } /* fall through */ default : bdb_cache_entry_private_destroy(e); ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return rc; } /* put the entry into 'CREATING' state */ /* will be marked after when entry is returned */ BEI(e)->bei_state = CACHE_ENTRY_CREATING; BEI(e)->bei_refcnt = 1; /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_mutex ); /* lru */ LRU_ADD( cache, e ); if ( ++cache->c_cursize > cache->c_maxsize ) { /* * find the lru entry not currently in use and delete it. * in case a lot of entries are in use, only look at the * first 10 on the tail of the list. */ i = 0; while ( cache->c_lrutail != NULL && BEI(cache->c_lrutail)->bei_refcnt != 0 && i < 10 ) { /* move this in-use entry to the front of the q */ ee = cache->c_lrutail; LRU_DELETE( cache, ee ); LRU_ADD( cache, ee ); i++; } /* * found at least one to delete - try to get back under * the max cache size. */ while ( cache->c_lrutail != NULL && BEI(cache->c_lrutail)->bei_refcnt == 0 && cache->c_cursize > cache->c_maxsize ) { e = cache->c_lrutail; /* delete from cache and lru q */ /* XXX do we need rc ? */ rc = bdb_cache_delete_entry_internal( cache, e ); bdb_cache_entry_private_destroy( e ); bdb_entry_return( e ); } } /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_mutex ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); return( 0 ); }