Skip to content

Commit bd86298

Browse files
Lukas Czernertytso
authored andcommitted
ext4: introduce ext4_get_group_number()
Currently on many places in ext4 we're using ext4_get_group_no_and_offset() even though we're only interested in knowing the block group of the particular block, not the offset within the block group so we can use more efficient way to compute block group. This patch introduces ext4_get_group_number() which computes block group for a given block much more efficiently. Use this function instead of ext4_get_group_no_and_offset() everywhere where we're only interested in knowing the block group. Signed-off-by: Lukas Czerner <[email protected]> Signed-off-by: "Theodore Ts'o" <[email protected]>
1 parent 6891100 commit bd86298

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

fs/ext4/balloc.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
2929
* balloc.c contains the blocks allocation and deallocation routines
3030
*/
3131

32+
/*
33+
* Calculate block group number for a given block number
34+
*/
35+
ext4_group_t ext4_get_group_number(struct super_block *sb,
36+
ext4_fsblk_t block)
37+
{
38+
ext4_group_t group;
39+
40+
if (test_opt2(sb, STD_GROUP_SIZE))
41+
group = (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
42+
block) >>
43+
(EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
44+
else
45+
ext4_get_group_no_and_offset(sb, block, &group, NULL);
46+
return group;
47+
}
48+
3249
/*
3350
* Calculate the block group number and offset into the block/cluster
3451
* allocation bitmap, given a block number
@@ -59,13 +76,7 @@ static inline int ext4_block_in_group(struct super_block *sb,
5976
{
6077
ext4_group_t actual_group;
6178

62-
if (test_opt2(sb, STD_GROUP_SIZE))
63-
actual_group =
64-
(le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
65-
block) >>
66-
(EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
67-
else
68-
ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
79+
actual_group = ext4_get_group_number(sb, block);
6980
return (actual_group == block_group) ? 1 : 0;
7081
}
7182

fs/ext4/ext4.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,9 +1793,6 @@ ext4_group_first_block_no(struct super_block *sb, ext4_group_t group_no)
17931793
*/
17941794
#define ERR_BAD_DX_DIR -75000
17951795

1796-
void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
1797-
ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp);
1798-
17991796
/*
18001797
* Timeout and state flag for lazy initialization inode thread.
18011798
*/
@@ -1917,6 +1914,13 @@ int ext4_block_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
19171914
struct buffer_head *bh);
19181915

19191916
/* balloc.c */
1917+
extern void ext4_get_group_no_and_offset(struct super_block *sb,
1918+
ext4_fsblk_t blocknr,
1919+
ext4_group_t *blockgrpp,
1920+
ext4_grpblk_t *offsetp);
1921+
extern ext4_group_t ext4_get_group_number(struct super_block *sb,
1922+
ext4_fsblk_t block);
1923+
19201924
extern void ext4_validate_block_bitmap(struct super_block *sb,
19211925
struct ext4_group_desc *desc,
19221926
unsigned int block_group,

fs/ext4/mballoc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,7 +3344,7 @@ static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
33443344
if (pa->pa_type == MB_GROUP_PA)
33453345
grp_blk--;
33463346

3347-
ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
3347+
grp = ext4_get_group_number(sb, grp_blk);
33483348

33493349
/*
33503350
* possible race:
@@ -3809,7 +3809,7 @@ void ext4_discard_preallocations(struct inode *inode)
38093809

38103810
list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
38113811
BUG_ON(pa->pa_type != MB_INODE_PA);
3812-
ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3812+
group = ext4_get_group_number(sb, pa->pa_pstart);
38133813

38143814
err = ext4_mb_load_buddy(sb, group, &e4b);
38153815
if (err) {
@@ -4071,7 +4071,7 @@ ext4_mb_discard_lg_preallocations(struct super_block *sb,
40714071

40724072
list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
40734073

4074-
ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4074+
group = ext4_get_group_number(sb, pa->pa_pstart);
40754075
if (ext4_mb_load_buddy(sb, group, &e4b)) {
40764076
ext4_error(sb, "Error loading buddy information for %u",
40774077
group);

fs/ext4/resize.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int ext4_alloc_group_tables(struct super_block *sb,
272272
if (start_blk >= last_blk)
273273
goto next_group;
274274
group_data[bb_index].block_bitmap = start_blk++;
275-
ext4_get_group_no_and_offset(sb, start_blk - 1, &group, NULL);
275+
group = ext4_get_group_number(sb, start_blk - 1);
276276
group -= group_data[0].group;
277277
group_data[group].free_blocks_count--;
278278
if (flexbg_size > 1)
@@ -284,7 +284,7 @@ static int ext4_alloc_group_tables(struct super_block *sb,
284284
if (start_blk >= last_blk)
285285
goto next_group;
286286
group_data[ib_index].inode_bitmap = start_blk++;
287-
ext4_get_group_no_and_offset(sb, start_blk - 1, &group, NULL);
287+
group = ext4_get_group_number(sb, start_blk - 1);
288288
group -= group_data[0].group;
289289
group_data[group].free_blocks_count--;
290290
if (flexbg_size > 1)
@@ -296,7 +296,7 @@ static int ext4_alloc_group_tables(struct super_block *sb,
296296
if (start_blk + EXT4_SB(sb)->s_itb_per_group > last_blk)
297297
goto next_group;
298298
group_data[it_index].inode_table = start_blk;
299-
ext4_get_group_no_and_offset(sb, start_blk, &group, NULL);
299+
group = ext4_get_group_number(sb, start_blk - 1);
300300
group -= group_data[0].group;
301301
group_data[group].free_blocks_count -=
302302
EXT4_SB(sb)->s_itb_per_group;
@@ -392,7 +392,7 @@ static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
392392
ext4_group_t group;
393393
int err;
394394

395-
ext4_get_group_no_and_offset(sb, block, &group, NULL);
395+
group = ext4_get_group_number(sb, block);
396396
start = ext4_group_first_block_no(sb, group);
397397
group -= flex_gd->groups[0].group;
398398

@@ -1879,7 +1879,7 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
18791879
/* Nothing need to do */
18801880
return 0;
18811881

1882-
ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &offset);
1882+
n_group = ext4_get_group_number(sb, n_blocks_count - 1);
18831883
ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
18841884

18851885
n_desc_blocks = num_desc_blocks(sb, n_group + 1);

0 commit comments

Comments
 (0)