Skip to content

Commit

Permalink
Implement simplefs_file_ops:open (#56)
Browse files Browse the repository at this point in the history
In the registration of simplefs_file_ops callbacks, there is no open
operation, which may lead to creating unexpected space. This commit
adds an open operation to handle file truncation when opening files
with O_WRONLY, O_RDWR, and O_TRUNC flags. This prevents unintended
space allocation by ensuring proper truncation of files.

The implementation checks the flags associated with the file opening
mode and performs truncation if the file is being opened for write or
read/write and the O_TRUNC flag is set. Truncation is achieved by
reading the file's index block from disk, iterating over the data block
pointers, releasing the associated data blocks, and updating the
inode metadata (size and block count).

Close #55
  • Loading branch information
HotMercury authored Jun 17, 2024
1 parent 2ee1fc1 commit 79a2831
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,54 @@ static int simplefs_write_end(struct file *file,
return ret;
}

/*
* Called when a file is opened in the simplefs.
* It checks the flags associated with the file opening mode (O_WRONLY, O_RDWR,
* O_TRUNC) and performs truncation if the file is being opened for write or
* read/write and the O_TRUNC flag is set.
*
* Truncation is achieved by reading the file's index block from disk, iterating
* over the data block pointers, releasing the associated data blocks, and
* updating the inode metadata (size and block count).
*/
static int simplefs_file_open(struct inode *inode, struct file *filp)
{
bool wronly = (filp->f_flags & O_WRONLY);
bool rdwr = (filp->f_flags & O_RDWR);
bool trunc = (filp->f_flags & O_TRUNC);

if ((wronly || rdwr) && trunc && inode->i_size) {
struct buffer_head *bh_index;
struct simplefs_file_ei_block *ei_block;
sector_t iblock;

/* Fetch the file's extent block from disk */
bh_index = sb_bread(inode->i_sb, SIMPLEFS_INODE(inode)->ei_block);
if (!bh_index)
return -EIO;

ei_block = (struct simplefs_file_ei_block *) bh_index->b_data;

for (iblock = 0; iblock <= SIMPLEFS_MAX_EXTENTS &&
ei_block->extents[iblock].ee_start;
iblock++) {
put_blocks(SIMPLEFS_SB(inode->i_sb),
ei_block->extents[iblock].ee_start,
ei_block->extents[iblock].ee_len);
memset(&ei_block->extents[iblock], 0,
sizeof(struct simplefs_extent));
}
/* Update inode metadata */
inode->i_size = 0;
inode->i_blocks = 1;

mark_buffer_dirty(bh_index);
brelse(bh_index);
mark_inode_dirty(inode);
}
return 0;
}

const struct address_space_operations simplefs_aops = {
#if SIMPLEFS_AT_LEAST(5, 19, 0)
.readahead = simplefs_readahead,
Expand All @@ -262,4 +310,5 @@ const struct file_operations simplefs_file_ops = {
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter,
.fsync = generic_file_fsync,
.open = simplefs_file_open,
};

0 comments on commit 79a2831

Please sign in to comment.