Skip to content

Commit

Permalink
ext4: use atomic64_t for the per-flexbg free_clusters count
Browse files Browse the repository at this point in the history
commit 90ba983 upstream.

A user who was using a 8TB+ file system and with a very large flexbg
size (> 65536) could cause the atomic_t used in the struct flex_groups
to overflow.  This was detected by PaX security patchset:

http://forums.grsecurity.net/viewtopic.php?f=3&t=3289&p=12551#p12551

This bug was introduced in commit 9f24e42, so it's been around
since 2.6.30.  :-(

Fix this by using an atomic64_t for struct orlav_stats's
free_clusters.

[Backported for 3.0-stable. Renamed free_clusters back to free_blocks;
fixed a few more atomic_read's of free_blocks left in 3.0.]

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Lingzhu Xiang <lxiang@redhat.com>
Reviewed-by: CAI Qian <caiqian@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Theodore Ts'o authored and Greg Kroah-Hartman committed Apr 5, 2013
1 parent 7fb54ba commit 503f4bd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions fs/ext4/ext4.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ struct ext4_group_desc
*/

struct flex_groups {
atomic_t free_inodes;
atomic_t free_blocks;
atomic_t used_dirs;
atomic64_t free_blocks;
atomic_t free_inodes;
atomic_t used_dirs;
};

#define EXT4_BG_INODE_UNINIT 0x0001 /* Inode table/bitmap not in use */
Expand Down
18 changes: 9 additions & 9 deletions fs/ext4/ialloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ static int find_group_flex(struct super_block *sb, struct inode *parent,
ext4_group_t ngroups = ext4_get_groups_count(sb);
int flex_size = ext4_flex_bg_size(sbi);
ext4_group_t best_flex = parent_fbg_group;
int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
int flexbg_free_blocks;
ext4_fsblk_t blocks_per_flex = sbi->s_blocks_per_group * flex_size;
ext4_fsblk_t flexbg_free_blocks;
int flex_freeb_ratio;
ext4_group_t n_fbg_groups;
ext4_group_t i;
Expand All @@ -355,7 +355,7 @@ static int find_group_flex(struct super_block *sb, struct inode *parent,
sbi->s_log_groups_per_flex;

find_close_to_parent:
flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
flexbg_free_blocks = atomic64_read(&flex_group[best_flex].free_blocks);
flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
if (atomic_read(&flex_group[best_flex].free_inodes) &&
flex_freeb_ratio > free_block_ratio)
Expand All @@ -370,7 +370,7 @@ static int find_group_flex(struct super_block *sb, struct inode *parent,
if (i == parent_fbg_group || i == parent_fbg_group - 1)
continue;

flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
flexbg_free_blocks = atomic64_read(&flex_group[i].free_blocks);
flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;

if (flex_freeb_ratio > free_block_ratio &&
Expand All @@ -380,14 +380,14 @@ static int find_group_flex(struct super_block *sb, struct inode *parent,
}

if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
((atomic_read(&flex_group[i].free_blocks) >
atomic_read(&flex_group[best_flex].free_blocks)) &&
((atomic64_read(&flex_group[i].free_blocks) >
atomic64_read(&flex_group[best_flex].free_blocks)) &&
atomic_read(&flex_group[i].free_inodes)))
best_flex = i;
}

if (!atomic_read(&flex_group[best_flex].free_inodes) ||
!atomic_read(&flex_group[best_flex].free_blocks))
!atomic64_read(&flex_group[best_flex].free_blocks))
return -1;

found_flexbg:
Expand All @@ -406,8 +406,8 @@ static int find_group_flex(struct super_block *sb, struct inode *parent,
}

struct orlov_stats {
__u64 free_blocks;
__u32 free_inodes;
__u32 free_blocks;
__u32 used_dirs;
};

Expand All @@ -424,7 +424,7 @@ static void get_orlov_stats(struct super_block *sb, ext4_group_t g,

if (flex_size > 1) {
stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
stats->free_blocks = atomic64_read(&flex_group[g].free_blocks);
stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
return;
}
Expand Down
10 changes: 5 additions & 5 deletions fs/ext4/mballoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2814,8 +2814,8 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
if (sbi->s_log_groups_per_flex) {
ext4_group_t flex_group = ext4_flex_group(sbi,
ac->ac_b_ex.fe_group);
atomic_sub(ac->ac_b_ex.fe_len,
&sbi->s_flex_groups[flex_group].free_blocks);
atomic64_sub(ac->ac_b_ex.fe_len,
&sbi->s_flex_groups[flex_group].free_blocks);
}

err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Expand Down Expand Up @@ -4614,7 +4614,7 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,

if (sbi->s_log_groups_per_flex) {
ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
atomic64_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
}

ext4_mb_unload_buddy(&e4b);
Expand Down Expand Up @@ -4745,8 +4745,8 @@ void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,

if (sbi->s_log_groups_per_flex) {
ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
atomic_add(blocks_freed,
&sbi->s_flex_groups[flex_group].free_blocks);
atomic64_add(blocks_freed,
&sbi->s_flex_groups[flex_group].free_blocks);
}

ext4_mb_unload_buddy(&e4b);
Expand Down
4 changes: 2 additions & 2 deletions fs/ext4/resize.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,8 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
sbi->s_log_groups_per_flex) {
ext4_group_t flex_group;
flex_group = ext4_flex_group(sbi, input->group);
atomic_add(input->free_blocks_count,
&sbi->s_flex_groups[flex_group].free_blocks);
atomic64_add(input->free_blocks_count,
&sbi->s_flex_groups[flex_group].free_blocks);
atomic_add(EXT4_INODES_PER_GROUP(sb),
&sbi->s_flex_groups[flex_group].free_inodes);
}
Expand Down
4 changes: 2 additions & 2 deletions fs/ext4/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -1992,8 +1992,8 @@ static int ext4_fill_flex_info(struct super_block *sb)
flex_group = ext4_flex_group(sbi, i);
atomic_add(ext4_free_inodes_count(sb, gdp),
&sbi->s_flex_groups[flex_group].free_inodes);
atomic_add(ext4_free_blks_count(sb, gdp),
&sbi->s_flex_groups[flex_group].free_blocks);
atomic64_add(ext4_free_blks_count(sb, gdp),
&sbi->s_flex_groups[flex_group].free_blocks);
atomic_add(ext4_used_dirs_count(sb, gdp),
&sbi->s_flex_groups[flex_group].used_dirs);
}
Expand Down

0 comments on commit 503f4bd

Please sign in to comment.