fix write_fd_block
This commit is contained in:
parent
ba8f8031f0
commit
e39815a2e7
87
src/fs.c
87
src/fs.c
|
@ -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;
|
||||
|
||||
|
@ -928,17 +933,14 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|||
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",
|
||||
|
@ -947,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue