Skip to contentSkip to conversation

mke2fs: set s_overhead_blocks

RFC PATCH New Eric Sandeen 1 participant 1 message Feb 11, 2013

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 mke2fs: set s_overhead_blocks

The recent thread on the list about strange df behavior made me go look at how we were calculating s_overhead_blocks again, and I realized that mkfs still doesn't set it. It seems like a simple thing to do; just look at how many blocks are in use when mkfs is done.

---

However, I realize that resize2fs breaks this. And by the time we have a populated fs, with bigalloc/meta/flex/whatnot, the calculation of overhead gets very tricky again. (at least for shrinking; growing is probably easier)

Ted, did you have a better patch for this? You added the superblock field so maybe you had something in mind to keep it up to date as the filesystem size changes?

misc/mke2fs.c +34 -0
index bbf477a..3413bb2 100644
@@ -2346,6 +2346,38 @@ static int create_quota_inodes(ext2_filsys fs)
 	return 0;
 }
 
+/*
+ * Set the filesystem's metadata overhead - the number of blocks
+ * used for all mkfs-time metadata.  We could carefully account
+ * for all blocks, or do it the easy way:  See how many blocks
+ * have been used by mkfs, at the end of mkfs.  Add the space used
+ * by the root dir and lost+found back in.
+ */
+void set_fs_overhead(ext2_filsys fs) {
+	unsigned int		overhead;
+	struct ext2_inode       inode;
+	ext2_ino_t		inum;
+	blk64_t			iblocks;
+
+	/* overhead is everything mkfs has used ... */
+	overhead = ext2fs_blocks_count(fs->super) -
+		ext2fs_free_blocks_count(fs->super);
+
+	/* ... not counting / and /lost+found */
+	if (!ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode)) {
+		iblocks = ext2fs_inode_i_blocks(fs, &inode);
+		overhead -= (iblocks << 9) / fs->blocksize;
+	}
+
+	if (!ext2fs_lookup(fs, EXT2_ROOT_INO, "lost+found", 10, 0, &inum) &&
+	    !ext2fs_read_inode(fs, inum, &inode)) {
+		iblocks = ext2fs_inode_i_blocks(fs, &inode);
+		overhead -= (iblocks << 9) / fs->blocksize;
+	}
+
+	fs->super->s_overhead_blocks = overhead;
+}
+
 int main (int argc, char *argv[])
 {
 	errcode_t	retval = 0;
@@ -2733,6 +2764,8 @@ no_journal:
 				       EXT4_FEATURE_RO_COMPAT_QUOTA))
 		create_quota_inodes(fs);
 
+	set_fs_overhead(fs);
+
 	if (!quiet)
 		printf(_("Writing superblocks and "
 		       "filesystem accounting information: "));