initialize.c (ext2fs_initialize): If the user specifies a really
	large number of inodes, then reduce the number of blocks
	per group until we find a workable set of filesystem
	parameters.

ext2_err.et.in (EXT2_ET_TOO_MANY_INODES): Add new error code.

diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog
index 930badd..749bafb 100644
--- a/lib/ext2fs/ChangeLog
+++ b/lib/ext2fs/ChangeLog
@@ -1,3 +1,12 @@
+2003-01-19  Theodore Ts'o  <tytso@mit.edu>
+
+	* initialize.c (ext2fs_initialize): If the user specifies a really
+		large number of inodes, then reduce the number of blocks
+		per group until we find a workable set of filesystem
+		parameters.
+
+	* ext2_err.et.in (EXT2_ET_TOO_MANY_INODES): Add new error code.
+
 2002-11-09  Theodore Ts'o  <tytso@mit.edu>
 
 	* Release of E2fsprogs 1.32
diff --git a/lib/ext2fs/ext2_err.et.in b/lib/ext2fs/ext2_err.et.in
index b08f863..b9ad5f0 100644
--- a/lib/ext2fs/ext2_err.et.in
+++ b/lib/ext2fs/ext2_err.et.in
@@ -281,5 +281,8 @@
 ec	EXT2_ET_BAD_EA_BLOCK_NUM,
 	"Illegal extended attribute block number"
 
+ec	EXT2_ET_TOO_MANY_INODES,
+	"Cannot create filesystem with requested number of inodes"
+
 	end
 
diff --git a/lib/ext2fs/initialize.c b/lib/ext2fs/initialize.c
index f5552e6..32b9c87 100644
--- a/lib/ext2fs/initialize.c
+++ b/lib/ext2fs/initialize.c
@@ -189,8 +189,17 @@
 	 */
 	ipg = (super->s_inodes_count + fs->group_desc_count - 1) /
 		fs->group_desc_count;
-	if (ipg > fs->blocksize * 8)
-		ipg = fs->blocksize * 8;
+	if (ipg > fs->blocksize * 8) {
+		if (super->s_blocks_per_group >= 256) {
+			/* Try again with slightly different parameters */
+			super->s_blocks_per_group -= 8;
+			super->s_blocks_count = param->s_blocks_count;
+			super->s_frags_per_group = super->s_blocks_per_group *
+				frags_per_block;
+			goto retry;
+		} else
+			return EXT2_ET_TOO_MANY_INODES;
+	}
 
 	if (ipg > EXT2_MAX_INODES_PER_GROUP(super))
 		ipg = EXT2_MAX_INODES_PER_GROUP(super);
@@ -239,7 +248,11 @@
 	 * being clever is tricky...
 	 */
 	overhead = (int) (3 + fs->desc_blocks + fs->inode_blocks_per_group);
-	
+
+	/* This can only happen if the user requested too many inodes */
+	if (overhead > super->s_blocks_per_group)
+		return EXT2_ET_TOO_MANY_INODES;
+
 	/*
 	 * See if the last group is big enough to support the
 	 * necessary data structures.  If not, we need to get rid of