Inode.h 4.9 KB

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