From b0db0e5de0909ad151b9e3e3f40993ef83865443 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 24 Dec 2018 23:58:00 +0100 Subject: [PATCH] Get rid of Ext2FS::modify_link_count() in favor of Inode accessors. --- VirtualFileSystem/Ext2FileSystem.cpp | 30 +++++++++------------------- VirtualFileSystem/Ext2FileSystem.h | 2 -- VirtualFileSystem/FileSystem.cpp | 19 ++++++++++++++++++ VirtualFileSystem/FileSystem.h | 2 ++ 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/VirtualFileSystem/Ext2FileSystem.cpp b/VirtualFileSystem/Ext2FileSystem.cpp index 0bf5fa52b7b..730ec3f93f4 100644 --- a/VirtualFileSystem/Ext2FileSystem.cpp +++ b/VirtualFileSystem/Ext2FileSystem.cpp @@ -612,20 +612,6 @@ void Ext2FS::traverse_block_bitmap(unsigned groupIndex, F callback) const } } -bool Ext2FS::modify_link_count(InodeIndex inode, int delta) -{ - ASSERT(inode); - auto e2inode = lookup_ext2_inode(inode); - if (!e2inode) - return false; - - auto newLinkCount = e2inode->i_links_count + delta; - dbgprintf("Ext2FS: changing inode %u link count from %u to %u\n", inode, e2inode->i_links_count, newLinkCount); - e2inode->i_links_count = newLinkCount; - - return write_ext2_inode(inode, *e2inode); -} - bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode) { unsigned blockIndex; @@ -830,9 +816,9 @@ bool Ext2FS::set_block_allocation_state(GroupIndex group, BlockIndex bi, bool ne return true; } -RetainPtr Ext2FS::create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, int& error) +RetainPtr Ext2FS::create_directory(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, int& error) { - ASSERT(parentInode.fsid() == id()); + ASSERT(parent_id.fsid() == id()); // Fix up the mode to definitely be a directory. // FIXME: This is a bit on the hackish side. @@ -841,21 +827,23 @@ RetainPtr Ext2FS::create_directory(InodeIdentifier parentInode, const Str // NOTE: When creating a new directory, make the size 1 block. // There's probably a better strategy here, but this works for now. - auto inode = create_inode(parentInode, name, mode, blockSize(), error); + auto inode = create_inode(parent_id, name, mode, blockSize(), error); if (!inode) - return { }; + return nullptr; dbgprintf("Ext2FS: create_directory: created new directory named '%s' with inode %u\n", name.characters(), inode->identifier().index()); Vector entries; entries.append({ ".", inode->identifier(), EXT2_FT_DIR }); - entries.append({ "..", parentInode, EXT2_FT_DIR }); + entries.append({ "..", parent_id, EXT2_FT_DIR }); bool success = write_directory_inode(inode->identifier().index(), move(entries)); ASSERT(success); - success = modify_link_count(parentInode.index(), 1); - ASSERT(success); + auto parent_inode = get_inode(parent_id); + error = parent_inode->increment_link_count(); + if (error < 0) + return nullptr; auto& bgd = const_cast(group_descriptor(group_index_from_inode(inode->identifier().index()))); ++bgd.bg_used_dirs_count; diff --git a/VirtualFileSystem/Ext2FileSystem.h b/VirtualFileSystem/Ext2FileSystem.h index 52c849eebb7..3e4992f53de 100644 --- a/VirtualFileSystem/Ext2FileSystem.h +++ b/VirtualFileSystem/Ext2FileSystem.h @@ -94,8 +94,6 @@ private: bool set_inode_allocation_state(unsigned inode, bool); bool set_block_allocation_state(GroupIndex, BlockIndex, bool); - bool modify_link_count(InodeIndex, int delta); - unsigned m_blockGroupCount { 0 }; mutable ByteBuffer m_cached_super_block; diff --git a/VirtualFileSystem/FileSystem.cpp b/VirtualFileSystem/FileSystem.cpp index 567c9647c2b..b5429d09cd7 100644 --- a/VirtualFileSystem/FileSystem.cpp +++ b/VirtualFileSystem/FileSystem.cpp @@ -144,6 +144,25 @@ int Inode::set_mtime(Unix::time_t ts) return 0; } +int Inode::increment_link_count() +{ + if (fs().is_readonly()) + return -EROFS; + ++m_metadata.linkCount; + m_metadata_dirty = true; + return 0; +} + +int Inode::decrement_link_count() +{ + if (fs().is_readonly()) + return -EROFS; + ASSERT(m_metadata.linkCount); + --m_metadata.linkCount; + m_metadata_dirty = true; + return 0; +} + void FS::sync() { for (auto* inode : all_inodes()) { diff --git a/VirtualFileSystem/FileSystem.h b/VirtualFileSystem/FileSystem.h index cd5958885c5..dc7727e5f39 100644 --- a/VirtualFileSystem/FileSystem.h +++ b/VirtualFileSystem/FileSystem.h @@ -88,6 +88,8 @@ public: int set_atime(Unix::time_t); int set_ctime(Unix::time_t); int set_mtime(Unix::time_t); + int increment_link_count(); + int decrement_link_count(); virtual void flush_metadata() = 0;