📄 cache.c
字号:
/* bdb_csn_commit can cause this when adding the database root entry */ if ( new->bei_e ) { new->bei_e->e_private = NULL;#ifdef SLAP_ZONE_ALLOC bdb_entry_return( bdb, new->bei_e, new->bei_zseq );#else bdb_entry_return( new->bei_e );#endif } new->bei_e = e; e->e_private = new; new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS; eip->bei_state &= ~CACHE_ENTRY_NO_KIDS; if (eip->bei_parent) { eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS; } bdb_cache_entryinfo_unlock( eip ); ++bdb->bi_cache.c_cursize; ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_head_mutex ); /* lru_head_mutex is unlocked for us */ bdb_cache_lru_add( bdb, new ); return rc;}intbdb_cache_modify( Entry *e, Attribute *newAttrs, DB_ENV *env, u_int32_t locker, DB_LOCK *lock ){ EntryInfo *ei = BEI(e); int rc; /* Get write lock on data */ rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock ); /* If we've done repeated mods on a cached entry, then e_attrs * is no longer contiguous with the entry, and must be freed. */ if ( ! rc ) { if ( (void *)e->e_attrs != (void *)(e+1) ) { attrs_free( e->e_attrs ); } e->e_attrs = newAttrs; } return rc;}/* * Change the rdn in the entryinfo. Also move to a new parent if needed. */intbdb_cache_modrdn( struct bdb_info *bdb, Entry *e, struct berval *nrdn, Entry *new, EntryInfo *ein, u_int32_t locker, DB_LOCK *lock ){ EntryInfo *ei = BEI(e), *pei; int rc;#ifdef BDB_HIER struct berval rdn;#endif /* Get write lock on data */ rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock ); if ( rc ) return rc; /* If we've done repeated mods on a cached entry, then e_attrs * is no longer contiguous with the entry, and must be freed. */ if ( (void *)e->e_attrs != (void *)(e+1) ) { attrs_free( e->e_attrs ); } e->e_attrs = new->e_attrs; if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len ) { ch_free(e->e_name.bv_val); ch_free(e->e_nname.bv_val); } e->e_name = new->e_name; e->e_nname = new->e_nname; /* Lock the parent's kids AVL tree */ pei = ei->bei_parent; bdb_cache_entryinfo_lock( pei ); avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp ); free( ei->bei_nrdn.bv_val ); ber_dupbv( &ei->bei_nrdn, nrdn ); if ( !pei->bei_kids ) pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;#ifdef BDB_HIER free( ei->bei_rdn.bv_val ); rdn = e->e_name; if ( nrdn->bv_len != e->e_nname.bv_len ) { char *ptr = ber_bvchr(&rdn, ','); assert( ptr != NULL ); rdn.bv_len = ptr - rdn.bv_val; } ber_dupbv( &ei->bei_rdn, &rdn ); pei->bei_ckids--; if ( pei->bei_dkids ) pei->bei_dkids--;#endif if (!ein) { ein = ei->bei_parent; } else { ei->bei_parent = ein; bdb_cache_entryinfo_unlock( pei ); bdb_cache_entryinfo_lock( ein ); } /* parent now has kids */ if ( ein->bei_state & CACHE_ENTRY_NO_KIDS ) ein->bei_state ^= CACHE_ENTRY_NO_KIDS;#ifdef BDB_HIER /* parent might now have grandkids */ if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS && !(ei->bei_state & (CACHE_ENTRY_NO_KIDS))) ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS; { /* Record the generation number of this change */ ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex ); bdb->bi_modrdns++; ei->bei_modrdns = bdb->bi_modrdns; ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex ); } ein->bei_ckids++; if ( ein->bei_dkids ) ein->bei_dkids++;#endif avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error ); bdb_cache_entryinfo_unlock( ein ); return rc;}/* * cache_delete - delete the entry e from the cache. * * returns: 0 e was deleted ok * 1 e was not in the cache * -1 something bad happened */intbdb_cache_delete( Cache *cache, Entry *e, DB_ENV *env, u_int32_t locker, DB_LOCK *lock ){ EntryInfo *ei = BEI(e); int rc; assert( e->e_private != NULL ); /* Set this early, warn off any queriers */ ei->bei_state |= CACHE_ENTRY_DELETED; /* Lock the entry's info */ bdb_cache_entryinfo_lock( ei ); /* Get write lock on the data */ rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock ); if ( rc ) { /* couldn't lock, undo and give up */ ei->bei_state ^= CACHE_ENTRY_DELETED; bdb_cache_entryinfo_unlock( ei ); return rc; } Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n", e->e_id, 0, 0 ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex ); /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); rc = bdb_cache_delete_internal( cache, e->e_private, 1 ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex ); /* Leave entry info locked */ return( rc );}voidbdb_cache_delete_cleanup( Cache *cache, EntryInfo *ei ){ if ( ei->bei_e ) { ei->bei_e->e_private = NULL;#ifdef SLAP_ZONE_ALLOC bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );#else bdb_entry_return( ei->bei_e );#endif ei->bei_e = NULL; } free( ei->bei_nrdn.bv_val ); ei->bei_nrdn.bv_val = NULL;#ifdef BDB_HIER free( ei->bei_rdn.bv_val ); ei->bei_rdn.bv_val = NULL; ei->bei_modrdns = 0; ei->bei_ckids = 0; ei->bei_dkids = 0;#endif ei->bei_parent = NULL; ei->bei_kids = NULL; ei->bei_lruprev = NULL; ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); ei->bei_lrunext = cache->c_eifree; cache->c_eifree = ei; ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock ); bdb_cache_entryinfo_unlock( ei );}static intbdb_cache_delete_internal( Cache *cache, EntryInfo *e, int decr ){ int rc = 0; /* return code */ /* Lock the parent's kids tree */ bdb_cache_entryinfo_lock( e->bei_parent );#ifdef BDB_HIER e->bei_parent->bei_ckids--; if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;#endif /* dn tree */ if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL ) { rc = -1; } if ( e->bei_parent->bei_kids ) cache->c_leaves--; /* id tree */ if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) { rc = -1; } if ( rc == 0 ){ cache->c_eiused--; /* lru */ LRU_DELETE( cache, e ); if ( e->bei_e ) cache->c_cursize--; } bdb_cache_entryinfo_unlock( e->bei_parent ); return( rc );}static voidbdb_entryinfo_release( void *data ){ EntryInfo *ei = (EntryInfo *)data; if ( ei->bei_kids ) { avl_free( ei->bei_kids, NULL ); } if ( ei->bei_e ) { ei->bei_e->e_private = NULL;#ifdef SLAP_ZONE_ALLOC bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );#else bdb_entry_return( ei->bei_e );#endif } bdb_cache_entryinfo_destroy( ei );}voidbdb_cache_release_all( Cache *cache ){ /* set cache write lock */ ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock ); /* set lru mutex */ ldap_pvt_thread_mutex_lock( &cache->lru_tail_mutex ); Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 ); avl_free( cache->c_dntree.bei_kids, NULL ); avl_free( cache->c_idtree, bdb_entryinfo_release ); for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) { cache->c_lruhead = cache->c_eifree->bei_lrunext; bdb_cache_entryinfo_destroy(cache->c_eifree); } cache->c_cursize = 0; cache->c_eiused = 0; cache->c_leaves = 0; cache->c_idtree = NULL; cache->c_lruhead = NULL; cache->c_lrutail = NULL; cache->c_dntree.bei_kids = NULL; /* free lru mutex */ ldap_pvt_thread_mutex_unlock( &cache->lru_tail_mutex ); /* free cache write lock */ ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );}#ifdef LDAP_DEBUG#ifdef SLAPD_UNUSEDstatic voidbdb_lru_print( Cache *cache ){ EntryInfo *e; fprintf( stderr, "LRU queue (head to tail):\n" ); for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) { fprintf( stderr, "\trdn \"%20s\" id %ld\n", e->bei_nrdn.bv_val, e->bei_id ); } fprintf( stderr, "LRU queue (tail to head):\n" ); for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) { fprintf( stderr, "\trdn \"%20s\" id %ld\n", e->bei_nrdn.bv_val, e->bei_id ); }}#endif#endif#ifdef BDB_REUSE_LOCKERSstatic voidbdb_locker_id_free( void *key, void *data ){ DB_ENV *env = key; u_int32_t lockid = (long)data; int rc; rc = XLOCK_ID_FREE( env, lockid ); if ( rc == EINVAL ) { DB_LOCKREQ lr; Debug( LDAP_DEBUG_ANY, "bdb_locker_id_free: %lu err %s(%d)\n", (unsigned long) lockid, db_strerror(rc), rc ); /* release all locks held by this locker. */ lr.op = DB_LOCK_PUT_ALL; lr.obj = NULL; env->lock_vec( env, lockid, 0, &lr, 1, NULL ); XLOCK_ID_FREE( env, lockid ); }}/* free up any keys used by the main thread */voidbdb_locker_flush( DB_ENV *env ){ void *data; void *ctx = ldap_pvt_thread_pool_context(); if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) { ldap_pvt_thread_pool_setkey( ctx, env, NULL, NULL ); bdb_locker_id_free( env, data ); }}intbdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker ){ int i, rc; u_int32_t lockid; void *data; void *ctx; if ( !env || !locker ) return -1; /* If no op was provided, try to find the ctx anyway... */ if ( op ) { ctx = op->o_threadctx; } else { ctx = ldap_pvt_thread_pool_context(); } /* Shouldn't happen unless we're single-threaded */ if ( !ctx ) { *locker = 0; return 0; } if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) { for ( i=0, rc=1; rc != 0 && i<4; i++ ) { rc = XLOCK_ID( env, &lockid ); if (rc) ldap_pvt_thread_yield(); } if ( rc != 0) { return rc; } data = (void *)((long)lockid); if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env, data, bdb_locker_id_free ) ) ) { XLOCK_ID_FREE( env, lockid ); Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n", db_strerror(rc), rc, 0 ); return rc; } } else { lockid = (long)data; } *locker = lockid; return 0;}#endif /* BDB_REUSE_LOCKERS */voidbdb_cache_delete_entry( struct bdb_info *bdb, EntryInfo *ei, u_int32_t locker, DB_LOCK *lock ){ ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock ); if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 ) { if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) { LRU_DELETE( &bdb->bi_cache, ei ); ei->bei_e->e_private = NULL;#ifdef SLAP_ZONE_ALLOC bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );#else bdb_entry_return( ei->bei_e );#endif ei->bei_e = NULL; --bdb->bi_cache.c_cursize; } bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock ); } ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -