diff --git a/lib/fs/datablock_manager.cpp b/lib/fs/datablock_manager.cpp index e793b29..192f463 100644 --- a/lib/fs/datablock_manager.cpp +++ b/lib/fs/datablock_manager.cpp @@ -58,6 +58,10 @@ int DataBlock_Manager_Bitmap::new_datablock(u_int64_t *block_num) { if ((err = fs->disk->read_block(bitmap_block_num, bitmap.buf)) < 0) return err; + // if (bitmap.get_next_node() == fs->superblock.free_list_head) + // printf("WARNING: ON LAST BITMAP " + // "BLOCK!\n"); + u_int64_t relative_block_num = bitmap.claim_relative_block(); if (relative_block_num == 0) diff --git a/lib/fs/fs_file_io.cpp b/lib/fs/fs_file_io.cpp index 8905a3d..db8dc37 100644 --- a/lib/fs/fs_file_io.cpp +++ b/lib/fs/fs_file_io.cpp @@ -31,8 +31,6 @@ 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, @@ -41,8 +39,6 @@ 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 < INDIRECT_BLOCKS) { @@ -52,8 +48,6 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data, start_index = INDIRECT_BLOCKS; } - // printf("test2.3\n"); - start_index -= INDIRECT_BLOCKS; if (start_index < INDIRECT_BLOCKS * INDIRECT_BLOCKS) { @@ -63,8 +57,6 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data, start_index = INDIRECT_BLOCKS * INDIRECT_BLOCKS; } - // printf("test2.4\n"); - start_index -= INDIRECT_BLOCKS * INDIRECT_BLOCKS; if (start_index < @@ -93,8 +85,6 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num, 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) @@ -104,8 +94,6 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num, } } - // 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) @@ -118,8 +106,6 @@ 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 { @@ -127,8 +113,6 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num, return err; } - // 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 - (num_blocks_indirect * this_layer_start_index); @@ -137,18 +121,11 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num, 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, next_layer_start_index, allocate, op)) < 0) return result; @@ -158,10 +135,9 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num, } if (result == 0) break; + next_layer_start_index = 0; } - // printf("test2.3.6 %d\n", indirect_num); - if (modified) { bool delete_block = true; for (size_t i = 0; i < IO_BLOCK_SIZE; ++i) @@ -179,9 +155,6 @@ 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; } @@ -193,9 +166,6 @@ public: char datablock_buf[IO_BLOCK_SIZE]; int err; - // 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); @@ -225,8 +195,6 @@ public: 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); @@ -324,8 +292,6 @@ 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); @@ -336,14 +302,10 @@ ssize_t Fs::write(INode_Data *inode_data, const 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); diff --git a/lib/main.cpp b/lib/main.cpp index 76875e2..1ac5e82 100644 --- a/lib/main.cpp +++ b/lib/main.cpp @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) { // err = fs->lseek_next_hole(&inode_data, offs + 100000); // printf("lseek_next_hole (%d): %d\n\n", offs + 100000, err); - RawDisk *disk = new FakeRawDisk(5000); + RawDisk *disk = new FakeRawDisk(5120); Fs *fs = new Fs(disk); fs->format(); @@ -188,40 +188,52 @@ int main(int argc, char *argv[]) { u_int64_t test_start_range = IO_BLOCK_SIZE * 3584; u_int64_t test_io_range = IO_BLOCK_SIZE * 100; - char ones[test_io_range]; - memset(ones, 1, test_io_range); - char twos[test_io_range]; - memset(twos, 2, test_io_range); + // char ones[test_io_range]; + // memset(ones, 1, test_io_range); + // char twos[test_io_range]; + // memset(twos, 2, test_io_range); - char *write_buf = ones; + char write_buf[test_io_range]; char reference_read_buf[test_io_range]; char test_read_buf[test_io_range]; size_t offset, count; int test_res, ref_res; bool reads_are_equal; - bool canwrite; + int num; - size_t weird_offset = 6508064; + // size_t weird_offset = 6508064; for (int i = 0; i < 100000; ++i) { offset = rand() % test_start_range; reads_are_equal = true; + num = rand() % 100; + if (num < 49) + num = 0; + else if (num < 99) + num = 1; + else + num = 2; - switch (rand() % 3) { + if (i % 100 == 0) + printf("%d\n", i); + + switch (num) { case 0: count = rand() % test_io_range; - write_buf = (write_buf == ones) ? twos : ones; - if (offset <= weird_offset && (count + offset) > weird_offset) - printf("write: %ds count=%d offset=%d\n", write_buf[0], count, offset); + memset(write_buf, i, count); + // write_buf = (write_buf == ones) ? twos : ones; + // if (offset <= weird_offset && (count + offset) > weird_offset || + // ((char)i == -77)) + // 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_io_range; - if (offset <= weird_offset && (count + offset) > weird_offset) - printf("read: count=%d offset=%d\n", count, offset); + // if (offset <= weird_offset && (count + offset) > weird_offset) + // 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); @@ -232,14 +244,14 @@ int main(int argc, char *argv[]) { } break; case 2: - if (offset <= weird_offset) - printf("truncate: length=%d\n", offset); + // if (offset <= weird_offset) + // printf("truncate: length=%d\n", offset); test_res = fs->truncate(&inode_data, offset); ref_res = ftruncate(fd, offset); break; } - // printf("test_res=%d, ref_res=%d\n", test_res, ref_res); + // printf("test_res=%d, ref_res=%d\n", test_res, ref_res); assert(test_res == ref_res); if (!reads_are_equal && count > 0) {