Kernel: Shell can now mount partitions

This commit is contained in:
Bananymous 2023-03-30 15:06:41 +03:00
parent 5b3a00c64f
commit 3e8ab8271d
9 changed files with 50 additions and 5 deletions

View File

@ -16,7 +16,8 @@ namespace Kernel
{
BlockDevice,
CharacterDevice,
DeviceController
DeviceController,
Partition,
};
Device();
@ -24,6 +25,8 @@ namespace Kernel
virtual DeviceType device_type() const = 0;
virtual void update() {}
virtual InodeType inode_type() const override { return InodeType::Device; }
virtual timespec atime() const override { return m_create_time; }
virtual timespec mtime() const override { return m_create_time; }
virtual timespec ctime() const override { return m_create_time; }
@ -58,6 +61,8 @@ namespace Kernel
virtual BAN::RefPtr<Inode> root_inode() override { return this; }
virtual InodeType inode_type() const override { return InodeType::Device; }
virtual BAN::StringView name() const override { return "device-manager"; }
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) override;

View File

@ -137,6 +137,8 @@ namespace Kernel
virtual dev_t dev() const override { return 0; }
virtual dev_t rdev() const override { return 0; }
virtual InodeType inode_type() const override { return InodeType::Ext2; }
virtual BAN::StringView name() const override { return m_name; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;

View File

@ -48,6 +48,12 @@ namespace Kernel
mode_t mode;
};
enum class InodeType
{
Device,
Ext2,
};
public:
virtual ~Inode() {}
@ -67,6 +73,8 @@ namespace Kernel
virtual dev_t dev() const = 0;
virtual dev_t rdev() const = 0;
virtual InodeType inode_type() const = 0;
virtual BAN::StringView name() const = 0;
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }

View File

@ -18,7 +18,7 @@ namespace Kernel
virtual BAN::RefPtr<Inode> root_inode() override { return m_root_fs->root_inode(); }
BAN::ErrorOr<void> mount(FileSystem*, BAN::StringView);
BAN::ErrorOr<void> mount(BAN::StringView, BAN::StringView);
struct File
{
@ -27,6 +27,10 @@ namespace Kernel
};
BAN::ErrorOr<File> file_from_absolute_path(BAN::StringView);
private:
VirtualFileSystem() = default;
BAN::ErrorOr<void> mount(FileSystem*, BAN::StringView);
struct MountPoint
{
File host;
@ -34,9 +38,6 @@ namespace Kernel
};
MountPoint* mount_point_for_inode(BAN::RefPtr<Inode>);
private:
VirtualFileSystem() = default;
private:
FileSystem* m_root_fs = nullptr;
BAN::Vector<MountPoint> m_mount_points;

View File

@ -37,6 +37,8 @@ namespace Kernel
BAN::ErrorOr<void> fstat(int, stat*);
BAN::ErrorOr<void> stat(BAN::StringView, stat*);
BAN::ErrorOr<void> mount(BAN::StringView, BAN::StringView);
BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(int);
BAN::String working_directory() const;

View File

@ -42,6 +42,8 @@ namespace Kernel
char m_label[36 * 4 + 1];
public:
virtual DeviceType device_type() const override { return DeviceType::Partition; }
virtual ino_t ino() const override { return m_index; }
virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
virtual nlink_t nlink() const override { return 1; }

View File

@ -35,6 +35,18 @@ namespace Kernel
return *s_instance;
}
BAN::ErrorOr<void> VirtualFileSystem::mount(BAN::StringView partition, BAN::StringView target)
{
auto partition_file = TRY(file_from_absolute_path(partition));
if (partition_file.inode->inode_type() != Inode::InodeType::Device)
return BAN::Error::from_c_string("Not a partition");
Device* device = (Device*)partition_file.inode.ptr();
if (device->device_type() != Device::DeviceType::Partition)
return BAN::Error::from_c_string("Not a partition");
auto* file_system = TRY(Ext2FS::create(*(Partition*)device));
return mount(file_system, target);
}
BAN::ErrorOr<void> VirtualFileSystem::mount(FileSystem* file_system, BAN::StringView path)
{
auto file = TRY(file_from_absolute_path(path));

View File

@ -97,6 +97,13 @@ namespace Kernel
return {};
}
BAN::ErrorOr<void> Process::mount(BAN::StringView partition, BAN::StringView path)
{
LockGuard _(m_lock);
TRY(VirtualFileSystem::get().mount(partition, path));
return {};
}
BAN::ErrorOr<void> Process::fstat(int fd, struct stat* out)
{
LockGuard _(m_lock);

View File

@ -508,6 +508,12 @@ argument_done:
TTY_PRINTLN("{} {} {}", crc32, total_read, arguments[i]);
}
}
else if (arguments.front() == "mount")
{
if (arguments.size() != 3)
return BAN::Error::from_c_string("usage: 'mount partition directory'");
TRY(Process::current()->mount(arguments[1], arguments[2]));
}
else if (arguments.front() == "loadfont")
{
if (arguments.size() != 2)