Kernel: Rewrite mounting code

This commit is contained in:
Bananymous 2023-03-29 21:34:48 +03:00
parent f9c3ae7090
commit 06ce1f0667
14 changed files with 121 additions and 144 deletions

View File

@ -44,7 +44,6 @@ kernel/Debug.o \
kernel/Device.o \ kernel/Device.o \
kernel/Font.o \ kernel/Font.o \
kernel/FS/Ext2.o \ kernel/FS/Ext2.o \
kernel/FS/Inode.o \
kernel/FS/VirtualFileSystem.o \ kernel/FS/VirtualFileSystem.o \
kernel/Input/PS2Controller.o \ kernel/Input/PS2Controller.o \
kernel/Input/PS2Keyboard.o \ kernel/Input/PS2Keyboard.o \

View File

@ -48,31 +48,12 @@ namespace Kernel
void update(); void update();
void add_device(Device*); void add_device(Device*);
virtual ino_t ino() const override { return 0; }
virtual mode_t mode() const override { return IFDIR | IRUSR | IWUSR | IXUSR | IRGRP | IXGRP | IROTH | IXOTH; }
virtual nlink_t nlink() const override { return 0; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return { 0, 0 }; }
virtual timespec mtime() const override { return { 0, 0 }; }
virtual timespec ctime() const override { return { 0, 0 }; }
virtual blksize_t blksize() const override { return 0; }
virtual blkcnt_t blocks() const override { return 0; }
virtual BAN::StringView name() const override { return "device-manager"sv; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override { return BAN::Error::from_errno(EISDIR); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(EINVAL); };
virtual InodeType type() const override { return InodeType::DeviceManager; }
virtual bool operator==(const Inode&) const override { return false; }
virtual BAN::RefPtr<Inode> root_inode() override { return this; } virtual BAN::RefPtr<Inode> root_inode() override { return this; }
protected: virtual BAN::StringView name() const override { return "device-manager"; }
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode_impl(BAN::StringView) override;
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries_impl(size_t) override; virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) override;
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t) override;
private: private:
DeviceManager() = default; DeviceManager() = default;
@ -82,6 +63,24 @@ namespace Kernel
BAN::Vector<Device*> m_devices; BAN::Vector<Device*> m_devices;
friend class BAN::RefPtr<DeviceManager>; friend class BAN::RefPtr<DeviceManager>;
public:
virtual ino_t ino() const override { return 0; }
virtual mode_t mode() const override { return Mode::IFDIR | Mode::IRUSR | Mode::IWUSR | Mode::IXUSR | Mode::IRGRP | Mode::IXGRP | Mode::IROTH | Mode::IXOTH; }
virtual nlink_t nlink() const override { return 0; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return { 0, 0 }; }
virtual timespec mtime() const override { return { 0, 0 }; }
virtual timespec ctime() const override { return { 0, 0 }; }
virtual blksize_t blksize() const override { return 0; }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return 0x4935; }
virtual dev_t rdev() const override { return 0x7854; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { ASSERT_NOT_REACHED(); }
}; };
} }

View File

@ -134,27 +134,24 @@ namespace Kernel
virtual timespec ctime() const override { return timespec { .tv_sec = m_inode.ctime, .tv_nsec = 0 }; } virtual timespec ctime() const override { return timespec { .tv_sec = m_inode.ctime, .tv_nsec = 0 }; }
virtual blksize_t blksize() const override; virtual blksize_t blksize() const override;
virtual blkcnt_t blocks() const override; virtual blkcnt_t blocks() const override;
virtual dev_t dev() const override { return 0; }
virtual dev_t rdev() const override { return 0; }
virtual BAN::StringView name() const override { return m_name; } virtual BAN::StringView name() const override { return m_name; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries_impl(size_t) override; virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t) override;
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) override;
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override; virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override;
virtual InodeType type() const override { return InodeType::Ext2; }
virtual bool operator==(const Inode& other) const override;
protected:
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode_impl(BAN::StringView) override;
private: private:
BAN::ErrorOr<uint32_t> data_block_index(uint32_t); BAN::ErrorOr<uint32_t> data_block_index(uint32_t);
uint32_t index() const { return m_index; } uint32_t index() const { return m_index; }
private: private:
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, BAN::StringView name, uint32_t index) Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t index, BAN::StringView name)
: m_fs(fs) : m_fs(fs)
, m_inode(inode) , m_inode(inode)
, m_name(name) , m_name(name)

