Skip to content

Commit 117fff1

Browse files
committed
ext4: grow the s_flex_groups array as needed when resizing
Previously, we allocated the s_flex_groups array to the maximum size that the file system could be resized. There was two problems with this approach. First, it wasted memory in the common case where the file system was not resized. Secondly, once we start allowing online resizing using the meta_bg scheme, there is no maximum size that the file system can be resized. So instead, we need to grow the s_flex_groups at inline resize time. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 2ebd170 commit 117fff1

3 files changed

Lines changed: 47 additions & 18 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ struct ext4_sb_info {
12761276

12771277
unsigned int s_log_groups_per_flex;
12781278
struct flex_groups *s_flex_groups;
1279+
ext4_group_t s_flex_groups_allocated;
12791280

12801281
/* workqueue for dio unwritten */
12811282
struct workqueue_struct *dio_unwritten_wq;
@@ -2055,6 +2056,8 @@ extern void ext4_superblock_csum_set(struct super_block *sb,
20552056
extern void *ext4_kvmalloc(size_t size, gfp_t flags);
20562057
extern void *ext4_kvzalloc(size_t size, gfp_t flags);
20572058
extern void ext4_kvfree(void *ptr);
2059+
extern int ext4_alloc_flex_bg_array(struct super_block *sb,
2060+
ext4_group_t ngroup);
20582061
extern __printf(4, 5)
20592062
void __ext4_error(struct super_block *, const char *, unsigned int,
20602063
const char *, ...);

‎fs/ext4/resize.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,10 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
15031503
if (err)
15041504
goto out;
15051505

1506+
err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1507+
if (err)
1508+
return err;
1509+
15061510
flex_gd.count = 1;
15071511
flex_gd.groups = input;
15081512
flex_gd.bg_flags = &bg_flags;
@@ -1662,7 +1666,7 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
16621666
unsigned long n_desc_blocks;
16631667
unsigned long o_desc_blocks;
16641668
unsigned long desc_blocks;
1665-
int err = 0, flexbg_size = 1;
1669+
int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
16661670

16671671
o_blocks_count = ext4_blocks_count(es);
16681672

@@ -1721,13 +1725,13 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
17211725
goto out;
17221726
}
17231727

1724-
if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
1725-
es->s_log_groups_per_flex)
1726-
flexbg_size = 1 << es->s_log_groups_per_flex;
1727-
17281728
if (ext4_blocks_count(es) == n_blocks_count)
17291729
goto out;
17301730

1731+
err = ext4_alloc_flex_bg_array(sb, n_group + 1);
1732+
if (err)
1733+
return err;
1734+
17311735
flex_gd = alloc_flex_gd(flexbg_size);
17321736
if (flex_gd == NULL) {
17331737
err = -ENOMEM;

‎fs/ext4/super.c‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,15 +1925,45 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
19251925
return res;
19261926
}
19271927

1928+
int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1929+
{
1930+
struct ext4_sb_info *sbi = EXT4_SB(sb);
1931+
struct flex_groups *new_groups;
1932+
int size;
1933+
1934+
if (!sbi->s_log_groups_per_flex)
1935+
return 0;
1936+
1937+
size = ext4_flex_group(sbi, ngroup - 1) + 1;
1938+
if (size <= sbi->s_flex_groups_allocated)
1939+
return 0;
1940+
1941+
size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1942+
new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1943+
if (!new_groups) {
1944+
ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1945+
size / (int) sizeof(struct flex_groups));
1946+
return -ENOMEM;
1947+
}
1948+
1949+
if (sbi->s_flex_groups) {
1950+
memcpy(new_groups, sbi->s_flex_groups,
1951+
(sbi->s_flex_groups_allocated *
1952+
sizeof(struct flex_groups)));
1953+
ext4_kvfree(sbi->s_flex_groups);
1954+
}
1955+
sbi->s_flex_groups = new_groups;
1956+
sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1957+
return 0;
1958+
}
1959+
19281960
static int ext4_fill_flex_info(struct super_block *sb)
19291961
{
19301962
struct ext4_sb_info *sbi = EXT4_SB(sb);
19311963
struct ext4_group_desc *gdp = NULL;
1932-
ext4_group_t flex_group_count;
19331964
ext4_group_t flex_group;
19341965
unsigned int groups_per_flex = 0;
1935-
size_t size;
1936-
int i;
1966+
int i, err;
19371967

19381968
sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
19391969
if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
@@ -1942,17 +1972,9 @@ static int ext4_fill_flex_info(struct super_block *sb)
19421972
}
19431973
groups_per_flex = 1 << sbi->s_log_groups_per_flex;
19441974

1945-
/* We allocate both existing and potentially added groups */
1946-
flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1947-
((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1948-
EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1949-
size = flex_group_count * sizeof(struct flex_groups);
1950-
sbi->s_flex_groups = ext4_kvzalloc(size, GFP_KERNEL);
1951-
if (sbi->s_flex_groups == NULL) {
1952-
ext4_msg(sb, KERN_ERR, "not enough memory for %u flex groups",
1953-
flex_group_count);
1975+
err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1976+
if (err)
19541977
goto failed;
1955-
}
19561978

19571979
for (i = 0; i < sbi->s_groups_count; i++) {
19581980
gdp = ext4_get_group_desc(sb, i, NULL);

0 commit comments

Comments
 (0)