2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-16 11:21:49 +02:00
|
|
|
#include "DiskBackedFileSystem.h"
|
2018-10-14 22:57:41 +02:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/Buffer.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
2018-11-13 13:02:39 +01:00
|
|
|
#include "ext2_fs.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
struct ext2_group_desc;
|
|
|
|
struct ext2_inode;
|
|
|
|
struct ext2_super_block;
|
|
|
|
|
2018-11-13 13:02:39 +01:00
|
|
|
class Ext2FileSystem;
|
|
|
|
|
|
|
|
class Ext2Inode final : public CoreInode {
|
|
|
|
friend class Ext2FileSystem;
|
|
|
|
public:
|
|
|
|
virtual ~Ext2Inode() override;
|
|
|
|
|
|
|
|
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 13:32:16 +01:00
|
|
|
// ^CoreInode
|
|
|
|
virtual Unix::ssize_t read_bytes(Unix::off_t, Unix::size_t, byte* buffer, FileDescriptor*) override;
|
|
|
|
virtual void populate_metadata() const override;
|
2018-11-13 23:44:54 +01:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FileSystem::DirectoryEntry&)>) override;
|
2018-11-15 16:34:36 +01:00
|
|
|
virtual InodeIdentifier lookup(const String& name) override;
|
2018-11-13 13:32:16 +01:00
|
|
|
|
2018-11-13 13:02:39 +01:00
|
|
|
Ext2FileSystem& fs();
|
2018-11-13 13:32:16 +01:00
|
|
|
const Ext2FileSystem& fs() const;
|
2018-11-13 13:02:39 +01:00
|
|
|
Ext2Inode(Ext2FileSystem&, unsigned index, const ext2_inode&);
|
|
|
|
|
|
|
|
SpinLock m_lock;
|
|
|
|
Vector<unsigned> m_block_list;
|
2018-11-15 16:34:36 +01:00
|
|
|
HashMap<String, unsigned> m_child_cache;
|
2018-11-13 13:02:39 +01:00
|
|
|
ext2_inode m_raw_inode;
|
|
|
|
};
|
|
|
|
|
2018-10-16 11:21:49 +02:00
|
|
|
class Ext2FileSystem final : public DiskBackedFileSystem {
|
2018-11-13 13:02:39 +01:00
|
|
|
friend class Ext2Inode;
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2018-10-16 11:21:49 +02:00
|
|
|
static RetainPtr<Ext2FileSystem> create(RetainPtr<DiskDevice>&&);
|
2018-10-10 11:53:07 +02:00
|
|
|
virtual ~Ext2FileSystem() override;
|
2018-10-17 10:55:43 +02:00
|
|
|
virtual bool initialize() override;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
private:
|
2018-10-16 00:35:03 +02:00
|
|
|
typedef unsigned BlockIndex;
|
|
|
|
typedef unsigned GroupIndex;
|
|
|
|
typedef unsigned InodeIndex;
|
2018-10-29 23:45:34 +01:00
|
|
|
class CachedExt2Inode;
|
|
|
|
class CachedExt2InodeImpl;
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-10-16 11:21:49 +02:00
|
|
|
explicit Ext2FileSystem(RetainPtr<DiskDevice>&&);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
const ext2_super_block& superBlock() const;
|
|
|
|
const ext2_group_desc& blockGroupDescriptor(unsigned groupIndex) const;
|
|
|
|
unsigned firstBlockOfGroup(unsigned groupIndex) const;
|
|
|
|
unsigned inodesPerBlock() const;
|
|
|
|
unsigned inodesPerGroup() const;
|
|
|
|
unsigned blocksPerGroup() const;
|
|
|
|
unsigned inodeSize() const;
|
|
|
|
|
2018-10-29 23:45:34 +01:00
|
|
|
CachedExt2Inode lookupExt2Inode(unsigned) const;
|
2018-10-10 11:53:07 +02:00
|
|
|
bool writeExt2Inode(unsigned, const ext2_inode&);
|
|
|
|
ByteBuffer readBlockContainingInode(unsigned inode, unsigned& blockIndex, unsigned& offset) const;
|
|
|
|
|
|
|
|
ByteBuffer readSuperBlock() const;
|
|
|
|
bool writeSuperBlock(const ext2_super_block&);
|
|
|
|
|
2018-11-15 15:36:35 +01:00
|
|
|
virtual const char* class_name() const override;
|
2018-10-10 11:53:07 +02:00
|
|
|
virtual InodeIdentifier rootInode() const override;
|
|
|
|
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
|
2018-10-17 10:55:43 +02:00
|
|
|
virtual bool enumerateDirectoryInode(InodeIdentifier, Function<bool(const DirectoryEntry&)>) const override;
|
2018-10-10 11:53:07 +02:00
|
|
|
virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
|
2018-11-15 15:36:35 +01:00
|
|
|
virtual bool set_mtime(InodeIdentifier, dword timestamp) override;
|
|
|
|
virtual InodeIdentifier create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
|
|
|
|
virtual Unix::ssize_t read_inode_bytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const override;
|
|
|
|
virtual InodeIdentifier create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
|
2018-11-15 15:10:12 +01:00
|
|
|
virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const override;
|
2018-11-13 23:44:54 +01:00
|
|
|
virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const override;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
bool isDirectoryInode(unsigned) const;
|
|
|
|
unsigned allocateInode(unsigned preferredGroup, unsigned expectedSize);
|
2018-10-16 00:35:03 +02:00
|
|
|
Vector<BlockIndex> allocateBlocks(unsigned group, unsigned count);
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned groupIndexFromInode(unsigned) const;
|
|
|
|
|
|
|
|
Vector<unsigned> blockListForInode(const ext2_inode&) const;
|
|
|
|
|
|
|
|
void dumpBlockBitmap(unsigned groupIndex) const;
|
|
|
|
void dumpInodeBitmap(unsigned groupIndex) const;
|
|
|
|
|
2018-10-16 00:35:03 +02:00
|
|
|
template<typename F> void traverseInodeBitmap(unsigned groupIndex, F) const;
|
|
|
|
template<typename F> void traverseBlockBitmap(unsigned groupIndex, F) const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-15 01:57:57 +02:00
|
|
|
bool addInodeToDirectory(unsigned directoryInode, unsigned inode, const String& name, byte fileType);
|
2018-10-10 11:53:07 +02:00
|
|
|
bool writeDirectoryInode(unsigned directoryInode, Vector<DirectoryEntry>&&);
|
|
|
|
bool setInodeAllocationState(unsigned inode, bool);
|
2018-10-16 00:35:03 +02:00
|
|
|
bool setBlockAllocationState(GroupIndex, BlockIndex, bool);
|
|
|
|
|
|
|
|
bool modifyLinkCount(InodeIndex, int delta);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
unsigned m_blockGroupCount { 0 };
|
|
|
|
|
|
|
|
mutable ByteBuffer m_cachedSuperBlock;
|
|
|
|
mutable ByteBuffer m_cachedBlockGroupDescriptorTable;
|
2018-10-29 23:45:34 +01:00
|
|
|
|
|
|
|
mutable SpinLock m_inodeCacheLock;
|
|
|
|
mutable HashMap<unsigned, RetainPtr<CachedExt2InodeImpl>> m_inodeCache;
|
2018-11-13 13:02:39 +01:00
|
|
|
|
|
|
|
mutable SpinLock m_inode_cache_lock;
|
|
|
|
mutable HashMap<BlockIndex, RetainPtr<Ext2Inode>> m_inode_cache;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:02:39 +01:00
|
|
|
inline Ext2FileSystem& Ext2Inode::fs()
|
|
|
|
{
|
|
|
|
return static_cast<Ext2FileSystem&>(CoreInode::fs());
|
|
|
|
}
|
2018-11-13 13:32:16 +01:00
|
|
|
|
|
|
|
inline const Ext2FileSystem& Ext2Inode::fs() const
|
|
|
|
{
|
|
|
|
return static_cast<const Ext2FileSystem&>(CoreInode::fs());
|
|
|
|
}
|