2023-03-30 14:22:15 +03:00
|
|
|
#include <BAN/ScopeGuard.h>
|
2023-02-22 01:23:11 +02:00
|
|
|
#include <BAN/StringView.h>
|
2023-07-10 23:17:14 +03:00
|
|
|
#include <kernel/FS/DevFS/FileSystem.h>
|
2023-09-30 21:19:36 +03:00
|
|
|
#include <kernel/FS/ProcFS/FileSystem.h>
|
2023-11-07 02:36:22 +02:00
|
|
|
#include <kernel/FS/TmpFS/FileSystem.h>
|
2023-02-20 01:46:00 +02:00
|
|
|
#include <kernel/FS/VirtualFileSystem.h>
|
2024-02-28 22:39:02 +02:00
|
|
|
#include <kernel/Lock/LockGuard.h>
|
2024-08-14 13:21:33 +03:00
|
|
|
#include <kernel/Storage/Partition.h>
|
|
|
|
|
2023-06-11 19:52:13 +03:00
|
|
|
#include <fcntl.h>
|
2023-02-20 01:46:00 +02:00
|
|
|
|
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
2024-06-11 10:50:26 +03:00
|
|
|
static BAN::RefPtr<VirtualFileSystem> s_instance;
|
2023-02-20 01:46:00 +02:00
|
|
|
|
2024-06-14 00:19:12 +03:00
|
|
|
void VirtualFileSystem::initialize(BAN::StringView root_path)
|
2023-02-20 01:46:00 +02:00
|
|
|
{
|
2024-06-11 10:50:26 +03:00
|
|
|
ASSERT(!s_instance);
|
|
|
|
s_instance = MUST(BAN::RefPtr<VirtualFileSystem>::create());
|
2023-03-09 02:17:42 +02:00
|
|
|
|
2024-08-14 13:21:33 +03:00
|
|
|
BAN::RefPtr<BlockDevice> root_device;
|
|
|
|
if (root_path.size() >= 5 && root_path.substring(0, 5) == "UUID="_sv)
|
|
|
|
{
|
|
|
|
auto uuid = root_path.substring(5);
|
|
|
|
if (uuid.size() != 36)
|
|
|
|
panic("Invalid UUID specified for root '{}'", uuid);
|
|
|
|
|
|
|
|
BAN::RefPtr<Partition> root_partition;
|
|
|
|
DevFileSystem::get().for_each_inode(
|
|
|
|
[&root_partition, uuid](BAN::RefPtr<Inode> inode) -> BAN::Iteration
|
|
|
|
{
|
|
|
|
if (!inode->is_device())
|
|
|
|
return BAN::Iteration::Continue;
|
|
|
|
if (!static_cast<Device*>(inode.ptr())->is_partition())
|
|
|
|
return BAN::Iteration::Continue;
|
|
|
|
auto* partition = static_cast<Partition*>(inode.ptr());
|
|
|
|
if (partition->uuid() != uuid)
|
|
|
|
return BAN::Iteration::Continue;
|
|
|
|
root_partition = partition;
|
|
|
|
return BAN::Iteration::Break;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!root_partition)
|
|
|
|
panic("Could not find partition with UUID '{}'", uuid);
|
|
|
|
root_device = root_partition;
|
|
|
|
}
|
|
|
|
else if (root_path.size() >= 5 && root_path.substring(0, 5) == "/dev/"_sv)
|
|
|
|
{
|
|
|
|
auto device_name = root_path.substring(5);
|
|
|
|
|
|
|
|
auto device_result = DevFileSystem::get().root_inode()->find_inode(device_name);
|
|
|
|
if (device_result.is_error())
|
|
|
|
panic("Could not open root device '{}': {}", root_path, device_result.error());
|
|
|
|
|
|
|
|
auto device_inode = device_result.release_value();
|
|
|
|
if (!device_inode->mode().ifblk())
|
|
|
|
panic("Root inode '{}' is not an block device", root_path);
|
|
|
|
|
|
|
|
root_device = static_cast<BlockDevice*>(device_inode.ptr());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
panic("Unknown root path format '{}' specified", root_path);
|
|
|
|
}
|
2023-03-30 16:37:53 +03:00
|
|
|
|
2024-08-14 13:21:33 +03:00
|
|
|
auto filesystem_result = FileSystem::from_block_device(root_device);
|
|
|
|
if (filesystem_result.is_error())
|
|
|
|
panic("Could not create filesystem from '{}': {}", root_path, filesystem_result.error());
|
|
|
|
s_instance->m_root_fs = filesystem_result.release_value();
|
2023-03-30 16:37:53 +03:00
|
|
|
|
2023-07-10 13:26:14 +03:00
|
|
|
Credentials root_creds { 0, 0, 0, 0 };
|
2024-06-18 20:32:43 +03:00
|
|
|
MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"_sv));
|
2023-09-30 19:06:05 +03:00
|
|
|
|
2024-06-18 20:32:43 +03:00
|
|
|
MUST(s_instance->mount(root_creds, &ProcFileSystem::get(), "/proc"_sv));
|
2023-09-30 21:19:36 +03:00
|
|
|
|
2024-06-11 10:50:26 +03:00
|
|
|
auto tmpfs = MUST(TmpFileSystem::create(1024, 0777, 0, 0));
|
2024-06-18 20:32:43 +03:00
|
|
|
MUST(s_instance->mount(root_creds, tmpfs, "/tmp"_sv));
|
2023-02-20 01:46:00 +02:00
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-02-20 01:46:00 +02:00
|
|
|
VirtualFileSystem& VirtualFileSystem::get()
|
|
|
|
{
|
|
|
|
ASSERT(s_instance);
|
|
|
|
return *s_instance;
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:20:24 +02:00
|
|
|
BAN::ErrorOr<void> VirtualFileSystem::mount(const Credentials& credentials, BAN::StringView block_device_path, BAN::StringView target)
|
2023-03-30 15:06:41 +03:00
|
|
|
{
|
2023-11-21 15:20:24 +02:00
|
|
|
auto block_device_file = TRY(file_from_absolute_path(credentials, block_device_path, true));
|
|
|
|
if (!block_device_file.inode->is_device())
|
2023-04-11 23:25:21 +03:00
|
|
|
return BAN::Error::from_errno(ENOTBLK);
|
2023-03-30 22:02:16 +03:00
|
|
|
|
2023-11-29 20:50:57 +02:00
|
|
|
auto* device = static_cast<Device*>(block_device_file.inode.ptr());
|
2023-11-21 15:20:24 +02:00
|
|
|
if (!device->mode().ifblk())
|
2023-04-11 23:25:21 +03:00
|
|
|
return BAN::Error::from_errno(ENOTBLK);
|
2023-03-30 22:02:16 +03:00
|
|
|
|
2024-06-11 10:50:26 +03:00
|
|
|
auto file_system = TRY(FileSystem::from_block_device(static_cast<BlockDevice*>(device)));
|
2023-06-11 19:52:13 +03:00
|
|
|
return mount(credentials, file_system, target);
|
2023-03-30 15:06:41 +03:00
|
|
|
}
|
|
|
|
|
2024-06-11 10:50:26 +03:00
|
|
|
BAN::ErrorOr<void> VirtualFileSystem::mount(const Credentials& credentials, BAN::RefPtr<FileSystem> file_system, BAN::StringView path)
|
2023-03-29 11:50:46 +03:00
|
|
|
{
|
2023-06-11 19:52:13 +03:00
|
|
|
auto file = TRY(file_from_absolute_path(credentials, path, true));
|
2023-03-30 14:41:15 +03:00
|
|
|
if (!file.inode->mode().ifdir())
|
2023-03-29 11:50:46 +03:00
|
|
|
return BAN::Error::from_errno(ENOTDIR);
|
2023-03-30 22:02:16 +03:00
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
LockGuard _(m_mutex);
|
2024-09-14 19:43:44 +03:00
|
|
|
TRY(m_mount_points.emplace_back(file_system, BAN::move(file)));
|
2023-03-19 05:51:25 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-01 01:54:35 +03:00
|
|
|
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_host_inode(BAN::RefPtr<Inode> inode)
|
2023-03-29 21:34:48 +03:00
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
LockGuard _(m_mutex);
|
2023-03-29 21:34:48 +03:00
|
|
|
for (MountPoint& mount : m_mount_points)
|
|
|
|
if (*mount.host.inode == *inode)
|
|
|
|
return &mount;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-04-01 01:54:35 +03:00
|
|
|
VirtualFileSystem::MountPoint* VirtualFileSystem::mount_from_root_inode(BAN::RefPtr<Inode> inode)
|
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
LockGuard _(m_mutex);
|
2023-04-01 01:54:35 +03:00
|
|
|
for (MountPoint& mount : m_mount_points)
|
|
|
|
if (*mount.target->root_inode() == *inode)
|
|
|
|
return &mount;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
BAN::ErrorOr<VirtualFileSystem::File> VirtualFileSystem::file_from_relative_path(const File& parent, const Credentials& credentials, BAN::StringView path, int flags)
|
2023-02-22 01:23:11 +02:00
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
LockGuard _(m_mutex);
|
2023-03-30 22:02:16 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
auto inode = parent.inode;
|
2023-04-11 23:25:21 +03:00
|
|
|
ASSERT(inode);
|
2023-03-09 02:31:24 +02:00
|
|
|
|
2023-03-29 21:34:48 +03:00
|
|
|
BAN::String canonical_path;
|
2024-09-14 19:43:44 +03:00
|
|
|
TRY(canonical_path.append(parent.canonical_path));
|
|
|
|
if (!canonical_path.empty() && canonical_path.back() == '/')
|
|
|
|
canonical_path.pop_back();
|
|
|
|
ASSERT(canonical_path.empty() || canonical_path.back() != '/');
|
2023-03-16 15:31:33 +02:00
|
|
|
|
2023-06-02 11:43:46 +03:00
|
|
|
BAN::Vector<BAN::String> path_parts;
|
2023-03-29 21:34:48 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
const auto append_string_view_in_reverse =
|
|
|
|
[&path_parts](BAN::StringView path) -> BAN::ErrorOr<void>
|
|
|
|
{
|
|
|
|
auto split_path = TRY(path.split('/'));
|
|
|
|
split_path.reverse();
|
|
|
|
for (auto part : split_path)
|
|
|
|
{
|
|
|
|
TRY(path_parts.emplace_back());
|
|
|
|
TRY(path_parts.back().append(part));
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
TRY(append_string_view_in_reverse(path));
|
2023-06-02 11:43:46 +03:00
|
|
|
|
2023-06-02 12:50:40 +03:00
|
|
|
size_t link_depth = 0;
|
|
|
|
|
2023-06-02 11:43:46 +03:00
|
|
|
while (!path_parts.empty())
|
|
|
|
{
|
2024-09-14 19:43:44 +03:00
|
|
|
BAN::String path_part = BAN::move(path_parts.back());
|
|
|
|
path_parts.pop_back();
|
2023-06-02 11:43:46 +03:00
|
|
|
|
2024-06-18 20:32:43 +03:00
|
|
|
if (path_part.empty() || path_part == "."_sv)
|
2024-09-14 19:43:44 +03:00
|
|
|
continue;
|
2023-06-02 11:43:46 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
auto orig = inode;
|
|
|
|
|
|
|
|
// resolve file name
|
2023-03-17 21:16:22 +02:00
|
|
|
{
|
2024-09-14 19:43:44 +03:00
|
|
|
auto parent_inode = inode;
|
|
|
|
if (path_part == ".."_sv)
|
|
|
|
if (auto* mount_point = mount_from_root_inode(inode))
|
|
|
|
parent_inode = mount_point->host.inode;
|
|
|
|
if (!parent_inode->can_access(credentials, O_SEARCH))
|
|
|
|
return BAN::Error::from_errno(EACCES);
|
|
|
|
inode = TRY(parent_inode->find_inode(path_part));
|
2023-03-29 21:34:48 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
if (path_part == ".."_sv)
|
2023-03-17 21:16:22 +02:00
|
|
|
{
|
2024-09-14 19:43:44 +03:00
|
|
|
if (!canonical_path.empty())
|
|
|
|
{
|
|
|
|
ASSERT(canonical_path.front() == '/');
|
|
|
|
while (canonical_path.back() != '/')
|
|
|
|
canonical_path.pop_back();
|
2023-03-29 21:34:48 +03:00
|
|
|
canonical_path.pop_back();
|
2024-09-14 19:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (auto* mount_point = mount_from_host_inode(inode))
|
|
|
|
inode = mount_point->target->root_inode();
|
|
|
|
TRY(canonical_path.push_back('/'));
|
|
|
|
TRY(canonical_path.append(path_part));
|
2023-03-17 21:16:22 +02:00
|
|
|
}
|
2023-06-02 11:43:46 +03:00
|
|
|
}
|
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
if (!inode->mode().iflnk())
|
|
|
|
continue;
|
|
|
|
if ((flags & O_NOFOLLOW) && path_parts.empty())
|
|
|
|
continue;
|
2023-06-02 11:43:46 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
// resolve symbolic links
|
2023-06-02 11:43:46 +03:00
|
|
|
{
|
2024-09-14 19:43:44 +03:00
|
|
|
auto link_target = TRY(inode->link_target());
|
|
|
|
if (link_target.empty())
|
2023-06-02 11:43:46 +03:00
|
|
|
return BAN::Error::from_errno(ENOENT);
|
2023-09-08 11:46:53 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
if (link_target.front() == '/')
|
2023-06-02 11:43:46 +03:00
|
|
|
{
|
|
|
|
inode = root_inode();
|
|
|
|
canonical_path.clear();
|
|
|
|
}
|
|
|
|
else
|
2023-06-01 00:24:45 +03:00
|
|
|
{
|
2023-06-02 11:43:46 +03:00
|
|
|
inode = orig;
|
|
|
|
|
|
|
|
while (canonical_path.back() != '/')
|
|
|
|
canonical_path.pop_back();
|
|
|
|
canonical_path.pop_back();
|
2023-06-01 00:24:45 +03:00
|
|
|
}
|
2023-06-02 12:50:40 +03:00
|
|
|
|
2024-09-14 19:43:44 +03:00
|
|
|
TRY(append_string_view_in_reverse(link_target.sv()));
|
|
|
|
|
2023-06-02 12:50:40 +03:00
|
|
|
link_depth++;
|
|
|
|
if (link_depth > 100)
|
|
|
|
return BAN::Error::from_errno(ELOOP);
|
2023-04-01 01:54:35 +03:00
|
|
|
}
|
2023-03-17 21:16:22 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 19:52:13 +03:00
|
|
|
if (!inode->can_access(credentials, flags))
|
|
|
|
return BAN::Error::from_errno(EACCES);
|
|
|
|
|
2023-03-29 21:34:48 +03:00
|
|
|
if (canonical_path.empty())
|
|
|
|
TRY(canonical_path.push_back('/'));
|
|
|
|
|
2023-03-17 21:16:22 +02:00
|
|
|
File file;
|
|
|
|
file.inode = inode;
|
2023-03-29 21:34:48 +03:00
|
|
|
file.canonical_path = BAN::move(canonical_path);
|
2023-03-17 21:16:22 +02:00
|
|
|
|
|
|
|
if (file.canonical_path.empty())
|
|
|
|
TRY(file.canonical_path.push_back('/'));
|
|
|
|
|
|
|
|
return file;
|
2023-02-22 01:23:11 +02:00
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|