View File

@ -33,19 +33,14 @@ namespace Kernel
IFREG = 0x8000, IFREG = 0x8000,
}; };
enum class InodeType
{
DeviceManager,
Device,
Ext2,
};
public: public:
virtual ~Inode() {} virtual ~Inode() {}
bool ifdir() const { return mode() & Mode::IFDIR; } bool ifdir() const { return mode() & Mode::IFDIR; }
bool ifreg() const { return mode() & Mode::IFREG; } bool ifreg() const { return mode() & Mode::IFREG; }
bool operator==(const Inode& other) const { return dev() == other.dev() && rdev() == other.rdev() && ino() == other.ino(); }
virtual ino_t ino() const = 0; virtual ino_t ino() const = 0;
virtual mode_t mode() const = 0; virtual mode_t mode() const = 0;
virtual nlink_t nlink() const = 0; virtual nlink_t nlink() const = 0;
@ -57,21 +52,16 @@ namespace Kernel
virtual timespec ctime() const = 0; virtual timespec ctime() const = 0;
virtual blksize_t blksize() const = 0; virtual blksize_t blksize() const = 0;
virtual blkcnt_t blocks() const = 0; virtual blkcnt_t blocks() const = 0;
virtual dev_t dev() const = 0;
virtual dev_t rdev() const = 0;
virtual BAN::StringView name() const = 0; virtual BAN::StringView name() const = 0;
BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView); virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t); virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) = 0; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { if ( ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) = 0; virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual InodeType type() const = 0;
virtual bool operator==(const Inode&) const = 0;
protected:
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode_impl(BAN::StringView) = 0;
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries_impl(size_t) = 0;
}; };
} }

View File

@ -30,10 +30,10 @@ namespace Kernel
struct MountPoint struct MountPoint
{ {
BAN::RefPtr<Inode> inode; File host;
FileSystem* target; FileSystem* target;
}; };
const BAN::Vector<MountPoint>& mount_points() const { return m_mount_points; } MountPoint* mount_point_for_inode(BAN::RefPtr<Inode>);
private: private:
VirtualFileSystem() = default; VirtualFileSystem() = default;

View File

@ -8,8 +8,27 @@ namespace Kernel::Input
class PS2Device : public CharacterDevice class PS2Device : public CharacterDevice
{ {
public: public:
PS2Device(dev_t);
virtual ~PS2Device() {} virtual ~PS2Device() {}
virtual void on_byte(uint8_t) = 0; virtual void on_byte(uint8_t) = 0;
public:
virtual ino_t ino() const { return m_ino; }
virtual mode_t mode() const override { return Mode::IFCHR | Mode::IRUSR | Mode::IRGRP; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return m_time; }
virtual timespec mtime() const override { return m_time; }
virtual timespec ctime() const override { return m_time; }
virtual blkcnt_t blocks() const override { return 0; }
virtual dev_t dev() const override { return m_dev; }
private:
timespec m_time;
ino_t m_ino;
dev_t m_dev;
}; };
class PS2Controller class PS2Controller

View File

