aboutsummaryrefslogtreecommitdiff
path: root/kernel/fs/s5fs/s5fs_subr.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2024-04-25 16:44:02 +0000
committersotech117 <michael_foiani@brown.edu>2024-04-25 16:44:02 +0000
commit30e8db594acc751d39d70bedfb83408b90676835 (patch)
tree806bb83d98d04d56084f8ff29178e55e6fc96ac0 /kernel/fs/s5fs/s5fs_subr.c
parent4f1fb377b55f04399dd9520ca8ff6122a0703887 (diff)
cleanup s5 and try some fixes for namev_dir
Diffstat (limited to 'kernel/fs/s5fs/s5fs_subr.c')
-rw-r--r--kernel/fs/s5fs/s5fs_subr.c73
1 files changed, 36 insertions, 37 deletions
diff --git a/kernel/fs/s5fs/s5fs_subr.c b/kernel/fs/s5fs/s5fs_subr.c
index 0d12030..a9fa638 100644
--- a/kernel/fs/s5fs/s5fs_subr.c
+++ b/kernel/fs/s5fs/s5fs_subr.c
@@ -160,25 +160,25 @@ long s5_file_block_to_disk_block(s5_node_t *sn, size_t file_blocknum,
}
// Else, allocate the block
- disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
+ long alloced_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
// Propogate errors from s5_alloc_block
- if (disk_blocknum < 0)
+ if (alloced_blocknum < 0)
{
- return disk_blocknum;
+ return alloced_blocknum;
}
// Update the inode
- sn->inode.s5_direct_blocks[file_blocknum] = disk_blocknum;
+ sn->inode.s5_direct_blocks[file_blocknum] = alloced_blocknum;
sn->dirtied_inode = 1;
// set ret params and return
*newp = 1;
- return disk_blocknum;
+ return alloced_blocknum;
}
// Case 2: Indirect block is not allocated but alloc is set
else if (!is_indirect_block_allocated && alloc)
{
- size_t disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
+ long disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
// Propogate errors from s5_alloc_block
if (disk_blocknum < 0)
{
@@ -228,24 +228,25 @@ long s5_file_block_to_disk_block(s5_node_t *sn, size_t file_blocknum,
}
// Else, allocate the block
- disk_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
+ long alloced_blocknum = s5_alloc_block(VNODE_TO_S5FS(&sn->vnode));
// Propogate errors from s5_alloc_block
- if (disk_blocknum < 0)
+ if (alloced_blocknum < 0)
{
- return disk_blocknum;
+ return alloced_blocknum;
}
// Update the inode
- indirect_block[indirect_block_index] = disk_blocknum;
+ indirect_block[indirect_block_index] = alloced_blocknum;
sn->dirtied_inode = 1;
// set ret params and return
*newp = 1;
- return disk_blocknum;
+ return alloced_blocknum;
}
// Case 4: The indirect block has not been allocated and alloc is clear
else
{
+ // TODO: check if this is correct
return 0;
}
}
@@ -289,31 +290,30 @@ ssize_t s5_read_file(s5_node_t *sn, size_t pos, char *buf, size_t len)
}
// Calculate the number of bytes to read
- size_t bytes_to_read = min(len, sn->inode.s5_un.s5_size - pos);
+ size_t tmp = sn->inode.s5_un.s5_size - pos;
+ size_t bytes_to_read = len < tmp ? len : tmp;
// Initialize the number of bytes read
size_t bytes_read = 0;
while (bytes_read < bytes_to_read)
{
- // Get the block number of the desired file block
- int new_block;
size_t file_blocknum = pos / S5_BLOCK_SIZE;
size_t block_offset = pos % S5_BLOCK_SIZE;
- size_t disk_blocknum = s5_file_block_to_disk_block(sn, file_blocknum, 0, &new_block);
-
- // Propogate errors from s5_file_block_to_disk_block
- if (disk_blocknum < 0)
- {
- return disk_blocknum;
- }
// Get the pframe containing the block data
pframe_t *pf;
- s5_get_file_block(sn, file_blocknum, 0, &pf);
+ long err = s5_get_file_block(sn, file_blocknum, 0, &pf);
+ // Propogate errors from s5_get_file_block
+ if (err < 0)
+ {
+ return err;
+ }
// Calculate the number of bytes to read in the current iteration
- size_t bytes_read_in_iteration = min(bytes_to_read - bytes_read, S5_BLOCK_SIZE - block_offset);
+ size_t tmp1 = bytes_to_read - bytes_read;
+ size_t tmp2 = S5_BLOCK_SIZE - block_offset;
+ size_t bytes_read_in_iteration = tmp1 < tmp2 ? tmp1 : tmp2;
// Copy the data from the block to the buffer
memcpy(buf + bytes_read, pf->pf_addr + block_offset, bytes_read_in_iteration);
@@ -372,31 +372,29 @@ ssize_t s5_write_file(s5_node_t *sn, size_t pos, const char *buf, size_t len)
}
// Calculate the number of bytes to write
- size_t bytes_to_write = min(len, S5_MAX_FILE_SIZE - pos);
+ size_t bytes_to_write = len < S5_MAX_FILE_SIZE - pos ? len : S5_MAX_FILE_SIZE - pos;
// Initialize the number of bytes written
size_t bytes_written = 0;
while (bytes_written < bytes_to_write)
{
- // Get the block number of the desired file block
- int new_block;
size_t file_blocknum = pos / S5_BLOCK_SIZE;
size_t block_offset = pos % S5_BLOCK_SIZE;
- size_t disk_blocknum = s5_file_block_to_disk_block(sn, file_blocknum, 1, &new_block);
-
- // Propogate errors from s5_file_block_to_disk_block
- if (disk_blocknum < 0)
- {
- return disk_blocknum;
- }
// Get the pframe containing the block data
pframe_t *pf;
- s5_get_file_block(sn, file_blocknum, 1, &pf);
+ long err = s5_get_file_block(sn, file_blocknum, 1, &pf);
+ // Propogate errors from s5_get_file_block
+ if (err < 0)
+ {
+ return err;
+ }
// Calculate the number of bytes to write in the current iteration
- size_t bytes_written_in_iteration = min(bytes_to_write - bytes_written, S5_BLOCK_SIZE - block_offset);
+ size_t tmp1 = bytes_to_write - bytes_written;
+ size_t tmp2 = S5_BLOCK_SIZE - block_offset;
+ size_t bytes_written_in_iteration = tmp1 < tmp2 ? tmp1 : tmp2;
// Copy the data from the buffer to the block
memcpy(pf->pf_addr + block_offset, buf + bytes_written, bytes_written_in_iteration);
@@ -410,8 +408,9 @@ ssize_t s5_write_file(s5_node_t *sn, size_t pos, const char *buf, size_t len)
// Update the position
pos += bytes_written_in_iteration;
- // Update the inode's s5_size and mark the inode dirty
- sn->inode.s5_un.s5_size = max(sn->inode.s5_un.s5_size, pos);
+ // Update the inode
+ sn->inode.s5_un.s5_size = sn->inode.s5_un.s5_size > pos ? sn->inode.s5_un.s5_size : pos;
+ sn->vnode.vn_len = sn->inode.s5_un.s5_size;
sn->dirtied_inode = 1;
}