2019-05-16 04:02:37 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <AK/Function.h>
|
2019-08-08 12:08:27 +03:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2019-05-16 04:02:37 +03:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
|
|
|
#include <Kernel/KResult.h>
|
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
class FileDescription;
|
2019-08-07 19:06:17 +03:00
|
|
|
class InodeVMObject;
|
2019-07-22 21:01:11 +03:00
|
|
|
class InodeWatcher;
|
2019-05-16 04:02:37 +03:00
|
|
|
class LocalSocket;
|
|
|
|
|
2019-08-08 12:08:27 +03:00
|
|
|
class Inode : public RefCounted<Inode>
|
|
|
|
, public Weakable<Inode>
|
|
|
|
, public InlineLinkedListNode<Inode> {
|
2019-05-16 04:02:37 +03:00
|
|
|
friend class VFS;
|
|
|
|
friend class FS;
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
public:
|
|
|
|
virtual ~Inode();
|
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
virtual void one_ref_left() {}
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
FS& fs() { return m_fs; }
|
|
|
|
const FS& fs() const { return m_fs; }
|
|
|
|
unsigned fsid() const;
|
|
|
|
unsigned 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;
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
ByteBuffer read_entire(FileDescription* = nullptr) const;
|
2019-05-16 04:02:37 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual ssize_t read_bytes(off_t, ssize_t, u8* buffer, FileDescription*) const = 0;
|
2019-05-16 04:02:37 +03:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const = 0;
|
2019-06-01 19:01:28 +03:00
|
|
|
virtual InodeIdentifier lookup(StringView name) = 0;
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual ssize_t write_bytes(off_t, ssize_t, const u8* data, FileDescription*) = 0;
|
2019-06-09 11:25:19 +03:00
|
|
|
virtual KResult add_child(InodeIdentifier child_id, const StringView& name, mode_t) = 0;
|
|
|
|
virtual KResult remove_child(const StringView& name) = 0;
|
2019-05-16 04:02:37 +03:00
|
|
|
virtual size_t directory_entry_count() const = 0;
|
|
|
|
virtual KResult chmod(mode_t) = 0;
|
|
|
|
virtual KResult chown(uid_t, gid_t) = 0;
|
|
|
|
virtual KResult truncate(off_t) { return KSuccess; }
|
|
|
|
|
|
|
|
LocalSocket* socket() { return m_socket.ptr(); }
|
|
|
|
const LocalSocket* socket() const { return m_socket.ptr(); }
|
|
|
|
bool bind_socket(LocalSocket&);
|
|
|
|
bool unbind_socket();
|
|
|
|
|
|
|
|
bool is_metadata_dirty() const { return m_metadata_dirty; }
|
|
|
|
|
|
|
|
virtual int set_atime(time_t);
|
|
|
|
virtual int set_ctime(time_t);
|
|
|
|
virtual int set_mtime(time_t);
|
|
|
|
virtual int increment_link_count();
|
|
|
|
virtual int decrement_link_count();
|
|
|
|
|
|
|
|
virtual void flush_metadata() = 0;
|
|
|
|
|
|
|
|
void will_be_destroyed();
|
|
|
|
|
2019-12-19 21:13:44 +03:00
|
|
|
void set_vmobject(VMObject&);
|
2019-09-04 12:27:14 +03:00
|
|
|
InodeVMObject* vmobject() { return m_vmobject.ptr(); }
|
|
|
|
const InodeVMObject* vmobject() const { return m_vmobject.ptr(); }
|
2019-05-16 04:02:37 +03:00
|
|
|
|
|
|
|
static void sync();
|
|
|
|
|
2019-11-04 18:31:46 +03:00
|
|
|
bool has_watchers() const { return !m_watchers.is_empty(); }
|
|
|
|
|
2019-07-22 21:01:11 +03:00
|
|
|
void register_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
|
|
|
void unregister_watcher(Badge<InodeWatcher>, InodeWatcher&);
|
|
|
|
|
2019-08-08 12:08:27 +03:00
|
|
|
// For InlineLinkedListNode.
|
|
|
|
Inode* m_next { nullptr };
|
|
|
|
Inode* m_prev { nullptr };
|
|
|
|
|
2019-05-16 04:02:37 +03:00
|
|
|
protected:
|
|
|
|
Inode(FS& fs, unsigned index);
|
2019-07-22 21:01:11 +03:00
|
|
|
void set_metadata_dirty(bool);
|
2019-07-03 22:17:35 +03:00
|
|
|
void inode_contents_changed(off_t, ssize_t, const u8*);
|
2019-05-16 04:02:37 +03:00
|
|
|
void inode_size_changed(size_t old_size, size_t new_size);
|
|
|
|
|
|
|
|
mutable Lock m_lock { "Inode" };
|
|
|
|
|
|
|
|
private:
|
|
|
|
FS& m_fs;
|
|
|
|
unsigned m_index { 0 };
|
2019-09-04 12:27:14 +03:00
|
|
|
WeakPtr<InodeVMObject> m_vmobject;
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<LocalSocket> m_socket;
|
2019-07-22 21:01:11 +03:00
|
|
|
HashTable<InodeWatcher*> m_watchers;
|
2019-05-16 04:02:37 +03:00
|
|
|
bool m_metadata_dirty { false };
|
|
|
|
};
|