Serenity Operating System
at master 127 lines 6.4 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/Badge.h> 10#include <AK/Error.h> 11#include <AK/Function.h> 12#include <AK/HashMap.h> 13#include <AK/OwnPtr.h> 14#include <AK/RefPtr.h> 15#include <Kernel/FileSystem/FileBackedFileSystem.h> 16#include <Kernel/FileSystem/FileSystem.h> 17#include <Kernel/FileSystem/InodeIdentifier.h> 18#include <Kernel/FileSystem/InodeMetadata.h> 19#include <Kernel/FileSystem/Mount.h> 20#include <Kernel/FileSystem/UnveilNode.h> 21#include <Kernel/Forward.h> 22#include <Kernel/Library/LockRefPtr.h> 23#include <Kernel/Locking/SpinlockProtected.h> 24 25namespace Kernel { 26 27// Kernel internal options. 28#define O_NOFOLLOW_NOERROR (1 << 29) 29#define O_UNLINK_INTERNAL (1 << 30) 30 31struct UidAndGid { 32 UserID uid; 33 GroupID gid; 34}; 35 36enum class AccessFlags { 37 None = 0, 38 EffectiveAccess = 1 << 0, 39 DoNotFollowSymlinks = 1 << 1, 40}; 41 42AK_ENUM_BITWISE_OPERATORS(AccessFlags); 43 44class VirtualFileSystem { 45public: 46 // Required to be at least 8 by POSIX 47 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html 48 static constexpr int symlink_recursion_limit = 8; 49 50 static void initialize(); 51 static VirtualFileSystem& the(); 52 53 VirtualFileSystem(); 54 ~VirtualFileSystem(); 55 56 ErrorOr<void> mount_root(FileSystem&); 57 ErrorOr<void> mount(FileSystem&, Custody& mount_point, int flags); 58 ErrorOr<void> bind_mount(Custody& source, Custody& mount_point, int flags); 59 ErrorOr<void> remount(Custody& mount_point, int new_flags); 60 ErrorOr<void> unmount(Custody& mount_point); 61 62 ErrorOr<NonnullRefPtr<OpenFileDescription>> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {}); 63 ErrorOr<NonnullRefPtr<OpenFileDescription>> open(Process const&, Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {}); 64 ErrorOr<NonnullRefPtr<OpenFileDescription>> create(Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {}); 65 ErrorOr<NonnullRefPtr<OpenFileDescription>> create(Process const&, Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {}); 66 ErrorOr<void> mkdir(Credentials const&, StringView path, mode_t mode, Custody& base); 67 ErrorOr<void> link(Credentials const&, StringView old_path, StringView new_path, Custody& base); 68 ErrorOr<void> unlink(Credentials const&, StringView path, Custody& base); 69 ErrorOr<void> symlink(Credentials const&, StringView target, StringView linkpath, Custody& base); 70 ErrorOr<void> rmdir(Credentials const&, StringView path, Custody& base); 71 ErrorOr<void> chmod(Credentials const&, StringView path, mode_t, Custody& base, int options = 0); 72 ErrorOr<void> chmod(Credentials const&, Custody&, mode_t); 73 ErrorOr<void> chown(Credentials const&, StringView path, UserID, GroupID, Custody& base, int options); 74 ErrorOr<void> chown(Credentials const&, Custody&, UserID, GroupID); 75 ErrorOr<void> access(Credentials const&, StringView path, int mode, Custody& base, AccessFlags); 76 ErrorOr<InodeMetadata> lookup_metadata(Credentials const&, StringView path, Custody& base, int options = 0); 77 ErrorOr<void> utime(Credentials const&, StringView path, Custody& base, time_t atime, time_t mtime); 78 ErrorOr<void> utimensat(Credentials const&, StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0); 79 ErrorOr<void> rename(Credentials const&, Custody& old_base, StringView oldpath, Custody& new_base, StringView newpath); 80 ErrorOr<void> mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base); 81 ErrorOr<NonnullRefPtr<Custody>> open_directory(Credentials const&, StringView path, Custody& base); 82 83 ErrorOr<void> for_each_mount(Function<ErrorOr<void>(Mount const&)>) const; 84 85 ErrorOr<NonnullLockRefPtr<FileBackedFileSystem>> find_already_existing_or_create_file_backed_file_system(OpenFileDescription& description, Function<ErrorOr<NonnullLockRefPtr<FileSystem>>(OpenFileDescription&)> callback); 86 87 InodeIdentifier root_inode_id() const; 88 89 void sync_filesystems(); 90 void lock_all_filesystems(); 91 92 static void sync(); 93 94 NonnullRefPtr<Custody> root_custody(); 95 ErrorOr<NonnullRefPtr<Custody>> resolve_path(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); 96 ErrorOr<NonnullRefPtr<Custody>> resolve_path(Process const&, Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); 97 ErrorOr<NonnullRefPtr<Custody>> resolve_path_without_veil(Credentials const&, StringView path, NonnullRefPtr<Custody> base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); 98 99private: 100 friend class OpenFileDescription; 101 102 UnveilNode const& find_matching_unveiled_path(Process const&, StringView path); 103 ErrorOr<void> validate_path_against_process_veil(Process const&, StringView path, int options); 104 ErrorOr<void> validate_path_against_process_veil(Process const& process, Custody const& custody, int options); 105 ErrorOr<void> validate_path_against_process_veil(Custody const& path, int options); 106 ErrorOr<void> validate_path_against_process_veil(StringView path, int options); 107 108 bool is_vfs_root(InodeIdentifier) const; 109 110 ErrorOr<void> traverse_directory_inode(Inode&, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>); 111 112 bool mount_point_exists_at_inode(InodeIdentifier inode); 113 114 // FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us. 115 Mount* find_mount_for_host(InodeIdentifier); 116 Mount* find_mount_for_guest(InodeIdentifier); 117 118 RefPtr<Inode> m_root_inode; 119 120 SpinlockProtected<RefPtr<Custody>, LockRank::None> m_root_custody {}; 121 122 SpinlockProtected<IntrusiveList<&Mount::m_vfs_list_node>, LockRank::None> m_mounts {}; 123 SpinlockProtected<IntrusiveList<&FileBackedFileSystem::m_file_backed_file_system_node>, LockRank::None> m_file_backed_file_systems_list {}; 124 SpinlockProtected<IntrusiveList<&FileSystem::m_file_system_node>, LockRank::FileSystem> m_file_systems_list {}; 125}; 126 127}