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/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