VirtualFileSystem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/NonnullOwnPtrVector.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/String.h>
  14. #include <Kernel/FileSystem/FileSystem.h>
  15. #include <Kernel/FileSystem/InodeIdentifier.h>
  16. #include <Kernel/FileSystem/InodeMetadata.h>
  17. #include <Kernel/FileSystem/Mount.h>
  18. #include <Kernel/FileSystem/UnveilNode.h>
  19. #include <Kernel/Forward.h>
  20. #include <Kernel/KResult.h>
  21. namespace Kernel {
  22. // Kernel internal options.
  23. #define O_NOFOLLOW_NOERROR (1 << 29)
  24. #define O_UNLINK_INTERNAL (1 << 30)
  25. struct UidAndGid {
  26. uid_t uid;
  27. gid_t gid;
  28. };
  29. class VirtualFileSystem {
  30. AK_MAKE_ETERNAL
  31. public:
  32. static void initialize();
  33. static VirtualFileSystem& the();
  34. VirtualFileSystem();
  35. ~VirtualFileSystem();
  36. bool mount_root(FileSystem&);
  37. KResult mount(FileSystem&, Custody& mount_point, int flags);
  38. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  39. KResult remount(Custody& mount_point, int new_flags);
  40. KResult unmount(Inode& guest_inode);
  41. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  42. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  43. KResult mkdir(StringView path, mode_t mode, Custody& base);
  44. KResult link(StringView old_path, StringView new_path, Custody& base);
  45. KResult unlink(StringView path, Custody& base);
  46. KResult symlink(StringView target, StringView linkpath, Custody& base);
  47. KResult rmdir(StringView path, Custody& base);
  48. KResult chmod(StringView path, mode_t, Custody& base);
  49. KResult chmod(Custody&, mode_t);
  50. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  51. KResult chown(Custody&, uid_t, gid_t);
  52. KResult access(StringView path, int mode, Custody& base);
  53. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  54. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  55. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  56. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  57. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  58. size_t mount_count() const { return m_mounts.size(); }
  59. void for_each_mount(Function<void(const Mount&)>) const;
  60. InodeIdentifier root_inode_id() const;
  61. static void sync();
  62. Custody& root_custody();
  63. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  64. KResultOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  65. private:
  66. friend class FileDescription;
  67. UnveilNode const& find_matching_unveiled_path(StringView path);
  68. KResult validate_path_against_process_veil(Custody const& path, int options);
  69. KResult validate_path_against_process_veil(StringView path, int options);
  70. bool is_vfs_root(InodeIdentifier) const;
  71. KResult traverse_directory_inode(Inode&, Function<bool(FileSystem::DirectoryEntryView const&)>);
  72. Mount* find_mount_for_host(InodeIdentifier);
  73. Mount* find_mount_for_guest(InodeIdentifier);
  74. Mutex m_lock { "VFSLock" };
  75. RefPtr<Inode> m_root_inode;
  76. Vector<Mount, 16> m_mounts;
  77. RefPtr<Custody> m_root_custody;
  78. };
  79. }