Kernel: Rework RamInode API
RamInode is now a general RamInode with no data. RamFileInode is now a inode for regular files. This is much cleaner and more intuitive since there is no reason for most non-regular inodes to hold data Vector.
This commit is contained in:
parent
0b93fce923
commit
2301654c4e
|
@ -12,7 +12,6 @@ namespace Kernel
|
||||||
class RamInode : public Inode
|
class RamInode : public Inode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<RamInode>> create(RamFileSystem&, mode_t, uid_t, gid_t);
|
|
||||||
virtual ~RamInode() = default;
|
virtual ~RamInode() = default;
|
||||||
|
|
||||||
virtual ino_t ino() const override { return m_inode_info.ino; }
|
virtual ino_t ino() const override { return m_inode_info.ino; }
|
||||||
|
@ -31,16 +30,10 @@ namespace Kernel
|
||||||
|
|
||||||
void add_link() { m_inode_info.nlink++; }
|
void add_link() { m_inode_info.nlink++; }
|
||||||
|
|
||||||
protected:
|
|
||||||
RamInode(RamFileSystem& fs, mode_t, uid_t, gid_t);
|
|
||||||
|
|
||||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, void*, size_t) override;
|
|
||||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, const void*, size_t) override;
|
|
||||||
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
struct FullInodeInfo
|
struct FullInodeInfo
|
||||||
{
|
{
|
||||||
|
FullInodeInfo(RamFileSystem&, mode_t, uid_t, gid_t);
|
||||||
ino_t ino;
|
ino_t ino;
|
||||||
mode_t mode;
|
mode_t mode;
|
||||||
nlink_t nlink;
|
nlink_t nlink;
|
||||||
|
@ -56,16 +49,36 @@ namespace Kernel
|
||||||
dev_t rdev;
|
dev_t rdev;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
RamInode(RamFileSystem& fs, const FullInodeInfo& inode_info)
|
||||||
|
: m_fs(fs)
|
||||||
|
, m_inode_info(inode_info)
|
||||||
|
{}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
RamFileSystem& m_fs;
|
RamFileSystem& m_fs;
|
||||||
FullInodeInfo m_inode_info;
|
FullInodeInfo m_inode_info;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RamFileInode : public RamInode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static BAN::ErrorOr<BAN::RefPtr<RamFileInode>> create(RamFileSystem&, mode_t, uid_t, gid_t);
|
||||||
|
~RamFileInode() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
RamFileInode(RamFileSystem&, const FullInodeInfo&);
|
||||||
|
|
||||||
|
virtual BAN::ErrorOr<size_t> read_impl(off_t, void*, size_t) override;
|
||||||
|
virtual BAN::ErrorOr<size_t> write_impl(off_t, const void*, size_t) override;
|
||||||
|
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
|
||||||
|
|
||||||
|
private:
|
||||||
BAN::Vector<uint8_t> m_data;
|
BAN::Vector<uint8_t> m_data;
|
||||||
|
|
||||||
friend class RamFileSystem;
|
friend class RamFileSystem;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RamDirectoryInode final : public RamInode
|
class RamDirectoryInode : public RamInode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> create(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> create(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t);
|
||||||
|
@ -74,13 +87,12 @@ namespace Kernel
|
||||||
BAN::ErrorOr<void> add_inode(BAN::StringView, BAN::RefPtr<RamInode>);
|
BAN::ErrorOr<void> add_inode(BAN::StringView, BAN::RefPtr<RamInode>);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
RamDirectoryInode(RamFileSystem&, const FullInodeInfo&, ino_t parent);
|
||||||
|
|
||||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||||
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override;
|
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override;
|
||||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||||
|
|
||||||
private:
|
|
||||||
RamDirectoryInode(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t m_name_max = NAME_MAX;
|
static constexpr size_t m_name_max = NAME_MAX;
|
||||||
struct Entry
|
struct Entry
|
||||||
|
@ -92,7 +104,7 @@ namespace Kernel
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Vector<Entry> m_entries;
|
BAN::Vector<Entry> m_entries;
|
||||||
ino_t m_parent;
|
const ino_t m_parent;
|
||||||
|
|
||||||
friend class RamFileSystem;
|
friend class RamFileSystem;
|
||||||
};
|
};
|
||||||
|
@ -111,7 +123,7 @@ namespace Kernel
|
||||||
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
|
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RamSymlinkInode(RamFileSystem&, mode_t, uid_t, gid_t);
|
RamSymlinkInode(RamFileSystem&, const FullInodeInfo&, BAN::String&&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::String m_target;
|
BAN::String m_target;
|
||||||
|
|
|
@ -5,7 +5,7 @@ namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
Device::Device(mode_t mode, uid_t uid, gid_t gid)
|
Device::Device(mode_t mode, uid_t uid, gid_t gid)
|
||||||
: RamInode(DevFileSystem::get(), mode, uid, gid)
|
: RamInode(DevFileSystem::get(), FullInodeInfo(DevFileSystem::get(), mode, uid, gid))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
}
|
}
|
|
@ -19,7 +19,7 @@ namespace Kernel
|
||||||
s_instance = new DevFileSystem(1024 * 1024);
|
s_instance = new DevFileSystem(1024 * 1024);
|
||||||
ASSERT(s_instance);
|
ASSERT(s_instance);
|
||||||
|
|
||||||
auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, Inode::Mode::IFDIR | 0755, 0, 0));
|
auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, 0755, 0, 0));
|
||||||
MUST(s_instance->set_root_inode(root_inode));
|
MUST(s_instance->set_root_inode(root_inode));
|
||||||
|
|
||||||
s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0)));
|
s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0)));
|
||||||
|
|
|
@ -5,42 +5,51 @@
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
|
||||||
/*
|
RamInode::FullInodeInfo::FullInodeInfo(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
||||||
|
|
||||||
RAM INODE
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<RamInode>> RamInode::create(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
|
||||||
{
|
|
||||||
ASSERT(Mode(mode).ifreg());
|
|
||||||
auto* ram_inode = new RamInode(fs, mode, uid, gid);
|
|
||||||
if (ram_inode == nullptr)
|
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
|
||||||
return BAN::RefPtr<RamInode>::adopt(ram_inode);
|
|
||||||
}
|
|
||||||
|
|
||||||
RamInode::RamInode(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
|
||||||
: m_fs(fs)
|
|
||||||
{
|
{
|
||||||
timespec current_time = SystemTimer::get().real_time();
|
timespec current_time = SystemTimer::get().real_time();
|
||||||
|
|
||||||
m_inode_info.ino = fs.next_ino();
|
this->ino = fs.next_ino();
|
||||||
m_inode_info.mode = mode;
|
this->mode = mode;
|
||||||
m_inode_info.nlink = 1;
|
this->nlink = 1;
|
||||||
m_inode_info.uid = uid;
|
this->uid = uid;
|
||||||
m_inode_info.gid = gid;
|
this->gid = gid;
|
||||||
m_inode_info.size = 0;
|
this->size = 0;
|
||||||
m_inode_info.atime = current_time;
|
this->atime = current_time;
|
||||||
m_inode_info.mtime = current_time;
|
this->mtime = current_time;
|
||||||
m_inode_info.ctime = current_time;
|
this->ctime = current_time;
|
||||||
m_inode_info.blksize = fs.blksize();
|
this->blksize = fs.blksize();
|
||||||
m_inode_info.blocks = 0;
|
this->blocks = 0;
|
||||||
m_inode_info.dev = 0;
|
|
||||||
m_inode_info.rdev = 0;
|
// TODO
|
||||||
|
this->dev = 0;
|
||||||
|
this->rdev = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> RamInode::read_impl(off_t offset, void* buffer, size_t bytes)
|
/*
|
||||||
|
|
||||||
|
RAM FILE INODE
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
BAN::ErrorOr<BAN::RefPtr<RamFileInode>> RamFileInode::create(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
||||||
|
{
|
||||||
|
FullInodeInfo inode_info(fs, mode, uid, gid);
|
||||||
|
|
||||||
|
auto* ram_inode = new RamFileInode(fs, inode_info);
|
||||||
|
if (ram_inode == nullptr)
|
||||||
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
|
return BAN::RefPtr<RamFileInode>::adopt(ram_inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
RamFileInode::RamFileInode(RamFileSystem& fs, const FullInodeInfo& inode_info)
|
||||||
|
: RamInode(fs, inode_info)
|
||||||
|
{
|
||||||
|
ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
|
||||||
|
m_inode_info.mode |= Inode::Mode::IFREG;
|
||||||
|
}
|
||||||
|
|
||||||
|
BAN::ErrorOr<size_t> RamFileInode::read_impl(off_t offset, void* buffer, size_t bytes)
|
||||||
{
|
{
|
||||||
ASSERT(offset >= 0);
|
ASSERT(offset >= 0);
|
||||||
if (offset >= size())
|
if (offset >= size())
|
||||||
|
@ -50,7 +59,7 @@ namespace Kernel
|
||||||
return to_copy;
|
return to_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<size_t> RamInode::write_impl(off_t offset, const void* buffer, size_t bytes)
|
BAN::ErrorOr<size_t> RamFileInode::write_impl(off_t offset, const void* buffer, size_t bytes)
|
||||||
{
|
{
|
||||||
ASSERT(offset >= 0);
|
ASSERT(offset >= 0);
|
||||||
if (offset + bytes > (size_t)size())
|
if (offset + bytes > (size_t)size())
|
||||||
|
@ -59,7 +68,7 @@ namespace Kernel
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> RamInode::truncate_impl(size_t new_size)
|
BAN::ErrorOr<void> RamFileInode::truncate_impl(size_t new_size)
|
||||||
{
|
{
|
||||||
TRY(m_data.resize(new_size, 0));
|
TRY(m_data.resize(new_size, 0));
|
||||||
m_inode_info.size = m_data.size();
|
m_inode_info.size = m_data.size();
|
||||||
|
@ -75,30 +84,32 @@ namespace Kernel
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> RamDirectoryInode::create(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<RamDirectoryInode>> RamDirectoryInode::create(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
ASSERT(Mode(mode).ifdir());
|
FullInodeInfo inode_info(fs, mode, uid, gid);
|
||||||
auto* ram_inode = new RamDirectoryInode(fs, parent, mode, uid, gid);
|
|
||||||
|
// "." links to this
|
||||||
|
inode_info.nlink++;
|
||||||
|
|
||||||
|
// ".." links to this or parent
|
||||||
|
if (parent)
|
||||||
|
TRY(fs.get_inode(parent))->add_link();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inode_info.nlink++;
|
||||||
|
parent = inode_info.ino;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* ram_inode = new RamDirectoryInode(fs, inode_info, parent);
|
||||||
if (ram_inode == nullptr)
|
if (ram_inode == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
return BAN::RefPtr<RamDirectoryInode>::adopt(ram_inode);
|
return BAN::RefPtr<RamDirectoryInode>::adopt(ram_inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
RamDirectoryInode::RamDirectoryInode(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid)
|
RamDirectoryInode::RamDirectoryInode(RamFileSystem& fs, const FullInodeInfo& inode_info, ino_t parent)
|
||||||
: RamInode(fs, mode, uid, gid)
|
: RamInode(fs, inode_info)
|
||||||
|
, m_parent(parent)
|
||||||
{
|
{
|
||||||
// "." links to this
|
ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
|
||||||
m_inode_info.nlink++;
|
m_inode_info.mode |= Inode::Mode::IFDIR;
|
||||||
|
|
||||||
// ".." links to this, if there is no parent
|
|
||||||
if (parent == 0)
|
|
||||||
{
|
|
||||||
m_inode_info.nlink++;
|
|
||||||
m_parent = ino();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MUST(fs.get_inode(parent))->add_link();
|
|
||||||
m_parent = parent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<Inode>> RamDirectoryInode::find_inode_impl(BAN::StringView name)
|
BAN::ErrorOr<BAN::RefPtr<Inode>> RamDirectoryInode::find_inode_impl(BAN::StringView name)
|
||||||
|
@ -181,7 +192,7 @@ namespace Kernel
|
||||||
{
|
{
|
||||||
BAN::RefPtr<RamInode> inode;
|
BAN::RefPtr<RamInode> inode;
|
||||||
if (Mode(mode).ifreg())
|
if (Mode(mode).ifreg())
|
||||||
inode = TRY(RamInode::create(m_fs, mode, uid, gid));
|
inode = TRY(RamFileInode::create(m_fs, mode, uid, gid));
|
||||||
else if (Mode(mode).ifdir())
|
else if (Mode(mode).ifdir())
|
||||||
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid));
|
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid));
|
||||||
else
|
else
|
||||||
|
@ -222,20 +233,26 @@ namespace Kernel
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<RamSymlinkInode>> RamSymlinkInode::create(RamFileSystem& fs, BAN::StringView target, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<RamSymlinkInode>> RamSymlinkInode::create(RamFileSystem& fs, BAN::StringView target_sv, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
ASSERT(Mode(mode).iflnk());
|
FullInodeInfo inode_info(fs, mode, uid, gid);
|
||||||
auto* ram_inode = new RamSymlinkInode(fs, mode, uid, gid);
|
|
||||||
|
BAN::String target_str;
|
||||||
|
TRY(target_str.append(target_sv));
|
||||||
|
|
||||||
|
auto* ram_inode = new RamSymlinkInode(fs, inode_info, BAN::move(target_str));
|
||||||
if (ram_inode == nullptr)
|
if (ram_inode == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
auto ref_ptr = BAN::RefPtr<RamSymlinkInode>::adopt(ram_inode);
|
return BAN::RefPtr<RamSymlinkInode>::adopt(ram_inode);
|
||||||
TRY(ref_ptr->set_link_target(target));
|
|
||||||
return ref_ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RamSymlinkInode::RamSymlinkInode(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
RamSymlinkInode::RamSymlinkInode(RamFileSystem& fs, const FullInodeInfo& inode_info, BAN::String&& target)
|
||||||
: RamInode(fs, mode, uid, gid)
|
: RamInode(fs, inode_info)
|
||||||
{ }
|
, m_target(BAN::move(target))
|
||||||
|
{
|
||||||
|
ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0);
|
||||||
|
m_inode_info.mode |= Inode::Mode::IFLNK;
|
||||||
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::String> RamSymlinkInode::link_target_impl()
|
BAN::ErrorOr<BAN::String> RamSymlinkInode::link_target_impl()
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,14 +26,10 @@ namespace Kernel
|
||||||
s_instance->m_root_fs = MUST(Ext2FS::create(*(Partition*)partition_inode.ptr()));
|
s_instance->m_root_fs = MUST(Ext2FS::create(*(Partition*)partition_inode.ptr()));
|
||||||
|
|
||||||
Credentials root_creds { 0, 0, 0, 0 };
|
Credentials root_creds { 0, 0, 0, 0 };
|
||||||
MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"));
|
MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"sv));
|
||||||
|
|
||||||
mode_t tmpfs_mode = Inode::Mode::IFDIR
|
auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, 0777, 0, 0));
|
||||||
| Inode::Mode::IRUSR | Inode::Mode::IWUSR | Inode::Mode::IXUSR
|
MUST(s_instance->mount(root_creds, tmpfs, "/tmp"sv));
|
||||||
| Inode::Mode::IRGRP | Inode::Mode::IWGRP | Inode::Mode::IXGRP
|
|
||||||
| Inode::Mode::IROTH | Inode::Mode::IWOTH | Inode::Mode::IXOTH;
|
|
||||||
auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, tmpfs_mode, 0, 0));
|
|
||||||
MUST(s_instance->mount(root_creds, tmpfs, "/tmp"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtualFileSystem& VirtualFileSystem::get()
|
VirtualFileSystem& VirtualFileSystem::get()
|
||||||
|
|
|
@ -32,7 +32,7 @@ namespace Kernel
|
||||||
if (inode_or_error.is_error())
|
if (inode_or_error.is_error())
|
||||||
{
|
{
|
||||||
if (inode_or_error.error().get_error_code() == ENOENT)
|
if (inode_or_error.error().get_error_code() == ENOENT)
|
||||||
DevFileSystem::get().add_device("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), S_IFLNK | 0666, 0, 0)));
|
DevFileSystem::get().add_device("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), 0666, 0, 0)));
|
||||||
else
|
else
|
||||||
dwarnln("{}", inode_or_error.error());
|
dwarnln("{}", inode_or_error.error());
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue