Compare commits

..

No commits in common. "e39815a2e7b92d6c11c8e85dae2d2029fc6b95d8" and "60a2815f3aced66ad80e8d746b3becca5b00a0cc" have entirely different histories.

117
src/fs.c
View File

@ -107,7 +107,7 @@ static void mark_free(unsigned int block_no)
read_block(0, (void *) &fsh); read_block(0, (void *) &fsh);
if (block_no > fsh.block_count) { if (block_no > fsh.block_count) {
pr_err("block %d is out of filesystem block range (%d)\n", block_no, fsh.block_count); pr_err("block %d is out of fimesystem block range (%d)\n", block_no, fsh.block_count);
return; return;
} }
@ -855,11 +855,6 @@ static void read_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
else else
memset(buf, 0, FS_BLOCK_SIZE); memset(buf, 0, FS_BLOCK_SIZE);
} else { } 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_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; int ext_offset = (block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
@ -927,20 +922,22 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
struct fs_inode_extension ext; struct fs_inode_extension ext;
memset(&ext, 0, sizeof(struct fs_inode_extension)); memset(&ext, 0, sizeof(struct fs_inode_extension));
write_block(new_block, (void *) &ext);
f.next_extension = new_block; f.next_extension = new_block;
write_block(fs_file_descriptions[fd].inode, (void *) &f); write_block(fs_file_descriptions[fd].inode, (void *) &f);
} }
unsigned int next_ext = f.next_extension; unsigned int target_extension_address;
unsigned int curr_ext = 0; unsigned int previous_extension_address = f.next_extension;
unsigned int i;
struct fs_inode_extension ext;
// seek to target extension address // seek to target extension address
for (unsigned int i = 0; i < ext_no + 1; i++) { for (
if (!next_ext) { target_extension_address = previous_extension_address, i = 0;
(i < ext_no);
i++
) {
if (!target_extension_address) {
int new_block = find_free_block(); int new_block = find_free_block();
if (!new_block) { if (!new_block) {
pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n", pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n",
@ -949,21 +946,61 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
} }
mark_used(new_block); mark_used(new_block);
ext.next_extension = new_block; // updating previous inode extension
write_block(curr_ext, (void *) &ext); 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
struct fs_inode_extension new_ext; // writing new inode extension
memset(&new_ext, 0, sizeof(struct fs_inode_extension)); struct fs_inode_extension ext;
write_block(new_block, (void *) &new_ext); memset(&ext, 0, sizeof(struct fs_inode_extension));
write_block(target_extension_address, (void *) &ext);
next_ext = new_block; target_extension_address = new_block;
} }
curr_ext = next_ext; struct fs_inode_extension ext;
read_block(curr_ext, (void *) &ext); read_block(target_extension_address, (void *) &ext);
next_ext = ext.next_extension; previous_extension_address = target_extension_address;
target_extension_address = 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
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]) { if (!ext.blocks[ext_offset]) {
int new_data_block = find_free_block(); int new_data_block = find_free_block();
if (!new_data_block) { if (!new_data_block) {
@ -974,11 +1011,12 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
mark_used(new_data_block); mark_used(new_data_block);
ext.blocks[ext_offset] = new_data_block; ext.blocks[ext_offset] = new_data_block;
write_block(curr_ext, (void *) &ext); write_block(target_extension_address, (void *) &ext);
} }
write_block(ext.blocks[ext_offset], (void *) buf); write_block(ext.blocks[ext_offset], (void *) buf);
} }
}
} }
int fs_read(void *d) int fs_read(void *d)
@ -1066,40 +1104,25 @@ int fs_write(void *d)
int block_index = fs_file_descriptions[fd].rw_offset / FS_BLOCK_SIZE; int block_index = fs_file_descriptions[fd].rw_offset / FS_BLOCK_SIZE;
int block_offset = 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]; unsigned char data_buffer[FS_BLOCK_SIZE];
// first block if (block_offset + str_len <= FS_BLOCK_SIZE) {
read_fd_block(fd, block_index, data_buffer); read_fd_block(fd, block_index, data_buffer);
{ memcpy(&(data_buffer[block_offset]), str, str_len);
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); write_fd_block(fd, block_index, data_buffer);
block_index++; } else {
// 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); read_fd_block(fd, block_index, data_buffer);
memcpy(data_buffer, &(str[total_bytes_written]), str_len - total_bytes_written); memcpy(&(data_buffer[block_offset]), str, FS_BLOCK_SIZE - block_offset);
write_fd_block(fd, block_index, data_buffer); write_fd_block(fd, block_index, data_buffer);
total_bytes_written += (str_len - total_bytes_written); 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);
} }
pr("Moving fd %d offset: %d -> %d\n", pr("Moving fd %d offset: %d -> %d\n",
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + total_bytes_written); fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + str_len);
fs_file_descriptions[fd].rw_offset += total_bytes_written; fs_file_descriptions[fd].rw_offset += str_len;
struct fs_inode f; struct fs_inode f;
read_block(fs_file_descriptions[fd].inode, (void *) &f); read_block(fs_file_descriptions[fd].inode, (void *) &f);