/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Kernel { class Inode : public RefCounted , public Weakable , public InlineLinkedListNode { friend class VFS; friend class FS; public: virtual ~Inode(); virtual void one_ref_left() { } FS& fs() { return m_fs; } const FS& fs() const { return m_fs; } unsigned fsid() const { return m_fs.fsid(); } InodeIndex index() const { return m_index; } size_t size() const { return metadata().size; } bool is_symlink() const { return metadata().is_symlink(); } bool is_directory() const { return metadata().is_directory(); } bool is_character_device() const { return metadata().is_character_device(); } mode_t mode() const { return metadata().mode; } InodeIdentifier identifier() const { return { fsid(), index() }; } virtual InodeMetadata metadata() const = 0; KResultOr> read_entire(FileDescription* = nullptr) const; virtual KResult attach(FileDescription&) { return KSuccess; } virtual void detach(FileDescription&) { } virtual void did_seek(FileDescription&, off_t) { } virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const = 0; virtual KResult traverse_as_directory(Function) const = 0; virtual RefPtr lookup(StringView name) = 0; virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) = 0; virtual KResultOr> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) = 0; virtual KResult add_child(Inode&, const StringView& name, mode_t) = 0; virtual KResult remove_child(const StringView& name) = 0; virtual KResultOr directory_entry_count() const = 0; virtual KResult chmod(mode_t) = 0; virtual KResult chown(uid_t, gid_t) = 0; virtual KResult truncate(u64) { return KSuccess; } virtual KResultOr> resolve_as_link(Custody& base, RefPtr* out_parent, int options, int symlink_recursion_level) const; virtual KResultOr get_block_address(int) { return ENOTSUP; } LocalSocket* socket() { return m_socket.ptr(); } const LocalSocket* socket() const { return m_socket.ptr(); } bool bind_socket(LocalSocket&); bool unbind_socket(); virtual FileDescription* preopen_fd() { return nullptr; }; bool is_metadata_dirty() const { return m_metadata_dirty; } virtual KResult set_atime(time_t); virtual KResult set_ctime(time_t); virtual KResult set_mtime(time_t); virtual KResult increment_link_count(); virtual KResult decrement_link_count(); virtual void flush_metadata() = 0; void will_be_destroyed(); void set_shared_vmobject(SharedInodeVMObject&); RefPtr shared_vmobject() const; bool is_shared_vmobject(const SharedInodeVMObject&) const; static InlineLinkedList& all_with_lock(); static void sync(); bool has_watchers() const { return !m_watchers.is_empty(); } void register_watcher(Badge, InodeWatcher&); void unregister_watcher(Badge, InodeWatcher&); NonnullRefPtr fifo(); // For InlineLinkedListNode. Inode* m_next { nullptr }; Inode* m_prev { nullptr }; static SpinLock& all_inodes_lock(); protected: Inode(FS& fs, InodeIndex); void set_metadata_dirty(bool); KResult prepare_to_write_data(); void did_add_child(const InodeIdentifier&); void did_remove_child(const InodeIdentifier&); mutable Lock m_lock { "Inode" }; private: FS& m_fs; InodeIndex m_index { 0 }; WeakPtr m_shared_vmobject; RefPtr m_socket; HashTable m_watchers; bool m_metadata_dirty { false }; RefPtr m_fifo; }; }