Example #1
0
long get_start_address(cs1550_directory_entry directory, int file_index)	//gives the new start address(if needed) of file
{
	long count = 0;
	long start_address = directory.files[ file_index ].nStartBlock;	//holds the start address of the file
	long error_checker_address = start_address;	

	while( count <= directory.files[ file_index ].fsize )
	{
		int check = is_next_block_free(start_address);
		if(check != 0)	//next block is free
		{
			count+= 512;	//add a block size for every iteration
		}
		
		else
		{
			start_address = find_next_free_block(start_address);	//finds next free block and sets block_start to that address
			if(error_checker_address == start_address)	//signalfies out of mememory
			{
				perror("OUT OF MEMORY!!\n");	//out of memeory
				break;
			}
			count = 0;	//reset count
		}
	}

	return start_address;	//returns the new start address
}
Example #2
0
uint8_t* ramdisk_init(){
	int i;
	uint8_t* ramdisk;
	int root_bid;
	struct rd_inode* root_inode;
	struct rd_super_block* InitSuperBlock;
#ifdef UL_DEBUG
	if(!(ramdisk=(uint8_t*)malloc(RAMDISK_SIZE*sizeof(uint8_t)))){
		fprintf(stderr,"No sufficient mem space for ramdisk!\n");
		exit(-1);
	}
#endif

#ifndef UL_DEBUG
	if(!(ramdisk=(uint8_t*)vmalloc(RAMDISK_SIZE*sizeof(uint8_t)))){
		printk("<1> No sufficient mem space for ramdisk!\n");
		return NULL;
	}
#endif

	//Nullify all the data in ramdisk
	memset(ramdisk,0,RAMDISK_SIZE);

	//Init the bitmap
	for(i=0;i<=(BITMAP_LIMIT+1)/RD_BLOCK_SIZE;i++){
		set_bitmap(ramdisk,i);
	}

	//Init the root directory
	root_bid=find_next_free_block(ramdisk);//BlockNO for root dir
#ifdef UL_DEBUG
	if(!(root_inode=(struct rd_inode*)malloc(sizeof(struct rd_inode)))){
		fprintf(stderr, "No sufficient mem space for root dir!\n");
		exit(-1);
	}
#endif
#ifndef UL_DEBUG
	if(!(root_inode=(struct rd_inode*)vmalloc(sizeof(struct rd_inode)))){
		printk("<1> No sufficient mem space for root dir!\n");
		return NULL;
	}
#endif

	root_inode->type=0;
	root_inode->size=0;
	root_inode->BlockPointer[0]=root_bid;
	update_inode(ramdisk,0,root_inode);
	
	//Init the superblock
#ifdef UL_DEBUG
	if(!(InitSuperBlock=(struct rd_super_block*)malloc(sizeof(struct rd_super_block)))){
		fprintf(stderr,"No sufficient mem\n");
		exit(-1);
	}
#endif 
#ifndef UL_DEBUG
	if(!(InitSuperBlock=(struct rd_super_block*)vmalloc(sizeof(struct rd_super_block)))){
		printk("<1> No sufficient mem\n");
		return NULL;
	}
#endif 

	InitSuperBlock->FreeBlockNum=BLOCK_NUM-(BITMAP_LIMIT+1)/RD_BLOCK_SIZE;
	InitSuperBlock->FreeInodeNum=INODE_NUM-1;//The root dir takes one inode
	memset(InitSuperBlock->InodeBitmap,0,INODEBITMAP_SIZE);
	update_superblock(ramdisk,InitSuperBlock);
	
	//Init the inode bitmap in superblock
	set_inode_bitmap(ramdisk,0);

#ifndef UL_DEBUG
	vfree(root_inode);
#endif
	return ramdisk;	

}
Example #3
0
void
create_new_file(ProDisk *disk, int dir_block, int storage_type, char *name,
	int file_type, word32 creation_time, int version, int min_version,
	int access, int aux_type, word32 last_mod, word32 eof)
{
	Vol_hdr	*vol_ptr;
	int	val;
	Directory *dir_ptr;
	File_entry *file_ptr;
	int	file_count;
	int	pos;
	int	last_pos;
	int	done;
	int	next_blk;
	int	name_len;

	name_len = strlen(name);
	dir_ptr = disk_read_dir(disk, dir_block);
	next_blk = dir_block;
	val = dir_ptr->file_entries[0].storage_type_name_len;
	last_pos = 13;
	pos = 0;
	if(((val & 0xf0) == 0xf0) || ((val & 0xf0) == 0xe0)) {
		/* vol dir or subdir header */
		vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]);
		file_count = get_l2byte(&(vol_ptr->file_count));
		pos = 1;
		last_pos = vol_ptr->entries_per_block;
	} else {
		printf("dir_block: %04x not subdir or voldir\n", dir_block);
		exit(6);
	}

	vol_ptr = 0;

	done = 0;
	while(!done) {
		file_ptr = &(dir_ptr->file_entries[pos]);
		if(((file_ptr->storage_type_name_len) & 0xf0) == 0) {
			/* Got it! */
			file_ptr->storage_type_name_len =
				(storage_type << 4) | name_len;
			strncpy((char *)file_ptr->file_name, name, 15);
			file_ptr->file_type = file_type;
			set_l2byte(&(file_ptr->key_pointer),
				find_next_free_block(disk));
			set_l2byte(&(file_ptr->blocks_used), 0);
			set_pro_time(&(file_ptr->creation_time),
				creation_time);
			file_ptr->version = version;
			file_ptr->min_version = min_version;
			file_ptr->access = access;
			set_l2byte(&(file_ptr->aux_type), aux_type);
			set_pro_time(&(file_ptr->last_mod), last_mod);
			set_l2byte(&(file_ptr->header_pointer),
					dir_block);
			set_l3byte(&(file_ptr->eof), eof);
			disk_write_dir(disk, next_blk);

			dir_ptr = disk_read_dir(disk, dir_block);
			vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]);
			set_l2byte(&(vol_ptr->file_count), file_count+1);
			disk_write_dir(disk, dir_block);

			disk_read_dir(disk, next_blk);
			/* re-read dir so that ptrs are set up right */
			disk->file_ptr = file_ptr;
			disk->file_open = 1;
			disk->ind_blk_num = -1;
			disk->master_ind_blk_num = -1;
			done = 1;
			break;
		} else {
			/* check to make sure name is unique */
			if((file_ptr->storage_type_name_len & 0x0f)== name_len){
				if(!memcmp(file_ptr->file_name, name,name_len)){
					printf("Name %s already on disk!\n",
						name);
					exit(8);
				}
			}
			pos++;
			if(pos >= last_pos) {
				/* Go to next block */
				next_blk = get_l2byte(&(dir_ptr->next_blk));
				if(next_blk) {
					dir_ptr = disk_read_dir(disk, next_blk);
					pos = 0;
				} else {
					printf("Top directory full!\n");
					exit(2);
				}
			}
		}
	}
}