added better errno to fileio
This commit is contained in:
parent
2085daa433
commit
6c16225919
@ -18,7 +18,7 @@ public:
|
||||
ssize_t read(INode_Data *inode_data, char buf[], size_t count, size_t offset);
|
||||
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);
|
||||
int truncate(INode_Data *inode_data, off_t length);
|
||||
ssize_t lseek_next_data(INode_Data *inode_data, size_t offset);
|
||||
ssize_t lseek_next_hole(INode_Data *inode_data, size_t offset);
|
||||
|
||||
|
@ -41,6 +41,10 @@ public:
|
||||
|
||||
#define NUMBER_OF_DIRECT_BLOCKS \
|
||||
(((INODE_SIZE - NUMBER_OF_METADATA_BYTES) / sizeof(u_int64_t)) - 3)
|
||||
#define FILE_SIZE_MAX \
|
||||
(IO_BLOCK_SIZE * (NUMBER_OF_DIRECT_BLOCKS + INDIRECT_BLOCKS + \
|
||||
(INDIRECT_BLOCKS * INDIRECT_BLOCKS) + \
|
||||
(INDIRECT_BLOCKS * INDIRECT_BLOCKS * INDIRECT_BLOCKS)))
|
||||
|
||||
u_int64_t single_indirect_block, double_indirect_block, triple_indirect_block;
|
||||
u_int64_t direct_blocks[NUMBER_OF_DIRECT_BLOCKS];
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define FS_CONSTANTS_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <linux/fs.h>
|
||||
@ -11,6 +12,7 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#define IO_BLOCK_SIZE 4096
|
||||
#define INDIRECT_BLOCKS 512
|
||||
|
||||
|
1660
lib/files.cpp
1660
lib/files.cpp
File diff suppressed because it is too large
Load Diff
@ -52,8 +52,10 @@ int DataBlock_Manager_Bitmap::new_datablock(u_int64_t *block_num) {
|
||||
char zero_buf[IO_BLOCK_SIZE] = {0};
|
||||
|
||||
if (bitmap_block_num < block_segment_start ||
|
||||
bitmap_block_num >= block_segment_end)
|
||||
bitmap_block_num >= block_segment_end) {
|
||||
perror("Error with new_datablock freelist head\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((err = fs->disk->read_block(bitmap_block_num, bitmap.buf)) < 0)
|
||||
return err;
|
||||
@ -64,8 +66,10 @@ int DataBlock_Manager_Bitmap::new_datablock(u_int64_t *block_num) {
|
||||
|
||||
u_int64_t relative_block_num = bitmap.claim_relative_block();
|
||||
|
||||
if (relative_block_num == 0)
|
||||
if (relative_block_num == 0) {
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t block_num_ = relative_block_num + bitmap_block_num;
|
||||
|
||||
|
@ -31,7 +31,8 @@ int Fs::sweep_inode_datablocks(INode_Data *inode_data,
|
||||
DatablockOperation *op) {
|
||||
int result;
|
||||
|
||||
//printf("SWEEP %llu %llu %llu\n", inode_data->inode_num, inode_data->single_indirect_block, inode_data->double_indirect_block);
|
||||
// printf("SWEEP %llu %llu %llu\n", inode_data->inode_num,
|
||||
// inode_data->single_indirect_block, inode_data->double_indirect_block);
|
||||
|
||||
u_int64_t start_index = start_block_index;
|
||||
for (size_t i = start_index; i < NUMBER_OF_DIRECT_BLOCKS; ++i) {
|
||||
@ -96,7 +97,8 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
|
||||
}
|
||||
}
|
||||
|
||||
//if((*block_num)>30000000000000LL)printf("DIES 1 %llu %d %llu\n", *block_num, indirect_num, start_block_index);
|
||||
// if((*block_num)>30000000000000LL)printf("DIES 1 %llu %d %llu\n",
|
||||
// *block_num, indirect_num, start_block_index);
|
||||
|
||||
if (indirect_num == 0) {
|
||||
bool delete_block = false;
|
||||
@ -113,7 +115,7 @@ int Fs::sweep_datablocks(u_int64_t *block_num, int indirect_num,
|
||||
if ((*block_num) == 0) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
} else {
|
||||
|
||||
|
||||
if ((err = disk->read_block(*block_num, buf)) < 0)
|
||||
return err;
|
||||
}
|
||||
@ -175,7 +177,8 @@ public:
|
||||
std::min(IO_BLOCK_SIZE - offset, count - bytes_completed);
|
||||
|
||||
if (block_num != 0) {
|
||||
if((block_num)>3000000000000LL)printf("DIES 2\n");
|
||||
if ((block_num) > 3000000000000LL)
|
||||
printf("DIES 2\n");
|
||||
if ((err = fs->disk->read_block(block_num, datablock_buf)) < 0)
|
||||
return err;
|
||||
|
||||
@ -204,8 +207,9 @@ public:
|
||||
size_t write_size =
|
||||
std::min(IO_BLOCK_SIZE - offset, count - bytes_completed);
|
||||
|
||||
if (write_size < IO_BLOCK_SIZE){
|
||||
if((block_num)>3000000000000LL)printf("DIES 3\n");
|
||||
if (write_size < IO_BLOCK_SIZE) {
|
||||
if ((block_num) > 3000000000000LL)
|
||||
printf("DIES 3\n");
|
||||
if ((err = fs->disk->read_block(block_num, datablock_buf)) < 0)
|
||||
return err;
|
||||
}
|
||||
@ -235,7 +239,8 @@ public:
|
||||
(*delete_block) = true;
|
||||
return 1;
|
||||
}
|
||||
if((block_num)>3000000000000LL)printf("DIES 4\n");
|
||||
if ((block_num) > 3000000000000LL)
|
||||
printf("DIES 4\n");
|
||||
if ((err = fs->disk->read_block(block_num, datablock_buf)) < 0)
|
||||
return err;
|
||||
|
||||
@ -289,9 +294,10 @@ ssize_t Fs::read(INode_Data *inode_data, char buf[], size_t count,
|
||||
op.bytes_completed = 0;
|
||||
op.fs = this;
|
||||
|
||||
//printf("IN READ %llu %llu %llu\n", inode_data->inode_num, inode_data->single_indirect_block, inode_data->double_indirect_block);
|
||||
// printf("IN READ %llu %llu %llu\n", inode_data->inode_num,
|
||||
// inode_data->single_indirect_block, inode_data->double_indirect_block);
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, false,
|
||||
&op)) != 0)
|
||||
&op)) < 0)
|
||||
return err;
|
||||
|
||||
return op.bytes_completed;
|
||||
@ -301,6 +307,11 @@ ssize_t Fs::write(INode_Data *inode_data, const char buf[], size_t count,
|
||||
size_t offset) {
|
||||
int err;
|
||||
|
||||
if (count + offset > FILE_SIZE_MAX) {
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
|
||||
|
||||
@ -311,8 +322,8 @@ ssize_t Fs::write(INode_Data *inode_data, const char buf[], size_t count,
|
||||
op.bytes_completed = 0;
|
||||
op.fs = this;
|
||||
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, true,
|
||||
&op)) != 0)
|
||||
if ((err = sweep_inode_datablocks(inode_data, start_block_index, true, &op)) <
|
||||
0)
|
||||
return err;
|
||||
|
||||
inode_data->metadata.size =
|
||||
@ -321,9 +332,19 @@ ssize_t Fs::write(INode_Data *inode_data, const char buf[], size_t count,
|
||||
return op.bytes_completed;
|
||||
}
|
||||
|
||||
int Fs::truncate(INode_Data *inode_data, size_t length) {
|
||||
int Fs::truncate(INode_Data *inode_data, off_t length) {
|
||||
int err;
|
||||
|
||||
if (length > FILE_SIZE_MAX) {
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (length < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t start_block_index = length / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = length - (start_block_index * IO_BLOCK_SIZE);
|
||||
|
||||
@ -343,8 +364,10 @@ int Fs::truncate(INode_Data *inode_data, size_t length) {
|
||||
ssize_t Fs::lseek_next_data(INode_Data *inode_data, size_t offset) {
|
||||
int err;
|
||||
|
||||
if (offset >= inode_data->metadata.size)
|
||||
if (offset >= inode_data->metadata.size) {
|
||||
errno = ENXIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
|
||||
@ -359,8 +382,10 @@ ssize_t Fs::lseek_next_data(INode_Data *inode_data, size_t offset) {
|
||||
&op)) < 0)
|
||||
return err;
|
||||
|
||||
if (op.bytes_completed >= inode_data->metadata.size)
|
||||
if (op.bytes_completed >= inode_data->metadata.size) {
|
||||
errno = ENXIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return op.bytes_completed;
|
||||
}
|
||||
@ -368,8 +393,10 @@ ssize_t Fs::lseek_next_data(INode_Data *inode_data, size_t offset) {
|
||||
ssize_t Fs::lseek_next_hole(INode_Data *inode_data, size_t offset) {
|
||||
int err;
|
||||
|
||||
if (offset >= inode_data->metadata.size)
|
||||
if (offset >= inode_data->metadata.size) {
|
||||
errno = ENXIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t start_block_index = offset / IO_BLOCK_SIZE;
|
||||
size_t internal_offset = offset - (start_block_index * IO_BLOCK_SIZE);
|
||||
|
@ -43,7 +43,7 @@ RealRawDisk::RealRawDisk(const char *directory)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//diskSize = 27648 * IO_BLOCK_SIZE;
|
||||
// diskSize = 27648 * IO_BLOCK_SIZE;
|
||||
|
||||
// Calculate the size in bytes
|
||||
numSectors = diskSize / 512; // Assuming a sector size of 512 bytes
|
||||
@ -65,16 +65,18 @@ int RealRawDisk::read_block(u_int64_t block_number, char *buffer) {
|
||||
if (lseek(fd, offset, SEEK_SET) == (u_int64_t)-1) {
|
||||
printf("LSEEK ERROR %llu %llu\n", block_number, offset);
|
||||
perror("Error seeking to offset");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: this is incorrect
|
||||
ssize_t bytesRead = read(fd, buffer, IO_BLOCK_SIZE);
|
||||
//printf("READ BLOCK: %llu\n", block_number);
|
||||
//for (int i = 0; i < IO_BLOCK_SIZE; i++)printf("%x", buffer[i]&0xff);
|
||||
//printf("\n");
|
||||
// printf("READ BLOCK: %llu\n", block_number);
|
||||
// for (int i = 0; i < IO_BLOCK_SIZE; i++)printf("%x", buffer[i]&0xff);
|
||||
// printf("\n");
|
||||
if (bytesRead < IO_BLOCK_SIZE) {
|
||||
perror("Error reading from device");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -86,6 +88,7 @@ int RealRawDisk::write_block(u_int64_t block_number, char *buffer) {
|
||||
|
||||
if (lseek(fd, offset, SEEK_SET) == (u_int64_t)-1) {
|
||||
perror("Error seeking to offset");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -93,6 +96,7 @@ int RealRawDisk::write_block(u_int64_t block_number, char *buffer) {
|
||||
ssize_t bytesWritten = write(fd, buffer, IO_BLOCK_SIZE);
|
||||
if (bytesWritten < IO_BLOCK_SIZE) {
|
||||
perror("Error writing to device");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -118,6 +122,7 @@ int FakeRawDisk::read_block(u_int64_t block_number, char *buffer) {
|
||||
|
||||
if (offset + IO_BLOCK_SIZE > diskSize) {
|
||||
perror("Error reading past fake disk size");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -131,6 +136,7 @@ int FakeRawDisk::write_block(u_int64_t block_number, char *buffer) {
|
||||
|
||||
if (offset + IO_BLOCK_SIZE > diskSize) {
|
||||
perror("Error writing past fake disk size");
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user