Ejemplo n.º 1
0
Archivo: fs.c Proyecto: lhsieh814/OS
/* 
 * Resize a file.
 * This file should be opened (in the file descriptor table). The new size
 * should be larger than the old one (not supposed to shrink a file)
 */
static void sfs_resize_file(int fd, u32 new_size)
{
	/* the length of content that can be hold by a full frame (in bytes) */
	int frame_size = BLOCK_SIZE * SFS_FRAME_COUNT;
	/* old file size */
	int old_size = fdtable[fd].inode.size;
	/* how many frames are used before resizing */
	int old_nframe = (old_size + frame_size -1) / frame_size;
	/* how many frames are required after resizing */
	int new_nframe = (new_size + frame_size - 1) / frame_size;
	int i, j;
	blkid frame_bid = 0;
	sfs_inode_frame_t frame;
	blkid bid, content_bid;

	/* TODO: check if new frames are required */
	if (new_nframe - old_nframe > 0)
	{
		/* TODO: allocate a full frame */
		frame_bid = sfs_alloc_block();
		for (j = 0; j < SFS_FRAME_COUNT; j++)
		{
			content_bid = sfs_alloc_block();
			frame.content[j] = content_bid;

			// Empty content block
			char tmp[BLOCK_SIZE];
			memset(tmp, 0, BLOCK_SIZE);
			sfs_write_block((char*)&tmp, content_bid);
		}

		/* TODO: add the new frame to the inode frame list
		   Note that if the inode is changed, you need to write it to the disk
		*/
		bid = fdtable[fd].inode.first_frame;
		if (bid == 0)
		{
			fdtable[fd].inode.first_frame = frame_bid;
			sfs_write_block((char*)&fdtable[fd].inode, fdtable[fd].inode_bid);
		} 
		else 
		{
			for (i = 0; i < SFS_FRAME_COUNT; i++)
			{
				if (bid == 0)
				{
					frame.next = frame_bid;
					break;
				}
				sfs_read_block((char*)&frame, bid);
				bid = frame.next;

			}
		}
		// Write the new frame block
		sfs_write_block((char*)&frame, frame_bid);
	}

}
Ejemplo n.º 2
0
/*
 * Get the bids of content blocks that hold the file content starting from cur
 * to cur+length. These bids are stored in the given array.
 * The caller of this function is supposed to allocate the memory for this
 * array. It is guaranteed that cur+length<size
 * 
 * This function returns the number of bids being stored to the array.
 */
static u32 sfs_get_file_content(blkid *bids, int fd, u32 cur, u32 length)
{
	u32 start;		//Starting block of content
	u32 end;		//Ending block of content
	sfs_inode_t inode;
	sfs_inode_frame_t frame;
	blkid frame_bid;
	char tmp[BLOCK_SIZE];
	char frame_ptr[BLOCK_SIZE];
	u32 num_bids = 0;			//Counts the number of bids in the array

	//Get the bid of the desired frame
	fd_struct_t fd_struct = fdtable[fd];
	sfs_read_block(tmp, fd_struct.inode_bid);
	inode = *(sfs_inode_t*)tmp;

	frame_bid = inode.first_frame;

	start = cur / BLOCK_SIZE;
	end = (cur + length) / BLOCK_SIZE;

	u32 numBlocks = end - start + 1;
	u32 blocks_remaining = numBlocks;
	u32 original_blocks = blocks_remaining;

	//Start searching at an offset determined by the start position
	u32 block_count = start % SFS_FRAME_COUNT;	

	//This loop will iterate through the frames
	while(blocks_remaining > 0){

		sfs_read_block(frame_ptr, frame_bid);
		frame = *(sfs_inode_frame_t*)frame_ptr;
		
		//Iterate through the content array of the frame
		while(block_count < SFS_FRAME_COUNT && blocks_remaining > 0){

			//Allocate a content block if it is not yet set
			if(frame.content[block_count] == 0){
				frame.content[block_count] = sfs_alloc_block();
			}

			bids[num_bids] = frame.content[block_count];
			num_bids++;
			block_count++;
			blocks_remaining--;
		}

		sfs_write_block(&frame, frame_bid);

		if(blocks_remaining != 0){
			frame_bid = frame.next;
			block_count = 0;
		}
				
	}
	return num_bids;
}
Ejemplo n.º 3
0
Archivo: fs.c Proyecto: lhsieh814/OS
/*
 * Create a new directory and return 0 on success.
 * If the dir already exists, return -1.
 */
int sfs_mkdir(char *dirname)
{
	/* TODO: test if the dir exists */
	/* TODO: insert a new dir to the linked list */
	blkid nextBlkid;
	sfs_dirblock_t dir, new_dir;

	// Try finding the directory if it exists
	blkid bid = sfs_find_dir(dirname);

	if (bid == 0)
	{
		// Directory does not exists, create a new one
		bid = sfs_alloc_block();
		// Add the new directory to the directory linked list
		nextBlkid = sb.first_dir;

		if (nextBlkid == 0) {
			sb.first_dir = bid;
			sfs_write_block((char*)&sb, 0);
		} 
		else 
		{
			sfs_read_block((char*)&dir, nextBlkid);

			while (dir.next_dir != 0) 
			{
				nextBlkid = dir.next_dir;
				sfs_read_block((char*)&dir, nextBlkid);
			}

			dir.next_dir = bid;
			sfs_write_block((char*)&dir, nextBlkid);
		}
		// Create new directory with next_dir=0 and dir_name
		new_dir.next_dir = 0;
		memset(new_dir.dir_name, 0, sizeof(new_dir.dir_name));	// Empty dir_name
		memset(new_dir.inodes, 0, sizeof(new_dir.inodes));	// Empty inodes
		strcpy(new_dir.dir_name, dirname);	// Copy new dir_name
		sfs_write_block((char*)&new_dir, bid);
		
		return 0;
	}	

	// Directory already exists
	return -1;
}
Ejemplo n.º 4
0
W
sfs_i_write (struct inode	*ip,
	     W			start,
	     B			*buf,
	     W			size,
	     W			*rsize)
{
#ifdef notdef
  B			blockbuf[ip->i_fs->fs_private.sfs_fs.sfs_blocksize];	/* GCC の拡張機能を使っている */
#endif
  int			copysize;
  int			offset;
  int			retsize;
  int			filesize;
  ID			fd;
  struct fs *fsp;
  W bn;
  W cn;
  B *cbuf;

#ifdef FMDEBUG
  printk ("sfs_i_write:(start = %d, size = %d)\n", start, size);		/* */
#endif

  *rsize = 0;
  retsize = size;
  filesize = start + retsize;
  fd = ip->i_device;
  fsp = ip->i_fs;

#ifdef FMDEBUG
  printk ("sfs_i_write: size = %d\n", size);					/* */
#endif

  while (size > 0)
    {
#ifdef FMDEBUG
      printk ("%s\n",
	      (sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
				  start / fsp->fs_blksize) <= 0) ?
	      "allocate block" : "read block");
#endif

      if (sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
			     start / fsp->fs_blksize) <= 0)
	{
	  /* ファイルサイズを越えて書き込む場合には、新しくブロックをアロケートする
	   */
	  bn = sfs_set_block_num (fd, fsp, &(ip->i_private.sfs_inode),
				  start / fsp->fs_blksize,
				  sfs_alloc_block (fd, fsp));
/*
 *   ip->sfs_i_direct[start / fsp->fs_blksize] = alloc_block (fd, fsp);
 */
#ifdef notdef
	  bzero (blockbuf, fsp->fs_blksize);
#else
	  if (bn < 0) {
	    return (EP_IO);
	  }
	  sfs_get_cache(fd, bn, &cn, &cbuf);
#endif
	}
      else
	{
#ifdef FMDEBUG
	  printk ("read block %d\n",
		  sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
				     start / fsp->fs_blksize));
#endif
	  bn = sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
				  start / fsp->fs_blksize);
	  if (bn < 0) {
	    return (EP_IO);
	  }
#ifdef notdef
	  sfs_read_block (fd, bn, fsp->fs_blksize, blockbuf);
#else
	  sfs_get_cache(fd, bn, &cn, &cbuf);
#endif
	}

      /* 読み込んだブロックの内容を更新する
       */
      offset = start % fsp->fs_blksize;
      copysize = MIN (fsp->fs_blksize - offset, size);

#ifdef FMDEBUG
      printk ("*** read block contents ***\n");
      {
	int	i;
	char	tmpbuf[2];

	tmpbuf[1] = '\0';
	printk ("copy size: %d\n", copysize);
	for (i = 0; i < copysize; i++)
	  {
	    tmpbuf[0] = blockbuf[i];
	    printk ("%s", tmpbuf);
	  }
      }
#endif

#ifdef notdef
      bcopy (buf, &blockbuf[offset], copysize);
#else
      bcopy(buf, &cbuf[offset], copysize);
#endif

#ifdef notdef
      printk ("sfs write block: block number = %d\n", 
	      sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
				 start / fsp->fs_blksize));		/* */
#endif

      /* 更新したブロックを書き込む
       */
#ifdef notdef
      bn = sfs_get_block_num (fd, fsp, &(ip->i_private.sfs_inode),
			      start / fsp->fs_blksize);
      if (bn < 0) {
	return (EP_IO);
      }
      sfs_write_block (fd, bn, fsp->fs_blksize, blockbuf);
#else
      sfs_put_cache(cn, 1);
#endif

      buf += copysize;
      start += copysize;
      size -= copysize;
    }

  /* cache の sync をここで行う必要はあるか? */
  sfs_sync_cache(fd);
  
  /* もし、書き込みをおこなった後にファイルのサイズが増えていれば、
   * サイズを更新して inode を書き込む。
   */
  if (filesize > ip->i_private.sfs_inode.sfs_i_size)
    {
      ip->i_size = filesize;
      ip->i_size_blk = 
	ROUNDUP(filesize, fsp->fs_blksize)/fsp->fs_blksize;
      ip->i_dirty = 1;
      /* これは deallocate の中で処理するのが普通 */
      sfs_i_sync(ip);
    }
  else if (filesize < ip->i_private.sfs_inode.sfs_i_size) {
    sfs_i_truncate (ip, filesize);
  }
  *rsize = retsize - size;

#ifdef FMDEBUG
  printk ("write size: %d bytes\n", *rsize);
#endif

  return (EP_OK);
}
Ejemplo n.º 5
0
/*
 * Open a file. If it does not exist, create a new one.
 * Allocate a file desriptor for the opened file and return the fd.
 */
int sfs_open(char *dirname, char *name)
{
	blkid dir_bid = 0, inode_bid = 0;
	char inode_ptr[BLOCK_SIZE];
	sfs_inode_t inode;
	sfs_dirblock_t dir;

	fd_struct_t *fd_ptr;
	fd_struct_t fd;

	//Finds a free file descriptor number
	int fd_num = 0;	

	while(fd_num < SFS_MAX_OPENED_FILES){
		fd = fdtable[fd_num];

		if(fd.valid == 0){
			fd.valid = 1;
			fd.cur = 0;
			
			break;

		} else if(fd_num == SFS_MAX_OPENED_FILES - 1){ 
			printf("The maximum number of files are already in use\n");
			return -1;

		} else{
			fd_num++;	
		}
	}

	
	//Find the directory in which the specified file is contained
	dir_bid = sfs_find_dir(dirname);
	sfs_read_block(&dir, dir_bid);
	fd.dir_bid = dir_bid;

	//Iterate through all the indices of the inodes to find the file
	int count = 0;
	int fd_set = 0;
	int free_inode = -1;
	char *file_name;
	
	while(count < SFS_DB_NINODES){
	
		inode_bid = dir.inodes[count];

		if(inode_bid == 0){
			if(free_inode == -1){
				free_inode = count;
			}
			count++;
			continue;
		}

		//Read the inode at the specified index
		sfs_read_block(inode_ptr, inode_bid);
		inode = *(sfs_inode_t*)inode_ptr;

		file_name = inode.file_name;

		if(strcmp(file_name, name) == 0){
			fd.inode = inode;
			fd.inode_bid = inode_bid;
			fd_set = 1;

			break;
		}
		count++;
	}

	//Create a new file
	if(fd_set == 0){

		//Allocate a block for the inode
		blkid new_inode_bid = sfs_alloc_block();
		
		//Put the inode bid into the directory inode array
		dir.inodes[free_inode] = new_inode_bid;
		sfs_write_block(&dir, dir_bid);

		strcpy(inode.file_name, name);
	
		//Allocate a block for the first frame	
		blkid new_frame_bid = sfs_alloc_block();
		inode.first_frame = new_frame_bid;
		inode.size = 0;

		//Place the inode in the file descriptor array
		fd.inode = inode;
		
		fd.inode_bid = new_inode_bid;

		sfs_write_block(&inode, new_inode_bid);

		sfs_read_block(&inode, new_inode_bid);

		//Allocate a block for the first content block of the frame
		char tmp[BLOCK_SIZE];
		sfs_read_block(tmp, new_frame_bid);
		sfs_inode_frame_t new_frame = *(sfs_inode_frame_t*)tmp;
		
		blkid new_content_bid = sfs_alloc_block();
		new_frame.content[0] = new_content_bid;

		sfs_write_block(&new_frame, new_frame_bid);
	}

	//Set the file descriptor in the file descriptor table	
	fdtable[fd_num] = fd;

	return fd_num;
}
Ejemplo n.º 6
0
/*
 * Create a new directory and return 0 on success.
 * If the dir already exists, return -1.
 */
