int32_t alloc_obj_block_and_cache(object_info_t *obj_info, ofs_block_cache_t **cache, uint32_t blk_id)
{
    ofs_block_cache_t *tmp_cache = NULL;
    int32_t ret = 0;
    uint64_t vbn = 0;

    ASSERT(obj_info != NULL);
    
    ret = OFS_ALLOC_BLOCK(obj_info->ct, obj_info->objid, &vbn);
    if (ret < 0)
    {
        LOG_ERROR("Allocate block failed. ret(%d)\n", ret);
        return ret;
    }

    OS_RWLOCK_WRLOCK(&obj_info->caches_lock);
    tmp_cache = alloc_obj_cache(obj_info, vbn, blk_id);
    if (!tmp_cache)
    {
        OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
        LOG_ERROR("Allocate cache failed.\n");
        OFS_FREE_BLOCK(obj_info->ct, obj_info->objid, vbn);
        return -INDEX_ERR_ALLOCATE_MEMORY;
    }

    OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
    
    *cache = tmp_cache;

    return 0;
}
int32_t flush_container_cache(container_handle_t *ct)
{
    int32_t ret = 0;
    
    ASSERT(ct != NULL);

    OS_RWLOCK_WRLOCK(&ct->metadata_cache_lock);
    avl_walk_all(&ct->metadata_cache, (avl_walk_cb_t)flush_container_dirty_cache, ct);
    OS_RWLOCK_WRUNLOCK(&ct->metadata_cache_lock);

    if (ct->flags & FLAG_DIRTY)
    {
        ct->sb.free_blocks = ct->sm.total_free_blocks;
        ct->sb.first_free_block = ct->sm.first_free_block;
        
        ct->sb.base_free_blocks = ct->bsm.total_free_blocks;
        ct->sb.base_first_free_block = ct->bsm.first_free_block;
        
        ct->sb.base_blk = ct->base_blk;

        ret = ofs_update_super_block(ct);
        if (ret < 0)
        {
            LOG_ERROR("Update super block failed. ct(%p) ret(%d)\n", ct, ret);
        }

        ct->flags &= ~FLAG_DIRTY;
    }

	return 0;
}
int32_t release_obj_all_cache(object_info_t *obj_info)
{
    OS_RWLOCK_WRLOCK(&obj_info->caches_lock);
    avl_walk_all(&obj_info->caches, (avl_walk_cb_t)release_obj_cache, obj_info);
    OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);

    return 0;
}
int32_t release_container_all_cache(container_handle_t *ct)
{
    ASSERT(ct != NULL);

    OS_RWLOCK_WRLOCK(&ct->metadata_cache_lock);
    avl_walk_all(&ct->metadata_cache, (avl_walk_cb_t)free_container_cache, ct);
    OS_RWLOCK_WRUNLOCK(&ct->metadata_cache_lock);

    return 0;
}
int32_t clean_all_obj_root_cache(container_handle_t *ct)
{
    ASSERT(ct != NULL);

    OS_RWLOCK_WRLOCK(&ct->ct_lock);
    avl_walk_all(&ct->obj_info_list, (avl_walk_cb_t)clean_obj_root_cache, ct);
    OS_RWLOCK_WRUNLOCK(&ct->ct_lock);

    return 0;
}
int32_t ofs_open_container(const char *ct_name, container_handle_t **ct)
{
    int32_t ret = 0;

    OS_RWLOCK_WRLOCK(&g_container_list_rwlock);
    ret = ofs_open_nolock(ct_name, ct);
    OS_RWLOCK_WRUNLOCK(&g_container_list_rwlock);
    
    return ret;
}     
int32_t ofs_close_container(container_handle_t *ct)
{
    int32_t ret = 0;

    OS_RWLOCK_WRLOCK(&g_container_list_rwlock);
    ret = ofs_close_nolock(ct);
    OS_RWLOCK_WRUNLOCK(&g_container_list_rwlock);

    return ret;
}     
int32_t ofs_create_container(const char *ct_name, uint64_t total_sectors, container_handle_t **ct)
{
    int32_t ret = 0;
    
    OS_RWLOCK_WRLOCK(&g_container_list_rwlock);
    ret = ofs_create_container_nolock(ct_name, total_sectors, ct);
    OS_RWLOCK_WRUNLOCK(&g_container_list_rwlock);

    return ret;
}     
int32_t ofs_walk_all_opened_container(container_cb_t cb, void *para)
{
    int32_t ret = 0;
    
    OS_RWLOCK_WRLOCK(&g_container_list_rwlock);
    ret = avl_walk_all(g_container_list, (avl_walk_cb_t)cb, para);
    OS_RWLOCK_WRUNLOCK(&g_container_list_rwlock);
    
    return ret;
}
void remove_obj_cache(object_info_t *obj_info, ofs_block_cache_t *cache)
{
    container_handle_t *ct = obj_info->ct;
    
    avl_remove(&obj_info->caches, cache); // remove from object
    
    OS_RWLOCK_WRLOCK(&ct->metadata_cache_lock);
    avl_remove(&ct->metadata_cache, cache); // remove from fs
    OS_RWLOCK_WRUNLOCK(&ct->metadata_cache_lock);
    
}
void insert_obj_cache(object_info_t *obj_info, ofs_block_cache_t *cache)
{
    container_handle_t *ct = obj_info->ct;
    
    avl_add(&obj_info->caches, cache); // add to object
    
    OS_RWLOCK_WRLOCK(&ct->metadata_cache_lock);
    avl_add(&ct->metadata_cache, cache); // add to fs
    OS_RWLOCK_WRUNLOCK(&ct->metadata_cache_lock);
    
}
예제 #12
0
static int32_t backup_log(log_t *log)
{
    int32_t ret = 0;
    char bakName[LOG_NAME_LEN];
    char name[LOG_NAME_LEN];
    time_t t = 0;    
    struct tm *ts = NULL;

    if (0 == (log->mode & LOG_TO_FILE))
    {
        return 0;
    }
    
    MkDir(log->dir);
    
    OS_RWLOCK_WRLOCK(&log->rwlock);
    
    t = time(NULL);
    ts = localtime(&t);
    if (NULL == ts)
    {
        OS_SNPRINTF(bakName, LOG_NAME_LEN, "%s/%s.log.bak",
            log->dir, log->name);
    }
    else
    {
        OS_SNPRINTF(bakName, LOG_NAME_LEN, "%s/%s_%04d%02d%02d_%02d%02d%02d.log", 
            log->dir, log->name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, 
            ts->tm_hour, ts->tm_min, ts->tm_sec);
    }
    
    OS_SNPRINTF(name, LOG_NAME_LEN, "%s/%s.log",
        log->dir, log->name);

    if (NULL != log->disk_hnd)
    {
        os_file_close(log->disk_hnd);
        log->disk_hnd = NULL;
    }
    
    unlink(bakName);
    rename(name, bakName);

    if (NULL == open_log(log))
    {
        ret = -1;
    }

    log->total_lines = 0;

    OS_RWLOCK_WRUNLOCK(&log->rwlock);
    
    return ret;
}
container_handle_t *ofs_get_container_handle(const char *ct_name)
{
    container_handle_t *ct = NULL;
    avl_index_t where = 0;
 
    if (ct_name == NULL)
    {   /* Not allocated yet */
        LOG_ERROR("Invalid parameter. ct_name(%p)\n", ct_name);
        return NULL;
    }

    OS_RWLOCK_WRLOCK(&g_container_list_rwlock);
    ct = avl_find(g_container_list, (avl_find_fn_t)compare_container2, ct_name, &where);
    OS_RWLOCK_WRUNLOCK(&g_container_list_rwlock);
    
    return ct;
}     
예제 #14
0
void log_trace(void *log, uint32_t pid, uint32_t level, const char *format, ...)
{
    log_t *tmp_log = (log_t *)log;
    va_list ap;

    if ((NULL == tmp_log) || (pid >= PIDS_NUM) || (level > tmp_log->levels[pid])
        || (0 == (tmp_log->mode & LOG_TO_SCNFILE))
        || ((0 == (tmp_log->mode & LOG_TO_SCREEN)) && (NULL == tmp_log->disk_hnd)))
    {
        return;
    }

    OS_RWLOCK_WRLOCK(&tmp_log->rwlock);
    
    os_get_date_time_string(tmp_log->date_time, DATA_TIME_STR_LEN);

    va_start(ap, format);
    OS_VSNPRINTF(tmp_log->buf, BUF_LEN, format, ap);
    va_end(ap);

    if (0 != (tmp_log->mode & LOG_TO_SCREEN))
    {
        //OS_PRINT("%s %s", tmp_log->date_time, tmp_log->buf);
    }
    
    if (NULL != tmp_log->disk_hnd)
    {
        os_file_printf(tmp_log->disk_hnd, "%s %s", tmp_log->date_time, tmp_log->buf);
        tmp_log->total_lines++;
    }
    
    
    OS_RWLOCK_WRUNLOCK(&tmp_log->rwlock);
    
    if (tmp_log->total_lines > MAX_FILE_LINES)
    {
        backup_log(tmp_log);
    }
}
예제 #15
0
static int32_t backup_log(log_t *log)
{
    int32_t ret = 0;

    if (0 == (log->mode & LOG_TO_FILE))
    {
        return 0;
    }
    
    OS_RWLOCK_WRLOCK(&log->rwlock);
    
    if (NULL == open_log(log))
    {
        ret = -1;
    }

    log->total_lines = 0;

    OS_RWLOCK_WRUNLOCK(&log->rwlock);
    
    return ret;
}
int32_t index_block_read2(object_info_t *obj_info, uint64_t vbn, uint32_t blk_id,
    ofs_block_cache_t **cache_out)
{
    int32_t ret = 0;
    ofs_block_cache_t *cache = NULL;
    avl_index_t where = 0;
    container_handle_t *ct;

    ASSERT(obj_info != NULL);

    ct = obj_info->ct;
    
    OS_RWLOCK_WRLOCK(&obj_info->caches_lock);
    cache = avl_find(&obj_info->caches, (avl_find_fn_t)compare_cache2, &vbn, &where);
    if (cache) // block already in the obj cache
    {
        OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
        *cache_out = cache;
        return 0;
    }
    
    OS_RWLOCK_RDLOCK(&ct->metadata_cache_lock);
    cache = avl_find(&ct->metadata_cache, (avl_find_fn_t)compare_cache2, &vbn, &where);
    if (cache) // block already in the container cache
    {
        OS_RWLOCK_RDUNLOCK(&ct->metadata_cache_lock);
        avl_add(&obj_info->caches, cache); // add to object
        OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
        *cache_out = cache;
        return 0;
    }
    OS_RWLOCK_RDUNLOCK(&ct->metadata_cache_lock);

    cache = alloc_obj_cache(obj_info, vbn, blk_id);
    if (!cache)
    {
        OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
        LOG_ERROR("Allocate cache failed.\n");
        *cache_out = NULL;
        return -INDEX_ERR_ALLOCATE_MEMORY;
    }
    
    ret = ofs_read_block_fixup(obj_info->ct, cache->ib, vbn, blk_id, obj_info->ct->sb.block_size);
    if (ret < 0)
    {   // Read the block
        LOG_ERROR("Read ct block failed. objid(%lld) vbn(%lld) size(%d) ret(%d)\n",
            obj_info->objid, vbn, obj_info->ct->sb.block_size, ret);
        OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);
        *cache_out = NULL;
        return ret;
    }

    LOG_DEBUG("Read ct block success. objid(%lld) vbn(%lld) size(%d)\n",
        obj_info->objid, vbn, obj_info->ct->sb.block_size);

    SET_CACHE_CLEAN(cache);
    OS_RWLOCK_WRUNLOCK(&obj_info->caches_lock);

    *cache_out = cache;
    return 0;
}