add fs
This commit is contained in:
parent
c254d78621
commit
e28d9d9749
@ -4,10 +4,13 @@ project(vc6)
|
|||||||
set(CMAKE_C_STANDARD 23)
|
set(CMAKE_C_STANDARD 23)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
set(CMAKE_COLOR_DIAGNOSTICS ON)
|
||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
option(ASAN "Use -fsanitize=address." OFF)
|
||||||
|
|
||||||
|
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
add_subdirectory(boot)
|
add_subdirectory(boot)
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +1,38 @@
|
|||||||
|
|
||||||
add_library(project_options INTERFACE)
|
add_library(opts INTERFACE)
|
||||||
|
|
||||||
target_compile_features(project_options INTERFACE c_std_23)
|
target_compile_features(opts INTERFACE c_std_23)
|
||||||
|
|
||||||
target_compile_options(project_options INTERFACE
|
if(MSVC)
|
||||||
$<$<COMPILE_LANG_AND_ID:C,MSVC>:/W4>
|
target_compile_options(opts INTERFACE /W4)
|
||||||
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang,AppleClang>:-Wall;-Wextra;-Wpedantic;-Werror=switch>
|
|
||||||
|
if(ASAN)
|
||||||
|
target_compile_options(opts INTERFACE /fsanitize=address)
|
||||||
|
target_link_options(opts INTERFACE /fsanitize=address)
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
|
target_compile_options(opts INTERFACE
|
||||||
|
-pedantic
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Werror=switch
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if(ASAN)
|
||||||
|
target_compile_options(opts INTERFACE -fsanitize=address)
|
||||||
|
target_link_options(opts INTERFACE -fsanitize=address)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
add_library(isa STATIC isa.c)
|
add_library(isa STATIC isa.c)
|
||||||
target_include_directories(isa PUBLIC .)
|
target_include_directories(isa PUBLIC .)
|
||||||
|
|
||||||
add_executable(emu WIN32 emu.c)
|
add_executable(emu WIN32 emu.c)
|
||||||
target_link_libraries(emu PRIVATE project_options isa SDL3::SDL3)
|
target_link_libraries(emu PRIVATE opts isa SDL3::SDL3)
|
||||||
|
|
||||||
add_executable(asm asm.c)
|
add_executable(asm asm.c)
|
||||||
target_link_libraries(asm PRIVATE project_options isa)
|
target_link_libraries(asm PRIVATE opts isa)
|
||||||
|
|
||||||
target_compile_options(asm PRIVATE -fsanitize=address)
|
add_executable(fs fs.c)
|
||||||
target_link_options(asm PRIVATE -fsanitize=address)
|
target_link_libraries(fs PRIVATE opts)
|
||||||
|
|
||||||
|
|||||||
@ -375,6 +375,8 @@ static void vcd_render(Vcd *vcd, VM *vm)
|
|||||||
vcd_render_char(vcd, ch, x, y);
|
vcd_render_char(vcd, ch, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int vm_thread_start(void *opt)
|
static int vm_thread_start(void *opt)
|
||||||
@ -634,3 +636,8 @@ uint64_t font_char(uint8_t ch)
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *__asan_default_options()
|
||||||
|
{
|
||||||
|
return "detect_leaks=0";
|
||||||
|
}
|
||||||
|
|||||||
880
src/fs.c
Normal file
880
src/fs.c
Normal file
@ -0,0 +1,880 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
constexpr size_t block_size = 512;
|
||||||
|
typedef uint16_t Block[block_size];
|
||||||
|
|
||||||
|
constexpr size_t size_of_block = sizeof(Block) / sizeof(uint16_t);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Block *blocks;
|
||||||
|
size_t blocks_count;
|
||||||
|
} Disk;
|
||||||
|
|
||||||
|
void disk_read(Disk *d, Block block, uint16_t id)
|
||||||
|
{
|
||||||
|
assert(id < d->blocks_count);
|
||||||
|
memcpy(block, &d->blocks[id], size_of_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
void disk_write(Disk *d, const Block block, uint16_t id)
|
||||||
|
{
|
||||||
|
assert(id < d->blocks_count);
|
||||||
|
memcpy(&d->blocks[id], block, size_of_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t blocks_cap = block_size * 16;
|
||||||
|
constexpr size_t inode_size = 16;
|
||||||
|
constexpr size_t direentry_max_size = 64;
|
||||||
|
constexpr size_t inodes_per_block = block_size / inode_size;
|
||||||
|
constexpr size_t inodeblocks_cap = 32;
|
||||||
|
constexpr size_t inodes_cap = inodeblocks_cap * inodes_per_block;
|
||||||
|
constexpr size_t inodebitmap_size = inodes_cap / 16;
|
||||||
|
|
||||||
|
constexpr size_t inode_blocks_cap = 12;
|
||||||
|
constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1;
|
||||||
|
constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count;
|
||||||
|
constexpr size_t inode_name_cap = 61;
|
||||||
|
|
||||||
|
typedef enum : uint16_t {
|
||||||
|
INode_File,
|
||||||
|
INode_Dir,
|
||||||
|
} INodeTy;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t id;
|
||||||
|
uint16_t mode;
|
||||||
|
uint16_t blocks_count;
|
||||||
|
uint16_t last_block_size;
|
||||||
|
uint16_t blocks[inode_blocks_cap];
|
||||||
|
} INode;
|
||||||
|
|
||||||
|
static_assert(sizeof(INode) / sizeof(uint16_t) == inode_size);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t inode;
|
||||||
|
uint16_t next;
|
||||||
|
uint16_t name_len;
|
||||||
|
uint16_t name[inode_name_cap];
|
||||||
|
} DirEntry;
|
||||||
|
|
||||||
|
static_assert(sizeof(DirEntry) / sizeof(uint16_t) == direentry_max_size);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t blocks_count;
|
||||||
|
uint16_t inodeblocks_count;
|
||||||
|
uint16_t inodeblocks[inodeblocks_cap];
|
||||||
|
uint16_t inodebitmap[inodebitmap_size];
|
||||||
|
} Superblock;
|
||||||
|
|
||||||
|
static_assert(sizeof(Superblock) <= sizeof(Block));
|
||||||
|
|
||||||
|
static size_t direntry_size(const DirEntry *entry)
|
||||||
|
{
|
||||||
|
return (sizeof(entry->inode) + sizeof(entry->next) + sizeof(entry->name_len)
|
||||||
|
+ sizeof(entry->name[0]) * entry->name_len)
|
||||||
|
/ sizeof(uint16_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Disk *disk;
|
||||||
|
uint16_t block_offset;
|
||||||
|
uint16_t blocks_count;
|
||||||
|
Block *superblock;
|
||||||
|
Superblock *sb;
|
||||||
|
Block *block_bitmap;
|
||||||
|
bool out_of_sync;
|
||||||
|
} Fs;
|
||||||
|
|
||||||
|
static void bitmap_set(uint16_t bitmap[], uint16_t id, bool v)
|
||||||
|
{
|
||||||
|
uint16_t *row = &bitmap[id / 16];
|
||||||
|
uint16_t bit = 1u << id % 16;
|
||||||
|
*row = v ? *row | bit : *row & ~bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool bitmap_get(uint16_t bitmap[], uint16_t id)
|
||||||
|
{
|
||||||
|
return bitmap[id / 16] >> id % 16 & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t allocate_block(Fs *fs)
|
||||||
|
{
|
||||||
|
uint16_t block_id = 0;
|
||||||
|
for (size_t i = 2; i < blocks_cap; ++i) {
|
||||||
|
if (!bitmap_get(*fs->block_bitmap, i)) {
|
||||||
|
block_id = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(block_id != 0);
|
||||||
|
|
||||||
|
bitmap_set(*fs->block_bitmap, block_id, true);
|
||||||
|
|
||||||
|
fs->out_of_sync = true;
|
||||||
|
return block_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deallocate_block(Fs *fs, uint16_t block_id)
|
||||||
|
{
|
||||||
|
bitmap_set(*fs->block_bitmap, block_id, false);
|
||||||
|
fs->out_of_sync = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t allocate_inode(Fs *fs)
|
||||||
|
{
|
||||||
|
Superblock *sb = fs->sb;
|
||||||
|
|
||||||
|
uint16_t inode_id = UINT16_MAX;
|
||||||
|
for (size_t i = 0; i < sb->inodeblocks_count * inodes_per_block; ++i) {
|
||||||
|
if (!bitmap_get(sb->inodebitmap, i)) {
|
||||||
|
inode_id = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inode_id == UINT16_MAX) {
|
||||||
|
assert(sb->inodeblocks_count < inodeblocks_cap);
|
||||||
|
uint16_t new_block_id = allocate_block(fs);
|
||||||
|
|
||||||
|
inode_id = sb->inodeblocks_count * inodes_per_block;
|
||||||
|
|
||||||
|
sb->inodeblocks[sb->inodeblocks_count] = new_block_id;
|
||||||
|
sb->inodeblocks_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bitmap_set(sb->inodebitmap, inode_id, true);
|
||||||
|
fs->out_of_sync = true;
|
||||||
|
|
||||||
|
return inode_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deallocate_inode(Fs *fs, uint16_t inode_id)
|
||||||
|
{
|
||||||
|
bitmap_set(fs->sb->inodebitmap, inode_id, false);
|
||||||
|
fs->out_of_sync = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_block(Fs *fs, const Block block, uint16_t id)
|
||||||
|
{
|
||||||
|
disk_write(fs->disk, block, id + fs->block_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_block(Fs *fs, Block block, uint16_t id)
|
||||||
|
{
|
||||||
|
disk_read(fs->disk, block, id + fs->block_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_inode(Fs *fs, const INode *inode)
|
||||||
|
{
|
||||||
|
uint16_t inode_block_idx = inode->id / inodes_per_block;
|
||||||
|
uint16_t block_offset = inode->id % inodes_per_block * inode_size;
|
||||||
|
assert(inode_block_idx < fs->blocks_count && block_offset < block_size);
|
||||||
|
|
||||||
|
uint16_t block_id = fs->sb->inodeblocks[inode_block_idx];
|
||||||
|
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
memcpy(&block[block_offset], inode, sizeof(INode));
|
||||||
|
write_block(fs, block, block_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_inode(Fs *fs, INode *out_inode, uint16_t inode_id)
|
||||||
|
{
|
||||||
|
uint16_t inode_block_idx = inode_id / inodes_per_block;
|
||||||
|
uint16_t block_offset = inode_id % inodes_per_block * inode_size;
|
||||||
|
assert(inode_block_idx < fs->blocks_count && block_offset < block_size);
|
||||||
|
|
||||||
|
uint16_t block_id = fs->sb->inodeblocks[inode_block_idx];
|
||||||
|
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
memcpy(out_inode, &block[block_offset], sizeof(INode));
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t inode_allocate_block(Fs *fs, INode *inode)
|
||||||
|
{
|
||||||
|
assert((size_t)inode->blocks_count + 1 < inode_blocks_cap);
|
||||||
|
|
||||||
|
uint16_t block_id = allocate_block(fs);
|
||||||
|
|
||||||
|
if (inode->blocks_count < inode_ndir_blocks_count) {
|
||||||
|
inode->blocks[inode->blocks_count] = block_id;
|
||||||
|
inode->blocks_count += 1;
|
||||||
|
return block_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t ind_block_id = inode->blocks[inode_ind_block_idx];
|
||||||
|
Block ind_block;
|
||||||
|
read_block(fs, ind_block, ind_block_id);
|
||||||
|
ind_block[inode->blocks_count - inode_ndir_blocks_count] = block_id;
|
||||||
|
write_block(fs, ind_block, ind_block_id);
|
||||||
|
return block_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool inode_has_ind_block(Fs *fs, const INode *inode)
|
||||||
|
{
|
||||||
|
(void)fs;
|
||||||
|
return inode->blocks_count > inode_ndir_blocks_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t *inode_block_at(INode *inode, uint16_t i, Block ind_block)
|
||||||
|
{
|
||||||
|
assert(i < inode->blocks_count);
|
||||||
|
|
||||||
|
if (i < inode_ndir_blocks_count)
|
||||||
|
return &inode->blocks[i];
|
||||||
|
|
||||||
|
assert(i < inode_ndir_blocks_count + block_size);
|
||||||
|
return &ind_block[i - inode_ndir_blocks_count];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void inode_deallocate_block(Fs *fs, INode *inode, uint16_t block_id)
|
||||||
|
{
|
||||||
|
assert(inode->blocks_count > 0);
|
||||||
|
|
||||||
|
bool has_ind_block_before = inode_has_ind_block(fs, inode);
|
||||||
|
uint16_t ind_block_id = inode->blocks[inode_ind_block_idx];
|
||||||
|
|
||||||
|
Block ind_block;
|
||||||
|
if (has_ind_block_before)
|
||||||
|
read_block(fs, ind_block, ind_block_id);
|
||||||
|
|
||||||
|
uint16_t i = 0;
|
||||||
|
for (; i < inode->blocks_count; ++i)
|
||||||
|
if (*inode_block_at(inode, i, ind_block) == block_id)
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (; i < inode->blocks_count - 1; ++i)
|
||||||
|
*inode_block_at(inode, i, ind_block)
|
||||||
|
= *inode_block_at(inode, i + 1, ind_block);
|
||||||
|
|
||||||
|
if (inode_has_ind_block(fs, inode))
|
||||||
|
write_block(fs, ind_block, ind_block_id);
|
||||||
|
else if (has_ind_block_before)
|
||||||
|
deallocate_block(fs, ind_block_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t inode_last_block(Fs *fs, INode *inode)
|
||||||
|
{
|
||||||
|
Block ind_block;
|
||||||
|
if (inode_has_ind_block(fs, inode))
|
||||||
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
||||||
|
return *inode_block_at(inode, inode->blocks_count - 1, ind_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void make_dir_with_inode_ids(
|
||||||
|
Fs *fs, INode *out_inode, uint16_t inode_id, uint16_t parent_inode_id)
|
||||||
|
{
|
||||||
|
*out_inode = (INode) {
|
||||||
|
.id = inode_id,
|
||||||
|
.mode = INode_Dir,
|
||||||
|
.blocks_count = 0,
|
||||||
|
.last_block_size = 0,
|
||||||
|
.blocks = { },
|
||||||
|
};
|
||||||
|
|
||||||
|
DirEntry parent_entry = {
|
||||||
|
.inode = parent_inode_id,
|
||||||
|
.next = 0,
|
||||||
|
.name_len = 2,
|
||||||
|
.name = { '.', '.' },
|
||||||
|
};
|
||||||
|
|
||||||
|
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
||||||
|
Block block = { };
|
||||||
|
memcpy(
|
||||||
|
block, &parent_entry, direntry_size(&parent_entry) * sizeof(uint16_t));
|
||||||
|
write_block(fs, block, entry_block_id);
|
||||||
|
out_inode->last_block_size = direntry_size(&parent_entry);
|
||||||
|
|
||||||
|
write_inode(fs, out_inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_dir_entry(Fs *fs, INode *dir_inode, const DirEntry *direntry)
|
||||||
|
{
|
||||||
|
uint16_t block_id = inode_last_block(fs, dir_inode);
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
DirEntry entry = { };
|
||||||
|
uint16_t offset = 0;
|
||||||
|
memcpy(&entry, &block[offset], direentry_max_size * sizeof(uint16_t));
|
||||||
|
|
||||||
|
while (entry.next) {
|
||||||
|
offset = entry.next;
|
||||||
|
memcpy(&entry, &block[offset], direentry_max_size * sizeof(uint16_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset + direntry_size(direntry) > block_size) {
|
||||||
|
uint16_t block_id = inode_allocate_block(fs, dir_inode);
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
memcpy(block, direntry, direntry_size(direntry) * sizeof(uint16_t));
|
||||||
|
write_block(fs, block, block_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t new_offset = offset + direntry_size(&entry);
|
||||||
|
entry.next = new_offset;
|
||||||
|
|
||||||
|
memcpy(&block[offset], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
||||||
|
|
||||||
|
memcpy(&block[new_offset],
|
||||||
|
direntry,
|
||||||
|
direntry_size(direntry) * sizeof(uint16_t));
|
||||||
|
write_block(fs, block, block_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_inode_to_dir(
|
||||||
|
Fs *fs, INode *dir_inode, const INode *inode, const char *name)
|
||||||
|
{
|
||||||
|
size_t name_len = strlen(name);
|
||||||
|
assert(dir_inode->mode == INode_Dir && name_len <= inode_name_cap);
|
||||||
|
|
||||||
|
DirEntry entry = {
|
||||||
|
.inode = inode->id,
|
||||||
|
.next = 0,
|
||||||
|
.name_len = name_len,
|
||||||
|
.name = { },
|
||||||
|
};
|
||||||
|
for (size_t i = 0; i < name_len; ++i)
|
||||||
|
entry.name[i] = name[i];
|
||||||
|
|
||||||
|
write_dir_entry(fs, dir_inode, &entry);
|
||||||
|
write_inode(fs, dir_inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void inode_foreach_block(Fs *fs,
|
||||||
|
const INode *inode,
|
||||||
|
void (*func)(uint16_t block_id, void *arg),
|
||||||
|
void *arg)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
for (; i < inode->blocks_count && i < inode_ndir_blocks_count; ++i)
|
||||||
|
func(inode->blocks[i], arg);
|
||||||
|
|
||||||
|
if (inode->blocks_count <= inode_ndir_blocks_count)
|
||||||
|
return;
|
||||||
|
assert(inode->blocks_count <= inode_ndir_blocks_count + block_size);
|
||||||
|
|
||||||
|
Block ind_block;
|
||||||
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
||||||
|
|
||||||
|
for (; i < inode->blocks_count; ++i)
|
||||||
|
func(ind_block[i], arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count)
|
||||||
|
{
|
||||||
|
assert(blocks_count <= sizeof(Block) * 8);
|
||||||
|
*fs = (Fs) {
|
||||||
|
.disk = disk,
|
||||||
|
.block_offset = block_offset,
|
||||||
|
.blocks_count = blocks_count,
|
||||||
|
.superblock = calloc(1, sizeof(Block)),
|
||||||
|
.sb = nullptr,
|
||||||
|
.block_bitmap = calloc(1, sizeof(Block)),
|
||||||
|
.out_of_sync = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
fs->sb = (Superblock *)fs->superblock;
|
||||||
|
|
||||||
|
bitmap_set(*fs->block_bitmap, 0, true);
|
||||||
|
bitmap_set(*fs->block_bitmap, 1, true);
|
||||||
|
|
||||||
|
uint16_t root_inode_id = allocate_inode(fs);
|
||||||
|
assert(root_inode_id == 0);
|
||||||
|
|
||||||
|
INode root_inode;
|
||||||
|
make_dir_with_inode_ids(fs, &root_inode, root_inode_id, root_inode_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_destroy(Fs *fs)
|
||||||
|
{
|
||||||
|
free(fs->superblock);
|
||||||
|
free(fs->block_bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_sync(Fs *fs)
|
||||||
|
{
|
||||||
|
if (!fs->out_of_sync)
|
||||||
|
return;
|
||||||
|
write_block(fs, *fs->superblock, 0);
|
||||||
|
write_block(fs, *fs->block_bitmap, 1);
|
||||||
|
fs->out_of_sync = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_root(Fs *fs, INode *out_inode)
|
||||||
|
{
|
||||||
|
read_inode(fs, out_inode, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_inode(Fs *fs, INode *out_inode, uint16_t inode_id)
|
||||||
|
{
|
||||||
|
read_inode(fs, out_inode, inode_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_make_dir(Fs *fs, INode *out_inode, INode *parent, const char *name)
|
||||||
|
{
|
||||||
|
uint16_t inode_id = allocate_inode(fs);
|
||||||
|
make_dir_with_inode_ids(fs, out_inode, inode_id, parent->id);
|
||||||
|
write_inode(fs, out_inode);
|
||||||
|
add_inode_to_dir(fs, parent, out_inode, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_make_file(Fs *fs, INode *out_inode, INode *parent, const char *name)
|
||||||
|
{
|
||||||
|
uint16_t inode_id = allocate_inode(fs);
|
||||||
|
|
||||||
|
*out_inode = (INode) {
|
||||||
|
.id = inode_id,
|
||||||
|
.mode = INode_File,
|
||||||
|
.blocks_count = 0,
|
||||||
|
.last_block_size = 0,
|
||||||
|
.blocks = { },
|
||||||
|
};
|
||||||
|
|
||||||
|
write_inode(fs, out_inode);
|
||||||
|
add_inode_to_dir(fs, parent, out_inode, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void remove_block_fn(uint16_t block_id, void *arg)
|
||||||
|
{
|
||||||
|
Fs *fs = arg;
|
||||||
|
deallocate_block(fs, block_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct remove_direntry_fn_args {
|
||||||
|
Fs *fs;
|
||||||
|
uint16_t inode_id;
|
||||||
|
uint16_t out_empty_block_id;
|
||||||
|
bool matched;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void inode_remove_blocks(Fs *fs, INode *inode)
|
||||||
|
{
|
||||||
|
inode_foreach_block(fs, inode, remove_block_fn, fs);
|
||||||
|
if (inode->blocks_count > inode_ndir_blocks_count)
|
||||||
|
deallocate_block(fs, inode->blocks[inode_ind_block_idx]);
|
||||||
|
inode->blocks_count = 0;
|
||||||
|
inode->last_block_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void remove_direntry_fn(uint16_t block_id, void *arg)
|
||||||
|
{
|
||||||
|
struct remove_direntry_fn_args *args = arg;
|
||||||
|
Fs *fs = args->fs;
|
||||||
|
|
||||||
|
if (args->matched)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
DirEntry entry = { };
|
||||||
|
uint16_t curr = 0;
|
||||||
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
||||||
|
|
||||||
|
if (!curr && !entry.next && entry.inode == args->inode_id) {
|
||||||
|
args->out_empty_block_id = block_id;
|
||||||
|
args->matched = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t prev = 0;
|
||||||
|
|
||||||
|
while (entry.next && entry.inode != args->inode_id) {
|
||||||
|
prev = curr;
|
||||||
|
curr = entry.next;
|
||||||
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!entry.next && entry.inode != args->inode_id)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!entry.next) {
|
||||||
|
memcpy(&entry, &block[prev], direentry_max_size * sizeof(uint16_t));
|
||||||
|
entry.next = 0;
|
||||||
|
memcpy(&block[prev], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (entry.next) {
|
||||||
|
prev = curr;
|
||||||
|
curr = entry.next;
|
||||||
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
||||||
|
memcpy(&block[prev], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
write_block(fs, block, block_id);
|
||||||
|
args->matched = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_remove(Fs *fs, INode *inode, INode *parent)
|
||||||
|
{
|
||||||
|
inode_remove_blocks(fs, inode);
|
||||||
|
struct remove_direntry_fn_args args = { fs, inode->id, 0, false };
|
||||||
|
inode_foreach_block(fs, parent, remove_direntry_fn, &args);
|
||||||
|
assert(args.matched);
|
||||||
|
if (args.out_empty_block_id)
|
||||||
|
inode_deallocate_block(fs, inode, args.out_empty_block_id);
|
||||||
|
deallocate_inode(fs, inode->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t inode;
|
||||||
|
char name[inode_name_cap + 1];
|
||||||
|
} ListEntry;
|
||||||
|
|
||||||
|
struct list_block_entries_fn_args {
|
||||||
|
Fs *fs;
|
||||||
|
ListEntry *entries;
|
||||||
|
size_t entries_cap;
|
||||||
|
size_t *entries_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||||
|
|
||||||
|
static void list_block_entries_fn(uint16_t block_id, void *arg)
|
||||||
|
{
|
||||||
|
struct list_block_entries_fn_args *args = arg;
|
||||||
|
Fs *fs = args->fs;
|
||||||
|
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
DirEntry entry = { };
|
||||||
|
do {
|
||||||
|
if (*args->entries_size + 1 >= args->entries_cap)
|
||||||
|
return;
|
||||||
|
|
||||||
|
memcpy(&entry,
|
||||||
|
&block[entry.next],
|
||||||
|
MIN(direentry_max_size, block_size - entry.next));
|
||||||
|
|
||||||
|
ListEntry list_entry = {
|
||||||
|
.inode = entry.inode,
|
||||||
|
.name = { },
|
||||||
|
};
|
||||||
|
for (size_t i = 0; i < entry.name_len; ++i)
|
||||||
|
list_entry.name[i] = entry.name[i];
|
||||||
|
args->entries[*args->entries_size] = list_entry;
|
||||||
|
*args->entries_size += 1;
|
||||||
|
} while (entry.next != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fs_list(
|
||||||
|
Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode)
|
||||||
|
{
|
||||||
|
size_t entries_size = 0;
|
||||||
|
struct list_block_entries_fn_args args
|
||||||
|
= { fs, entries, entries_cap, &entries_size };
|
||||||
|
inode_foreach_block(fs, inode, list_block_entries_fn, &args);
|
||||||
|
return entries_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t fs_write(
|
||||||
|
Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size)
|
||||||
|
{
|
||||||
|
inode_remove_blocks(fs, inode);
|
||||||
|
uint16_t i = 0;
|
||||||
|
while (i < buffer_size) {
|
||||||
|
uint16_t block_id = inode_allocate_block(fs, inode);
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
uint16_t chunk_size = MIN((size_t)buffer_size - i, block_size);
|
||||||
|
memcpy(block, &buffer[i], chunk_size * sizeof(uint16_t));
|
||||||
|
write_block(fs, block, block_id);
|
||||||
|
inode->last_block_size = chunk_size;
|
||||||
|
i += chunk_size;
|
||||||
|
}
|
||||||
|
write_inode(fs, inode);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t fs_read(Fs *fs,
|
||||||
|
INode *inode,
|
||||||
|
uint16_t *buffer,
|
||||||
|
uint16_t buffer_size,
|
||||||
|
uint16_t offset)
|
||||||
|
{
|
||||||
|
Block ind_block;
|
||||||
|
if (inode_has_ind_block(fs, inode))
|
||||||
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
||||||
|
|
||||||
|
uint16_t block_idx = offset / block_size;
|
||||||
|
uint16_t block_offset = offset / block_size;
|
||||||
|
uint16_t i = 0;
|
||||||
|
|
||||||
|
for (; block_idx < inode->blocks_count && i < buffer_size; ++block_idx) {
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, *inode_block_at(inode, block_idx, ind_block));
|
||||||
|
uint16_t chunk_size
|
||||||
|
= MIN((size_t)buffer_size - i, block_size - block_offset);
|
||||||
|
memcpy(&buffer[i], &block[block_offset], chunk_size);
|
||||||
|
i += chunk_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_info(Fs *fs)
|
||||||
|
{
|
||||||
|
printf("block_offset = %u\n", fs->block_offset);
|
||||||
|
printf("blocks_count = %u\n", fs->blocks_count);
|
||||||
|
printf("out_of_sync = %s\n", fs->out_of_sync ? "true" : "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_superblock(Fs *fs)
|
||||||
|
{
|
||||||
|
Superblock *sb = fs->sb;
|
||||||
|
printf("blocks_count = %u\n", sb->blocks_count);
|
||||||
|
printf("inode_blocks_count = %u\n", sb->inodeblocks_count);
|
||||||
|
printf("inode_blocks = [");
|
||||||
|
for (size_t i = 0; i < sb->inodeblocks_count; ++i)
|
||||||
|
printf("%s%u", i != 0 ? ", " : "", sb->inodeblocks[i]);
|
||||||
|
printf("]\n");
|
||||||
|
printf("inode_bitmap = [");
|
||||||
|
for (size_t i = 0; i < inodebitmap_size; ++i)
|
||||||
|
if (bitmap_get(sb->inodebitmap, i))
|
||||||
|
printf("%s%zu", i > 0 ? ", " : "", i);
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_block_bitmap(Fs *fs)
|
||||||
|
{
|
||||||
|
printf("[");
|
||||||
|
for (size_t i = 0; i < inodebitmap_size; ++i)
|
||||||
|
if (bitmap_get(*fs->block_bitmap, i))
|
||||||
|
printf("%s%zu", i > 0 ? ", " : "", i);
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_inode(Fs *fs, uint16_t inode_id)
|
||||||
|
{
|
||||||
|
INode inode;
|
||||||
|
read_inode(fs, &inode, inode_id);
|
||||||
|
printf("id = %u\n", inode.id);
|
||||||
|
printf("mode = %u (%s)\n",
|
||||||
|
inode.mode,
|
||||||
|
inode.mode == INode_File ? "File" : "Dir");
|
||||||
|
printf("blocks_count = %u\n", inode.blocks_count);
|
||||||
|
printf("last_block_size = %u\n", inode.last_block_size);
|
||||||
|
printf("ndir_blocks = [");
|
||||||
|
for (size_t i = 0; i < inode.blocks_count && i < inode_ndir_blocks_count;
|
||||||
|
++i)
|
||||||
|
printf("%s%u", i > 0 ? ", " : "", inode.blocks[i]);
|
||||||
|
printf("]\n");
|
||||||
|
printf("ind_block = %u\n", inode.blocks[inode_ind_block_idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_block_hex(Fs *fs, uint16_t block_id)
|
||||||
|
{
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
constexpr size_t values_per_row = 16;
|
||||||
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
||||||
|
for (size_t j = 0; j < values_per_row; ++j)
|
||||||
|
printf("%s%4x", j > 0 ? " " : "", block[i + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_block_dec(Fs *fs, uint16_t block_id)
|
||||||
|
{
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
constexpr size_t values_per_row = 16;
|
||||||
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
||||||
|
for (size_t j = 0; j < values_per_row; ++j)
|
||||||
|
printf("%s%5u", j > 0 ? " " : "", block[i + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_dump_direntry_block(Fs *fs, uint16_t block_id)
|
||||||
|
{
|
||||||
|
Block block;
|
||||||
|
read_block(fs, block, block_id);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
DirEntry entry = { };
|
||||||
|
do {
|
||||||
|
memcpy(&entry, &block[entry.next], direentry_max_size);
|
||||||
|
|
||||||
|
printf("dir_entries[%d]\n", i);
|
||||||
|
printf(" inode = %u\n", entry.inode);
|
||||||
|
printf(" next = %u\n", entry.next);
|
||||||
|
printf(" name_len = %u\n", entry.name_len);
|
||||||
|
char name[inode_name_cap];
|
||||||
|
for (size_t i = 0; i < entry.name_len; ++i)
|
||||||
|
name[i] = entry.name[i];
|
||||||
|
printf(" name = \"%.*s\"\n", (int)entry.name_len, name);
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
} while (entry.next != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
(void)argc, (void)argv;
|
||||||
|
// assert(argc >= 2);
|
||||||
|
// const char *output_filename = argv[1];
|
||||||
|
|
||||||
|
constexpr size_t amount_of_blocks = 128;
|
||||||
|
|
||||||
|
Disk disk = {
|
||||||
|
.blocks = calloc(amount_of_blocks, sizeof(Block)),
|
||||||
|
.blocks_count = amount_of_blocks,
|
||||||
|
};
|
||||||
|
|
||||||
|
Fs fs_val;
|
||||||
|
Fs *fs = &fs_val;
|
||||||
|
|
||||||
|
fs_init(fs, &disk, 0, amount_of_blocks);
|
||||||
|
fs_sync(fs);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
char command[32];
|
||||||
|
printf("> ");
|
||||||
|
if (scanf(" %31s", command) != 1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (strcmp(command, "q") == 0) {
|
||||||
|
break;
|
||||||
|
} else if (strcmp(command, "h") == 0) {
|
||||||
|
printf("h help\n"
|
||||||
|
"fs print fs info\n"
|
||||||
|
"sb print superblock\n"
|
||||||
|
"bm print block bitmap\n"
|
||||||
|
"in i print inode[i]\n"
|
||||||
|
"bh i print block[i] as hex\n"
|
||||||
|
"bd i print block[i] as decimal\n"
|
||||||
|
"deb i print block[i] as dir entry\n"
|
||||||
|
"root print root inode id\n"
|
||||||
|
"mkdir i n make dir with name n in inode[i]\n"
|
||||||
|
"touch i n make file with name n in inode[i]\n"
|
||||||
|
"rm p i remove inode[i] from dir inode[p]\n"
|
||||||
|
"ls i list entries in inode[i]\n"
|
||||||
|
"w i write to inode[i]\n"
|
||||||
|
"r i read from inode[i]\n"
|
||||||
|
"");
|
||||||
|
} else if (strcmp(command, "fs") == 0) {
|
||||||
|
fs_dump_info(fs);
|
||||||
|
} else if (strcmp(command, "sb") == 0) {
|
||||||
|
fs_dump_superblock(fs);
|
||||||
|
} else if (strcmp(command, "bm") == 0) {
|
||||||
|
fs_dump_block_bitmap(fs);
|
||||||
|
} else if (strcmp(command, "in") == 0) {
|
||||||
|
uint16_t inode_id;
|
||||||
|
if (scanf(" %hu", &inode_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
fs_dump_inode(fs, inode_id);
|
||||||
|
} else if (strcmp(command, "bh") == 0) {
|
||||||
|
uint16_t block_id;
|
||||||
|
if (scanf(" %hu", &block_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
fs_dump_block_hex(fs, block_id);
|
||||||
|
} else if (strcmp(command, "bd") == 0) {
|
||||||
|
uint16_t block_id;
|
||||||
|
if (scanf(" %hu", &block_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
fs_dump_block_dec(fs, block_id);
|
||||||
|
} else if (strcmp(command, "deb") == 0) {
|
||||||
|
uint16_t block_id;
|
||||||
|
if (scanf(" %hu", &block_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
fs_dump_direntry_block(fs, block_id);
|
||||||
|
} else if (strcmp(command, "root") == 0) {
|
||||||
|
INode root;
|
||||||
|
fs_root(fs, &root);
|
||||||
|
printf("id = %u\n", root.id);
|
||||||
|
} else if (strcmp(command, "mkdir") == 0) {
|
||||||
|
uint16_t parent_id;
|
||||||
|
char name[62];
|
||||||
|
if (scanf(" %hu %61s", &parent_id, name) != 2)
|
||||||
|
goto malformed_command;
|
||||||
|
INode parent;
|
||||||
|
fs_inode(fs, &parent, parent_id);
|
||||||
|
INode inode;
|
||||||
|
fs_make_dir(fs, &inode, &parent, name);
|
||||||
|
fs_sync(fs);
|
||||||
|
printf("id = %u\n", inode.id);
|
||||||
|
} else if (strcmp(command, "touch") == 0) {
|
||||||
|
uint16_t parent_id;
|
||||||
|
char name[62];
|
||||||
|
if (scanf(" %hu %61s", &parent_id, name) != 2)
|
||||||
|
goto malformed_command;
|
||||||
|
INode parent;
|
||||||
|
fs_inode(fs, &parent, parent_id);
|
||||||
|
INode inode;
|
||||||
|
fs_make_file(fs, &inode, &parent, name);
|
||||||
|
fs_sync(fs);
|
||||||
|
printf("id = %u\n", inode.id);
|
||||||
|
} else if (strcmp(command, "rm") == 0) {
|
||||||
|
uint16_t parent_id;
|
||||||
|
uint16_t inode_id;
|
||||||
|
if (scanf(" %hu %hu", &parent_id, &inode_id) != 2)
|
||||||
|
goto malformed_command;
|
||||||
|
INode parent;
|
||||||
|
fs_inode(fs, &parent, parent_id);
|
||||||
|
INode inode;
|
||||||
|
fs_inode(fs, &inode, inode_id);
|
||||||
|
fs_remove(fs, &inode, &parent);
|
||||||
|
fs_sync(fs);
|
||||||
|
} else if (strcmp(command, "ls") == 0) {
|
||||||
|
uint16_t inode_id;
|
||||||
|
if (scanf(" %hu", &inode_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
INode inode;
|
||||||
|
fs_inode(fs, &inode, inode_id);
|
||||||
|
ListEntry entries[32];
|
||||||
|
size_t entries_size = fs_list(
|
||||||
|
fs, entries, sizeof(entries) / sizeof(entries[0]), &inode);
|
||||||
|
fs_sync(fs);
|
||||||
|
for (size_t i = 0; i < entries_size; ++i)
|
||||||
|
printf("%s\n", entries[i].name);
|
||||||
|
} else if (strcmp(command, "w") == 0) {
|
||||||
|
uint16_t inode_id;
|
||||||
|
if (scanf(" %hu", &inode_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
INode inode;
|
||||||
|
fs_inode(fs, &inode, inode_id);
|
||||||
|
uint16_t buffer[64] = { 0 };
|
||||||
|
size_t i = 0;
|
||||||
|
fgetc(stdin);
|
||||||
|
int ch;
|
||||||
|
while ((ch = fgetc(stdin)) != EOF)
|
||||||
|
buffer[i++] = ch;
|
||||||
|
clearerr(stdin);
|
||||||
|
fs_write(fs, &inode, buffer, i);
|
||||||
|
fs_sync(fs);
|
||||||
|
} else if (strcmp(command, "r") == 0) {
|
||||||
|
uint16_t inode_id;
|
||||||
|
if (scanf(" %hu", &inode_id) != 1)
|
||||||
|
goto malformed_command;
|
||||||
|
INode inode;
|
||||||
|
fs_inode(fs, &inode, inode_id);
|
||||||
|
uint16_t buffer[64] = { 0 };
|
||||||
|
size_t buffer_size = fs_read(fs, &inode, buffer, 64, 0);
|
||||||
|
for (size_t i = 0; i < buffer_size; ++i)
|
||||||
|
fputc(buffer[i], stdout);
|
||||||
|
} else {
|
||||||
|
printf("error: unknown command '%s'\n", command);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
malformed_command:
|
||||||
|
printf("error: malformed '%s' command\n", command);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs_destroy(fs);
|
||||||
|
free(disk.blocks);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user