Esempio n. 1
0
static int read_block_raw(struct hlfs_ctrl *ctrl,uint32_t storage_address,char* block_buf)
{
	//HLOG_DEBUG("enter func %s", __func__);
    uint32_t block_size = ctrl->sb.block_size;
    struct back_storage * storage =NULL;
    uint32_t offset = get_offset(storage_address);
    uint32_t segno = get_segno(storage_address);
    if(segno >= ctrl->start_segno){
             storage = ctrl->storage;
    }else{
             if(NULL == (storage = get_parent_storage(ctrl->family,segno))){
		  return -1;
	      }
    }
    int ret = read_block(storage,storage_address,block_size,block_buf);
    //HLOG_DEBUG("leave func %s", __func__);
    return ret;
}
Esempio n. 2
0
int prev_open_rsegfile(struct hlfs_ctrl *ctrl, uint32_t segno){
	//HLOG_DEBUG("enter func %s", __func__);
	struct back_storage *storage = NULL;
	if (NULL == ctrl->last_rsegfile_handler) {
		char segfile_name[SEGMENT_FILE_NAME_MAX];
		memset((void *)segfile_name, 0, SEGMENT_FILE_NAME_MAX);
		build_segfile_name(segno, segfile_name);
		bs_file_t file;   
		if (segno >= ctrl->start_segno) {
			storage = ctrl->storage;
		}else{
			HLOG_DEBUG("get parent storage for segno:%d",segno);
			if (NULL == (storage = get_parent_storage(ctrl->family, segno))){
				g_assert(0);
				return -1;
			}
		}
		file = storage->bs_file_open(storage, segfile_name, BS_READONLY); 
		if (file == NULL) {
			HLOG_ERROR("can not open segment file %s", segfile_name);
			g_assert(0);
			return -1;
		}
		ctrl->last_rsegfile_handler = file;
		ctrl->last_rsegfile_offset = ctrl->last_offset;
		ctrl->last_read_segno =segno;
	}else if (ctrl->last_read_segno != segno || (ctrl->last_read_segno == \
				segno && ctrl->last_rsegfile_offset != ctrl->last_offset)){
		HLOG_DEBUG("cur segno:%d is, last segno no:%d, \
				last rsegfile offset:%d, last offset:%d,start_segno:%d - \
				need close old and open new segfile", segno, \
				ctrl->last_read_segno,ctrl->last_rsegfile_offset, \
				ctrl->last_offset, ctrl->start_segno);
		/* close last seg file handler...  */
		if (ctrl->last_read_segno >= ctrl->start_segno) {
			storage = ctrl->storage;
		}else{
			HLOG_DEBUG("get parent storage for segno:%d",segno);
			if (NULL == (storage = get_parent_storage(ctrl->family, \
							ctrl->last_read_segno))){
				g_assert(0);
				return -1;
			}
		}
		if (0 != storage->bs_file_close(storage, ctrl->last_rsegfile_handler)) {
			g_assert(0);
			return -1;
		}	
		/* open cur seg file handler...  */
		const char segfile_name[SEGMENT_FILE_NAME_MAX];
		build_segfile_name(segno, segfile_name);
		if (segno >= ctrl->start_segno) {
			storage = ctrl->storage;
		}else{
			HLOG_DEBUG("get parent storage for segno:%d", segno);	
			if (NULL == (storage = get_parent_storage(ctrl->family, segno))) {
				g_assert(0);
				return -1;
			}
		}
		bs_file_t file = storage->bs_file_open(storage, segfile_name,\
				BS_READONLY); 
		if (file == NULL) {
			HLOG_ERROR("can not open segment file %s", segfile_name);
			g_assert(0);
			return -1;
		}
		ctrl->last_rsegfile_handler = file;
		ctrl->last_rsegfile_offset  = ctrl->last_offset;
		ctrl->last_read_segno       = segno;
	}else{
Esempio n. 3
0
/*
 * hlfs_open: open a file.
 * @param ctrl: the global control.
 * @param flag: the flag for open operation, flag == 0
 *        readonly and flag == 1 writable.
 * @return: if successful return 0, else return -1.
 */
int hlfs_open(struct hlfs_ctrl *ctrl, int flag)
{
	HLOG_DEBUG("enter func %s", __func__);
	if (ctrl == NULL ||(flag != 0 && flag != 1)) { /* check the parameters */
		HLOG_ERROR("error params :flag %d", flag);
		return -1;
	}

	if (1 == flag) {
		ctrl->rw_inode_flag = 1;
	} else if (0 == flag) {
		ctrl->rw_inode_flag = 0;
	} else {
		HLOG_ERROR("the bad flag for hlfs open by inode");
        return -1;
    }

	if (ctrl->usage_ref > 0) {
		HLOG_DEBUG("This fs has opened by other,can not use it"); 
        return -1;
	}
    int ret = 0;
    HLOG_DEBUG("inode no %llu , inode address %llu", ctrl->imap_entry.inode_no, ctrl->imap_entry.inode_addr);
    if (ctrl->imap_entry.inode_no == 0 && ctrl->imap_entry.inode_addr == 0) { /* no inode condition */
	 HLOG_DEBUG("empty filesystem %s", ctrl->sb.fsname);
        if (flag == 0) {
			HLOG_ERROR("must create it with writeable flag");
			return -1;
	 }
        HLOG_DEBUG("create new fs inode !");
        ctrl->inode.length = 0;
        ctrl->inode.mtime = get_current_time();
        //ctrl->inode.ctime = get_current_time();
        //ctrl->inode.atime = get_current_time();
	} else { /* exist inode */
	      HLOG_DEBUG("this is not empty filesystem:%s", ctrl->sb.fsname);
	      struct back_storage *storage = NULL;
	      uint32_t segno = get_segno(ctrl->imap_entry.inode_addr);
             if (segno >= ctrl->start_segno) {
            		storage = ctrl->storage;
            }else{
            		HLOG_DEBUG("get parent storage for segno:%d", segno);
                     if (NULL == (storage = \
			get_parent_storage(ctrl->family, segno))){
                               g_assert(0);
                               return -1;
                     }
            } 
         struct inode *my_inode = NULL;
	    
	     my_inode = load_inode(storage, ctrl->imap_entry.inode_addr);    
	     if (my_inode == NULL) {
		     HLOG_ERROR("load_inode error!");
             return -1;
	     }
	    
	     HLOG_DEBUG("inode'length:%llu, ctrl->inode length:%llu, \
		sizeof inode:%d",my_inode->length,ctrl->inode.length, \
		sizeof(struct inode));	 
            memcpy(&(ctrl->inode), my_inode, sizeof(struct inode));
            g_free(my_inode);
	}
	HLOG_DEBUG("ctrl->rw_inode_flag:%d", ctrl->rw_inode_flag);
       struct snapshot *ss;
	if (0 == ctrl->storage->bs_file_is_exist(ctrl->storage, \
		SNAPSHOT_FILE)) {
        ret = find_latest_alive_snapshot(ctrl->storage, \
		ALIVE_SNAPSHOT_FILE, SNAPSHOT_FILE, &ss);
        if (ret != 0) {
		    HLOG_DEBUG("can not read alive snapshot, \
			there must be some error");
            return -1; 
        }