int
hlfs_get_inode_info(const char *uri,
		uint64_t inode_addr,
		uint64_t *mtime,
		uint64_t *length) {
	int ret = 0;
	struct back_storage *storage = init_storage_handler(uri);
	if (NULL == uri) {
		HLOG_ERROR("init storage handler error!");
		ret = -1;
		goto out;
	}
	struct inode *inode = load_inode(storage, inode_addr);
	if (NULL == inode) {
		HLOG_ERROR("load inode error!");
		ret = -1;
		goto out;
	}
	*mtime = inode->mtime;
	*length = inode->length;
out:
	g_free(storage);
	g_free(inode);
	return ret;
}
Exemple #2
0
struct Ext2FSInode * Ext2FS::get_file_inode_from_dir_inode(struct Ext2FSInode * from, const char * filename)
{
	if(from == NULL)
		from = load_inode(EXT2_RDIR_INODE_NUMBER);
	//std::cerr << *from << std::endl;
	assert(INODE_ISDIR(from));

}
int hlfs_open_by_snapshot(struct hlfs_ctrl *ctrl,
					const char* snapshot,
					int flag) {
	//HLOG_DEBUG("enter func %s", __func__);
	if (NULL == ctrl || NULL == snapshot) {
		HLOG_ERROR("Parameter Error!");
		return -1;
	}
	if ((strlen(snapshot) + 1) > HLFS_FILE_NAME_MAX) {
		HLOG_ERROR("snapshot name beyond max length");
		return -1;
	}
	int ret = 0;
	struct snapshot *ss = NULL;
	if (0 == ctrl->storage->bs_file_is_exist(ctrl->storage, SNAPSHOT_FILE)) {
		if (0 > (ret = load_snapshot_by_name(ctrl->storage, SNAPSHOT_FILE, &ss, snapshot))) {
			HLOG_ERROR("load ss by name error");
			g_free(ss);
			ret = -1;
			goto out;
		}
	} else {
		HLOG_ERROR("We have no snapshot at the moment");
		ret = -1;
		goto out;
	}
	struct inode *inode = load_inode(ctrl->storage,ss->inode_addr);
	if (inode == NULL) {
		HLOG_ERROR("load_inode error!");
		ret = -1;
		goto out;
	}

    memcpy(&(ctrl->inode), inode, sizeof(struct inode));
	g_free(inode);
    ctrl->imap_entry.inode_no = HLFS_INODE_NO;
    ctrl->imap_entry.inode_addr = ss->inode_addr;

	if (0 == flag) {
		ctrl->rw_inode_flag = 0;
	} else if (1 == flag) {
		ctrl->rw_inode_flag = 1;
	} else {
		HLOG_ERROR("the bad flag for hlfs open by inode");
		ret = -1;
	}
	memset(ctrl->alive_ss_name, 0, MAX_FILE_NAME_LEN);
	sprintf(ctrl->alive_ss_name, "%s", ss->sname);
    g_free(ss);
	ctrl->usage_ref += 1;
out:
	//HLOG_DEBUG("leave func %s", __func__);
	return ret;
}
Exemple #4
0
static int load_file ()
{
	int err;

	while (1) {
		err = load_inode ();
		if (err) break;

		/*
		puts ("size=");
		word_hex (i_data->i_size);
		puts ("\r\n");
		*/

		f_pos = 0;

		while (1) {

			// Direct zones
			load_zone (0, &(i_data->i_zone [ZONE_IND_L0]), &(i_data->i_zone [ZONE_IND_L1]));
			if (f_pos >= i_data->i_size) break;

			// Indirect zones
			load_zone (1, &(i_data->i_zone [ZONE_IND_L1]), &(i_data->i_zone [ZONE_IND_L2]));
			if (f_pos >= i_data->i_size) break;

			// Double-indirect zones
			//load_zone (2, &(i_data->i_zone [ZONE_IND_L2]), &(i_data->i_zone [ZONE_IND_END]));

			break;
		}

		break;
	}

	return err;
}
Exemple #5
0
void do_copy(char* src, char* dst, char* size) {

	link_block = 261;

	for (i = 0; i < 30; i++)
		name[i] = dst[i];

	for (i = 0; i < 30; i++)
		if (size[i] == 0)
			break;
		else
			length = length * 10 + size[i] - '0';

	for (i = 0; i < 30; i++)
		newfile.name[i] = name[i];
	printf("name: %s %s\n", name, newfile.name);
	newfile.size = length;
	newfile.flags = DIR << 8;
	sfd = open(src, O_RDONLY);
	if (sfd == -1) {
		printf("can't open\n");
		exit(1) ;
	}
	read(sfd, buf, length);

	base = buf;
	printf("base1: %s\n" , base);
	for (i = 0; i <= (newfile.size-1) / 512; i++) {
		newfile.addr[i] = need_block();
		printf("newfile.addr[%d]: %d\n", i, newfile.addr[i]);

		lseek(disk, newfile.addr[i]*512 - 512, SEEK_SET);
		write(disk, base, 512);
		base += 512;
		if (i == 0) printf("base:%s\n", base);
	}
	printf("link_block: %d\n", link_block);

	index = 0;
	lseek(disk, 194 * 512, SEEK_SET);
	read(disk, &root, 64);
	lseek(disk, root.addr[0]*512 - 512, SEEK_SET);

	read(disk, buf, 512);
	list = (struct dir_entry*) buf;
	for (i = 0; i < 16; i++) {
		if (strcmp(list[i].name, "bin") == 0)
			break;
		else printf("%s \n", list[i].name);
	}


	index = list[i].inode_index;
	load_inode(index, &bin);
	if (bin.size % 512 == 0)
		bin.addr[bin.size/512] = need_block();
	bin.size += 32;
	write_inode(index, &bin);

	lseek(disk, 192*512, SEEK_SET);
	read(disk, buf, 1024);
	tmp = (short*) buf;
	newind = tmp[tmp[0]];
	printf("newind: %d\n", newind);
	tmp[0] --;
	lseek(disk, 192*512, SEEK_SET);
	write(disk, buf, 512);

	i = (bin.size - 1) / 512;
	i = bin.addr[i] - 1;
	j = bin.size / 32;
	j = (j - 1) % 16;
	printf(" block: %d; list[%d]\n", i, j);

	lseek(disk, i*512, SEEK_SET);
	read(disk, buf, 512);
	list = (struct dir_entry*) buf;
	printf("%s %s\n", list[0].name, list[1].name);

	for (k = 0; k < 30; k++)
		list[j].name[k] = newfile.name[k];

	printf("%s %s\n", list[j].name, newfile.name);
	list[j].inode_index = newind;
	lseek(disk, i*512, SEEK_SET);
	write(disk, buf, 512);


	newfile.unused[0] = index;
	
	write_inode(newind, &newfile);
	printf("copy over\n");

}
Exemple #6
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; 
        }
