/* * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Kernel { // Kernel internal options. #define O_NOFOLLOW_NOERROR (1 << 29) #define O_UNLINK_INTERNAL (1 << 30) struct UidAndGid { UserID uid; GroupID gid; }; enum class AccessFlags { None = 0, EffectiveAccess = 1 << 0, DoNotFollowSymlinks = 1 << 1, }; AK_ENUM_BITWISE_OPERATORS(AccessFlags); class VirtualFileSystem { public: // Required to be at least 8 by POSIX // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html static constexpr int symlink_recursion_limit = 8; static void initialize(); static VirtualFileSystem& the(); VirtualFileSystem(); ~VirtualFileSystem(); ErrorOr mount_root(FileSystem&); ErrorOr mount(FileSystem&, Custody& mount_point, int flags); ErrorOr bind_mount(Custody& source, Custody& mount_point, int flags); ErrorOr remount(Custody& mount_point, int new_flags); ErrorOr unmount(Custody& mount_point); ErrorOr> open(Credentials const&, StringView path, int options, mode_t mode, Custody& base, Optional = {}); ErrorOr> create(Credentials const&, StringView path, int options, mode_t mode, Custody& parent_custody, Optional = {}); ErrorOr mkdir(Credentials const&, StringView path, mode_t mode, Custody& base); ErrorOr link(Credentials const&, StringView old_path, StringView new_path, Custody& base); ErrorOr unlink(Credentials const&, StringView path, Custody& base); ErrorOr symlink(Credentials const&, StringView target, StringView linkpath, Custody& base); ErrorOr rmdir(Credentials const&, StringView path, Custody& base); ErrorOr chmod(Credentials const&, StringView path, mode_t, Custody& base, int options = 0); ErrorOr chmod(Credentials const&, Custody&, mode_t); ErrorOr chown(Credentials const&, StringView path, UserID, GroupID, Custody& base, int options); ErrorOr chown(Credentials const&, Custody&, UserID, GroupID); ErrorOr access(Credentials const&, StringView path, int mode, Custody& base, AccessFlags); ErrorOr lookup_metadata(Credentials const&, StringView path, Custody& base, int options = 0); ErrorOr utime(Credentials const&, StringView path, Custody& base, time_t atime, time_t mtime); ErrorOr utimensat(Credentials const&, StringView path, Custody& base, timespec const& atime, timespec const& mtime, int options = 0); ErrorOr rename(Credentials const&, Custody& old_base, StringView oldpath, Custody& new_base, StringView newpath); ErrorOr mknod(Credentials const&, StringView path, mode_t, dev_t, Custody& base); ErrorOr> open_directory(Credentials const&, StringView path, Custody& base); ErrorOr for_each_mount(Function(Mount const&)>) const; ErrorOr> find_already_existing_or_create_file_backed_file_system(OpenFileDescription& description, Function>(OpenFileDescription&)> callback); InodeIdentifier root_inode_id() const; void sync_filesystems(); void lock_all_filesystems(); static void sync(); NonnullRefPtr root_custody(); ErrorOr> resolve_path(Credentials const&, StringView path, NonnullRefPtr base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); ErrorOr> resolve_path_without_veil(Credentials const&, StringView path, NonnullRefPtr base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); private: friend class OpenFileDescription; UnveilNode const& find_matching_unveiled_path(StringView path); ErrorOr validate_path_against_process_veil(Custody const& path, int options); ErrorOr validate_path_against_process_veil(StringView path, int options); bool is_vfs_root(InodeIdentifier) const; ErrorOr traverse_directory_inode(Inode&, Function(FileSystem::DirectoryEntryView const&)>); bool mount_point_exists_at_inode(InodeIdentifier inode); // FIXME: These functions are totally unsafe as someone could unmount the returned Mount underneath us. Mount* find_mount_for_host(InodeIdentifier); Mount* find_mount_for_guest(InodeIdentifier); LockRefPtr m_root_inode; SpinlockProtected, LockRank::None> m_root_custody {}; SpinlockProtected, LockRank::None> m_mounts {}; SpinlockProtected, LockRank::None> m_file_backed_file_systems_list {}; SpinlockProtected, LockRank::FileSystem> m_file_systems_list {}; }; }