Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 10/23] ext4: fix inode table accounting in count_overhead()
Date: Wed, 11 Nov 2026 23:46:59 -0500	[thread overview]
Message-ID: <20261112044712.1577296-11-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>

count_overhead() computes the per-group overhead of bigalloc file
systems which do not have s_overhead_clusters set in the superblock.
It only counts a group's inode table if the whole table lies inside
the block group being examined, using

	b >= first_block && b + sbi->s_itb_per_group <= last_block

where last_block is the (inclusive) last block of the group.  This
has two problems:

 - The end check is off by one: an inode table occupying the last
   s_itb_per_group blocks of the group is not counted at all.

 - With flex_bg, mke2fs and online resize pack the inode tables of a
   flex group back to back, so an inode table can straddle the
   boundary between two block groups.  Such a table is not entirely
   contained in either group and so is not counted in either.

In both cases the overhead is underestimated, so statfs(2) reports
f_blocks (and thus the file system size and used space shown by df)
as larger than it should be.

Fix this by counting the intersection of the inode table with the
block group, which handles both cases.

Fixes: 952fc18ef9ec ("ext4: fix overhead calculation used by ext4_statfs()")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/super-test.c | 25 +++++++++++++++++++++++++
 fs/ext4/super.c      | 15 +++++++++++++--
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/super-test.c b/fs/ext4/super-test.c
index abd844c8c29f..72115c141986 100644
--- a/fs/ext4/super-test.c
+++ b/fs/ext4/super-test.c
@@ -1077,6 +1077,31 @@ static void test_overhead_and_statfs(struct kunit *test)
 	/* Cluster 0 holds super+gdb+bbitmap+ibitmap; clusters 1..2 hold itb */
 	KUNIT_EXPECT_EQ(test, ext4_count_overhead_test(sb, 0, buf), 3);
 
+	/* Inode table ending on the group's last block: clusters 254..255 */
+	ext4_inode_table_set(sb, &ctx->gdps[0], SUPER_TEST_BLOCKS_PER_GRP - 8);
+	memset(buf, 0, SUPER_TEST_BLK_SIZE);
+	KUNIT_EXPECT_EQ(test, ext4_count_overhead_test(sb, 0, buf), 3);
+
+	/*
+	 * flex_bg style layout where group 1's inode table (2044..2051)
+	 * straddles the boundary between group 1 (1024..2047) and group 2
+	 * (2048..3071); each group must count its own part of it.
+	 */
+	sbi->s_groups_count = 3;
+	ext4_inode_table_set(sb, &ctx->gdps[0], 4);
+	ext4_block_bitmap_set(sb, &ctx->gdps[1], 1028);
+	ext4_inode_bitmap_set(sb, &ctx->gdps[1], 1029);
+	ext4_inode_table_set(sb, &ctx->gdps[1], 2044);
+	ext4_block_bitmap_set(sb, &ctx->gdps[2], 2056);
+	ext4_inode_bitmap_set(sb, &ctx->gdps[2], 2057);
+	ext4_inode_table_set(sb, &ctx->gdps[2], 2060);
+	/* Group 1: super+gdb, bitmaps, and the head of its inode table */
+	memset(buf, 0, SUPER_TEST_BLK_SIZE);
+	KUNIT_EXPECT_EQ(test, ext4_count_overhead_test(sb, 1, buf), 3);
+	/* Group 2: tail of group 1's inode table, bitmaps, own inode table */
+	memset(buf, 0, SUPER_TEST_BLK_SIZE);
+	KUNIT_EXPECT_EQ(test, ext4_count_overhead_test(sb, 2, buf), 4);
+
 	/* Restore 1:1 geometry and verify ext4_statfs accounting & clamping */
 	ext4_clear_feature_bigalloc(sb);
 	ctx->es.s_first_data_block = cpu_to_le32(SUPER_TEST_FIRST_DATA_BLK);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index e89f37419266..00d430e6e794 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4266,12 +4266,23 @@ static int count_overhead(struct super_block *sb, ext4_group_t grp,
 			count++;
 		}
 		b = ext4_inode_table(sb, gdp);
-		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
-			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
+		/*
+		 * The inode table is s_itb_per_group blocks long and, with
+		 * flex_bg, may straddle a block group boundary.  Only count
+		 * the part of it which lies inside this block group.
+		 */
+		if (b <= last_block &&
+		    b + sbi->s_itb_per_group - 1 >= first_block) {
+			ext4_fsblk_t start = max(b, first_block);
+			ext4_fsblk_t end = min(b + sbi->s_itb_per_group - 1,
+					       last_block);
+
+			for (b = start; b <= end; b++) {
 				int c = EXT4_B2C(sbi, b - first_block);
 				ext4_set_bit(c, buf);
 				count++;
 			}
+		}
 		if (i != grp)
 			continue;
 		s = 0;
-- 
2.56.0.542.g3ef89b7020-goog


  parent reply	other threads:[~2026-11-12  4:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12  4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12  4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12  4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12  4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12  4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12  4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12  4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12  4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12  4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12  4:46 ` Theodore Ts'o [this message]
2026-11-12  4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12  4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12  4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12  4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12  4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12  4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12  4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12  4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112044712.1577296-11-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox