Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Xing Ma" <maxing.lan@bytedance.com>
To: <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>,
	 "Andreas Dilger" <adilger.kernel@dilger.ca>,
	"Jan Kara" <jack@suse.cz>,
	 "Baokun Li" <libaokun@linux.alibaba.com>,
	 "Ojaswin Mujoo" <ojaswin@linux.ibm.com>,
	 "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	 "Zhang Yi" <yi.zhang@huawei.com>,
	 "Harshad Shirwadkar" <harshadshirwadkar@gmail.com>,
	 <stable@vger.kernel.org>
Subject: [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for fast commit
Date: Tue, 01 Dec 2026 07:06:00 +0800	[thread overview]
Message-ID: <20261201070558.4017023-3-maxing.lan@bytedance.com> (raw)
In-Reply-To: <20261201070558.4017023-1-maxing.lan@bytedance.com>

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.  Tracking that range without
populating the hole entry causes ext4_fc_snapshot_inode_data() to fail
with EXT4_FC_SNAP_ERR_ES_MISS and fall back to a full commit (even on a
no-op same-size ftruncate() + fsync()).

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
ext4_truncate(), call ext4_fc_track_range() for [last_block,
EXT_MAX_BLOCKS - 1] after checking err and !ext4_handle_is_aborted().
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 <maxing.lan@bytedance.com>
---
 fs/ext4/extents.c |  5 +++++
 fs/ext4/inode.c   | 10 ++++++++++
 2 files changed, 15 insertions(+)

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..78823397d4245 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);
 
-- 
2.39.5

  parent reply	other threads:[~2026-11-30 23:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 23:05 [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking Xing Ma
2026-11-30 23:05 ` [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit Xing Ma
2026-11-30 23:06 ` Xing Ma [this message]
2026-11-30 23:28   ` [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for " sashiko-bot
2026-11-30 23:28 ` [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201070558.4017023-3-maxing.lan@bytedance.com \
    --to=maxing.lan@bytedance.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=harshadshirwadkar@gmail.com \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    --cc=yi.zhang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox