Compare commits

...

2 Commits

Author SHA1 Message Date
e39815a2e7 fix write_fd_block 2025-04-30 22:46:35 +03:00
ba8f8031f0 [wip] fixing fs_write 2025-04-30 22:33:23 +03:00

127
src/fs.c
View File

@ -107,7 +107,7 @@ static void mark_free(unsigned int block_no)
read_block(0, (void *) &fsh);
if (block_no > fsh.block_count) {
pr_err("block %d is out of fimesystem block range (%d)\n", block_no, fsh.block_count);
pr_err("block %d is out of filesystem block range (%d)\n", block_no, fsh.block_count);
return;
}
@ -855,6 +855,11 @@ static void read_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
else
memset(buf, 0, FS_BLOCK_SIZE);
} else {
/*
* TODO: rewrite this potentially broken abomination normally
* it has not caused issues so far, but you can never be too sure
*/
int ext_no = (block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
int ext_offset = (block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
@ -922,22 +927,20 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
struct fs_inode_extension ext;
memset(&ext, 0, sizeof(struct fs_inode_extension));
write_block(new_block, (void *) &ext);
f.next_extension = new_block;
write_block(fs_file_descriptions[fd].inode, (void *) &f);
}
unsigned int target_extension_address;
unsigned int previous_extension_address = f.next_extension;
unsigned int i;
unsigned int next_ext = f.next_extension;
unsigned int curr_ext = 0;
struct fs_inode_extension ext;
// seek to target extension address
for (
target_extension_address = previous_extension_address, i = 0;
(i < ext_no);
i++
) {
if (!target_extension_address) {
for (unsigned int i = 0; i < ext_no + 1; i++) {
if (!next_ext) {
int new_block = find_free_block();
if (!new_block) {
pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n",
@ -946,76 +949,35 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
}
mark_used(new_block);
// updating previous inode extension
struct fs_inode_extension prev_ext;
read_block(previous_extension_address, (void *) &prev_ext); // read
prev_ext.next_extension = new_block; // modify
write_block(previous_extension_address, (void *) &prev_ext); // write
ext.next_extension = new_block;
write_block(curr_ext, (void *) &ext);
// writing new inode extension
struct fs_inode_extension ext;
memset(&ext, 0, sizeof(struct fs_inode_extension));
write_block(target_extension_address, (void *) &ext);
struct fs_inode_extension new_ext;
memset(&new_ext, 0, sizeof(struct fs_inode_extension));
write_block(new_block, (void *) &new_ext);
target_extension_address = new_block;
next_ext = new_block;
}
struct fs_inode_extension ext;
read_block(target_extension_address, (void *) &ext);
previous_extension_address = target_extension_address;
target_extension_address = ext.next_extension;
curr_ext = next_ext;
read_block(curr_ext, (void *) &ext);
next_ext = ext.next_extension;
}
if (!target_extension_address) {
int new_block = find_free_block();
if (!new_block) {
pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n",
fd, block_index);
return -1;
}
struct fs_inode_extension prev_ext;
read_block(previous_extension_address, (void *) &prev_ext); // read
prev_ext.next_extension = new_block; // modify
write_block(previous_extension_address, (void *) &prev_ext); // write
if (!ext.blocks[ext_offset]) {
int new_data_block = find_free_block();
if (!new_data_block) {
pr_err("failed to write to fd=%d block=%d: can't allocate block for data\n",
fd, block_index);
return -1;
}
mark_used(new_block);
mark_used(new_data_block);
// writing new inode extension
struct fs_inode_extension ext;
memset(&ext, 0, sizeof(struct fs_inode_extension));
ext.blocks[ext_offset] = new_data_block;
write_block(target_extension_address, (void *) &ext);
// writing new data block
write_block(new_data_block, (void *) buf);
} else {
struct fs_inode_extension ext;
read_block(target_extension_address, (void *) &ext);
if (!ext.blocks[ext_offset]) {
int new_data_block = find_free_block();
if (!new_data_block) {
pr_err("failed to write to fd=%d block=%d: can't allocate block for data\n",
fd, block_index);
return -1;
}
mark_used(new_data_block);
ext.blocks[ext_offset] = new_data_block;
write_block(target_extension_address, (void *) &ext);
}
write_block(ext.blocks[ext_offset], (void *) buf);
write_block(curr_ext, (void *) &ext);
}
write_block(ext.blocks[ext_offset], (void *) buf);
}
}
@ -1104,25 +1066,40 @@ int fs_write(void *d)
int block_index = fs_file_descriptions[fd].rw_offset / FS_BLOCK_SIZE;
int block_offset = fs_file_descriptions[fd].rw_offset % FS_BLOCK_SIZE;
unsigned int total_bytes_written = 0;
unsigned char data_buffer[FS_BLOCK_SIZE];
if (block_offset + str_len <= FS_BLOCK_SIZE) {
// first block
read_fd_block(fd, block_index, data_buffer);
{
unsigned int copy_amount = ((FS_BLOCK_SIZE - block_offset) <= str_len)
? FS_BLOCK_SIZE - block_offset
: str_len;
memcpy(&(data_buffer[block_offset]), str, copy_amount);
total_bytes_written += copy_amount;
}
write_fd_block(fd, block_index, data_buffer);
block_index++;
// full middle blocks
for ( ; str_len - total_bytes_written >= FS_BLOCK_SIZE; block_index++) {
write_fd_block(fd, block_index, (unsigned char *) &(str[total_bytes_written]));
total_bytes_written += FS_BLOCK_SIZE;
}
// last partial block
if (str_len - total_bytes_written) {
read_fd_block(fd, block_index, data_buffer);
memcpy(&(data_buffer[block_offset]), str, str_len);
write_fd_block(fd, block_index, data_buffer);
} else {
read_fd_block(fd, block_index, data_buffer);
memcpy(&(data_buffer[block_offset]), str, FS_BLOCK_SIZE - block_offset);
memcpy(data_buffer, &(str[total_bytes_written]), str_len - total_bytes_written);
write_fd_block(fd, block_index, data_buffer);
read_fd_block(fd, block_index+1, data_buffer);
memcpy(data_buffer, &(str[FS_BLOCK_SIZE - block_offset]), str_len - block_offset);
write_fd_block(fd, block_index+1, data_buffer);
total_bytes_written += (str_len - total_bytes_written);
}
pr("Moving fd %d offset: %d -> %d\n",
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + str_len);
fs_file_descriptions[fd].rw_offset += str_len;
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + total_bytes_written);
fs_file_descriptions[fd].rw_offset += total_bytes_written;
struct fs_inode f;
read_block(fs_file_descriptions[fd].inode, (void *) &f);