Compare commits
2 Commits
60a2815f3a
...
e39815a2e7
Author | SHA1 | Date | |
---|---|---|---|
e39815a2e7 | |||
ba8f8031f0 |
127
src/fs.c
127
src/fs.c
@ -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 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -855,6 +855,11 @@ 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;
|
||||||
|
|
||||||
@ -922,22 +927,20 @@ 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 target_extension_address;
|
unsigned int next_ext = f.next_extension;
|
||||||
unsigned int previous_extension_address = f.next_extension;
|
unsigned int curr_ext = 0;
|
||||||
unsigned int i;
|
|
||||||
|
struct fs_inode_extension ext;
|
||||||
|
|
||||||
// seek to target extension address
|
// seek to target extension address
|
||||||
for (
|
for (unsigned int i = 0; i < ext_no + 1; i++) {
|
||||||
target_extension_address = previous_extension_address, i = 0;
|
if (!next_ext) {
|
||||||
(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",
|
||||||
@ -946,76 +949,35 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|||||||
}
|
}
|
||||||
mark_used(new_block);
|
mark_used(new_block);
|
||||||
|
|
||||||
// updating previous inode extension
|
ext.next_extension = new_block;
|
||||||
struct fs_inode_extension prev_ext;
|
write_block(curr_ext, (void *) &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
|
|
||||||
|
|
||||||
// writing new inode extension
|
struct fs_inode_extension new_ext;
|
||||||
struct fs_inode_extension ext;
|
memset(&new_ext, 0, sizeof(struct fs_inode_extension));
|
||||||
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
write_block(new_block, (void *) &new_ext);
|
||||||
write_block(target_extension_address, (void *) &ext);
|
|
||||||
|
|
||||||
target_extension_address = new_block;
|
next_ext = new_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fs_inode_extension ext;
|
curr_ext = next_ext;
|
||||||
read_block(target_extension_address, (void *) &ext);
|
read_block(curr_ext, (void *) &ext);
|
||||||
previous_extension_address = target_extension_address;
|
next_ext = ext.next_extension;
|
||||||
target_extension_address = ext.next_extension;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!target_extension_address) {
|
if (!ext.blocks[ext_offset]) {
|
||||||
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();
|
int new_data_block = find_free_block();
|
||||||
if (!new_data_block) {
|
if (!new_data_block) {
|
||||||
pr_err("failed to write to fd=%d block=%d: can't allocate block for data\n",
|
pr_err("failed to write to fd=%d block=%d: can't allocate block for data\n",
|
||||||
fd, block_index);
|
fd, block_index);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mark_used(new_block);
|
|
||||||
mark_used(new_data_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;
|
ext.blocks[ext_offset] = new_data_block;
|
||||||
write_block(target_extension_address, (void *) &ext);
|
write_block(curr_ext, (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(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_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];
|
||||||
|
|
||||||
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);
|
read_fd_block(fd, block_index, data_buffer);
|
||||||
memcpy(&(data_buffer[block_offset]), str, str_len);
|
memcpy(data_buffer, &(str[total_bytes_written]), str_len - total_bytes_written);
|
||||||
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);
|
|
||||||
write_fd_block(fd, block_index, data_buffer);
|
write_fd_block(fd, block_index, data_buffer);
|
||||||
|
|
||||||
read_fd_block(fd, block_index+1, data_buffer);
|
total_bytes_written += (str_len - total_bytes_written);
|
||||||
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 + str_len);
|
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + total_bytes_written);
|
||||||
fs_file_descriptions[fd].rw_offset += str_len;
|
fs_file_descriptions[fd].rw_offset += total_bytes_written;
|
||||||
|
|
||||||
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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user