--- busybox.orig/util-linux/mkfs_ext2.c	Mon Oct 19 11:41:13 2009
+++ busybox/util-linux/mkfs_ext2.c	Mon Oct 19 11:47:32 2009
@@ -181,7 +181,7 @@
 	uint32_t bytes_per_inode;
 	uint32_t first_block;
 	uint32_t inodes_per_group;
-	uint32_t gdtsz, itsz;
+	uint32_t gdtsz, itsz, landfsz;
 	time_t timestamp;
 	unsigned opts;
 	const char *label = "";
@@ -326,10 +326,17 @@
 		inodes_per_group &= ~7;
 		itsz = div_roundup(inodes_per_group * sizeof(*inode), blocksize);
 
+		// 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));
+
 		// the last group 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_block) % blocks_per_group;
-		if ((1 == ngroups) && remainder && (remainder < overhead))
+		if ((1 == ngroups) && remainder && (remainder < overhead + 1/* "/" */ + landfsz/* "/lost+found" */))
 			bb_error_msg_and_die("way small device");
 		// Standard mke2fs uses 50. Looks like a bug in our calculation
 		// of "remainder" or "overhead" - we don't match standard mke2fs
@@ -450,7 +457,7 @@
 	// write the label
 	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;
@@ -459,34 +466,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;
 		}
 
+		// 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" blocks
 			overhead,
-			blocks_per_group - (fb + overhead)
+			// mark unused trailing 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 reserved inodes
 			inodes_per_group - gd[i].bg_free_inodes_count,
+			// mark unused trailing inodes
 			blocks_per_group - inodes_per_group
 		);
 		// dump inode bitmap
@@ -496,7 +514,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);
 
@@ -506,8 +524,8 @@
 		// dump superblock and group descriptors and their backups
 		if (has_super(i)) {
 			// N.B. 1024 byte blocks are special
-			PUT(((uint64_t)pos * blocksize) + ((0 == i && 0 == first_block) ? 1024 : 0), sb, 1024);//blocksize);
-			PUT(((uint64_t)pos * blocksize) + blocksize, gd, gdtsz * blocksize);
+			PUT(((uint64_t)pos << blocksize_log2) + ((0 == i && 0 == first_block) ? 1024 : 0), sb, 1024);
+			PUT(((uint64_t)pos << blocksize_log2) + blocksize, gd, gdtsz * blocksize);
 		}
 	}
 
@@ -527,21 +545,29 @@
 	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
 	STORE_LE(inode->i_block[0], FETCH_LE32(gd[0].bg_inode_table) + itsz);
-	PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) * blocksize) + (EXT2_ROOT_INO-1) * sizeof(*inode), buf, sizeof(*inode));
+	PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) << blocksize_log2) + (EXT2_ROOT_INO-1) * sizeof(*inode), buf, sizeof(*inode));
 
 	// 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
-	PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) * blocksize) + (EXT2_GOOD_OLD_FIRST_INO-1) * sizeof(*inode), buf, sizeof(*inode));
+	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);
