changeset 81:f95f6fd94eed

Move superblock initialization to a function. Trim unused padding from superblock.
author Rob Landley <rob@landley.net>
date Sat, 27 Jan 2007 20:43:26 -0500
parents 7a058d60faf1
children 612c8d6c2829
files toys/e2fs.h toys/mke2fs.c
diffstat 2 files changed, 35 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/toys/e2fs.h	Sat Jan 27 15:10:48 2007 -0500
+++ b/toys/e2fs.h	Sat Jan 27 20:43:26 2007 -0500
@@ -61,7 +61,7 @@
  	uint32_t first_meta_bg;      // First metablock block group
 	uint32_t mkfs_time;          // Creation timestamp
 	uint32_t jnl_blocks[17];     // Backup of journal inode
-	uint32_t reserved[172];      // Padding to the end of the block
+	// uint32_t reserved[172];      // Padding to the end of the block
 };
 
 struct ext2_group
--- a/toys/mke2fs.c	Sat Jan 27 15:10:48 2007 -0500
+++ b/toys/mke2fs.c	Sat Jan 27 20:43:26 2007 -0500
@@ -54,27 +54,11 @@
 	uuid[11] = uuid[11] | 128;
 }
 
-int mke2fs_main(void)
-{
-	struct ext2_superblock *sb = xzalloc(sizeof(struct ext2_superblock));
-	uint32_t temp;
-	off_t length;
-
-	// Handle command line arguments.
+// Fill out superblock and TT
 
-	if (toys.optargs[1]) {
-		sscanf(toys.optargs[1], "%u", &TT.blocks);
-		temp = O_RDWR|O_CREAT;
-	} else temp = O_RDWR;
-
-	// TODO: Check if filesystem is mounted here
-
-	// For mke?fs, open file.  For gene?fs, create file.
-	length = fdlength(TT.fsfd = xcreate(*toys.optargs, temp, 0777));
-
-	// TODO: collect gene2fs list, calculate requirements.
-
-	// Fill out superblock structure
+void init_superblock(struct ext2_superblock *sb, off_t length)
+{
+	uint32_t temp;
 
 	// Determine appropriate block size, set log_block_size and log_frag_size.
 
@@ -92,7 +76,7 @@
 	temp = (TT.blocks * (uint64_t)TT.reserved_percent) /100;
 	sb->r_blocks_count = SWAP_LE32(temp);
 
-	sb->first_data_block = TT.blocksize == 1024 ? 1 : 0;
+	sb->first_data_block = TT.blocksize == SWAP_LE32(1024 ? 1 : 0);
 
 	// Set blocks_per_group and frags_per_group, which is the size of an
 	// allocation bitmap that fits in one block (I.E. how many bits per block)?
@@ -134,7 +118,7 @@
 	sb->state = sb->errors = SWAP_LE16(1);
 
 	sb->rev_level = SWAP_LE32(1);
-	sb->inode_size = sizeof(struct ext2_inode);
+	sb->inode_size = SWAP_LE16(sizeof(struct ext2_inode));
 	sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE);
 	sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER);
 
@@ -143,14 +127,38 @@
 	// If we're called as mke3fs or mkfs.ext3, do a journal.
 
 	//if (strchr(toys.which->name,'3'))
-	//	sb->feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
+	//	sb->feature_compat = SWAP_LE32(EXT3_FEATURE_COMPAT_HAS_JOURNAL);
+}
+
+int mke2fs_main(void)
+{
+	struct ext2_superblock *sb = xzalloc(sizeof(struct ext2_superblock));
+	int temp;
+
+	// Handle command line arguments.
+
+	if (toys.optargs[1]) {
+		sscanf(toys.optargs[1], "%u", &TT.blocks);
+		temp = O_RDWR|O_CREAT;
+	} else temp = O_RDWR;
+
+	// TODO: collect gene2fs list, calculate requirements.
+
+	// TODO: Check if filesystem is mounted here
+
+	// For mke?fs, open file.  For gene?fs, create file.
+	TT.fsfd = xcreate(*toys.optargs, temp, 0777);
 
 	// We skip the first 1k (to avoid the boot sector, if any).  Use this to
 	// figure out if this file is seekable.
-	if(-1 == lseek(TT.fsfd, 1024, SEEK_SET)) perror_exit("lseek");
-	//{ TT.noseek=1; xwrite(TT.fsfd, sb, 1024); }
+	if(-1 == lseek(TT.fsfd, 1024, SEEK_SET)) {
+		TT.noseek=1;
+		xwrite(TT.fsfd, sb, 1024);
+	}
+	
+	// Create and write first superblock.
 
-	// Write superblock to disk.	
+	init_superblock(sb, fdlength(TT.fsfd));
 	xwrite(TT.fsfd, sb, sizeof(struct ext2_superblock)); // 4096-1024
 
 	return 0;