2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-16 09:21:49 +00:00
|
|
|
#include "DiskDevice.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include "InodeIdentifier.h"
|
|
|
|
#include "InodeMetadata.h"
|
2018-10-14 19:19:27 +00:00
|
|
|
#include "Limits.h"
|
2018-10-14 20:57:41 +00:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
2018-12-03 23:27:16 +00:00
|
|
|
#include <AK/AKString.h>
|
2018-10-17 08:55:43 +00:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/kstdio.h>
|
2019-01-23 04:42:23 +00:00
|
|
|
#include <AK/Lock.h>
|
2019-02-08 15:40:48 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
static const dword mepoch = 476763780;
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
class Inode;
|
2018-11-07 10:37:54 +00:00
|
|
|
class FileDescriptor;
|
2019-02-14 14:55:19 +00:00
|
|
|
class LocalSocket;
|
2019-01-16 11:57:07 +00:00
|
|
|
class VMObject;
|
2018-10-26 22:14:24 +00:00
|
|
|
|
2018-11-15 16:13:10 +00:00
|
|
|
class FS : public Retainable<FS> {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2018-11-15 16:13:10 +00:00
|
|
|
virtual ~FS();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-01-23 04:38:54 +00:00
|
|
|
unsigned fsid() const { return m_fsid; }
|
2018-12-02 23:20:00 +00:00
|
|
|
static FS* from_fsid(dword);
|
2018-12-19 23:39:29 +00:00
|
|
|
static void sync();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
virtual bool initialize() = 0;
|
2018-11-15 14:36:35 +00:00
|
|
|
virtual const char* class_name() const = 0;
|
2018-12-02 23:20:00 +00:00
|
|
|
virtual InodeIdentifier root_inode() const = 0;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-19 20:56:45 +00:00
|
|
|
bool is_readonly() const { return m_readonly; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
struct DirectoryEntry {
|
2019-01-31 16:31:23 +00:00
|
|
|
DirectoryEntry(const char* name, InodeIdentifier, byte file_type);
|
|
|
|
DirectoryEntry(const char* name, size_t name_length, InodeIdentifier, byte file_type);
|
2018-11-12 23:17:30 +00:00
|
|
|
char name[256];
|
2018-12-02 22:34:50 +00:00
|
|
|
size_t name_length { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
InodeIdentifier inode;
|
2019-01-31 16:31:23 +00:00
|
|
|
byte file_type { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2019-01-23 05:53:01 +00:00
|
|
|
virtual RetainPtr<Inode> create_inode(InodeIdentifier parentInode, const String& name, mode_t, unsigned size, int& error) = 0;
|
|
|
|
virtual RetainPtr<Inode> create_directory(InodeIdentifier parentInode, const String& name, mode_t, int& error) = 0;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
virtual RetainPtr<Inode> get_inode(InodeIdentifier) const = 0;
|
2018-11-13 12:02:39 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
protected:
|
2018-11-15 16:13:10 +00:00
|
|
|
FS();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
private:
|
2019-01-23 04:38:54 +00:00
|
|
|
unsigned m_fsid { 0 };
|
2018-12-19 20:56:45 +00:00
|
|
|
bool m_readonly { false };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
class Inode : public Retainable<Inode> {
|
2018-11-15 13:43:10 +00:00
|
|
|
friend class VFS;
|
2018-11-13 22:44:54 +00:00
|
|
|
public:
|
2018-12-19 20:18:28 +00:00
|
|
|
virtual ~Inode();
|
2018-11-13 22:44:54 +00:00
|
|
|
|
2019-01-01 01:38:09 +00:00
|
|
|
virtual void one_retain_left() { }
|
|
|
|
|
2018-11-15 16:13:10 +00:00
|
|
|
FS& fs() { return m_fs; }
|
|
|
|
const FS& fs() const { return m_fs; }
|
2018-11-13 22:44:54 +00:00
|
|
|
unsigned fsid() const;
|
|
|
|
unsigned index() const { return m_index; }
|
|
|
|
|
|
|
|
size_t size() const { return metadata().size; }
|
2019-01-31 16:31:23 +00:00
|
|
|
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(); }
|
2019-01-29 03:55:08 +00:00
|
|
|
mode_t mode() const { return metadata().mode; }
|
2018-11-13 22:44:54 +00:00
|
|
|
|
|
|
|
InodeIdentifier identifier() const { return { fsid(), index() }; }
|
2019-01-01 02:16:36 +00:00
|
|
|
virtual InodeMetadata metadata() const = 0;
|
2018-11-13 22:44:54 +00:00
|
|
|
|
2019-01-28 03:16:01 +00:00
|
|
|
ByteBuffer read_entire(FileDescriptor* = nullptr) const;
|
2018-11-15 15:34:36 +00:00
|
|
|
|
2019-01-28 03:16:01 +00:00
|
|
|
virtual ssize_t read_bytes(off_t, size_t, byte* buffer, FileDescriptor*) const = 0;
|
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const = 0;
|
2018-11-15 15:34:36 +00:00
|
|
|
virtual InodeIdentifier lookup(const String& name) = 0;
|
2018-11-15 16:04:55 +00:00
|
|
|
virtual String reverse_lookup(InodeIdentifier) = 0;
|
2019-01-23 05:53:01 +00:00
|
|
|
virtual ssize_t write_bytes(off_t, size_t, const byte* data, FileDescriptor*) = 0;
|
2018-12-24 23:27:39 +00:00
|
|
|
virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) = 0;
|
2019-01-22 06:03:44 +00:00
|
|
|
virtual bool remove_child(const String& name, int& error) = 0;
|
2019-01-04 17:37:58 +00:00
|
|
|
virtual RetainPtr<Inode> parent() const = 0;
|
2019-01-28 03:16:01 +00:00
|
|
|
virtual size_t directory_entry_count() const = 0;
|
2019-01-29 03:55:08 +00:00
|
|
|
virtual bool chmod(mode_t, int& error) = 0;
|
2018-11-13 22:44:54 +00:00
|
|
|
|
2019-02-14 14:55:19 +00:00
|
|
|
LocalSocket* socket() { return m_socket.ptr(); }
|
|
|
|
const LocalSocket* socket() const { return m_socket.ptr(); }
|
|
|
|
bool bind_socket(LocalSocket&);
|
|
|
|
bool unbind_socket();
|
|
|
|
|
2018-12-19 21:28:09 +00:00
|
|
|
bool is_metadata_dirty() const { return m_metadata_dirty; }
|
2018-12-19 20:56:45 +00:00
|
|
|
|
2019-01-23 05:53:01 +00:00
|
|
|
virtual int set_atime(time_t);
|
|
|
|
virtual int set_ctime(time_t);
|
|
|
|
virtual int set_mtime(time_t);
|
2019-01-01 02:16:36 +00:00
|
|
|
virtual int increment_link_count();
|
|
|
|
virtual int decrement_link_count();
|
2018-12-19 20:56:45 +00:00
|
|
|
|
|
|
|
virtual void flush_metadata() = 0;
|
2018-12-19 20:14:55 +00:00
|
|
|
|
2018-12-19 21:28:09 +00:00
|
|
|
void will_be_destroyed();
|
|
|
|
|
2019-02-08 15:40:48 +00:00
|
|
|
void set_vmo(VMObject&);
|
2019-01-16 11:57:07 +00:00
|
|
|
VMObject* vmo() { return m_vmo.ptr(); }
|
|
|
|
const VMObject* vmo() const { return m_vmo.ptr(); }
|
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
protected:
|
2018-12-19 23:39:29 +00:00
|
|
|
Inode(FS& fs, unsigned index);
|
2018-12-19 21:28:09 +00:00
|
|
|
void set_metadata_dirty(bool b) { m_metadata_dirty = b; }
|
2019-02-05 07:17:46 +00:00
|
|
|
void inode_contents_changed(off_t, size_t, const byte*);
|
|
|
|
void inode_size_changed(size_t old_size, size_t new_size);
|
2019-01-23 04:42:23 +00:00
|
|
|
|
2019-01-28 03:16:01 +00:00
|
|
|
mutable Lock m_lock;
|
2019-01-23 04:42:23 +00:00
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
private:
|
2018-11-15 16:13:10 +00:00
|
|
|
FS& m_fs;
|
2018-11-13 22:44:54 +00:00
|
|
|
unsigned m_index { 0 };
|
2019-02-08 15:40:48 +00:00
|
|
|
WeakPtr<VMObject> m_vmo;
|
2019-02-14 14:55:19 +00:00
|
|
|
RetainPtr<LocalSocket> m_socket;
|
2018-12-19 21:28:09 +00:00
|
|
|
bool m_metadata_dirty { false };
|
2018-11-13 22:44:54 +00:00
|
|
|
};
|
|
|
|
|
2018-12-02 23:20:00 +00:00
|
|
|
inline FS* InodeIdentifier::fs()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-12-02 23:20:00 +00:00
|
|
|
return FS::from_fsid(m_fsid);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 23:20:00 +00:00
|
|
|
inline const FS* InodeIdentifier::fs() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-12-02 23:20:00 +00:00
|
|
|
return FS::from_fsid(m_fsid);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 23:20:00 +00:00
|
|
|
inline bool InodeIdentifier::is_root_inode() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-12-02 23:20:00 +00:00
|
|
|
return (*this) == fs()->root_inode();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
inline unsigned Inode::fsid() const
|
2018-11-13 12:02:39 +00:00
|
|
|
{
|
2019-01-23 04:38:54 +00:00
|
|
|
return m_fs.fsid();
|
2018-11-13 12:02:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<InodeIdentifier> {
|
2019-02-10 19:07:14 +00:00
|
|
|
static unsigned hash(const InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index()); }
|
2018-11-15 14:10:12 +00:00
|
|
|
static void dump(const InodeIdentifier& inode) { kprintf("%02u:%08u", inode.fsid(), inode.index()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|