2020-07-31 01:01:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 01:01:41 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/DevPtsFS.h>
|
2021-08-14 16:28:09 +03:00
|
|
|
#include <Kernel/FileSystem/DevTmpFS.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/Ext2FileSystem.h>
|
2021-07-29 20:19:12 +00:00
|
|
|
#include <Kernel/FileSystem/ISO9660FileSystem.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/Plan9FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/ProcFS.h>
|
2021-03-13 12:01:44 +02:00
|
|
|
#include <Kernel/FileSystem/SysFS.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/TmpFS.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*> user_params)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!is_superuser())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 17:51:37 +02:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
auto source_fd = params.source_fd;
|
2021-09-05 18:20:57 +02:00
|
|
|
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();
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-01-29 01:22:28 +01:00
|
|
|
auto description_or_error = open_file_description(source_fd);
|
2021-09-05 18:34:28 +02:00
|
|
|
if (!description_or_error.is_error())
|
2021-01-10 15:43:09 +01:00
|
|
|
dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
|
2020-07-30 23:38:15 +02:00
|
|
|
else
|
2021-01-10 15:43:09 +01:00
|
|
|
dbgln("mount {} @ {}", fs_type, target);
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 18:20:57 +02:00
|
|
|
auto target_custody = TRY(VirtualFileSystem::the().resolve_path(target->view(), current_directory()));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (params.flags & MS_REMOUNT) {
|
|
|
|
// We're not creating a new mount, we're updating an existing one!
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(VirtualFileSystem::the().remount(target_custody, params.flags & ~MS_REMOUNT));
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (params.flags & MS_BIND) {
|
|
|
|
// We're doing a bind mount.
|
2021-09-05 18:34:28 +02:00
|
|
|
if (description_or_error.is_error())
|
2021-11-08 00:51:39 +01:00
|
|
|
return description_or_error.release_error();
|
2021-09-05 18:34:28 +02:00
|
|
|
auto description = description_or_error.release_value();
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description->custody()) {
|
|
|
|
// We only support bind-mounting inodes, not arbitrary files.
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENODEV;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(VirtualFileSystem::the().bind_mount(*description->custody(), target_custody, params.flags));
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
RefPtr<FileSystem> fs;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-07-23 08:19:32 -07:00
|
|
|
if (fs_type == "ext2"sv || fs_type == "Ext2FS"sv) {
|
2021-09-05 18:34:28 +02:00
|
|
|
if (description_or_error.is_error())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2021-09-05 18:34:28 +02:00
|
|
|
auto description = description_or_error.release_value();
|
2021-01-28 23:54:29 +01:00
|
|
|
if (!description->file().is_block_device())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENOTBLK;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!description->file().is_seekable()) {
|
2021-01-09 18:51:44 +01:00
|
|
|
dbgln("mount: this is not a seekable file");
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENODEV;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 00:45:23 +02:00
|
|
|
auto source_pseudo_path = TRY(description->pseudo_path());
|
|
|
|
dbgln("mount: attempting to mount {} on {}", source_pseudo_path, target);
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-06 11:14:25 +02:00
|
|
|
fs = TRY(Ext2FS::try_create(*description));
|
2021-07-23 08:19:32 -07:00
|
|
|
} else if (fs_type == "9p"sv || fs_type == "Plan9FS"sv) {
|
2021-09-05 18:34:28 +02:00
|
|
|
if (description_or_error.is_error())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2021-09-05 18:34:28 +02:00
|
|
|
auto description = description_or_error.release_value();
|
2021-09-06 11:17:53 +02:00
|
|
|
fs = TRY(Plan9FS::try_create(*description));
|
2021-07-23 08:19:32 -07:00
|
|
|
} else if (fs_type == "proc"sv || fs_type == "ProcFS"sv) {
|
2021-09-05 18:20:57 +02:00
|
|
|
fs = TRY(ProcFS::try_create());
|
2021-07-23 08:19:32 -07:00
|
|
|
} else if (fs_type == "devpts"sv || fs_type == "DevPtsFS"sv) {
|
2021-09-06 10:42:09 +02:00
|
|
|
fs = TRY(DevPtsFS::try_create());
|
2021-08-14 16:28:09 +03:00
|
|
|
} else if (fs_type == "dev"sv || fs_type == "DevTmpFS"sv) {
|
|
|
|
fs = TRY(DevTmpFS::try_create());
|
2021-07-23 08:19:32 -07:00
|
|
|
} else if (fs_type == "sys"sv || fs_type == "SysFS"sv) {
|
2021-09-06 11:06:20 +02:00
|
|
|
fs = TRY(SysFS::try_create());
|
2021-07-23 08:19:32 -07:00
|
|
|
} else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) {
|
2021-09-06 02:28:38 +02:00
|
|
|
fs = TRY(TmpFS::try_create());
|
2021-07-29 20:19:12 +00:00
|
|
|
} else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) {
|
2021-09-05 18:34:28 +02:00
|
|
|
if (description_or_error.is_error())
|
2021-07-29 20:19:12 +00:00
|
|
|
return EBADF;
|
2021-09-05 18:34:28 +02:00
|
|
|
auto description = description_or_error.release_value();
|
2021-07-29 20:19:12 +00:00
|
|
|
if (!description->file().is_seekable()) {
|
|
|
|
dbgln("mount: this is not a seekable file");
|
|
|
|
return ENODEV;
|
|
|
|
}
|
2021-10-30 00:45:23 +02:00
|
|
|
auto source_pseudo_path = TRY(description->pseudo_path());
|
|
|
|
dbgln("mount: attempting to mount {} on {}", source_pseudo_path, target);
|
2021-09-05 18:20:57 +02:00
|
|
|
fs = TRY(ISO9660FS::try_create(*description));
|
2020-07-30 23:38:15 +02:00
|
|
|
} else {
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENODEV;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:42:03 -07:00
|
|
|
if (!fs)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2021-09-05 18:20:57 +02:00
|
|
|
TRY(fs->initialize());
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(VirtualFileSystem::the().mount(fs.release_nonnull(), target_custody, params.flags));
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$umount(Userspace<const char*> user_mountpoint, size_t mountpoint_length)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!is_superuser())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 18:18:23 +02:00
|
|
|
auto mountpoint = TRY(get_syscall_path_argument(user_mountpoint, mountpoint_length));
|
|
|
|
auto custody = TRY(VirtualFileSystem::the().resolve_path(mountpoint->view(), current_directory()));
|
|
|
|
auto& guest_inode = custody->inode();
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(VirtualFileSystem::the().unmount(guest_inode));
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|