Inode.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Function.h>
  28. #include <AK/HashTable.h>
  29. #include <AK/InlineLinkedList.h>
  30. #include <AK/RefCounted.h>
  31. #include <AK/String.h>
  32. #include <AK/WeakPtr.h>
  33. #include <Kernel/FileSystem/FIFO.h>
  34. #include <Kernel/FileSystem/FileSystem.h>
  35. #include <Kernel/FileSystem/InodeIdentifier.h>
  36. #include <Kernel/FileSystem/InodeMetadata.h>
  37. #include <Kernel/Forward.h>
  38. #include <Kernel/KResult.h>
  39. #include <Kernel/Lock.h>
  40. namespace Kernel {
  41. class Inode : public RefCounted<Inode>
  42. , public Weakable<Inode>
  43. , public InlineLinkedListNode<Inode> {
  44. friend class VFS;
  45. friend class FS;
  46. public:
  47. virtual ~Inode();
  48. virtual void one_ref_left() { }
  49. FS& fs() { return m_fs; }
  50. const FS& fs() const { return m_fs; }
  51. unsigned fsid() const { return m_fs.fsid(); }
  52. unsigned index() const { return m_index; }
  53. size_t size() const { return metadata().size; }
  54. bool is_symlink() const { return metadata().is_symlink(); }
  55. bool is_directory() const { return metadata().is_directory(); }
  56. bool is_character_device() const { return metadata().is_character_device(); }
  57. mode_t mode() const { return metadata().mode; }
  58. InodeIdentifier identifier() const { return { fsid(), index() }; }
  59. virtual InodeMetadata metadata() const = 0;
  60. KResultOr<NonnullOwnPtr<KBuffer>> read_entire(FileDescription* = nullptr) const;
  61. virtual KResult attach(FileDescription&) { return KSuccess; }
  62. virtual void detach(FileDescription&) { }
  63. virtual void did_seek(FileDescription&, off_t) { }
  64. virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const = 0;
  65. virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const = 0;
  66. virtual RefPtr<Inode> lookup(StringView name) = 0;
  67. virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) = 0;
  68. virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) = 0;
  69. virtual KResult add_child(Inode&, const StringView& name, mode_t) = 0;
  70. virtual KResult remove_child(const StringView& name) = 0;
  71. virtual KResultOr<size_t> directory_entry_count() const = 0;
  72. virtual KResult chmod(mode_t) = 0;
  73. virtual KResult chown(uid_t, gid_t) = 0;
  74. virtual KResult truncate(u64) { return KSuccess; }
  75. virtual KResultOr<NonnullRefPtr<Custody>> resolve_as_link(Custody& base, RefPtr<Custody>* out_parent = nullptr, int options = 0, int symlink_recursion_level = 0) const;
  76. LocalSocket* socket() { return m_socket.ptr(); }
  77. const LocalSocket* socket() const { return m_socket.ptr(); }
  78. bool bind_socket(LocalSocket&);
  79. bool unbind_socket();
  80. virtual FileDescription* preopen_fd() { return nullptr; };
  81. bool is_metadata_dirty() const { return m_metadata_dirty; }
  82. virtual int set_atime(time_t);
  83. virtual int set_ctime(time_t);
  84. virtual int set_mtime(time_t);
  85. virtual KResult increment_link_count();
  86. virtual KResult decrement_link_count();
  87. virtual void flush_metadata() = 0;
  88. void will_be_destroyed();
  89. void set_shared_vmobject(SharedInodeVMObject&);
  90. RefPtr<SharedInodeVMObject> shared_vmobject() const;
  91. bool is_shared_vmobject(const SharedInodeVMObject&) const;
  92. static InlineLinkedList<Inode>& all_with_lock();
  93. static void sync();
  94. bool has_watchers() const { return !m_watchers.is_empty(); }
  95. void register_watcher(Badge<InodeWatcher>, InodeWatcher&);
  96. void unregister_watcher(Badge<InodeWatcher>, InodeWatcher&);
  97. NonnullRefPtr<FIFO> fifo();
  98. // For InlineLinkedListNode.
  99. Inode* m_next { nullptr };
  100. Inode* m_prev { nullptr };
  101. static SpinLock<u32>& all_inodes_lock();
  102. protected:
  103. Inode(FS& fs, unsigned index);
  104. void set_metadata_dirty(bool);
  105. void inode_contents_changed(off_t, ssize_t, const UserOrKernelBuffer&);
  106. void inode_size_changed(size_t old_size, size_t new_size);
  107. KResult prepare_to_write_data();
  108. void did_add_child(const InodeIdentifier&);
  109. void did_remove_child(const InodeIdentifier&);
  110. mutable Lock m_lock { "Inode" };
  111. private:
  112. FS& m_fs;
  113. unsigned m_index { 0 };
  114. WeakPtr<SharedInodeVMObject> m_shared_vmobject;
  115. RefPtr<LocalSocket> m_socket;
  116. HashTable<InodeWatcher*> m_watchers;
  117. bool m_metadata_dirty { false };
  118. RefPtr<FIFO> m_fifo;
  119. };
  120. }