Skip to content

Commit 5e70030

Browse files
Badari PulavartyLinus Torvalds
authored andcommitted
ext4: statfs speed up
This is a patch that speeds up statfs. It is very simple - the "overhead" calculation, which takes a huge amount of time for large filesystems, never changes unless the size of the filesystem itself changes. That means we can store it in memory and only recalculate if the filesystem has been resized (almost never). It also fixes a minor problem that we never update the on-disk superblock free blocks/inodes counts until the filesystem is unmounted. While not fatal, we may as well update that on disk when we have the information, and it makes things like debugfs and dumpe2fs report a bit more accurate info. Signed-off-by: Badari Pulavarty <pbadari@us.ibm.com> Signed-off-by: Andreas Dilger <adilger@clusterfs.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a71ce8c commit 5e70030

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

‎fs/ext4/super.c‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,19 +2500,19 @@ static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
25002500
struct super_block *sb = dentry->d_sb;
25012501
struct ext4_sb_info *sbi = EXT4_SB(sb);
25022502
struct ext4_super_block *es = sbi->s_es;
2503-
ext4_fsblk_t overhead;
2504-
int i;
25052503
u64 fsid;
25062504

2507-
if (test_opt (sb, MINIX_DF))
2508-
overhead = 0;
2509-
else {
2510-
unsigned long ngroups;
2511-
ngroups = EXT4_SB(sb)->s_groups_count;
2505+
if (test_opt(sb, MINIX_DF)) {
2506+
sbi->s_overhead_last = 0;
2507+
} else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2508+
unsigned long ngroups = sbi->s_groups_count, i;
2509+
ext4_fsblk_t overhead = 0;
25122510
smp_rmb();
25132511

25142512
/*
2515-
* Compute the overhead (FS structures)
2513+
* Compute the overhead (FS structures). This is constant
2514+
* for a given filesystem unless the number of block groups
2515+
* changes so we cache the previous value until it does.
25162516
*/
25172517

25182518
/*
@@ -2536,18 +2536,23 @@ static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
25362536
* Every block group has an inode bitmap, a block
25372537
* bitmap, and an inode table.
25382538
*/
2539-
overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
2539+
overhead += ngroups * (2 + sbi->s_itb_per_group);
2540+
sbi->s_overhead_last = overhead;
2541+
smp_wmb();
2542+
sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
25402543
}
25412544

25422545
buf->f_type = EXT4_SUPER_MAGIC;
25432546
buf->f_bsize = sb->s_blocksize;
2544-
buf->f_blocks = ext4_blocks_count(es) - overhead;
2547+
buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
25452548
buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
2549+
es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
25462550
buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
25472551
if (buf->f_bfree < ext4_r_blocks_count(es))
25482552
buf->f_bavail = 0;
25492553
buf->f_files = le32_to_cpu(es->s_inodes_count);
25502554
buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
2555+
es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
25512556
buf->f_namelen = EXT4_NAME_LEN;
25522557
fsid = le64_to_cpup((void *)es->s_uuid) ^
25532558
le64_to_cpup((void *)es->s_uuid + sizeof(u64));

‎include/linux/ext4_fs_sb.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct ext4_sb_info {
3939
unsigned long s_gdb_count; /* Number of group descriptor blocks */
4040
unsigned long s_desc_per_block; /* Number of group descriptors per block */
4141
unsigned long s_groups_count; /* Number of groups in the fs */
42+
unsigned long s_overhead_last; /* Last calculated overhead */
43+
unsigned long s_blocks_last; /* Last seen block count */
4244
struct buffer_head * s_sbh; /* Buffer containing the super block */
4345
struct ext4_super_block * s_es; /* Pointer to the super block in the buffer */
4446
struct buffer_head ** s_group_desc;

0 commit comments

Comments
 (0)