comparison toys/mke2fs.c @ 76:e6332139adae

More work on mke2fs.
author Rob Landley <rob@landley.net>
date Tue, 23 Jan 2007 19:54:01 -0500
parents 89ca591a9236
children 1aff4181427b
comparison
equal deleted inserted replaced
75:89ca591a9236 76:e6332139adae
4 * 4 *
5 * Copyright 2006 Rob Landley <rob@landley.net> 5 * Copyright 2006 Rob Landley <rob@landley.net>
6 */ 6 */
7 7
8 #include "toys.h" 8 #include "toys.h"
9
10 #define TT toy.mke2fs
9 11
10 // b - block size (1024, 2048, 4096) 12 // b - block size (1024, 2048, 4096)
11 // F - force (run on mounted device or non-block device) 13 // F - force (run on mounted device or non-block device)
12 // i - bytes per inode 14 // i - bytes per inode
13 // N - number of inodes 15 // N - number of inodes
52 uuid[11] = uuid[11] | 128; 54 uuid[11] = uuid[11] | 128;
53 } 55 }
54 56
55 int mke2fs_main(void) 57 int mke2fs_main(void)
56 { 58 {
57 struct ext2_super_block *sb = xzalloc(sizeof(struct ext2_super_block)); 59 struct ext2_superblock *sb = xzalloc(sizeof(struct ext2_superblock));
58 int temp; 60 uint32_t temp;
59 off_t length; 61 off_t length;
60 62
61 // Handle command line arguments. 63 // Handle command line arguments.
62 64
63 if (toys.optargs[1]) { 65 if (toys.optargs[1]) {
64 sscanf(toys.optargs[1], "%u", &(sb->inodes_count)); 66 sscanf(toys.optargs[1], "%u", &TT.blocks);
65 temp = O_RDWR|O_CREAT; 67 temp = O_RDWR|O_CREAT;
66 } else temp = O_RDWR; 68 } else temp = O_RDWR;
67 69
68 // Check if filesystem is mounted 70 // TODO: Check if filesystem is mounted here
69 71
70 // For mke?fs, open file. For gene?fs, create file. 72 // For mke?fs, open file. For gene?fs, create file.
71 length = fdlength(toy.mke2fs.fsfd = xcreate(*toys.optargs, temp, 0777)); 73 length = fdlength(TT.fsfd = xcreate(*toys.optargs, temp, 0777));
72 74
73 if (toy.mke2fs.blocksize && toy.mke2fs.blocksize!=1024 75 // TODO: collect gene2fs list, calculate requirements.
74 && toy.mke2fs.blocksize!=2048 && toy.mke2fs.blocksize!=4096)
75 error_exit("bad blocksize");
76
77 // Determine block size. If unspecified, use simple heuristic.
78 if (toy.mke2fs.blocksize)
79 sb->log_block_size = (length && length < 1<<24) ? 1024 : 4096;
80 else sb->log_block_size = toy.mke2fs.blocksize;
81
82 if (!sb->inodes_count) sb->inodes_count = length/toy.mke2fs.blocksize;
83 76
84 // Fill out superblock structure 77 // Fill out superblock structure
85 78
79 // Determine appropriate block size, set log_block_size and log_frag_size.
80
81 if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096;
82 if (TT.blocksize == 1024) temp = 0;
83 else if (TT.blocksize == 2048) temp = 1;
84 else if (TT.blocksize == 4096) temp = 2;
85 else error_exit("bad blocksize");
86 sb->log_block_size = sb->log_frag_size = SWAP_LE32(temp);
87
88 // Fill out blocks_count, inodes_count, r_blocks_count
89
90 if (!TT.blocks) TT.blocks = length/TT.blocksize;
91 sb->blocks_count = SWAP_LE32(TT.blocks);
92
93 if (!TT.inodes) {
94 if (!TT.bytes_per_inode) TT.bytes_per_inode = 8192;
95 TT.inodes = (TT.blocks * (uint64_t)TT.blocksize) / TT.bytes_per_inode;
96 }
97 sb->inodes_count = SWAP_LE32(TT.inodes);
98
99 if (!TT.reserved_percent) TT.reserved_percent = 5;
100 temp = (TT.blocks * (uint64_t)TT.reserved_percent) /100;
101 sb->r_blocks_count = SWAP_LE32(temp);
102
103 // Set blocks_per_group and frags_per_group, which is the size of an
104 // allocation bitmap that fits in one block (I.E. how many bits per block)?
105
106 temp = TT.blocksize*8;
107 sb->blocks_per_group = sb->frags_per_group = SWAP_LE32(temp);
108
109 // Set inodes_per_group
110
111 TT.groups = (TT.blocks)/temp;
112 if (TT.blocks & (temp-1)) TT.groups++; // Round up without int overflow.
113 temp = TT.inodes/TT.groups;
114 if (TT.blocks & (TT.groups-1)) TT.blocks++;
115 sb->inodes_per_group = SWAP_LE32(temp);
116
117 sb->max_mnt_count=0xFFFF;
118 sb->wtime = sb->lastcheck = sb->mkfs_time = SWAP_LE32(time(NULL));
119 sb->magic = SWAP_LE32(0xEF53);
120 sb->state = sb->errors = SWAP_LE16(1);
121
86 sb->rev_level = SWAP_LE32(1); 122 sb->rev_level = SWAP_LE32(1);
123 sb->inode_size = sizeof(struct ext2_inode);
87 sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE); 124 sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE);
88 sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); 125 sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER);
126
127 create_uuid(sb->uuid);
89 128
90 // If we're called as mke3fs or mkfs.ext3, do a journal. 129 // If we're called as mke3fs or mkfs.ext3, do a journal.
91 130
92 //if (strchr(toys.which->name,'3')) 131 //if (strchr(toys.which->name,'3'))
93 // sb->feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; 132 // sb->feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
94 133
95 // We skip the first 1k (to avoid the boot sector, if any). Use this to 134 // We skip the first 1k (to avoid the boot sector, if any). Use this to
96 // figure out if this file is seekable. 135 // figure out if this file is seekable.
97 if(-1 == lseek(toy.mke2fs.fsfd, 1024, SEEK_SET)) perror_exit("lseek"); 136 if(-1 == lseek(TT.fsfd, 1024, SEEK_SET)) perror_exit("lseek");
98 //{ toy.mke2fs.noseek=1; xwrite(toy.mke2fs.fsfd, sb, 1024); } 137 //{ TT.noseek=1; xwrite(TT.fsfd, sb, 1024); }
99 138
100 // Write superblock to disk. 139 // Write superblock to disk.
101 xwrite(toy.mke2fs.fsfd, sb, 3072); // 4096-1024 140 xwrite(TT.fsfd, sb, sizeof(struct ext2_superblock)); // 4096-1024
102 141
103 return 0; 142 return 0;
104 } 143 }