コード例 #1
0
ファイル: inode.c プロジェクト: kcm1700/ChoPintOS
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset)
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;

  while (size > 0)
    {
      /* Disk sector to read, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      cache_read (fs_cache, sector_idx, buffer + bytes_read,
        sector_ofs, chunk_size);

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }

  return bytes_read;
}
コード例 #2
0
ファイル: inode.c プロジェクト: IVY-bug/Pintos4
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  //uint8_t *bounce = NULL;
  //printf("@@@@@@@@@@@@ file_read start @@@@@@@@@@@\n");
  struct inode_disk *inode_d;
  inode_d = malloc(sizeof(struct inode_disk));
  while (size > 0) 
    {
      cache_read(inode->sector, inode_d, DISK_SECTOR_SIZE, 0);
      /* Disk sector to read, starting byte offset within sector. */
      int sector_idx = byte_to_sector (inode, offset);
      //printf("@@@@@@@@@ sector_idx : %d @@@@@@@@@@\n",sector_idx);
      if(sector_idx == -1) 
        break;
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;
      cache_read(sector_idx, buffer + bytes_read, chunk_size, sector_ofs);
      /*
      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
        {
           Read full sector directly into caller's buffer. 
          disk_read (filesys_disk, sector_idx, buffer + bytes_read); 
        }
      else 
        {
           Read sector into bounce buffer, then partially copy
             into caller's buffer. 
          
          if (bounce == NULL) 
            {
              bounce = malloc (DISK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
          disk_read (filesys_disk, sector_idx, bounce);
          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }
      */
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  free(inode_d);
  //free (bounce);
  //printf("@@@@@@@@@@@@ file_read finish @@@@@@@@@@@\n");
  return bytes_read;
}
コード例 #3
0
ファイル: inode_backups.c プロジェクト: VicoandMe/Pintos
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  uint8_t *bounce = NULL;

  while (size > 0) 
    {
      /* Disk sector to read, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;
      
      if (sector_ofs == 0 && chunk_size == BLOCK_SECTOR_SIZE)
        {
          /* Read full sector directly into caller's buffer. */
          block_read (fs_device, sector_idx, buffer + bytes_read);
        }
       else 
        {
          /* Read sector into bounce buffer, then partially copy
             into caller's buffer. */
          if (bounce == NULL) 
            {
              bounce = malloc (BLOCK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
          block_read (fs_device, sector_idx, bounce);
          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }
      
      /* Advance. */
      
	 // struct cache_entry *c = filesys_cache_block_get(sector_idx, false);
     // memcpy (buffer + bytes_read, (uint8_t *) (&c->block + sector_ofs),
	//		  chunk_size);
	  //c->accessed = true;
	  //c->open_cnt--;

	  size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  free (bounce);

  return bytes_read;
}
コード例 #4
0
ファイル: inode.c プロジェクト: jsquaredlau/os11-pintos
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if an error occurs. */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
	if(INODE_DEBUG) printf("INODE: writing inode %u @ offset %i from buffer %x. Size: %i bytes\n", inode->sector, offset, (unsigned) buffer_, size);

	const void *buffer = buffer_;
	off_t bytes_written = 0;

	if (inode->deny_write_cnt)
		return 0;

	/* current inode size */
	off_t length = inode_length (inode);

	/* space left in last sector */
	off_t space_left = BLOCK_SECTOR_SIZE - length % BLOCK_SECTOR_SIZE;
	if(space_left == BLOCK_SECTOR_SIZE)
		space_left = 0;

	/* add block sectors if needed */
	if(offset + size > length)
	{
		/* extend file */
		ASSERT(inode_extend (inode, offset + size - length));
	
		/* update length */
		length += offset + size;
	}

	/* write to file */
	while (size > 0)
	{
		/* Sector to write, starting byte offset within sector. */
		block_sector_t sector_idx = byte_to_sector (inode, offset);
		int sector_ofs = offset % BLOCK_SECTOR_SIZE;
		
		/* Bytes left in inode, bytes left in sector, lesser of the two. */
		off_t inode_left = length - offset;
		int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
		int min_left = inode_left < sector_left ? inode_left : sector_left;

		/* Number of bytes to actually write into this sector. */
		int chunk_size = size < min_left ? size : min_left;
		if (chunk_size <= 0)
			break;

		/* write chunk to cache */
		cache_write(sector_idx, buffer + bytes_written, sector_ofs, chunk_size);
	
		/* Advance. */
		size -= chunk_size;
		offset += chunk_size;
		bytes_written += chunk_size;
    }

	return bytes_written;
}
コード例 #5
0
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  uint8_t *bounce = NULL;

  while (size > 0) 
    {
      /* Disk sector to read, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two.
       * If bytes left in inode (bytes needs to be read for this request) is smaller, 
       * then this is the last sector to read. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. 
       * In cases where size go beyond length of the actual data */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      if (sector_ofs == 0 && chunk_size == BLOCK_SECTOR_SIZE)
        {
          /* Read full sector directly into caller's buffer. */
          block_read (fs_device, sector_idx, buffer + bytes_read);
        }
      else 
        {
          /* Read sector into bounce buffer, then partially copy
             into caller's buffer. */
          if (bounce == NULL) 
            {
              bounce = malloc (BLOCK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
          block_read (fs_device, sector_idx, bounce);
          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }
      
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  free (bounce);

  return bytes_read;
}
コード例 #6
0
ファイル: inode.c プロジェクト: goodahn/cs330_OperatingSystem
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  uint8_t *bounce = NULL;
  int i=0;
  while (size > 0) 
    {
      i++;
      /* Disk sector to read, starting byte offset within sector. */
      disk_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
        {
          /* Read full sector directly into caller's buffer. */
          disk_read (filesys_disk, sector_idx, buffer + bytes_read); 
        }
      else 
        {
          /* Read sector into bounce buffer, then partially copy
             into caller's buffer. */
          if (bounce == NULL) 
            {
              bounce = malloc (DISK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
          disk_read (filesys_disk, sector_idx, bounce);
          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }
      
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  free (bounce);
  return bytes_read;
}
コード例 #7
0
ファイル: inode.c プロジェクト: 120B/OS15x138_140_141
/*! Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
    Returns the number of bytes actually written, which may be
    less than SIZE if an error occurs. */
off_t inode_write_at(struct inode *inode, const void *buffer_, off_t size, off_t offset) {
    const uint8_t *buffer = buffer_;
    off_t bytes_written = 0;
    uint8_t *bounce = NULL;

    if (inode->deny_write_cnt)
        return 0;
    
    // grow if necessary
    if (offset + size > inode->data.length) {
      bool success = grow(&(inode->data), offset + size);
      lock_acquire(filesys_lock_list + inode->sector);
      block_write(fs_device, inode->sector, &(inode->data));
      lock_release(filesys_lock_list + inode->sector);
    }

    while (size > 0) {
        /* Sector to write, starting byte offset within sector. */
        block_sector_t sector_idx = byte_to_sector(inode, offset);
        int sector_ofs = offset % BLOCK_SECTOR_SIZE;

        /* Bytes left in inode, bytes left in sector, lesser of the two. */
        off_t inode_left = inode_length(inode) - offset;
        int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
        int min_left = inode_left < sector_left ? inode_left : sector_left;

        /* Number of bytes to actually write into this sector. */
        int chunk_size = size < min_left ? size : min_left;
        if (chunk_size <= 0)
            break;
        
        if (sector_idx == -1) {
          return bytes_written;
        }
        
        bounce = cache_write(inode, sector_idx);
        if(bounce == NULL) { break; }
        if (sector_ofs == 0 && chunk_size == sector_left)
            memset(bounce, 0, BLOCK_SECTOR_SIZE);
        memcpy(bounce + sector_ofs, buffer + bytes_written, chunk_size);

        /* Advance. */
        size -= chunk_size;
        offset += chunk_size;
        bytes_written += chunk_size;
    }

    return bytes_written;
}
コード例 #8
0
ファイル: inode.c プロジェクト: jsquaredlau/os11-pintos
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
	if(INODE_DEBUG) printf("INODE: reading inode %u @ offset %i into buffer %x. Size: %i bytes\n", inode->sector, offset, (unsigned) buffer_, size);

	void *buffer = buffer_;
	off_t bytes_read = 0;

	while (size > 0)
	{
		/* Disk sector to read, starting byte offset within sector. */
		block_sector_t sector_idx = byte_to_sector (inode, offset);
	
		int sector_ofs = offset % BLOCK_SECTOR_SIZE;

		/* legal sector found */
		if(sector_idx != (block_sector_t) -1)
		{
			/* Bytes left in inode, bytes left in sector, lesser of the two. */
			off_t inode_left = inode_length (inode) - offset;
			int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
			int min_left = inode_left < sector_left ? inode_left : sector_left;

			/* Number of bytes to actually copy out of this sector. */
			int chunk_size = size < min_left ? size : min_left;
			if (chunk_size <= 0)
				break;

			/* read chunk from cache */
			cache_read(sector_idx, buffer + bytes_read, sector_ofs, chunk_size);

			/* Advance. */
			size -= chunk_size;
			offset += chunk_size;
			bytes_read += chunk_size;
		}
		else
		{
			/* EOF reached. */
			if(INODE_DEBUG) printf("INODE: end of file\n");
			break;
		}
	}

	if(INODE_DEBUG) printf("INODE: %i bytes read\n", bytes_read);
  return bytes_read;
}
コード例 #9
0
ファイル: inode.c プロジェクト: bangin20/Pintos-Project
void print_sector_info(struct dir * dir)
{
    block_sector_t bounce[128];
    int i,j;
    block_sector_t sector = byte_to_sector(dir_get_inode(dir),0);
    printf("Sector num : %d\n",sector);
    block_read(fs_device,sector,bounce);
    for(i=0; i<16; i++)
    {
        printf("%d : ",i);
        for(j=0; j<8; j++)
        {
            printf("%#010x  ",bounce[i*8 + j]);
        }
        printf("\n");
    }
}
コード例 #10
0
ファイル: inode.c プロジェクト: linearhw/pintos
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  uint8_t *bounce = NULL;

  if (inode->deny_write_cnt)
    return 0;

  while (size > 0) 
    {
      /* Sector to write, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      struct cache_entry *c = filesys_get_cache(sector_idx);
      if (c)
          filesys_update_cache(c, true);
      else
          c = filesys_new_cache(sector_idx, true);
      bounce = (uint8_t *) &c->block;

      memcpy(bounce + sector_ofs, buffer + bytes_written, chunk_size);
      block_write (fs_device, sector_idx, bounce);
      
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }
  return bytes_written;
}
コード例 #11
0
ファイル: inode.c プロジェクト: tdzhang/cs140
/* padding zeros from start_pos (inclusive) to end_pos (exclusive) */
bool zero_padding(struct inode *inode, struct inode_disk *id,
		off_t start_pos, off_t end_pos) {
	ASSERT(lock_held_by_current_thread (&inode->inode_lock));
	static char zeros[BLOCK_SECTOR_SIZE];
	/* padding the first partial sector */
	if (start_pos % BLOCK_SECTOR_SIZE != 0) {
		block_sector_t eof_sector = byte_to_sector(inode, start_pos-1);
		off_t sector_ofs = start_pos % BLOCK_SECTOR_SIZE;
		size_t zero_bytes = BLOCK_SECTOR_SIZE - sector_ofs;
		cache_write(eof_sector, zeros, sector_ofs, zero_bytes);
	}

	/* padding full sectors until end_pos-1 */
	int extra_sectors = (int)bytes_to_sectors(end_pos)-
			(int)bytes_to_sectors(start_pos);
	off_t* record_sectors=malloc(sizeof(off_t) * extra_sectors);
	off_t i,j;
	block_sector_t new_sector=-1;
	for(i=0;i<extra_sectors;i++){
		if (!free_map_allocate (1, &new_sector)) {
			for(j=0;j<i;j++){
				free_map_release(record_sectors[i],1);
			}
			free(record_sectors);
			return false;
		}
		if(!append_sector_to_inode(id,new_sector)){
			for(j=0;j<i;j++){
				free_map_release(record_sectors[i],1);
			}
			free(record_sectors);
			return false;
		}
		cache_write(new_sector, zeros, 0, BLOCK_SECTOR_SIZE);
		record_sectors[i]=new_sector;
		id->length += BLOCK_SECTOR_SIZE;
	}
	/*update the physical length info*/
	id->length=end_pos;
	cache_write(inode->sector, id, 0, BLOCK_SECTOR_SIZE);
	free(record_sectors);
	return true;

}
コード例 #12
0
ファイル: inode.c プロジェクト: Seondong/pintos
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if an error occurs. */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset)
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  off_t length;

  if (inode->deny_write_cnt)
    return 0;

  /* Extend file. */
  length = inode_length (inode);
  if (offset + size > length)
    inode_extend (inode, offset + size - length);

  while (size > 0)
    {
      /* Sector to write, starting byte offset within sector. */
      disk_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      /* Write sector through buffer cache. */
      cache_write (sector_idx, buffer + bytes_written, sector_ofs, chunk_size);

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }

  return bytes_written;
}
コード例 #13
0
ファイル: inode.c プロジェクト: linearhw/pintos
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  uint8_t *bounce = NULL;
  struct cache_entry *c;
  while (size > 0) 
    {
      /* Disk sector to read, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;
      
      c = filesys_get_cache(sector_idx);
      if (c)
          filesys_update_cache(c, false);
      else
          c = filesys_new_cache(sector_idx, false);
      
      bounce = c->block;
      block_read (fs_device, sector_idx, bounce);
      memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  return bytes_read;
}
コード例 #14
0
ファイル: inode.c プロジェクト: mjy0503/pintos
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  if (inode->deny_write_cnt)
    return 0;
  if(offset + size > inode->data.length){ //growth
    if(!inode_growth(&inode->data, inode->sector, offset + size, INODE_MAX_LEVEL, inode->data.is_dir))
      return 0;
    inode->data.length = offset + size;
  }

  while (size > 0) 
    {
      /* Sector to write, starting byte offset within sector. */
      disk_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;
      
      cache_write (sector_idx, buffer + bytes_written, sector_ofs, chunk_size); 

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }

  return bytes_written;
}
コード例 #15
0
ファイル: inode.c プロジェクト: IVY-bug/Pintos4
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  //uint8_t *bounce = NULL;
  //struct inode_disk *temp = calloc(1, sizeof(struct inode_disk));

  if (inode->deny_write_cnt)
    return 0;
  struct inode_disk *inode_d;
  inode_d = malloc(sizeof(struct inode_disk));
  cache_read(inode->sector, inode_d, DISK_SECTOR_SIZE, 0);

  if(bytes_to_sectors(offset + size) > bytes_to_sectors(inode_d->length)) // 추가할당이 필요한 경우
  {
    if(!allocate_sectors(inode_d, bytes_to_sectors(offset+size), bytes_to_sectors(inode_d->length)))
      return 0;
    inode_d -> length = offset + size; //할당 완료시 length를 pos로 변경 
    cache_write (inode->sector, inode_d, 512, 0); //변경된 inode_d를 기록
  }
      
 // printf("@@@@@@@@@@@@ file_write start inode length : %d size : %d offset : %d  @@@@@@@@@@@@@@\n",inode_d->length,size,offset);
  while (size > 0) 
    {
      cache_read(inode->sector, inode_d, DISK_SECTOR_SIZE, 0);
      
      /* Sector to write, starting byte offset within sector. */
      int sector_idx = byte_to_sector (inode, offset);
      if(sector_idx == -1)
        break;
    //  printf("@@@@@@@@ find_sector : %d   @@@@@@@@@@@@@\n",sector_idx);
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      //off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      //int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < sector_left ? size : sector_left;
      //printf("chunk_Size??????????????????:%d\n",chunk_size);
      if (chunk_size <= 0)
        break;
      //printf("@@@@@@@@@@@@@ write at : %d location : %x size :%d, offset : %d@@@@@@@@@@@@\n",sector_idx, buffer + bytes_written, chunk_size, sector_ofs);
      cache_write(sector_idx, buffer + bytes_written, chunk_size, sector_ofs);
      /*
      cache_read(inode->sector, temp, 512, 0);

      if(offset > temp->length)
      {
        temp->length = offset;
        cache_write(inode->sector, temp, 512, 0);
      }
      */
      /*
      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
        {
          //Write full sector directly to disk. 
          //disk_write (filesys_disk, sector_idx, buffer + bytes_written); 
          cache_write(sector_idx, buffer + bytes_written, 512, 0);
        }
      else 
        {
          
           //We need a bounce buffer. 
          if (bounce == NULL) 
            {
              bounce = malloc (DISK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
           If the sector contains data before or after the chunk
             we're writing, then we need to read in the sector
             first.  Otherwise we start with a sector of all zeros. 
          
          if (sector_ofs > 0 || chunk_size < sector_left) 
            disk_read (filesys_disk, sector_idx, bounce);
          else
            memset (bounce, 0, DISK_SECTOR_SIZE);
          memcpy (bounce + sector_ofs, buffer + bytes_written, chunk_size);
          disk_write (filesys_disk, sector_idx, bounce); 
          
          if(!(sector_ofs > 0 || chunk_size < sector_left))
            cache_write(sector_idx, 0, 512, 0);
          cache_write(sector_idx, buffer + bytes_written, chunk_size, sector_ofs);
        }*/
        
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
      //printf("%d %d\n",inode->sector,offset);
      if(offset > inode_d->length){
        inode_d->length = offset;
        //printf("update!!!!!");
        cache_write(inode->sector, inode_d, DISK_SECTOR_SIZE, 0);
      }
    }
  //free (bounce);
  free(inode_d);   
  //printf("@@@@@@@@@@@@ file_write finish @@@@@@@@@@@\n");
  //printf("%d\n",bytes_written);
  return bytes_written;
}
コード例 #16
0
ファイル: inode.c プロジェクト: bangin20/Pintos-Project
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
	 Returns the number of bytes actually written, which may be
	 less than SIZE if end of file is reached or an error occurs.
	 (Normally a write at end of file would extend the inode, but
	 growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset)
{
    const uint8_t *buffer = buffer_;
    off_t bytes_written = 0;
    uint8_t *bounce = NULL;
    /* Debugging */
    bool debug = false;

    if (inode->deny_write_cnt)
        return 0;

    //printf("inode_length : %d\n",inode_length(inode));
    ASSERT(inode->sector <20000);
    //printf("*node[%d]->extension=%d \n", inode->sector, inode->extension);
    while(inode->extension);
    //printf("inode_write_at called size: %d, offset : %d, inode_length : %d\n",size,offset,inode->data.length);
    while (size > 0)
    {
        /* Sector to write, starting byte offset within sector. */
        block_sector_t sector_idx = byte_to_sector (inode, offset);
        int sector_ofs = offset % BLOCK_SECTOR_SIZE;

        /* Bytes left in inode, bytes left in sector, lesser of the two. */
        off_t inode_left = inode_length (inode) - offset;
        int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
        int min_left = inode_left < sector_left ? inode_left : sector_left;

        /* Number of bytes to actually write into this sector. */
        int chunk_size = size < min_left ? size : min_left;

        struct cache * cache = find_cache(sector_idx);

        //if(debug) printf("sector_idx : %d\n",sector_idx);

        if(offset > inode_length(inode))
        {
            //printf("setting node[%d]->extension=true \n", inode->sector);
            inode->extension = true;
            //printf("You made me true\n");
            int left = (offset - inode_length(inode));
            int i;
            int sectors = (left+1)/BLOCK_SECTOR_SIZE;

            //printf("Offset Over\n");
            block_sector_t new_sector;

            int check = inode_length(inode) + BLOCK_SECTOR_SIZE - inode_length(inode)%512+1;
            inode->data.length += left;
            block_write(fs_device,inode->sector,&(inode->data));

            //printf("offset inode length increase from :%d to %d sector num : %d check : %d\n",inode_length(inode),inode_length(inode)-left,sectors,check);

            for(i=0; i<sectors; i++)
            {
                //printf("check is : %d\n",check);
                if(!free_map_allocate(1,&new_sector))
                {
                    printf("Fail to allocate freemap\n");
                    ASSERT(false);
                }
                updateBlock(inode,check,new_sector);
                //printf("Allocate new Sector : %d\n",new_sector);
                check += BLOCK_SECTOR_SIZE;
            }
            //debug = true;
            continue;
        }
        else if((size >= inode_left - offset) && min_left == 0)
        {
            int sector_num;
            int i;

            //printf("Size Over\n");
            if((size-sector_left+1) <=0)
                sector_num = 0;
            else
                sector_num = bytes_to_sectors((size-sector_left)+1);

            //printf("size : %d , offset : %d, sector_left : %d\n",size,offset,sector_left);
            block_sector_t new_sector;
            int check = inode_length(inode) + sector_left ;
            //printf("setting node[%d]->extension=true \n", inode->sector);
            inode->extension = true;
            //printf("You made me true\n");
            //file_extension(inode,buffer+bytes_written,size);
            inode->data.length = inode->data.length + size;
            block_write(fs_device,inode->sector,&(inode->data));
            //printf("Sector_left : %d\n",sector_left);
            //printf("size inode length increase from :%d to %d now sector num : %d sector_num : %d\n",inode_length(inode),inode_length(inode)-size,sector_idx,sector_num);

            for(i=0; i<sector_num; i++)
            {
                if(!free_map_allocate(1,&new_sector))
                {
                    printf("Freemap Fail\n");
                    ASSERT(false);
                }
                updateBlock(inode,check,new_sector);
                //printf("Allocate new Sector : %d\n",new_sector);
                //	print_table_info(inode);
                check +=BLOCK_SECTOR_SIZE;
            }
            debug = true;
            continue;
        }
        else if(sector_idx == -1)
        {
            print_table_info(inode);
            //printf("offset : %d inode : %#x inode_sector : %d length : %d\n",offset,inode,inode->sector,inode_length(inode));
        }

        if(debug)
        {
        }

        //ASSERT(sector_idx < 20000);
        if (chunk_size <= 0)
            break;
        /*
        			if(debug)
        			{
        				print_table_info(inode);
        			}
        	*/
        //printf("inode read at called chunksize : %d , sector_ofs : %d, sector_idx : %d\n",chunk_size,sector_ofs,sector_idx);
        if(cache == NULL)
        {
            cache = add_cache(sector_idx);

            if(cache == NULL)
            {
                print_table_info(inode);
                printf("read Error\n");
                return bytes_written;
            }
        }
        cache->accessed = true;
        cache->pin = true;
        //if(inode_left - sector_ofs > 512)
        //	read_ahead(sector_idx +1);
        write_cache(cache , buffer+bytes_written , sector_ofs ,chunk_size);
        /* Advance. */
        cache->pin = false;
        size -= chunk_size;
        offset += chunk_size;
        bytes_written += chunk_size;
    } // free (bounce);
    if(inode->extension == true)
    {
        //printf("set node[%d]->extension as false\n", inode->sector);
        inode->extension = false;
        ASSERT(inode->extension==false);
    }
    return bytes_written;
}
コード例 #17
0
ファイル: inode.c プロジェクト: jamesbw/CS140_W11-12
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  struct cached_block *cached_block;
  block_sector_t sector_idx = -1;

  while (size > 0) 
    {
      /* Disk sector to read, starting byte offset within sector. */
      sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode->max_read_length - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;


      cached_block = cache_insert (sector_idx);


      cached_block->active_r_w ++ ;
      lock_release (&cached_block->lock);



      thread_current ()->cache_block_being_accessed = cached_block;
      memcpy (buffer + bytes_read, cached_block->data + sector_ofs, chunk_size);
      cached_block->accessed = true;

      thread_current ()->cache_block_being_accessed = NULL;

      lock_acquire (&cached_block->lock);
      cached_block->active_r_w --;
      if (cached_block->active_r_w == 0)
        cond_broadcast (&cached_block->r_w_done, &cached_block->lock);
      lock_release (&cached_block->lock);


      
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;

    }
    if (((int)sector_idx != -1) && (bytes_to_sectors (inode->max_read_length) > sector_idx))
    {
      block_sector_t next_sector = byte_to_sector (inode, offset + BLOCK_SECTOR_SIZE);
      cache_read_ahead (next_sector);
    }


  return bytes_read;
}
コード例 #18
0
ファイル: inode.c プロジェクト: kcm1700/ChoPintOS
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset)
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;

  if (inode->deny_write_cnt)
    return 0;

  if (offset >= MAX_FILE_LENGTH)
    return 0;

  if (size > MAX_FILE_LENGTH - offset)
    size = MAX_FILE_LENGTH - offset;

  if (size + offset <= MAX_FILE_LENGTH &&
      size + offset > inode->data.length)
  {
    /* extend file */
    off_t length = size + offset;
    size_t sectors = bytes_to_sectors (length);
    for (;inode->data.block_count < sectors; inode->data.block_count++)
    {
      if (!allocate_block (&inode->data, inode->data.block_count))
      {
        /* update to the inode sector */
        cache_write (fs_cache, inode->sector, &inode->data, 0, BLOCK_SECTOR_SIZE);
        return 0;
      }
    }
    inode->data.length = length;

    /* update to the inode sector */
    cache_write (fs_cache, inode->sector, &inode->data, 0, BLOCK_SECTOR_SIZE);
  }

  while (size > 0)
    {
      /* Sector to write, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      cache_write (fs_cache, sector_idx, buffer + bytes_written,
        sector_ofs, chunk_size);

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }

  return bytes_written;
}
コード例 #19
0
ファイル: inode.c プロジェクト: jamesbw/CS140_W11-12
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs. */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  struct cached_block *cached_block;
  block_sector_t sector_idx = -1;
  bool extending = false;

  if (inode->deny_write_cnt)
    return 0;

  //grow inode if necessary
  
  if (offset + size > inode->max_read_length)
  {

    extending = true;
    lock_acquire (&inode->extend_lock);
    if (offset + size > inode->max_read_length)
    {
      int num_blocks_to_add = bytes_to_sectors (offset + size ) - bytes_to_sectors (inode_length (inode));
      if (num_blocks_to_add > 0)
      {
        if (!inode_extend(inode, num_blocks_to_add))
        {
          lock_release (&inode->extend_lock);
          return 0;
        }
      }
      inode->length = offset + size ;
    }
    else
    {
      extending = false;
      lock_release (&inode->extend_lock);
    }
  }

  while (size > 0) 
  {
    /* Sector to write, starting byte offset within sector. */
    sector_idx = byte_to_sector (inode, offset);
    int sector_ofs = offset % BLOCK_SECTOR_SIZE;

    /* Bytes left in inode, bytes left in sector, lesser of the two. */
    off_t inode_left = inode_length (inode) - offset;
    int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
    int min_left = inode_left < sector_left ? inode_left : sector_left;

    /* Number of bytes to actually write into this sector. */
    int chunk_size = size < min_left ? size : min_left;
    if (chunk_size <= 0)
      break;

    cached_block = cache_insert (sector_idx);

    cached_block->active_r_w ++ ;
    lock_release (&cached_block->lock);


    thread_current ()->cache_block_being_accessed = cached_block;
    memcpy (cached_block->data + sector_ofs, buffer + bytes_written, chunk_size);
    cached_block->dirty = true;

    thread_current ()->cache_block_being_accessed = NULL;

    lock_acquire (&cached_block->lock);
    cached_block->active_r_w --;
    if (cached_block->active_r_w == 0)
      cond_broadcast (&cached_block->r_w_done, &cached_block->lock);
    lock_release (&cached_block->lock);


    /* Advance. */
    size -= chunk_size;
    offset += chunk_size;
    bytes_written += chunk_size;

  }

  if (((int)sector_idx != -1) && (bytes_to_sectors (inode->max_read_length) > sector_idx))
  {
    block_sector_t next_sector = byte_to_sector (inode, offset + BLOCK_SECTOR_SIZE);
    cache_read_ahead (next_sector);
  }

  if (extending)
  {
    inode->max_read_length = inode->length;
    uint8_t buf[BLOCK_SECTOR_SIZE];
    memset (buf, 0, BLOCK_SECTOR_SIZE);
    memcpy (buf, inode, sizeof (*inode));
    block_write (fs_device, inode->sector, buf);
    lock_release (&inode->extend_lock);
  }

  return bytes_written;
}
コード例 #20
0
ファイル: inode.c プロジェクト: stylemate/CS330_project1
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset)
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  uint8_t *bounce = NULL;
  if (inode->deny_write_cnt)
    return 0;

  while (size > 0)
    {
      /* Sector to write, starting byte offset within sector. */
      block_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      if (sector_ofs == 0 && chunk_size == BLOCK_SECTOR_SIZE)
        {
          /* Write full sector directly to disk. */
          block_write (fs_device, sector_idx, buffer + bytes_written);
        }
      else
        {
          /* We need a bounce buffer. */
          if (bounce == NULL)
            {
              bounce = malloc (BLOCK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }

          /* If the sector contains data before or after the chunk
             we're writing, then we need to read in the sector
             first.  Otherwise we start with a sector of all zeros. */
          if (sector_ofs > 0 || chunk_size < sector_left)
            block_read (fs_device, sector_idx, bounce);
          else
            memset (bounce, 0, BLOCK_SECTOR_SIZE);
          memcpy (bounce + sector_ofs, buffer + bytes_written, chunk_size);
          block_write (fs_device, sector_idx, bounce);
        }

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }
  free (bounce);

  return bytes_written;
}
コード例 #21
0
ファイル: inode.c プロジェクト: TheZoq2/tddb68-labs
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;
  uint8_t *bounce = NULL;

  lock_acquire(&inode->internal_lock);
  inode->reader_count++;
  //We are the first reader, lock the resource for writers and start reading
  if(inode->reader_count == 1)
  {
    lock_acquire(&inode->reader_writer_lock);
  }
  lock_release(&inode->internal_lock);

  while (size > 0) 
    {
      /* Disk sector to read, starting byte offset within sector. */
      disk_sector_t sector_idx = byte_to_sector (inode, offset);
      int sector_ofs = offset % DISK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
      if (chunk_size <= 0)
        break;

      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
        {
          /* Read full sector directly into caller's buffer. */
          disk_read (filesys_disk, sector_idx, buffer + bytes_read); 
        }
      else 
        {
          /* Read sector into bounce buffer, then partially copy
             into caller's buffer. */
          if (bounce == NULL) 
            {
              bounce = malloc (DISK_SECTOR_SIZE);
              if (bounce == NULL)
                break;
            }
          disk_read (filesys_disk, sector_idx, bounce);
          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }
      
      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }
  free (bounce);

  lock_acquire(&inode->internal_lock);
  inode->reader_count--;
  //If we are the last reader, we need to open back up for writers
  if(inode->reader_count == 0)
  {
    lock_release(&inode->reader_writer_lock);
  }
  lock_release(&inode->internal_lock);
  return bytes_read;
}
コード例 #22
0
ファイル: mkimage.c プロジェクト: deadbok/aMOS
int main(int argc, char *argv[])
{
	FILE			*config_file, *boot_file, *loader_file, *kernel_file, *output_file;
	unsigned long	size, total_size, i;
	unsigned char	padding = 0;
	
	buffer=(unsigned char *)xmalloc(65535);		/* This sets the limit of bootsector + loader
												   to 64K which i should be enough. complains?*/

	print_welcome();

	if (argc>1 && !strcmp(argv[1], "-p"))
		padding=1;
	
	/* Open and parse config file */
	config_file=xopen("boot.cfg", "rt");
	printf("Reading configuration:\n");
	parse_config(config_file);
	
	/* Open boot image and read it into the buffer */
	boot_file=xopen(boot_filename, "rb");
	printf("Reading boot image: %s\n",boot_filename);
	size=file_size(boot_file);
	if (size>512)
	{
		fprintf(stderr, "\nMAIN: boot image must be exactly 512 bytes\n");
		die();
	}
	fread(buffer, size, 1, boot_file);
	if (!((buffer[510]==0x55) && (buffer[511]==0xAA)))
	{
		fprintf(stderr, "\nMAIN: No boot signature (0x55AA) found\n");
		die();
	}
	boot_setup.boot_sectors=byte_to_sector(size);
	total_size=size;
	fclose(boot_file);

	boot_setup.loader_start=byte_to_sector(total_size);

	/* Open loader image and read it into the buffer */
	loader_file=xopen(loader_filename, "rb");
	printf("Reading kernel loader image: %s\n", loader_filename);
	size=file_size(loader_file);
	fread(&buffer[total_size], size, 1, loader_file);
	fclose(loader_file);
	
	total_size+=size;
	boot_setup.loader_sectors=byte_to_sector(size);
	boot_setup.boot_drive=0;
	boot_setup.kernel_start=byte_to_sector(total_size);

	memset(&buffer[total_size], 0, (boot_setup.kernel_start*512)-total_size);
	total_size=boot_setup.kernel_start*512;

	/* Open kernel image and read it into the buffer */
	kernel_file=xopen(kernel_filename, "rb");
	printf("Reading kernel image: %s\n", kernel_filename);
	size=file_size(kernel_file);
	fread(&buffer[total_size], size, 1, loader_file);
	fclose(kernel_file);

	total_size+=size;

	boot_setup.loader_start++;
	boot_setup.kernel_start++;
	boot_setup.kernel_sectors=byte_to_sector(size);

	printf("\nSaving configuration:\n");
	memcpy(&buffer[3], &boot_setup, sizeof(boot_setup));
/*	printf("    Number of heads:                 %d\n", boot_setup.heads); */
/*	printf("    Sectors per track:               %d\n", boot_setup.sectors); */
	printf("    Bytes per sector:                %hu\n", boot_setup.bytes_per_sector);
	printf("    Boot sectors                     %hhu\n", boot_setup.boot_sectors);
	printf("    Kernel loader start at sector:   %lu\n", boot_setup.loader_start);
	printf("    Kernel loader sectors:           %lu\n", boot_setup.loader_sectors);	
	printf("    Kernel start at sector:          %lu\n", boot_setup.kernel_start);
	printf("    Kernel sectors                   %lu\n", boot_setup.kernel_sectors);
	
	printf("Writing %lu bytes to %s\n", total_size, output_filename);
	output_file=xopen(output_filename, "wb");
	fwrite(buffer, 	total_size, 1, output_file);

	if (padding)
	{
		printf("Padding to %lu bytes\n", (output_sectors*512));
		for(i=total_size; i<output_sectors*512; i++)
			fputc(0, output_file);
	}

	fclose(output_file);
	free(buffer);

	printf("That did'nt hurt too much did it?\n");
	
	return 0;
}
コード例 #23
0
ファイル: inode.c プロジェクト: bangin20/Pintos-Project
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
	 Returns the number of bytes actually read, which may be less
	 than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset)
{
    if( inode->sector > 20000)
    {
        printf("inode : %#x node[%#x]->extension=%d \n", inode,inode->sector, inode->extension);
        ASSERT(inode->sector < 20000);
    }
    uint8_t *buffer = buffer_;
    off_t bytes_read = 0;
    uint8_t *bounce = NULL;

    while(inode->extension);
    while (size > 0)
    {
        /* Disk sector to read, starting byte offset within sector. */
        block_sector_t sector_idx = byte_to_sector (inode, offset);
        int sector_ofs = offset % BLOCK_SECTOR_SIZE;

        /* Bytes left in inode, bytes left in sector, lesser of the two. */
        off_t inode_left = inode_length (inode) - offset;
        int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
        int min_left = inode_left < sector_left ? inode_left : sector_left;

        /* Number of bytes to actually copy out of this sector. */
        int chunk_size = size < min_left ? size : min_left;
        if (chunk_size <= 0)
            break;

        struct cache * cache = find_cache(sector_idx);

        if(cache == NULL)
        {
            cache = add_cache(sector_idx);

            if(cache == NULL)
            {
                printf("offset : %d inode : %d inode_sector : %d\n",offset,inode,inode->sector);
                printf("read Error\n");
                ASSERT(false);
                return bytes_read;
            }

        }
        cache->accessed = true;
        cache->pin = true;
        read_cache(cache,buffer+bytes_read,sector_ofs,chunk_size);
        //	if(inode_left > BLOCK_SECTOR_SIZE)
        //	read_ahead(byte_to_sector(inode,offset + sector_left + 1));

        cache->pin = false;
        /*
        		if (sector_ofs == 0 && chunk_size == BLOCK_SECTOR_SIZE)
        			{
        			*/
        /* Read full sector directly into caller's buffer. */
        /*
        		block_read (fs_device, sector_idx, buffer + bytes_read);
        	}
        else
        	{ */
        /* Read sector into bounce buffer, then partially copy
        	 into caller's buffer. */
        /*
        if (bounce == NULL)
        	{
        		bounce = malloc (BLOCK_SECTOR_SIZE);
        		if (bounce == NULL)
        			break;
        	}
        block_read (fs_device, sector_idx, bounce);
        memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
        }*/

        /* Advance. */
        size -= chunk_size;
        offset += chunk_size;
        bytes_read += chunk_size;
    }
    //free (bounce);

    return bytes_read;
}
コード例 #24
0
ファイル: inode.c プロジェクト: samath/140-v2
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
   Returns the number of bytes actually written, which may be
   less than SIZE if end of file is reached or an error occurs.
   (Normally a write at end of file would extend the inode, but
   growth is not yet implemented.) */