@ -26,14 +26,16 @@ namespace Kernel::Input
}; };
public: public:
static BAN::ErrorOr<PS2Keyboard*> create(PS2Controller&); static BAN::ErrorOr<PS2Keyboard*> create(PS2Controller&, dev_t);
virtual void on_byte(uint8_t) override; virtual void on_byte(uint8_t) override;
virtual void update() override; virtual void update() override;
private: private:
PS2Keyboard(PS2Controller& controller) PS2Keyboard(PS2Controller& controller, dev_t device)
: m_controller(controller) : PS2Device(device)
, m_controller(controller)
, m_name(BAN::String::formatted("input{}", device))
{} {}
BAN::ErrorOr<void> initialize(); BAN::ErrorOr<void> initialize();
@ -58,30 +60,13 @@ namespace Kernel::Input
State m_state { State::Normal }; State m_state { State::Normal };
BAN::String m_name;
public: public:
virtual ino_t ino() const override { return 0; } virtual BAN::StringView name() const override { return m_name; }
virtual mode_t mode() const override { return IFCHR | IRUSR | IRGRP; }
virtual nlink_t nlink() const override { return 0; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
virtual off_t size() const override { return 0; }
virtual timespec atime() const override { return { 0, 0 }; }
virtual timespec mtime() const override { return { 0, 0 }; }
virtual timespec ctime() const override { return { 0, 0 }; }
virtual blksize_t blksize() const override { return sizeof(KeyEvent); } virtual blksize_t blksize() const override { return sizeof(KeyEvent); }
virtual blkcnt_t blocks() const override { return 0; } virtual dev_t rdev() const override { return 0x8594; }
virtual BAN::StringView name() const override { return "input"sv; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(ENOTDIR); }
virtual InodeType type() const override { return InodeType::Device; }
virtual bool operator==(const Inode&) const override { return false; }
protected:
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode_impl(BAN::StringView) override { return BAN::Error::from_errno(ENOTDIR); }
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries_impl(size_t) override { return BAN::Error::from_errno(ENOTDIR); }
}; };
} }

View File

