Compare commits

...

3 Commits

Author SHA1 Message Date
ff770b7328
VFS+TmpFS: Add support for creating subdirectories (mkdir)
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-10 21:09:08 +01:00
41203df472
libluna/Vector: Use bytes instead of count in resize()
This fixes a bug that was causing the heap linked list to become quite corrupted.
2023-03-10 21:07:08 +01:00
e8a401efc2
libluna/Heap: Crash the kernel (but not userspace) on invalid frees
This makes them way easier to catch and forces us to get those out of the way.
2023-03-10 21:02:09 +01:00
6 changed files with 60 additions and 5 deletions

View File

@ -19,6 +19,8 @@ namespace VFS
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
// Generic methods
virtual FileSystem& fs() const = 0;
@ -42,6 +44,11 @@ namespace VFS
return err(ENOTDIR);
}
Result<SharedPtr<Inode>> create_subdirectory(const char*) override
{
return err(ENOTDIR);
}
InodeType type() const override
{
return InodeType::RegularFile;

View File

@ -26,13 +26,17 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode(SharedPtr<VFS::Inode> parent)
{
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
TRY(inode->add_entry(inode, "."));
TRY(inode->add_entry(parent ? parent : (SharedPtr<VFS::Inode>)inode, ".."));
inode->set_self(inode, {});
inode->set_fs(*this, {});
inode->set_inode_number(m_next_inode_number, {});
TRY(m_inodes.try_append(inode));
m_next_inode_number++;
return (SharedPtr<VFS::Inode>)inode;
}
@ -68,4 +72,13 @@ namespace TmpFS
return inode;
}
Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name)
{
auto inode = TRY(m_fs->create_dir_inode(m_self));
TRY(add_entry(inode, name));
return inode;
}
}

View File

@ -80,6 +80,11 @@ namespace TmpFS
m_inode_number = inum;
}
void set_self(SharedPtr<VFS::Inode> self, Badge<FileSystem>)
{
m_self = self;
}
Result<SharedPtr<VFS::Inode>> find(const char* name) const override;
VFS::FileSystem& fs() const override
@ -98,6 +103,7 @@ namespace TmpFS
}
Result<SharedPtr<VFS::Inode>> create_file(const char* name) override;
Result<SharedPtr<VFS::Inode>> create_subdirectory(const char* name) override;
Result<void> add_entry(SharedPtr<VFS::Inode> inode, const char* name);
@ -107,6 +113,8 @@ namespace TmpFS
VFS::FileSystem* m_fs;
usize m_inode_number;
SharedPtr<VFS::Inode> m_self;
struct Entry
{
SharedPtr<VFS::Inode> inode;

View File

@ -52,14 +52,24 @@ static Result<void> try_init_vfs()
VFS::root_fs = TRY(TmpFS::FileSystem::create());
VFS::Inode& root_inode = VFS::root_inode();
kinfoln("root inode number: %zu", root_inode.inode_number());
kinfoln("root inode's '.' entry inode number: %zu", TRY(root_inode.find("."))->inode_number());
kinfoln("root inode's '..' entry inode number: %zu", TRY(root_inode.find(".."))->inode_number());
TRY(root_inode.create_file("usr"));
TRY(root_inode.create_subdirectory("etc"));
kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number());
kinfoln("root inode's 'etc' entry inode number: %zu", TRY(root_inode.find("etc"))->inode_number());
auto& etc = *TRY(root_inode.find("etc"));
kinfoln("etc inode's '.' entry inode number: %zu", TRY(etc.find("."))->inode_number());
kinfoln("etc inode's '..' entry inode number: %zu", TRY(etc.find(".."))->inode_number());
TRY(etc.create_file("passwd"));
kinfoln("etc inode's 'passwd' entry inode number: %zu", TRY(etc.find("passwd"))->inode_number());
return {};
}

View File

@ -150,6 +150,11 @@ template <typename T> class Vector
return m_capacity;
}
usize byte_capacity() const
{
return m_capacity * sizeof(T);
}
usize size() const
{
return m_size;
@ -162,10 +167,14 @@ template <typename T> class Vector
Result<void> resize(usize new_capacity)
{
void* ptr = TRY(realloc_impl(m_data, new_capacity));
if (new_capacity < m_capacity) memcpy(ptr, m_data, new_capacity);
const usize new_byte_capacity = new_capacity * sizeof(T);
void* const ptr = TRY(realloc_impl(m_data, new_byte_capacity));
if (new_capacity < m_capacity) memcpy(ptr, m_data, new_byte_capacity);
else
memcpy(ptr, m_data, m_capacity);
memcpy(ptr, m_data, byte_capacity());
m_capacity = new_capacity;
m_data = (T*)ptr;
return {};

View File

@ -249,13 +249,21 @@ Result<void> free_impl(void* ptr)
else
dbgln("ERROR: Attempt to free memory at %p, which wasn't allocated with malloc", ptr);
#ifdef USE_FREESTANDING
fail("Call to free_impl() with an invalid argument (double-free or erroneous deallocation)");
#else
return err(EFAULT);
#endif
}
if (is_block_free(block))
{
dbgln("ERROR: Attempt to free memory at %p, which was already freed", ptr);
#ifdef USE_FREESTANDING
fail("Call to free_impl() with a pointer to freed memory (probably double-free)");
#else
return err(EFAULT);
#endif
}
else
block->status &= ~BLOCK_USED;