diff --git a/ext/filesystem.cpp b/ext/filesystem.cpp index b53f28e5..d18cf541 100644 --- a/ext/filesystem.cpp +++ b/ext/filesystem.cpp @@ -50,6 +50,7 @@ Filesystem::Filesystem(Device* device) block_size = device->block_size; mru_inode = NULL; lru_inode = NULL; + dirty_inode = NULL; inode_size = this->sb->s_inode_size; num_blocks = sb->s_blocks_count; num_groups = divup(this->sb->s_blocks_count, this->sb->s_blocks_per_group); @@ -90,8 +91,8 @@ void Filesystem::Dirty() void Filesystem::Sync() { - for ( Inode* iter = mru_inode; iter; iter = iter->next_inode ) - iter->Sync(); + while ( dirty_inode ) + dirty_inode->Sync(); for ( size_t i = 0; i < num_groups; i++ ) if ( block_groups && block_groups[i] ) block_groups[i]->Sync(); diff --git a/ext/filesystem.h b/ext/filesystem.h index 13e3abd0..dba12006 100644 --- a/ext/filesystem.h +++ b/ext/filesystem.h @@ -45,6 +45,7 @@ public: uint32_t num_inodes; Inode* mru_inode; Inode* lru_inode; + Inode* dirty_inode; time_t mtime_realtime; time_t mtime_monotonic; bool dirty; diff --git a/ext/inode.cpp b/ext/inode.cpp index 9f4fd43a..85eb8113 100644 --- a/ext/inode.cpp +++ b/ext/inode.cpp @@ -53,6 +53,8 @@ Inode::Inode(Filesystem* filesystem, uint32_t inode_id) { this->prev_inode = NULL; this->next_inode = NULL; + this->prev_dirty = NULL; + this->next_dirty = NULL; this->filesystem = filesystem; this->reference_count = 1; this->inode_id = inode_id; @@ -969,15 +971,29 @@ void Inode::RemoteUnref() void Inode::Dirty() { - dirty = true; + if ( !dirty ) + { + dirty = true; + prev_dirty = NULL; + next_dirty = filesystem->dirty_inode; + if ( next_dirty ) + next_dirty->prev_dirty = this; + filesystem->dirty_inode = this; + } data_block->Dirty(); Use(); } void Inode::Sync() { - if ( dirty ) - data_block->Sync(); + if ( !dirty ) + return; + data_block->Sync(); + (prev_dirty ? prev_dirty->next_dirty : filesystem->dirty_inode) = next_dirty; + if ( next_dirty ) + next_dirty->prev_dirty = prev_dirty; + prev_dirty = NULL; + next_dirty = NULL; dirty = false; } diff --git a/ext/inode.h b/ext/inode.h index 879bf3da..e2a5613d 100644 --- a/ext/inode.h +++ b/ext/inode.h @@ -35,6 +35,8 @@ public: public: Inode* prev_inode; Inode* next_inode; + Inode* prev_dirty; + Inode* next_dirty; Block* data_block; struct ext_inode* data; Filesystem* filesystem;