ext4: fix fast commit orphan inode replay and truncate range tracking
Subsystems (2)
cover ext4: fix fast commit orphan inode replay and truncate range tracking
This 2-patch series fixes two crash-recovery bugs in ext4 fast commit on tytso/ext4.git dev (aceadbccd428f):
1. Patch 1 ("ext4: fall back to full commit for unlinked or orphan inodes in fast commit"):
Fast commit does not log orphan list updates. Snapshotting an unlinked open inode (i_nlink == 0) emits EXT4_FC_TAG_INODE with i_links_count == 0, which causes ext4_fc_replay_inode() to fail ext4_iget() with -ESTALE -> -EFSCORRUPTED and abort journal recovery. Snapshotting a mid-truncate orphan inode logs shrunk i_size with unfreed extents. Fall back cleanly to a full JBD2 commit via -ECANCELED, and add KUnit coverage in fast_commit-test.c.
2. Patch 2 ("ext4: track truncated block range in ext4_truncate() for fast commit"):
ext4_fc_track_range() for truncation is only called in ext4_truncate_down(), missing same-size ftruncate(fd, oldsize) dropping FALLOC_FL_KEEP_SIZE post-EOF blocks, cross-transaction ext4_truncate() handles, and handle restarts in ext4_ext_remove_space(). Track [start_lblk, EXT_MAX_BLOCKS - 1] directly in ext4_truncate().
Testing on tytso/ext4.git dev (aceadbccd428f, -O fast_commit):
- Patch 1: xfstests generic/764 fails 100% before ("Structure needs cleaning" / "JBD2: journal recovery failed") and passes after; KUnit ext4_fast_commit_test 11/11 PASS.
- Patch 2: 4-step xfs_io + dm-flakey reproducer (falloc -k 4k 64k, sync, truncate 4k + fsync, crash/remount) resurrects 64K post-EOF blocks before (stat -c %b = 136 vs 8) and stays 8 after.
Xing Ma (2):
ext4: fall back to full commit for unlinked or orphan inodes in fast
commit
ext4: track truncated block range in ext4_truncate() for fast commit
fs/ext4/fast_commit-test.c | 18 ++++++++++++++++++ fs/ext4/fast_commit.c | 17 +++++++++++++++++ fs/ext4/inode.c | 8 ++++++++ 3 files changed, 43 insertions(+)
base-commit: aceadbccd428fb8e6593b27a3091d961f80225d1
Signature · 1 line
2.39.5
PATCH 1/2 ext4: fall back to full commit for unlinked or orphan inodes in fast commit
Fast commit does not log orphan list updates (s_last_orphan or orphan file). When a fast commit snapshots a tracked inode that is unlinked or on the orphan list, crash recovery fails in two ways:
1. Unlinked open inode (open+unlink, overwrite rename over an open file, or O_TMPFILE): the inode has i_nlink == 0 and stays on s_fc_q until closed. Emitting EXT4_FC_TAG_INODE with i_links_count == 0 causes ext4_fc_replay_inode() to reload it via ext4_iget() without EXT4_ORPHAN_FS, turning -ESTALE into -EFSCORRUPTED and aborting journal recovery ("JBD2: journal recovery failed"). This is deterministically reproduced by xfstests generic/764 with -O fast_commit.
2. Mid-truncate orphan inode: ext4_truncate_down() adds the inode to the orphan list, shrinks i_size/i_disksize, and stops its handle before ext4_truncate() frees blocks. Snapshotting the inode in this window logs the shrunk i_size with unfreed extents and no orphan entry, causing e2fsck to report "end of extent exceeds allowed value".
Return -ECANCELED from ext4_fc_snapshot_inode() when i_nlink == 0, ext4_inode_orphan_tracked(inode) is true, or raw i_links_count == 0 so ext4_fc_commit() falls back to a full JBD2 commit. Normal unlinks of closed files and completed truncates remain fast-commit eligible.
Also add KUnit coverage in test_fc_snapshot_inode_and_ranges() for i_nlink == 0 and EXT4_STATE_ORPHAN_FILE fallback.
--- fs/ext4/fast_commit-test.c | 18 ++++++++++++++++++ fs/ext4/fast_commit.c | 17 +++++++++++++++++ 2 files changed, 35 insertions(+)
fs/ext4/fast_commit-test.c +18 -0
@@ -1752,6 +1752,24 @@ static void test_fc_snapshot_inode_and_ranges(struct kunit *test) ext4_test_inode_state(file2, EXT4_STATE_FC_COMMITTING));KUNIT_EXPECT_NOT_NULL (test,EXT4_I (file2)->i_fc_snap);kvfree (inodes); + +/* +* 8) Unlinked (i_nlink == 0) or orphan-tracked inodes force fallback +* via -ECANCELED. +*/ +clear_nlink (file1); +KUNIT_EXPECT_EQ (test, +ext4_fc_snapshot_inode_test (file1,0 , &nr_ranges, + &snap_err), + -ECANCELED ); +set_nlink (file1,1 ); + +ext4_set_inode_state (file1,EXT4_STATE_ORPHAN_FILE ); +KUNIT_EXPECT_EQ (test, +ext4_fc_snapshot_inode_test (file1,0 , &nr_ranges, + &snap_err), + -ECANCELED ); +ext4_clear_inode_state (file1,EXT4_STATE_ORPHAN_FILE ); }/* =========================================================================
fs/ext4/fast_commit.c +17 -0
@@ -1119,6 +1119,18 @@ static int ext4_fc_snapshot_inode(struct inode *inode,int ret;int alloc_ctx; +/* +* Fast commit does not log orphan list updates, and +* ext4_fc_replay_inode() reloads replayed inodes with +* EXT4_IGET_NORMAL, which rejects i_nlink == 0 inodes with +* -ESTALE (causing journal recovery to abort with -EFSCORRUPTED). +* Fall back to a full commit whenever a tracked inode is +* currently unlinked or on the orphan list (e.g., open-unlinked, +* rename-overwritten while open, O_TMPFILE, or mid-truncate). +*/ +if (inode->i_nlink ==0 ||ext4_inode_orphan_tracked (inode)) +return -ECANCELED ; + ret =ext4_get_inode_loc_noio (inode, &iloc);if (ret) {atomic64_inc (&stats->snap_fail_inode_loc);
@@ -1126,6 +1138,11 @@ static int ext4_fc_snapshot_inode(struct inode *inode,return ret; } +if (le16_to_cpu (ext4_raw_inode (&iloc)->i_links_count) ==0 ) { +brelse (iloc.bh); +return -ECANCELED ; + } +if (ext4_test_inode_flag (inode,EXT4_INODE_INLINE_DATA )) inode_len =EXT4_INODE_SIZE (inode->i_sb);else if (EXT4_INODE_SIZE (inode->i_sb) >EXT4_GOOD_OLD_INODE_SIZE )
Signature · 1 line
2.39.5
PATCH 2/2 ext4: track truncated block range in ext4_truncate() for fast commit
In reply to Xing MaTruncation 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() 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 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
Call ext4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1) in ext4_truncate() right after ext4_ext_truncate() releases i_data_sem while the active journal handle is still open.
--- fs/ext4/inode.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/ext4/inode.c +8 -0
@@ -5605,6 +5605,14 @@ int ext4_truncate(struct inode *inode)ext4_ind_truncate (handle, inode);up_write (&ei->i_data_sem); +if (ext4_test_inode_flag (inode,EXT4_INODE_EXTENTS )) { +ext4_lblk_t start_lblk; + + start_lblk = inode->i_size >0 ? + (inode->i_size -1 ) >> inode->i_blkbits :0 ; +ext4_fc_track_range (handle, inode, start_lblk, +EXT_MAX_BLOCKS -1 ); + }if (err) goto out_stop;
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 2/2] ext4: track truncated block range in ext4_truncate() for fast commit
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ext4: NULL pointer dereference in ext4_truncate() on failed journal restart - [Medium] ext4…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ext4: NULL pointer dereference in ext4_truncate() on failed journal restart
- [Medium] ext4: ext4_truncate() skips ext4_fc_track_range() for indirect inodes
--
commit 80834a510a624cb3f489ed5a5d96c25207ac7e1e
Author: Xing Ma <[email protected]>
ext4: track truncated block range in ext4_truncate() for fast commit
This commit calls ext4_fc_track_range() in ext4_truncate() after releasing i_data_sem so fast commit records the truncated logical block range when ext4_truncate_down() was bypassed or ran in an earlier transaction.
fs/ext4/inode.c int ext4_truncate(struct inode *inode) · line 5608
Quoted 9 lines
fs/ext4/inode.c +1
-0@@ -5605,6 +5605,14 @@ int ext4_truncate(struct inode *inode)ext4_ind_truncate (handle, inode);up_write (&ei->i_data_sem); +if (ext4_test_inode_flag (inode,EXT4_INODE_EXTENTS )) {
[Severity: Medium]
Should ext4_fc_track_range() be called unconditionally here, as in ext4_truncate_down()?
ext4_fc_track_inode() is called unconditionally earlier in ext4_truncate(), which resets ei->i_fc_lblk_len to 0 in a new transaction, and ext4_ind_truncate() removes the truncated tail from the extent status tree and can also restart the journal handle. When an indirect-mapped inode is truncated without calling ext4_fc_track_range(), ei->i_fc_lblk_len stays 0, so ext4_fc_snapshot_inode_data() returns 0 with no ranges instead of hitting an extent status tree miss (-EAGAIN) and falling back to a full commit. Fast commit then writes an EXT4_FC_TAG_INODE record for the indirect-mapped inode, causing ext4_fc_replay_inode() -> ext4_ext_clear_bb() to fail journal recovery with -EFSCORRUPTED.
fs/ext4/inode.c int ext4_truncate(struct inode *inode) · line 5617
Quoted 9 lines
fs/ext4/inode.c +7
-0+ext4_lblk_t start_lblk; + + start_lblk = inode->i_size >0 ? + (inode->i_size -1 ) >> inode->i_blkbits :0 ; +ext4_fc_track_range (handle, inode, start_lblk, +EXT_MAX_BLOCKS -1 ); + }if (err) goto out_stop;
[Severity: High]
Can calling ext4_fc_track_range() before checking err cause a NULL pointer dereference on handle->h_transaction?
If ext4_ext_truncate() -> ext4_ext_remove_space() runs out of handle credits and calls jbd2__journal_restart() after the journal has aborted, stop_this_handle() sets handle->h_transaction = NULL before start_this_handle() fails with -EROFS. Calling ext4_fc_track_range() on that error path reaches ext4_fc_track_template(), which dereferences handle->h_transaction->t_tid before the if (err) check jumps to out_stop.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewSashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("ext4: track truncated block range in ext4_truncate()..."): 2 potential issues (1 High, 1 Medium)
A detailed review has been sent as a separate reply to patch 2.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]