From nobody Sat Feb 7 22:07:18 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D7C32E696; Mon, 19 Jan 2026 17:43:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844609; cv=none; b=qSYuf9HmahbGgyZ78Dak7F1vdT/ukpIU6kbPxiivSKlMOpBnEPARxn/zqx0zSoUuRsqwLjjifwo/ZzS8jC8b8/M+R2jynNNMvMecMbtDmjwMePg4kWajnVGmv2jqjYfA1KIJc1hDkw0+QZV+/lexq+PENHza1KGAjO9ZRwq6ISM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844609; c=relaxed/simple; bh=cPriq5hZHa1YDBM8YYMNMzhGERO10t1ypUs4OPZ2SHI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IkZTbgn8pgc7EyxWDVjc+p/rj6mFoU5FvTdq7Cgj+f5+juPgSVMpko4ihi86wb+NaF02Kz5MKRTyoBp+TTbe8fgBbWVxlI+Z+S3NQsjfgbbQH2Vrkq5NclTcWTIfSbiUiShwTlMtq49VQYVvRS8sNeDzw3I6aQRLTJvGNGBBZE8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=o5TAHU2g; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="o5TAHU2g" Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JCo03u019031; Mon, 19 Jan 2026 17:43:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=7hjg5Ob8ZRvP13fwz IIc+rcDpdQh6GOfOsbgB/ueneM=; b=o5TAHU2grbQFpe/APtXa+k4V6aAMWm4Zb oW33XBBcb8Rz/n1u8TMQ0H4CkrkA9APQMb4AfyjiT+3PANJEs0amV8RF9OxiNtgw rR0FMj2Ufy+ei6+qqZ9ClDIKFrsPIrvXckBZKuxZsEjYaKx/RdrABbxhI8yfdmz4 NLHOQFoLOFShLp6tFpunp00HbyRuu1cNw5med7PsZuaJ6kdwtLnSvJdweMtzWMDy sqPlyN5RkUZehlr59W83EB+qwfO7cIptIINHeQB0LB0Jr5FxUJhjOqevzw4i5qXQ x6m3phYEiL9nHOcxzl2xvt5SPjOwtiRevWc414BQjZ3w1juQ5ZK1w== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4bqyuk198n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:14 +0000 (GMT) Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHhDAl021021; Mon, 19 Jan 2026 17:43:13 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4bqyuk198g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:13 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JHMCND001159; Mon, 19 Jan 2026 17:43:12 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4brpyjg03e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:12 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhBqs30933432 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:11 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EBF3320043; Mon, 19 Jan 2026 17:43:10 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BF37F20040; Mon, 19 Jan 2026 17:43:08 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:08 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/8] ext4: kunit tests for extent splitting and conversion Date: Mon, 19 Jan 2026 23:12:57 +0530 Message-ID: <1e1e16b544927f7c2f28cfb8c53f59af8fe9ccf7.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX+SYJv72ESrmw yMf6YvgvFzKgQCWEG19gJxDxvRR5x1RwXN1D45k/HDMbtQnx/6whDGWh4I8/DoS61Q6J9gv7es3 LBvlTE3sPf6JllvJCI4evM9tqhvGmkuBxUPsef6vUK0GvNzk/3uV4QhJn1l/et3/9X1tqpgYLh5 g0NzDhimfsQBxta1ciXujH7PjzW3yB6CrDgsaZQV7Uc/82GgjvHqu90b/ePhbVs5/hav+HE4XU+ v0mznrf/WjZbV3XWGIf651n8AMaud0xBNQLZsyVd9mIyDPNf/2YeZY/ii4wem1MBny6rpSyB9ZM J8iSgECEIXcM9qYfMkbyzAOik1ZmxJxVBkxA+7m+wZGGvb2mfPPmhys+e9yXnV64SyPvJV4Rhdr rD9Ab+xAFifMV9WTFQ/zBQcYaGivXAokW0M1H8LMTajjHdz0FTUp6/8NJy4LzcfzOA8To6rulC6 eNeld22F6A+86E+y7eQ== X-Authority-Analysis: v=2.4 cv=bsBBxUai c=1 sm=1 tr=0 ts=696e6d32 cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=i0EeH86SAAAA:8 a=OOxyGJNtmdNL3i7zqkUA:9 X-Proofpoint-ORIG-GUID: _BnFDsjT0Rcannsu47aEdKUEE6jbVS05 X-Proofpoint-GUID: wBYdLs1Y6eQO0TH5Tq4pn5pow8NomIYz X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 suspectscore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 malwarescore=0 clxscore=1015 adultscore=0 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Add multiple KUnit tests to test various permutations of extent splitting and conversion. We test the following cases: 1. Split of unwritten extent into 2 parts and convert 1 part to written 2. Split of unwritten extent into 3 parts and convert 1 part to written 3. Split of written extent into 2 parts and convert 1 part to unwritten 4. Split of written extent into 3 parts and convert 1 part to unwritten 5. Zeroout fallback for all the above cases except 3-4 because zeroout is not supported for written to unwritten splits The main function we test here is ext4_split_convert_extents(). Currently some of the tests are failing due to issues in implementation. All failures are mitigated at other layers in ext4 [1] but still point out the mismatch in expectation of what the caller wants vs what the function does. The aim is to eventually fix all the failures we see here. More detailed implementation notes can be found in the topmost commit in the test file. [1] for example, EXT4_GET_BLOCKS_CONVERT doesn't really convert the split extent to written, but rather the callers end up doing the conversion. Reviewed-by: Zhang Yi Reviewed-by: Jan Kara Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents-test.c | 556 +++++++++++++++++++++++++++++++++++++++ fs/ext4/extents.c | 23 +- fs/ext4/extents_status.c | 3 + fs/ext4/inode.c | 4 + 4 files changed, 584 insertions(+), 2 deletions(-) create mode 100644 fs/ext4/extents-test.c diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c new file mode 100644 index 000000000000..f68e43d924db --- /dev/null +++ b/fs/ext4/extents-test.c @@ -0,0 +1,556 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Written by Ojaswin Mujoo (IBM) + * + * These Kunit tests are designed to test the functionality of + * extent split and conversion in ext4. + * + * Currently, ext4 can split extents in 2 ways: + * 1. By splitting the extents in the extent tree and optionally convertin= g them + * to written or unwritten based on flags passed. + * 2. In case 1 encounters an error, ext4 instead zerooes out the unwritten + * areas of the extent and marks the complete extent written. + * + * The primary function that handles this is ext4_split_convert_extents(). + * + * We test both of the methods of split. The behavior we try to enforce is: + * 1. When passing EXT4_GET_BLOCKS_CONVERT flag to ext4_split_convert_exte= nts(), + * the split extent should be converted to initialized. + * 2. When passing EXT4_GET_BLOCKS_CONVERT_UNWRITTEN flag to + * ext4_split_convert_extents(), the split extent should be converted to + * uninitialized. + * 3. In case we use the zeroout method, then we should correctly write ze= roes + * to the unwritten areas of the extent and we should not corrupt/leak = any + * data. + * + * Enforcing 1 and 2 is straight forward, we just setup a minimal inode wi= th + * extent tree, call ext4_split_convert_extents() and check the final stat= e of + * the extent tree. + * + * For zeroout testing, we maintain a separate buffer which represents the= disk + * data corresponding to the extents. We then override ext4's zeroout func= tions + * to instead write zeroes to our buffer. Then, we override + * ext4_ext_insert_extent() to return -ENOSPC, which triggers the zeroout. + * Finally, we check the state of the extent tree and zeroout buffer to co= nfirm + * everything went well. + */ + +#include +#include +#include +#include + +#include "ext4.h" +#include "ext4_extents.h" + +#define EX_DATA_PBLK 100 +#define EX_DATA_LBLK 10 +#define EX_DATA_LEN 3 + +struct kunit_ctx { + /* + * Ext4 inode which has only 1 unwrit extent + */ + struct ext4_inode_info *k_ei; + /* + * Represents the underlying data area (used for zeroout testing) + */ + char *k_data; +} k_ctx; + +/* + * describes the state of an expected extent in extent tree. + */ +struct kunit_ext_state { + ext4_lblk_t ex_lblk; + ext4_lblk_t ex_len; + bool is_unwrit; +}; + +/* + * describes the state of the data area of a writ extent. Used for testing + * correctness of zeroout. + */ +struct kunit_ext_data_state { + char exp_char; + ext4_lblk_t off_blk; + ext4_lblk_t len_blk; +}; + +struct kunit_ext_test_param { + /* description of test */ + char *desc; + + /* is extent unwrit at beginning of test */ + bool is_unwrit_at_start; + + /* flags to pass while splitting */ + int split_flags; + + /* map describing range to split */ + struct ext4_map_blocks split_map; + + /* no of extents expected after split */ + int nr_exp_ext; + + /* + * expected state of extents after split. We will never split into more + * than 3 extents + */ + struct kunit_ext_state exp_ext_state[3]; + + /* Below fields used for zeroout tests */ + + bool is_zeroout_test; + /* + * no of expected data segments (zeroout tests). Example, if we expect + * data to be 4kb 0s, followed by 8kb non-zero, then nr_exp_data_segs=3D= =3D2 + */ + int nr_exp_data_segs; + + /* + * expected state of data area after zeroout. + */ + struct kunit_ext_data_state exp_data_state[3]; +}; + +static void ext_kill_sb(struct super_block *sb) +{ + generic_shutdown_super(sb); +} + +static int ext_set(struct super_block *sb, void *data) +{ + return 0; +} + +static struct file_system_type ext_fs_type =3D { + .name =3D "extents test", + .kill_sb =3D ext_kill_sb, +}; + +static void extents_kunit_exit(struct kunit *test) +{ + kfree(k_ctx.k_ei); + kfree(k_ctx.k_data); +} + +static void ext4_cache_extents_stub(struct inode *inode, + struct ext4_extent_header *eh) +{ + return; +} + +static int __ext4_ext_dirty_stub(const char *where, unsigned int line, + handle_t *handle, struct inode *inode, + struct ext4_ext_path *path) +{ + return 0; +} + +static struct ext4_ext_path * +ext4_ext_insert_extent_stub(handle_t *handle, struct inode *inode, + struct ext4_ext_path *path, + struct ext4_extent *newext, int gb_flags) +{ + return ERR_PTR(-ENOSPC); +} + +static void ext4_es_remove_extent_stub(struct inode *inode, ext4_lblk_t lb= lk, + ext4_lblk_t len) +{ + return; +} + +static void ext4_zeroout_es_stub(struct inode *inode, struct ext4_extent *= ex) +{ + return; +} + +/* + * We will zeroout the equivalent range in the data area + */ +static int ext4_ext_zeroout_stub(struct inode *inode, struct ext4_extent *= ex) +{ + ext4_lblk_t ee_block, off_blk; + loff_t ee_len; + loff_t off_bytes; + struct kunit *test =3D kunit_get_current_test(); + + ee_block =3D le32_to_cpu(ex->ee_block); + ee_len =3D ext4_ext_get_actual_len(ex); + + KUNIT_EXPECT_EQ_MSG(test, 1, ee_block >=3D EX_DATA_LBLK, "ee_block=3D%d", + ee_block); + KUNIT_EXPECT_EQ(test, 1, + ee_block + ee_len <=3D EX_DATA_LBLK + EX_DATA_LEN); + + off_blk =3D ee_block - EX_DATA_LBLK; + off_bytes =3D off_blk << inode->i_sb->s_blocksize_bits; + memset(k_ctx.k_data + off_bytes, 0, + ee_len << inode->i_sb->s_blocksize_bits); + + return 0; +} + +static int ext4_issue_zeroout_stub(struct inode *inode, ext4_lblk_t lblk, + ext4_fsblk_t pblk, ext4_lblk_t len) +{ + ext4_lblk_t off_blk; + loff_t off_bytes; + struct kunit *test =3D kunit_get_current_test(); + + kunit_log(KERN_ALERT, test, + "%s: lblk=3D%u pblk=3D%llu len=3D%u", __func__, lblk, pblk, len); + KUNIT_EXPECT_EQ(test, 1, lblk >=3D EX_DATA_LBLK); + KUNIT_EXPECT_EQ(test, 1, lblk + len <=3D EX_DATA_LBLK + EX_DATA_LEN); + KUNIT_EXPECT_EQ(test, 1, lblk - EX_DATA_LBLK =3D=3D pblk - EX_DATA_PBLK); + + off_blk =3D lblk - EX_DATA_LBLK; + off_bytes =3D off_blk << inode->i_sb->s_blocksize_bits; + memset(k_ctx.k_data + off_bytes, 0, + len << inode->i_sb->s_blocksize_bits); + + return 0; +} + +static int extents_kunit_init(struct kunit *test) +{ + struct ext4_extent_header *eh =3D NULL; + struct ext4_inode_info *ei; + struct inode *inode; + struct super_block *sb; + struct kunit_ext_test_param *param =3D + (struct kunit_ext_test_param *)(test->param_value); + + /* setup the mock inode */ + k_ctx.k_ei =3D kzalloc(sizeof(struct ext4_inode_info), GFP_KERNEL); + if (k_ctx.k_ei =3D=3D NULL) + return -ENOMEM; + ei =3D k_ctx.k_ei; + inode =3D &ei->vfs_inode; + + sb =3D sget(&ext_fs_type, NULL, ext_set, 0, NULL); + if (IS_ERR(sb)) + return PTR_ERR(sb); + + sb->s_blocksize =3D 4096; + sb->s_blocksize_bits =3D 12; + + ei->i_disksize =3D (EX_DATA_LBLK + EX_DATA_LEN + 10) << sb->s_blocksize_b= its; + inode->i_sb =3D sb; + + k_ctx.k_data =3D kzalloc(EX_DATA_LEN * 4096, GFP_KERNEL); + if (k_ctx.k_data =3D=3D NULL) + return -ENOMEM; + + /* + * set the data area to a junk value + */ + memset(k_ctx.k_data, 'X', EX_DATA_LEN * 4096); + + /* create a tree with depth 0 */ + eh =3D (struct ext4_extent_header *)k_ctx.k_ei->i_data; + + /* Fill extent header */ + eh =3D ext_inode_hdr(&k_ctx.k_ei->vfs_inode); + eh->eh_depth =3D 0; + eh->eh_entries =3D cpu_to_le16(1); + eh->eh_magic =3D EXT4_EXT_MAGIC; + eh->eh_max =3D + cpu_to_le16(ext4_ext_space_root_idx(&k_ctx.k_ei->vfs_inode, 0)); + eh->eh_generation =3D 0; + + /* + * add 1 extent in leaf node covering lblks [10,13) and pblk [100,103) + */ + EXT_FIRST_EXTENT(eh)->ee_block =3D cpu_to_le32(EX_DATA_LBLK); + EXT_FIRST_EXTENT(eh)->ee_len =3D cpu_to_le16(EX_DATA_LEN); + ext4_ext_store_pblock(EXT_FIRST_EXTENT(eh), EX_DATA_PBLK); + if (!param || param->is_unwrit_at_start) + ext4_ext_mark_unwritten(EXT_FIRST_EXTENT(eh)); + + /* Add stubs */ + kunit_activate_static_stub(test, ext4_cache_extents, + ext4_cache_extents_stub); + kunit_activate_static_stub(test, __ext4_ext_dirty, + __ext4_ext_dirty_stub); + kunit_activate_static_stub(test, ext4_es_remove_extent, + ext4_es_remove_extent_stub); + kunit_activate_static_stub(test, ext4_zeroout_es, ext4_zeroout_es_stub); + kunit_activate_static_stub(test, ext4_ext_zeroout, ext4_ext_zeroout_stub); + kunit_activate_static_stub(test, ext4_issue_zeroout, + ext4_issue_zeroout_stub); + return 0; +} + +/* + * Return 1 if all bytes in the buf equal to c, else return the offset of = first mismatch + */ +static int check_buffer(char *buf, int c, int size) +{ + void *ret =3D NULL; + + ret =3D memchr_inv(buf, c, size); + if (ret =3D=3D NULL) + return 0; + + kunit_log(KERN_ALERT, kunit_get_current_test(), + "# %s: wrong char found at offset %u (expected:%d got:%d)", __func__, + (u32)((char *)ret - buf), c, *((char *)ret)); + return 1; +} + +static void test_split_convert(struct kunit *test) +{ + struct ext4_ext_path *path; + struct inode *inode =3D &k_ctx.k_ei->vfs_inode; + struct ext4_extent *ex; + struct ext4_map_blocks map; + const struct kunit_ext_test_param *param =3D + (const struct kunit_ext_test_param *)(test->param_value); + int blkbits =3D inode->i_sb->s_blocksize_bits; + + if (param->is_zeroout_test) + /* + * Force zeroout by making ext4_ext_insert_extent return ENOSPC + */ + kunit_activate_static_stub(test, ext4_ext_insert_extent, + ext4_ext_insert_extent_stub); + + path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0); + ex =3D path->p_ext; + KUNIT_EXPECT_EQ(test, EX_DATA_LBLK, ex->ee_block); + KUNIT_EXPECT_EQ(test, EX_DATA_LEN, ext4_ext_get_actual_len(ex)); + KUNIT_EXPECT_EQ(test, param->is_unwrit_at_start, ext4_ext_is_unwritten(ex= )); + if (param->is_zeroout_test) + KUNIT_EXPECT_EQ(test, 0, + check_buffer(k_ctx.k_data, 'X', + EX_DATA_LEN << blkbits)); + + map.m_lblk =3D param->split_map.m_lblk; + map.m_len =3D param->split_map.m_len; + ext4_split_convert_extents(NULL, inode, &map, path, + param->split_flags, NULL); + + path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0); + ex =3D path->p_ext; + + for (int i =3D 0; i < param->nr_exp_ext; i++) { + struct kunit_ext_state exp_ext =3D param->exp_ext_state[i]; + + KUNIT_EXPECT_EQ(test, exp_ext.ex_lblk, ex->ee_block); + KUNIT_EXPECT_EQ(test, exp_ext.ex_len, + ext4_ext_get_actual_len(ex)); + KUNIT_EXPECT_EQ(test, exp_ext.is_unwrit, + ext4_ext_is_unwritten(ex)); + + /* Only printed on failure */ + kunit_log(KERN_INFO, test, + "# [extent %d] exp: lblk:%d len:%d unwrit:%d \n", i, + exp_ext.ex_lblk, exp_ext.ex_len, exp_ext.is_unwrit); + kunit_log(KERN_INFO, test, + "# [extent %d] got: lblk:%d len:%d unwrit:%d\n", i, + ex->ee_block, ext4_ext_get_actual_len(ex), + ext4_ext_is_unwritten(ex)); + kunit_log(KERN_INFO, test, "------------------\n"); + + ex =3D ex + 1; + } + + if (!param->is_zeroout_test) + return; + + /* + * Check that then data area has been zeroed out correctly + */ + for (int i =3D 0; i < param->nr_exp_data_segs; i++) { + loff_t off, len; + struct kunit_ext_data_state exp_data_seg =3D param->exp_data_state[i]; + + off =3D exp_data_seg.off_blk << blkbits; + len =3D exp_data_seg.len_blk << blkbits; + KUNIT_EXPECT_EQ_MSG(test, 0, + check_buffer(k_ctx.k_data + off, + exp_data_seg.exp_char, len), + "# corruption in byte range [%lld, %lld)", + off, len); + } + + return; +} + +static const struct kunit_ext_test_param test_split_convert_params[] =3D { + /* unwrit to writ splits */ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half to wri= t", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 3, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 2, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1 + (EX_DATA_LEN - 2), + .ex_len =3D 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + + /* writ to unwrit splits */ + { .desc =3D "split writ extent to 2 extents and convert 1st half unwrit", + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split writ extent to 2 extents and convert 2nd half unwrit", + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split writ extent to 3 extents and convert 2nd half to unwri= t", + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 3, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 2, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1 + (EX_DATA_LEN - 2), + .ex_len =3D 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + + /* + * ***** zeroout tests ***** + */ + /* unwrit to writ splits */ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= zeroout)", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= zeroout)", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half writ (= zeroout)", + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 2 }, + { .exp_char =3D 0, + .off_blk =3D EX_DATA_LEN - 1, + .len_blk =3D 1 } } }, +}; + +static void ext_get_desc(struct kunit *test, const void *p, char *desc) + +{ + struct kunit_ext_test_param *param =3D (struct kunit_ext_test_param *)p; + + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s\n", param->desc); +} + +static int test_split_convert_param_init(struct kunit *test) +{ + size_t arr_size =3D ARRAY_SIZE(test_split_convert_params); + + kunit_register_params_array(test, test_split_convert_params, arr_size, + ext_get_desc); + return 0; +} + +/* + * Note that we use KUNIT_CASE_PARAM_WITH_INIT() instead of the more compa= ct + * KUNIT_ARRAY_PARAM() because the later currently has a limitation causin= g the + * output parsing to be prone to error. For more context: + * + * https://lore.kernel.org/linux-kselftest/aULJpTvJDw9ctUDe@li-dc0c254c-25= 7c-11b2-a85c-98b6c1322444.ibm.com/ + */ +static struct kunit_case extents_test_cases[] =3D { + KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params, + test_split_convert_param_init, NULL), + {} +}; + +static struct kunit_suite extents_test_suite =3D { + .name =3D "ext4_extents_test", + .init =3D extents_kunit_init, + .exit =3D extents_kunit_exit, + .test_cases =3D extents_test_cases, +}; + +kunit_test_suites(&extents_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index c7c66ab825e7..4cebd82ef3e4 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -32,6 +32,7 @@ #include "ext4_jbd2.h" #include "ext4_extents.h" #include "xattr.h" +#include =20 #include =20 @@ -197,6 +198,9 @@ static int __ext4_ext_dirty(const char *where, unsigned= int line, { int err; =20 + KUNIT_STATIC_STUB_REDIRECT(__ext4_ext_dirty, where, line, handle, inode, + path); + WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); if (path->p_bh) { ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); @@ -535,6 +539,8 @@ static void ext4_cache_extents(struct inode *inode, ext4_lblk_t prev =3D 0; int i; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_cache_extents, inode, eh); + for (i =3D le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { unsigned int status =3D EXTENT_STATUS_WRITTEN; ext4_lblk_t lblk =3D le32_to_cpu(ex->ee_block); @@ -898,6 +904,8 @@ ext4_find_extent(struct inode *inode, ext4_lblk_t block, int ret; gfp_t gfp_flags =3D GFP_NOFS; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_find_extent, inode, block, path, flags); + if (flags & EXT4_EX_NOFAIL) gfp_flags |=3D __GFP_NOFAIL; =20 @@ -1990,6 +1998,9 @@ ext4_ext_insert_extent(handle_t *handle, struct inode= *inode, ext4_lblk_t next; int mb_flags =3D 0, unwritten; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_ext_insert_extent, handle, inode, path, + newext, gb_flags); + if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) mb_flags |=3D EXT4_MB_DELALLOC_RESERVED; if (unlikely(ext4_ext_get_actual_len(newext) =3D=3D 0)) { @@ -3138,8 +3149,10 @@ static void ext4_zeroout_es(struct inode *inode, str= uct ext4_extent *ex) ext4_fsblk_t ee_pblock; unsigned int ee_len; =20 - ee_block =3D le32_to_cpu(ex->ee_block); - ee_len =3D ext4_ext_get_actual_len(ex); + KUNIT_STATIC_STUB_REDIRECT(ext4_zeroout_es, inode, ex); + + ee_block =3D le32_to_cpu(ex->ee_block); + ee_len =3D ext4_ext_get_actual_len(ex); ee_pblock =3D ext4_ext_pblock(ex); =20 if (ee_len =3D=3D 0) @@ -3155,6 +3168,8 @@ static int ext4_ext_zeroout(struct inode *inode, stru= ct ext4_extent *ex) ext4_fsblk_t ee_pblock; unsigned int ee_len; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_ext_zeroout, inode, ex); + ee_len =3D ext4_ext_get_actual_len(ex); ee_pblock =3D ext4_ext_pblock(ex); return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, @@ -6180,3 +6195,7 @@ int ext4_ext_clear_bb(struct inode *inode) ext4_free_ext_path(path); return 0; } + +#ifdef CONFIG_EXT4_KUNIT_TESTS +#include "extents-test.c" +#endif diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c index fc83e7e2ca9e..6c1faf7c9f2a 100644 --- a/fs/ext4/extents_status.c +++ b/fs/ext4/extents_status.c @@ -16,6 +16,7 @@ #include "ext4.h" =20 #include +#include =20 /* * According to previous discussion in Ext4 Developer Workshop, we @@ -1627,6 +1628,8 @@ void ext4_es_remove_extent(struct inode *inode, ext4_= lblk_t lblk, int reserved =3D 0; struct extent_status *es =3D NULL; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_es_remove_extent, inode, lblk, len); + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) return; =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index d54d49d1f7a7..fdfd706c79c1 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -48,6 +48,8 @@ #include "acl.h" #include "truncate.h" =20 +#include + #include =20 static void ext4_journalled_zero_new_buffers(handle_t *handle, @@ -400,6 +402,8 @@ int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t= lblk, ext4_fsblk_t pblk, { int ret; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_issue_zeroout, inode, lblk, pblk, len); + if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) return fscrypt_zeroout_range(inode, lblk, pblk, len); =20 --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FA601DFD96; Mon, 19 Jan 2026 17:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844611; cv=none; b=AgXsbiI7LCbgwnGYH57tw80Ve78ol0uYm+qEz4J6FWabgkStYJzlhPxzz/tKBknJdFptxMaV8JShZSokTXwJXkToB4nOvY4DbhwalknNhYpounOTdSMWRpBov7E0JN6+LIcoxSdqOD/T8jcgWSgrWS3FDwDJQl7e43vkOX9ayR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844611; c=relaxed/simple; bh=kt8sye8UuKglVIRfz+ug+nyslIZcqrnkyrMPnjvFbvk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uxwNK8ORy3LAEwL6aW7v5Rf2Fyz7nIJh2kX3eT7kxKnLwkvzh81qRXhbq+jaOmwj4omkkT24vFTDcLbWryKzjVj3SFT15+Ofm53AUKRTdxecnTFw/9rKM56TSJz/Jz1u/NFW/ADPlnGjNoNbAU/w9y8N+rr9i9h8umwI1PDUT6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=fGZL/1Ym; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="fGZL/1Ym" Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGArmr000368; Mon, 19 Jan 2026 17:43:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=hiUtxnzbnRI7/k+yz EdJhWmu9sgFQBUNiWr9jxk1+gM=; b=fGZL/1YmTkOPbp1OoereDilZN+qL9R0eW 1rgDYAr+Cpyozf0YS86hlYpy8Gzm9wJjD5usioQ7uLuUCT5yyQjh1AzdCEf3M4uI fXToHooj8/JmwpWyK0rOAc2YjVk07A4Lqexz8r5emvS5ICST3eUpB4XCEh32UepT GEE4WmeXSVMursLitfvdl1kjEUal3jWst8nj53Byofddb+IcP039OO5+AZ8VI707 2BOZ/gmAaqSTQ7AGPdwx5g5PG+BWMHGd5G4StXLXwWxTF2zfdqgPlhnUCbfh4cPs /0hu+Iaw9eYrYfi/kFVs7QsNvCYAHJegp9upvgLGXIceeA+AWdFrg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br0uf94a4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:17 +0000 (GMT) Received: from m0353725.ppops.net (m0353725.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHOYj6009360; Mon, 19 Jan 2026 17:43:16 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br0uf94a2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:16 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGM5qG016627; Mon, 19 Jan 2026 17:43:16 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brn4xrbqc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:16 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhE4p61342062 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:14 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0C9B020043; Mon, 19 Jan 2026 17:43:14 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7277E20040; Mon, 19 Jan 2026 17:43:11 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:11 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/8] ext4: kunit tests for higher level extent manipulation functions Date: Mon, 19 Jan 2026 23:12:58 +0530 Message-ID: <455c0744698179266659bd1f3e737de71507cb98.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: lOPx-9T5er3nuLyNx4PzAyBmuBrqoYB8 X-Proofpoint-ORIG-GUID: 2GkJMOtHM7MT6tNZ6e38SLVhtr86HCxT X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfXwO0guNpGN5zz iwz/UvqBJmDpz1yCu2KZWiPsj9neOFD/ku/WtYnb94PWMH7CS1ODmc5tfbO1/jyuGkR3ECaK//6 4uV2PCg+CYpqcSStpU44R4Ccwsucu+iTg0JK3kcvAWIHkwkJ8qu6ILczYR6KhfTEWVM7KLzPcGx 6lM4FXfrnU/EZbHmJ9hmFR5RIsx98CPcYggt2SOChbsoE+rTH8SPwE7h2I7cV5+8vtDDlTv1a+h cFKhY41Vu5Qz5UX+G7u10g8RW0zOL48uHqHzLMYhGLfD11GirVK/9XLTZQB4INooUb+SYMMRuNC xAqhc7rXqBLjRIeixueNpqK16QYJDepJtAWk75M7rfV7BAx5QHnAPZmHIkXFs0DJ+U/G1ww/i4n RnvOUd52NMOKPL6mFQIW6A+xGPBa6nqdZN8UFMskFF7td8dYL9t6lXrO42NFvEabj0w3U7xWu7/ 249tRctqtPhOyrs/1uw== X-Authority-Analysis: v=2.4 cv=bopBxUai c=1 sm=1 tr=0 ts=696e6d35 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=M-LP-KjQj-4Pn8WnV4cA:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 adultscore=0 suspectscore=0 impostorscore=0 phishscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Add more kunit tests to cover the high level caller ext4_map_create_blocks(). We pass flags in a manner that covers the below function: 1. ext4_ext_handle_unwritten_extents() 1.1 - Split/Convert unwritten extent to written in endio convtext. 1.2 - Split/Convert unwritten extent to written in non endio context. 1.3 - Zeroout tests for the above 2 cases 2. convert_initialized_extent() - Convert written extent to unwritten during zero range Reviewed-by: Zhang Yi Reviewed-by: Jan Kara Signed-off-by: Ojaswin Mujoo --- fs/ext4/ext4.h | 4 + fs/ext4/extents-test.c | 359 ++++++++++++++++++++++++++++++++++++++- fs/ext4/extents_status.c | 3 + fs/ext4/inode.c | 8 +- 4 files changed, 365 insertions(+), 9 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 174c51402864..5f744bd19dea 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -3786,6 +3786,10 @@ extern int ext4_convert_unwritten_io_end_vec(handle_= t *handle, ext4_io_end_t *io_end); extern int ext4_map_blocks(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, int flags); +extern int ext4_map_query_blocks(handle_t *handle, struct inode *inode, + struct ext4_map_blocks *map, int flags); +extern int ext4_map_create_blocks(handle_t *handle, struct inode *inode, + struct ext4_map_blocks *map, int flags); extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int num, struct ext4_ext_path *path); diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c index f68e43d924db..082c3e170e54 100644 --- a/fs/ext4/extents-test.c +++ b/fs/ext4/extents-test.c @@ -77,10 +77,18 @@ struct kunit_ext_data_state { ext4_lblk_t len_blk; }; =20 +enum kunit_test_types { + TEST_SPLIT_CONVERT, + TEST_CREATE_BLOCKS, +}; + struct kunit_ext_test_param { /* description of test */ char *desc; =20 + /* determines which function will be tested */ + int type; + /* is extent unwrit at beginning of test */ bool is_unwrit_at_start; =20 @@ -90,6 +98,9 @@ struct kunit_ext_test_param { /* map describing range to split */ struct ext4_map_blocks split_map; =20 + /* disable zeroout */ + bool disable_zeroout; + /* no of extents expected after split */ int nr_exp_ext; =20 @@ -131,6 +142,9 @@ static struct file_system_type ext_fs_type =3D { =20 static void extents_kunit_exit(struct kunit *test) { + struct ext4_sb_info *sbi =3D k_ctx.k_ei->vfs_inode.i_sb->s_fs_info; + + kfree(sbi); kfree(k_ctx.k_ei); kfree(k_ctx.k_data); } @@ -162,6 +176,13 @@ static void ext4_es_remove_extent_stub(struct inode *i= node, ext4_lblk_t lblk, return; } =20 +void ext4_es_insert_extent_stub(struct inode *inode, ext4_lblk_t lblk, + ext4_lblk_t len, ext4_fsblk_t pblk, + unsigned int status, bool delalloc_reserve_used) +{ + return; +} + static void ext4_zeroout_es_stub(struct inode *inode, struct ext4_extent *= ex) { return; @@ -220,6 +241,7 @@ static int extents_kunit_init(struct kunit *test) struct ext4_inode_info *ei; struct inode *inode; struct super_block *sb; + struct ext4_sb_info *sbi =3D NULL; struct kunit_ext_test_param *param =3D (struct kunit_ext_test_param *)(test->param_value); =20 @@ -237,7 +259,20 @@ static int extents_kunit_init(struct kunit *test) sb->s_blocksize =3D 4096; sb->s_blocksize_bits =3D 12; =20 - ei->i_disksize =3D (EX_DATA_LBLK + EX_DATA_LEN + 10) << sb->s_blocksize_b= its; + sbi =3D kzalloc(sizeof(struct ext4_sb_info), GFP_KERNEL); + if (sbi =3D=3D NULL) + return -ENOMEM; + + sbi->s_sb =3D sb; + sb->s_fs_info =3D sbi; + + if (!param || !param->disable_zeroout) + sbi->s_extent_max_zeroout_kb =3D 32; + + ei->i_disksize =3D (EX_DATA_LBLK + EX_DATA_LEN + 10) + << sb->s_blocksize_bits; + ei->i_flags =3D 0; + ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); inode->i_sb =3D sb; =20 k_ctx.k_data =3D kzalloc(EX_DATA_LEN * 4096, GFP_KERNEL); @@ -262,7 +297,9 @@ static int extents_kunit_init(struct kunit *test) eh->eh_generation =3D 0; =20 /* - * add 1 extent in leaf node covering lblks [10,13) and pblk [100,103) + * add 1 extent in leaf node covering: + * - lblks: [EX_DATA_LBLK, EX_DATA_LBLK * + EX_DATA_LEN) + * - pblks: [EX_DATA_PBLK, EX_DATA_PBLK + EX_DATA_LEN) */ EXT_FIRST_EXTENT(eh)->ee_block =3D cpu_to_le32(EX_DATA_LBLK); EXT_FIRST_EXTENT(eh)->ee_len =3D cpu_to_le16(EX_DATA_LEN); @@ -277,6 +314,8 @@ static int extents_kunit_init(struct kunit *test) __ext4_ext_dirty_stub); kunit_activate_static_stub(test, ext4_es_remove_extent, ext4_es_remove_extent_stub); + kunit_activate_static_stub(test, ext4_es_insert_extent, + ext4_es_insert_extent_stub); kunit_activate_static_stub(test, ext4_zeroout_es, ext4_zeroout_es_stub); kunit_activate_static_stub(test, ext4_ext_zeroout, ext4_ext_zeroout_stub); kunit_activate_static_stub(test, ext4_issue_zeroout, @@ -301,6 +340,30 @@ static int check_buffer(char *buf, int c, int size) return 1; } =20 +/* + * Simulate a map block call by first calling ext4_map_query_blocks() to + * correctly populate map flags and pblk and then call the + * ext4_map_create_blocks() to do actual split and conversion. This is eas= ier + * than calling ext4_map_blocks() because that needs mocking a lot of unre= lated + * functions. + */ +static void ext4_map_create_blocks_helper(struct kunit *test, + struct inode *inode, + struct ext4_map_blocks *map, + int flags) +{ + int retval =3D 0; + + retval =3D ext4_map_query_blocks(NULL, inode, map, flags); + if (retval < 0) { + KUNIT_FAIL(test, + "ext4_map_query_blocks() failed. Cannot proceed\n"); + return; + } + + ext4_map_create_blocks(NULL, inode, map, flags); +} + static void test_split_convert(struct kunit *test) { struct ext4_ext_path *path; @@ -330,8 +393,18 @@ static void test_split_convert(struct kunit *test) =20 map.m_lblk =3D param->split_map.m_lblk; map.m_len =3D param->split_map.m_len; - ext4_split_convert_extents(NULL, inode, &map, path, - param->split_flags, NULL); + + switch (param->type) { + case TEST_SPLIT_CONVERT: + path =3D ext4_split_convert_extents(NULL, inode, &map, path, + param->split_flags, NULL); + break; + case TEST_CREATE_BLOCKS: + ext4_map_create_blocks_helper(test, inode, &map, param->split_flags); + break; + default: + KUNIT_FAIL(test, "param->type %d not support.", param->type); + } =20 path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0); ex =3D path->p_ext; @@ -383,6 +456,7 @@ static void test_split_convert(struct kunit *test) static const struct kunit_ext_test_param test_split_convert_params[] =3D { /* unwrit to writ splits */ { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, @@ -395,6 +469,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { .is_unwrit =3D 1 } }, .is_zeroout_test =3D 0 }, { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, @@ -407,6 +482,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { .is_unwrit =3D 0 } }, .is_zeroout_test =3D 0 }, { .desc =3D "split unwrit extent to 3 extents and convert 2nd half to wri= t", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, @@ -424,6 +500,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { =20 /* writ to unwrit splits */ { .desc =3D "split writ extent to 2 extents and convert 1st half unwrit", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 0, .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, @@ -436,6 +513,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { .is_unwrit =3D 0 } }, .is_zeroout_test =3D 0 }, { .desc =3D "split writ extent to 2 extents and convert 2nd half unwrit", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 0, .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, @@ -448,6 +526,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { .is_unwrit =3D 1 } }, .is_zeroout_test =3D 0 }, { .desc =3D "split writ extent to 3 extents and convert 2nd half to unwri= t", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 0, .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, @@ -468,6 +547,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { */ /* unwrit to writ splits */ { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, @@ -482,6 +562,7 @@ static const struct kunit_ext_test_param test_split_con= vert_params[] =3D { .off_blk =3D 1, .len_blk =3D EX_DATA_LEN - 1 } } }, { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, @@ -496,6 +577,201 @@ static const struct kunit_ext_test_param test_split_c= onvert_params[] =3D { .off_blk =3D 1, .len_blk =3D EX_DATA_LEN - 1 } } }, { .desc =3D "split unwrit extent to 3 extents and convert 2nd half writ (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 2 }, + { .exp_char =3D 0, + .off_blk =3D EX_DATA_LEN - 1, + .len_blk =3D 1 } } }, +}; + +/* Tests to trigger ext4_ext_map_blocks() -> convert_initialized_extent() = */ +static const struct kunit_ext_test_param test_convert_initialized_params[]= =3D { + /* writ to unwrit splits */ + { .desc =3D "split writ extent to 2 extents and convert 1st half unwrit", + .type =3D TEST_CREATE_BLOCKS, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .is_unwrit_at_start =3D 0, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split writ extent to 2 extents and convert 2nd half unwrit", + .type =3D TEST_CREATE_BLOCKS, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .is_unwrit_at_start =3D 0, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split writ extent to 3 extents and convert 2nd half to unwri= t", + .type =3D TEST_CREATE_BLOCKS, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .is_unwrit_at_start =3D 0, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 3, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 2, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1 + (EX_DATA_LEN - 2), + .ex_len =3D 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, +}; + +/* Tests to trigger ext4_ext_map_blocks() -> ext4_ext_handle_unwritten_exn= tents() */ +static const struct kunit_ext_test_param test_handle_unwritten_params[] = =3D { + /* unwrit to writ splits via endio path */ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 2, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half to wri= t (endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 3, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 2, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1 + (EX_DATA_LEN - 2), + .ex_len =3D 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + + /* unwrit to writ splits via non-endio path */ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= non endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 2, + .disable_zeroout =3D true, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= non endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 2, + .disable_zeroout =3D true, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 1, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 0 }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half to wri= t (non endio)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 3, + .disable_zeroout =3D true, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D 1, + .is_unwrit =3D 1 }, + { .ex_lblk =3D EX_DATA_LBLK + 1, + .ex_len =3D EX_DATA_LEN - 2, + .is_unwrit =3D 0 }, + { .ex_lblk =3D EX_DATA_LBLK + 1 + (EX_DATA_LEN - 2), + .ex_len =3D 1, + .is_unwrit =3D 1 } }, + .is_zeroout_test =3D 0 }, + + /* + * ***** zeroout tests ***** + */ + /* unwrit to writ splits (endio)*/ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half writ (= endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, .is_unwrit_at_start =3D 1, .split_flags =3D EXT4_GET_BLOCKS_CONVERT, .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, @@ -512,6 +788,56 @@ static const struct kunit_ext_test_param test_split_co= nvert_params[] =3D { { .exp_char =3D 0, .off_blk =3D EX_DATA_LEN - 1, .len_blk =3D 1 } } }, + + /* unwrit to writ splits (non-endio)*/ + { .desc =3D "split unwrit extent to 2 extents and convert 1st half writ (= non-endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 2 extents and convert 2nd half writ (= non-endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split unwrit extent to 3 extents and convert 2nd half writ (= non-endio, zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 1, + .split_flags =3D EXT4_GET_BLOCKS_CREATE, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 2 }, + { .exp_char =3D 0, + .off_blk =3D EX_DATA_LEN - 1, + .len_blk =3D 1 } } }, }; =20 static void ext_get_desc(struct kunit *test, const void *p, char *desc) @@ -519,7 +845,8 @@ static void ext_get_desc(struct kunit *test, const void= *p, char *desc) { struct kunit_ext_test_param *param =3D (struct kunit_ext_test_param *)p; =20 - snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s\n", param->desc); + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s %s\n", param->desc, + (param->type & TEST_CREATE_BLOCKS) ? "(highlevel)" : ""); } =20 static int test_split_convert_param_init(struct kunit *test) @@ -531,6 +858,24 @@ static int test_split_convert_param_init(struct kunit = *test) return 0; } =20 +static int test_convert_initialized_param_init(struct kunit *test) +{ + size_t arr_size =3D ARRAY_SIZE(test_convert_initialized_params); + + kunit_register_params_array(test, test_convert_initialized_params, + arr_size, ext_get_desc); + return 0; +} + +static int test_handle_unwritten_init(struct kunit *test) +{ + size_t arr_size =3D ARRAY_SIZE(test_handle_unwritten_params); + + kunit_register_params_array(test, test_handle_unwritten_params, + arr_size, ext_get_desc); + return 0; +} + /* * Note that we use KUNIT_CASE_PARAM_WITH_INIT() instead of the more compa= ct * KUNIT_ARRAY_PARAM() because the later currently has a limitation causin= g the @@ -541,6 +886,10 @@ static int test_split_convert_param_init(struct kunit = *test) static struct kunit_case extents_test_cases[] =3D { KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params, test_split_convert_param_init, NULL), + KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params, + test_convert_initialized_param_init, NULL), + KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params, + test_handle_unwritten_init, NULL), {} }; =20 diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c index 6c1faf7c9f2a..095ccb7ba4ba 100644 --- a/fs/ext4/extents_status.c +++ b/fs/ext4/extents_status.c @@ -916,6 +916,9 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lb= lk_t lblk, struct pending_reservation *pr =3D NULL; bool revise_pending =3D false; =20 + KUNIT_STATIC_STUB_REDIRECT(ext4_es_insert_extent, inode, lblk, len, + pblk, status, delalloc_reserve_used); + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) return; =20 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index fdfd706c79c1..32cf362c471c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -541,8 +541,8 @@ static int ext4_map_query_blocks_next_in_leaf(handle_t = *handle, return map->m_len; } =20 -static int ext4_map_query_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) +int ext4_map_query_blocks(handle_t *handle, struct inode *inode, + struct ext4_map_blocks *map, int flags) { unsigned int status; int retval; @@ -588,8 +588,8 @@ static int ext4_map_query_blocks(handle_t *handle, stru= ct inode *inode, return retval; } =20 -static int ext4_map_create_blocks(handle_t *handle, struct inode *inode, - struct ext4_map_blocks *map, int flags) +int ext4_map_create_blocks(handle_t *handle, struct inode *inode, + struct ext4_map_blocks *map, int flags) { unsigned int status; int err, retval =3D 0; --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0215532D0DC; Mon, 19 Jan 2026 17:43:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844614; cv=none; b=UIsmzsOVatBxMvdrcl9NBxgFhv/wc3hVqd/2u0esAHv1JXggYQA/UTibD85qkbqcU7uGqgq2jabb/nSWOmwlYnZOhh3FUMycx+wMIEljb3vVJbGNuaPdYQ2Li9zsqWPFVc4Cpj/Afcmz4C22/MmLnAToD1nTXHloEyzzjLndW18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844614; c=relaxed/simple; bh=j4izDwJFPE/GHTjcLl8ooKHtF7XKOWYbKDRMEgVHr3g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VuiyALJ3/hhC9KGwA/B6ky1bzCi9c3zqulDon/Lo1pgpXPsL7698aFY35YdfoZuWg49nSBYCsVAfPIIb9+NYuV0Pg3S8l8ynWLoWRc3xPgw7bCM+0vSHLEUrgVIgPB+9U89cOaN357/+Wv4YWcXLpqpjWoOpRBy1qYzMsoHS1t4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=gX6g/DYb; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="gX6g/DYb" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JDUs1I020107; Mon, 19 Jan 2026 17:43:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=XIA3bAo38/ZFOvHt8 kVGua+Qk479+vUj2vzug9Gcw0A=; b=gX6g/DYbEDaifaXPMjxz6UaD3JdnAHaJx CzFvjJbtYsWkUXWar640C/sj92/zTXZatJ90WZnzHVJu+gcZuC71VWLrXuBaoXCJ BwVdONNTuA+XceEACp3nQrZ7xQCzwMK69hgdggZ4EzqKtVCZfX7Rn9a7j2BFK2Eh ezDshAjhKk6eX9HJGQTdeFV3uVruBHexkn8lTDRS8vBMh5rIsJKzKs7xydg2wZgE JCNDCcSlo7JKPXfOIcyjBitkn3+gEm/ix4cbaDbSawH1naOgGwZx2YypdPcxJ3fK RrsbVHuZ/gO8HwIKSPZF8PV/JzYEwW2MttMO+0WXkLiwZ9tIMEuMg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br23rstbp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:21 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHh4he016657; Mon, 19 Jan 2026 17:43:20 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br23rstbj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:20 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGXqg0017070; Mon, 19 Jan 2026 17:43:19 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brn4xrbqw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:19 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhHOg42271160 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:17 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 69D3020043; Mon, 19 Jan 2026 17:43:17 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 88F6E20040; Mon, 19 Jan 2026 17:43:14 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:14 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/8] ext4: Add extent status cache support to kunit tests Date: Mon, 19 Jan 2026 23:12:59 +0530 Message-ID: <5e78d0dbd45a8677ed9590abace42e4c8b0c2489.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 0SJFS8vaTAfShnbpWjoyk-y2h5O48_eD X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX5GjBfkS6FqNI nUsXxFK7WqHKxkpojYM4XEvaJiJHwDgHeZV8cdhAUgxAQXb+xuO+/F2fOPMdq1TMKMm1Jb14w+q 4iulsDP6lVsYhCfHwsM717I07N4BICBdocxMaN2EqglGjwCceVQ3gKu7snUC/zJcdZx2yx6wglw UCsT1UgwHvNpHexDLdSh0IrDMMyNcs+gEr8CgxgHRWIB0OyoojNP3pob0et0mIKEYaWfyMHvUmJ 48amgaAw8xu0bM7UNnfBi+lieaH73js9qkNB6nr1Imuyijq3xPNBzG+AJMV1xbR0ev6YOAieXvL NeFwEUOCTOuySNq7hzW+YMpSTiDV+L/HjO/kCFUMM0yjA6GCTgO3Wp0KIbNljvHW7WMebCmAbrF UhMGe0Yi5Q8urrQFrDBs7PBCunlezWi9FY2huzg/67wVL56yWe4pgy5lhkAXz8SmUbTGSf9bFHQ kUwf22JVNCz7FzodUFA== X-Authority-Analysis: v=2.4 cv=J9SnLQnS c=1 sm=1 tr=0 ts=696e6d39 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=mO1zcom0dC79SphzpucA:9 X-Proofpoint-ORIG-GUID: oZtZigFvy9ffx7vjyLefb4e04QgXk-pf X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 adultscore=0 suspectscore=0 spamscore=0 lowpriorityscore=0 malwarescore=0 clxscore=1015 bulkscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Add support in Kunit tests to ensure that the extent status cache is also in sync after the extent split and conversion operations. Reviewed-by: Zhang Yi Reviewed-by: Jan Kara Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents-test.c | 106 ++++++++++++++++++++++++--------------- fs/ext4/extents.c | 2 - fs/ext4/extents_status.c | 5 -- 3 files changed, 65 insertions(+), 48 deletions(-) diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c index 082c3e170e54..7b7ed347e14e 100644 --- a/fs/ext4/extents-test.c +++ b/fs/ext4/extents-test.c @@ -149,12 +149,6 @@ static void extents_kunit_exit(struct kunit *test) kfree(k_ctx.k_data); } =20 -static void ext4_cache_extents_stub(struct inode *inode, - struct ext4_extent_header *eh) -{ - return; -} - static int __ext4_ext_dirty_stub(const char *where, unsigned int line, handle_t *handle, struct inode *inode, struct ext4_ext_path *path) @@ -170,24 +164,6 @@ ext4_ext_insert_extent_stub(handle_t *handle, struct i= node *inode, return ERR_PTR(-ENOSPC); } =20 -static void ext4_es_remove_extent_stub(struct inode *inode, ext4_lblk_t lb= lk, - ext4_lblk_t len) -{ - return; -} - -void ext4_es_insert_extent_stub(struct inode *inode, ext4_lblk_t lblk, - ext4_lblk_t len, ext4_fsblk_t pblk, - unsigned int status, bool delalloc_reserve_used) -{ - return; -} - -static void ext4_zeroout_es_stub(struct inode *inode, struct ext4_extent *= ex) -{ - return; -} - /* * We will zeroout the equivalent range in the data area */ @@ -244,13 +220,7 @@ static int extents_kunit_init(struct kunit *test) struct ext4_sb_info *sbi =3D NULL; struct kunit_ext_test_param *param =3D (struct kunit_ext_test_param *)(test->param_value); - - /* setup the mock inode */ - k_ctx.k_ei =3D kzalloc(sizeof(struct ext4_inode_info), GFP_KERNEL); - if (k_ctx.k_ei =3D=3D NULL) - return -ENOMEM; - ei =3D k_ctx.k_ei; - inode =3D &ei->vfs_inode; + int err; =20 sb =3D sget(&ext_fs_type, NULL, ext_set, 0, NULL); if (IS_ERR(sb)) @@ -269,6 +239,24 @@ static int extents_kunit_init(struct kunit *test) if (!param || !param->disable_zeroout) sbi->s_extent_max_zeroout_kb =3D 32; =20 + /* setup the mock inode */ + k_ctx.k_ei =3D kzalloc(sizeof(struct ext4_inode_info), GFP_KERNEL); + if (k_ctx.k_ei =3D=3D NULL) + return -ENOMEM; + ei =3D k_ctx.k_ei; + inode =3D &ei->vfs_inode; + + err =3D ext4_es_register_shrinker(sbi); + if (err) + return err; + + ext4_es_init_tree(&ei->i_es_tree); + rwlock_init(&ei->i_es_lock); + INIT_LIST_HEAD(&ei->i_es_list); + ei->i_es_all_nr =3D 0; + ei->i_es_shk_nr =3D 0; + ei->i_es_shrink_lblk =3D 0; + ei->i_disksize =3D (EX_DATA_LBLK + EX_DATA_LEN + 10) << sb->s_blocksize_bits; ei->i_flags =3D 0; @@ -307,16 +295,15 @@ static int extents_kunit_init(struct kunit *test) if (!param || param->is_unwrit_at_start) ext4_ext_mark_unwritten(EXT_FIRST_EXTENT(eh)); =20 + ext4_es_insert_extent(inode, EX_DATA_LBLK, EX_DATA_LEN, EX_DATA_PBLK, + ext4_ext_is_unwritten(EXT_FIRST_EXTENT(eh)) ? + EXTENT_STATUS_UNWRITTEN : + EXTENT_STATUS_WRITTEN, + 0); + /* Add stubs */ - kunit_activate_static_stub(test, ext4_cache_extents, - ext4_cache_extents_stub); kunit_activate_static_stub(test, __ext4_ext_dirty, __ext4_ext_dirty_stub); - kunit_activate_static_stub(test, ext4_es_remove_extent, - ext4_es_remove_extent_stub); - kunit_activate_static_stub(test, ext4_es_insert_extent, - ext4_es_insert_extent_stub); - kunit_activate_static_stub(test, ext4_zeroout_es, ext4_zeroout_es_stub); kunit_activate_static_stub(test, ext4_ext_zeroout, ext4_ext_zeroout_stub); kunit_activate_static_stub(test, ext4_issue_zeroout, ext4_issue_zeroout_stub); @@ -381,11 +368,12 @@ static void test_split_convert(struct kunit *test) kunit_activate_static_stub(test, ext4_ext_insert_extent, ext4_ext_insert_extent_stub); =20 - path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0); + path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, EXT4_EX_NOCACHE); ex =3D path->p_ext; KUNIT_EXPECT_EQ(test, EX_DATA_LBLK, ex->ee_block); KUNIT_EXPECT_EQ(test, EX_DATA_LEN, ext4_ext_get_actual_len(ex)); - KUNIT_EXPECT_EQ(test, param->is_unwrit_at_start, ext4_ext_is_unwritten(ex= )); + KUNIT_EXPECT_EQ(test, param->is_unwrit_at_start, + ext4_ext_is_unwritten(ex)); if (param->is_zeroout_test) KUNIT_EXPECT_EQ(test, 0, check_buffer(k_ctx.k_data, 'X', @@ -406,17 +394,47 @@ static void test_split_convert(struct kunit *test) KUNIT_FAIL(test, "param->type %d not support.", param->type); } =20 - path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0); + path =3D ext4_find_extent(inode, EX_DATA_LBLK, NULL, EXT4_EX_NOCACHE); ex =3D path->p_ext; =20 for (int i =3D 0; i < param->nr_exp_ext; i++) { struct kunit_ext_state exp_ext =3D param->exp_ext_state[i]; + bool es_check_needed =3D param->type !=3D TEST_SPLIT_CONVERT; + struct extent_status es; + int contains_ex, ex_end, es_end, es_pblk; =20 KUNIT_EXPECT_EQ(test, exp_ext.ex_lblk, ex->ee_block); KUNIT_EXPECT_EQ(test, exp_ext.ex_len, ext4_ext_get_actual_len(ex)); KUNIT_EXPECT_EQ(test, exp_ext.is_unwrit, ext4_ext_is_unwritten(ex)); + /* + * Confirm extent cache is in sync. Note that es cache can be + * merged even when on-disk extents are not so take that into + * account. + * + * Also, ext4_split_convert_extents() forces EXT4_EX_NOCACHE hence + * es status are ignored for that case. + */ + if (es_check_needed) { + ext4_es_lookup_extent(inode, ex->ee_block, NULL, &es, + NULL); + + ex_end =3D exp_ext.ex_lblk + exp_ext.ex_len; + es_end =3D es.es_lblk + es.es_len; + contains_ex =3D es.es_lblk <=3D exp_ext.ex_lblk && + es_end >=3D ex_end; + es_pblk =3D ext4_es_pblock(&es) + + (exp_ext.ex_lblk - es.es_lblk); + + KUNIT_EXPECT_EQ(test, contains_ex, 1); + KUNIT_EXPECT_EQ(test, ext4_ext_pblock(ex), es_pblk); + KUNIT_EXPECT_EQ(test, 1, + (exp_ext.is_unwrit && + ext4_es_is_unwritten(&es)) || + (!exp_ext.is_unwrit && + ext4_es_is_written(&es))); + } =20 /* Only printed on failure */ kunit_log(KERN_INFO, test, @@ -426,6 +444,12 @@ static void test_split_convert(struct kunit *test) "# [extent %d] got: lblk:%d len:%d unwrit:%d\n", i, ex->ee_block, ext4_ext_get_actual_len(ex), ext4_ext_is_unwritten(ex)); + if (es_check_needed) + kunit_log( + KERN_INFO, test, + "# [extent %d] es: lblk:%d len:%d pblk:%lld type:0x%x\n", + i, es.es_lblk, es.es_len, ext4_es_pblock(&es), + ext4_es_type(&es)); kunit_log(KERN_INFO, test, "------------------\n"); =20 ex =3D ex + 1; diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 4cebd82ef3e4..a581e9278d48 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3149,8 +3149,6 @@ static void ext4_zeroout_es(struct inode *inode, stru= ct ext4_extent *ex) ext4_fsblk_t ee_pblock; unsigned int ee_len; =20 - KUNIT_STATIC_STUB_REDIRECT(ext4_zeroout_es, inode, ex); - ee_block =3D le32_to_cpu(ex->ee_block); ee_len =3D ext4_ext_get_actual_len(ex); ee_pblock =3D ext4_ext_pblock(ex); diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c index 095ccb7ba4ba..a1538bac51c6 100644 --- a/fs/ext4/extents_status.c +++ b/fs/ext4/extents_status.c @@ -916,9 +916,6 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lb= lk_t lblk, struct pending_reservation *pr =3D NULL; bool revise_pending =3D false; =20 - KUNIT_STATIC_STUB_REDIRECT(ext4_es_insert_extent, inode, lblk, len, - pblk, status, delalloc_reserve_used); - if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) return; =20 @@ -1631,8 +1628,6 @@ void ext4_es_remove_extent(struct inode *inode, ext4_= lblk_t lblk, int reserved =3D 0; struct extent_status *es =3D NULL; =20 - KUNIT_STATIC_STUB_REDIRECT(ext4_es_remove_extent, inode, lblk, len); - if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) return; =20 --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2401232D0E6; Mon, 19 Jan 2026 17:43:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844612; cv=none; b=r23SQTGDDc2EzUknIupdhO7dafWfiBn475MXpNz4iIJCEznWHuEwVVeZvrBaKxHqprzcV4l1ZsKYuTZdUmCaO/LKvGeW+hk3RFQRYUv7Mo66iBK7xCyQCnftDpGmdgm+Tuq7e/40fRN7SnuAkmjc+NMCsUP7JDgFaaMEB7gINJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844612; c=relaxed/simple; bh=6yjg/WWE5GrRXNyyf/T9jL49b3ab1APC0eCCv/iB/f4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hqP0XXxh058KCynQ8I9dbZJqrJUdNEj/lWeHaSLyifEg7Z5o+Jln3Y93vbteV7aSLLEvV+rmPEJZOoQoh/Jk9YbN0xZuafox1N5dtt/C8mS0at35SJEc0J8TfIZCM+w+e1/UI6IR+dS5Brc6AHF38dx1m0jVLoEIHDMJ6GuYqeQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=TK7XhYn9; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="TK7XhYn9" Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JDLOaq009820; Mon, 19 Jan 2026 17:43:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=A0txSm+yOaX8X9l7S AP6hmeQlyIyXyPqTJkYpJAzdMc=; b=TK7XhYn9hXOUBvswHF5f30cDlda3CBWkP 50bWHV/pwgvQKC2jOPL4eNUOsTsDmQbEj1yr450fgzYGj7RYbt296Q8KdK+yCKOq BdU8MIs6xllK26oOvkuKDtLebvLUMcLz59NgmDkruoVd5lxEltCi/8pBMc7ViaEi YKYKW40IzA0KnIZB2J6palzf2RRHHxW4FnkeQinqOoGFqdgvp4aZ2ANwl7UG7HXf G8XRj63rqxjfKEJKpUN4Mk+k9+8KHLpFtUkMnN75fiBXqgtgaErfNDIjKDQZfBQd BajwCL6aJzbKHxrq4t4/EXYhIaqnojM9Nb69uEc/YvkBFfPQ0/CGQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4bqyuk199b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:23 +0000 (GMT) Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHT5GD020220; Mon, 19 Jan 2026 17:43:22 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4bqyuk1998-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:22 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGXqg1017070; Mon, 19 Jan 2026 17:43:21 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brn4xrbr3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:21 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhJlg21430612 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:19 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C330F20043; Mon, 19 Jan 2026 17:43:19 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CFBEC20040; Mon, 19 Jan 2026 17:43:17 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:17 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/8] ext4: propagate flags to convert_initialized_extent() Date: Mon, 19 Jan 2026 23:13:00 +0530 Message-ID: <62f6e748689d9136151c9771f4118304550ae4f3.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX4lyWXHkH+0UA mqf7YV2A6lS0PJ0YTf/z5AAX7VMKvCz5d+XNU0w2esjkuC3TxbLjgFXpfIn2jUxVwkD98SszjeC pjYlOx8xo6F6NVILQs4yyGbSzUYydEGfJqJcthcctT3QlBfjZNdtB64ud1JCrw/U284FKYxzrF4 OjHaGKKT09FK8mjIHSPdLuXLoTx4bEmmMWpvIVS38/g6dbKhgisJwNWq9GXWusjLvakcfQBiVkJ va5nX4yG1ctsE77opffQHAM2823sTeT58+88Lt9TsugamQsUnLtKmJchmWbV1vfSeq0ADB5vEi/ o1lMU2ZmYtzFgt/fkeyao1yZTqEGgJ6euonUue3gSqNR4BwSNAaLSyCKeFsEO7cu8+xpIgQ6Tgu lgT93Ucq/3TidQlgIHmJYXmwDiF/n161XYIjLZ7sKs/wC8a2J1ZJ7OkTVU9qXvXXxfOM4VGlFqq rc5YtNgZDdimxEQ8TCw== X-Authority-Analysis: v=2.4 cv=bsBBxUai c=1 sm=1 tr=0 ts=696e6d3b cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=oIlp7qZINqeTlFEOi2UA:9 X-Proofpoint-ORIG-GUID: xQn2vQYeEvnfrJXqvF2m-nntuRkFJZgs X-Proofpoint-GUID: pbXG9Wax9dzRpAWDyE7yo2MsBjkph8gh X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 suspectscore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 impostorscore=0 malwarescore=0 clxscore=1015 adultscore=0 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Currently, ext4_zero_range passes EXT4_EX_NOCACHE flag to avoid caching extents however this is not respected by convert_initialized_extent(). Hence, modify it to accept flags from the caller and to pass the flags on to other extent manipulation functions it calls. This makes sure the NOCACHE flag is respected throughout the code path. Also, we no longer explicitly pass CONVERT_UNWRITTEN as the caller takes care of this. Reviewed-by: Zhang Yi Reviewed-by: Jan Kara Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index a581e9278d48..3d45abfb13cd 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3844,6 +3844,7 @@ static struct ext4_ext_path * convert_initialized_extent(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, struct ext4_ext_path *path, + int flags, unsigned int *allocated) { struct ext4_extent *ex; @@ -3869,11 +3870,11 @@ convert_initialized_extent(handle_t *handle, struct= inode *inode, =20 if (ee_block !=3D map->m_lblk || ee_len > map->m_len) { path =3D ext4_split_convert_extents(handle, inode, map, path, - EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, NULL); + flags, NULL); if (IS_ERR(path)) return path; =20 - path =3D ext4_find_extent(inode, map->m_lblk, path, 0); + path =3D ext4_find_extent(inode, map->m_lblk, path, flags); if (IS_ERR(path)) return path; depth =3D ext_depth(inode); @@ -4263,7 +4264,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inod= e *inode, if ((!ext4_ext_is_unwritten(ex)) && (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { path =3D convert_initialized_extent(handle, - inode, map, path, &allocated); + inode, map, path, flags, &allocated); if (IS_ERR(path)) err =3D PTR_ERR(path); goto out; --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC96435CB70; Mon, 19 Jan 2026 17:43:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844619; cv=none; b=XyFH70IyQJbIbnqyxKlQj0zFYXJHQ1vIvCM/xAvffcFXl+Wgp6Dn+2hzzAB5xYplzRF4tCxxl8I8aX8gAzxvvJwve0T3GPm+gXEywRNU0vQ79tFe/iA+d3uRmF4T7aPLVRj7xDRFiYTkLSUcza6lc6gvcb5LYx/Ja1zrWO1vZeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844619; c=relaxed/simple; bh=rPpXttzo4vI1QAS/Xj9RU5Z7XzhHlT0/JQTyq66TZpw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OrdIG6izdQMORY2c307hGOFAXSgD3GJlwNjMOaySv+bb0wpHaZdxlsKJ9bA3HKLzqbLrjNkvhSa4z66S9vR8ogaC5FB44FmcgOavXHHCTGig+1OpN3+NbQt1P/FKLKDbkT436HkWezaJvUG8pxtdjAJdEcxA2vg1dLYjrvkLctE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=hL3Df0UZ; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="hL3Df0UZ" Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JDWbOH030226; Mon, 19 Jan 2026 17:43:25 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=VvrMQ1atE32JIWo94 FXD2K9x/DJd6/XWdF98HGpYtMY=; b=hL3Df0UZz8RBsiCWk3R1ptBEvkqTXNmdd fvxhd+GQUJ2SwyIT5ZyucnTHo1lfY+pWREbiVvMKhe3ZIcbUhi7Dre+WwSp8ldjG f5bGOV5m+YGDjtITBV5kYb6Fm2+qufyMmg2n4mGcQ1E1jnGngpxSBwz1yjyolQhD gDcbeJWGbjW/E5xMRADhhFHTH01PJr/fp10CuJ9dNKCXqtELmlz/hr81AZa/i8uF ZnbLEcOAZTX7hhnUwYx9qMao1e0yyGMoUraZExUosAqKvanl1aKPkfxs1Cui24oQ TZDsbjiauwoVeBdvD4mPiLHE6dTd0uyFeo5pSXZ5liooEqoQsaLGg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br0uf94as-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:25 +0000 (GMT) Received: from m0353725.ppops.net (m0353725.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHOYj8009360; Mon, 19 Jan 2026 17:43:25 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br0uf94am-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:25 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGfSVw027220; Mon, 19 Jan 2026 17:43:24 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brnrmr6vg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:24 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhML133685974 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:22 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7610120043; Mon, 19 Jan 2026 17:43:22 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A589520040; Mon, 19 Jan 2026 17:43:20 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:20 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 5/8] ext4: propagate flags to ext4_convert_unwritten_extents_endio() Date: Mon, 19 Jan 2026 23:13:01 +0530 Message-ID: <046a3805edd51a910dc827e15aab04998d6bb002.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: F8LsUNCjsaVYFz4hmo4VXs4fJgymmv2e X-Proofpoint-ORIG-GUID: OOSfyTI8601HJ1NEodSVkXJ1lopk285d X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX4TFdqo6+esV5 gfkZQW62iCIhsqK/m0RXZURYPUBwWQQR8oQW7q81zWio549srFV8P3/1TQG9jfQyqrZEmZ5LRL1 MGgyw3J1aSgV16P0oRGISK99ZL58o1AgunAaQUpaq+NT6gVJlfapVxrSnBZ3FWGxuK8KkRLY9jd nMZC4MRanUklBvlnDgpIV1/PLg3TkmnEAGiFCDeCK46pK83gmrJELyHQyGuJSX7DTvdNYvwrwRq 144XPXW3kaUFzu7FGVZ68PTRvaHEi4pdxVK0vwH8Bz1kv9/Qb1E04NjFY/1SCwQVgfTM/a3hR1z FimdrgSdekat8ACG47kPriqI3sDYqO9AvXsiLP2+WzLF1bTGySk5d2jD/czi6yj0Nf7nQrObXbi 1WRQ16rnb3HI6ZMZT4ih4Pa3UAOTEKA62Ga87V6Xc11YYwsTnk+l4uTyHe0phscXUFYvOraES8B kj+MbAG9sRHL6yR2YiQ== X-Authority-Analysis: v=2.4 cv=bopBxUai c=1 sm=1 tr=0 ts=696e6d3d cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=s5sEB8iLFlBga5WgvCgA:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 adultscore=0 suspectscore=0 impostorscore=0 phishscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Currently, callers like ext4_convert_unwritten_extents() pass EXT4_EX_NOCACHE flag to avoid caching extents however this is not respected by ext4_convert_unwritten_extents_endio(). Hence, modify it to accept flags from the caller and to pass the flags on to other extent manipulation functions it calls. This makes sure the NOCACHE flag is respected throughout the code path. Also, since the caller already passes METADATA_NOFAIL and CONVERT flags we don't need to explicitly pass it anymore. Reviewed-by: Jan Kara Reviewed-by: Zhang Yi Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 3d45abfb13cd..54f45b40fe73 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3784,7 +3784,7 @@ static struct ext4_ext_path *ext4_split_convert_exten= ts(handle_t *handle, static struct ext4_ext_path * ext4_convert_unwritten_extents_endio(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, - struct ext4_ext_path *path) + struct ext4_ext_path *path, int flags) { struct ext4_extent *ex; ext4_lblk_t ee_block; @@ -3801,15 +3801,12 @@ ext4_convert_unwritten_extents_endio(handle_t *hand= le, struct inode *inode, (unsigned long long)ee_block, ee_len); =20 if (ee_block !=3D map->m_lblk || ee_len > map->m_len) { - int flags =3D EXT4_GET_BLOCKS_CONVERT | - EXT4_GET_BLOCKS_METADATA_NOFAIL; - path =3D ext4_split_convert_extents(handle, inode, map, path, flags, NULL); if (IS_ERR(path)) return path; =20 - path =3D ext4_find_extent(inode, map->m_lblk, path, 0); + path =3D ext4_find_extent(inode, map->m_lblk, path, flags); if (IS_ERR(path)) return path; depth =3D ext_depth(inode); @@ -3942,7 +3939,7 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, s= truct inode *inode, /* IO end_io complete, convert the filled extent to written */ if (flags & EXT4_GET_BLOCKS_CONVERT) { path =3D ext4_convert_unwritten_extents_endio(handle, inode, - map, path); + map, path, flags); if (IS_ERR(path)) return path; ext4_update_inode_fsync_trans(handle, inode, 1); --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A57CD32ED22; Mon, 19 Jan 2026 17:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844620; cv=none; b=ddVDBTT+hieWD11DDSORJ02e9+zLtCUvOBQ4NnN3Os57Ql6ZckSzY5BOHFQ1o7ed20xFmSSs97jeK34cfVpRLWLy6an+KgeOjPsetodw/4Iq98GHlDjO71RXgTX/zErQsnS4ZdHd7mEWm5fJFO7m8N4eHkAbE7NhGYCY3gXNe5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844620; c=relaxed/simple; bh=CzpJdemQ3A2Ug1gfgFX7q1zL5f3k4zhaPhEzfGfVGOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uLhlsEHrIF1Jo4xiwp8GxEysQAJ+v5ZaXzysIO+LHbTGMcgpW2IFt4S7s4JvnsNY5gBKEvZbwaaIHeX7ZRIKkPV2skb5bmddKk+rtG204eO5QuEv2D3VAw4K2pjJuu5bMmgAwohVWI8DjT8PwMnSCFihdKIpemtYXbB5AAeeHss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=kaAcx5id; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="kaAcx5id" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JDFlXN022836; Mon, 19 Jan 2026 17:43:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=ZZ+VAMG7z7p77pPAX Nh2Ej/36KWjprfNMz0vOvGF2Io=; b=kaAcx5idWuZLoE1UMbAH4/8k/KVpuDjrd YN5S5v/nT728J5VQZ0bHJeMBWu1I5EA36bITQWbHHyVYbXz0YpoPkZebUzMmLaVT Rr0DoGk3XzhY/NxZpoTJ++v+EfDvop8mA/ki2/22tzTSIsqyqJRPL/ajeLburHeo 8rXz+9WylI6HIOALBFhfOKUzbuebl5LqWqXLlYG1utppN9XzNI5rfscRC8WOf0lU M8d5oXy9CaSD1BCMvzczLRGopxjqSJYBXPlJoAe9IdWFSbXg9tuyOaMhqIOepsnT EUaUOGWrzDdq+4VelqS87+mQc9GbiPtIoKE44PUat+0Icb7xzlEew== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br23rstc5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:29 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHh4hg016657; Mon, 19 Jan 2026 17:43:28 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br23rstc2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:28 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGhRP7027274; Mon, 19 Jan 2026 17:43:27 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brnrmr6vs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:27 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhPck36176334 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:25 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 24C1620043; Mon, 19 Jan 2026 17:43:25 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D98D620040; Mon, 19 Jan 2026 17:43:22 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:22 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 6/8] ext4: Refactor zeroout path and handle all cases Date: Mon, 19 Jan 2026 23:13:02 +0530 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: RqGEEG2CnKMQDSS9Qt6NZrwwlX-wftVk X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfXz9yqU3IzxyST KnV+ePAVPpkput7OdtB1O+uds21a1e4i5eSavHpWPQVtJjwXOZ06fdVcH5P9ZLOn6bMFOpHI3/0 yuSxCwMon1uKJwhjRR7RwTbofGoKphRgUTpbHXj113ZL8Lfxb2Ai2+d3IQO9wGnhHhi+vxyL/lc yLc+0//mmFYqs6WxpduC9X9qQPwVMvg9x9uThwuSu9/uv/CbHZrcd1Qd8460e8gFTbK0czwvlB0 pR0FCSpIUH7dfjyzZBkDRH4iJigbXX0LdxRgQJtaf7PPlAYVqUwmnHIHLFlPt9A3ZanVbf1Zx52 xafZd8+/mhGqT1DElOMZYuENZRcDQugkVsyk87Oexg4vvr4RpJKLk0+ii3D55Tl2Jd/Amzuryn6 pEsRxXdUQv+LfIzJYCUkjtue5dFV0rpewfksLHoyqXJqkixaplCqJKu2QB2LHhekdlNv7aEZzaa AqIzAvY6GEKbhdYg3eg== X-Authority-Analysis: v=2.4 cv=J9SnLQnS c=1 sm=1 tr=0 ts=696e6d41 cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=CHfUTuQ1SsgL1pUXN6wA:9 X-Proofpoint-ORIG-GUID: IQw1TW333sVj6KONJpG6z7skTBJzplx9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 adultscore=0 suspectscore=0 spamscore=0 lowpriorityscore=0 malwarescore=0 clxscore=1015 bulkscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Currently, zeroout is used as a fallback in case we fail to split/convert extents in the "traditional" modify-the-extent-tree way. This is essential to mitigate failures in critical paths like extent splitting during endio. However, the logic is very messy and not easy to follow. Further, the fragile use of various flags has made it prone to errors. Refactor zeroout out logic by moving it up to ext4_split_extents(). Further, zeroout correctly based on the type of conversion we want, ie: - unwritten to written: Zeroout everything around the mapped range. - written to unwritten: Zeroout only the mapped range. Also, ext4_ext_convert_to_initialized() now passes EXT4_GET_BLOCKS_CONVERT to make the intention clear. Reviewed-by: Zhang Yi Reviewed-by: Jan Kara Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents.c | 283 ++++++++++++++++++++++++++++++---------------- 1 file changed, 185 insertions(+), 98 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 54f45b40fe73..5689375221d2 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -44,14 +44,6 @@ #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ =20 -/* first half contains valid data */ -#define EXT4_EXT_DATA_ENTIRE_VALID1 0x8 /* has entirely valid data */ -#define EXT4_EXT_DATA_PARTIAL_VALID1 0x10 /* has partially valid data */ -#define EXT4_EXT_DATA_VALID1 (EXT4_EXT_DATA_ENTIRE_VALID1 | \ - EXT4_EXT_DATA_PARTIAL_VALID1) - -#define EXT4_EXT_DATA_VALID2 0x20 /* second half contains valid data */ - static __le32 ext4_extent_block_csum(struct inode *inode, struct ext4_extent_header *eh) { @@ -3193,7 +3185,8 @@ static int ext4_ext_zeroout(struct inode *inode, stru= ct ext4_extent *ex) * a> the extent are splitted into two extent. * b> split is not needed, and just mark the extent. * - * Return an extent path pointer on success, or an error pointer on failur= e. + * Return an extent path pointer on success, or an error pointer on failur= e. On + * failure, the extent is restored to original state. */ static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle, struct inode *inode, @@ -3203,14 +3196,10 @@ static struct ext4_ext_path *ext4_split_extent_at(h= andle_t *handle, { ext4_fsblk_t newblock; ext4_lblk_t ee_block; - struct ext4_extent *ex, newex, orig_ex, zero_ex; + struct ext4_extent *ex, newex, orig_ex; struct ext4_extent *ex2 =3D NULL; unsigned int ee_len, depth; - int err =3D 0; - - BUG_ON((split_flag & EXT4_EXT_DATA_VALID1) =3D=3D EXT4_EXT_DATA_VALID1); - BUG_ON((split_flag & EXT4_EXT_DATA_VALID1) && - (split_flag & EXT4_EXT_DATA_VALID2)); + int err =3D 0, insert_err =3D 0; =20 /* Do not cache extents that are in the process of being modified. */ flags |=3D EXT4_EX_NOCACHE; @@ -3276,11 +3265,10 @@ static struct ext4_ext_path *ext4_split_extent_at(h= andle_t *handle, =20 path =3D ext4_ext_insert_extent(handle, inode, path, &newex, flags); if (!IS_ERR(path)) - goto out; + return path; =20 - err =3D PTR_ERR(path); - if (err !=3D -ENOSPC && err !=3D -EDQUOT && err !=3D -ENOMEM) - goto out_path; + insert_err =3D PTR_ERR(path); + err =3D 0; =20 /* * Get a new path to try to zeroout or fix the extent length. @@ -3296,72 +3284,130 @@ static struct ext4_ext_path *ext4_split_extent_at(= handle_t *handle, split, PTR_ERR(path)); goto out_path; } + + err =3D ext4_ext_get_access(handle, inode, path + depth); + if (err) + goto out; + depth =3D ext_depth(inode); ex =3D path[depth].p_ext; =20 - if (EXT4_EXT_MAY_ZEROOUT & split_flag) { - if (split_flag & EXT4_EXT_DATA_VALID1) - memcpy(&zero_ex, ex2, sizeof(zero_ex)); - else if (split_flag & EXT4_EXT_DATA_VALID2) - memcpy(&zero_ex, ex, sizeof(zero_ex)); - else - memcpy(&zero_ex, &orig_ex, sizeof(zero_ex)); - ext4_ext_mark_initialized(&zero_ex); +fix_extent_len: + ex->ee_len =3D orig_ex.ee_len; + err =3D ext4_ext_dirty(handle, inode, path + path->p_depth); +out: + if (err || insert_err) { + ext4_free_ext_path(path); + path =3D err ? ERR_PTR(err) : ERR_PTR(insert_err); + } +out_path: + if (IS_ERR(path)) + /* Remove all remaining potentially stale extents. */ + ext4_es_remove_extent(inode, ee_block, ee_len); + ext4_ext_show_leaf(inode, path); + return path; +} =20 - err =3D ext4_ext_zeroout(inode, &zero_ex); - if (err) - goto fix_extent_len; +static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode, + struct ext4_ext_path *path, + struct ext4_map_blocks *map, int flags) +{ + struct ext4_extent *ex; + unsigned int ee_len, depth; + ext4_lblk_t ee_block; + uint64_t lblk, pblk, len; + int is_unwrit; + int err =3D 0; =20 + depth =3D ext_depth(inode); + ex =3D path[depth].p_ext; + ee_block =3D le32_to_cpu(ex->ee_block); + ee_len =3D ext4_ext_get_actual_len(ex); + is_unwrit =3D ext4_ext_is_unwritten(ex); + + if (flags & EXT4_GET_BLOCKS_CONVERT) { /* - * The first half contains partially valid data, the splitting - * of this extent has not been completed, fix extent length - * and ext4_split_extent() split will the first half again. + * EXT4_GET_BLOCKS_CONVERT: Caller wants the range specified by + * map to be initialized. Zeroout everything except the map + * range. */ - if (split_flag & EXT4_EXT_DATA_PARTIAL_VALID1) { - /* - * Drop extent cache to prevent stale unwritten - * extents remaining after zeroing out. - */ - ext4_es_remove_extent(inode, - le32_to_cpu(zero_ex.ee_block), - ext4_ext_get_actual_len(&zero_ex)); - goto fix_extent_len; + + loff_t map_end =3D (loff_t) map->m_lblk + map->m_len; + loff_t ex_end =3D (loff_t) ee_block + ee_len; + + if (!is_unwrit) + /* Shouldn't happen. Just exit */ + return -EINVAL; + + /* zeroout left */ + if (map->m_lblk > ee_block) { + lblk =3D ee_block; + len =3D map->m_lblk - ee_block; + pblk =3D ext4_ext_pblock(ex); + err =3D ext4_issue_zeroout(inode, lblk, pblk, len); + if (err) + /* ZEROOUT failed, just return original error */ + return err; } =20 - /* update the extent length and mark as initialized */ - ex->ee_len =3D cpu_to_le16(ee_len); - ext4_ext_try_to_merge(handle, inode, path, ex); - err =3D ext4_ext_dirty(handle, inode, path + path->p_depth); - if (!err) - /* update extent status tree */ - ext4_zeroout_es(inode, &zero_ex); + /* zeroout right */ + if (map_end < ex_end) { + lblk =3D map_end; + len =3D ex_end - map_end; + pblk =3D ext4_ext_pblock(ex) + (map_end - ee_block); + err =3D ext4_issue_zeroout(inode, lblk, pblk, len); + if (err) + /* ZEROOUT failed, just return original error */ + return err; + } + } else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { /* - * If we failed at this point, we don't know in which - * state the extent tree exactly is so don't try to fix - * length of the original extent as it may do even more - * damage. + * EXT4_GET_BLOCKS_CONVERT_UNWRITTEN: Caller wants the + * range specified by map to be marked unwritten. + * Zeroout the map range leaving rest as it is. */ - goto out; + + if (is_unwrit) + /* Shouldn't happen. Just exit */ + return -EINVAL; + + lblk =3D map->m_lblk; + len =3D map->m_len; + pblk =3D ext4_ext_pblock(ex) + (map->m_lblk - ee_block); + err =3D ext4_issue_zeroout(inode, lblk, pblk, len); + if (err) + /* ZEROOUT failed, just return original error */ + return err; + } else { + /* + * We no longer perform unwritten to unwritten splits in IO paths. + * Hence this should not happen. + */ + WARN_ON_ONCE(true); + return -EINVAL; } =20 -fix_extent_len: - ex->ee_len =3D orig_ex.ee_len; + err =3D ext4_ext_get_access(handle, inode, path + depth); + if (err) + return err; + + ext4_ext_mark_initialized(ex); + + ext4_ext_dirty(handle, inode, path + path->p_depth); + if (err) + return err; + /* - * Ignore ext4_ext_dirty return value since we are already in error path - * and err is a non-zero error code. + * The whole extent is initialized and stable now so it can be added to + * es cache */ - ext4_ext_dirty(handle, inode, path + path->p_depth); -out: - if (err) { - ext4_free_ext_path(path); - path =3D ERR_PTR(err); - } -out_path: - if (IS_ERR(path)) - /* Remove all remaining potentially stale extents. */ - ext4_es_remove_extent(inode, ee_block, ee_len); - ext4_ext_show_leaf(inode, path); - return path; + if (!(flags & EXT4_EX_NOCACHE)) + ext4_es_insert_extent(inode, le32_to_cpu(ex->ee_block), + ext4_ext_get_actual_len(ex), + ext4_ext_pblock(ex), + EXTENT_STATUS_WRITTEN, false); + + return 0; } =20 /* @@ -3382,11 +3428,13 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, int split_flag, int flags, unsigned int *allocated) { - ext4_lblk_t ee_block; + ext4_lblk_t ee_block, orig_ee_block; struct ext4_extent *ex; - unsigned int ee_len, depth; - int unwritten; - int split_flag1, flags1; + unsigned int ee_len, orig_ee_len, depth; + int unwritten, orig_unwritten; + int split_flag1 =3D 0, flags1 =3D 0; + int orig_err =3D 0; + int orig_flags =3D flags; =20 depth =3D ext_depth(inode); ex =3D path[depth].p_ext; @@ -3394,30 +3442,31 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, ee_len =3D ext4_ext_get_actual_len(ex); unwritten =3D ext4_ext_is_unwritten(ex); =20 + orig_ee_block =3D ee_block; + orig_ee_len =3D ee_len; + orig_unwritten =3D unwritten; + /* Do not cache extents that are in the process of being modified. */ flags |=3D EXT4_EX_NOCACHE; =20 if (map->m_lblk + map->m_len < ee_block + ee_len) { - split_flag1 =3D split_flag & EXT4_EXT_MAY_ZEROOUT; flags1 =3D flags | EXT4_GET_BLOCKS_SPLIT_NOMERGE; if (unwritten) split_flag1 |=3D EXT4_EXT_MARK_UNWRIT1 | EXT4_EXT_MARK_UNWRIT2; - if (split_flag & EXT4_EXT_DATA_VALID2) - split_flag1 |=3D map->m_lblk > ee_block ? - EXT4_EXT_DATA_PARTIAL_VALID1 : - EXT4_EXT_DATA_ENTIRE_VALID1; path =3D ext4_split_extent_at(handle, inode, path, map->m_lblk + map->m_len, split_flag1, flags1); if (IS_ERR(path)) - return path; + goto try_zeroout; + /* * Update path is required because previous ext4_split_extent_at * may result in split of original leaf or extent zeroout. */ path =3D ext4_find_extent(inode, map->m_lblk, path, flags); if (IS_ERR(path)) - return path; + goto try_zeroout; + depth =3D ext_depth(inode); ex =3D path[depth].p_ext; if (!ex) { @@ -3426,22 +3475,61 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, ext4_free_ext_path(path); return ERR_PTR(-EFSCORRUPTED); } - unwritten =3D ext4_ext_is_unwritten(ex); + + /* extent would have changed so update original values */ + orig_ee_block =3D le32_to_cpu(ex->ee_block); + orig_ee_len =3D ext4_ext_get_actual_len(ex); + orig_unwritten =3D ext4_ext_is_unwritten(ex); } =20 if (map->m_lblk >=3D ee_block) { - split_flag1 =3D split_flag & EXT4_EXT_DATA_VALID2; + split_flag1 =3D 0; if (unwritten) { split_flag1 |=3D EXT4_EXT_MARK_UNWRIT1; - split_flag1 |=3D split_flag & (EXT4_EXT_MAY_ZEROOUT | - EXT4_EXT_MARK_UNWRIT2); + split_flag1 |=3D split_flag & EXT4_EXT_MARK_UNWRIT2; } - path =3D ext4_split_extent_at(handle, inode, path, - map->m_lblk, split_flag1, flags); + path =3D ext4_split_extent_at(handle, inode, path, map->m_lblk, + split_flag1, flags); if (IS_ERR(path)) - return path; + goto try_zeroout; } =20 + goto success; + +try_zeroout: + /* + * There was an error in splitting the extent. So instead, just zeroout + * unwritten portions and convert it to initialized as a last resort. If + * there is any failure here we just return the original error + */ + + orig_err =3D PTR_ERR(path); + if (orig_err !=3D -ENOSPC && orig_err !=3D -EDQUOT && orig_err !=3D -ENOM= EM) + goto out_orig_err; + + /* we can't zeroout? just return the original err */ + if (!(split_flag & EXT4_EXT_MAY_ZEROOUT)) + goto out_orig_err; + + path =3D ext4_find_extent(inode, map->m_lblk, NULL, flags); + if (IS_ERR(path)) + goto out_orig_err; + + depth =3D ext_depth(inode); + ex =3D path[depth].p_ext; + ee_block =3D le32_to_cpu(ex->ee_block); + ee_len =3D ext4_ext_get_actual_len(ex); + unwritten =3D ext4_ext_is_unwritten(ex); + + /* extent to zeroout should have been unchanged but its not */ + if (WARN_ON(ee_block !=3D orig_ee_block || ee_len !=3D orig_ee_len || + unwritten !=3D orig_unwritten)) + goto out_free_path; + + if (ext4_split_extent_zeroout(handle, inode, path, map, orig_flags)) + goto out_free_path; + +success: if (allocated) { if (map->m_lblk + map->m_len > ee_block + ee_len) *allocated =3D ee_len - (map->m_lblk - ee_block); @@ -3450,6 +3538,12 @@ static struct ext4_ext_path *ext4_split_extent(handl= e_t *handle, } ext4_ext_show_leaf(inode, path); return path; + +out_free_path: + ext4_free_ext_path(path); +out_orig_err: + return ERR_PTR(orig_err); + } =20 /* @@ -3485,7 +3579,7 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, ext4_lblk_t ee_block, eof_block; unsigned int ee_len, depth, map_len =3D map->m_len; int err =3D 0; - int split_flag =3D EXT4_EXT_DATA_VALID2; + int split_flag =3D 0; unsigned int max_zeroout =3D 0; =20 ext_debug(inode, "logical block %llu, max_blocks %u\n", @@ -3695,7 +3789,7 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, =20 fallback: path =3D ext4_split_extent(handle, inode, path, &split_map, split_flag, - flags, NULL); + flags | EXT4_GET_BLOCKS_CONVERT, NULL); if (IS_ERR(path)) return path; out: @@ -3759,11 +3853,7 @@ static struct ext4_ext_path *ext4_split_convert_exte= nts(handle_t *handle, ee_block =3D le32_to_cpu(ex->ee_block); ee_len =3D ext4_ext_get_actual_len(ex); =20 - /* Convert to unwritten */ - if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { - split_flag |=3D EXT4_EXT_DATA_ENTIRE_VALID1; - /* Split the existing unwritten extent */ - } else if (flags & (EXT4_GET_BLOCKS_UNWRIT_EXT | + if (flags & (EXT4_GET_BLOCKS_UNWRIT_EXT | EXT4_GET_BLOCKS_CONVERT)) { /* * It is safe to convert extent to initialized via explicit @@ -3772,9 +3862,6 @@ static struct ext4_ext_path *ext4_split_convert_exten= ts(handle_t *handle, split_flag |=3D ee_block + ee_len <=3D eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; split_flag |=3D EXT4_EXT_MARK_UNWRIT2; - /* Convert to initialized */ - if (flags & EXT4_GET_BLOCKS_CONVERT) - split_flag |=3D EXT4_EXT_DATA_VALID2; } flags |=3D EXT4_GET_BLOCKS_SPLIT_NOMERGE; return ext4_split_extent(handle, inode, path, map, split_flag, flags, --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A572132D0D0; Mon, 19 Jan 2026 17:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844620; cv=none; b=FRbmccbEY+LW+hW06v2VkTHtpyCTS6r3PJ8EU/l18iTUaYwDxQdGrxjpurbYGy65tYovy22Z8lhGVtW/WRgZHnWLdJmWxw94lHGO+FD15gPU27shpUuuffLFUBFFnutacTzAPGdM7DkOUKqfs635miZC5cY7Avw+xRPh1042asA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844620; c=relaxed/simple; bh=m+QM1qnpslIn+awlLZV83T5BSfpEspmCK4fduK2uODk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BxSdesB3LYKtbiMoWs4AlbaX2hdeRLIVZMS5EJ8OuF9a8Eb0TiRYy5ooaTDN6JdcxcM6zJh8rJoUewvfN0tP9htv6SIS7nvl6iclE3Shp1yO3pzSRIVFa7yeAK+4Mfhky1XtB3Zwk0mi9oiJOnLK2ywbUhVP/AfqxuvoZpLlncw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=Qi7xR9bL; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="Qi7xR9bL" Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JEtTMc030537; Mon, 19 Jan 2026 17:43:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=4rxENgHJ53tAsebRM 2B9Pp/ssQtME1JWkyZ11dYkrJg=; b=Qi7xR9bLE7p7sYCbWi9mru4hfCRF/y4ON Jbn7p47fFcjTVrhMo5OldKX5eCPzQLnz5AnSPOGy6729myaxh+RZw7jeNCC/y4Qx Ygksc78NKiTQERIRRma1KriZCILi0aIc+R3/8g4o8cFKPFPFlx1us6corYqlRI1E I7p2Ha9IVm19SC17iIK/nL4o6zN4yhbx+0uOWCeUVpmKvgVBP1zLrT9qW4Sfapom 2l2+qG6LSeIfLrAPKu0pO3c9dBv5/2/appDvpJIrIRSXzL66waeHv46wxyXv1q3w S3Ea9wNkNkM21cfHSu2SvB+1oBWU6esgYCsXf08//cqeSmGeOlwjg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br255spcx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:32 +0000 (GMT) Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHaLcZ016668; Mon, 19 Jan 2026 17:43:31 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br255spcu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:31 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JH16Xc009265; Mon, 19 Jan 2026 17:43:30 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brp8k04vh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:30 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhSw658917292 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:28 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5261C20043; Mon, 19 Jan 2026 17:43:28 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 883FE20040; Mon, 19 Jan 2026 17:43:25 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:25 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 7/8] ext4: Refactor split and convert extents Date: Mon, 19 Jan 2026 23:13:03 +0530 Message-ID: <2987c270310d2554d171eaeb83b1249b2aac8fe4.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX86JAWWhG0/UM mYNy6lwYIrvVzA/JEcFXdo1axo0El1UH3EaNicJxdfoNC3FMJLJp7XY5tBQhi+keDquEI0YgZf+ 8z1N9gB5K68veOfnx3AFt4rkDshxreAsQlG8wcDVhrKZp6NqoUy7VEYuQwhuQ1sRrCz8xPRLhpO jbOSgZjiDKItgtx1JCppSegPkC6INwTfMf9xmkGUmGH7FKwohF7+XgK1jnVVdx6rRexiZQ4oQ2A a5ISVJcJYIX+DkkC82meERQEa4sxjI5Ck7s6K8cfg2/qgXZv0rfiQE+1r7ACBEhAeI45oM4vi84 +B0yI27Ck6g45V8CYnMN++frhaBgqUUgserRyryR5DAlYlm/sepj8l8mQHxjNAM9iIMnjt6ryST hWd6lyQvpAtmJXrQ/G0qZp1FRIqY/ImE7cbH83HX/KzKHEK73XRXN/N4bpXcfAf0L1p3uw7z6Ou SFCyVdzEdJ3aP32Isfg== X-Authority-Analysis: v=2.4 cv=BpSQAIX5 c=1 sm=1 tr=0 ts=696e6d44 cx=c_pps a=3Bg1Hr4SwmMryq2xdFQyZA==:117 a=3Bg1Hr4SwmMryq2xdFQyZA==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=0bmpNBf66RkvyeOCC5QA:9 X-Proofpoint-GUID: m95IDv8_-EgRakaVbtD76q3SGonv_R0T X-Proofpoint-ORIG-GUID: u6GdxZGDr0MID9fMI4PCsxdKTgxbC71_ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 clxscore=1015 adultscore=0 phishscore=0 malwarescore=0 impostorscore=0 suspectscore=0 priorityscore=1501 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" ext4_split_convert_extents() has been historically prone to subtle bugs and inconsistent behavior due to the way all the various flags interact with the extent split and conversion process. For example, callers like ext4_convert_unwritten_extents_endio() and convert_initialized_extents() needed to open code extent conversion despite passing CONVERT or CONVERT_UNWRITTEN flags because ext4_split_convert_extents() wasn't performing the conversion. Hence, refactor ext4_split_convert_extents() to clearly enforce the semantics of each flag. The major changes here are: * Clearly separate the split and convert process: * ext4_split_extent() and ext4_split_extent_at() are now only responsible to perform the split. * ext4_split_convert_extents() is now responsible to perform extent conversion after calling ext4_split_extent() for splitting. * This helps get rid of all the MARK_UNWRIT* flags. * Clearly enforce the semantics of flags passed to ext4_split_convert_extents(): * EXT4_GET_BLOCKS_CONVERT: Will convert the split extent to written * EXT4_GET_BLOCKS_CONVERT_UNWRITTEN: Will convert the split extent to unwritten * Modify all callers to enforce the above semantics. * Use ext4_split_convert_extents() instead of ext4_split_extents() in ext4_ext_convert_to_initialized() for uniformity. * Now that ext4_split_convert_extents() is handling caching to es, we dont need to do it in ext4_split_extent_zeroout(). * Cleanup all callers open coding the conversion logic. Further, modify kuniy tests to pass flags based on the new semantics. From an end user point of view, we should not see any changes in behavior of ext4. Reviewed-by: Jan Kara Reviewed-by: Zhang Yi Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents.c | 277 +++++++++++++++++++--------------------------- 1 file changed, 112 insertions(+), 165 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 5689375221d2..fbfe2576d5f4 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -41,8 +41,9 @@ */ #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ due to ENOSPC */ -#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ -#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ +static struct ext4_ext_path *ext4_split_convert_extents( + handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, + struct ext4_ext_path *path, int flags, unsigned int *allocated); =20 static __le32 ext4_extent_block_csum(struct inode *inode, struct ext4_extent_header *eh) @@ -84,8 +85,7 @@ static void ext4_extent_block_csum_set(struct inode *inod= e, static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle, struct inode *inode, struct ext4_ext_path *path, - ext4_lblk_t split, - int split_flag, int flags); + ext4_lblk_t split, int flags); =20 static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) { @@ -333,15 +333,12 @@ ext4_force_split_extent_at(handle_t *handle, struct i= node *inode, struct ext4_ext_path *path, ext4_lblk_t lblk, int nofail) { - int unwritten =3D ext4_ext_is_unwritten(path[path->p_depth].p_ext); int flags =3D EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_SPLIT_NOMERGE; =20 if (nofail) flags |=3D EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL; =20 - return ext4_split_extent_at(handle, inode, path, lblk, unwritten ? - EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, - flags); + return ext4_split_extent_at(handle, inode, path, lblk, flags); } =20 static int @@ -3173,17 +3170,11 @@ static int ext4_ext_zeroout(struct inode *inode, st= ruct ext4_extent *ex) * @inode: the file inode * @path: the path to the extent * @split: the logical block where the extent is splitted. - * @split_flags: indicates if the extent could be zeroout if split fails, = and - * the states(init or unwritten) of new extents. * @flags: flags used to insert new extent to extent tree. * * * Splits extent [a, b] into two extents [a, @split) and [@split, b], stat= es - * of which are determined by split_flag. - * - * There are two cases: - * a> the extent are splitted into two extent. - * b> split is not needed, and just mark the extent. + * of which are same as the original extent. No conversion is performed. * * Return an extent path pointer on success, or an error pointer on failur= e. On * failure, the extent is restored to original state. @@ -3192,14 +3183,14 @@ static struct ext4_ext_path *ext4_split_extent_at(h= andle_t *handle, struct inode *inode, struct ext4_ext_path *path, ext4_lblk_t split, - int split_flag, int flags) + int flags) { ext4_fsblk_t newblock; ext4_lblk_t ee_block; struct ext4_extent *ex, newex, orig_ex; struct ext4_extent *ex2 =3D NULL; unsigned int ee_len, depth; - int err =3D 0, insert_err =3D 0; + int err =3D 0, insert_err =3D 0, is_unwrit =3D 0; =20 /* Do not cache extents that are in the process of being modified. */ flags |=3D EXT4_EX_NOCACHE; @@ -3213,39 +3204,24 @@ static struct ext4_ext_path *ext4_split_extent_at(h= andle_t *handle, ee_block =3D le32_to_cpu(ex->ee_block); ee_len =3D ext4_ext_get_actual_len(ex); newblock =3D split - ee_block + ext4_ext_pblock(ex); + is_unwrit =3D ext4_ext_is_unwritten(ex); =20 BUG_ON(split < ee_block || split >=3D (ee_block + ee_len)); - BUG_ON(!ext4_ext_is_unwritten(ex) && - split_flag & (EXT4_EXT_MAY_ZEROOUT | - EXT4_EXT_MARK_UNWRIT1 | - EXT4_EXT_MARK_UNWRIT2)); =20 - err =3D ext4_ext_get_access(handle, inode, path + depth); - if (err) + /* + * No split needed + */ + if (split =3D=3D ee_block) goto out; =20 - if (split =3D=3D ee_block) { - /* - * case b: block @split is the block that the extent begins with - * then we just change the state of the extent, and splitting - * is not needed. - */ - if (split_flag & EXT4_EXT_MARK_UNWRIT2) - ext4_ext_mark_unwritten(ex); - else - ext4_ext_mark_initialized(ex); - - if (!(flags & EXT4_GET_BLOCKS_SPLIT_NOMERGE)) - ext4_ext_try_to_merge(handle, inode, path, ex); - - err =3D ext4_ext_dirty(handle, inode, path + path->p_depth); + err =3D ext4_ext_get_access(handle, inode, path + depth); + if (err) goto out; - } =20 /* case a */ memcpy(&orig_ex, ex, sizeof(orig_ex)); ex->ee_len =3D cpu_to_le16(split - ee_block); - if (split_flag & EXT4_EXT_MARK_UNWRIT1) + if (is_unwrit) ext4_ext_mark_unwritten(ex); =20 /* @@ -3260,7 +3236,7 @@ static struct ext4_ext_path *ext4_split_extent_at(han= dle_t *handle, ex2->ee_block =3D cpu_to_le32(split); ex2->ee_len =3D cpu_to_le16(ee_len - (split - ee_block)); ext4_ext_store_pblock(ex2, newblock); - if (split_flag & EXT4_EXT_MARK_UNWRIT2) + if (is_unwrit) ext4_ext_mark_unwritten(ex2); =20 path =3D ext4_ext_insert_extent(handle, inode, path, &newex, flags); @@ -3393,20 +3369,10 @@ static int ext4_split_extent_zeroout(handle_t *hand= le, struct inode *inode, =20 ext4_ext_mark_initialized(ex); =20 - ext4_ext_dirty(handle, inode, path + path->p_depth); + ext4_ext_dirty(handle, inode, path + depth); if (err) return err; =20 - /* - * The whole extent is initialized and stable now so it can be added to - * es cache - */ - if (!(flags & EXT4_EX_NOCACHE)) - ext4_es_insert_extent(inode, le32_to_cpu(ex->ee_block), - ext4_ext_get_actual_len(ex), - ext4_ext_pblock(ex), - EXTENT_STATUS_WRITTEN, false); - return 0; } =20 @@ -3426,15 +3392,13 @@ static struct ext4_ext_path *ext4_split_extent(hand= le_t *handle, struct ext4_ext_path *path, struct ext4_map_blocks *map, int split_flag, int flags, - unsigned int *allocated) + unsigned int *allocated, bool *did_zeroout) { ext4_lblk_t ee_block, orig_ee_block; struct ext4_extent *ex; unsigned int ee_len, orig_ee_len, depth; int unwritten, orig_unwritten; - int split_flag1 =3D 0, flags1 =3D 0; int orig_err =3D 0; - int orig_flags =3D flags; =20 depth =3D ext_depth(inode); ex =3D path[depth].p_ext; @@ -3450,12 +3414,8 @@ static struct ext4_ext_path *ext4_split_extent(handl= e_t *handle, flags |=3D EXT4_EX_NOCACHE; =20 if (map->m_lblk + map->m_len < ee_block + ee_len) { - flags1 =3D flags | EXT4_GET_BLOCKS_SPLIT_NOMERGE; - if (unwritten) - split_flag1 |=3D EXT4_EXT_MARK_UNWRIT1 | - EXT4_EXT_MARK_UNWRIT2; path =3D ext4_split_extent_at(handle, inode, path, - map->m_lblk + map->m_len, split_flag1, flags1); + map->m_lblk + map->m_len, flags); if (IS_ERR(path)) goto try_zeroout; =20 @@ -3483,13 +3443,8 @@ static struct ext4_ext_path *ext4_split_extent(handl= e_t *handle, } =20 if (map->m_lblk >=3D ee_block) { - split_flag1 =3D 0; - if (unwritten) { - split_flag1 |=3D EXT4_EXT_MARK_UNWRIT1; - split_flag1 |=3D split_flag & EXT4_EXT_MARK_UNWRIT2; - } path =3D ext4_split_extent_at(handle, inode, path, map->m_lblk, - split_flag1, flags); + flags); if (IS_ERR(path)) goto try_zeroout; } @@ -3526,9 +3481,13 @@ static struct ext4_ext_path *ext4_split_extent(handl= e_t *handle, unwritten !=3D orig_unwritten)) goto out_free_path; =20 - if (ext4_split_extent_zeroout(handle, inode, path, map, orig_flags)) + if (ext4_split_extent_zeroout(handle, inode, path, map, flags)) goto out_free_path; =20 + /* zeroout succeeded */ + if (did_zeroout) + *did_zeroout =3D true; + success: if (allocated) { if (map->m_lblk + map->m_len > ee_block + ee_len) @@ -3579,7 +3538,6 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, ext4_lblk_t ee_block, eof_block; unsigned int ee_len, depth, map_len =3D map->m_len; int err =3D 0; - int split_flag =3D 0; unsigned int max_zeroout =3D 0; =20 ext_debug(inode, "logical block %llu, max_blocks %u\n", @@ -3731,9 +3689,7 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, * It is safe to convert extent to initialized via explicit * zeroout only if extent is fully inside i_size or new_size. */ - split_flag |=3D ee_block + ee_len <=3D eof_block ? EXT4_EXT_MAY_ZEROOUT := 0; - - if (EXT4_EXT_MAY_ZEROOUT & split_flag) + if (ee_block + ee_len <=3D eof_block) max_zeroout =3D sbi->s_extent_max_zeroout_kb >> (inode->i_sb->s_blocksize_bits - 10); =20 @@ -3788,8 +3744,8 @@ ext4_ext_convert_to_initialized(handle_t *handle, str= uct inode *inode, } =20 fallback: - path =3D ext4_split_extent(handle, inode, path, &split_map, split_flag, - flags | EXT4_GET_BLOCKS_CONVERT, NULL); + path =3D ext4_split_convert_extents(handle, inode, &split_map, path, + flags | EXT4_GET_BLOCKS_CONVERT, NULL); if (IS_ERR(path)) return path; out: @@ -3839,7 +3795,8 @@ static struct ext4_ext_path *ext4_split_convert_exten= ts(handle_t *handle, ext4_lblk_t ee_block; struct ext4_extent *ex; unsigned int ee_len; - int split_flag =3D 0, depth; + int split_flag =3D 0, depth, err =3D 0; + bool did_zeroout =3D false; =20 ext_debug(inode, "logical block %llu, max_blocks %u\n", (unsigned long long)map->m_lblk, map->m_len); @@ -3853,19 +3810,81 @@ static struct ext4_ext_path *ext4_split_convert_ext= ents(handle_t *handle, ee_block =3D le32_to_cpu(ex->ee_block); ee_len =3D ext4_ext_get_actual_len(ex); =20 - if (flags & (EXT4_GET_BLOCKS_UNWRIT_EXT | - EXT4_GET_BLOCKS_CONVERT)) { - /* - * It is safe to convert extent to initialized via explicit - * zeroout only if extent is fully inside i_size or new_size. - */ + /* No split needed */ + if (ee_block =3D=3D map->m_lblk && ee_len =3D=3D map->m_len) + goto convert; + + /* + * We don't use zeroout fallback for written to unwritten conversion as + * it is not as critical as endio and it might take unusually long. + * Also, it is only safe to convert extent to initialized via explicit + * zeroout only if extent is fully inside i_size or new_size. + */ + if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) split_flag |=3D ee_block + ee_len <=3D eof_block ? - EXT4_EXT_MAY_ZEROOUT : 0; - split_flag |=3D EXT4_EXT_MARK_UNWRIT2; + EXT4_EXT_MAY_ZEROOUT : + 0; + + /* + * pass SPLIT_NOMERGE explicitly so we don't end up merging extents we + * just split. + */ + path =3D ext4_split_extent(handle, inode, path, map, split_flag, + flags | EXT4_GET_BLOCKS_SPLIT_NOMERGE, + allocated, &did_zeroout); + if (IS_ERR(path)) + return path; + +convert: + path =3D ext4_find_extent(inode, map->m_lblk, path, flags); + if (IS_ERR(path)) + return path; + + depth =3D ext_depth(inode); + ex =3D path[depth].p_ext; + + /* + * Conversion is already handled in case of zeroout + */ + if (!did_zeroout) { + err =3D ext4_ext_get_access(handle, inode, path + depth); + if (err) + goto err; + + if (flags & EXT4_GET_BLOCKS_CONVERT) + ext4_ext_mark_initialized(ex); + else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) + ext4_ext_mark_unwritten(ex); + + if (!(flags & EXT4_GET_BLOCKS_SPLIT_NOMERGE)) + /* + * note: ext4_ext_correct_indexes() isn't needed here because + * borders are not changed + */ + ext4_ext_try_to_merge(handle, inode, path, ex); + + err =3D ext4_ext_dirty(handle, inode, path + depth); + if (err) + goto err; + } + + /* Lets update the extent status tree after conversion */ + if (!(flags & EXT4_EX_NOCACHE)) + ext4_es_insert_extent(inode, le32_to_cpu(ex->ee_block), + ext4_ext_get_actual_len(ex), + ext4_ext_pblock(ex), + ext4_ext_is_unwritten(ex) ? + EXTENT_STATUS_UNWRITTEN : + EXTENT_STATUS_WRITTEN, + false); + +err: + if (err) { + ext4_free_ext_path(path); + return ERR_PTR(err); } - flags |=3D EXT4_GET_BLOCKS_SPLIT_NOMERGE; - return ext4_split_extent(handle, inode, path, map, split_flag, flags, - allocated); + + return path; } =20 static struct ext4_ext_path * @@ -3877,7 +3896,6 @@ ext4_convert_unwritten_extents_endio(handle_t *handle= , struct inode *inode, ext4_lblk_t ee_block; unsigned int ee_len; int depth; - int err =3D 0; =20 depth =3D ext_depth(inode); ex =3D path[depth].p_ext; @@ -3887,41 +3905,8 @@ ext4_convert_unwritten_extents_endio(handle_t *handl= e, struct inode *inode, ext_debug(inode, "logical block %llu, max_blocks %u\n", (unsigned long long)ee_block, ee_len); =20 - if (ee_block !=3D map->m_lblk || ee_len > map->m_len) { - path =3D ext4_split_convert_extents(handle, inode, map, path, - flags, NULL); - if (IS_ERR(path)) - return path; - - path =3D ext4_find_extent(inode, map->m_lblk, path, flags); - if (IS_ERR(path)) - return path; - depth =3D ext_depth(inode); - ex =3D path[depth].p_ext; - } - - err =3D ext4_ext_get_access(handle, inode, path + depth); - if (err) - goto errout; - /* first mark the extent as initialized */ - ext4_ext_mark_initialized(ex); - - /* note: ext4_ext_correct_indexes() isn't needed here because - * borders are not changed - */ - ext4_ext_try_to_merge(handle, inode, path, ex); - - /* Mark modified extent as dirty */ - err =3D ext4_ext_dirty(handle, inode, path + path->p_depth); - if (err) - goto errout; - - ext4_ext_show_leaf(inode, path); - return path; - -errout: - ext4_free_ext_path(path); - return ERR_PTR(err); + return ext4_split_convert_extents(handle, inode, map, path, flags, + NULL); } =20 static struct ext4_ext_path * @@ -3935,7 +3920,6 @@ convert_initialized_extent(handle_t *handle, struct i= node *inode, ext4_lblk_t ee_block; unsigned int ee_len; int depth; - int err =3D 0; =20 /* * Make sure that the extent is no bigger than we support with @@ -3952,40 +3936,11 @@ convert_initialized_extent(handle_t *handle, struct= inode *inode, ext_debug(inode, "logical block %llu, max_blocks %u\n", (unsigned long long)ee_block, ee_len); =20 - if (ee_block !=3D map->m_lblk || ee_len > map->m_len) { - path =3D ext4_split_convert_extents(handle, inode, map, path, - flags, NULL); - if (IS_ERR(path)) - return path; - - path =3D ext4_find_extent(inode, map->m_lblk, path, flags); - if (IS_ERR(path)) - return path; - depth =3D ext_depth(inode); - ex =3D path[depth].p_ext; - if (!ex) { - EXT4_ERROR_INODE(inode, "unexpected hole at %lu", - (unsigned long) map->m_lblk); - err =3D -EFSCORRUPTED; - goto errout; - } - } - - err =3D ext4_ext_get_access(handle, inode, path + depth); - if (err) - goto errout; - /* first mark the extent as unwritten */ - ext4_ext_mark_unwritten(ex); - - /* note: ext4_ext_correct_indexes() isn't needed here because - * borders are not changed - */ - ext4_ext_try_to_merge(handle, inode, path, ex); + path =3D ext4_split_convert_extents(handle, inode, map, path, flags, + NULL); + if (IS_ERR(path)) + return path; =20 - /* Mark modified extent as dirty */ - err =3D ext4_ext_dirty(handle, inode, path + path->p_depth); - if (err) - goto errout; ext4_ext_show_leaf(inode, path); =20 ext4_update_inode_fsync_trans(handle, inode, 1); @@ -3995,10 +3950,6 @@ convert_initialized_extent(handle_t *handle, struct = inode *inode, *allocated =3D map->m_len; map->m_len =3D *allocated; return path; - -errout: - ext4_free_ext_path(path); - return ERR_PTR(err); } =20 static struct ext4_ext_path * @@ -5632,7 +5583,7 @@ static int ext4_insert_range(struct file *file, loff_= t offset, loff_t len) struct ext4_extent *extent; ext4_lblk_t start_lblk, len_lblk, ee_start_lblk =3D 0; unsigned int credits, ee_len; - int ret, depth, split_flag =3D 0; + int ret, depth; loff_t start; =20 trace_ext4_insert_range(inode, offset, len); @@ -5703,12 +5654,8 @@ static int ext4_insert_range(struct file *file, loff= _t offset, loff_t len) */ if ((start_lblk > ee_start_lblk) && (start_lblk < (ee_start_lblk + ee_len))) { - if (ext4_ext_is_unwritten(extent)) - split_flag =3D EXT4_EXT_MARK_UNWRIT1 | - EXT4_EXT_MARK_UNWRIT2; path =3D ext4_split_extent_at(handle, inode, path, - start_lblk, split_flag, - EXT4_EX_NOCACHE | + start_lblk, EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_SPLIT_NOMERGE | EXT4_GET_BLOCKS_METADATA_NOFAIL); } --=20 2.52.0 From nobody Sat Feb 7 22:07:18 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5799A3659F5; Mon, 19 Jan 2026 17:43:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844623; cv=none; b=PEs8ktYuhmtjj0pM6yFqLqStEjUz0P+T6wPDz57JC4Y7hQqolR2HGlc6pf64PWdkCH7/o9zaqiHvfypvR+Bo3QTWIL++GolHI0X8C4FX403s8wMv4SjY5CBkDOwDX3I96wR43WZsUtANsBLgAbhjaIKEJJGkPObCYvt9s0NNus4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768844623; c=relaxed/simple; bh=+s/h8WJppVKzzUHvAQjxlIa3VRKnGTPCvZL1eqkR6kQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s5/T2qumyebY8/9VJOupeNMQJiNrvFgOH08kanFPnPa9vDUv0v90qzFvqPY1Y9QM5uGJqEbinepbZSt6ItNcI19oqwl6bh7rlADum/MvsIUPpTGEnoElwgx8YkFAX2mKdqRLC4zb2jRlajdPZq4/f0J7Qua3AC+yHIvnxxeHEiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=Q5+NNjnB; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="Q5+NNjnB" Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 60JDUDaJ032418; Mon, 19 Jan 2026 17:43:34 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=jsoQTB2b/hNGorzIg t09q6mjMQ9NXv3UIZdugbKGb4Q=; b=Q5+NNjnB+lDQDEvFJeqP9vWpy85AhguMc By8p3vTHQsyGWs3plxZs8vlYDHB4uypS33RyXFYC/FpPXAa4qtGLEzMhb6wdqenb 2/QDxyFWxqn1A6SvpPC4rl15zz9l2GXL62g+1fRXDtaYNixNJSDUjC1WQS0eiQlY 3JWLYRGrmtD7NXp8TYQZXgAKE99rn7cwH27z7OpJ0npPclSEOWkDAEpbHTDB/zfm Zy1xBAmc/Gu2dVSafefO6T9Y74OK1wlzTCucHdmpRhMMy+4Nej7OJY6KkWlRqs9L Sa18ZMA//a/bZgp4lWm6iY03VRHioYUfxJ3jEFyQ5H8W5hPrWTKXA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br22u8xsq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:34 +0000 (GMT) Received: from m0360072.ppops.net (m0360072.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 60JHhYdu030376; Mon, 19 Jan 2026 17:43:34 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4br22u8xsk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:34 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 60JGBMqo016636; Mon, 19 Jan 2026 17:43:33 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4brn4xrbs4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Jan 2026 17:43:33 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 60JHhVIb52363674 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 19 Jan 2026 17:43:31 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4DBF420043; Mon, 19 Jan 2026 17:43:31 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E86A020040; Mon, 19 Jan 2026 17:43:28 +0000 (GMT) Received: from li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com (unknown [9.124.220.173]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 19 Jan 2026 17:43:28 +0000 (GMT) From: Ojaswin Mujoo To: linux-ext4@vger.kernel.org, "Theodore Ts'o" Cc: Ritesh Harjani , Zhang Yi , Jan Kara , libaokun1@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 8/8] ext4: Allow zeroout when doing written to unwritten split Date: Mon, 19 Jan 2026 23:13:04 +0530 Message-ID: <326263653b81906881df4cc45c7e5e6e61755721.1768844021.git.ojaswin@linux.ibm.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: DNSPP5izju6hX2v2qOyaSfs0imAW5vmP X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE5MDE0NCBTYWx0ZWRfX76j/O55z1HIj nJhu2u94BgoI+UWhCL80CFaZjkem1jz46rfS6ZQ9KHJ3eLuVVU8gaKkBoJfA+TTScxwahMR1Eeu WOJvE0GJO3FezWNdJlrHXlDMQ/3OcdZvaycWPoM99FqB1fQal0MUlVHrlYozkSCFW2UaXDUgVFE 2dPf19ua4WYfPyQt03Ps9ElLIA3v+1QCrJAl1sQFwlYTLGuyNzHJCaDZRgIo7vhWmpF8KWWf26y kAw7c3yNR9YH65hXnCIvdvu4S/FohCJVjolVTWte8HjWuSsaW30DtDcXGEhAl27U61Pvh3ODQzR R0cI2iy2VBA50kGxOXfzbFccy1eeZM0qAex8JUR1N9SlTE1SgbEeOI8Up5CUIK8sUakVZvGoXDK k2XQKG2kA2kI1OzZq2OSU05Kxa8N0oYM9cAjuPds15K52Pj+JpA6JDNgenftTmxnQ4fcrn1phad yuRKGhqnYX3EnrzXHPw== X-Proofpoint-ORIG-GUID: NG0zSLyqZQRDvUJirqjCIuThTWsb-Y_N X-Authority-Analysis: v=2.4 cv=Sp2dKfO0 c=1 sm=1 tr=0 ts=696e6d46 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=i0EeH86SAAAA:8 a=VnNF1IyMAAAA:8 a=kZK9M-q0HhXr2Cl_658A:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-19_04,2026-01-19_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 bulkscore=0 spamscore=0 adultscore=0 phishscore=0 priorityscore=1501 lowpriorityscore=0 malwarescore=0 suspectscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2601150000 definitions=main-2601190144 Content-Type: text/plain; charset="utf-8" Currently, when we are doing an extent split and convert operation of written to unwritten extent (example, as done by ZERO_RANGE), we don't allow the zeroout fallback in case the extent tree manipulation fails. This is mostly because zeroout might take unsually long and the fact that this code path is more tolerant to failures than endio. Since we have zeroout machinery in place, we might as well use it hence lift this restriction. To mitigate zeroout taking too long respect the max zeroout limit here so that the operation finishes relatively fast. Also, add kunit tests for this case. Reviewed-by: Jan Kara Reviewed-by: Zhang Yi Signed-off-by: Ojaswin Mujoo --- fs/ext4/extents-test.c | 98 +++++++++++++++++++++++++++++++++++++++++- fs/ext4/extents.c | 33 ++++++++++---- 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c index 7b7ed347e14e..f58f59b7872b 100644 --- a/fs/ext4/extents-test.c +++ b/fs/ext4/extents-test.c @@ -611,11 +611,57 @@ static const struct kunit_ext_test_param test_split_c= onvert_params[] =3D { .is_unwrit =3D 0 } }, .is_zeroout_test =3D 1, .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', .off_blk =3D 1, .len_blk =3D EX_DATA_LEN - 2= }, + { .exp_char =3D 0, .off_blk =3D EX_DATA_LEN - 1, .len_blk =3D 1 }= } }, + + /* writ to unwrit splits */ + { .desc =3D "split writ extent to 2 extents and convert 1st half unwrit (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, { .exp_char =3D 'X', .off_blk =3D 1, - .len_blk =3D EX_DATA_LEN - 2 }, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split writ extent to 2 extents and convert 2nd half unwrit (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split writ extent to 3 extents and convert 2nd half unwrit (= zeroout)", + .type =3D TEST_SPLIT_CONVERT, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 2 }, + { .exp_char =3D 'X', .off_blk =3D EX_DATA_LEN - 1, .len_blk =3D 1 } } }, }; @@ -665,6 +711,56 @@ static const struct kunit_ext_test_param test_convert_= initialized_params[] =3D { .ex_len =3D 1, .is_unwrit =3D 0 } }, .is_zeroout_test =3D 0 }, + + /* writ to unwrit splits (zeroout) */ + { .desc =3D "split writ extent to 2 extents and convert 1st half unwrit (= zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK, .m_len =3D 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 0, .off_blk =3D 0, .len_blk =3D 1= }, + { .exp_char =3D 'X', + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split writ extent to 2 extents and convert 2nd half unwrit (= zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 1 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 2, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 1 } } }, + { .desc =3D "split writ extent to 3 extents and convert 2nd half unwrit (= zeroout)", + .type =3D TEST_CREATE_BLOCKS, + .is_unwrit_at_start =3D 0, + .split_flags =3D EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, + .split_map =3D { .m_lblk =3D EX_DATA_LBLK + 1, .m_len =3D EX_DATA_LEN -= 2 }, + .nr_exp_ext =3D 1, + .exp_ext_state =3D { { .ex_lblk =3D EX_DATA_LBLK, + .ex_len =3D EX_DATA_LEN, + .is_unwrit =3D 0 } }, + .is_zeroout_test =3D 1, + .nr_exp_data_segs =3D 3, + .exp_data_state =3D { { .exp_char =3D 'X', .off_blk =3D 0, .len_blk =3D= 1 }, + { .exp_char =3D 0, + .off_blk =3D 1, + .len_blk =3D EX_DATA_LEN - 2 }, + { .exp_char =3D 'X', + .off_blk =3D EX_DATA_LEN - 1, + .len_blk =3D 1 } } }, }; =20 /* Tests to trigger ext4_ext_map_blocks() -> ext4_ext_handle_unwritten_exn= tents() */ diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index fbfe2576d5f4..d55633ddaad0 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -3466,6 +3466,15 @@ static struct ext4_ext_path *ext4_split_extent(handl= e_t *handle, if (!(split_flag & EXT4_EXT_MAY_ZEROOUT)) goto out_orig_err; =20 + if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { + int max_zeroout_blks =3D + EXT4_SB(inode->i_sb)->s_extent_max_zeroout_kb >> + (inode->i_sb->s_blocksize_bits - 10); + + if (map->m_len > max_zeroout_blks) + goto out_orig_err; + } + path =3D ext4_find_extent(inode, map->m_lblk, NULL, flags); if (IS_ERR(path)) goto out_orig_err; @@ -3815,15 +3824,10 @@ static struct ext4_ext_path *ext4_split_convert_ext= ents(handle_t *handle, goto convert; =20 /* - * We don't use zeroout fallback for written to unwritten conversion as - * it is not as critical as endio and it might take unusually long. - * Also, it is only safe to convert extent to initialized via explicit + * It is only safe to convert extent to initialized via explicit * zeroout only if extent is fully inside i_size or new_size. */ - if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) - split_flag |=3D ee_block + ee_len <=3D eof_block ? - EXT4_EXT_MAY_ZEROOUT : - 0; + split_flag |=3D ee_block + ee_len <=3D eof_block ? EXT4_EXT_MAY_ZEROOUT := 0; =20 /* * pass SPLIT_NOMERGE explicitly so we don't end up merging extents we @@ -3945,7 +3949,20 @@ convert_initialized_extent(handle_t *handle, struct = inode *inode, =20 ext4_update_inode_fsync_trans(handle, inode, 1); =20 - map->m_flags |=3D EXT4_MAP_UNWRITTEN; + /* + * The extent might be initialized in case of zeroout. + */ + path =3D ext4_find_extent(inode, map->m_lblk, path, flags); + if (IS_ERR(path)) + return path; + + depth =3D ext_depth(inode); + ex =3D path[depth].p_ext; + + if (ext4_ext_is_unwritten(ex)) + map->m_flags |=3D EXT4_MAP_UNWRITTEN; + else + map->m_flags |=3D EXT4_MAP_MAPPED; if (*allocated > map->m_len) *allocated =3D map->m_len; map->m_len =3D *allocated; --=20 2.52.0