2018-10-10 06:53:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <Kernel/FileSystem/DiskBackedFileSystem.h>
|
2019-05-15 22:02:37 -03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
|
#include <Kernel/FileSystem/ext2_fs.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
|
|
|
|
struct ext2_group_desc;
|
|
|
|
|
struct ext2_inode;
|
|
|
|
|
struct ext2_super_block;
|
|
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
class Ext2FS;
|
2018-11-13 10:02:39 -02:00
|
|
|
|
2018-12-19 18:18:28 -02:00
|
|
|
class Ext2FSInode final : public Inode {
|
2018-11-15 14:13:10 -02:00
|
|
|
friend class Ext2FS;
|
2019-05-28 06:53:16 -03:00
|
|
|
|
2018-11-13 10:02:39 -02:00
|
|
|
public:
|
2018-11-15 14:13:10 -02:00
|
|
|
virtual ~Ext2FSInode() override;
|
2018-11-13 10:02:39 -02:00
|
|
|
|
|
|
|
|
size_t size() const { return m_raw_inode.i_size; }
|
2019-01-31 14:31:23 -02:00
|
|
|
bool is_symlink() const { return ::is_symlink(m_raw_inode.i_mode); }
|
2018-11-13 10:02:39 -02:00
|
|
|
|
2019-06-21 13:58:45 -03:00
|
|
|
// ^Inode (RefCounted magic)
|
2019-06-21 10:29:31 -03:00
|
|
|
virtual void one_ref_left() override;
|
2018-12-31 23:38:09 -02:00
|
|
|
|
2018-11-13 10:02:39 -02:00
|
|
|
private:
|
2018-12-19 18:56:45 -02:00
|
|
|
// ^Inode
|
2019-07-03 16:17:35 -03:00
|
|
|
virtual ssize_t read_bytes(off_t, ssize_t, u8* buffer, FileDescription*) const override;
|
2019-01-01 00:16:36 -02:00
|
|
|
virtual InodeMetadata metadata() const override;
|
2019-01-28 01:16:01 -02:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const override;
|
2019-06-01 13:01:28 -03:00
|
|
|
virtual InodeIdentifier lookup(StringView name) override;
|
2018-12-19 18:56:45 -02:00
|
|
|
virtual void flush_metadata() override;
|
2019-07-03 16:17:35 -03:00
|
|
|
virtual ssize_t write_bytes(off_t, ssize_t, const u8* data, FileDescription*) override;
|
2019-06-09 05:25:19 -03:00
|
|
|
virtual KResult add_child(InodeIdentifier child_id, const StringView& name, mode_t) override;
|
|
|
|
|
virtual KResult remove_child(const StringView& name) override;
|
2019-01-23 03:53:01 -02:00
|
|
|
virtual int set_atime(time_t) override;
|
|
|
|
|
virtual int set_ctime(time_t) override;
|
|
|
|
|
virtual int set_mtime(time_t) override;
|
2019-01-01 00:16:36 -02:00
|
|
|
virtual int increment_link_count() override;
|
|
|
|
|
virtual int decrement_link_count() override;
|
2019-01-28 01:16:01 -02:00
|
|
|
virtual size_t directory_entry_count() const override;
|
2019-02-25 16:47:56 -03:00
|
|
|
virtual KResult chmod(mode_t) override;
|
2019-02-27 08:32:53 -03:00
|
|
|
virtual KResult chown(uid_t, gid_t) override;
|
2019-04-23 08:00:53 -03:00
|
|
|
virtual KResult truncate(off_t) override;
|
2018-11-15 14:04:55 -02:00
|
|
|
|
2019-06-09 07:46:23 -03:00
|
|
|
bool write_directory(const Vector<FS::DirectoryEntry>&);
|
2019-01-28 01:16:01 -02:00
|
|
|
void populate_lookup_cache() const;
|
2019-07-03 16:17:35 -03:00
|
|
|
bool resize(u64);
|
2018-11-13 10:32:16 -02:00
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
Ext2FS& fs();
|
|
|
|
|
const Ext2FS& fs() const;
|
2019-02-03 01:05:30 -02:00
|
|
|
Ext2FSInode(Ext2FS&, unsigned index);
|
2018-11-13 10:02:39 -02:00
|
|
|
|
2019-01-28 01:16:01 -02:00
|
|
|
mutable Vector<unsigned> m_block_list;
|
|
|
|
|
mutable HashMap<String, unsigned> m_lookup_cache;
|
2018-11-13 10:02:39 -02:00
|
|
|
ext2_inode m_raw_inode;
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
class Ext2FS final : public DiskBackedFS {
|
|
|
|
|
friend class Ext2FSInode;
|
2019-05-28 06:53:16 -03:00
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
public:
|
2019-07-11 10:38:47 -03:00
|
|
|
static NonnullRefPtr<Ext2FS> create(NonnullRefPtr<DiskDevice>);
|
2018-11-15 14:13:10 -02:00
|
|
|
virtual ~Ext2FS() override;
|
2018-10-17 05:55:43 -03:00
|
|
|
virtual bool initialize() override;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-02-21 10:48:00 -03:00
|
|
|
virtual unsigned total_block_count() const override;
|
|
|
|
|
virtual unsigned free_block_count() const override;
|
|
|
|
|
virtual unsigned total_inode_count() const override;
|
|
|
|
|
virtual unsigned free_inode_count() const override;
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
private:
|
2018-10-15 19:35:03 -03:00
|
|
|
typedef unsigned BlockIndex;
|
|
|
|
|
typedef unsigned GroupIndex;
|
|
|
|
|
typedef unsigned InodeIndex;
|
2019-06-21 13:37:47 -03:00
|
|
|
explicit Ext2FS(NonnullRefPtr<DiskDevice>&&);
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2018-12-02 21:20:00 -02:00
|
|
|
const ext2_super_block& super_block() const;
|
|
|
|
|
const ext2_group_desc& group_descriptor(unsigned groupIndex) const;
|
2019-01-28 01:16:01 -02:00
|
|
|
void flush_block_group_descriptor_table();
|
2018-12-02 21:20:00 -02:00
|
|
|
unsigned first_block_of_group(unsigned groupIndex) const;
|
|
|
|
|
unsigned inodes_per_block() const;
|
|
|
|
|
unsigned inodes_per_group() const;
|
|
|
|
|
unsigned blocks_per_group() const;
|
|
|
|
|
unsigned inode_size() const;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-04-23 08:00:53 -03:00
|
|
|
bool write_ext2_inode(InodeIndex, const ext2_inode&);
|
|
|
|
|
ByteBuffer read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2018-12-02 21:20:00 -02:00
|
|
|
ByteBuffer read_super_block() const;
|
|
|
|
|
bool write_super_block(const ext2_super_block&);
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2018-11-15 12:36:35 -02:00
|
|
|
virtual const char* class_name() const override;
|
2018-12-02 21:20:00 -02:00
|
|
|
virtual InodeIdentifier root_inode() const override;
|
2019-06-21 13:37:47 -03:00
|
|
|
virtual RefPtr<Inode> create_inode(InodeIdentifier parentInode, const String& name, mode_t, off_t size, dev_t, int& error) override;
|
|
|
|
|
virtual RefPtr<Inode> create_directory(InodeIdentifier parentInode, const String& name, mode_t, int& error) override;
|
|
|
|
|
virtual RefPtr<Inode> get_inode(InodeIdentifier) const override;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-04-23 08:00:53 -03:00
|
|
|
InodeIndex allocate_inode(GroupIndex preferred_group, off_t expected_size);
|
|
|
|
|
Vector<BlockIndex> allocate_blocks(GroupIndex, int count);
|
|
|
|
|
GroupIndex group_index_from_inode(InodeIndex) const;
|
2019-02-15 20:24:01 -02:00
|
|
|
GroupIndex group_index_from_block_index(BlockIndex) const;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-04-23 08:00:53 -03:00
|
|
|
Vector<BlockIndex> block_list_for_inode(const ext2_inode&, bool include_block_list_blocks = false) const;
|
2019-01-23 01:29:56 -02:00
|
|
|
bool write_block_list_for_inode(InodeIndex, ext2_inode&, const Vector<BlockIndex>&);
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-01-01 00:35:33 -02:00
|
|
|
bool get_inode_allocation_state(InodeIndex) const;
|
2019-04-23 08:00:53 -03:00
|
|
|
bool set_inode_allocation_state(InodeIndex, bool);
|
2019-02-15 20:24:01 -02:00
|
|
|
bool set_block_allocation_state(BlockIndex, bool);
|
2018-10-15 19:35:03 -03:00
|
|
|
|
2019-01-22 04:03:44 -02:00
|
|
|
void uncache_inode(InodeIndex);
|
2019-01-22 17:48:48 -02:00
|
|
|
void free_inode(Ext2FSInode&);
|
2019-01-22 04:03:44 -02:00
|
|
|
|
2019-01-22 23:45:25 -02:00
|
|
|
struct BlockListShape {
|
|
|
|
|
unsigned direct_blocks { 0 };
|
|
|
|
|
unsigned indirect_blocks { 0 };
|
|
|
|
|
unsigned doubly_indirect_blocks { 0 };
|
|
|
|
|
unsigned triply_indirect_blocks { 0 };
|
|
|
|
|
unsigned meta_blocks { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BlockListShape compute_block_list_shape(unsigned blocks);
|
|
|
|
|
|
2019-01-31 14:31:23 -02:00
|
|
|
unsigned m_block_group_count { 0 };
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2018-12-02 21:20:00 -02:00
|
|
|
mutable ByteBuffer m_cached_super_block;
|
|
|
|
|
mutable ByteBuffer m_cached_group_descriptor_table;
|
2018-10-29 19:45:34 -03:00
|
|
|
|
2019-06-21 13:37:47 -03:00
|
|
|
mutable HashMap<BlockIndex, RefPtr<Ext2FSInode>> m_inode_cache;
|
2018-10-10 06:53:07 -03:00
|
|
|
};
|
|
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
inline Ext2FS& Ext2FSInode::fs()
|
2018-11-13 10:02:39 -02:00
|
|
|
{
|
2018-12-19 18:18:28 -02:00
|
|
|
return static_cast<Ext2FS&>(Inode::fs());
|
2018-11-13 10:02:39 -02:00
|
|
|
}
|
2018-11-13 10:32:16 -02:00
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
inline const Ext2FS& Ext2FSInode::fs() const
|
2018-11-13 10:32:16 -02:00
|
|
|
{
|
2018-12-19 18:18:28 -02:00
|
|
|
return static_cast<const Ext2FS&>(Inode::fs());
|
2018-11-13 10:32:16 -02:00
|
|
|
}
|