From 3ac8d261fd430c45f4827570cb5146336cbc656a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 28 Jan 2007 04:54:01 -0500 Subject: More work on mke2fs. --- toys/mke2fs.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 15 deletions(-) (limited to 'toys/mke2fs.c') diff --git a/toys/mke2fs.c b/toys/mke2fs.c index 7d7e5ac7..6b13dd01 100644 --- a/toys/mke2fs.c +++ b/toys/mke2fs.c @@ -37,7 +37,7 @@ // On the other hand, we have 128 bits to come up with a unique identifier, of // which 6 have a defined value. /dev/urandom it is. -void create_uuid(char *uuid) +static void create_uuid(char *uuid) { // Read 128 random bytes int fd = xopen("/dev/urandom", O_RDONLY); @@ -56,20 +56,18 @@ void create_uuid(char *uuid) // Fill out superblock and TT -void init_superblock(struct ext2_superblock *sb, off_t length) +static void init_superblock(struct ext2_superblock *sb) { uint32_t temp; - // Determine appropriate block size, set log_block_size and log_frag_size. + // Set log_block_size and log_frag_size. - if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096; - for (temp = 0; temp < 7; temp++) if (TT.blocksize == 1024<log_block_size = sb->log_frag_size = SWAP_LE32(temp); // Fill out blocks_count, r_blocks_count, first_data_block - if (!TT.blocks) TT.blocks = length/TT.blocksize; sb->blocks_count = SWAP_LE32(TT.blocks); if (!TT.reserved_percent) TT.reserved_percent = 5; @@ -130,10 +128,30 @@ void init_superblock(struct ext2_superblock *sb, off_t length) // sb->feature_compat = SWAP_LE32(EXT3_FEATURE_COMPAT_HAS_JOURNAL); } +// Number of blocks used in this group by superblock/group list backup. +// Returns 0 if this group doesn't have a superblock backup. +static int group_superblock_used(uint32_t group) +{ + int used ; + + if (0) return 0; // todo, which groups have no superblock? + + // How blocks does the group table take up? + used = TT.groups * sizeof(struct ext2_group); + used += TT.blocksize - 1; + used /= TT.blocksize; + // Plus the superblock itself. + used++; + // And a corner case. + if (!group && TT.blocksize == 1024) used++; + + return used; +} + int mke2fs_main(void) { - struct ext2_superblock *sb = xzalloc(sizeof(struct ext2_superblock)); - int temp; + int i, temp; + off_t length; // Handle command line arguments. @@ -149,17 +167,77 @@ int mke2fs_main(void) // 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 + // Determine appropriate block size and block count from file length. + + length = fdlength(TT.fsfd); + if (length<1) error_exit("gene2fs is a todo item"); + if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096; + if (!TT.blocks) TT.blocks = length/TT.blocksize; + + // 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)) { TT.noseek=1; - xwrite(TT.fsfd, sb, 1024); + xwrite(TT.fsfd, &TT.sb, 1024); } - // Create and write first superblock. - - init_superblock(sb, fdlength(TT.fsfd)); - xwrite(TT.fsfd, sb, sizeof(struct ext2_superblock)); // 4096-1024 + // Initialize superblock structure + + init_superblock(&TT.sb); + + // Loop through block groups. + + for (i=0; i 1024) temp -= 1024; + memset(toybuf, 0, TT.blocksize); + xwrite(TT.fsfd, toybuf, temp); + + // Loop through groups to write group descriptor table. + for(j=0; j