off_t
inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                off_t offset) 
{
  const uint8_t *buffer = buffer_;
  off_t bytes_written = 0;
  
  if (inode->deny_write_cnt)
    return 0;

  struct file_synch_status *status = status_for_inode (inode); 
  bool extending = false;
  
  ASSERT (status);
  if (offset + size >= inode->data.length) {
    lock_acquire (&status->lock);
    /* File needs to be extended. */
    status->writers_waiting++;
    /* Wait for all running readers to finish. */
    while (status->readers_running != 0) 
      cond_wait (&status->write_cond, &status->lock);
    status->writers_waiting--;
    
    /* Check if write still requires extension. */
    if (offset + size >= inode->data.length) {
      extending = true;
      
      /* Calculate first unallocated block. */
      unsigned i = (inode->data.length % BLOCK_SECTOR_SIZE == 0) ? 
                        inode->data.length / BLOCK_SECTOR_SIZE : 
                        inode->data.length / BLOCK_SECTOR_SIZE + 1;
      unsigned last_block = (offset + size - 1) / BLOCK_SECTOR_SIZE;
      for(; i <= last_block; i++) {
        if (allocate_block (inode, i) == NO_BLOCK) {
          /* Could not allocate block for extension.
             Limit size to fit within allocated space. */
          size = i * BLOCK_SECTOR_SIZE - offset;
          break;
        }
      }
    } else lock_release (&status->lock);
  }

  
  while (size > 0) 
    {
      /* Sector to write, starting byte offset within sector. */
      block_sector_t next;
      block_sector_t sector_idx = byte_to_sector (inode, offset, &next);
      ASSERT (sector_idx != NO_BLOCK);

      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;

      /* Number of bytes to actually write into this sector. */
      int chunk_size = size < sector_left ? size : sector_left;
      if (chunk_size <= 0)
        break;

      block_write_cache(fs_device, sector_idx, buffer + bytes_written,
                        sector_ofs, chunk_size, false, next);

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_written += chunk_size;
    }
 
  if (extending) {
    /* Update length with offset after while loop terminates. 
       Must be done after write is complete, since length is checked
       to see if read / write pass end of valid file. */
    inode->data.length = offset;
    /* Write disk_inode back to disk. */
    block_write_cache (fs_device, inode->sector, &inode->data,
                       0, BLOCK_SECTOR_SIZE, true, -1);
    /* Wake all sleeping readers. */
    cond_broadcast (&status->read_cond, &status->lock);
    lock_release (&status->lock);
  }

  return bytes_written;
}
コード例 #25
0
ファイル: inode.c プロジェクト: samath/140-v2
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
   Returns the number of bytes actually read, which may be less
   than SIZE if an error occurs or end of file is reached. */
