mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
Kernel+LibCore+LibC: Split the mount syscall into multiple syscalls
This is a preparation before we can create a usable mechanism to use filesystem-specific mount flags. To keep some compatibility with userland code, LibC and LibCore mount functions are kept being usable, but now instead of doing an "atomic" syscall, they do multiple syscalls to perform the complete procedure of mounting a filesystem. The FileBackedFileSystem IntrusiveList in the VFS code is now changed to be protected by a Mutex, because when we mount a new filesystem, we need to check if a filesystem is already created for a given source_fd so we do a scan for that OpenFileDescription in that list. If we fail to find an already-created filesystem we create a new one and register it in the list if we successfully mounted it. We use a Mutex because we might need to initiate disk access during the filesystem creation, which will take other mutexes in other parts of the kernel, therefore making it not possible to take a spinlock while doing this.
This commit is contained in:
parent
63fc36d00d
commit
23a7ccf607
39 changed files with 651 additions and 214 deletions
|
@ -1,120 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/FileSystem/Custody.h>
|
||||
#include <Kernel/FileSystem/DevPtsFS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/Ext2FS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/FATFS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/ISO9660FS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/Plan9FS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/ProcFS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/RAMFS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/SysFS/FileSystem.h>
|
||||
#include <Kernel/FileSystem/MountFile.h>
|
||||
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
||||
#include <Kernel/Tasks/Process.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
struct FileSystemInitializer {
|
||||
StringView short_name;
|
||||
StringView name;
|
||||
bool requires_open_file_description { false };
|
||||
bool requires_block_device { false };
|
||||
bool requires_seekable_file { false };
|
||||
ErrorOr<NonnullRefPtr<FileSystem>> (*create_with_fd)(OpenFileDescription&) = nullptr;
|
||||
ErrorOr<NonnullRefPtr<FileSystem>> (*create)(void) = nullptr;
|
||||
};
|
||||
|
||||
static constexpr FileSystemInitializer s_initializers[] = {
|
||||
{ "proc"sv, "ProcFS"sv, false, false, false, {}, ProcFS::try_create },
|
||||
{ "devpts"sv, "DevPtsFS"sv, false, false, false, {}, DevPtsFS::try_create },
|
||||
{ "sys"sv, "SysFS"sv, false, false, false, {}, SysFS::try_create },
|
||||
{ "ram"sv, "RAMFS"sv, false, false, false, {}, RAMFS::try_create },
|
||||
{ "ext2"sv, "Ext2FS"sv, true, true, true, Ext2FS::try_create, {} },
|
||||
{ "9p"sv, "Plan9FS"sv, true, true, true, Plan9FS::try_create, {} },
|
||||
{ "iso9660"sv, "ISO9660FS"sv, true, true, true, ISO9660FS::try_create, {} },
|
||||
{ "fat"sv, "FATFS"sv, true, true, true, FATFS::try_create, {} },
|
||||
};
|
||||
|
||||
static ErrorOr<NonnullRefPtr<FileSystem>> find_or_create_filesystem_instance(StringView fs_type, OpenFileDescription* possible_description)
|
||||
ErrorOr<FlatPtr> Process::sys$fsopen(Userspace<Syscall::SC_fsopen_params const*> user_params)
|
||||
{
|
||||
for (auto& initializer_entry : s_initializers) {
|
||||
if (fs_type != initializer_entry.short_name && fs_type != initializer_entry.name)
|
||||
continue;
|
||||
if (!initializer_entry.requires_open_file_description) {
|
||||
VERIFY(initializer_entry.create);
|
||||
NonnullRefPtr<FileSystem> fs = TRY(initializer_entry.create());
|
||||
return fs;
|
||||
}
|
||||
// Note: If there's an associated file description with the filesystem, we could
|
||||
// try to first find it from the VirtualFileSystem filesystem list and if it was not found,
|
||||
// then create it and add it.
|
||||
VERIFY(initializer_entry.create_with_fd);
|
||||
if (!possible_description)
|
||||
return EBADF;
|
||||
OpenFileDescription& description = *possible_description;
|
||||
|
||||
if (initializer_entry.requires_block_device && !description.file().is_block_device())
|
||||
return ENOTBLK;
|
||||
if (initializer_entry.requires_seekable_file && !description.file().is_seekable()) {
|
||||
dbgln("mount: this is not a seekable file");
|
||||
return ENODEV;
|
||||
}
|
||||
return TRY(VirtualFileSystem::the().find_already_existing_or_create_file_backed_file_system(description, initializer_entry.create_with_fd));
|
||||
}
|
||||
return ENODEV;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$mount(Userspace<Syscall::SC_mount_params const*> user_params)
|
||||
{
|
||||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
||||
TRY(require_no_promises());
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
TRY(require_promise(Pledge::mount));
|
||||
auto credentials = this->credentials();
|
||||
if (!credentials->is_superuser())
|
||||
return EPERM;
|
||||
return Error::from_errno(EPERM);
|
||||
auto params = TRY(copy_typed_from_user(user_params));
|
||||
auto fs_type_string = TRY(try_copy_kstring_from_user(params.fs_type));
|
||||
|
||||
// NOTE: If some userspace program uses MS_REMOUNT, return EINVAL to indicate that we never want this
|
||||
// flag to appear in the mount table...
|
||||
if (params.flags & MS_REMOUNT || params.flags & MS_BIND)
|
||||
return Error::from_errno(EINVAL);
|
||||
|
||||
auto const* fs_type_initializer = TRY(VirtualFileSystem::find_filesystem_type_initializer(fs_type_string->view()));
|
||||
VERIFY(fs_type_initializer);
|
||||
auto mount_file = TRY(MountFile::create(*fs_type_initializer, params.flags));
|
||||
auto description = TRY(OpenFileDescription::try_create(move(mount_file)));
|
||||
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
||||
auto new_fd = TRY(fds.allocate());
|
||||
fds[new_fd.fd].set(move(description), FD_CLOEXEC);
|
||||
return new_fd.fd;
|
||||
});
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$fsmount(Userspace<Syscall::SC_fsmount_params const*> user_params)
|
||||
{
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
TRY(require_promise(Pledge::mount));
|
||||
auto credentials = this->credentials();
|
||||
if (!credentials->is_superuser())
|
||||
return Error::from_errno(EPERM);
|
||||
|
||||
auto params = TRY(copy_typed_from_user(user_params));
|
||||
if (params.flags & MS_REMOUNT)
|
||||
return EINVAL;
|
||||
if (params.flags & MS_BIND)
|
||||
return EINVAL;
|
||||
auto mount_description = TRY(open_file_description(params.mount_fd));
|
||||
if (!mount_description->is_mount_file())
|
||||
return Error::from_errno(EINVAL);
|
||||
|
||||
auto source_fd = params.source_fd;
|
||||
RefPtr<OpenFileDescription> source_description = TRY(open_file_description_ignoring_negative(params.source_fd));
|
||||
auto target = TRY(try_copy_kstring_from_user(params.target));
|
||||
auto fs_type_string = TRY(try_copy_kstring_from_user(params.fs_type));
|
||||
auto fs_type = fs_type_string->view();
|
||||
|
||||
auto description_or_error = open_file_description(source_fd);
|
||||
if (!description_or_error.is_error())
|
||||
dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
|
||||
else
|
||||
dbgln("mount {} @ {}", fs_type, target);
|
||||
|
||||
auto target_custody = TRY(VirtualFileSystem::the().resolve_path(credentials, target->view(), current_directory()));
|
||||
|
||||
RefPtr<FileSystem> fs;
|
||||
|
||||
if (!description_or_error.is_error()) {
|
||||
auto description = description_or_error.release_value();
|
||||
fs = TRY(find_or_create_filesystem_instance(fs_type, description.ptr()));
|
||||
auto source_pseudo_path = TRY(description->pseudo_path());
|
||||
dbgln("mount: attempting to mount {} on {}", source_pseudo_path, target);
|
||||
} else {
|
||||
fs = TRY(find_or_create_filesystem_instance(fs_type, {}));
|
||||
}
|
||||
|
||||
TRY(fs->initialize());
|
||||
TRY(VirtualFileSystem::the().mount(*fs, target_custody, params.flags));
|
||||
auto flags = mount_description->mount_file()->mount_flags();
|
||||
TRY(VirtualFileSystem::the().mount(*mount_description->mount_file(), source_description.ptr(), target_custody, flags));
|
||||
return 0;
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> Process::sys$remount(Userspace<Syscall::SC_remount_params const*> user_params)
|
||||
{
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
TRY(require_no_promises());
|
||||
TRY(require_promise(Pledge::mount));
|
||||
auto credentials = this->credentials();
|
||||
if (!credentials->is_superuser())
|
||||
return EPERM;
|
||||
|
@ -134,7 +82,7 @@ ErrorOr<FlatPtr> Process::sys$remount(Userspace<Syscall::SC_remount_params const
|
|||
ErrorOr<FlatPtr> Process::sys$bindmount(Userspace<Syscall::SC_bindmount_params const*> user_params)
|
||||
{
|
||||
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
||||
TRY(require_no_promises());
|
||||
TRY(require_promise(Pledge::mount));
|
||||
auto credentials = this->credentials();
|
||||
if (!credentials->is_superuser())
|
||||
return EPERM;
|
||||
|
@ -166,7 +114,7 @@ ErrorOr<FlatPtr> Process::sys$umount(Userspace<char const*> user_mountpoint, siz
|
|||
if (!credentials->is_superuser())
|
||||
return EPERM;
|
||||
|
||||
TRY(require_no_promises());
|
||||
TRY(require_promise(Pledge::mount));
|
||||
|
||||
auto mountpoint = TRY(get_syscall_path_argument(user_mountpoint, mountpoint_length));
|
||||
auto custody = TRY(VirtualFileSystem::the().resolve_path(credentials, mountpoint->view(), current_directory()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue