/* * 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 namespace Kernel { // Kernel internal options. #define O_NOFOLLOW_NOERROR (1 << 29) #define O_UNLINK_INTERNAL (1 << 30) struct UidAndGid { UserID uid; GroupID gid; }; class VirtualFileSystem { AK_MAKE_ETERNAL public: 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(Inode& guest_inode); ErrorOr> open(StringView path, int options, mode_t mode, Custody& base, Optional = {}); ErrorOr> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional = {}); ErrorOr mkdir(StringView path, mode_t mode, Custody& base); ErrorOr link(StringView old_path, StringView new_path, Custody& base); ErrorOr unlink(StringView path, Custody& base); ErrorOr symlink(StringView target, StringView linkpath, Custody& base); ErrorOr rmdir(StringView path, Custody& base); ErrorOr chmod(StringView path, mode_t, Custody& base); ErrorOr chmod(Custody&, mode_t); ErrorOr chown(StringView path, UserID, GroupID, Custody& base); ErrorOr chown(Custody&, UserID, GroupID); ErrorOr access(StringView path, int mode, Custody& base); ErrorOr lookup_metadata(StringView path, Custody& base, int options = 0); ErrorOr utime(StringView path, Custody& base, time_t atime, time_t mtime); ErrorOr rename(StringView oldpath, StringView newpath, Custody& base); ErrorOr mknod(StringView path, mode_t, dev_t, Custody& base); ErrorOr> open_directory(StringView path, Custody& base); void for_each_mount(Function) const; InodeIdentifier root_inode_id() const; static void sync(); Custody& root_custody(); ErrorOr> resolve_path(StringView path, Custody& base, RefPtr* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0); ErrorOr> resolve_path_without_veil(StringView path, Custody& 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&)>); Mount* find_mount_for_host(InodeIdentifier); Mount* find_mount_for_guest(InodeIdentifier); RefPtr m_root_inode; RefPtr m_root_custody; MutexProtected> m_mounts; }; }