Skip to content

Commit 7c9fa39

Browse files
Kemeng Shitytso
authored andcommitted
ext4: add first unit test for ext4_mb_new_blocks_simple in mballoc
Here are prepared work: 1. Include mballoc-test.c to mballoc.c to be able test static function in mballoc.c. 2. Implement static stub to avoid read IO to disk. 3. Construct fake super_block. Only partial members are set, more members will be set when more functions are tested. Then unit test for ext4_mb_new_blocks_simple is added. Signed-off-by: Kemeng Shi <[email protected]> Reviewed-by: "Ritesh Harjani (IBM)" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent bdefd68 commit 7c9fa39

File tree

2 files changed

+329
-0
lines changed

2 files changed

+329
-0
lines changed

fs/ext4/mballoc-test.c

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KUnit test of ext4 multiblocks allocation.
4+
*/
5+
6+
#include <kunit/test.h>
7+
#include <kunit/static_stub.h>
8+
9+
#include "ext4.h"
10+
11+
struct mbt_grp_ctx {
12+
struct buffer_head bitmap_bh;
13+
/* desc and gd_bh are just the place holders for now */
14+
struct ext4_group_desc desc;
15+
struct buffer_head gd_bh;
16+
};
17+
18+
struct mbt_ctx {
19+
struct mbt_grp_ctx *grp_ctx;
20+
};
21+
22+
struct mbt_ext4_super_block {
23+
struct super_block sb;
24+
struct mbt_ctx mbt_ctx;
25+
};
26+
27+
#define MBT_CTX(_sb) (&(container_of((_sb), struct mbt_ext4_super_block, sb)->mbt_ctx))
28+
#define MBT_GRP_CTX(_sb, _group) (&MBT_CTX(_sb)->grp_ctx[_group])
29+
30+
static struct super_block *mbt_ext4_alloc_super_block(void)
31+
{
32+
struct ext4_super_block *es = kzalloc(sizeof(*es), GFP_KERNEL);
33+
struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
34+
struct mbt_ext4_super_block *fsb = kzalloc(sizeof(*fsb), GFP_KERNEL);
35+
36+
if (fsb == NULL || sbi == NULL || es == NULL)
37+
goto out;
38+
39+
sbi->s_es = es;
40+
fsb->sb.s_fs_info = sbi;
41+
return &fsb->sb;
42+
43+
out:
44+
kfree(fsb);
45+
kfree(sbi);
46+
kfree(es);
47+
return NULL;
48+
}
49+
50+
static void mbt_ext4_free_super_block(struct super_block *sb)
51+
{
52+
struct mbt_ext4_super_block *fsb =
53+
container_of(sb, struct mbt_ext4_super_block, sb);
54+
struct ext4_sb_info *sbi = EXT4_SB(sb);
55+
56+
kfree(sbi->s_es);
57+
kfree(sbi);
58+
kfree(fsb);
59+
}
60+
61+
struct mbt_ext4_block_layout {
62+
unsigned char blocksize_bits;
63+
unsigned int cluster_bits;
64+
uint32_t blocks_per_group;
65+
ext4_group_t group_count;
66+
uint16_t desc_size;
67+
};
68+
69+
static void mbt_init_sb_layout(struct super_block *sb,
70+
struct mbt_ext4_block_layout *layout)
71+
{
72+
struct ext4_sb_info *sbi = EXT4_SB(sb);
73+
struct ext4_super_block *es = sbi->s_es;
74+
75+
sb->s_blocksize = 1UL << layout->blocksize_bits;
76+
sb->s_blocksize_bits = layout->blocksize_bits;
77+
78+
sbi->s_groups_count = layout->group_count;
79+
sbi->s_blocks_per_group = layout->blocks_per_group;
80+
sbi->s_cluster_bits = layout->cluster_bits;
81+
sbi->s_cluster_ratio = 1U << layout->cluster_bits;
82+
sbi->s_clusters_per_group = layout->blocks_per_group >>
83+
layout->cluster_bits;
84+
sbi->s_desc_size = layout->desc_size;
85+
86+
es->s_first_data_block = cpu_to_le32(0);
87+
es->s_blocks_count_lo = cpu_to_le32(layout->blocks_per_group *
88+
layout->group_count);
89+
}
90+
91+
static int mbt_grp_ctx_init(struct super_block *sb,
92+
struct mbt_grp_ctx *grp_ctx)
93+
{
94+
grp_ctx->bitmap_bh.b_data = kzalloc(EXT4_BLOCK_SIZE(sb), GFP_KERNEL);
95+
if (grp_ctx->bitmap_bh.b_data == NULL)
96+
return -ENOMEM;
97+
98+
return 0;
99+
}
100+
101+
static void mbt_grp_ctx_release(struct mbt_grp_ctx *grp_ctx)
102+
{
103+
kfree(grp_ctx->bitmap_bh.b_data);
104+
grp_ctx->bitmap_bh.b_data = NULL;
105+
}
106+
107+
static void mbt_ctx_mark_used(struct super_block *sb, ext4_group_t group,
108+
unsigned int start, unsigned int len)
109+
{
110+
struct mbt_grp_ctx *grp_ctx = MBT_GRP_CTX(sb, group);
111+
112+
mb_set_bits(grp_ctx->bitmap_bh.b_data, start, len);
113+
}
114+
115+
/* called after mbt_init_sb_layout */
116+
static int mbt_ctx_init(struct super_block *sb)
117+
{
118+
struct mbt_ctx *ctx = MBT_CTX(sb);
119+
ext4_group_t i, ngroups = ext4_get_groups_count(sb);
120+
121+
ctx->grp_ctx = kcalloc(ngroups, sizeof(struct mbt_grp_ctx),
122+
GFP_KERNEL);
123+
if (ctx->grp_ctx == NULL)
124+
return -ENOMEM;
125+
126+
for (i = 0; i < ngroups; i++)
127+
if (mbt_grp_ctx_init(sb, &ctx->grp_ctx[i]))
128+
goto out;
129+
130+
/*
131+
* first data block(first cluster in first group) is used by
132+
* metadata, mark it used to avoid to alloc data block at first
133+
* block which will fail ext4_sb_block_valid check.
134+
*/
135+
mb_set_bits(ctx->grp_ctx[0].bitmap_bh.b_data, 0, 1);
136+
137+
return 0;
138+
out:
139+
while (i-- > 0)
140+
mbt_grp_ctx_release(&ctx->grp_ctx[i]);
141+
kfree(ctx->grp_ctx);
142+
return -ENOMEM;
143+
}
144+
145+
static void mbt_ctx_release(struct super_block *sb)
146+
{
147+
struct mbt_ctx *ctx = MBT_CTX(sb);
148+
ext4_group_t i, ngroups = ext4_get_groups_count(sb);
149+
150+
for (i = 0; i < ngroups; i++)
151+
mbt_grp_ctx_release(&ctx->grp_ctx[i]);
152+
kfree(ctx->grp_ctx);
153+
}
154+
155+
static struct buffer_head *
156+
ext4_read_block_bitmap_nowait_stub(struct super_block *sb, ext4_group_t block_group,
157+
bool ignore_locked)
158+
{
159+
struct mbt_grp_ctx *grp_ctx = MBT_GRP_CTX(sb, block_group);
160+
161+
/* paired with brelse from caller of ext4_read_block_bitmap_nowait */
162+
get_bh(&grp_ctx->bitmap_bh);
163+
return &grp_ctx->bitmap_bh;
164+
}
165+
166+
static int ext4_wait_block_bitmap_stub(struct super_block *sb,
167+
ext4_group_t block_group,
168+
struct buffer_head *bh)
169+
{
170+
return 0;
171+
}
172+
173+
static struct ext4_group_desc *
174+
ext4_get_group_desc_stub(struct super_block *sb, ext4_group_t block_group,
175+
struct buffer_head **bh)
176+
{
177+
struct mbt_grp_ctx *grp_ctx = MBT_GRP_CTX(sb, block_group);
178+
179+
if (bh != NULL)
180+
*bh = &grp_ctx->gd_bh;
181+
182+
return &grp_ctx->desc;
183+
}
184+
185+
static int
186+
ext4_mb_mark_context_stub(handle_t *handle, struct super_block *sb, bool state,
187+
ext4_group_t group, ext4_grpblk_t blkoff,
188+
ext4_grpblk_t len, int flags,
189+
ext4_grpblk_t *ret_changed)
190+
{
191+
struct mbt_grp_ctx *grp_ctx = MBT_GRP_CTX(sb, group);
192+
struct buffer_head *bitmap_bh = &grp_ctx->bitmap_bh;
193+
194+
if (state)
195+
mb_set_bits(bitmap_bh->b_data, blkoff, len);
196+
else
197+
mb_clear_bits(bitmap_bh->b_data, blkoff, len);
198+
199+
return 0;
200+
}
201+
202+
#define TEST_BLOCKSIZE_BITS 10
203+
#define TEST_CLUSTER_BITS 3
204+
#define TEST_BLOCKS_PER_GROUP 8192
205+
#define TEST_GROUP_COUNT 4
206+
#define TEST_DESC_SIZE 64
207+
#define TEST_GOAL_GROUP 1
208+
static int mbt_kunit_init(struct kunit *test)
209+
{
210+
struct mbt_ext4_block_layout layout = {
211+
.blocksize_bits = TEST_BLOCKSIZE_BITS,
212+
.cluster_bits = TEST_CLUSTER_BITS,
213+
.blocks_per_group = TEST_BLOCKS_PER_GROUP,
214+
.group_count = TEST_GROUP_COUNT,
215+
.desc_size = TEST_DESC_SIZE,
216+
};
217+
struct super_block *sb;
218+
int ret;
219+
220+
sb = mbt_ext4_alloc_super_block();
221+
if (sb == NULL)
222+
return -ENOMEM;
223+
224+
mbt_init_sb_layout(sb, &layout);
225+
226+
ret = mbt_ctx_init(sb);
227+
if (ret != 0) {
228+
mbt_ext4_free_super_block(sb);
229+
return ret;
230+
}
231+
232+
test->priv = sb;
233+
kunit_activate_static_stub(test,
234+
ext4_read_block_bitmap_nowait,
235+
ext4_read_block_bitmap_nowait_stub);
236+
kunit_activate_static_stub(test,
237+
ext4_wait_block_bitmap,
238+
ext4_wait_block_bitmap_stub);
239+
kunit_activate_static_stub(test,
240+
ext4_get_group_desc,
241+
ext4_get_group_desc_stub);
242+
kunit_activate_static_stub(test,
243+
ext4_mb_mark_context,
244+
ext4_mb_mark_context_stub);
245+
return 0;
246+
}
247+
248+
static void mbt_kunit_exit(struct kunit *test)
249+
{
250+
struct super_block *sb = (struct super_block *)test->priv;
251+
252+
mbt_ctx_release(sb);
253+
mbt_ext4_free_super_block(sb);
254+
}
255+
256+
static void test_new_blocks_simple(struct kunit *test)
257+
{
258+
struct super_block *sb = (struct super_block *)test->priv;
259+
struct inode inode = { .i_sb = sb, };
260+
struct ext4_allocation_request ar;
261+
ext4_group_t i, goal_group = TEST_GOAL_GROUP;
262+
int err = 0;
263+
ext4_fsblk_t found;
264+
struct ext4_sb_info *sbi = EXT4_SB(sb);
265+
266+
ar.inode = &inode;
267+
268+
/* get block at goal */
269+
ar.goal = ext4_group_first_block_no(sb, goal_group);
270+
found = ext4_mb_new_blocks_simple(&ar, &err);
271+
KUNIT_ASSERT_EQ_MSG(test, ar.goal, found,
272+
"failed to alloc block at goal, expected %llu found %llu",
273+
ar.goal, found);
274+
275+
/* get block after goal in goal group */
276+
ar.goal = ext4_group_first_block_no(sb, goal_group);
277+
found = ext4_mb_new_blocks_simple(&ar, &err);
278+
KUNIT_ASSERT_EQ_MSG(test, ar.goal + EXT4_C2B(sbi, 1), found,
279+
"failed to alloc block after goal in goal group, expected %llu found %llu",
280+
ar.goal + 1, found);
281+
282+
/* get block after goal group */
283+
mbt_ctx_mark_used(sb, goal_group, 0, EXT4_CLUSTERS_PER_GROUP(sb));
284+
ar.goal = ext4_group_first_block_no(sb, goal_group);
285+
found = ext4_mb_new_blocks_simple(&ar, &err);
286+
KUNIT_ASSERT_EQ_MSG(test,
287+
ext4_group_first_block_no(sb, goal_group + 1), found,
288+
"failed to alloc block after goal group, expected %llu found %llu",
289+
ext4_group_first_block_no(sb, goal_group + 1), found);
290+
291+
/* get block before goal group */
292+
for (i = goal_group; i < ext4_get_groups_count(sb); i++)
293+
mbt_ctx_mark_used(sb, i, 0, EXT4_CLUSTERS_PER_GROUP(sb));
294+
ar.goal = ext4_group_first_block_no(sb, goal_group);
295+
found = ext4_mb_new_blocks_simple(&ar, &err);
296+
KUNIT_ASSERT_EQ_MSG(test,
297+
ext4_group_first_block_no(sb, 0) + EXT4_C2B(sbi, 1), found,
298+
"failed to alloc block before goal group, expected %llu found %llu",
299+
ext4_group_first_block_no(sb, 0 + EXT4_C2B(sbi, 1)), found);
300+
301+
/* no block available, fail to allocate block */
302+
for (i = 0; i < ext4_get_groups_count(sb); i++)
303+
mbt_ctx_mark_used(sb, i, 0, EXT4_CLUSTERS_PER_GROUP(sb));
304+
ar.goal = ext4_group_first_block_no(sb, goal_group);
305+
found = ext4_mb_new_blocks_simple(&ar, &err);
306+
KUNIT_ASSERT_NE_MSG(test, err, 0,
307+
"unexpectedly get block when no block is available");
308+
}
309+
310+
311+
static struct kunit_case mbt_test_cases[] = {
312+
KUNIT_CASE(test_new_blocks_simple),
313+
{}
314+
};
315+
316+
static struct kunit_suite mbt_test_suite = {
317+
.name = "ext4_mballoc_test",
318+
.init = mbt_kunit_init,
319+
.exit = mbt_kunit_exit,
320+
.test_cases = mbt_test_cases,
321+
};
322+
323+
kunit_test_suites(&mbt_test_suite);
324+
325+
MODULE_LICENSE("GPL");

fs/ext4/mballoc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6991,3 +6991,7 @@ ext4_mballoc_query_range(
69916991

69926992
return error;
69936993
}
6994+
6995+
#ifdef CONFIG_EXT4_KUNIT_TESTS
6996+
#include "mballoc-test.c"
6997+
#endif

0 commit comments

Comments
 (0)