off_t
inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) 
{
  struct file_synch_status *status = status_for_inode (inode);
  ASSERT (status);
  
  
  bool synched = false;
  /* At least inode->data.length bytes are guaranteed to be accurate */
  if (offset + size >= inode->data.length) {
    synched = true;
    lock_acquire (&status->lock);
    /* Yield to writers. */
    int wait_count = 0;
    while (status->writers_waiting != 0 && wait_count < MAX_WRITES) {
      wait_count++;
      cond_wait (&status->read_cond, &status->lock);
    }
    /* Increment reader count. */
    status->readers_running++;
    lock_release (&status->lock);
  }

  uint8_t *buffer = buffer_;
  off_t bytes_read = 0;

  while (size > 0) 
    {
      int sector_ofs = offset % BLOCK_SECTOR_SIZE;

      /* Bytes left in inode, bytes left in sector, lesser of the two. */
      off_t inode_left = inode_length (inode) - offset;
      int sector_left = BLOCK_SECTOR_SIZE - sector_ofs;
      int min_left = inode_left < sector_left ? inode_left : sector_left;

      /* Number of bytes to actually copy out of this sector. */
      int chunk_size = size < min_left ? size : min_left;
   
      if (chunk_size <= 0)
        break;

      /* Disk sector to read, starting byte offset within sector. */
      block_sector_t next;
      block_sector_t sector_idx = byte_to_sector (inode, offset, &next);
      ASSERT (sector_idx != NO_BLOCK);
      
      if (sector_idx != NO_BLOCK) { 
        block_read_cache(fs_device, sector_idx, 
                         buffer + bytes_read, sector_ofs,
                         chunk_size, false, next);
      } else {
        PANIC ("no block found");
      }

      /* Advance. */
      size -= chunk_size;
      offset += chunk_size;
      bytes_read += chunk_size;
    }

  if (synched) {
    lock_acquire (&status->lock);
    status->readers_running--;
    /* Signal a writer to run. */
    cond_signal (&status->write_cond, &status->lock);
    lock_release (&status->lock);
  } 

  return bytes_read;

}