kernel: Make a factory function for filesystem creation of inodes, and add inode numbers

Now every filesystem must provide fs->create_file_inode() for new inodes :)
This commit is contained in:
apio 2023-02-25 18:03:11 +01:00
parent 5edd316198
commit ddc4692671
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 39 additions and 19 deletions

View File

@ -21,6 +21,8 @@ namespace VFS
virtual ~Inode() = default; virtual ~Inode() = default;
virtual InodeType type() const = 0; virtual InodeType type() const = 0;
virtual usize inode_number() const = 0;
}; };
class FileInode : Inode class FileInode : Inode
@ -44,6 +46,8 @@ namespace VFS
public: public:
virtual Inode& root() const = 0; virtual Inode& root() const = 0;
virtual Result<SharedPtr<Inode>> create_file_inode() = 0;
virtual ~FileSystem() = default; virtual ~FileSystem() = default;
}; };

View File

@ -5,29 +5,24 @@ namespace TmpFS
{ {
Result<SharedPtr<VFS::FileSystem>> FileSystem::create() Result<SharedPtr<VFS::FileSystem>> FileSystem::create()
{ {
SharedPtr<Inode> root = TRY(make_shared<Inode>());
SharedPtr<FileSystem> fs = TRY(adopt_shared(new (std::nothrow) FileSystem())); SharedPtr<FileSystem> fs = TRY(adopt_shared(new (std::nothrow) FileSystem()));
root->set_fs(*fs, {}); SharedPtr<VFS::Inode> root = TRY(fs->create_file_inode());
TRY(fs->set_root(root)); fs->set_root(root);
return (SharedPtr<VFS::FileSystem>)fs; return (SharedPtr<VFS::FileSystem>)fs;
} }
FileSystem::FileSystem() Result<SharedPtr<VFS::Inode>> FileSystem::create_file_inode()
{ {
SharedPtr<Inode> inode = TRY(make_shared<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;
} }
Result<void> FileSystem::set_root(SharedPtr<VFS::Inode> root) void FileSystem::set_root(SharedPtr<VFS::Inode> root)
{ {
m_root_inode = root; m_root_inode = root;
return m_inodes.try_append(root);
}
Inode::Inode()
{
}
void Inode::set_fs(FileSystem& fs, Badge<FileSystem>)
{
m_fs = &fs;
} }
} }

View File

@ -13,34 +13,52 @@ namespace TmpFS
return *m_root_inode; return *m_root_inode;
} }
Result<SharedPtr<VFS::Inode>> create_file_inode() override;
static Result<SharedPtr<VFS::FileSystem>> create(); static Result<SharedPtr<VFS::FileSystem>> create();
virtual ~FileSystem() = default; virtual ~FileSystem() = default;
private: private:
FileSystem(); FileSystem() = default;
Result<void> set_root(SharedPtr<VFS::Inode> root); void set_root(SharedPtr<VFS::Inode> root);
SharedPtr<VFS::Inode> m_root_inode; SharedPtr<VFS::Inode> m_root_inode;
Vector<SharedPtr<VFS::Inode>> m_inodes; Vector<SharedPtr<VFS::Inode>> m_inodes;
usize m_next_inode_number { 2 };
}; };
class Inode : public VFS::FileInode class Inode : public VFS::FileInode
{ {
public: public:
Inode(); Inode() = default;
void set_fs(FileSystem& fs, Badge<FileSystem>); void set_fs(FileSystem& fs, Badge<FileSystem>)
{
m_fs = &fs;
}
void set_inode_number(usize inum, Badge<FileSystem>)
{
m_inode_number = inum;
}
VFS::FileSystem& fs() const override VFS::FileSystem& fs() const override
{ {
return *m_fs; return *m_fs;
} }
usize inode_number() const override
{
return m_inode_number;
}
virtual ~Inode() = default; virtual ~Inode() = default;
private: private:
VFS::FileSystem* m_fs; VFS::FileSystem* m_fs;
usize m_inode_number;
}; };
} }

View File

@ -58,6 +58,9 @@ Result<void> init()
VFS::root_fs = TRY(TmpFS::FileSystem::create()); VFS::root_fs = TRY(TmpFS::FileSystem::create());
VFS::Inode& root_inode = VFS::root_fs->root();
kinfoln("root inode number: %zu", root_inode.inode_number());
TarStream::Entry entry; TarStream::Entry entry;
while (TRY(g_initrd.read_next_entry(entry))) while (TRY(g_initrd.read_next_entry(entry)))
{ {