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