summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/Ext2FileSystem.h
blob: c958bac4111272a752af9c428269dc7618d85a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once

#include <Kernel/UnixTypes.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/FileSystem/ext2_fs.h>
#include <Kernel/FileSystem/DiskBackedFileSystem.h>

struct ext2_group_desc;
struct ext2_inode;
struct ext2_super_block;

class Ext2FS;

class Ext2FSInode final : public Inode {
    friend class Ext2FS;
public:
    virtual ~Ext2FSInode() override;

    size_t size() const { return m_raw_inode.i_size; }
    bool is_symlink() const { return ::is_symlink(m_raw_inode.i_mode); }

    // ^Inode (Retainable magic)
    virtual void one_retain_left() override;

private:
    // ^Inode
    virtual ssize_t read_bytes(off_t, ssize_t, byte* buffer, FileDescriptor*) const override;
    virtual InodeMetadata metadata() const override;
    virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const override;
    virtual InodeIdentifier lookup(const String& name) override;
    virtual String reverse_lookup(InodeIdentifier) override;
    virtual void flush_metadata() override;
    virtual ssize_t write_bytes(off_t, ssize_t, const byte* data, FileDescriptor*) override;
    virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) override;
    virtual KResult remove_child(const String& name) override;
    virtual RetainPtr<Inode> parent() const override;
    virtual int set_atime(time_t) override;
    virtual int set_ctime(time_t) override;
    virtual int set_mtime(time_t) override;
    virtual int increment_link_count() override;
    virtual int decrement_link_count() override;
    virtual size_t directory_entry_count() const override;
    virtual KResult chmod(mode_t) override;
    virtual KResult chown(uid_t, gid_t) override;
    virtual KResult truncate(off_t) override;

    void populate_lookup_cache() const;
    bool resize(qword);

    Ext2FS& fs();
    const Ext2FS& fs() const;
    Ext2FSInode(Ext2FS&, unsigned index);

    mutable Vector<unsigned> m_block_list;
    mutable HashMap<String, unsigned> m_lookup_cache;
    ext2_inode m_raw_inode;
    mutable InodeIdentifier m_parent_id;
};

class Ext2FS final : public DiskBackedFS {
    friend class Ext2FSInode;
public:
    static Retained <Ext2FS> create(Retained<DiskDevice>&&);
    virtual ~Ext2FS() override;
    virtual bool initialize() override;

    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;

private:
    typedef unsigned BlockIndex;
    typedef unsigned GroupIndex;
    typedef unsigned InodeIndex;
    explicit Ext2FS(Retained<DiskDevice>&&);

    const ext2_super_block& super_block() const;
    const ext2_group_desc& group_descriptor(unsigned groupIndex) const;
    void flush_block_group_descriptor_table();
    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;

    bool write_ext2_inode(InodeIndex, const ext2_inode&);
    ByteBuffer read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const;

    ByteBuffer read_super_block() const;
    bool write_super_block(const ext2_super_block&);

    virtual const char* class_name() const override;
    virtual InodeIdentifier root_inode() const override;
    virtual RetainPtr<Inode> create_inode(InodeIdentifier parentInode, const String& name, mode_t, off_t size, dev_t, int& error) override;
    virtual RetainPtr<Inode> create_directory(InodeIdentifier parentInode, const String& name, mode_t, int& error) override;
    virtual RetainPtr<Inode> get_inode(InodeIdentifier) const override;

    InodeIndex allocate_inode(GroupIndex preferred_group, off_t expected_size);
    Vector<BlockIndex> allocate_blocks(GroupIndex, int count);
    GroupIndex group_index_from_inode(InodeIndex) const;
    GroupIndex group_index_from_block_index(BlockIndex) const;

    Vector<BlockIndex> block_list_for_inode(const ext2_inode&, bool include_block_list_blocks = false) const;
    bool write_block_list_for_inode(InodeIndex, ext2_inode&, const Vector<BlockIndex>&);

    bool add_inode_to_directory(InodeIndex parent, InodeIndex child, const String& name, byte file_type, int& error);
    bool write_directory_inode(InodeIndex, Vector<DirectoryEntry>&&);
    bool get_inode_allocation_state(InodeIndex) const;
    bool set_inode_allocation_state(InodeIndex, bool);
    bool set_block_allocation_state(BlockIndex, bool);

    void uncache_inode(InodeIndex);
    void free_inode(Ext2FSInode&);

    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);

    unsigned m_block_group_count { 0 };

    mutable ByteBuffer m_cached_super_block;
    mutable ByteBuffer m_cached_group_descriptor_table;

    mutable HashMap<BlockIndex, RetainPtr<Ext2FSInode>> m_inode_cache;
};

inline Ext2FS& Ext2FSInode::fs()
{
    return static_cast<Ext2FS&>(Inode::fs());
}

inline const Ext2FS& Ext2FSInode::fs() const
{
    return static_cast<const Ext2FS&>(Inode::fs());
}