2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-16 12:21:49 +03:00
|
|
|
#include "DiskBackedFileSystem.h"
|
2018-10-14 23:57:41 +03:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/Buffer.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
2018-11-13 15:02:39 +03:00
|
|
|
#include "ext2_fs.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
struct ext2_group_desc;
|
|
|
|
struct ext2_inode;
|
|
|
|
struct ext2_super_block;
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
class Ext2FS;
|
2018-11-13 15:02:39 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
class Ext2FSInode final : public CoreInode {
|
|
|
|
friend class Ext2FS;
|
2018-11-13 15:02:39 +03:00
|
|
|
public:
|
2018-11-15 19:13:10 +03:00
|
|
|
virtual ~Ext2FSInode() override;
|
2018-11-13 15:02:39 +03:00
|
|
|
|
|
|
|
size_t size() const { return m_raw_inode.i_size; }
|
|
|
|
bool is_symlink() const { return isSymbolicLink(m_raw_inode.i_mode); }
|
|
|
|
|
|
|
|
private:
|
2018-11-13 15:32:16 +03:00
|
|
|
// ^CoreInode
|
2018-12-03 01:34:50 +03:00
|
|
|
virtual ssize_t read_bytes(Unix::off_t, size_t, byte* buffer, FileDescriptor*) override;
|
2018-11-13 15:32:16 +03:00
|
|
|
virtual void populate_metadata() const override;
|
2018-11-15 19:13:10 +03:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) override;
|
2018-11-15 18:34:36 +03:00
|
|
|
virtual InodeIdentifier lookup(const String& name) override;
|
2018-11-15 19:04:55 +03:00
|
|
|
virtual String reverse_lookup(InodeIdentifier) override;
|
|
|
|
|
|
|
|
void populate_lookup_cache();
|
2018-11-13 15:32:16 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
Ext2FS& fs();
|
|
|
|
const Ext2FS& fs() const;
|
|
|
|
Ext2FSInode(Ext2FS&, unsigned index, const ext2_inode&);
|
2018-11-13 15:02:39 +03:00
|
|
|
|
|
|
|
SpinLock m_lock;
|
|
|
|
Vector<unsigned> m_block_list;
|
2018-11-15 19:04:55 +03:00
|
|
|
HashMap<String, unsigned> m_lookup_cache;
|
2018-11-13 15:02:39 +03:00
|
|
|
ext2_inode m_raw_inode;
|
|
|
|
};
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
class Ext2FS final : public DiskBackedFS {
|
|
|
|
friend class Ext2FSInode;
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2018-11-15 19:13:10 +03:00
|
|
|
static RetainPtr<Ext2FS> create(RetainPtr<DiskDevice>&&);
|
|
|
|
virtual ~Ext2FS() override;
|
2018-10-17 11:55:43 +03:00
|
|
|
virtual bool initialize() override;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
private:
|
2018-10-16 01:35:03 +03:00
|
|
|
typedef unsigned BlockIndex;
|
|
|
|
typedef unsigned GroupIndex;
|
|
|
|
typedef unsigned InodeIndex;
|
2018-11-15 19:13:10 +03:00
|
|
|
explicit Ext2FS(RetainPtr<DiskDevice>&&);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
const ext2_super_block& super_block() const;
|
|
|
|
const ext2_group_desc& group_descriptor(unsigned groupIndex) const;
|
|
|
|
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 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
OwnPtr<ext2_inode> lookup_ext2_inode(unsigned) const;
|
|
|
|
bool write_ext2_inode(unsigned, const ext2_inode&);
|
|
|
|
ByteBuffer read_block_containing_inode(unsigned inode, unsigned& blockIndex, unsigned& offset) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
ByteBuffer read_super_block() const;
|
|
|
|
bool write_super_block(const ext2_super_block&);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:36:35 +03:00
|
|
|
virtual const char* class_name() const override;
|
2018-12-03 02:20:00 +03:00
|
|
|
virtual InodeIdentifier root_inode() const override;
|
|
|
|
virtual bool write_inode(InodeIdentifier, const ByteBuffer&) override;
|
|
|
|
virtual InodeMetadata inode_metadata(InodeIdentifier) const override;
|
2018-11-15 17:36:35 +03:00
|
|
|
virtual bool set_mtime(InodeIdentifier, dword timestamp) override;
|
2018-11-18 16:57:41 +03:00
|
|
|
virtual InodeIdentifier create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size, int& error) override;
|
2018-12-03 01:34:50 +03:00
|
|
|
virtual ssize_t read_inode_bytes(InodeIdentifier, Unix::off_t offset, size_t count, byte* buffer, FileDescriptor*) const override;
|
2018-11-18 16:57:41 +03:00
|
|
|
virtual InodeIdentifier create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t, int& error) override;
|
2018-11-15 17:10:12 +03:00
|
|
|
virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const override;
|
2018-11-14 01:44:54 +03:00
|
|
|
virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const override;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
bool is_directory_inode(unsigned) const;
|
|
|
|
unsigned allocate_inode(unsigned preferredGroup, unsigned expectedSize);
|
|
|
|
Vector<BlockIndex> allocate_blocks(unsigned group, unsigned count);
|
|
|
|
unsigned group_index_from_inode(unsigned) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
Vector<unsigned> block_list_for_inode(const ext2_inode&) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
void dump_block_bitmap(unsigned groupIndex) const;
|
|
|
|
void dump_inode_bitmap(unsigned groupIndex) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
template<typename F> void traverse_inode_bitmap(unsigned groupIndex, F) const;
|
|
|
|
template<typename F> void traverse_block_bitmap(unsigned groupIndex, F) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
bool add_inode_to_directory(unsigned directoryInode, unsigned inode, const String& name, byte fileType, int& error);
|
|
|
|
bool write_directory_inode(unsigned directoryInode, Vector<DirectoryEntry>&&);
|
|
|
|
bool set_inode_allocation_state(unsigned inode, bool);
|
|
|
|
bool set_block_allocation_state(GroupIndex, BlockIndex, bool);
|
2018-10-16 01:35:03 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
bool modify_link_count(InodeIndex, int delta);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
unsigned m_blockGroupCount { 0 };
|
|
|
|
|
2018-11-15 19:04:55 +03:00
|
|
|
bool deprecated_enumerateDirectoryInode(InodeIdentifier, Function<bool(const DirectoryEntry&)>) const;
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
mutable ByteBuffer m_cached_super_block;
|
|
|
|
mutable ByteBuffer m_cached_group_descriptor_table;
|
2018-10-30 01:45:34 +03:00
|
|
|
|
2018-11-13 15:02:39 +03:00
|
|
|
mutable SpinLock m_inode_cache_lock;
|
2018-11-15 19:13:10 +03:00
|
|
|
mutable HashMap<BlockIndex, RetainPtr<Ext2FSInode>> m_inode_cache;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
inline Ext2FS& Ext2FSInode::fs()
|
2018-11-13 15:02:39 +03:00
|
|
|
{
|
2018-11-15 19:13:10 +03:00
|
|
|
return static_cast<Ext2FS&>(CoreInode::fs());
|
2018-11-13 15:02:39 +03:00
|
|
|
}
|
2018-11-13 15:32:16 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
inline const Ext2FS& Ext2FSInode::fs() const
|
2018-11-13 15:32:16 +03:00
|
|
|
{
|
2018-11-15 19:13:10 +03:00
|
|
|
return static_cast<const Ext2FS&>(CoreInode::fs());
|
2018-11-13 15:32:16 +03:00
|
|
|
}
|