Truncation range tracking (ext4_fc_track_range()) is currently only called in ext4_truncate_down(), which misses three cases where ext4_truncate() frees extents: 1. Same-size ftruncate(fd, oldsize) dropping FALLOC_FL_KEEP_SIZE preallocated blocks beyond EOF jumps straight to ext4_truncate() without calling ext4_fc_track_range(). 2. If a commit occurs between ext4_truncate_down() stopping its handle and ext4_truncate() starting a new handle in transaction T+1, ext4_fc_track_inode() resets i_fc_lblk_len to 0 and freed extents are not tracked. 3. If ext4_ext_remove_space() or ext4_ind_truncate() restarts the journal handle onto a new transaction, earlier tracking belonged to the previous transaction. In all three cases, a subsequent fast commit omits EXT4_FC_TAG_DEL_RANGE and resurrects freed extents (or emits an indirect-mapped inode without falling back to a full commit) after crash recovery. Case 1 reproduces deterministically on -O fast_commit: xfs_io -f -c "pwrite -S 0xaa 0 4k" -c "falloc -k 4k 64k" mnt/foo sync xfs_io -c "truncate 4k" -c "fsync" mnt/foo # crash (dm-flakey drop_writes) and remount: # stat -c %b mnt/foo returns 136 instead of 8 Furthermore, unlike ext4_punch_hole(), ext4_ext_truncate() removes extent status entries for [last_block, EXT_MAX_BLOCKS - 1] without inserting an EXTENT_STATUS_HOLE entry, and ext4_truncate_down() tracked from (newsize - 1) >> i_blkbits (last_block - 1) instead of last_block. Without the hole entry and matching start block, ext4_fc_snapshot_inode_data() hits EXT4_FC_SNAP_ERR_ES_MISS and falls back to a full commit on both same-size ftruncate() and cold-cache block-aligned shrinking truncate. In ext4_ext_truncate(), insert an EXTENT_STATUS_HOLE entry covering [last_block, EXT_MAX_BLOCKS - 1] after ext4_ext_remove_space() succeeds when JOURNAL_FAST_COMMIT is enabled and the inode is still linked. In both ext4_truncate() (after checking err and !ext4_handle_is_aborted()) and ext4_truncate_down(), track [last_block, EXT_MAX_BLOCKS - 1] using the same last_block calculation. Extent-based inodes then emit EXT4_FC_TAG_DEL_RANGE cleanly via fast commit, while indirect-mapped inodes have no hole entry inserted and continue to fall back safely to a full commit. Fixes: aa75f4d3daae ("ext4: main fast-commit commit path") Cc: stable@vger.kernel.org Signed-off-by: Xing Ma --- fs/ext4/extents.c | 5 +++++ fs/ext4/inode.c | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index b65b0261ee5c3..a47b56e3cbe9c 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4608,6 +4608,11 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode) memalloc_retry_wait(GFP_ATOMIC); goto retry_remove_space; } + if (!err && inode->i_nlink && test_opt2(sb, JOURNAL_FAST_COMMIT) && + last_block < EXT_MAX_BLOCKS) + ext4_es_insert_extent(inode, last_block, + EXT_MAX_BLOCKS - last_block, ~0, + EXTENT_STATUS_HOLE, 0); return err; } diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 07a62d8569d31..ccf112e19bdba 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -5608,6 +5608,16 @@ int ext4_truncate(struct inode *inode) if (err) goto out_stop; + if (!ext4_handle_is_aborted(handle)) { + ext4_lblk_t last_block; + + last_block = (inode->i_size + inode->i_sb->s_blocksize - 1) + >> inode->i_blkbits; + if (last_block < EXT_MAX_BLOCKS) + ext4_fc_track_range(handle, inode, last_block, + EXT_MAX_BLOCKS - 1); + } + if (IS_SYNC(inode)) ext4_handle_sync(handle); @@ -7063,7 +7073,7 @@ static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize) static int ext4_truncate_down(struct inode *inode, loff_t oldsize, loff_t newsize, int *orphan) { - ext4_lblk_t start_lblk; + ext4_lblk_t last_block; handle_t *handle; int ret; @@ -7085,8 +7095,11 @@ static int ext4_truncate_down(struct inode *inode, loff_t oldsize, } } - start_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0; - ext4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1); + last_block = (newsize + inode->i_sb->s_blocksize - 1) + >> inode->i_blkbits; + if (last_block < EXT_MAX_BLOCKS) + ext4_fc_track_range(handle, inode, last_block, + EXT_MAX_BLOCKS - 1); down_write(&EXT4_I(inode)->i_data_sem); /* -- 2.39.5