int sfs_mkdir(char *dirname)
{
	//Check if the directory has a valid name
	if(sizeof(dirname) > 120){
		printf("Dir name exceeds max allowed characters.\n");
		return -1;
	}

	//Test if the dir exists
	if(sfs_find_dir(dirname) != 0){
		return -1;
	} else{	
		//Initialize variables
		blkid next_dir_id;
		char block_ptr[BLOCK_SIZE];
		sfs_dirblock_t dir;
	
		//Allocate space for a new block
		blkid new_bid = sfs_alloc_block();

		//Set the next_dir in the last directory block
		next_dir_id = sb.first_dir;
		sfs_read_block(block_ptr, next_dir_id);
		dir = *(sfs_dirblock_t*)block_ptr;

		if(next_dir_id == 0){
			sb.first_dir = new_bid;
			sfs_write_block(&sb, 0);
		} else{
			while(next_dir_id != 0){	
				sfs_read_block(block_ptr, next_dir_id);
				dir = *(sfs_dirblock_t*)block_ptr;

				//Next dir is zero, set it to the next available block
				if(dir.next_dir == 0){
					blkid modified_dir_id = next_dir_id;

					sfs_read_block(block_ptr, modified_dir_id);
					dir = *(sfs_dirblock_t*)block_ptr;
										
					dir.next_dir = new_bid;

					//A value was changed, so this must be flushed to disk
					sfs_write_block(&dir, modified_dir_id);
					next_dir_id = 0;

				} else{
					next_dir_id = dir.next_dir;	
				}
			}
		}

		//Create a temporary directory and initialize its members
		//Set the next dir, name, and set all its inodes to zero
		sfs_dirblock_t temp_dir;
		temp_dir.next_dir = 0;
		strcpy(temp_dir.dir_name, dirname);
		memset(&temp_dir.inodes[0], 0, SFS_DB_NINODES * sizeof(blkid));

		//Write the temporary block into the disk
		sfs_write_block(&temp_dir, new_bid);
	}
	return 0;
}
Ejemplo n.º 7
0
/* 
 * Resize a file.
 * This file should be opened (in the file descriptor table). The new size
 * should be larger than the old one (not supposed to shrink a file)
 */
