Exemplo n.º 1
0
Arquivo: lfs.c Projeto: kitalda/lfs
/**
 * Adds the buffer as a segment to the appropriate place in the log, then clears
 * the buffer.
 */
int main(int argc, char *argv[]) {
	int res = 0;

//printf("%s\n", "lfs_main");

	log_system = malloc(sizeof(struct file_system));
	if (!log_system) {
		perror("main malloc");
	} else {
		////printf("%s\n", "lfs_main: 1");
		log_system->log_file_name = "/tmp/semihugefile.file";
		if (!log_system->log_file_name) {
			perror("Filename not allocated/set");
		} else {
			////printf("%s\n", "lfs_main: 2");
			log_system->buffer = malloc(SEGMENT_SIZE);
			if (!log_system->buffer) {
				res = -ENOMEM;
			} else {
				memset(log_system->buffer_summary, 0, BLOCKS_PR_SEGMENT);
				memset(log_system->buffer, 0, SEGMENT_SIZE);
				log_system->next_segment = 0;
				log_system->used_segments = 0;
				res = init_inode_table(log_system);
				////printf("%s\n", "lfs_main: 4");
			}
		}
	}
	if (res) {
		//printf("main fail: %d\n", res);
		//TODO error handling
	}
	return fuse_main(argc, argv, &lfs_oper);
}
Exemplo n.º 2
0
static void init_fs(){
    /* u8 fsbuf[SECTOR_SIZE]; */
    MESSAGE message;
    message.type=INFO_FS_DEVICE;
    message.device=ROOT_DEVICE;
    assert(dd_map[DRIVER(ROOT_DEVICE)].driver_pid!=PID_INVALID,"");
    send_receive(BOTH,dd_map[DRIVER(ROOT_DEVICE)].driver_pid,&message);

    //如果系统已经是要求的系统,就不需要在格式化系统了
    get_super_block(ROOT_DEVICE,&super_block);
    /* if(super_block.magic!=MAGIC_V1) */{
        mkfs();
        get_super_block(ROOT_DEVICE,&super_block);
    }
    
    init_inode_table();
    init_file_descriptor_table();
#ifdef DEBUG_FS
    //write test
    /* memset(fsbuf,0x23,SECTOR_SIZE); */
    /* WRITE_SECTOR(ROOT_DEVICE,fsbuf,1); */
    //read test
    u8 fsbuf[SECTOR_SIZE];
    READ_SECTOR(ROOT_DEVICE,fsbuf,1);
    printl("read test:\nfsbuf[0]=%x fsbuf[1]=%x fsbuf[2]=%x fsbuf[3]=%x\n",fsbuf[0],fsbuf[1],fsbuf[2],fsbuf[3]);
#endif
}
Exemplo n.º 3
0
static int
execute_update_commands(WIMStruct *wim,
			const struct wimlib_update_command *cmds,
			size_t num_cmds,
			int update_flags)
{
	struct wim_inode_table *inode_table;
	struct wim_sd_set *sd_set;
	struct list_head unhashed_streams;
	struct update_command_journal *j;
	union wimlib_progress_info info;
	int ret;

	if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
		/* If we have at least one "add" command, create the inode and
		 * security descriptor tables to index new inodes and new
		 * security descriptors, respectively.  */
		inode_table = alloca(sizeof(struct wim_inode_table));
		sd_set = alloca(sizeof(struct wim_sd_set));

		ret = init_inode_table(inode_table, 9001);
		if (ret)
			goto out;

		ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
		if (ret)
			goto out_destroy_inode_table;

		INIT_LIST_HEAD(&unhashed_streams);
	} else {
		inode_table = NULL;
		sd_set = NULL;
	}

	/* Start an in-memory journal to allow rollback if something goes wrong
	 */
	j = new_update_command_journal(num_cmds,
				       &wim_get_current_image_metadata(wim)->root_dentry,
				       wim->lookup_table);
	if (!j) {
		ret = WIMLIB_ERR_NOMEM;
		goto out_destroy_sd_set;
	}

	info.update.completed_commands = 0;
	info.update.total_commands = num_cmds;
	ret = 0;
	for (size_t i = 0; i < num_cmds; i++) {
		DEBUG("Executing update command %zu of %zu (op=%"TS")",
		      i + 1, num_cmds, update_op_to_str(cmds[i].op));
		info.update.command = &cmds[i];
		if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
			ret = call_progress(wim->progfunc,
					    WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
					    &info, wim->progctx);
			if (ret)
				goto rollback;
		}

		switch (cmds[i].op) {
		case WIMLIB_UPDATE_OP_ADD:
			ret = execute_add_command(j, wim, &cmds[i], inode_table,
						  sd_set, &unhashed_streams);
			break;
		case WIMLIB_UPDATE_OP_DELETE:
			ret = execute_delete_command(j, wim, &cmds[i]);
			break;
		case WIMLIB_UPDATE_OP_RENAME:
			ret = execute_rename_command(j, wim, &cmds[i]);
			break;
		}
		if (unlikely(ret))
			goto rollback;
		info.update.completed_commands++;
		if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
			ret = call_progress(wim->progfunc,
					    WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
					    &info, wim->progctx);
			if (ret)
				goto rollback;
		}
		next_command(j);
	}

	commit_update(j);
	if (inode_table) {
		struct wim_image_metadata *imd;

		imd = wim_get_current_image_metadata(wim);

		list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
		inode_table_prepare_inode_list(inode_table, &imd->inode_list);
	}
	goto out_destroy_sd_set;