@ -45,7 +45,7 @@ namespace Kernel
MUST(m_devices.push_back(device)); MUST(m_devices.push_back(device));
} }
BAN::ErrorOr<BAN::RefPtr<Inode>> DeviceManager::read_directory_inode_impl(BAN::StringView name) BAN::ErrorOr<BAN::RefPtr<Inode>> DeviceManager::read_directory_inode(BAN::StringView name)
{ {
LockGuard _(m_lock); LockGuard _(m_lock);
for (Device* device : m_devices) for (Device* device : m_devices)
@ -54,7 +54,7 @@ namespace Kernel
return BAN::Error::from_errno(ENOENT); return BAN::Error::from_errno(ENOENT);
} }
BAN::ErrorOr<BAN::Vector<BAN::String>> DeviceManager::read_directory_entries_impl(size_t index) BAN::ErrorOr<BAN::Vector<BAN::String>> DeviceManager::read_directory_entries(size_t index)
{ {
BAN::Vector<BAN::String> result; BAN::Vector<BAN::String> result;
if (index > 0) if (index > 0)

View File

@ -169,7 +169,7 @@ namespace Kernel
auto inode_location = TRY(fs.locate_inode(inode_inode)); auto inode_location = TRY(fs.locate_inode(inode_inode));
fs.read_block(inode_location.block, block_buffer.span()); fs.read_block(inode_location.block, block_buffer.span());
auto& inode = *(Ext2::Inode*)(block_buffer.data() + inode_location.offset); auto& inode = *(Ext2::Inode*)(block_buffer.data() + inode_location.offset);
Ext2Inode* result = new Ext2Inode(fs, inode, name, inode_inode); Ext2Inode* result = new Ext2Inode(fs, inode, inode_inode, name);
if (result == nullptr) if (result == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<Inode>::adopt(result); return BAN::RefPtr<Inode>::adopt(result);
@ -284,7 +284,7 @@ namespace Kernel
return n_read; return n_read;
} }
BAN::ErrorOr<BAN::Vector<BAN::String>> Ext2Inode::read_directory_entries_impl(size_t index) BAN::ErrorOr<BAN::Vector<BAN::String>> Ext2Inode::read_directory_entries(size_t index)
{ {
if (!ifdir()) if (!ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
@ -328,7 +328,7 @@ namespace Kernel
BAN::Vector<uint8_t> block_buffer; BAN::Vector<uint8_t> block_buffer;
TRY(block_buffer.resize(block_size)); TRY(block_buffer.resize(block_size));
auto error_or = read_directory_inode_impl(name); auto error_or = read_directory_inode(name);
if (!error_or.is_error()) if (!error_or.is_error())
return BAN::Error::from_errno(EEXISTS); return BAN::Error::from_errno(EEXISTS);
if (error_or.error().get_error_code() != ENOENT) if (error_or.error().get_error_code() != ENOENT)
@ -401,7 +401,7 @@ namespace Kernel
return {}; return {};
} }
BAN::ErrorOr<BAN::RefPtr<Inode>> Ext2Inode::read_directory_inode_impl(BAN::StringView file_name) BAN::ErrorOr<BAN::RefPtr<Inode>> Ext2Inode::read_directory_inode(BAN::StringView file_name)
{ {
if (!ifdir()) if (!ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
@ -425,7 +425,7 @@ namespace Kernel
const auto& entry = *(const Ext2::LinkedDirectoryEntry*)entry_addr; const auto& entry = *(const Ext2::LinkedDirectoryEntry*)entry_addr;
BAN::StringView entry_name(entry.name, entry.name_len); BAN::StringView entry_name(entry.name, entry.name_len);
if (entry.inode && entry_name == file_name) if (entry.inode && entry_name == file_name)
return TRY(Ext2Inode::create(m_fs, entry.inode, entry.name)); return TRY(Ext2Inode::create(m_fs, entry.inode, entry_name));
entry_addr += entry.rec_len; entry_addr += entry.rec_len;
} }
} }
@ -433,17 +433,6 @@ namespace Kernel
return BAN::Error::from_errno(ENOENT); return BAN::Error::from_errno(ENOENT);
} }
bool Ext2Inode::operator==(const Inode& other) const
{
if (type() != other.type())
return false;
const auto& ext2_other = (const Ext2Inode&)other;
if (&m_fs != &ext2_other.m_fs)
return false;
return index() == ext2_other.index();
}
BAN::ErrorOr<Ext2FS*> Ext2FS::create(Partition& partition) BAN::ErrorOr<Ext2FS*> Ext2FS::create(Partition& partition)
{ {
Ext2FS* ext2fs = new Ext2FS(partition); Ext2FS* ext2fs = new Ext2FS(partition);

View File

@ -1,26 +0,0 @@
#include <BAN/StringView.h>
#include <kernel/FS/Inode.h>
#include <kernel/FS/VirtualFileSystem.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<Inode>> Inode::read_directory_inode(BAN::StringView name)
{
if (name == ".."sv)
return read_directory_inode_impl(name);
for (const auto& mount : VirtualFileSystem::get().mount_points())
if (*mount.inode == *this)
return mount.target->root_inode()->read_directory_inode_impl(name);
return read_directory_inode_impl(name);
}
BAN::ErrorOr<BAN::Vector<BAN::String>> Inode::read_directory_entries(size_t index)
{
for (const auto& mount : VirtualFileSystem::get().mount_points())
if (*mount.inode == *this)
return mount.target->root_inode()->read_directory_entries_impl(index);
return read_directory_entries_impl(index);
}
}

View File

@ -144,10 +144,18 @@ namespace Kernel
auto file = TRY(file_from_absolute_path(path)); auto file = TRY(file_from_absolute_path(path));
if (!file.inode->ifdir()) if (!file.inode->ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
TRY(m_mount_points.push_back({ file.inode, file_system })); TRY(m_mount_points.push_back({ file, file_system }));
return {}; return {};
} }
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_point_for_inode(BAN::RefPtr<Inode> inode)
{
for (MountPoint& mount : m_mount_points)
if (*mount.host.inode == *inode)
return &mount;
return nullptr;
}
BAN::ErrorOr<VirtualFileSystem::File> VirtualFileSystem::file_from_absolute_path(BAN::StringView path) BAN::ErrorOr<VirtualFileSystem::File> VirtualFileSystem::file_from_absolute_path(BAN::StringView path)
{ {
ASSERT(path.front() == '/'); ASSERT(path.front() == '/');
@ -156,39 +164,47 @@ namespace Kernel
if (!inode) if (!inode)
return BAN::Error::from_c_string("No root inode available"); return BAN::Error::from_c_string("No root inode available");
auto path_parts = TRY(path.split('/')); BAN::String canonical_path;
for (size_t i = 0; i < path_parts.size();) const auto path_parts = TRY(path.split('/'));
for (const auto& path_part : path_parts)
{ {
if (path_parts[i] == "."sv) if (path_part.empty() || path_part == "."sv)
{ {
path_parts.remove(i); continue;
} }
else if (path_parts[i] == ".."sv) else if (path_part == ".."sv)
{ {
inode = TRY(inode->read_directory_inode(path_parts[i])); if (auto* mount_point = mount_point_for_inode(inode))
path_parts.remove(i); inode = TRY(mount_point->host.inode->read_directory_inode(".."sv));
if (i > 0) else
inode = TRY(inode->read_directory_inode(".."sv));
if (!canonical_path.empty())
{ {
path_parts.remove(i - 1); while (canonical_path.back() != '/')
i--; canonical_path.pop_back();
canonical_path.pop_back();
} }
} }
else else
{ {
inode = TRY(inode->read_directory_inode(path_parts[i])); inode = TRY(inode->read_directory_inode(path_part));
i++; TRY(canonical_path.push_back('/'));
TRY(canonical_path.append(path_part));
} }
if (auto* mount_point = mount_point_for_inode(inode))
inode = mount_point->target->root_inode();
} }
if (canonical_path.empty())
TRY(canonical_path.push_back('/'));
File file; File file;
file.inode = inode; file.inode = inode;
file.canonical_path = BAN::move(canonical_path);
for (const auto& part : path_parts)
{
TRY(file.canonical_path.push_back('/'));
TRY(file.canonical_path.append(part));
}
if (file.canonical_path.empty()) if (file.canonical_path.empty())
TRY(file.canonical_path.push_back('/')); TRY(file.canonical_path.push_back('/'));

View File

@ -1,3 +1,4 @@
#include <BAN/Time.h>
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <kernel/ACPI.h> #include <kernel/ACPI.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
@ -5,6 +6,7 @@
#include <kernel/Input/PS2Keyboard.h> #include <kernel/Input/PS2Keyboard.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/RTC.h>
namespace Kernel::Input namespace Kernel::Input
{ {
@ -301,7 +303,7 @@ namespace Kernel::Input
// MF2 Keyboard // MF2 Keyboard
if (index == 2 && (bytes[0] == 0xAB && bytes[1] == 0x83)) if (index == 2 && (bytes[0] == 0xAB && bytes[1] == 0x83))
m_devices[device] = TRY(PS2Keyboard::create(*this)); m_devices[device] = TRY(PS2Keyboard::create(*this, device));
if (m_devices[device]) if (m_devices[device])
return {}; return {};
@ -331,4 +333,11 @@ namespace Kernel::Input
return {}; return {};
} }
PS2Device::PS2Device(dev_t dev)
{
m_dev = dev;
m_ino = dev & 1;
m_time = { BAN::to_unix_time(RTC::get_current_time()), 0 };
}
} }

View File

@ -37,9 +37,9 @@ namespace Kernel::Input
} }
BAN::ErrorOr<PS2Keyboard*> PS2Keyboard::create(PS2Controller& controller) BAN::ErrorOr<PS2Keyboard*> PS2Keyboard::create(PS2Controller& controller, dev_t device)
{ {
PS2Keyboard* keyboard = new PS2Keyboard(controller); PS2Keyboard* keyboard = new PS2Keyboard(controller, device);
if (keyboard == nullptr) if (keyboard == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard guard([keyboard] { delete keyboard; }); BAN::ScopeGuard guard([keyboard] { delete keyboard; });

View File

@ -98,7 +98,7 @@ namespace Kernel
void Shell::run() void Shell::run()
{ {
int fd = MUST(Process::current()->open("/dev/input"sv, O_RDONLY)); int fd = MUST(Process::current()->open("/dev/input0"sv, O_RDONLY));
TTY_PRINT("{}", m_prompt); TTY_PRINT("{}", m_prompt);
for (;;) for (;;)