file io bug fixes
This commit is contained in:
parent
d8f8594c13
commit
81f81d8e80
@ -16,6 +16,7 @@ add_executable(fischl
|
||||
lib/fs/datablock_manager.cpp
|
||||
lib/fs/fs_data_types.cpp
|
||||
lib/fs/fs_resize.cpp
|
||||
lib/fs/fs_read_write.cpp
|
||||
lib/fs/fs.cpp
|
||||
lib/fs/inode_manager.cpp
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef FS_CONSTANTS_HPP
|
||||
#define FS_CONSTANTS_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <linux/fs.h>
|
||||
@ -10,7 +11,6 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#define IO_BLOCK_SIZE 4096
|
||||
|
||||
#define NUM_INODE_BLOCKS 1023
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "fs.hpp"
|
||||
#include <cmath>
|
||||
|
||||
class DatablockOperation {
|
||||
public:
|
||||
@ -44,7 +43,7 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data,
|
||||
|
||||
start_index -= IO_BLOCK_SIZE * IO_BLOCK_SIZE;
|
||||
|
||||
if (start_index < IO_BLOCK_SIZE * IO_BLOCK_SIZE * IO_BLOCK_SIZE) {
|
||||
if (start_index < (u_int64_t)IO_BLOCK_SIZE * IO_BLOCK_SIZE * IO_BLOCK_SIZE) {
|
||||
if ((result = sweep_datablocks(&(inode_data->triple_indirect_block), 3,
|
||||
start_index, allocate, op)) <= 0)
|
||||
return result;
|
||||
@ -61,9 +60,6 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
|
||||
char buf[IO_BLOCK_SIZE];
|
||||
int err;
|
||||
int result = -1;
|
||||
u_int64_t temp;
|
||||
u_int64_t next_block_num;
|
||||
bool modified = false;
|
||||
|
||||
if (allocate && (*block_num) == 0)
|
||||
if ((err = datablock_manager->new_datablock(block_num)) < 0)
|
||||
@ -92,14 +88,14 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
|
||||
bool modified = false;
|
||||
|
||||
for (size_t i = this_layer_start_index * sizeof(u_int64_t); i < IO_BLOCK_SIZE;
|
||||
i += *sizeof(u_int64_t)) {
|
||||
i += sizeof(u_int64_t)) {
|
||||
read_u64(&temp, &buf[i]);
|
||||
next_block_num = temp;
|
||||
if ((result = sweep_datablocks(&next_block_num, indirect_num - 1,
|
||||
next_layer_start_index, allocate, func)) < 0)
|
||||
next_layer_start_index, allocate, op)) < 0)
|
||||
return result;
|
||||
if (next_block_num != temp) {
|
||||
write_u64(&next_block_num, &buf[i]);
|
||||
write_u64(next_block_num, &buf[i]);
|
||||
modified = true;
|
||||
}
|
||||
if (result == 0)
|
||||
@ -119,7 +115,11 @@ public:
|
||||
char datablock_buf[IO_BLOCK_SIZE];
|
||||
int err;
|
||||
|
||||
size_t read_size = min(IO_BLOCK_SIZE - offset, count);
|
||||
// printf("PRINT: (%d) %d %d %d\n", block_num, count, offset,
|
||||
// bytes_completed);
|
||||
|
||||
size_t read_size =
|
||||
std::min(IO_BLOCK_SIZE - offset, count - bytes_completed);
|
||||
|
||||
if (block_num != 0) {
|
||||
if ((err = disk->read_block(block_num, datablock_buf)) < 0)
|
||||
@ -145,13 +145,13 @@ public:
|
||||
char datablock_buf[IO_BLOCK_SIZE];
|
||||
int err;
|
||||
|
||||
size_t write_size = min(IO_BLOCK_SIZE - offset, count);
|
||||
size_t write_size =
|
||||
std::min(IO_BLOCK_SIZE - offset, count - bytes_completed);
|
||||
|
||||
if (write_size < IO_BLOCK_SIZE)
|
||||
if ((err = disk->read_block(block_num, datablock_buf)) < 0)
|
||||
return err;
|
||||
|
||||
size_t write_size = min(IO_BLOCK_SIZE - offset, count);
|
||||
memcpy(&datablock_buf[offset], &buf[bytes_completed], write_size);
|
||||
|
||||
if ((err = disk->write_block(block_num, datablock_buf)) < 0)
|
||||
@ -171,17 +171,17 @@ ssize_t Fs::read(INode_Data *inode_data, char buf[], size_t count,
|
||||
int err;
|
||||
|
||||
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = offset - block_start;
|
||||
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
|
||||
|
||||
ReadDatablockOperation op = ReadDatablockOperation();
|
||||
op.offset = internal_offset;
|
||||
op.buf = buf;
|
||||
op.count = min(count, inode_data->metadata.size - offset);
|
||||
op.count = std::min(count, inode_data->metadata.size - offset);
|
||||
op.bytes_completed = 0;
|
||||
op.disk = disk;
|
||||
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, false, op)) <
|
||||
0)
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, false,
|
||||
&op)) < 0)
|
||||
return err;
|
||||
|
||||
return op.bytes_completed;
|
||||
@ -192,21 +192,21 @@ ssize_t Fs::write(INode_Data *inode_data, char buf[], size_t count,
|
||||
int err;
|
||||
|
||||
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = offset - block_start;
|
||||
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
|
||||
|
||||
ReadDatablockOperation op = WriteDatablockOperation();
|
||||
WriteDatablockOperation op = WriteDatablockOperation();
|
||||
op.offset = internal_offset;
|
||||
op.buf = buf;
|
||||
op.count = count;
|
||||
op.bytes_completed = 0;
|
||||
op.disk = disk;
|
||||
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, true, op)) <
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, true, &op)) <
|
||||
0)
|
||||
return err;
|
||||
|
||||
inode_data->metadata.size =
|
||||
max(offset + op.bytes_completed, inode_data->metadata.size);
|
||||
std::max(offset + op.bytes_completed, inode_data->metadata.size);
|
||||
|
||||
return op.bytes_completed;
|
||||
}
|
73
lib/main.cpp
73
lib/main.cpp
@ -7,31 +7,80 @@ int main() {
|
||||
// fischl *F = new fischl;
|
||||
// F->init();
|
||||
// char *d = strdup("/dev/vdc");
|
||||
|
||||
// RawDisk *disk = new FakeRawDisk(2048);
|
||||
// Fs *fs = new Fs(disk);
|
||||
// fs->format();
|
||||
// disk->print_block(0);
|
||||
// disk->print_block(1);
|
||||
// INode_Data inode_data = INode_Data();
|
||||
// fs->inode_manager->new_inode(1, 2, 3, &inode_data);
|
||||
// int err;
|
||||
// u_int64_t block_num = 0;
|
||||
// for (int i = 0; i < 56 + 512 + 4; ++i)
|
||||
// err = fs->allocate_datablock(&inode_data, &block_num);
|
||||
|
||||
// for (int i = 0; i < 5; ++i)
|
||||
// printf("%d\n", err = fs->deallocate_datablock(&inode_data, &block_num));
|
||||
|
||||
// fs->inode_manager->save_inode(&inode_data);
|
||||
|
||||
// disk->print_block(0);
|
||||
// disk->print_block(1);
|
||||
|
||||
// disk->print_block(1081);
|
||||
|
||||
// disk->print_block(1596);
|
||||
|
||||
// disk->print_block(1597);
|
||||
|
||||
int err;
|
||||
|
||||
RawDisk *disk = new FakeRawDisk(2048);
|
||||
Fs *fs = new Fs(disk);
|
||||
fs->format();
|
||||
disk->print_block(0);
|
||||
disk->print_block(1);
|
||||
INode_Data inode_data = INode_Data();
|
||||
|
||||
INode_Data inode_data;
|
||||
fs->inode_manager->new_inode(1, 2, 3, &inode_data);
|
||||
int err;
|
||||
u_int64_t block_num = 0;
|
||||
for (int i = 0; i < 56 + 512 + 4; ++i)
|
||||
err = fs->allocate_datablock(&inode_data, &block_num);
|
||||
|
||||
for (int i = 0; i < 5; ++i)
|
||||
printf("%d\n", err = fs->deallocate_datablock(&inode_data, &block_num));
|
||||
|
||||
fs->inode_manager->save_inode(&inode_data);
|
||||
|
||||
disk->print_block(0);
|
||||
disk->print_block(1);
|
||||
|
||||
int BL_SIZE = 4096 / 8;
|
||||
|
||||
u_int64_t buf[BL_SIZE * (56 + 512 + 10)];
|
||||
|
||||
for (int i = 0; i < BL_SIZE * (56 + 512 + 10); ++i)
|
||||
buf[i] = (i / BL_SIZE) + 1;
|
||||
|
||||
err = fs->write(&inode_data, (char *)buf, 4096 * (56 + 3) + 16 + 8, 0);
|
||||
fs->inode_manager->save_inode(&inode_data);
|
||||
|
||||
printf("Write %d", err);
|
||||
|
||||
disk->print_block(0);
|
||||
disk->print_block(1);
|
||||
disk->print_block(1025);
|
||||
disk->print_block(1026);
|
||||
disk->print_block(1027);
|
||||
disk->print_block(1080);
|
||||
disk->print_block(1081);
|
||||
disk->print_block(1082);
|
||||
disk->print_block(1083);
|
||||
disk->print_block(1084);
|
||||
disk->print_block(1085);
|
||||
|
||||
disk->print_block(1596);
|
||||
int N = 5;
|
||||
|
||||
disk->print_block(1597);
|
||||
u_int64_t buf2[4096] = {0};
|
||||
err = fs->read(&inode_data, (char *)buf2, (8 * N), 4096 - 8 - 8);
|
||||
|
||||
printf("\n\nREAD: %d\n", err);
|
||||
for (int i = 0; i < N; ++i)
|
||||
printf("%d ", buf2[i]);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user