2018-10-10 11:53:07 +02:00
|
|
|
#include "Ext2FileSystem.h"
|
|
|
|
#include "ext2_fs.h"
|
2018-10-14 22:57:41 +02:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/Bitmap.h>
|
2018-12-04 00:27:16 +01:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-13 00:44:54 +02:00
|
|
|
#include <AK/kmalloc.h>
|
2018-10-17 10:55:43 +02:00
|
|
|
#include <AK/ktime.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-24 12:43:52 +02:00
|
|
|
#include <AK/BufferStream.h>
|
2018-11-07 21:19:47 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-17 11:47:14 +02:00
|
|
|
//#define EXT2_DEBUG
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<Ext2FS> Ext2FS::create(RetainPtr<DiskDevice>&& device)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 17:13:10 +01:00
|
|
|
return adopt(*new Ext2FS(move(device)));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
Ext2FS::Ext2FS(RetainPtr<DiskDevice>&& device)
|
|
|
|
: DiskBackedFS(move(device))
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
Ext2FS::~Ext2FS()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
ByteBuffer Ext2FS::read_super_block() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-21 02:10:45 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(1024);
|
2018-12-03 01:38:22 +01:00
|
|
|
device().read_block(2, buffer.pointer());
|
2018-12-21 02:10:45 +01:00
|
|
|
device().read_block(3, buffer.offset_pointer(512));
|
2018-10-10 11:53:07 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool Ext2FS::write_super_block(const ext2_super_block& sb)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
const byte* raw = (const byte*)&sb;
|
|
|
|
bool success;
|
2018-12-03 01:38:22 +01:00
|
|
|
success = device().write_block(2, raw);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(success);
|
2018-12-03 01:38:22 +01:00
|
|
|
success = device().write_block(3, raw + 512);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(success);
|
|
|
|
// FIXME: This is an ugly way to refresh the superblock cache. :-|
|
2018-12-03 00:20:00 +01:00
|
|
|
super_block();
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::first_block_of_group(unsigned groupIndex) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
return super_block().s_first_data_block + (groupIndex * super_block().s_blocks_per_group);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
const ext2_super_block& Ext2FS::super_block() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
if (!m_cached_super_block)
|
|
|
|
m_cached_super_block = read_super_block();
|
|
|
|
return *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
const ext2_group_desc& Ext2FS::group_descriptor(unsigned groupIndex) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
// FIXME: Should this fail gracefully somehow?
|
|
|
|
ASSERT(groupIndex <= m_blockGroupCount);
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
if (!m_cached_group_descriptor_table) {
|
2018-10-16 00:35:03 +02:00
|
|
|
unsigned blocksToRead = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
|
2018-10-24 13:38:53 +02:00
|
|
|
#ifdef EXT2_DEBUG
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: block group count: %u, blocks-to-read: %u\n", m_blockGroupCount, blocksToRead);
|
|
|
|
kprintf("ext2fs: first block of BGDT: %u\n", firstBlockOfBGDT);
|
2018-10-24 13:38:53 +02:00
|
|
|
#endif
|
2018-12-03 00:20:00 +01:00
|
|
|
m_cached_group_descriptor_table = readBlocks(firstBlockOfBGDT, blocksToRead);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-12-03 00:20:00 +01:00
|
|
|
return reinterpret_cast<ext2_group_desc*>(m_cached_group_descriptor_table.pointer())[groupIndex - 1];
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool Ext2FS::initialize()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& superBlock = this->super_block();
|
2018-10-24 13:38:53 +02:00
|
|
|
#ifdef EXT2_DEBUG
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: super block magic: %x (super block size: %u)\n", superBlock.s_magic, sizeof(ext2_super_block));
|
2018-10-24 13:38:53 +02:00
|
|
|
#endif
|
2018-10-10 11:53:07 +02:00
|
|
|
if (superBlock.s_magic != EXT2_SUPER_MAGIC)
|
|
|
|
return false;
|
|
|
|
|
2018-10-24 13:38:53 +02:00
|
|
|
#ifdef EXT2_DEBUG
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: %u inodes, %u blocks\n", superBlock.s_inodes_count, superBlock.s_blocks_count);
|
|
|
|
kprintf("ext2fs: block size = %u\n", EXT2_BLOCK_SIZE(&superBlock));
|
|
|
|
kprintf("ext2fs: first data block = %u\n", superBlock.s_first_data_block);
|
|
|
|
kprintf("ext2fs: inodes per block = %u\n", inodesPerBlock());
|
|
|
|
kprintf("ext2fs: inodes per group = %u\n", inodesPerGroup());
|
|
|
|
kprintf("ext2fs: free inodes = %u\n", superBlock.s_free_inodes_count);
|
|
|
|
kprintf("ext2fs: desc per block = %u\n", EXT2_DESC_PER_BLOCK(&superBlock));
|
|
|
|
kprintf("ext2fs: desc size = %u\n", EXT2_DESC_SIZE(&superBlock));
|
2018-10-24 13:38:53 +02:00
|
|
|
#endif
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
setBlockSize(EXT2_BLOCK_SIZE(&superBlock));
|
|
|
|
|
|
|
|
m_blockGroupCount = ceilDiv(superBlock.s_blocks_count, superBlock.s_blocks_per_group);
|
|
|
|
|
|
|
|
if (m_blockGroupCount == 0) {
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: no block groups :(\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 21:31:56 +01:00
|
|
|
// Preheat the BGD cache.
|
2018-12-03 00:20:00 +01:00
|
|
|
group_descriptor(0);
|
2018-10-31 21:31:56 +01:00
|
|
|
|
2018-10-24 13:38:53 +02:00
|
|
|
#ifdef EXT2_DEBUG
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned i = 1; i <= m_blockGroupCount; ++i) {
|
|
|
|
auto& group = blockGroupDescriptor(i);
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: group[%u] { block_bitmap: %u, inode_bitmap: %u, inode_table: %u }\n",
|
2018-10-10 11:53:07 +02:00
|
|
|
i,
|
|
|
|
group.bg_block_bitmap,
|
|
|
|
group.bg_inode_bitmap,
|
|
|
|
group.bg_inode_table);
|
|
|
|
}
|
2018-10-24 13:38:53 +02:00
|
|
|
#endif
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
const char* Ext2FS::class_name() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
return "ext2fs";
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
InodeIdentifier Ext2FS::root_inode() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
return { id(), EXT2_ROOT_INO };
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef EXT2_DEBUG
|
|
|
|
static void dumpExt2Inode(const ext2_inode& inode)
|
|
|
|
{
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("Dump of ext2_inode:\n");
|
|
|
|
kprintf(" i_size: %u\n", inode.i_size);
|
|
|
|
kprintf(" i_mode: %u\n", inode.i_mode);
|
|
|
|
kprintf(" i_blocks: %u\n", inode.i_blocks);
|
|
|
|
kprintf(" i_uid: %u\n", inode.i_uid);
|
|
|
|
kprintf(" i_gid: %u\n", inode.i_gid);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
ByteBuffer Ext2FS::read_block_containing_inode(unsigned inode, unsigned& blockIndex, unsigned& offset) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& superBlock = this->super_block();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
if (inode != EXT2_ROOT_INO && inode < EXT2_FIRST_INO(&superBlock))
|
|
|
|
return { };
|
|
|
|
|
|
|
|
if (inode > superBlock.s_inodes_count)
|
|
|
|
return { };
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(group_index_from_inode(inode));
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
offset = ((inode - 1) % inodes_per_group()) * inode_size();
|
2018-10-10 11:53:07 +02:00
|
|
|
blockIndex = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&superBlock));
|
|
|
|
offset &= blockSize() - 1;
|
|
|
|
|
|
|
|
return readBlock(blockIndex);
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
OwnPtr<ext2_inode> Ext2FS::lookup_ext2_inode(unsigned inode) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
unsigned blockIndex;
|
|
|
|
unsigned offset;
|
2018-12-03 00:20:00 +01:00
|
|
|
auto block = read_block_containing_inode(inode, blockIndex, offset);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
if (!block)
|
2018-10-29 23:45:34 +01:00
|
|
|
return { };
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
auto* e2inode = reinterpret_cast<ext2_inode*>(kmalloc(inode_size()));
|
2018-12-21 02:10:45 +01:00
|
|
|
memcpy(e2inode, reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), inode_size());
|
2018-10-10 11:53:07 +02:00
|
|
|
#ifdef EXT2_DEBUG
|
|
|
|
dumpExt2Inode(*e2inode);
|
|
|
|
#endif
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
return OwnPtr<ext2_inode>(e2inode);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
Vector<unsigned> Ext2FS::block_list_for_inode(const ext2_inode& e2inode) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned entriesPerBlock = EXT2_ADDR_PER_BLOCK(&super_block());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// NOTE: i_blocks is number of 512-byte blocks, not number of fs-blocks.
|
|
|
|
unsigned blockCount = e2inode.i_blocks / (blockSize() / 512);
|
|
|
|
unsigned blocksRemaining = blockCount;
|
|
|
|
Vector<unsigned> list;
|
|
|
|
list.ensureCapacity(blocksRemaining);
|
|
|
|
|
|
|
|
unsigned directCount = min(blockCount, (unsigned)EXT2_NDIR_BLOCKS);
|
|
|
|
for (unsigned i = 0; i < directCount; ++i) {
|
2018-11-13 00:17:30 +01:00
|
|
|
list.unchecked_append(e2inode.i_block[i]);
|
2018-10-10 11:53:07 +02:00
|
|
|
--blocksRemaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blocksRemaining)
|
|
|
|
return list;
|
|
|
|
|
2018-11-13 00:17:30 +01:00
|
|
|
auto processBlockArray = [&] (unsigned arrayBlockIndex, auto&& callback) {
|
2018-10-10 11:53:07 +02:00
|
|
|
auto arrayBlock = readBlock(arrayBlockIndex);
|
|
|
|
ASSERT(arrayBlock);
|
|
|
|
auto* array = reinterpret_cast<const __u32*>(arrayBlock.pointer());
|
|
|
|
unsigned count = min(blocksRemaining, entriesPerBlock);
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
if (!array[i]) {
|
|
|
|
blocksRemaining = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(array[i]);
|
|
|
|
--blocksRemaining;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
processBlockArray(e2inode.i_block[EXT2_IND_BLOCK], [&] (unsigned entry) {
|
2018-11-13 00:17:30 +01:00
|
|
|
list.unchecked_append(entry);
|
2018-10-10 11:53:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!blocksRemaining)
|
|
|
|
return list;
|
|
|
|
|
|
|
|
processBlockArray(e2inode.i_block[EXT2_DIND_BLOCK], [&] (unsigned entry) {
|
|
|
|
processBlockArray(entry, [&] (unsigned entry) {
|
2018-11-13 00:17:30 +01:00
|
|
|
list.unchecked_append(entry);
|
2018-10-10 11:53:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!blocksRemaining)
|
|
|
|
return list;
|
|
|
|
|
|
|
|
processBlockArray(e2inode.i_block[EXT2_TIND_BLOCK], [&] (unsigned entry) {
|
|
|
|
processBlockArray(entry, [&] (unsigned entry) {
|
|
|
|
processBlockArray(entry, [&] (unsigned entry) {
|
2018-11-13 00:17:30 +01:00
|
|
|
list.unchecked_append(entry);
|
2018-10-10 11:53:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
Ext2FSInode::Ext2FSInode(Ext2FS& fs, unsigned index, const ext2_inode& raw_inode)
|
2018-12-19 21:18:28 +01:00
|
|
|
: Inode(fs, index)
|
2018-11-13 13:02:39 +01:00
|
|
|
, m_raw_inode(raw_inode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
Ext2FSInode::~Ext2FSInode()
|
2018-11-13 13:02:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
void Ext2FSInode::populate_metadata() const
|
2018-11-13 13:32:16 +01:00
|
|
|
{
|
|
|
|
m_metadata.inode = identifier();
|
|
|
|
m_metadata.size = m_raw_inode.i_size;
|
|
|
|
m_metadata.mode = m_raw_inode.i_mode;
|
|
|
|
m_metadata.uid = m_raw_inode.i_uid;
|
|
|
|
m_metadata.gid = m_raw_inode.i_gid;
|
|
|
|
m_metadata.linkCount = m_raw_inode.i_links_count;
|
|
|
|
m_metadata.atime = m_raw_inode.i_atime;
|
|
|
|
m_metadata.ctime = m_raw_inode.i_ctime;
|
|
|
|
m_metadata.mtime = m_raw_inode.i_mtime;
|
|
|
|
m_metadata.dtime = m_raw_inode.i_dtime;
|
|
|
|
m_metadata.blockSize = fs().blockSize();
|
|
|
|
m_metadata.blockCount = m_raw_inode.i_blocks;
|
|
|
|
|
|
|
|
if (isBlockDevice(m_raw_inode.i_mode) || isCharacterDevice(m_raw_inode.i_mode)) {
|
|
|
|
unsigned dev = m_raw_inode.i_block[0];
|
|
|
|
m_metadata.majorDevice = (dev & 0xfff00) >> 8;
|
|
|
|
m_metadata.minorDevice= (dev & 0xff) | ((dev >> 12) & 0xfff00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 21:56:45 +01:00
|
|
|
void Ext2FSInode::flush_metadata()
|
|
|
|
{
|
2018-12-20 00:39:29 +01:00
|
|
|
dbgprintf("Ext2FSInode: flush_metadata for inode %u\n", index());
|
2018-12-19 21:56:45 +01:00
|
|
|
m_raw_inode.i_size = m_metadata.size;
|
|
|
|
m_raw_inode.i_mode = m_metadata.mode;
|
|
|
|
m_raw_inode.i_uid = m_metadata.uid;
|
|
|
|
m_raw_inode.i_gid = m_metadata.gid;
|
|
|
|
m_raw_inode.i_links_count = m_metadata.linkCount;
|
|
|
|
m_raw_inode.i_atime = m_metadata.atime;
|
|
|
|
m_raw_inode.i_ctime = m_metadata.ctime;
|
|
|
|
m_raw_inode.i_mtime = m_metadata.mtime;
|
|
|
|
m_raw_inode.i_dtime = m_metadata.dtime;
|
|
|
|
m_raw_inode.i_blocks = m_metadata.blockCount;
|
|
|
|
fs().write_ext2_inode(index(), m_raw_inode);
|
2018-12-19 22:28:09 +01:00
|
|
|
set_metadata_dirty(false);
|
2018-12-19 21:56:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:18:28 +01:00
|
|
|
RetainPtr<Inode> Ext2FS::get_inode(InodeIdentifier inode) const
|
2018-11-13 13:02:39 +01:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
ASSERT(inode.fsid() == id());
|
2018-11-13 13:02:39 +01:00
|
|
|
{
|
|
|
|
LOCKER(m_inode_cache_lock);
|
|
|
|
auto it = m_inode_cache.find(inode.index());
|
|
|
|
if (it != m_inode_cache.end())
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2018-12-03 00:20:00 +01:00
|
|
|
auto raw_inode = lookup_ext2_inode(inode.index());
|
2018-11-13 13:02:39 +01:00
|
|
|
if (!raw_inode)
|
|
|
|
return nullptr;
|
|
|
|
LOCKER(m_inode_cache_lock);
|
|
|
|
auto it = m_inode_cache.find(inode.index());
|
|
|
|
if (it != m_inode_cache.end())
|
|
|
|
return (*it).value;
|
2018-11-15 17:13:10 +01:00
|
|
|
auto new_inode = adopt(*new Ext2FSInode(const_cast<Ext2FS&>(*this), inode.index(), *raw_inode));
|
2018-11-13 13:02:39 +01:00
|
|
|
m_inode_cache.set(inode.index(), new_inode.copyRef());
|
|
|
|
return new_inode;
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t Ext2FSInode::read_bytes(Unix::off_t offset, size_t count, byte* buffer, FileDescriptor*)
|
2018-11-13 13:02:39 +01:00
|
|
|
{
|
|
|
|
ASSERT(offset >= 0);
|
|
|
|
if (m_raw_inode.i_size == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Symbolic links shorter than 60 characters are store inline inside the i_block array.
|
|
|
|
// This avoids wasting an entire block on short links. (Most links are short.)
|
|
|
|
static const unsigned max_inline_symlink_length = 60;
|
|
|
|
if (is_symlink() && size() < max_inline_symlink_length) {
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t nread = min((Unix::off_t)size() - offset, static_cast<Unix::off_t>(count));
|
2018-11-13 13:02:39 +01:00
|
|
|
memcpy(buffer, m_raw_inode.i_block + offset, nread);
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2018-12-21 02:10:45 +01:00
|
|
|
if (m_block_list.is_empty()) {
|
2018-12-03 00:20:00 +01:00
|
|
|
auto block_list = fs().block_list_for_inode(m_raw_inode);
|
2018-11-13 13:02:39 +01:00
|
|
|
LOCKER(m_lock);
|
|
|
|
if (m_block_list.size() != block_list.size())
|
|
|
|
m_block_list = move(block_list);
|
|
|
|
}
|
|
|
|
|
2018-12-21 02:10:45 +01:00
|
|
|
if (m_block_list.is_empty()) {
|
2018-11-13 13:02:39 +01:00
|
|
|
kprintf("ext2fs: read_bytes: empty block list for inode %u\n", index());
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t block_size = fs().blockSize();
|
|
|
|
|
|
|
|
dword first_block_logical_index = offset / block_size;
|
|
|
|
dword last_block_logical_index = (offset + count) / block_size;
|
|
|
|
if (last_block_logical_index >= m_block_list.size())
|
|
|
|
last_block_logical_index = m_block_list.size() - 1;
|
|
|
|
|
|
|
|
dword offset_into_first_block = offset % block_size;
|
|
|
|
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t nread = 0;
|
|
|
|
size_t remaining_count = min((Unix::off_t)count, (Unix::off_t)size() - offset);
|
2018-11-13 13:02:39 +01:00
|
|
|
byte* out = buffer;
|
|
|
|
|
|
|
|
#ifdef EXT2_DEBUG
|
|
|
|
kprintf("ok let's do it, read(%llu, %u) -> blocks %u thru %u, oifb: %u\n", offset, count, firstBlockLogicalIndex, lastBlockLogicalIndex, offsetIntoFirstBlock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (dword bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; ++bi) {
|
|
|
|
auto block = fs().readBlock(m_block_list[bi]);
|
|
|
|
if (!block) {
|
|
|
|
kprintf("ext2fs: read_bytes: readBlock(%u) failed (lbi: %u)\n", m_block_list[bi], bi);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
dword offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
|
|
|
|
dword num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
|
|
|
|
memcpy(out, block.pointer() + offset_into_block, num_bytes_to_copy);
|
|
|
|
remaining_count -= num_bytes_to_copy;
|
|
|
|
nread += num_bytes_to_copy;
|
|
|
|
out += num_bytes_to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2018-12-25 00:10:32 +01:00
|
|
|
bool Ext2FSInode::write(const ByteBuffer& data)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
// FIXME: Support writing to symlink inodes.
|
2018-12-25 00:10:32 +01:00
|
|
|
ASSERT(!m_metadata.isSymbolicLink());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-25 00:10:32 +01:00
|
|
|
unsigned blocksNeededBefore = ceilDiv(size(), fs().blockSize());
|
|
|
|
unsigned blocksNeededAfter = ceilDiv((unsigned)data.size(), fs().blockSize());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// FIXME: Support growing or shrinking the block list.
|
|
|
|
ASSERT(blocksNeededBefore == blocksNeededAfter);
|
|
|
|
|
2018-12-25 00:10:32 +01:00
|
|
|
auto list = fs().block_list_for_inode(m_raw_inode);
|
2018-12-21 02:10:45 +01:00
|
|
|
if (list.is_empty()) {
|
2018-12-25 00:10:32 +01:00
|
|
|
kprintf("ext2fs: writeInode: empty block list for inode %u\n", index());
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < list.size(); ++i) {
|
2018-12-25 00:10:32 +01:00
|
|
|
auto section = data.slice(i * fs().blockSize(), fs().blockSize());
|
2018-11-18 14:57:41 +01:00
|
|
|
//kprintf("section = %p (%u)\n", section.pointer(), section.size());
|
2018-12-25 00:10:32 +01:00
|
|
|
bool success = fs().writeBlock(list[i], section);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback)
|
2018-11-13 23:44:54 +01:00
|
|
|
{
|
|
|
|
ASSERT(metadata().isDirectory());
|
|
|
|
|
|
|
|
#ifdef EXT2_DEBUG
|
|
|
|
kprintf("Ext2Inode::traverse_as_directory: inode=%u:\n", index());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto buffer = read_entire();
|
|
|
|
ASSERT(buffer);
|
|
|
|
auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.pointer());
|
|
|
|
|
2018-12-21 02:10:45 +01:00
|
|
|
while (entry < buffer.end_pointer()) {
|
2018-11-13 23:44:54 +01:00
|
|
|
if (entry->inode != 0) {
|
|
|
|
#ifdef EXT2_DEBUG
|
|
|
|
kprintf("Ext2Inode::traverse_as_directory: %u, name_len: %u, rec_len: %u, file_type: %u, name: %s\n", entry->inode, entry->name_len, entry->rec_len, entry->file_type, namebuf);
|
|
|
|
#endif
|
|
|
|
if (!callback({ entry->name, entry->name_len, { fsid(), entry->inode }, entry->file_type }))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-25 00:27:39 +01:00
|
|
|
bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-25 00:27:39 +01:00
|
|
|
ASSERT(is_directory());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
//#ifdef EXT2_DEBUG
|
2018-12-25 00:27:39 +01:00
|
|
|
dbgprintf("Ext2FS: Adding inode %u with name '%s' to directory %u\n", child_id.index(), name.characters(), index());
|
2018-10-10 11:53:07 +02:00
|
|
|
//#endif
|
|
|
|
|
2018-12-25 00:27:39 +01:00
|
|
|
Vector<FS::DirectoryEntry> entries;
|
|
|
|
bool name_already_exists = false;
|
|
|
|
traverse_as_directory([&] (auto& entry) {
|
2018-11-13 00:17:30 +01:00
|
|
|
if (!strcmp(entry.name, name.characters())) {
|
2018-12-25 00:27:39 +01:00
|
|
|
name_already_exists = true;
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
entries.append(entry);
|
|
|
|
return true;
|
|
|
|
});
|
2018-12-25 00:27:39 +01:00
|
|
|
if (name_already_exists) {
|
|
|
|
kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
|
2018-11-18 14:57:41 +01:00
|
|
|
error = -EEXIST;
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-25 00:27:39 +01:00
|
|
|
entries.append({ name.characters(), name.length(), child_id, file_type });
|
|
|
|
return fs().write_directory_inode(index(), move(entries));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool Ext2FS::write_directory_inode(unsigned directoryInode, Vector<DirectoryEntry>&& entries)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: New directory inode %u contents to write:\n", directoryInode);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
unsigned directorySize = 0;
|
|
|
|
for (auto& entry : entries) {
|
2018-11-18 14:57:41 +01:00
|
|
|
//kprintf(" - %08u %s\n", entry.inode.index(), entry.name);
|
2018-11-13 00:17:30 +01:00
|
|
|
directorySize += EXT2_DIR_REC_LEN(entry.name_length);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned blocksNeeded = ceilDiv(directorySize, blockSize());
|
|
|
|
unsigned occupiedSize = blocksNeeded * blockSize();
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: directory size: %u (occupied: %u)\n", directorySize, occupiedSize);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-21 02:10:45 +01:00
|
|
|
auto directoryData = ByteBuffer::create_uninitialized(occupiedSize);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
BufferStream stream(directoryData);
|
|
|
|
for (unsigned i = 0; i < entries.size(); ++i) {
|
|
|
|
auto& entry = entries[i];
|
|
|
|
|
2018-11-13 00:17:30 +01:00
|
|
|
unsigned recordLength = EXT2_DIR_REC_LEN(entry.name_length);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (i == entries.size() - 1)
|
|
|
|
recordLength += occupiedSize - directorySize;
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("* inode: %u", entry.inode.index());
|
|
|
|
dbgprintf(", name_len: %u", word(entry.name_length));
|
|
|
|
dbgprintf(", rec_len: %u", word(recordLength));
|
|
|
|
dbgprintf(", file_type: %u", byte(entry.fileType));
|
|
|
|
dbgprintf(", name: %s\n", entry.name);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
stream << dword(entry.inode.index());
|
|
|
|
stream << word(recordLength);
|
2018-11-13 00:17:30 +01:00
|
|
|
stream << byte(entry.name_length);
|
2018-10-10 11:53:07 +02:00
|
|
|
stream << byte(entry.fileType);
|
|
|
|
stream << entry.name;
|
|
|
|
|
2018-11-13 00:17:30 +01:00
|
|
|
unsigned padding = recordLength - entry.name_length - 8;
|
2018-11-18 14:57:41 +01:00
|
|
|
//dbgprintf(" *** pad %u bytes\n", padding);
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned j = 0; j < padding; ++j) {
|
|
|
|
stream << byte(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.fillToEnd(0);
|
|
|
|
|
|
|
|
#if 0
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("data to write (%u):\n", directoryData.size());
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned i = 0; i < directoryData.size(); ++i) {
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("%02x ", directoryData[i]);
|
2018-10-10 11:53:07 +02:00
|
|
|
if ((i + 1) % 8 == 0)
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf(" ");
|
2018-10-10 11:53:07 +02:00
|
|
|
if ((i + 1) % 16 == 0)
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-25 00:10:32 +01:00
|
|
|
return get_inode({ id(), directoryInode })->write(directoryData);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::inodes_per_block() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
return EXT2_INODES_PER_BLOCK(&super_block());
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::inodes_per_group() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
return EXT2_INODES_PER_GROUP(&super_block());
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::inode_size() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
return EXT2_INODE_SIZE(&super_block());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
}
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::blocks_per_group() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
return EXT2_BLOCKS_PER_GROUP(&super_block());
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
void Ext2FS::dump_block_bitmap(unsigned groupIndex) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
ASSERT(groupIndex <= m_blockGroupCount);
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(groupIndex);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned blocksInGroup = min(blocks_per_group(), super_block().s_blocks_count);
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned blockCount = ceilDiv(blocksInGroup, 8u);
|
|
|
|
|
|
|
|
auto bitmapBlocks = readBlocks(bgd.bg_block_bitmap, blockCount);
|
|
|
|
ASSERT(bitmapBlocks);
|
|
|
|
|
2018-10-31 20:10:39 +01:00
|
|
|
kprintf("ext2fs: group[%u] block bitmap (bitmap occupies %u blocks):\n", groupIndex, blockCount);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
auto bitmap = Bitmap::wrap(bitmapBlocks.pointer(), blocksInGroup);
|
|
|
|
for (unsigned i = 0; i < blocksInGroup; ++i) {
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("%c", bitmap.get(i) ? '1' : '0');
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
void Ext2FS::dump_inode_bitmap(unsigned groupIndex) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
traverse_inode_bitmap(groupIndex, [] (unsigned, const Bitmap& bitmap) {
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned i = 0; i < bitmap.size(); ++i)
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("%c", bitmap.get(i) ? '1' : '0');
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
2018-12-03 00:20:00 +01:00
|
|
|
void Ext2FS::traverse_inode_bitmap(unsigned groupIndex, F callback) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
ASSERT(groupIndex <= m_blockGroupCount);
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(groupIndex);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned inodesInGroup = min(inodes_per_group(), super_block().s_inodes_count);
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned blockCount = ceilDiv(inodesInGroup, 8u);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < blockCount; ++i) {
|
|
|
|
auto block = readBlock(bgd.bg_inode_bitmap + i);
|
|
|
|
ASSERT(block);
|
2018-10-15 02:39:55 +02:00
|
|
|
bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), inodesInGroup));
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!shouldContinue)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:35:03 +02:00
|
|
|
template<typename F>
|
2018-12-03 00:20:00 +01:00
|
|
|
void Ext2FS::traverse_block_bitmap(unsigned groupIndex, F callback) const
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
|
|
|
ASSERT(groupIndex <= m_blockGroupCount);
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(groupIndex);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned blocksInGroup = min(blocks_per_group(), super_block().s_blocks_count);
|
2018-10-16 00:35:03 +02:00
|
|
|
unsigned blockCount = ceilDiv(blocksInGroup, 8u);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < blockCount; ++i) {
|
|
|
|
auto block = readBlock(bgd.bg_block_bitmap + i);
|
|
|
|
ASSERT(block);
|
|
|
|
bool shouldContinue = callback(i * (blockSize() / 8) + 1, Bitmap::wrap(block.pointer(), blocksInGroup));
|
|
|
|
if (!shouldContinue)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
unsigned blockIndex;
|
|
|
|
unsigned offset;
|
2018-12-03 00:20:00 +01:00
|
|
|
auto block = read_block_containing_inode(inode, blockIndex, offset);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (!block)
|
|
|
|
return false;
|
2018-11-18 14:57:41 +01:00
|
|
|
{
|
|
|
|
LOCKER(m_inode_cache_lock);
|
|
|
|
auto it = m_inode_cache.find(inode);
|
|
|
|
if (it != m_inode_cache.end()) {
|
|
|
|
auto& cached_inode = *(*it).value;
|
|
|
|
LOCKER(cached_inode.m_lock);
|
|
|
|
cached_inode.m_raw_inode = e2inode;
|
|
|
|
cached_inode.populate_metadata();
|
|
|
|
if (cached_inode.is_directory())
|
|
|
|
cached_inode.m_lookup_cache.clear();
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 02:10:45 +01:00
|
|
|
memcpy(reinterpret_cast<ext2_inode*>(block.offset_pointer(offset)), &e2inode, inode_size());
|
2018-10-10 11:53:07 +02:00
|
|
|
writeBlock(blockIndex, block);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(unsigned group, unsigned count)
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: allocateBlocks(group: %u, count: %u)\n", group, count);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(group);
|
2018-10-16 00:35:03 +02:00
|
|
|
if (bgd.bg_free_blocks_count < count) {
|
2018-11-18 14:57:41 +01:00
|
|
|
kprintf("ExtFS: allocateBlocks can't allocate out of group %u, wanted %u but only %u available\n", group, count, bgd.bg_free_blocks_count);
|
2018-10-16 00:35:03 +02:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Implement a scan that finds consecutive blocks if possible.
|
|
|
|
Vector<BlockIndex> blocks;
|
2018-12-03 00:20:00 +01:00
|
|
|
traverse_block_bitmap(group, [&blocks, count] (unsigned firstBlockInBitmap, const Bitmap& bitmap) {
|
2018-10-16 00:35:03 +02:00
|
|
|
for (unsigned i = 0; i < bitmap.size(); ++i) {
|
|
|
|
if (!bitmap.get(i)) {
|
|
|
|
blocks.append(firstBlockInBitmap + i);
|
|
|
|
if (blocks.size() == count)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: allocateBlock found these blocks:\n");
|
2018-10-16 00:35:03 +02:00
|
|
|
for (auto& bi : blocks) {
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf(" > %u\n", bi);
|
2018-10-16 00:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::allocate_inode(unsigned preferredGroup, unsigned expectedSize)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: allocateInode(preferredGroup: %u, expectedSize: %u)\n", preferredGroup, expectedSize);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
unsigned neededBlocks = ceilDiv(expectedSize, blockSize());
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: minimum needed blocks: %u\n", neededBlocks);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
unsigned groupIndex = 0;
|
|
|
|
|
|
|
|
auto isSuitableGroup = [this, neededBlocks] (unsigned groupIndex) {
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(groupIndex);
|
2018-10-10 11:53:07 +02:00
|
|
|
return bgd.bg_free_inodes_count && bgd.bg_free_blocks_count >= neededBlocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (preferredGroup && isSuitableGroup(preferredGroup)) {
|
|
|
|
groupIndex = preferredGroup;
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 1; i <= m_blockGroupCount; ++i) {
|
|
|
|
if (isSuitableGroup(i))
|
|
|
|
groupIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!groupIndex) {
|
2018-11-18 14:57:41 +01:00
|
|
|
kprintf("Ext2FS: allocateInode: no suitable group found for new inode with %u blocks needed :(\n", neededBlocks);
|
2018-10-10 11:53:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: allocateInode: found suitable group [%u] for new inode with %u blocks needed :^)\n", groupIndex, neededBlocks);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
unsigned firstFreeInodeInGroup = 0;
|
2018-12-03 00:20:00 +01:00
|
|
|
traverse_inode_bitmap(groupIndex, [&firstFreeInodeInGroup] (unsigned firstInodeInBitmap, const Bitmap& bitmap) {
|
2018-10-10 11:53:07 +02:00
|
|
|
for (unsigned i = 0; i < bitmap.size(); ++i) {
|
|
|
|
if (!bitmap.get(i)) {
|
|
|
|
firstFreeInodeInGroup = firstInodeInBitmap + i;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!firstFreeInodeInGroup) {
|
2018-11-18 14:57:41 +01:00
|
|
|
kprintf("Ext2FS: firstFreeInodeInGroup returned no inode, despite bgd claiming there are inodes :(\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned inode = firstFreeInodeInGroup;
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: found suitable inode %u\n", inode);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// FIXME: allocate blocks if needed!
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned Ext2FS::group_index_from_inode(unsigned inode) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
if (!inode)
|
|
|
|
return 0;
|
2018-12-03 00:20:00 +01:00
|
|
|
return (inode - 1) / inodes_per_group() + 1;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool Ext2FS::set_inode_allocation_state(unsigned inode, bool newState)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(group_index_from_inode(inode));
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// Update inode bitmap
|
|
|
|
unsigned inodesPerBitmapBlock = blockSize() * 8;
|
|
|
|
unsigned bitmapBlockIndex = (inode - 1) / inodesPerBitmapBlock;
|
|
|
|
unsigned bitIndex = (inode - 1) % inodesPerBitmapBlock;
|
|
|
|
auto block = readBlock(bgd.bg_inode_bitmap + bitmapBlockIndex);
|
|
|
|
ASSERT(block);
|
|
|
|
auto bitmap = Bitmap::wrap(block.pointer(), block.size());
|
|
|
|
bool currentState = bitmap.get(bitIndex);
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("ext2fs: setInodeAllocationState(%u) %u -> %u\n", inode, currentState, newState);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
if (currentState == newState)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bitmap.set(bitIndex, newState);
|
|
|
|
writeBlock(bgd.bg_inode_bitmap + bitmapBlockIndex, block);
|
|
|
|
|
|
|
|
// Update superblock
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: superblock free inode count %u -> %u\n", sb.s_free_inodes_count, sb.s_free_inodes_count - 1);
|
2018-10-10 11:53:07 +02:00
|
|
|
if (newState)
|
|
|
|
--sb.s_free_inodes_count;
|
|
|
|
else
|
|
|
|
++sb.s_free_inodes_count;
|
2018-12-03 00:20:00 +01:00
|
|
|
write_super_block(sb);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// Update BGD
|
|
|
|
auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
|
|
|
|
if (newState)
|
|
|
|
--mutableBGD.bg_free_inodes_count;
|
|
|
|
else
|
|
|
|
++mutableBGD.bg_free_inodes_count;
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: group free inode count %u -> %u\n", bgd.bg_free_inodes_count, bgd.bg_free_inodes_count - 1);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-16 00:35:03 +02:00
|
|
|
unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
|
|
|
|
unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
|
2018-12-03 00:20:00 +01:00
|
|
|
writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool Ext2FS::set_block_allocation_state(GroupIndex group, BlockIndex bi, bool newState)
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& bgd = group_descriptor(group);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
// Update block bitmap
|
|
|
|
unsigned blocksPerBitmapBlock = blockSize() * 8;
|
|
|
|
unsigned bitmapBlockIndex = (bi - 1) / blocksPerBitmapBlock;
|
|
|
|
unsigned bitIndex = (bi - 1) % blocksPerBitmapBlock;
|
|
|
|
auto block = readBlock(bgd.bg_block_bitmap + bitmapBlockIndex);
|
|
|
|
ASSERT(block);
|
|
|
|
auto bitmap = Bitmap::wrap(block.pointer(), block.size());
|
|
|
|
bool currentState = bitmap.get(bitIndex);
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: setBlockAllocationState(%u) %u -> %u\n", bi, currentState, newState);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
if (currentState == newState)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bitmap.set(bitIndex, newState);
|
|
|
|
writeBlock(bgd.bg_block_bitmap + bitmapBlockIndex, block);
|
|
|
|
|
|
|
|
// Update superblock
|
2018-12-03 00:20:00 +01:00
|
|
|
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: superblock free block count %u -> %u\n", sb.s_free_blocks_count, sb.s_free_blocks_count - 1);
|
2018-10-16 00:35:03 +02:00
|
|
|
if (newState)
|
|
|
|
--sb.s_free_blocks_count;
|
|
|
|
else
|
|
|
|
++sb.s_free_blocks_count;
|
2018-12-03 00:20:00 +01:00
|
|
|
write_super_block(sb);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
// Update BGD
|
|
|
|
auto& mutableBGD = const_cast<ext2_group_desc&>(bgd);
|
|
|
|
if (newState)
|
|
|
|
--mutableBGD.bg_free_blocks_count;
|
|
|
|
else
|
|
|
|
++mutableBGD.bg_free_blocks_count;
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: group free block count %u -> %u\n", bgd.bg_free_blocks_count, bgd.bg_free_blocks_count - 1);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
|
2018-12-03 00:20:00 +01:00
|
|
|
writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-24 23:58:00 +01:00
|
|
|
RetainPtr<Inode> Ext2FS::create_directory(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, int& error)
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
2018-12-24 23:58:00 +01:00
|
|
|
ASSERT(parent_id.fsid() == id());
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
// Fix up the mode to definitely be a directory.
|
|
|
|
// FIXME: This is a bit on the hackish side.
|
|
|
|
mode &= ~0170000;
|
|
|
|
mode |= 0040000;
|
|
|
|
|
|
|
|
// NOTE: When creating a new directory, make the size 1 block.
|
|
|
|
// There's probably a better strategy here, but this works for now.
|
2018-12-24 23:58:00 +01:00
|
|
|
auto inode = create_inode(parent_id, name, mode, blockSize(), error);
|
2018-12-24 23:44:46 +01:00
|
|
|
if (!inode)
|
2018-12-24 23:58:00 +01:00
|
|
|
return nullptr;
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-12-24 23:44:46 +01:00
|
|
|
dbgprintf("Ext2FS: create_directory: created new directory named '%s' with inode %u\n", name.characters(), inode->identifier().index());
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
Vector<DirectoryEntry> entries;
|
2018-12-24 23:44:46 +01:00
|
|
|
entries.append({ ".", inode->identifier(), EXT2_FT_DIR });
|
2018-12-24 23:58:00 +01:00
|
|
|
entries.append({ "..", parent_id, EXT2_FT_DIR });
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-12-24 23:44:46 +01:00
|
|
|
bool success = write_directory_inode(inode->identifier().index(), move(entries));
|
2018-10-16 00:35:03 +02:00
|
|
|
ASSERT(success);
|
|
|
|
|
2018-12-24 23:58:00 +01:00
|
|
|
auto parent_inode = get_inode(parent_id);
|
|
|
|
error = parent_inode->increment_link_count();
|
|
|
|
if (error < 0)
|
|
|
|
return nullptr;
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-12-24 23:44:46 +01:00
|
|
|
auto& bgd = const_cast<ext2_group_desc&>(group_descriptor(group_index_from_inode(inode->identifier().index())));
|
2018-10-16 00:35:03 +02:00
|
|
|
++bgd.bg_used_dirs_count;
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: incremented bg_used_dirs_count %u -> %u\n", bgd.bg_used_dirs_count - 1, bgd.bg_used_dirs_count);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
unsigned blocksToWrite = ceilDiv(m_blockGroupCount * (unsigned)sizeof(ext2_group_desc), blockSize());
|
|
|
|
unsigned firstBlockOfBGDT = blockSize() == 1024 ? 2 : 1;
|
2018-12-03 00:20:00 +01:00
|
|
|
writeBlocks(firstBlockOfBGDT, blocksToWrite, m_cached_group_descriptor_table);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
error = 0;
|
2018-10-16 00:35:03 +02:00
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2018-12-25 00:27:39 +01:00
|
|
|
RetainPtr<Inode> Ext2FS::create_inode(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, unsigned size, int& error)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-25 00:27:39 +01:00
|
|
|
ASSERT(parent_id.fsid() == id());
|
|
|
|
auto parent_inode = get_inode(parent_id);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-25 00:27:39 +01:00
|
|
|
dbgprintf("Ext2FS: Adding inode '%s' (mode %u) to parent directory %u:\n", name.characters(), mode, parent_inode->identifier().index());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
// NOTE: This doesn't commit the inode allocation just yet!
|
2018-12-24 23:44:46 +01:00
|
|
|
auto inode_id = allocate_inode(0, 0);
|
|
|
|
if (!inode_id) {
|
2018-12-25 00:27:39 +01:00
|
|
|
kprintf("Ext2FS: createInode: allocate_inode failed\n");
|
2018-11-18 14:57:41 +01:00
|
|
|
error = -ENOSPC;
|
2018-10-16 00:35:03 +02:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2018-12-24 23:44:46 +01:00
|
|
|
auto blocks = allocate_blocks(group_index_from_inode(inode_id), ceilDiv(size, blockSize()));
|
2018-12-21 02:10:45 +01:00
|
|
|
if (blocks.is_empty()) {
|
2018-12-25 00:27:39 +01:00
|
|
|
kprintf("Ext2FS: createInode: allocate_blocks failed\n");
|
2018-11-18 14:57:41 +01:00
|
|
|
error = -ENOSPC;
|
2018-10-16 00:35:03 +02:00
|
|
|
return { };
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-15 01:57:57 +02:00
|
|
|
byte fileType = 0;
|
|
|
|
if (isRegularFile(mode))
|
|
|
|
fileType = EXT2_FT_REG_FILE;
|
|
|
|
else if (isDirectory(mode))
|
|
|
|
fileType = EXT2_FT_DIR;
|
|
|
|
else if (isCharacterDevice(mode))
|
|
|
|
fileType = EXT2_FT_CHRDEV;
|
|
|
|
else if (isBlockDevice(mode))
|
|
|
|
fileType = EXT2_FT_BLKDEV;
|
|
|
|
else if (isFIFO(mode))
|
|
|
|
fileType = EXT2_FT_FIFO;
|
|
|
|
else if (isSocket(mode))
|
|
|
|
fileType = EXT2_FT_SOCK;
|
|
|
|
else if (isSymbolicLink(mode))
|
|
|
|
fileType = EXT2_FT_SYMLINK;
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
// Try adding it to the directory first, in case the name is already in use.
|
2018-12-25 00:27:39 +01:00
|
|
|
bool success = parent_inode->add_child({ id(), inode_id }, name, fileType, error);
|
2018-11-18 14:57:41 +01:00
|
|
|
if (!success)
|
2018-10-10 11:53:07 +02:00
|
|
|
return { };
|
|
|
|
|
|
|
|
// Looks like we're good, time to update the inode bitmap and group+global inode counters.
|
2018-12-24 23:44:46 +01:00
|
|
|
success = set_inode_allocation_state(inode_id, true);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(success);
|
|
|
|
|
2018-10-16 00:35:03 +02:00
|
|
|
for (auto bi : blocks) {
|
2018-12-24 23:44:46 +01:00
|
|
|
success = set_block_allocation_state(group_index_from_inode(inode_id), bi, true);
|
2018-10-16 00:35:03 +02:00
|
|
|
ASSERT(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned initialLinksCount;
|
|
|
|
if (isDirectory(mode))
|
|
|
|
initialLinksCount = 2; // (parent directory + "." entry in self)
|
|
|
|
else
|
|
|
|
initialLinksCount = 1;
|
|
|
|
|
2018-10-17 11:40:58 +02:00
|
|
|
auto timestamp = ktime(nullptr);
|
2018-10-10 11:53:07 +02:00
|
|
|
auto e2inode = make<ext2_inode>();
|
|
|
|
memset(e2inode.ptr(), 0, sizeof(ext2_inode));
|
|
|
|
e2inode->i_mode = mode;
|
|
|
|
e2inode->i_uid = 0;
|
2018-10-16 00:35:03 +02:00
|
|
|
e2inode->i_size = size;
|
2018-10-10 11:53:07 +02:00
|
|
|
e2inode->i_atime = timestamp;
|
|
|
|
e2inode->i_ctime = timestamp;
|
|
|
|
e2inode->i_mtime = timestamp;
|
|
|
|
e2inode->i_dtime = 0;
|
|
|
|
e2inode->i_gid = 0;
|
2018-10-16 00:35:03 +02:00
|
|
|
e2inode->i_links_count = initialLinksCount;
|
|
|
|
e2inode->i_blocks = blocks.size() * (blockSize() / 512);
|
|
|
|
|
|
|
|
// FIXME: Implement writing out indirect blocks!
|
|
|
|
ASSERT(blocks.size() < EXT2_NDIR_BLOCKS);
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
dbgprintf("Ext2FS: writing %zu blocks to i_block array\n", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()));
|
2018-10-16 14:11:58 +02:00
|
|
|
for (unsigned i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i) {
|
2018-10-16 00:35:03 +02:00
|
|
|
e2inode->i_block[i] = blocks[i];
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
e2inode->i_flags = 0;
|
2018-12-24 23:44:46 +01:00
|
|
|
success = write_ext2_inode(inode_id, *e2inode);
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(success);
|
|
|
|
|
2018-12-24 23:44:46 +01:00
|
|
|
return get_inode({ id(), inode_id });
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
InodeIdentifier Ext2FS::find_parent_of_inode(InodeIdentifier inode_id) const
|
2018-10-28 12:20:25 +01:00
|
|
|
{
|
2018-11-13 23:44:54 +01:00
|
|
|
auto inode = get_inode(inode_id);
|
|
|
|
ASSERT(inode);
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
unsigned groupIndex = group_index_from_inode(inode->index());
|
|
|
|
unsigned firstInodeInGroup = inodes_per_group() * (groupIndex - 1);
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
Vector<RetainPtr<Ext2FSInode>> directories_in_group;
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
for (unsigned i = 0; i < inodes_per_group(); ++i) {
|
2018-11-13 23:44:54 +01:00
|
|
|
auto group_member = get_inode({ id(), firstInodeInGroup + i });
|
|
|
|
if (!group_member)
|
2018-10-28 12:20:25 +01:00
|
|
|
continue;
|
2018-11-13 23:44:54 +01:00
|
|
|
if (group_member->is_directory())
|
|
|
|
directories_in_group.append(move(group_member));
|
2018-10-28 12:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InodeIdentifier foundParent;
|
2018-11-13 23:44:54 +01:00
|
|
|
for (auto& directory : directories_in_group) {
|
2018-12-21 02:10:45 +01:00
|
|
|
if (!directory->reverse_lookup(inode->identifier()).is_null()) {
|
2018-11-15 17:04:55 +01:00
|
|
|
foundParent = directory->identifier();
|
2018-10-28 12:20:25 +01:00
|
|
|
break;
|
2018-11-15 17:04:55 +01:00
|
|
|
}
|
2018-10-28 12:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return foundParent;
|
|
|
|
}
|
2018-11-15 16:34:36 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
void Ext2FSInode::populate_lookup_cache()
|
2018-11-15 16:34:36 +01:00
|
|
|
{
|
2018-11-15 17:04:55 +01:00
|
|
|
{
|
2018-11-15 16:34:36 +01:00
|
|
|
LOCKER(m_lock);
|
2018-12-21 02:10:45 +01:00
|
|
|
if (!m_lookup_cache.is_empty())
|
2018-11-15 17:04:55 +01:00
|
|
|
return;
|
2018-11-15 16:34:36 +01:00
|
|
|
}
|
2018-11-15 17:04:55 +01:00
|
|
|
HashMap<String, unsigned> children;
|
|
|
|
|
|
|
|
traverse_as_directory([&children] (auto& entry) {
|
|
|
|
children.set(String(entry.name, entry.name_length), entry.inode.index());
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
LOCKER(m_lock);
|
2018-12-21 02:10:45 +01:00
|
|
|
if (!m_lookup_cache.is_empty())
|
2018-11-15 17:04:55 +01:00
|
|
|
return;
|
|
|
|
m_lookup_cache = move(children);
|
|
|
|
}
|
2018-11-15 16:34:36 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
InodeIdentifier Ext2FSInode::lookup(const String& name)
|
2018-11-15 17:04:55 +01:00
|
|
|
{
|
|
|
|
ASSERT(is_directory());
|
|
|
|
populate_lookup_cache();
|
2018-11-15 16:34:36 +01:00
|
|
|
LOCKER(m_lock);
|
2018-11-15 17:04:55 +01:00
|
|
|
auto it = m_lookup_cache.find(name);
|
|
|
|
if (it != m_lookup_cache.end())
|
2018-11-15 16:34:36 +01:00
|
|
|
return { fsid(), (*it).value };
|
2018-11-15 17:04:55 +01:00
|
|
|
return { };
|
|
|
|
}
|
2018-11-15 16:34:36 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
String Ext2FSInode::reverse_lookup(InodeIdentifier child_id)
|
2018-11-15 17:04:55 +01:00
|
|
|
{
|
|
|
|
ASSERT(is_directory());
|
|
|
|
ASSERT(child_id.fsid() == fsid());
|
|
|
|
populate_lookup_cache();
|
|
|
|
LOCKER(m_lock);
|
|
|
|
for (auto it : m_lookup_cache) {
|
|
|
|
if (it.value == child_id.index())
|
|
|
|
return it.key;
|
|
|
|
}
|
2018-11-15 16:34:36 +01:00
|
|
|
return { };
|
|
|
|
}
|