Ext2FS: Clear out the direct block list when an inode is resized to 0

e2fsck was complaining about blocks being allocated in an inode's list
of direct blocks while at the same time being free in the block bitmap.

It was easy to reproduce by creating a file with non-zero length and
then truncating it. This fixes the issue by clearing out the direct
block list when resizing a file to 0.
This commit is contained in:
Andreas Kling 2020-11-23 12:30:12 +01:00
parent dd43cf2657
commit df758a5a51
Notes: sideshowbarker 2024-07-19 01:18:13 +09:00

View file

@ -227,6 +227,13 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in
{
LOCKER(m_lock);
if (blocks.is_empty()) {
e2inode.i_blocks = 0;
memset(e2inode.i_block, 0, sizeof(e2inode.i_block));
write_ext2_inode(inode_index, e2inode);
return true;
}
// NOTE: There is a mismatch between i_blocks and blocks.size() since i_blocks includes meta blocks and blocks.size() does not.
auto old_block_count = ceil_div(static_cast<size_t>(e2inode.i_size), block_size());