[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <0182586e50e4332375d0db77f31c596536a94f2e.1767528171.git.ojaswin@linux.ibm.com>
Date: Sun, 4 Jan 2026 17:49:15 +0530
From: Ojaswin Mujoo <ojaswin@...ux.ibm.com>
To: linux-ext4@...r.kernel.org, "Theodore Ts'o" <tytso@....edu>
Cc: Ritesh Harjani <ritesh.list@...il.com>, Zhang Yi <yi.zhang@...wei.com>,
Jan Kara <jack@...e.cz>, libaokun1@...wei.com,
linux-kernel@...r.kernel.org
Subject: [PATCH 2/7] ext4: kunit tests for higher level extent manipulation functions
Add more kunit tests to cover all high level callers of
ext4_split_convert_extents(). The main functions we cover are:
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.
2. convert_initialized_extent() - Convert written extent to unwritten
during zero range
Signed-off-by: Ojaswin Mujoo <ojaswin@...ux.ibm.com>
---
fs/ext4/extents-test.c | 275 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 274 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c
index 937810a0f264..4fb94d3c8a1e 100644
--- a/fs/ext4/extents-test.c
+++ b/fs/ext4/extents-test.c
@@ -90,6 +90,9 @@ struct kunit_ext_test_param {
/* map describing range to split */
struct ext4_map_blocks split_map;
+ /* disable zeroout */
+ bool disable_zeroout;
+
/* no of extents expected after split */
int nr_exp_ext;
@@ -131,6 +134,9 @@ static struct file_system_type ext_fs_type = {
static void extents_kunit_exit(struct kunit *test)
{
+ struct ext4_sb_info *sbi = k_ctx.k_ei->vfs_inode.i_sb->s_fs_info;
+
+ kfree(sbi);
kfree(k_ctx.k_ei);
kfree(k_ctx.k_data);
}
@@ -220,6 +226,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 = NULL;
struct kunit_ext_test_param *param =
(struct kunit_ext_test_param *)(test->param_value);
@@ -237,7 +244,18 @@ static int extents_kunit_init(struct kunit *test)
sb->s_blocksize = 4096;
sb->s_blocksize_bits = 12;
- ei->i_disksize = (EX_DATA_LBLK + EX_DATA_LEN + 10) << sb->s_blocksize_bits;
+ sbi = kzalloc(sizeof(struct ext4_sb_info), GFP_KERNEL);
+ if (sbi == NULL)
+ return -ENOMEM;
+
+ sbi->s_sb = sb;
+ sb->s_fs_info = sbi;
+
+ if (!param || !param->disable_zeroout)
+ sbi->s_extent_max_zeroout_kb = 32;
+
+ ei->i_disksize = (EX_DATA_LBLK + EX_DATA_LEN + 10)
+ << sb->s_blocksize_bits;
inode->i_sb = sb;
k_ctx.k_data = kzalloc(EX_DATA_LEN * 4096, GFP_KERNEL);
@@ -279,6 +297,8 @@ static int extents_kunit_init(struct kunit *test)
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);
kunit_activate_static_stub(test, ext4_issue_zeroout,
ext4_issue_zeroout_stub);
return 0;
@@ -372,6 +392,150 @@ static void test_split_convert(struct kunit *test)
return;
}
+static void test_convert_initialized(struct kunit *test)
+{
+ struct ext4_ext_path *path;
+ struct inode *inode = &k_ctx.k_ei->vfs_inode;
+ struct ext4_extent *ex;
+ struct ext4_map_blocks map;
+ const struct kunit_ext_test_param *param =
+ (const struct kunit_ext_test_param *)(test->param_value);
+ int blkbits = inode->i_sb->s_blocksize_bits;
+ int allocated = 0;
+
+ 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 = ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0);
+ ex = path->p_ext;
+ KUNIT_EXPECT_EQ(test, 10, ex->ee_block);
+ KUNIT_EXPECT_EQ(test, 3, 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 = param->split_map.m_lblk;
+ map.m_len = param->split_map.m_len;
+ convert_initialized_extent(NULL, inode, &map, path, &allocated);
+
+ path = ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0);
+ ex = path->p_ext;
+
+ for (int i = 0; i < param->nr_exp_ext; i++) {
+ struct kunit_ext_state exp_ext = 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_MSG(
+ test, exp_ext.is_unwrit, ext4_ext_is_unwritten(ex),
+ "# exp: lblk:%d len:%d unwrit:%d, got: lblk:%d len:%d unwrit:%d\n",
+ exp_ext.ex_lblk, exp_ext.ex_len, exp_ext.is_unwrit,
+ ex->ee_block, ext4_ext_get_actual_len(ex), ext4_ext_is_unwritten(ex));
+
+ ex = ex + 1;
+ }
+
+ if (!param->is_zeroout_test)
+ return;
+
+ /*
+ * Check that then data area has been zeroed out correctly
+ */
+ for (int i = 0; i < param->nr_exp_data_segs; i++) {
+ loff_t off, len;
+ struct kunit_ext_data_state exp_data_seg = param->exp_data_state[i];
+
+ off = exp_data_seg.off_blk << blkbits;
+ len = 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 void test_handle_unwritten(struct kunit *test)
+{
+ struct ext4_ext_path *path;
+ struct inode *inode = &k_ctx.k_ei->vfs_inode;
+ struct ext4_extent *ex;
+ struct ext4_map_blocks map;
+ const struct kunit_ext_test_param *param =
+ (const struct kunit_ext_test_param *)(test->param_value);
+ int blkbits = inode->i_sb->s_blocksize_bits;
+ int allocated = 0;
+ ext4_fsblk_t dummy_pblk = 999;
+
+ 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 = ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0);
+ ex = path->p_ext;
+ KUNIT_EXPECT_EQ(test, 10, ex->ee_block);
+ KUNIT_EXPECT_EQ(test, 3, 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 = param->split_map.m_lblk;
+ map.m_len = param->split_map.m_len;
+ ext4_ext_handle_unwritten_extents(NULL, inode, &map, path, param->split_flags,
+ &allocated, dummy_pblk);
+
+ path = ext4_find_extent(inode, EX_DATA_LBLK, NULL, 0);
+ ex = path->p_ext;
+
+ for (int i = 0; i < param->nr_exp_ext; i++) {
+ struct kunit_ext_state exp_ext = 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_MSG(
+ test, exp_ext.is_unwrit, ext4_ext_is_unwritten(ex),
+ "# exp: lblk:%d len:%d unwrit:%d, got: lblk:%d len:%d unwrit:%d\n",
+ exp_ext.ex_lblk, exp_ext.ex_len, exp_ext.is_unwrit,
+ ex->ee_block, ext4_ext_get_actual_len(ex), ext4_ext_is_unwritten(ex));
+
+ ex = ex + 1;
+ }
+
+ if (!param->is_zeroout_test)
+ return;
+
+ /*
+ * Check that then data area has been zeroed out correctly
+ */
+ for (int i = 0; i < param->nr_exp_data_segs; i++) {
+ loff_t off, len;
+ struct kunit_ext_data_state exp_data_seg = param->exp_data_state[i];
+
+ off = exp_data_seg.off_blk << blkbits;
+ len = 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[] = {
/* unwrit to writ splits */
{ .desc = "split unwrit extent to 2 extents and convert 1st half writ",
@@ -523,6 +687,93 @@ static const struct kunit_ext_test_param test_split_convert_params[] = {
.exp_data_state = { { .exp_char = 0, .off_blk = 0, .len_blk = 3 } } },
};
+static const struct kunit_ext_test_param
+test_convert_initialized_params[] = {
+ /* writ to unwrit splits */
+ { .desc = "split writ extent to 2 extents and convert 1st half unwrit",
+ .is_unwrit_at_start = 0,
+ .split_map = { .m_lblk = 10, .m_len = 1 },
+ .nr_exp_ext = 2,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 0 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split writ extent to 2 extents and convert 2nd half unwrit",
+ .is_unwrit_at_start = 0,
+ .split_map = { .m_lblk = 11, .m_len = 2 },
+ .nr_exp_ext = 2,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 1 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split writ extent to 3 extents and convert 2nd half to unwrit",
+ .is_unwrit_at_start = 0,
+ .split_map = { .m_lblk = 11, .m_len = 1 },
+ .nr_exp_ext = 3,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 11, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 12, .ex_len = 1, .is_unwrit = 0 } },
+ .is_zeroout_test = 0 },
+};
+
+static const struct kunit_ext_test_param test_handle_unwritten_params[] = {
+ /* unwrit to writ splits via endio path */
+ { .desc = "split unwrit extent to 2 extents and convert 1st half writ (endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CONVERT,
+ .split_map = { .m_lblk = 10, .m_len = 1 },
+ .nr_exp_ext = 2,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 1 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split unwrit extent to 2 extents and convert 2nd half writ (endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CONVERT,
+ .split_map = { .m_lblk = 11, .m_len = 2 },
+ .nr_exp_ext = 2,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 0 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split unwrit extent to 3 extents and convert 2nd half to writ (endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CONVERT,
+ .split_map = { .m_lblk = 11, .m_len = 1 },
+ .nr_exp_ext = 3,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 11, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 12, .ex_len = 1, .is_unwrit = 1 } },
+ .is_zeroout_test = 0 },
+
+ /* unwrit to writ splits via non-endio path */
+ { .desc = "split unwrit extent to 2 extents and convert 1st half writ (non endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CREATE,
+ .split_map = { .m_lblk = 10, .m_len = 1 },
+ .nr_exp_ext = 2,
+ .disable_zeroout = true,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 1 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split unwrit extent to 2 extents and convert 2nd half writ (non endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CREATE,
+ .split_map = { .m_lblk = 11, .m_len = 2 },
+ .nr_exp_ext = 2,
+ .disable_zeroout = true,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 11, .ex_len = 2, .is_unwrit = 0 } },
+ .is_zeroout_test = 0 },
+ { .desc = "split unwrit extent to 3 extents and convert 2nd half to writ (non endio)",
+ .is_unwrit_at_start = 1,
+ .split_flags = EXT4_GET_BLOCKS_CREATE,
+ .split_map = { .m_lblk = 11, .m_len = 1 },
+ .nr_exp_ext = 3,
+ .disable_zeroout = true,
+ .exp_ext_state = { { .ex_lblk = 10, .ex_len = 1, .is_unwrit = 1 },
+ { .ex_lblk = 11, .ex_len = 1, .is_unwrit = 0 },
+ { .ex_lblk = 12, .ex_len = 1, .is_unwrit = 1 } },
+ .is_zeroout_test = 0 },
+
+};
+
static void ext_get_desc(struct kunit *test, const void *p, char *desc)
{
@@ -540,6 +791,24 @@ static int test_split_convert_param_init(struct kunit *test)
return 0;
}
+static int test_convert_initialized_param_init(struct kunit *test)
+{
+ size_t arr_size = 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 = 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 compact
* KUNIT_ARRAY_PARAM() because the later currently has a limitation causing the
@@ -550,6 +819,10 @@ static int test_split_convert_param_init(struct kunit *test)
static struct kunit_case extents_test_cases[] = {
KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params,
test_split_convert_param_init, NULL),
+ KUNIT_CASE_PARAM_WITH_INIT(test_convert_initialized, kunit_array_gen_params,
+ test_convert_initialized_param_init, NULL),
+ KUNIT_CASE_PARAM_WITH_INIT(test_handle_unwritten, kunit_array_gen_params,
+ test_handle_unwritten_init, NULL),
{}
};
--
2.51.0
Powered by blists - more mailing lists