Exemple #7
0
int main(int argc, char **argv)
{

    int fd;

    if (argc <= 1) {
        printf("Usage: load_file image\n");
        return 1;
    }

    fd = open(argv[1], O_RDONLY);

    lseek(fd, BOOT_BLOCK_SIZE, SEEK_SET);

    struct superblock *superblock =
        (struct superblock *) malloc(SUPERBLOCK_SIZE);
    read(fd, superblock, SUPERBLOCK_SIZE);
    uint32_t block_size = 1024 << superblock->s_log_block_size;

    void *block = malloc(block_size);

    cached_blocks = malloc(sizeof(void *) * (DEPTHS - 1));
    for (int i = 0; i < DEPTHS - 1; i++) {
        cached_blocks[i] = malloc(block_size);
    }

    struct group_descriptor *group_descriptor = block;

    load_block(group_descriptor, fd, 1 + superblock->s_first_data_block,
               block_size, 0);

    printf("block size: %d\n", block_size);
    printf("inode table: %d\n", group_descriptor->bg_inode_table);
    printf("block bitmap: %d\n", group_descriptor->bg_block_bitmap);
    printf("inodes per group: %d\n", superblock->s_inodes_per_group);
    printf("inode size: %d\n", superblock->s_inode_size);


    uint32_t inode_table_start = group_descriptor->bg_inode_table;
#if 0
    uint32_t inodes_per_block = 8 << superblock->s_log_block_size;
    printf("inodes per block: %d\n", inodes_per_block);
    for (int i = 0; i < superblock->s_inodes_per_group;
            i += inodes_per_block) {
        load_block(block, fd, inode_table_start + i / inodes_per_block,
                   block_size, 0);

        struct inode_table *inode_table = block;


        for (int j = 0; j < inodes_per_block; j++) {
            if (inode_table[j].i_mode) {
                printf("i_mode[%d]: %.8x; %.8lx\n", i + j,
                       inode_table[j].i_mode,
                       j * sizeof(struct inode_table) +
                       block_size * (inode_table_start +
                                     i / inodes_per_block));
                printf("\ti_size[%d]: %.8x\n", i+j, inode_table[j].i_size);

#if 1
                if((inode_table[j].i_mode >>12) == 0x08) {
                    uint8_t * buf = load_inode(fd, &inode_table[j], block_size);
                    printf("inode size: %d\n", inode_table[j].i_size);
                    for(int i=0; i<inode_table[j].i_size; i++) {
                        putchar(buf[i]);
                    }
                    free(buf);

                }
#endif

            }
        }
    }