VirtualFileSystem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2018-2020, 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/KResult.h>
  18. #include <Kernel/UnveilNode.h>
  19. namespace Kernel {
  20. class Custody;
  21. class Device;
  22. class FileDescription;
  23. struct UidAndGid {
  24. uid_t uid;
  25. gid_t gid;
  26. };
  27. class VFS {
  28. AK_MAKE_ETERNAL
  29. public:
  30. class Mount {
  31. public:
  32. Mount(FS&, Custody* host_custody, int flags);
  33. Mount(Inode& source, Custody& host_custody, int flags);
  34. const Inode* host() const;
  35. Inode* host();
  36. const Inode& guest() const { return *m_guest; }
  37. Inode& guest() { return *m_guest; }
  38. const FS& guest_fs() const { return *m_guest_fs; }
  39. String absolute_path() const;
  40. int flags() const { return m_flags; }
  41. void set_flags(int flags) { m_flags = flags; }
  42. private:
  43. NonnullRefPtr<Inode> m_guest;
  44. NonnullRefPtr<FS> m_guest_fs;
  45. RefPtr<Custody> m_host_custody;
  46. int m_flags;
  47. };
  48. static void initialize();
  49. static VFS& the();
  50. VFS();
  51. ~VFS();
  52. bool mount_root(FS&);
  53. KResult mount(FS&, Custody& mount_point, int flags);
  54. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  55. KResult remount(Custody& mount_point, int new_flags);
  56. KResult unmount(Inode& guest_inode);
  57. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  58. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  59. KResult mkdir(StringView path, mode_t mode, Custody& base);
  60. KResult link(StringView old_path, StringView new_path, Custody& base);
  61. KResult unlink(StringView path, Custody& base);
  62. KResult symlink(StringView target, StringView linkpath, Custody& base);
  63. KResult rmdir(StringView path, Custody& base);
  64. KResult chmod(StringView path, mode_t, Custody& base);
  65. KResult chmod(Custody&, mode_t);
  66. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  67. KResult chown(Custody&, uid_t, gid_t);
  68. KResult access(StringView path, int mode, Custody& base);
  69. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  70. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  71. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  72. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  73. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  74. size_t mount_count() const { return m_mounts.size(); }
  75. void for_each_mount(Function<void(const Mount&)>) const;
  76. InodeIdentifier root_inode_id() const;
  77. void sync();
  78. Custody& root_custody();
  79. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  80. KResultOr<NonnullRefPtr<Custody>> resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  81. private:
  82. friend class FileDescription;
  83. UnveilNode const& find_matching_unveiled_path(StringView path);
  84. KResult validate_path_against_process_veil(StringView path, int options);
  85. bool is_vfs_root(InodeIdentifier) const;
  86. KResult traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntryView&)>);
  87. Mount* find_mount_for_host(Inode&);
  88. Mount* find_mount_for_host(InodeIdentifier);
  89. Mount* find_mount_for_guest(Inode&);
  90. Mount* find_mount_for_guest(InodeIdentifier);
  91. Lock m_lock { "VFSLock" };
  92. RefPtr<Inode> m_root_inode;
  93. Vector<Mount, 16> m_mounts;
  94. RefPtr<Custody> m_root_custody;
  95. };
  96. }