VirtualFileSystem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <AK/Badge.h>
  3. #include <AK/Function.h>
  4. #include <AK/HashMap.h>
  5. #include <AK/NonnullOwnPtrVector.h>
  6. #include <AK/OwnPtr.h>
  7. #include <AK/RefPtr.h>
  8. #include <AK/String.h>
  9. #include <Kernel/FileSystem/FileSystem.h>
  10. #include <Kernel/FileSystem/InodeIdentifier.h>
  11. #include <Kernel/FileSystem/InodeMetadata.h>
  12. #include <Kernel/KResult.h>
  13. #define O_RDONLY 0
  14. #define O_WRONLY 1
  15. #define O_RDWR 2
  16. #define O_EXEC 4
  17. #define O_CREAT 0100
  18. #define O_EXCL 0200
  19. #define O_NOCTTY 0400
  20. #define O_TRUNC 01000
  21. #define O_APPEND 02000
  22. #define O_NONBLOCK 04000
  23. #define O_DIRECTORY 00200000
  24. #define O_NOFOLLOW 00400000
  25. #define O_CLOEXEC 02000000
  26. #define O_DIRECT 04000000
  27. #define O_NOFOLLOW_NOERROR 0x4000000
  28. #define MS_NODEV 1
  29. #define MS_NOEXEC 2
  30. #define MS_NOSUID 4
  31. #define MS_BIND 8
  32. class Custody;
  33. class Device;
  34. class FileDescription;
  35. struct UidAndGid {
  36. uid_t uid;
  37. gid_t gid;
  38. };
  39. class VFS {
  40. AK_MAKE_ETERNAL
  41. public:
  42. class Mount {
  43. public:
  44. Mount(FS&, Custody* host_custody, int flags);
  45. InodeIdentifier host() const;
  46. InodeIdentifier guest() const { return m_guest; }
  47. const FS& guest_fs() const { return *m_guest_fs; }
  48. String absolute_path() const;
  49. int flags() const { return m_flags; }
  50. private:
  51. InodeIdentifier m_host;
  52. InodeIdentifier m_guest;
  53. NonnullRefPtr<FS> m_guest_fs;
  54. RefPtr<Custody> m_host_custody;
  55. int m_flags;
  56. };
  57. static VFS& the();
  58. VFS();
  59. ~VFS();
  60. bool mount_root(FS&);
  61. KResult mount(FS&, Custody& mount_point, int flags);
  62. KResult unmount(InodeIdentifier guest_inode_id);
  63. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  64. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  65. KResult mkdir(StringView path, mode_t mode, Custody& base);
  66. KResult link(StringView old_path, StringView new_path, Custody& base);
  67. KResult unlink(StringView path, Custody& base);
  68. KResult symlink(StringView target, StringView linkpath, Custody& base);
  69. KResult rmdir(StringView path, Custody& base);
  70. KResult chmod(StringView path, mode_t, Custody& base);
  71. KResult chmod(Inode&, mode_t);
  72. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  73. KResult chown(Inode&, uid_t, gid_t);
  74. KResult access(StringView path, int mode, Custody& base);
  75. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  76. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  77. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  78. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  79. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  80. size_t mount_count() const { return m_mounts.size(); }
  81. void for_each_mount(Function<void(const Mount&)>) const;
  82. InodeIdentifier root_inode_id() const;
  83. void sync();
  84. Custody& root_custody();
  85. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  86. private:
  87. friend class FileDescription;
  88. RefPtr<Inode> get_inode(InodeIdentifier);
  89. bool is_vfs_root(InodeIdentifier) const;
  90. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  91. Mount* find_mount_for_host(InodeIdentifier);
  92. Mount* find_mount_for_guest(InodeIdentifier);
  93. Lock m_lock { "VFSLock" };
  94. RefPtr<Inode> m_root_inode;
  95. Vector<Mount> m_mounts;
  96. RefPtr<Custody> m_root_custody;
  97. };