work on fs2
This commit is contained in:
parent
6d58d0c64c
commit
e5ffb4ff3d
@ -48,6 +48,9 @@ target_link_libraries(fscli PRIVATE opts fs)
|
||||
add_executable(fstool fstool.c)
|
||||
target_link_libraries(fstool PRIVATE opts fs)
|
||||
|
||||
add_executable(fs2 fs2.c)
|
||||
target_link_libraries(fs2 PRIVATE opts)
|
||||
|
||||
add_executable(mkdisk mkdisk.c)
|
||||
target_link_libraries(mkdisk PRIVATE opts)
|
||||
|
||||
|
||||
21
src/fs.c
21
src/fs.c
@ -25,7 +25,7 @@ constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1;
|
||||
constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count;
|
||||
|
||||
typedef struct {
|
||||
uint16_t inode;
|
||||
uint16_t inode_id;
|
||||
uint16_t next;
|
||||
uint16_t name_len;
|
||||
uint16_t name[inode_name_cap];
|
||||
@ -35,7 +35,8 @@ static_assert(sizeof(DirEntry) / sizeof(uint16_t) == direentry_max_size);
|
||||
|
||||
static size_t direntry_size(const DirEntry *entry)
|
||||
{
|
||||
return (sizeof(entry->inode) + sizeof(entry->next) + sizeof(entry->name_len)
|
||||
return (sizeof(entry->inode_id) + sizeof(entry->next)
|
||||
+ sizeof(entry->name_len)
|
||||
+ sizeof(entry->name[0]) * entry->name_len)
|
||||
/ sizeof(uint16_t);
|
||||
}
|
||||
@ -230,7 +231,7 @@ static void make_dir_with_inode_ids(
|
||||
};
|
||||
|
||||
DirEntry self_entry = {
|
||||
.inode = inode_id,
|
||||
.inode_id = inode_id,
|
||||
.next = 0,
|
||||
.name_len = 1,
|
||||
.name = { '.' },
|
||||
@ -239,7 +240,7 @@ static void make_dir_with_inode_ids(
|
||||
self_entry.next = self_entry_size;
|
||||
|
||||
DirEntry parent_entry = {
|
||||
.inode = parent_inode_id,
|
||||
.inode_id = parent_inode_id,
|
||||
.next = 0,
|
||||
.name_len = 2,
|
||||
.name = { '.', '.' },
|
||||
@ -299,7 +300,7 @@ static void add_inode_to_dir(
|
||||
assert(dir_inode->mode == INode_Dir && name_len <= inode_name_cap);
|
||||
|
||||
DirEntry entry = {
|
||||
.inode = inode->id,
|
||||
.inode_id = inode->id,
|
||||
.next = 0,
|
||||
.name_len = name_len,
|
||||
.name = { },
|
||||
@ -464,7 +465,7 @@ static void remove_direntry_fn(uint16_t block_id, void *arg)
|
||||
uint16_t curr = 0;
|
||||
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
||||
|
||||
if (!curr && !entry.next && entry.inode == args->inode_id) {
|
||||
if (!curr && !entry.next && entry.inode_id == args->inode_id) {
|
||||
args->out_empty_block_id = block_id;
|
||||
args->matched = true;
|
||||
return;
|
||||
@ -472,13 +473,13 @@ static void remove_direntry_fn(uint16_t block_id, void *arg)
|
||||
|
||||
int16_t prev = 0;
|
||||
|
||||
while (entry.next && entry.inode != args->inode_id) {
|
||||
while (entry.next && entry.inode_id != 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)
|
||||
if (!entry.next && entry.inode_id != args->inode_id)
|
||||
return;
|
||||
|
||||
if (!entry.next) {
|
||||
@ -536,7 +537,7 @@ static void list_block_entries_fn(uint16_t block_id, void *arg)
|
||||
MIN(direentry_max_size, block_size - entry.next));
|
||||
|
||||
FsListEntry list_entry = {
|
||||
.inode = entry.inode,
|
||||
.inode = entry.inode_id,
|
||||
.name = { },
|
||||
};
|
||||
for (size_t i = 0; i < entry.name_len; ++i)
|
||||
@ -725,7 +726,7 @@ void fs_dump_direntry_block(Fs *fs, uint16_t block_id)
|
||||
memcpy(&entry, &block[entry.next], direentry_max_size);
|
||||
|
||||
printf("dir_entries[%d]\n", i);
|
||||
printf(" inode = %u\n", entry.inode);
|
||||
printf(" inode = %u\n", entry.inode_id);
|
||||
printf(" next = %u\n", entry.next);
|
||||
printf(" name_len = %u\n", entry.name_len);
|
||||
char name[inode_name_cap];
|
||||
|
||||
877
src/fs2.c
Normal file
877
src/fs2.c
Normal file
@ -0,0 +1,877 @@
|
||||
#include "fs2.h"
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void bitmap_set(word map[], word bit)
|
||||
{
|
||||
map[bit / 16] |= 1 << bit % 16;
|
||||
}
|
||||
|
||||
static void bitmap_unset(word map[], word bit)
|
||||
{
|
||||
map[bit / 16] &= ~(1 << bit % 16);
|
||||
}
|
||||
|
||||
static bool bitmap_is_set(const word map[], word bit)
|
||||
{
|
||||
return map[bit / 16] >> bit % 16 & 1;
|
||||
}
|
||||
|
||||
static word word_strlen(const word *str)
|
||||
{
|
||||
word len = 0;
|
||||
for (; *str; ++str, ++len)
|
||||
;
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool word_streq(const word *a, const word *b)
|
||||
{
|
||||
for (; *a && *b; ++a, ++b)
|
||||
if (*a != *b)
|
||||
return false;
|
||||
return *a == *b;
|
||||
}
|
||||
|
||||
static word word_min(word a, word b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
static bool name_ch_is_valid(word ch)
|
||||
{
|
||||
if (ch >= 'a' && ch <= 'z')
|
||||
return true;
|
||||
if (ch >= 'A' && ch <= 'Z')
|
||||
return true;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
return true;
|
||||
return ch == '_' || ch == '-';
|
||||
}
|
||||
|
||||
static bool name_is_valid(const word *name)
|
||||
{
|
||||
if (!name[0])
|
||||
return false;
|
||||
for (; *name; ++name)
|
||||
if (!name_ch_is_valid(*name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t direntry_size(const DirEntry *entry)
|
||||
{
|
||||
return (sizeof(entry->inode_id) + sizeof(entry->next)
|
||||
+ sizeof(entry->name_len)
|
||||
+ sizeof(entry->name[0]) * entry->name_len)
|
||||
/ sizeof(uint16_t);
|
||||
}
|
||||
|
||||
static word allocate_block(Fs *fs)
|
||||
{
|
||||
word block_id = 2;
|
||||
for (; block_id < fs_blocks_cap; ++block_id)
|
||||
if (!bitmap_is_set(fs->block_bitmap, block_id))
|
||||
break;
|
||||
|
||||
assert(block_id < fs_blocks_cap);
|
||||
|
||||
bitmap_set(fs->block_bitmap, block_id);
|
||||
fs->out_of_sync = true;
|
||||
|
||||
return block_id;
|
||||
}
|
||||
|
||||
static void deallocate_block(Fs *fs, word block_id)
|
||||
{
|
||||
bitmap_unset(fs->block_bitmap, block_id);
|
||||
fs->out_of_sync = true;
|
||||
}
|
||||
|
||||
static word allocate_inode(Fs *fs)
|
||||
{
|
||||
Superblock *sb = fs->sb;
|
||||
const size_t ids_in_allocated_blocks
|
||||
= sb->inodeblocks_count * fs_inodes_per_block;
|
||||
|
||||
word inode_id = 0;
|
||||
for (; inode_id < ids_in_allocated_blocks; ++inode_id)
|
||||
if (!bitmap_is_set(sb->inodebitmap, inode_id))
|
||||
break;
|
||||
|
||||
if (inode_id >= ids_in_allocated_blocks) {
|
||||
assert(sb->inodeblocks_count < fs_inodeblocks_cap);
|
||||
word block_id = allocate_block(fs);
|
||||
|
||||
inode_id = sb->inodeblocks_count * fs_inodes_per_block;
|
||||
|
||||
sb->inodeblocks[sb->inodeblocks_count] = block_id;
|
||||
sb->inodeblocks_count += 1;
|
||||
}
|
||||
|
||||
bitmap_set(sb->inodebitmap, inode_id);
|
||||
fs->out_of_sync = true;
|
||||
|
||||
return inode_id;
|
||||
}
|
||||
|
||||
static void deallocate_inode(Fs *fs, word inode_id)
|
||||
{
|
||||
bitmap_unset(fs->sb->inodebitmap, inode_id);
|
||||
fs->out_of_sync = true;
|
||||
}
|
||||
|
||||
static void write_block(Fs *fs, const word *block, word id)
|
||||
{
|
||||
fs->disk->write_block(fs->disk, block, id + fs->block_offset);
|
||||
}
|
||||
|
||||
static void read_block(Fs *fs, word *block, word id)
|
||||
{
|
||||
fs->disk->read_block(fs->disk, block, id + fs->block_offset);
|
||||
}
|
||||
|
||||
static void load_ind_block(Fs *fs, word block_id)
|
||||
{
|
||||
if (fs->current_ind_block_id == block_id)
|
||||
return;
|
||||
|
||||
read_block(fs, fs->ind_block, block_id);
|
||||
fs->current_ind_block_id = block_id;
|
||||
}
|
||||
static void load_direntry_block(Fs *fs, word block_id)
|
||||
{
|
||||
if (fs->current_direntry_block_id == block_id)
|
||||
return;
|
||||
|
||||
read_block(fs, fs->direntry_block, block_id);
|
||||
fs->current_direntry_block_id = block_id;
|
||||
}
|
||||
|
||||
static void load_inode_block(Fs *fs, word block_id)
|
||||
{
|
||||
if (fs->current_inode_block_id == block_id)
|
||||
return;
|
||||
|
||||
read_block(fs, fs->inode_block, block_id);
|
||||
fs->current_inode_block_id = block_id;
|
||||
}
|
||||
|
||||
static void write_inode(Fs *fs, const INode *inode)
|
||||
{
|
||||
word inode_block_idx = inode->id / fs_inodes_per_block;
|
||||
word block_offset = inode->id % fs_inodes_per_block * fs_inode_size;
|
||||
assert(inode_block_idx < fs_blocks_cap && block_offset < fs_block_size);
|
||||
|
||||
word block_id = fs->sb->inodeblocks[inode_block_idx];
|
||||
|
||||
load_inode_block(fs, block_id);
|
||||
memcpy(&fs->inode_block[block_offset], inode, sizeof(INode));
|
||||
write_block(fs, fs->inode_block, block_id);
|
||||
}
|
||||
|
||||
static void read_inode(Fs *fs, INode *out_inode, word inode_id)
|
||||
{
|
||||
word inode_block_idx = inode_id / fs_inodes_per_block;
|
||||
word block_offset = inode_id % fs_inodes_per_block * fs_inode_size;
|
||||
assert(inode_block_idx < fs_blocks_cap && block_offset < fs_block_size);
|
||||
|
||||
word block_id = fs->sb->inodeblocks[inode_block_idx];
|
||||
|
||||
load_inode_block(fs, block_id);
|
||||
memcpy(out_inode, &fs->inode_block[block_offset], sizeof(INode));
|
||||
}
|
||||
|
||||
static uint16_t inode_allocate_block(Fs *fs, INode *inode)
|
||||
{
|
||||
assert(inode->blocks_count < fs_inode_blocks_cap);
|
||||
|
||||
uint16_t block_id = allocate_block(fs);
|
||||
|
||||
if (inode->blocks_count < fs_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[fs_inode_ind_block_idx];
|
||||
load_ind_block(fs, ind_block_id);
|
||||
fs->ind_block[inode->blocks_count - fs_inode_ndir_blocks_count] = block_id;
|
||||
write_block(fs, fs->ind_block, ind_block_id);
|
||||
return block_id;
|
||||
}
|
||||
|
||||
static bool inode_has_ind_block(const INode *inode)
|
||||
{
|
||||
return inode->blocks_count > fs_inode_ndir_blocks_count;
|
||||
}
|
||||
|
||||
static word *inode_block_at(INode *inode, word idx, word *ind_block)
|
||||
{
|
||||
assert(idx < inode->blocks_count);
|
||||
|
||||
if (idx < fs_inode_ndir_blocks_count)
|
||||
return &inode->blocks[idx];
|
||||
|
||||
assert(idx < fs_inode_ndir_blocks_count + fs_block_size);
|
||||
return &ind_block[idx - fs_inode_ndir_blocks_count];
|
||||
}
|
||||
|
||||
static bool dirinode_entry_exists(Fs *fs, INode *dirinode, const word *name)
|
||||
{
|
||||
bool exists = false;
|
||||
DirCursor cursor;
|
||||
fs_opendir(fs, &cursor, dirinode);
|
||||
DirEntry *entry;
|
||||
while ((entry = fs_readdir(fs, &cursor))) {
|
||||
if (word_streq(entry->name, name)) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fs_closedir(fs, &cursor);
|
||||
return exists;
|
||||
}
|
||||
|
||||
static void make_dir_inode(
|
||||
Fs *fs, INode *out_inode, word inode_id, word parent_inode_id)
|
||||
{
|
||||
*out_inode = (INode) {
|
||||
.id = inode_id,
|
||||
.mode = INode_Dir,
|
||||
.blocks_count = 0,
|
||||
.last_block_size = 0,
|
||||
.blocks = { },
|
||||
};
|
||||
|
||||
DirEntry self_entry = {
|
||||
.inode_id = inode_id,
|
||||
.next = 0,
|
||||
.name_len = 1,
|
||||
.name = { '.' },
|
||||
};
|
||||
size_t self_entry_size = direntry_size(&self_entry);
|
||||
self_entry.next = self_entry_size;
|
||||
|
||||
DirEntry parent_entry = {
|
||||
.inode_id = parent_inode_id,
|
||||
.next = 0,
|
||||
.name_len = 2,
|
||||
.name = { '.', '.' },
|
||||
};
|
||||
|
||||
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
||||
memcpy(fs->direntry_block, &self_entry, self_entry_size * sizeof(word));
|
||||
memcpy(&fs->direntry_block[self_entry_size],
|
||||
&parent_entry,
|
||||
direntry_size(&parent_entry) * sizeof(word));
|
||||
write_block(fs, fs->direntry_block, entry_block_id);
|
||||
out_inode->last_block_size = self_entry_size + direntry_size(&parent_entry);
|
||||
|
||||
write_inode(fs, out_inode);
|
||||
}
|
||||
|
||||
static void dirinode_append_direntry(
|
||||
Fs *fs, INode *inode, const DirEntry *direntry)
|
||||
{
|
||||
if (inode_has_ind_block(inode))
|
||||
load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]);
|
||||
|
||||
uint16_t block_id
|
||||
= *inode_block_at(inode, inode->blocks_count - 1, fs->ind_block);
|
||||
load_direntry_block(fs, block_id);
|
||||
|
||||
DirEntry entry = { };
|
||||
word offset = 0;
|
||||
memcpy(&entry,
|
||||
&fs->direntry_block[offset],
|
||||
fs_direntry_max_size * sizeof(word));
|
||||
|
||||
while (entry.next) {
|
||||
offset = entry.next;
|
||||
memcpy(&entry,
|
||||
&fs->direntry_block[offset],
|
||||
fs_direntry_max_size * sizeof(word));
|
||||
}
|
||||
|
||||
if (offset + direntry_size(direntry) <= fs_block_size) {
|
||||
uint16_t new_offset = offset + direntry_size(&entry);
|
||||
entry.next = new_offset;
|
||||
|
||||
memcpy(&fs->direntry_block[offset],
|
||||
&entry,
|
||||
direntry_size(&entry) * sizeof(uint16_t));
|
||||
memcpy(&fs->direntry_block[new_offset],
|
||||
direntry,
|
||||
direntry_size(direntry) * sizeof(uint16_t));
|
||||
inode->last_block_size = new_offset + direntry_size(direntry);
|
||||
write_block(fs, fs->direntry_block, block_id);
|
||||
return;
|
||||
}
|
||||
|
||||
word new_block_id = inode_allocate_block(fs, inode);
|
||||
load_direntry_block(fs, new_block_id);
|
||||
memcpy(fs->direntry_block,
|
||||
direntry,
|
||||
direntry_size(direntry) * sizeof(uint16_t));
|
||||
inode->last_block_size = direntry_size(direntry);
|
||||
write_block(fs, fs->direntry_block, new_block_id);
|
||||
}
|
||||
|
||||
static void add_inode_to_dir(
|
||||
Fs *fs, INode *dir_inode, const INode *inode, const word *name)
|
||||
{
|
||||
word name_len = word_strlen(name);
|
||||
assert(dir_inode->mode == INode_Dir && name_len <= fs_name_max_len);
|
||||
|
||||
DirEntry entry = {
|
||||
.inode_id = inode->id,
|
||||
.next = 0,
|
||||
.name_len = name_len,
|
||||
.name = { },
|
||||
};
|
||||
memcpy(entry.name, name, name_len * sizeof(word));
|
||||
|
||||
dirinode_append_direntry(fs, dir_inode, &entry);
|
||||
write_inode(fs, dir_inode);
|
||||
}
|
||||
|
||||
static void read_direntry_at_offset(
|
||||
DirEntry *out_entry, const word *block, word offset)
|
||||
{
|
||||
word read_size = word_min(fs_direntry_max_size, fs_block_size - offset);
|
||||
memcpy(out_entry, &block[offset], read_size * sizeof(word));
|
||||
}
|
||||
|
||||
static void remove_inode_from_dir(Fs *fs, INode *dir_inode, word inode_id)
|
||||
{
|
||||
if (inode_has_ind_block(dir_inode))
|
||||
load_ind_block(fs, dir_inode->blocks[fs_inode_ind_block_idx]);
|
||||
|
||||
word block_idx = 0;
|
||||
word block_id;
|
||||
word offset;
|
||||
word next_offset;
|
||||
word last_size;
|
||||
|
||||
for (; block_idx < dir_inode->blocks_count; ++block_idx) {
|
||||
|
||||
block_id = *inode_block_at(dir_inode, block_idx, fs->ind_block);
|
||||
load_direntry_block(fs, block_id);
|
||||
|
||||
offset = 0;
|
||||
|
||||
do {
|
||||
|
||||
DirEntry entry = { };
|
||||
read_direntry_at_offset(&entry, fs->direntry_block, offset);
|
||||
next_offset = entry.next;
|
||||
last_size = direntry_size(&entry);
|
||||
|
||||
if (entry.inode_id == inode_id)
|
||||
goto entry_found;
|
||||
|
||||
offset = next_offset;
|
||||
} while (offset);
|
||||
}
|
||||
|
||||
assert(false);
|
||||
entry_found:
|
||||
|
||||
word prev_next_offset = offset;
|
||||
|
||||
while (next_offset) {
|
||||
DirEntry entry = { };
|
||||
read_direntry_at_offset(&entry, fs->direntry_block, next_offset);
|
||||
|
||||
word next_next_offset = entry.next;
|
||||
entry.next = next_offset;
|
||||
|
||||
memcpy(&fs->direntry_block[prev_next_offset],
|
||||
&entry,
|
||||
direntry_size(&entry) * sizeof(word));
|
||||
last_size = direntry_size(&entry);
|
||||
|
||||
prev_next_offset = next_offset;
|
||||
next_offset = next_next_offset;
|
||||
}
|
||||
|
||||
if (block_idx == dir_inode->blocks_count - 1)
|
||||
dir_inode->last_block_size = prev_next_offset + last_size;
|
||||
|
||||
if (!offset && !next_offset) {
|
||||
if (block_idx == dir_inode->blocks_count - 1) {
|
||||
word offset = 0;
|
||||
word prev_offset = 0;
|
||||
word last_size = 0;
|
||||
DirEntry *entry;
|
||||
do {
|
||||
entry = (DirEntry *)&fs->direntry_block[offset];
|
||||
last_size = direntry_size(entry);
|
||||
prev_offset = offset;
|
||||
offset = entry->next;
|
||||
} while (offset);
|
||||
dir_inode->last_block_size = prev_offset + last_size;
|
||||
}
|
||||
|
||||
for (word i = block_id; i < dir_inode->blocks_count - 1; ++i) {
|
||||
word id = *inode_block_at(dir_inode, i + 1, fs->ind_block);
|
||||
*inode_block_at(dir_inode, i, fs->ind_block) = id;
|
||||
}
|
||||
*inode_block_at(dir_inode, dir_inode->blocks_count - 1, fs->ind_block)
|
||||
= 0;
|
||||
dir_inode->blocks_count -= 1;
|
||||
|
||||
if (dir_inode->blocks_count == fs_inode_ndir_blocks_count) {
|
||||
deallocate_block(fs, dir_inode->blocks[fs_inode_ind_block_idx]);
|
||||
dir_inode->blocks[fs_inode_ind_block_idx] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void init_fs(Fs *fs, Disk *disk, word block_offset)
|
||||
{
|
||||
*fs = (Fs) {
|
||||
.disk = disk,
|
||||
.block_offset = block_offset,
|
||||
.sb_block = calloc(fs_block_size, sizeof(word)),
|
||||
.block_bitmap = calloc(fs_block_size, sizeof(word)),
|
||||
.ind_block = calloc(fs_block_size, sizeof(word)),
|
||||
.direntry_block = calloc(fs_block_size, sizeof(word)),
|
||||
.inode_block = calloc(fs_block_size, sizeof(word)),
|
||||
.sb = nullptr,
|
||||
.current_ind_block_id = 0,
|
||||
.current_direntry_block_id = 0,
|
||||
.current_inode_block_id = 0,
|
||||
.out_of_sync = true,
|
||||
};
|
||||
fs->sb = (Superblock *)fs->sb_block;
|
||||
}
|
||||
|
||||
void fs_mount(Fs *fs, Disk *disk, word block_offset)
|
||||
{
|
||||
init_fs(fs, disk, block_offset);
|
||||
|
||||
read_block(fs, fs->sb_block, 0);
|
||||
read_block(fs, fs->block_bitmap, 1);
|
||||
|
||||
assert(fs->sb->magic == fs_superblock_magic);
|
||||
}
|
||||
|
||||
void fs_umount(Fs *fs)
|
||||
{
|
||||
fs_sync(fs);
|
||||
|
||||
free(fs->sb_block);
|
||||
free(fs->block_bitmap);
|
||||
free(fs->ind_block);
|
||||
free(fs->direntry_block);
|
||||
free(fs->inode_block);
|
||||
}
|
||||
|
||||
void fs_sync(Fs *fs)
|
||||
{
|
||||
if (!fs->out_of_sync)
|
||||
return;
|
||||
write_block(fs, fs->sb_block, 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, word inode_id)
|
||||
{
|
||||
read_inode(fs, out_inode, inode_id);
|
||||
}
|
||||
|
||||
void fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode)
|
||||
{
|
||||
(void)fs;
|
||||
*out_cursor = (DirCursor) {
|
||||
.inode = inode,
|
||||
.direntry = { },
|
||||
.current_block_idx = 0,
|
||||
.current_offset = 0,
|
||||
};
|
||||
}
|
||||
|
||||
void fs_closedir(Fs *fs, DirCursor *cursor)
|
||||
{
|
||||
(void)fs;
|
||||
(void)cursor;
|
||||
}
|
||||
|
||||
DirEntry *fs_readdir(Fs *fs, DirCursor *cursor)
|
||||
{
|
||||
INode *inode = cursor->inode;
|
||||
DirEntry *entry = &cursor->direntry;
|
||||
|
||||
if (cursor->current_block_idx >= cursor->inode->blocks_count)
|
||||
return nullptr;
|
||||
|
||||
if (inode_has_ind_block(inode))
|
||||
load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]);
|
||||
|
||||
word block_id
|
||||
= *inode_block_at(inode, cursor->current_block_idx, fs->ind_block);
|
||||
|
||||
load_direntry_block(fs, block_id);
|
||||
|
||||
word remaining_size = fs_block_size - cursor->current_offset;
|
||||
word read_size = fs_direntry_max_size;
|
||||
if (read_size > remaining_size)
|
||||
read_size = remaining_size;
|
||||
|
||||
memcpy(entry,
|
||||
&fs->direntry_block[cursor->current_offset],
|
||||
read_size * sizeof(word));
|
||||
|
||||
cursor->current_offset = entry->next;
|
||||
if (!cursor->current_offset)
|
||||
cursor->current_block_idx += 1;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
word fs_mkdir(Fs *fs, INode *out_inode, INode *parent, const word *name)
|
||||
{
|
||||
if (!name_is_valid(name))
|
||||
return -1; // Invalid name.
|
||||
|
||||
bool exists = dirinode_entry_exists(fs, parent, name);
|
||||
if (exists)
|
||||
return -2; // Entry exists.
|
||||
|
||||
word inode_id = allocate_inode(fs);
|
||||
make_dir_inode(fs, out_inode, inode_id, parent->id);
|
||||
write_inode(fs, out_inode);
|
||||
add_inode_to_dir(fs, parent, out_inode, name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
word fs_rmdir(Fs *fs, INode *inode)
|
||||
{
|
||||
if (inode->mode != INode_Dir)
|
||||
return -1; // Not a directory.
|
||||
|
||||
if (inode->blocks_count != 1)
|
||||
return -2; // Not empty.
|
||||
|
||||
DirCursor cursor;
|
||||
fs_opendir(fs, &cursor, inode);
|
||||
DirEntry *entry;
|
||||
entry = fs_readdir(fs, &cursor);
|
||||
assert(entry);
|
||||
entry = fs_readdir(fs, &cursor);
|
||||
assert(entry);
|
||||
word parent_id = entry->inode_id;
|
||||
|
||||
entry = fs_readdir(fs, &cursor);
|
||||
bool is_empty = entry == nullptr;
|
||||
fs_closedir(fs, &cursor);
|
||||
|
||||
if (!is_empty)
|
||||
return -2; // Not empty;
|
||||
|
||||
// We have cleaned up correctly.
|
||||
assert(inode->blocks_count == 1 && inode->last_block_size == 9);
|
||||
|
||||
if (parent_id == inode->id)
|
||||
return -3; // Root.
|
||||
|
||||
INode parent;
|
||||
fs_inode(fs, &parent, parent_id);
|
||||
|
||||
remove_inode_from_dir(fs, &parent, inode->id);
|
||||
|
||||
// deallocate_inode(fs, inode->id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fs_mkfs(Fs *fs, Disk *disk, word block_offset)
|
||||
{
|
||||
init_fs(fs, disk, block_offset);
|
||||
fs->sb->magic = fs_superblock_magic;
|
||||
|
||||
bitmap_set(fs->block_bitmap, 0);
|
||||
bitmap_set(fs->block_bitmap, 1);
|
||||
|
||||
word root_inode_id = allocate_inode(fs);
|
||||
assert(root_inode_id == 0);
|
||||
|
||||
INode root_inode = { };
|
||||
make_dir_inode(fs, &root_inode, root_inode_id, root_inode_id);
|
||||
|
||||
fs_umount(fs);
|
||||
}
|
||||
|
||||
static int resolve_path(Fs *fs, INode *inode, const word *path)
|
||||
{
|
||||
if (!*path)
|
||||
return 0;
|
||||
if (*path == '/') {
|
||||
fs_root(fs, inode);
|
||||
return resolve_path(fs, inode, path + 1);
|
||||
}
|
||||
|
||||
const word *name = path;
|
||||
for (; *path && *path != '/'; ++path)
|
||||
;
|
||||
for (; *path == '/'; ++path)
|
||||
;
|
||||
|
||||
DirCursor cursor;
|
||||
fs_opendir(fs, &cursor, inode);
|
||||
DirEntry *entry;
|
||||
while ((entry = fs_readdir(fs, &cursor)))
|
||||
if (word_streq(entry->name, name))
|
||||
break;
|
||||
if (!entry)
|
||||
return -1; // Not found.
|
||||
fs_inode(fs, inode, entry->inode_id);
|
||||
fs_closedir(fs, &cursor);
|
||||
return resolve_path(fs, inode, path);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
Fs *fs;
|
||||
INode *cwd;
|
||||
char **argv;
|
||||
int argc;
|
||||
} Shell;
|
||||
|
||||
static void do_ls(Shell *s)
|
||||
{
|
||||
INode inode = *s->cwd;
|
||||
if (s->argc >= 2) {
|
||||
word *path = calloc(strlen(s->argv[1]) + 1, sizeof(word));
|
||||
for (size_t i = 0; s->argv[1][i]; ++i)
|
||||
path[i] = s->argv[1][i];
|
||||
if (resolve_path(s->fs, &inode, path) != 0) {
|
||||
printf("error occured\n");
|
||||
return;
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
|
||||
DirCursor cursor;
|
||||
fs_opendir(s->fs, &cursor, &inode);
|
||||
|
||||
DirEntry *entry;
|
||||
while ((entry = fs_readdir(s->fs, &cursor))) {
|
||||
char name[64];
|
||||
size_t i = 0;
|
||||
for (; i < entry->name_len; ++i)
|
||||
name[i] = entry->name[i];
|
||||
printf("%s\n", name);
|
||||
}
|
||||
|
||||
fs_closedir(s->fs, &cursor);
|
||||
}
|
||||
|
||||
static void do_mkdir(Shell *s)
|
||||
{
|
||||
assert(s->argc >= 2);
|
||||
const char *byte_name = s->argv[1];
|
||||
word name[fs_name_max_len] = { };
|
||||
for (size_t i = 0; byte_name[i]; ++i)
|
||||
name[i] = byte_name[i];
|
||||
|
||||
INode inode = { };
|
||||
word result = fs_mkdir(s->fs, &inode, s->cwd, name);
|
||||
if (result != 0)
|
||||
printf("error occured\n");
|
||||
}
|
||||
|
||||
static void do_rmdir(Shell *s)
|
||||
{
|
||||
if (s->argc < 2) {
|
||||
printf("expected path\n");
|
||||
return;
|
||||
}
|
||||
|
||||
INode inode = *s->cwd;
|
||||
word *path = calloc(strlen(s->argv[1]) + 1, sizeof(word));
|
||||
for (size_t i = 0; s->argv[1][i]; ++i)
|
||||
path[i] = s->argv[1][i];
|
||||
if (resolve_path(s->fs, &inode, path) != 0) {
|
||||
printf("error occured\n");
|
||||
return;
|
||||
}
|
||||
free(path);
|
||||
|
||||
word result = fs_rmdir(s->fs, &inode);
|
||||
if (result != 0)
|
||||
printf("error occured\n");
|
||||
}
|
||||
|
||||
static void run_shell(Fs *fs)
|
||||
{
|
||||
INode cwd = { };
|
||||
fs_root(fs, &cwd);
|
||||
|
||||
while (true) {
|
||||
printf("> ");
|
||||
|
||||
char line[64];
|
||||
fgets(line, 64, stdin);
|
||||
size_t line_len = strlen(line);
|
||||
assert(line_len);
|
||||
line[line_len - 1] = '\0';
|
||||
line_len -= 1;
|
||||
|
||||
if (!line_len)
|
||||
continue;
|
||||
|
||||
int argc = 1;
|
||||
char *argv[10];
|
||||
argv[0] = strtok(line, " ");
|
||||
while ((argv[argc] = strtok(nullptr, " ")))
|
||||
argc += 1;
|
||||
|
||||
Shell shell = { fs, &cwd, argv, argc };
|
||||
|
||||
char *cmd = argv[0];
|
||||
size_t cmd_len = strlen(cmd);
|
||||
|
||||
if (strcmp(cmd, "exit") == 0 || strncmp(cmd, "quit", cmd_len) == 0) {
|
||||
break;
|
||||
} else if (strcmp(cmd, "ls") == 0) {
|
||||
do_ls(&shell);
|
||||
} else if (strcmp(cmd, "mkdir") == 0) {
|
||||
do_mkdir(&shell);
|
||||
} else if (strcmp(cmd, "rmdir") == 0) {
|
||||
do_rmdir(&shell);
|
||||
} else {
|
||||
printf("unknown command '%s'\n", cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
Disk disk;
|
||||
FILE *file;
|
||||
size_t max_blocks_count;
|
||||
} FileDisk;
|
||||
|
||||
static int file_disk_read_block(Disk *disk, word *buffer, word block_id)
|
||||
{
|
||||
FileDisk *d = (FileDisk *)disk;
|
||||
assert(block_id < d->max_blocks_count);
|
||||
fseek(d->file, block_id * fs_block_size * sizeof(word), SEEK_SET);
|
||||
size_t words_read = fread(buffer, sizeof(word), fs_block_size, d->file);
|
||||
assert(words_read == fs_block_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int file_disk_write_block(Disk *disk, const word *buffer, word block_id)
|
||||
{
|
||||
FileDisk *d = (FileDisk *)disk;
|
||||
assert(block_id < d->max_blocks_count);
|
||||
fseek(d->file, block_id * fs_block_size * sizeof(word), SEEK_SET);
|
||||
size_t words_written = fwrite(buffer, sizeof(word), fs_block_size, d->file);
|
||||
assert(words_written == fs_block_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
Disk disk;
|
||||
word *blocks;
|
||||
size_t blocks_count;
|
||||
} MemDisk;
|
||||
|
||||
static int mem_disk_read_block(Disk *disk, word *buffer, word block_id)
|
||||
{
|
||||
MemDisk *d = (MemDisk *)disk;
|
||||
assert(block_id < d->blocks_count);
|
||||
memcpy(buffer,
|
||||
&d->blocks[fs_block_size * block_id],
|
||||
fs_block_size * sizeof(word));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mem_disk_write_block(Disk *disk, const word *buffer, word block_id)
|
||||
{
|
||||
MemDisk *d = (MemDisk *)disk;
|
||||
assert(block_id < d->blocks_count);
|
||||
memcpy(&d->blocks[fs_block_size * block_id],
|
||||
buffer,
|
||||
fs_block_size * sizeof(word));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const char *disk_filename = nullptr;
|
||||
bool init = false;
|
||||
word offset = 0;
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "-d") == 0) {
|
||||
assert(argc >= i + 1);
|
||||
i += 1;
|
||||
disk_filename = argv[i];
|
||||
} else if (strcmp(argv[i], "--init") == 0) {
|
||||
init = true;
|
||||
} else if (strcmp(argv[i], "--offset") == 0) {
|
||||
assert(argc >= i + 1);
|
||||
i += 1;
|
||||
offset = atoi(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
union {
|
||||
FileDisk file_disk;
|
||||
MemDisk mem_disk;
|
||||
} disk_impl;
|
||||
|
||||
Disk *disk = (Disk *)&disk_impl;
|
||||
Fs fs;
|
||||
|
||||
if (disk_filename) {
|
||||
FILE *file = fopen(disk_filename, init ? "w+" : "r+");
|
||||
if (!file) {
|
||||
perror("fopen() failed");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
disk_impl.file_disk = (FileDisk) {
|
||||
.disk = { file_disk_read_block, file_disk_write_block },
|
||||
.file = file,
|
||||
.max_blocks_count = 128,
|
||||
};
|
||||
|
||||
if (init)
|
||||
fs_mkfs(&fs, disk, offset);
|
||||
} else {
|
||||
size_t blocks_count = 128;
|
||||
disk_impl.mem_disk = (MemDisk) {
|
||||
.disk = { mem_disk_read_block, mem_disk_write_block },
|
||||
.blocks = calloc(blocks_count, sizeof(word) * fs_block_size),
|
||||
.blocks_count = blocks_count,
|
||||
};
|
||||
|
||||
fs_mkfs(&fs, disk, offset);
|
||||
}
|
||||
|
||||
fs_mount(&fs, disk, offset);
|
||||
|
||||
run_shell(&fs);
|
||||
|
||||
fs_umount(&fs);
|
||||
|
||||
if (disk_filename) {
|
||||
fclose(disk_impl.file_disk.file);
|
||||
} else {
|
||||
free(disk_impl.mem_disk.blocks);
|
||||
}
|
||||
}
|
||||
104
src/fs2.h
Normal file
104
src/fs2.h
Normal file
@ -0,0 +1,104 @@
|
||||
#ifndef FS2_H
|
||||
#define FS2_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
constexpr size_t fs_block_size = 512;
|
||||
|
||||
constexpr size_t fs_blocks_cap = fs_block_size * 16;
|
||||
|
||||
constexpr size_t fs_inode_size = 16;
|
||||
constexpr size_t fs_inode_blocks_cap = 12;
|
||||
|
||||
constexpr size_t fs_inodes_per_block = fs_block_size / fs_inode_size;
|
||||
constexpr size_t fs_inodeblocks_cap = 32;
|
||||
constexpr size_t fs_inodes_cap = fs_inodeblocks_cap * fs_inodes_per_block;
|
||||
constexpr size_t fs_inodebitmap_size = fs_inodes_cap / 16;
|
||||
|
||||
constexpr size_t fs_direntry_max_size = 64;
|
||||
constexpr size_t fs_name_max_len = 61;
|
||||
|
||||
constexpr size_t fs_inode_ndir_blocks_count = fs_inode_blocks_cap - 1;
|
||||
constexpr size_t fs_inode_ind_block_idx = fs_inode_ndir_blocks_count;
|
||||
|
||||
constexpr uint16_t fs_superblock_magic = 0x5a5a;
|
||||
|
||||
typedef uint16_t word;
|
||||
|
||||
enum : word {
|
||||
INode_File,
|
||||
INode_Dir,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
word id;
|
||||
word mode;
|
||||
word blocks_count;
|
||||
word last_block_size;
|
||||
word blocks[fs_inode_blocks_cap];
|
||||
} INode;
|
||||
|
||||
static_assert(sizeof(INode) == sizeof(word) * fs_inode_size);
|
||||
|
||||
typedef struct {
|
||||
word inode_id;
|
||||
word next;
|
||||
word name_len;
|
||||
word name[fs_name_max_len];
|
||||
} DirEntry;
|
||||
|
||||
static_assert(sizeof(DirEntry) == sizeof(word) * fs_direntry_max_size);
|
||||
|
||||
typedef struct {
|
||||
word magic;
|
||||
word blocks_count;
|
||||
word inodeblocks_count;
|
||||
word inodeblocks[fs_inodeblocks_cap];
|
||||
word inodebitmap[fs_inodebitmap_size];
|
||||
} Superblock;
|
||||
|
||||
typedef struct {
|
||||
INode *inode;
|
||||
DirEntry direntry;
|
||||
word current_block_idx;
|
||||
word current_offset;
|
||||
} DirCursor;
|
||||
|
||||
typedef struct Disk Disk;
|
||||
|
||||
struct Disk {
|
||||
int (*read_block)(Disk *disk, word *buffer, word block_id);
|
||||
int (*write_block)(Disk *disk, const word *buffer, word block_id);
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
Disk *disk;
|
||||
word block_offset;
|
||||
word *sb_block;
|
||||
word *block_bitmap;
|
||||
word *ind_block;
|
||||
word *direntry_block;
|
||||
word *inode_block;
|
||||
Superblock *sb;
|
||||
word current_ind_block_id;
|
||||
word current_direntry_block_id;
|
||||
word current_inode_block_id;
|
||||
bool out_of_sync;
|
||||
} Fs;
|
||||
|
||||
void fs_mount(Fs *fs, Disk *disk, word block_offset);
|
||||
void fs_umount(Fs *fs);
|
||||
void fs_sync(Fs *fs);
|
||||
void fs_root(Fs *fs, INode *out_inode);
|
||||
void fs_inode(Fs *fs, INode *out_inode, word inode_id);
|
||||
|
||||
void fs_opendir(Fs *fs, DirCursor *out_cursor, INode *inode);
|
||||
void fs_closedir(Fs *fs, DirCursor *cursor);
|
||||
DirEntry *fs_readdir(Fs *fs, DirCursor *cursor);
|
||||
word fs_mkdir(Fs *fs, INode *out_inode, INode *parent, const word *name);
|
||||
word fs_rmdir(Fs *fs, INode *inode);
|
||||
|
||||
void fs_mkfs(Fs *fs, Disk *disk, word block_offset);
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user