@@ -3085,6 +3085,114 @@ static int set_journal_csum_feature_set(struct super_block *sb)
30853085 return ret ;
30863086}
30873087
3088+ /*
3089+ * Note: calculating the overhead so we can be compatible with
3090+ * historical BSD practice is quite difficult in the face of
3091+ * clusters/bigalloc. This is because multiple metadata blocks from
3092+ * different block group can end up in the same allocation cluster.
3093+ * Calculating the exact overhead in the face of clustered allocation
3094+ * requires either O(all block bitmaps) in memory or O(number of block
3095+ * groups**2) in time. We will still calculate the superblock for
3096+ * older file systems --- and if we come across with a bigalloc file
3097+ * system with zero in s_overhead_clusters the estimate will be close to
3098+ * correct especially for very large cluster sizes --- but for newer
3099+ * file systems, it's better to calculate this figure once at mkfs
3100+ * time, and store it in the superblock. If the superblock value is
3101+ * present (even for non-bigalloc file systems), we will use it.
3102+ */
3103+ static int count_overhead (struct super_block * sb , ext4_group_t grp ,
3104+ char * buf )
3105+ {
3106+ struct ext4_sb_info * sbi = EXT4_SB (sb );
3107+ struct ext4_group_desc * gdp ;
3108+ ext4_fsblk_t first_block , last_block , b ;
3109+ ext4_group_t i , ngroups = ext4_get_groups_count (sb );
3110+ int s , j , count = 0 ;
3111+
3112+ first_block = le32_to_cpu (sbi -> s_es -> s_first_data_block ) +
3113+ (grp * EXT4_BLOCKS_PER_GROUP (sb ));
3114+ last_block = first_block + EXT4_BLOCKS_PER_GROUP (sb ) - 1 ;
3115+ for (i = 0 ; i < ngroups ; i ++ ) {
3116+ gdp = ext4_get_group_desc (sb , i , NULL );
3117+ b = ext4_block_bitmap (sb , gdp );
3118+ if (b >= first_block && b <= last_block ) {
3119+ ext4_set_bit (EXT4_B2C (sbi , b - first_block ), buf );
3120+ count ++ ;
3121+ }
3122+ b = ext4_inode_bitmap (sb , gdp );
3123+ if (b >= first_block && b <= last_block ) {
3124+ ext4_set_bit (EXT4_B2C (sbi , b - first_block ), buf );
3125+ count ++ ;
3126+ }
3127+ b = ext4_inode_table (sb , gdp );
3128+ if (b >= first_block && b + sbi -> s_itb_per_group <= last_block )
3129+ for (j = 0 ; j < sbi -> s_itb_per_group ; j ++ , b ++ ) {
3130+ int c = EXT4_B2C (sbi , b - first_block );
3131+ ext4_set_bit (c , buf );
3132+ count ++ ;
3133+ }
3134+ if (i != grp )
3135+ continue ;
3136+ s = 0 ;
3137+ if (ext4_bg_has_super (sb , grp )) {
3138+ ext4_set_bit (s ++ , buf );
3139+ count ++ ;
3140+ }
3141+ for (j = ext4_bg_num_gdb (sb , grp ); j > 0 ; j -- ) {
3142+ ext4_set_bit (EXT4_B2C (sbi , s ++ ), buf );
3143+ count ++ ;
3144+ }
3145+ }
3146+ if (!count )
3147+ return 0 ;
3148+ return EXT4_CLUSTERS_PER_GROUP (sb ) -
3149+ ext4_count_free (buf , EXT4_CLUSTERS_PER_GROUP (sb ) / 8 );
3150+ }
3151+
3152+ /*
3153+ * Compute the overhead and stash it in sbi->s_overhead
3154+ */
3155+ int ext4_calculate_overhead (struct super_block * sb )
3156+ {
3157+ struct ext4_sb_info * sbi = EXT4_SB (sb );
3158+ struct ext4_super_block * es = sbi -> s_es ;
3159+ ext4_group_t i , ngroups = ext4_get_groups_count (sb );
3160+ ext4_fsblk_t overhead = 0 ;
3161+ char * buf = (char * ) get_zeroed_page (GFP_KERNEL );
3162+
3163+ memset (buf , 0 , PAGE_SIZE );
3164+ if (!buf )
3165+ return - ENOMEM ;
3166+
3167+ /*
3168+ * Compute the overhead (FS structures). This is constant
3169+ * for a given filesystem unless the number of block groups
3170+ * changes so we cache the previous value until it does.
3171+ */
3172+
3173+ /*
3174+ * All of the blocks before first_data_block are overhead
3175+ */
3176+ overhead = EXT4_B2C (sbi , le32_to_cpu (es -> s_first_data_block ));
3177+
3178+ /*
3179+ * Add the overhead found in each block group
3180+ */
3181+ for (i = 0 ; i < ngroups ; i ++ ) {
3182+ int blks ;
3183+
3184+ blks = count_overhead (sb , i , buf );
3185+ overhead += blks ;
3186+ if (blks )
3187+ memset (buf , 0 , PAGE_SIZE );
3188+ cond_resched ();
3189+ }
3190+ sbi -> s_overhead = overhead ;
3191+ smp_wmb ();
3192+ free_page ((unsigned long ) buf );
3193+ return 0 ;
3194+ }
3195+
30883196static int ext4_fill_super (struct super_block * sb , void * data , int silent )
30893197{
30903198 char * orig_data = kstrdup (data , GFP_KERNEL );
@@ -3734,6 +3842,18 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
37343842 percpu_counter_set (& sbi -> s_dirtyclusters_counter , 0 );
37353843
37363844no_journal :
3845+ /*
3846+ * Get the # of file system overhead blocks from the
3847+ * superblock if present.
3848+ */
3849+ if (es -> s_overhead_clusters )
3850+ sbi -> s_overhead = le32_to_cpu (es -> s_overhead_clusters );
3851+ else {
3852+ ret = ext4_calculate_overhead (sb );
3853+ if (ret )
3854+ goto failed_mount_wq ;
3855+ }
3856+
37373857 /*
37383858 * The maximum number of concurrent works can be high and
37393859 * concurrency isn't really necessary. Limit it to 1.
@@ -4600,67 +4720,21 @@ static int ext4_remount(struct super_block *sb, int *flags, char *data)
46004720 return err ;
46014721}
46024722
4603- /*
4604- * Note: calculating the overhead so we can be compatible with
4605- * historical BSD practice is quite difficult in the face of
4606- * clusters/bigalloc. This is because multiple metadata blocks from
4607- * different block group can end up in the same allocation cluster.
4608- * Calculating the exact overhead in the face of clustered allocation
4609- * requires either O(all block bitmaps) in memory or O(number of block
4610- * groups**2) in time. We will still calculate the superblock for
4611- * older file systems --- and if we come across with a bigalloc file
4612- * system with zero in s_overhead_clusters the estimate will be close to
4613- * correct especially for very large cluster sizes --- but for newer
4614- * file systems, it's better to calculate this figure once at mkfs
4615- * time, and store it in the superblock. If the superblock value is
4616- * present (even for non-bigalloc file systems), we will use it.
4617- */
46184723static int ext4_statfs (struct dentry * dentry , struct kstatfs * buf )
46194724{
46204725 struct super_block * sb = dentry -> d_sb ;
46214726 struct ext4_sb_info * sbi = EXT4_SB (sb );
46224727 struct ext4_super_block * es = sbi -> s_es ;
4623- struct ext4_group_desc * gdp ;
4728+ ext4_fsblk_t overhead = 0 ;
46244729 u64 fsid ;
46254730 s64 bfree ;
46264731
4627- if (test_opt (sb , MINIX_DF )) {
4628- sbi -> s_overhead_last = 0 ;
4629- } else if (es -> s_overhead_clusters ) {
4630- sbi -> s_overhead_last = le32_to_cpu (es -> s_overhead_clusters );
4631- } else if (sbi -> s_blocks_last != ext4_blocks_count (es )) {
4632- ext4_group_t i , ngroups = ext4_get_groups_count (sb );
4633- ext4_fsblk_t overhead = 0 ;
4634-
4635- /*
4636- * Compute the overhead (FS structures). This is constant
4637- * for a given filesystem unless the number of block groups
4638- * changes so we cache the previous value until it does.
4639- */
4640-
4641- /*
4642- * All of the blocks before first_data_block are
4643- * overhead
4644- */
4645- overhead = EXT4_B2C (sbi , le32_to_cpu (es -> s_first_data_block ));
4646-
4647- /*
4648- * Add the overhead found in each block group
4649- */
4650- for (i = 0 ; i < ngroups ; i ++ ) {
4651- gdp = ext4_get_group_desc (sb , i , NULL );
4652- overhead += ext4_num_overhead_clusters (sb , i , gdp );
4653- cond_resched ();
4654- }
4655- sbi -> s_overhead_last = overhead ;
4656- smp_wmb ();
4657- sbi -> s_blocks_last = ext4_blocks_count (es );
4658- }
4732+ if (!test_opt (sb , MINIX_DF ))
4733+ overhead = sbi -> s_overhead ;
46594734
46604735 buf -> f_type = EXT4_SUPER_MAGIC ;
46614736 buf -> f_bsize = sb -> s_blocksize ;
4662- buf -> f_blocks = (ext4_blocks_count (es ) -
4663- EXT4_C2B (sbi , sbi -> s_overhead_last ));
4737+ buf -> f_blocks = ext4_blocks_count (es ) - EXT4_C2B (sbi , sbi -> s_overhead );
46644738 bfree = percpu_counter_sum_positive (& sbi -> s_freeclusters_counter ) -
46654739 percpu_counter_sum_positive (& sbi -> s_dirtyclusters_counter );
46664740 /* prevent underflow in case that few free space is available */
0 commit comments