--- busybox.orig/util-linux/mkfs_ext2.c	Mon Oct 19 03:30:50 2009
+++ busybox/util-linux/mkfs_ext2.c	Mon Oct 19 10:38:46 2009
@@ -180,7 +180,7 @@
 	uint32_t bytes_per_inode;
 	uint32_t first_data_block;
 	uint32_t inodes_per_group;
-	uint32_t gdtsz, itsz;
+	uint32_t gdtsz, itsz, landfsz;
 	time_t timestamp;
 	unsigned opts;
 	const char *label = "";
@@ -299,7 +299,8 @@
 	{
 		// N.B. e2fsprogs does as follows!
 		// ninodes is the total number of inodes (files) in the file system
-		uint32_t ninodes = nblocks_full / (blocksize >= 4096 ? 1 : 4096 / blocksize);
+		//uint32_t ninodes = nblocks_full / (blocksize >= 4096 ? 1 : 4096 / blocksize);
+		uint32_t ninodes = ((uint64_t) nblocks_full * blocksize) / bytes_per_inode;
 		uint32_t overhead, remainder;
 		if (ninodes < EXT2_GOOD_OLD_FIRST_INO+1)
 			ninodes = EXT2_GOOD_OLD_FIRST_INO+1;
@@ -317,16 +318,23 @@
 		inodes_per_group &= ~7;
 		itsz = div_roundup(inodes_per_group * sizeof(*inode), blocksize);
 
-		// the last block needs more attention: doesn't it too small for possible overhead?
+		// the last block needs more attention: isn't it too small for possible overhead?
 		overhead = (has_super(ngroups - 1) ? (1/*sb*/ + gdtsz) : 0) + 1/*bbmp*/ + 1/*ibmp*/ + itsz;
 		remainder = (nblocks - first_data_block) % blocks_per_group;
 		if ((1 == ngroups) && remainder && (remainder < overhead))
 			bb_error_msg_and_die("way small device");
-		if (remainder && (remainder < overhead + 50)) {
+		if (remainder && (remainder < overhead + 50/* e2fsprogs hardcoded */)) {
 //bb_info_msg("CHOP[%u]", remainder);
 			nblocks -= remainder;
 			goto retry;
 		}
+
+		// to be useful, lost+found should occupy at least 2 blocks (but not exceeding 16*1024 bytes),
+		// and at most EXT2_NDIR_BLOCKS. So reserve these blocks right now
+		/* Or e2fsprogs comment verbatim (what does it mean?):
+		 * Ensure that lost+found is at least 2 blocks, so we always
+		 * test large empty blocks for big-block filesystems. */
+		landfsz = MIN(EXT2_NDIR_BLOCKS, 16 >> (blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE));
 	}
 
 	// print info
@@ -439,7 +447,7 @@
 	if (label) //opts & OPT_L)
 		safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
 
-	// fill group descriptors
+	// calculate filesystem skeleton structures
 	gd = xzalloc(gdtsz * blocksize);
 	buf = xmalloc(blocksize);
 	sb->s_free_blocks_count = 0;
@@ -448,39 +456,45 @@
 		i++, pos += blocks_per_group, n -= blocks_per_group
 	) {
 		uint32_t overhead = pos + (has_super(i) ? (1/*sb*/ + gdtsz) : 0);
-		uint32_t fb;
+		uint32_t free_blocks;
+		// fill group descriptors
 		STORE_LE(gd[i].bg_block_bitmap, overhead + 0);
 		STORE_LE(gd[i].bg_inode_bitmap, overhead + 1);
 		STORE_LE(gd[i].bg_inode_table, overhead + 2);
 		overhead = overhead - pos + 1/*bbmp*/ + 1/*ibmp*/ + itsz;
 		gd[i].bg_free_inodes_count = inodes_per_group;
 		//STORE_LE(gd[i].bg_used_dirs_count, 0);
-		// N.B. both root and lost+found dirs are within the first block group, thus +2
+		// N.B. both "/" and "/lost+found" are within the first block group
+		// "/" occupies 1 block, "/lost+found" occupies landfsz blocks ...
 		if (0 == i) {
-			overhead += 2;
+			// ... thus increased overhead for the first block group ...
+			overhead += 1 + landfsz;
+			// ... and 2 used directories
 			STORE_LE(gd[i].bg_used_dirs_count, 2);
+			// well known reserved inodes belong to the first block too
 			gd[i].bg_free_inodes_count -= EXT2_GOOD_OLD_FIRST_INO;
 		}
-//		// N.B. the following is pure heuristics!
-//		// Likely to cope with 1024-byte blocks, when first block is for boot sectors
-//		if (ngroups-1 == i) {
-//			n -= first_data_block;
-//		}
 
+		// cache free block count of the group
+		free_blocks = (n < blocks_per_group ? n : blocks_per_group) - overhead;
+
 		// mark preallocated blocks as allocated
-		fb = (n < blocks_per_group ? n : blocks_per_group) - overhead;
 //bb_info_msg("ALLOC: [%u][%u][%u]", blocksize, overhead, blocks_per_group - (fb + overhead));
 		allocate(buf, blocksize,
+			// reserve overhead starting blocks
 			overhead,
-			blocks_per_group - (fb + overhead)
+			// mark unused ending blocks
+			blocks_per_group - (free_blocks + overhead)
 		);
 		// dump block bitmap
 		PUT((uint64_t)(FETCH_LE32(gd[i].bg_block_bitmap)) * blocksize, buf, blocksize);
-		STORE_LE(gd[i].bg_free_blocks_count, fb);
+		STORE_LE(gd[i].bg_free_blocks_count, free_blocks);
 
 		// mark preallocated inodes as allocated
 		allocate(buf, blocksize,
+			// mark used starting inodes
 			inodes_per_group - gd[i].bg_free_inodes_count,
+			// mark unused ending inodes
 			blocks_per_group - inodes_per_group
 		);
 		// dump inode bitmap
@@ -490,7 +504,7 @@
 		STORE_LE(gd[i].bg_free_inodes_count, gd[i].bg_free_inodes_count);
 
 		// count overall free blocks
-		sb->s_free_blocks_count += fb;
+		sb->s_free_blocks_count += free_blocks;
 	}
 	STORE_LE(sb->s_free_blocks_count, sb->s_free_blocks_count);
 
