VirtualFileSystem.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/Function.h>
  29. #include <AK/HashMap.h>
  30. #include <AK/NonnullOwnPtrVector.h>
  31. #include <AK/OwnPtr.h>
  32. #include <AK/RefPtr.h>
  33. #include <AK/String.h>
  34. #include <Kernel/FileSystem/FileSystem.h>
  35. #include <Kernel/FileSystem/InodeIdentifier.h>
  36. #include <Kernel/FileSystem/InodeMetadata.h>
  37. #include <Kernel/KResult.h>
  38. #define O_RDONLY (1 << 0)
  39. #define O_WRONLY (1 << 1)
  40. #define O_RDWR (O_RDONLY | O_WRONLY)
  41. #define O_ACCMODE (O_RDONLY | O_WRONLY)
  42. #define O_EXEC (1 << 2)
  43. #define O_CREAT (1 << 3)
  44. #define O_EXCL (1 << 4)
  45. #define O_NOCTTY (1 << 5)
  46. #define O_TRUNC (1 << 6)
  47. #define O_APPEND (1 << 7)
  48. #define O_NONBLOCK (1 << 8)
  49. #define O_DIRECTORY (1 << 9)
  50. #define O_NOFOLLOW (1 << 10)
  51. #define O_CLOEXEC (1 << 11)
  52. #define O_DIRECT (1 << 12)
  53. // Kernel internal options
  54. #define O_NOFOLLOW_NOERROR (1 << 29)
  55. #define O_UNLINK_INTERNAL (1 << 30)
  56. #define MS_NODEV 1
  57. #define MS_NOEXEC 2
  58. #define MS_NOSUID 4
  59. #define MS_BIND 8
  60. class Custody;
  61. class Device;
  62. class FileDescription;
  63. class UnveiledPath;
  64. struct UidAndGid {
  65. uid_t uid;
  66. gid_t gid;
  67. };
  68. class VFS {
  69. AK_MAKE_ETERNAL
  70. public:
  71. class Mount {
  72. public:
  73. Mount(FS&, Custody* host_custody, int flags);
  74. Mount(Inode& source, Custody& host_custody, int flags);
  75. InodeIdentifier host() const;
  76. InodeIdentifier guest() const { return m_guest; }
  77. const FS& guest_fs() const { return *m_guest_fs; }
  78. String absolute_path() const;
  79. int flags() const { return m_flags; }
  80. private:
  81. InodeIdentifier m_host;
  82. InodeIdentifier m_guest;
  83. NonnullRefPtr<FS> m_guest_fs;
  84. RefPtr<Custody> m_host_custody;
  85. int m_flags;
  86. };
  87. static VFS& the();
  88. VFS();
  89. ~VFS();
  90. bool mount_root(FS&);
  91. KResult mount(FS&, Custody& mount_point, int flags);
  92. KResult bind_mount(Custody& source, Custody& mount_point, int flags);
  93. KResult unmount(InodeIdentifier guest_inode_id);
  94. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> = {});
  95. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> = {});
  96. KResult mkdir(StringView path, mode_t mode, Custody& base);
  97. KResult link(StringView old_path, StringView new_path, Custody& base);
  98. KResult unlink(StringView path, Custody& base);
  99. KResult symlink(StringView target, StringView linkpath, Custody& base);
  100. KResult rmdir(StringView path, Custody& base);
  101. KResult chmod(StringView path, mode_t, Custody& base);
  102. KResult chmod(Inode&, mode_t);
  103. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  104. KResult chown(Inode&, uid_t, gid_t);
  105. KResult access(StringView path, int mode, Custody& base);
  106. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  107. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  108. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  109. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  110. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  111. size_t mount_count() const { return m_mounts.size(); }
  112. void for_each_mount(Function<void(const Mount&)>) const;
  113. InodeIdentifier root_inode_id() const;
  114. void sync();
  115. Custody& root_custody();
  116. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  117. private:
  118. friend class FileDescription;
  119. const UnveiledPath* find_matching_unveiled_path(StringView path);
  120. KResult validate_path_against_process_veil(StringView path, int options);
  121. RefPtr<Inode> get_inode(InodeIdentifier);
  122. bool is_vfs_root(InodeIdentifier) const;
  123. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  124. Mount* find_mount_for_host(InodeIdentifier);
  125. Mount* find_mount_for_guest(InodeIdentifier);
  126. Lock m_lock { "VFSLock" };
  127. RefPtr<Inode> m_root_inode;
  128. Vector<Mount> m_mounts;
  129. RefPtr<Custody> m_root_custody;
  130. };