static void sfs_resize_file(int fd, u32 new_size)
{
	/* the length of content that can be hold by a full frame (in bytes) */
	int frame_size = BLOCK_SIZE * SFS_FRAME_COUNT;
	/* old file size */
	int old_size = fdtable[fd].inode.size;
	/* how many frames are used before resizing */
	int old_nframe = (old_size + frame_size -1) / frame_size;
	//Opening a file always results in at least one frame
	if(old_nframe == 0){
		old_nframe++;
	}
	
	/* how many frames are required after resizing */
	int new_nframe = (new_size + frame_size - 1) / frame_size;
	
	int i, j;
	char inode_ptr[BLOCK_SIZE];
	char tmp[BLOCK_SIZE];
	
	sfs_inode_t inode;
	
	sfs_read_block(inode_ptr, fdtable[fd].inode_bid);
	inode = *(sfs_inode_t*)inode_ptr;

	blkid frame_bid = inode.first_frame;
	sfs_inode_frame_t frame;

	/* TODO: check if new frames are required */
	int count = old_nframe;
	while(count < new_nframe){
		
		sfs_read_block(tmp, frame_bid);
		frame = *(sfs_inode_frame_t*)tmp;

		blkid new_frame_bid = sfs_alloc_block();

		//Update the next frame bid for the current frame
		frame.next = new_frame_bid;	
		sfs_write_block(&frame, frame_bid);

		//Read the new frame bid and add the content blocks
		sfs_read_block(tmp, new_frame_bid);
		frame = *(sfs_inode_frame_t*)tmp;

		for(i = 0; i < SFS_FRAME_COUNT; i++){
			frame.content[i] = sfs_alloc_block();
		}

		sfs_write_block(&frame, new_frame_bid);

		frame_bid = new_frame_bid;
		count++;
	}

		sfs_read_block(tmp, inode.first_frame);
		frame = *(sfs_inode_frame_t*)tmp;

	inode.size = new_size;
	sfs_write_block(&inode, fdtable[fd].inode_bid);

	sfs_read_block(tmp, fdtable[fd].inode_bid);
	inode = *(sfs_inode_t*)tmp;
}
Ejemplo n.º 8
0
Archivo: fs.c Proyecto: lhsieh814/OS
/*
 * Open a file. If it does not exist, create a new one.
 * Allocate a file desriptor for the opened file and return the fd.
 */
int sfs_open(char *dirname, char *name)
{
	blkid dir_bid = 0, inode_bid = 0;
	sfs_inode_t *inode, new_inode;
	sfs_dirblock_t dir;
	int fd;
	int i;

	/* TODO: find a free fd number */
	for (i = 0; i < SFS_MAX_OPENED_FILES; i++)
	{
		if (fdtable[i].valid == 0) 
		{
			// Found a free fd
			fd = i;
			break;
		}
	}

	/* TODO: find the dir first */
	dir_bid = sfs_find_dir(dirname);
	if (dir_bid == 0)
	{
		printf("ERROR: the directory does not exists\n");
		return -1;
	}
	sfs_read_block((char*)&dir, dir_bid);

	/* TODO: traverse the inodes to see if the file exists.
	   If it exists, load its inode. Otherwise, create a new file.
	*/
	for (i = 0; i < SFS_DB_NINODES; i++) 
	{
		if (dir.inodes[i] != 0)
		{
			sfs_read_block((char*)&new_inode, dir.inodes[i]);
			
			if (strcmp(new_inode.file_name, name) == 0) {
				inode_bid = dir.inodes[i];
				break;
			}
		}
	}

	if (inode_bid == 0)
	{
		// Allocate a block to store new inode
		inode_bid = sfs_alloc_block();
		// Find empty space in dir.inodes[] and set inode_bid
		for (i=0; i<SFS_DB_NINODES; i++)
		{
			if (dir.inodes[i] == 0) 
			{
				dir.inodes[i] = inode_bid;
				break;
			}
		}
		// Did not find the inode, create a new one
		new_inode.first_frame = 0;
		new_inode.size = 0;
		memset(new_inode.file_name, 0, sizeof(new_inode.file_name));
		strcpy(new_inode.file_name, name);
		sfs_write_block((char*)&new_inode, inode_bid);
		sfs_write_block((char*)&dir, dir_bid);
	}

	/* TODO: create a new file */
	fdtable[fd].dir_bid = dir_bid;
	fdtable[fd].inode_bid = inode_bid;
	fdtable[fd].inode = new_inode;
	fdtable[fd].cur = 0;
	fdtable[fd].valid = 1;

	return fd;
}