Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.230.114 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796089695; b=H0lulwXMHzXYyCTOhjPJ5qtuWpwlI14iWIs/gG+DRS21JgcJ2DIzUMkmFWxAqGzQn0UP GpEXdq9/tZZTCKNOtVribZOugc0vR3W0m7cibwB2GDQTQkRGeGIZI9yJY+Ds5Bm+CAIJL W0g+EzBnyR34uitba5spv9eUWQ/Wr8yxaNJeixQji2DpRIMICqMOaIxMawdyunTz0KPUV jOVOZhsQEKDn61sdhHby6yvU7CupFS3n+m9aGIxzgdw0lrFFgavlLGQXmFWeWKcbuFyHh 5ouXAfFY6NbgfnkPLirQt/EDB04LR7xZuM6a2Ir9QbKUscQgecyRlQLkg7TBu7RFTjQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796089695; h=DMARC-Filter:DKIM-Signature:To:Mime-Version:From:Date:Message-Id: Content-Type:Content-Transfer-Encoding:References:Cc:Subject: X-Original-From:In-Reply-To:X-Lms-Return-Path; bh=0JYYtt0VI+5sCE1FpnA/eJnHsaJsoVNGNY93oe/P6ik=; b=aeykR5IT/7D2LUAlPmeGZQB0C9xMouw65H16aX3rf4lFxagNsbMr/WDliPLoG9W/w0HN XHV4kwewwTExiRCyC0Bvsj6FNjdr6Q3320zL9sOemI8LX0BAyrISbsOCA7jDhEv5eBjc8 s9u+3WbwsuLa1DRLqHMFJREXmIOkENYqqq0Qg2V1jf/EjBDq8uJg2iBt/zdVSk/uJeGqT DNNiviWhAL+Yz4Sby9vYmSNNfI1NZjX0RgYC6PBj2vkND/ABbBmIRj+KOGM9gDPtypfOA yQn1kIyCjTbuc8xVEAdntNm0i5T0FlbKx/Z9X2qDMAk8CuQgq+o+5sT2Kky3mf4RJ5A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=ghfKWGdO; arc=none smtp.remote-ip=209.127.230.114 Received: from va-1-114.ptr.blmpb.com (va-1-114.ptr.blmpb.com [209.127.230.114]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51905606A320 for ; Tue, 1 Dec 2026 01:48:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 51905606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=ghfKWGdO DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1796089684; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=0JYYtt0VI+5sCE1FpnA/eJnHsaJsoVNGNY93oe/P6ik=; b=ghfKWGdOk0k+IC40/C4dkCCWBAJ/AXX1JsrykoVmSjlCoIcKAl4IBS5t5MPBq12aBa/jcX MO7FGTsDnEIXLJCvOztcTLpIDjSNqi0vhuhpR3p+/mZGwlhZvEz4K2ljdXQmQ95wb5/iSZ ArHgZoTeihIq+b59Wvexk9/yXRyWtwno6OB2w7WGj41BNa7CJdLagQMJi5z+xZ4dhoRXG0 V2i3DlXMn3IMyBXJVBBkjCDFjIl2DJ2ib3OYLS53ZBB/ZwVGKFo+OUYMHiWOHciNfYjEAI r9KJFO4/Tb3EOGYEea5+ww1jSro6WL4A6ndb+HmdJbE5a6sNhgKc9MnLXz+7Ag== To: X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 From: "Xing Ma" Date: Tue, 01 Dec 2026 09:47:27 +0800 Message-Id: <20261201094725.1223999-3-maxing.lan@bytedance.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit References: <20261201094725.1223999-1-maxing.lan@bytedance.com> Cc: "Theodore Ts'o" , "Andreas Dilger" , "Jan Kara" , "Baokun Li" , "Ojaswin Mujoo" , "Ritesh Harjani (IBM)" , "Zhang Yi" , "Harshad Shirwadkar" , Subject: [PATCH v4 2/2] ext4: track truncated block range in ext4_truncate() for fast commit X-Original-From: Xing Ma In-Reply-To: <20261201094725.1223999-1-maxing.lan@bytedance.com> X-Lms-Return-Path: 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