From 8cc2d90123486a812278a0b0e2d98f8166607602 Mon Sep 17 00:00:00 2001 From: Franciszek Pindel Date: Fri, 29 Mar 2024 13:01:08 +0100 Subject: [PATCH] fs: ext2: Fix removing indirect blocks This commit fixes removing indirect blocks (marking them as 0) in the inode structure. Previous version of the code was removing the top-level blocks only when the first removed block was one of the first 12 direct blocks. However, when the first removed block is the first block in the referenced block list, its parent (indirect block) should also be removed. Signed-off-by: Franciszek Pindel --- subsys/fs/ext2/ext2_diskops.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/subsys/fs/ext2/ext2_diskops.c b/subsys/fs/ext2/ext2_diskops.c index 92fd298d64..7e6016a8ba 100644 --- a/subsys/fs/ext2/ext2_diskops.c +++ b/subsys/fs/ext2/ext2_diskops.c @@ -669,9 +669,11 @@ int64_t ext2_inode_remove_blocks(struct ext2_inode *inode, uint32_t first) max_lvl = get_level_offsets(inode->i_fs, first, offsets); - if (all_zero(offsets, max_lvl)) { - /* We remove also the first block because all blocks referenced from it will be - * deleted. + if (all_zero(&offsets[1], max_lvl)) { + /* The first block to remove is either: + * - one of the first 12 blocks in the indode + * - the first referenced block in the indirect block list; + * we remove also the indirect block */ start = offsets[0]; } else {