rollback:
	if (sd_set)
		rollback_new_security_descriptors(sd_set);
	rollback_update(j);
out_destroy_sd_set:
	if (sd_set)
		destroy_sd_set(sd_set);
out_destroy_inode_table:
	if (inode_table)
		destroy_inode_table(inode_table);
out:
	return ret;
}
Exemplo n.º 4
0
void mksfs(int fresh){
	//Implement mksfs here
	if (fresh == 1){
		//deletes fs if it already exists
		if(access(DISK_FILE, F_OK) != -1){
			unlink(DISK_FILE);			
		}
		//fs creation
		printf("Initalizing sfs\n");
		init_fresh_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS);
        //zero_everything();
		// writes superblock to the first block
        printf("Writing superblocks\n");
        init_superblock();
		write_blocks(0, 1, &sb);
		// write the inode table to the 2nd block
        printf("Writing inode table\n");
		init_inode_table();
        //add_root_dir_inode();
        //add_dummy_file_inode();
		//write_blocks(1, 1, &inode_table);
		// creates free list
		init_free_list();
        // write root directory data to the 3rd block
        printf("Writing root dir\n");		
		init_root_dir();

		//allocate memory for directory inode
		inode_t *inode = malloc(ADJUST((MAX_FILES+1)*sizeof(inode_t)));
		read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode);
		if(inode == NULL){
			return;
		}
		//set first inode to point to directory
		inode[0].size = DIRECTORY_SIZE*BLOCK_SIZE;
		inode[0].link_cnt = DIRECTORY_SIZE;
		inode[0].exists = 1;
		
		//check to see if we need to use ref_ptr
		//if(DIRECTORY_SIZE > 12){
			//inode[0].ref_ptr = search();
			//setAlloc(inode[0].ref_ptr);
			//unsigned int *buffer = malloc(BLOCK_SIZE);
			//write_blocks(19 + inode[0].ref_ptr, 1, buffer);
			//free(buffer);
		
		//assign the pointers the location of directory files
		int i;
		for(i = 0; i < DIRECTORY_SIZE; i++){
			if(i > 11){
				unsigned int *buffer = malloc(BLOCK_SIZE);
				read_blocks(19+ inode[0].ref_ptr, 1, buffer);
				buffer[i - 12] = 2 + i;
				write_blocks(19+ inode[0].ref_ptr, 1, buffer);
				free(buffer);
			} 
			else{
				inode[0].data_ptrs[i] = 2 + i;
			}
		}
		//update inode and free memory
		write_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode);
		free(inode);		
	}
	
	else if (fresh == 0){
		if(init_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS) != 0){
			printf("Error initializing disk\n");
			return;
		}
	}
	//allocate main memory for filesystem data structures
	int *superblock = malloc(BLOCK_SIZE*SUPERBLOCK_SIZE);

	if(superblock == NULL){
		printf("Error allocating memory for superblock\n");
		return;
	}
	read_blocks(0, SUPERBLOCK_SIZE, superblock);
	//allocate main memory for directory
	root_dir = malloc(ADJUST(sizeof(dir_entry_t)*MAX_FILES));
	
	if(root_dir == NULL){
		printf("Error allocating memory for root directory\n");
		return;
	}
	read_blocks(2, DIRECTORY_SIZE, root_dir);
	//allocate memory for inode table
	inode_table = malloc(ADJUST(sizeof(inode_t)*(MAX_FILES+1)));
	
	if(inode_table == NULL){
		printf("Error allocating memory for inode table");
		return;
	}

	read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode_table);
	fileCount = 0;
	fd_table = NULL;
	return;		
}
Exemplo n.º 5
0
int main (int argc, char **argv)
{
	int rc, c, opt_ind;
	char *device_name;
	size_t devblock_size;

	struct mfs_sb_info sb;

	/* Default is MinixFS V3 */
	sb.magic = MFS_MAGIC_V3;
	sb.fs_version = 3;

	/* Default block size is 4Kb */
	sb.block_size = MFS_MAX_BLOCKSIZE;
	sb.dirsize = MFS3_DIRSIZE;
	sb.n_inodes = 0;
	sb.longnames = false;
	sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);

	if (argc == 1) {
		help_cmd_mkmfs(HELP_SHORT);
		printf("Incorrect number of arguments, try `mkmfs --help'\n");
		exit(0);
	}

	for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
		c = getopt_long(argc, argv, "lh12b:i:",
		    long_options, &opt_ind);
		switch (c) {
		case 'h':
			help_cmd_mkmfs(HELP_LONG);
			exit(0);
		case '1':
			sb.magic = MFS_MAGIC_V1;
			sb.block_size = MFS_BLOCKSIZE;
			sb.fs_version = 1;
			sb.ino_per_block = V1_INODES_PER_BLOCK;
			sb.dirsize = MFS_DIRSIZE;
			break;
		case '2':
			sb.magic = MFS_MAGIC_V2;
			sb.block_size = MFS_BLOCKSIZE;
			sb.fs_version = 2;
			sb.ino_per_block = V2_INODES_PER_BLOCK;
			sb.dirsize = MFS_DIRSIZE;
			break;
		case 'b':
			sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
			break;
		case 'i':
			sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
			break;
		case 'l':
			sb.longnames = true;
			sb.dirsize = MFSL_DIRSIZE;
			break;
		}
	}

	if (sb.block_size < MFS_MIN_BLOCKSIZE || 
	    sb.block_size > MFS_MAX_BLOCKSIZE) {
		printf(NAME ":Error! Invalid block size.\n");
		exit(0);
	} else if (!is_power_of_two(sb.block_size)) {
		/* Block size must be a power of 2. */
		printf(NAME ":Error! Invalid block size.\n");
		exit(0);
	} else if (sb.block_size > MFS_BLOCKSIZE && 
	    sb.fs_version != 3) {
		printf(NAME ":Error! Block size > 1024 is "
		    "supported by V3 filesystem only.\n");
		exit(0);
	} else if (sb.fs_version == 3 && sb.longnames) {
		printf(NAME ":Error! Long filenames are supported "
		    "by V1/V2 filesystem only.\n");
		exit(0);
	}

	if (sb.block_size == MFS_MIN_BLOCKSIZE)
		shift = 1;
	else if (sb.block_size == MFS_MAX_BLOCKSIZE)
		shift = 3;
	else
		shift = 2;

	argv += optind;

	device_name = argv[0];

	if (!device_name) {
		help_cmd_mkmfs(HELP_LONG);
		exit(0);
	}

	rc = loc_service_get_id(device_name, &service_id, 0);
	if (rc != EOK) {
		printf(NAME ": Error resolving device `%s'.\n", device_name);
		return 2;
	}

	rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
	if (rc != EOK)  {
		printf(NAME ": Error initializing libblock.\n");
		return 2;
	}

	rc = block_get_bsize(service_id, &devblock_size);
	if (rc != EOK) {
		printf(NAME ": Error determining device block size.\n");
		return 2;
	}

	rc = block_get_nblocks(service_id, &sb.dev_nblocks);
	if (rc != EOK) {
		printf(NAME ": Warning, failed to obtain "
		    "block device size.\n");
	} else {
		printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
		    sb.dev_nblocks);
	}

	if (devblock_size != 512) {
		printf(NAME ": Error. Device block size is not 512 bytes.\n");
		return 2;
	}

	/* Minimum block size is 1 Kb */
	sb.dev_nblocks /= 2;

	printf(NAME ": Creating Minix file system on device\n");
	printf(NAME ": Writing superblock\n");

	/* Initialize superblock */
	if (init_superblock(&sb) != EOK) {
		printf(NAME ": Error. Superblock initialization failed\n");
		return 2;
	}

	printf(NAME ": Initializing bitmaps\n");

	/* Initialize bitmaps */
	if (init_bitmaps(&sb) != EOK) {
		printf(NAME ": Error. Bitmaps initialization failed\n");
		return 2;
	}

	printf(NAME ": Initializing the inode table\n");

	/* Init inode table */
	if (init_inode_table(&sb) != EOK) {
		printf(NAME ": Error. Inode table initialization failed\n");
		return 2;
	}

	printf(NAME ": Creating the root directory inode\n");

	/* Make the root inode */
	if (sb.fs_version == 1)
		rc = make_root_ino(&sb);
	else
		rc = make_root_ino2(&sb);

	if (rc != EOK) {
		printf(NAME ": Error. Root inode initialization failed\n");
		return 2;
	}

	/* Insert directory entries . and .. */
	if (insert_dentries(&sb) != EOK) {
		printf(NAME ": Error. Root directory initialization failed\n");
		return 2;
	}

	block_fini(service_id);

	return 0;
}