fixed fileio bug

This commit is contained in:
Connor 2023-12-01 20:39:12 -08:00
parent 4dcb74d29e
commit f3a9022897
3 changed files with 587 additions and 439 deletions

View File

@ -16,7 +16,7 @@ public:
~Fs();
ssize_t read(INode_Data *inode_data, char buf[], size_t count, size_t offset);
ssize_t write(INode_Data *inode_data, char buf[], size_t count,
ssize_t write(INode_Data *inode_data, const char buf[], size_t count,
size_t offset);
int truncate(INode_Data *inode_data, size_t length);
ssize_t lseek_next_data(INode_Data *inode_data, size_t offset);

View File

@ -1,10 +1,11 @@
#include "fs.hpp"
const u_int64_t INDIRECT_BLOCKS = IO_BLOCK_SIZE / sizeof(u_int64_t);
class DatablockOperation {
public:
DatablockOperation(int (*_skip)(DatablockOperation *, u_int64_t) = nullptr)
: skip(_skip) {}
char *buf;
size_t count;
size_t offset;
size_t bytes_completed;
@ -32,6 +33,8 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data,
DatablockOperation *op) {
int result;
// printf("test2.1\n");
u_int64_t start_index = start_block_index;
for (size_t i = start_index; i < NUMBER_OF_DIRECT_BLOCKS; ++i) {
if ((result = sweep_datablocks(&(inode_data->direct_blocks[i]), 0, 0,
@ -40,27 +43,34 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data,
start_index = NUMBER_OF_DIRECT_BLOCKS;
}
// printf("test2.2\n");
start_index -= NUMBER_OF_DIRECT_BLOCKS;
if (start_index < IO_BLOCK_SIZE) {
if (start_index < INDIRECT_BLOCKS) {
if ((result = sweep_datablocks(&(inode_data->single_indirect_block), 1,
start_index, allocate, op)) <= 0)
return result;
start_index = IO_BLOCK_SIZE;
start_index = INDIRECT_BLOCKS;
}
start_index -= IO_BLOCK_SIZE;
// printf("test2.3\n");
if (start_index < IO_BLOCK_SIZE * IO_BLOCK_SIZE) {
start_index -= INDIRECT_BLOCKS;
if (start_index < INDIRECT_BLOCKS * INDIRECT_BLOCKS) {
if ((result = sweep_datablocks(&(inode_data->double_indirect_block), 2,
start_index, allocate, op)) <= 0)
return result;
start_index = IO_BLOCK_SIZE * IO_BLOCK_SIZE;
start_index = INDIRECT_BLOCKS * INDIRECT_BLOCKS;
}
start_index -= IO_BLOCK_SIZE * IO_BLOCK_SIZE;
// printf("test2.4\n");
if (start_index < (u_int64_t)IO_BLOCK_SIZE * IO_BLOCK_SIZE * IO_BLOCK_SIZE) {
start_index -= INDIRECT_BLOCKS * INDIRECT_BLOCKS;
if (start_index <
(u_int64_t)INDIRECT_BLOCKS * INDIRECT_BLOCKS * INDIRECT_BLOCKS) {
if ((result = sweep_datablocks(&(inode_data->triple_indirect_block), 3,
start_index, allocate, op)) <= 0)
return result;
@ -78,22 +88,26 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
int err;
int result = -1;
u_int64_t indirect_block_size;
u_int64_t direct_block_size = 1;
u_int64_t num_blocks_indirect;
u_int64_t num_blocks = 1;
for (int i = 0; i < indirect_num; ++i) {
indirect_block_size = direct_block_size;
direct_block_size *= IO_BLOCK_SIZE;
num_blocks_indirect = num_blocks;
num_blocks *= INDIRECT_BLOCKS;
}
// printf("test2.3.1 %d\n", indirect_num);
if ((*block_num) == 0) {
if (allocate) {
if ((err = datablock_manager->new_datablock(block_num)) < 0)
return err;
} else if (op->skip != nullptr) {
return (*(op->skip))(op, direct_block_size);
return (*(op->skip))(op, num_blocks);
}
}
// printf("test2.3.2 %d\n", indirect_num);
if (indirect_num == 0) {
bool delete_block = false;
if ((result = op->operation(*block_num, &delete_block)) < 0)
@ -106,6 +120,8 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
return result;
}
// printf("test2.3.3 %d\n", indirect_num);
if ((*block_num) == 0) {
memset(buf, 0, sizeof(buf));
} else {
@ -113,16 +129,26 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
return err;
}
u_int64_t this_layer_start_index = start_block_index / indirect_block_size;
// printf("test2.3.4 %d\n", indirect_num);
u_int64_t this_layer_start_index = start_block_index / num_blocks_indirect;
u_int64_t next_layer_start_index =
start_block_index - (indirect_block_size * this_layer_start_index);
start_block_index - (num_blocks_indirect * this_layer_start_index);
u_int64_t temp;
u_int64_t next_block_num;
bool modified = false;
// printf("test2.3.4- %d\n", indirect_num);
// printf("start_block_index=%d\n", start_block_index);
// printf("this_layer_start_index=%d\n", this_layer_start_index);
// printf("next_layer_start_index=%d\n", next_layer_start_index);
// printf("num_blocks_indirect=%d\n", num_blocks_indirect);
for (size_t i = this_layer_start_index * sizeof(u_int64_t); i < IO_BLOCK_SIZE;
i += sizeof(u_int64_t)) {
// printf("test2.3.5- %d\n", indirect_num);
read_u64(&temp, &buf[i]);
next_block_num = temp;
if ((result = sweep_datablocks(&next_block_num, indirect_num - 1,
@ -136,6 +162,8 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
break;
}
// printf("test2.3.6 %d\n", indirect_num);
if (modified) {
bool delete_block = true;
for (size_t i = 0; i < IO_BLOCK_SIZE; ++i)
@ -153,11 +181,15 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
}
}
// printf("test2.3.7 %d\n", indirect_num);
// printf("test2.3.8 result=%d %d\n", result, indirect_num);
return result;
}
class ReadDatablockOperation : public DatablockOperation {
public:
char *buf;
ReadDatablockOperation() : DatablockOperation() {}
int operation(u_int64_t block_num, bool *delete_block) override {
char datablock_buf[IO_BLOCK_SIZE];
@ -189,11 +221,14 @@ public:
class WriteDatablockOperation : public DatablockOperation {
public:
const char *buf;
WriteDatablockOperation() : DatablockOperation() {}
int operation(u_int64_t block_num, bool *delete_block) override {
char datablock_buf[IO_BLOCK_SIZE];
int err;
// printf("w: %d\n", bytes_completed);
size_t write_size =
std::min(IO_BLOCK_SIZE - offset, count - bytes_completed);
@ -284,10 +319,12 @@ ssize_t Fs::read(INode_Data *inode_data, char buf[], size_t count,
return op.bytes_completed;
}
ssize_t Fs::write(INode_Data *inode_data, char buf[], size_t count,
ssize_t Fs::write(INode_Data *inode_data, const char buf[], size_t count,
size_t offset) {
int err;
// printf("test1\n");
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
@ -298,10 +335,14 @@ ssize_t Fs::write(INode_Data *inode_data, char buf[], size_t count,
op.bytes_completed = 0;
op.fs = this;
// printf("test2\n");
if ((err = sweep_inode_datablocks(inode_data, start_block_index, true,
&op)) != 0)
return err;
// printf("test3\n");
inode_data->metadata.size =
std::max(offset + op.bytes_completed, inode_data->metadata.size);

View File

@ -1,6 +1,12 @@
#define _GNU_SOURCE
#include "fischl.h"
#include "fs.hpp"
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
int main() {
// printf("hello word!");
@ -36,7 +42,7 @@ int main() {
// return 0;
int err;
// int err;
// RawDisk *disk = new FakeRawDisk(2048);
// Fs *fs = new Fs(disk);
@ -50,7 +56,7 @@ int main() {
// disk->print_block(0);
// disk->print_block(1);
int BL_SIZE = 4096 / 8;
// int BL_SIZE = 4096 / 8;
// u_int64_t buf[BL_SIZE * (56 + 512 + 10)];
@ -84,58 +90,39 @@ int main() {
// printf("%d ", buf2[i]);
// printf("\n");
u_int64_t big_buf[BL_SIZE * 1000];
char *buf = (char *)big_buf;
// u_int64_t big_buf[BL_SIZE * 1000];
// char *buf = (char *)big_buf;
int offs = 55 * 4096;
// int offs = 55 * 4096;
RawDisk *disk = new FakeRawDisk(2048);
Fs *fs = new Fs(disk);
// RawDisk *disk = new FakeRawDisk(2048);
// Fs *fs = new Fs(disk);
fs->format();
disk->print_block(0);
disk->print_block(1);
// fs->format();
// disk->print_block(0);
// disk->print_block(1);
INode_Data inode_data;
fs->inode_manager->new_inode(1, 2, 3, &inode_data);
// INode_Data inode_data;
// fs->inode_manager->new_inode(1, 2, 3, &inode_data);
disk->print_block(0);
disk->print_block(1);
disk->print_block(1024);
// disk->print_block(0);
// disk->print_block(1);
// disk->print_block(1024);
for (int i = 0; i < BL_SIZE * 3; ++i)
big_buf[i] = 1;
// for (int i = 0; i < BL_SIZE * 3; ++i)
// big_buf[i] = 1;
err = fs->write(&inode_data, buf, 4096 * 3, offs);
// err = fs->write(&inode_data, buf, 4096 * 3, offs);
for (int i = 0; i < BL_SIZE * 3; ++i)
big_buf[i] = 2;
// for (int i = 0; i < BL_SIZE * 3; ++i)
// big_buf[i] = 2;
err = fs->truncate(&inode_data, offs + 4096);
err = fs->write(&inode_data, buf, 4096 * 2, offs + 4096 * 2);
err = fs->truncate(&inode_data, offs + 4096 * 2);
// err = fs->truncate(&inode_data, offs + 4096);
// err = fs->write(&inode_data, buf, 4096 * 2, offs + 4096 * 2);
// err = fs->truncate(&inode_data, offs + 4096 * 2);
fs->inode_manager->save_inode(&inode_data);
printf("Write %d", err);
disk->print_block(0);
disk->print_block(1);
disk->print_block(1024);
disk->print_block(1025);
disk->print_block(1026);
disk->print_block(1027);
disk->print_block(1028);
disk->print_block(1029);
// disk->print_block(1080);
// disk->print_block(1081);
// disk->print_block(1082);
// disk->print_block(1083);
// disk->print_block(1084);
// disk->print_block(1085);
// err = fs->truncate(&inode_data, 4096 + 4);
// fs->inode_manager->save_inode(&inode_data);
// printf("Truncate %d", err);
// printf("Write %d", err);
// disk->print_block(0);
// disk->print_block(1);
@ -144,25 +131,145 @@ int main() {
// disk->print_block(1026);
// disk->print_block(1027);
// disk->print_block(1028);
// disk->print_block(1029);
// // disk->print_block(1080);
// // disk->print_block(1081);
// // disk->print_block(1082);
// // disk->print_block(1083);
// // disk->print_block(1084);
// // disk->print_block(1085);
err = fs->lseek_next_hole(&inode_data, offs + 0);
printf("lseek_next_hole (%d): %d\n\n", offs + 0, err);
err = fs->lseek_next_hole(&inode_data, offs + 1);
printf("lseek_next_hole (%d): %d\n\n", offs + 1, err);
err = fs->lseek_next_hole(&inode_data, offs + 4096);
printf("lseek_next_hole (%d): %d\n\n", offs + 4096, err);
err = fs->lseek_next_hole(&inode_data, offs + 4097);
printf("lseek_next_hole (%d): %d\n\n", offs + 4097, err);
err = fs->lseek_next_hole(&inode_data, offs + 8192);
printf("lseek_next_hole (%d): %d\n\n", offs + 8192, err);
err = fs->lseek_next_hole(&inode_data, offs + 8193);
printf("lseek_next_hole (%d): %d\n\n", offs + 8193, err);
err = fs->lseek_next_hole(&inode_data, offs + 12288);
printf("lseek_next_hole (%d): %d\n\n", offs + 12288, err);
err = fs->lseek_next_hole(&inode_data, offs + 12289);
printf("lseek_next_hole (%d): %d\n\n", offs + 12289, err);
err = fs->lseek_next_hole(&inode_data, offs + 100000);
printf("lseek_next_hole (%d): %d\n\n", offs + 100000, err);
// // err = fs->truncate(&inode_data, 4096 + 4);
// // fs->inode_manager->save_inode(&inode_data);
// // printf("Truncate %d", err);
// // disk->print_block(0);
// // disk->print_block(1);
// // disk->print_block(1024);
// // disk->print_block(1025);
// // disk->print_block(1026);
// // disk->print_block(1027);
// // disk->print_block(1028);
// err = fs->lseek_next_hole(&inode_data, offs + 0);
// printf("lseek_next_hole (%d): %d\n\n", offs + 0, err);
// err = fs->lseek_next_hole(&inode_data, offs + 1);
// printf("lseek_next_hole (%d): %d\n\n", offs + 1, err);
// err = fs->lseek_next_hole(&inode_data, offs + 4096);
// printf("lseek_next_hole (%d): %d\n\n", offs + 4096, err);
// err = fs->lseek_next_hole(&inode_data, offs + 4097);
// printf("lseek_next_hole (%d): %d\n\n", offs + 4097, err);
// err = fs->lseek_next_hole(&inode_data, offs + 8192);
// printf("lseek_next_hole (%d): %d\n\n", offs + 8192, err);
// err = fs->lseek_next_hole(&inode_data, offs + 8193);
// printf("lseek_next_hole (%d): %d\n\n", offs + 8193, err);
// err = fs->lseek_next_hole(&inode_data, offs + 12288);
// printf("lseek_next_hole (%d): %d\n\n", offs + 12288, err);
// err = fs->lseek_next_hole(&inode_data, offs + 12289);
// printf("lseek_next_hole (%d): %d\n\n", offs + 12289, err);
// err = fs->lseek_next_hole(&inode_data, offs + 100000);
// printf("lseek_next_hole (%d): %d\n\n", offs + 100000, err);
// RawDisk *disk = new FakeRawDisk(2048);
// Fs *fs = new Fs(disk);
// fs->format();
// INode_Data inode_data;
// fs->inode_manager->new_inode(1, 2, 3, &inode_data);
// char cwd_buf[PATH_MAX];
// int fd;
// assert(getcwd(cwd_buf, sizeof(cwd_buf)) != NULL);
// printf("\n\n(");
// printf(cwd_buf);
// printf(")\n\n");
// fd = open("/home/connor", O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
// assert(fd != -1);
// u_int64_t test_start_range = IO_BLOCK_SIZE * 100;
// u_int64_t test_write_range = IO_BLOCK_SIZE * 50;
// u_int64_t test_read_range = IO_BLOCK_SIZE * 50;
// char zeros[test_write_range] = {0};
// char ones[test_write_range];
// memset(ones, 1, test_write_range);
// char *write_buf = ones;
// char reference_read_buf[test_read_range];
// char test_read_buf[test_read_range];
// size_t offset, count;
// int test_res, ref_res;
// for (int i = 0; i < 1000; ++i) {
// offset = rand() & test_start_range;
// switch (rand() % 2) {
// case 0:
// count = rand() & test_write_range;
// write_buf = (write_buf == ones) ? zeros : ones;
// printf("write: %ds count=%d offset=%d\n", write_buf[0], count, offset);
// test_res = fs->write(&inode_data, write_buf, count, offset);
// assert(lseek(fd, offset, SEEK_SET) == offset);
// ref_res = write(fd, write_buf, count);
// break;
// case 1:
// count = rand() & test_read_range;
// printf("read: count=%d offset=%d\n", count, offset);
// test_res = fs->read(&inode_data, test_read_buf, count, offset);
// assert(lseek(fd, offset, SEEK_SET) == offset);
// ref_res = read(fd, reference_read_buf, count);
// bool reads_are_equal = true;
// for (size_t j = 0; j < count; ++j)
// if (test_read_buf[i] != reference_read_buf[i]) {
// reads_are_equal = false;
// break;
// }
// assert(reads_are_equal);
// break;
// }
// assert(test_res == ref_res);
// }
RawDisk *disk = new FakeRawDisk(5120);
Fs *fs = new Fs(disk);
fs->format();
int buf_size = IO_BLOCK_SIZE * 200;
int loops = 14 * 1024 * 1024 / buf_size;
char buf[buf_size];
memset(buf, 1, sizeof(buf));
INode_Data inode_data;
fs->inode_manager->new_inode(1, 2, 3, &inode_data);
int res;
for (int j = 0; j < loops; ++j) {
res = fs->write(&inode_data, buf, sizeof(buf), sizeof(buf) * j);
printf("write: %d j=%d\n", res, j);
}
for (int j = 0; j < loops; ++j) {
memset(buf, 0, sizeof(buf));
res = fs->read(&inode_data, buf, sizeof(buf), sizeof(buf) * j);
printf("read: %d j=%d\n", res, j);
for (int i = 0; i < sizeof(buf); ++i)
if (buf[1] != 1) {
printf("error: %d\n", i);
return -1;
}
}
return 0;
}