@@ -521,7 +535,7 @@
 	STORE_LE(inode->i_ctime, timestamp);
 	STORE_LE(inode->i_size, blocksize);
 	// N.B. inode->i_blocks stores the number of 512 byte data blocks. Why on Earth?!
-	STORE_LE(inode->i_blocks, blocksize / 512);
+	STORE_LE(inode->i_blocks, blocksize >> 9);
 
 	// dump root dir inode
 	STORE_LE(inode->i_links_count, 3); // "/.", "/..", "/lost+found/.." point to this inode
@@ -530,12 +544,20 @@
 
 	// dump lost+found dir inode
 	STORE_LE(inode->i_links_count, 2); // both "/lost+found" and "/lost+found/." point to this inode
-	STORE_LE(inode->i_block[0], inode->i_block[0]+1); // use next block
+	STORE_LE(inode->i_size, landfsz << blocksize_log2);
+	STORE_LE(inode->i_blocks, landfsz << (blocksize_log2 - 9));
+	for (i = 0, n = FETCH_LE32(inode->i_block[0])+1; i < landfsz; ++i)
+		STORE_LE(inode->i_block[i], i+n); // use next block
+//bb_info_msg("LAST BLOCK USED[%u]", i+n);
 	PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) << blocksize_log2) + (EXT2_GOOD_OLD_FIRST_INO-1) * sizeof(*inode), buf, sizeof(*inode));
 
 	// dump directories
 	memset(buf, 0, blocksize);
 	dir = (struct ext2_dir *)buf;
+
+	// zero the blocks for "/lost+found"
+	for (i = 1; i < landfsz; ++i)
+		PUT((uint64_t)(FETCH_LE32(gd[0].bg_inode_table) + itsz + 1+i) * blocksize, buf, blocksize);
 
 	// dump lost+found dir block
 	STORE_LE(dir->inode1, EXT2_GOOD_OLD_FIRST_INO);
--- busybox.orig/util-linux/mkfs_ext2_test.sh	Mon Oct 19 03:30:50 2009
+++ busybox/util-linux/mkfs_ext2_test.sh	Mon Oct 19 06:03:27 2009
@@ -1,4 +1,5 @@
 #!/bin/sh
+exec >1 2>&1
 
 run_test() { # params: mke2fs_invocation image_name
     >$2
@@ -23,8 +24,8 @@
 test_mke2fs() {
     echo Testing $kilobytes
 
-    run_test '/usr/bin/mke2fs' image_std || return 1
-    run_test './busybox mke2fs' image_bb || return 1
+    run_test '/usr/bin/mke2fs.0' image_std || return 1
+    run_test 'build/busybox mke2fs' image_bb || return 1
 
     diff -ua image_bb.out image_std.out >image.out.diff || {
 	cat image.out.diff
