Mercurial > hg > toybox
comparison toys/mke2fs.c @ 75:89ca591a9236
More random progress on mke2fs. Nothing to see yet.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 23 Jan 2007 13:20:38 -0500 |
parents | 41d55b5d49fd |
children | e6332139adae |
comparison
equal
deleted
inserted
replaced
74:dfe99495acbc | 75:89ca591a9236 |
---|---|
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 // Stuff defined in linux/ext2_fs.h | |
11 | |
12 #define EXT2_SUPER_MAGIC 0xEF53 | |
13 | |
14 struct ext2_inode { | |
15 uint16_t mode; // File mode | |
16 uint16_t uid; // Low 16 bits of Owner Uid | |
17 uint32_t size; // Size in bytes | |
18 uint32_t atime; // Access time | |
19 uint32_t ctime; // Creation time | |
20 uint32_t mtime; // Modification time | |
21 uint32_t dtime; // Deletion Time | |
22 uint16_t gid; // Low 16 bits of Group Id | |
23 uint16_t links_count; // Links count | |
24 uint32_t blocks; // Blocks count | |
25 uint32_t flags; // File flags | |
26 uint32_t reserved1; | |
27 uint32_t block[15]; // Pointers to blocks | |
28 uint32_t generation; // File version (for NFS) | |
29 uint32_t file_acl; // File ACL | |
30 uint32_t dir_acl; // Directory ACL | |
31 uint32_t faddr; // Fragment address | |
32 uint8_t frag; // Fragment number | |
33 uint8_t fsize; // Fragment size | |
34 uint16_t pad1; | |
35 uint16_t uid_high; // High bits of uid | |
36 uint16_t gid_high; // High bits of gid | |
37 uint32_t reserved2; | |
38 }; | |
39 | |
40 struct ext2_super_block { | |
41 uint32_t inodes_count; // Inodes count | |
42 uint32_t blocks_count; // Blocks count | |
43 uint32_t r_blocks_count; // Reserved blocks count | |
44 uint32_t free_blocks_count; // Free blocks count | |
45 uint32_t free_inodes_count; // Free inodes count | |
46 uint32_t first_data_block; // First Data Block | |
47 uint32_t log_block_size; // Block size | |
48 uint32_t log_frag_size; // Fragment size | |
49 uint32_t blocks_per_group; // # Blocks per group | |
50 uint32_t frags_per_group; // # Fragments per group | |
51 uint32_t inodes_per_group; // # Inodes per group | |
52 uint32_t mtime; // Mount time | |
53 uint32_t wtime; // Write time | |
54 uint16_t mnt_count; // Mount count | |
55 uint16_t max_mnt_count; // Maximal mount count | |
56 uint16_t magic; // Magic signature | |
57 uint16_t state; // File system state | |
58 uint16_t errors; // Behaviour when detecting errors | |
59 uint16_t minor_rev_level; // minor revision level | |
60 uint32_t lastcheck; // time of last check | |
61 uint32_t checkinterval; // max. time between checks | |
62 uint32_t creator_os; // OS | |
63 uint32_t rev_level; // Revision level | |
64 uint16_t def_resuid; // Default uid for reserved blocks | |
65 uint16_t def_resgid; // Default gid for reserved blocks | |
66 uint32_t first_ino; // First non-reserved inode | |
67 uint16_t inode_size; // size of inode structure | |
68 uint16_t block_group_nr; // block group # of this superblock | |
69 uint32_t feature_compat; // compatible feature set | |
70 uint32_t feature_incompat; // incompatible feature set | |
71 uint32_t feature_ro_compat; // readonly-compatible feature set | |
72 char uuid[16]; // 128-bit uuid for volume | |
73 char volume_name[16]; // volume name | |
74 char last_mounted[64]; // directory where last mounted | |
75 uint32_t alg_usage_bitmap; // For compression | |
76 // For EXT2_COMPAT_PREALLOC | |
77 uint8_t prealloc_blocks; // Nr of blocks to try to preallocate | |
78 uint8_t prealloc_dir_blocks; //Nr to preallocate for dirs | |
79 uint16_t padding1; | |
80 // For EXT3_FEATURE_COMPAT_HAS_JOURNAL | |
81 uint8_t journal_uuid[16]; // uuid of journal superblock | |
82 uint32_t journal_inum; // inode number of journal file | |
83 uint32_t journal_dev; // device number of journal file | |
84 uint32_t last_orphan; // start of list of inodes to delete | |
85 uint32_t hash_seed[4]; // HTREE hash seed | |
86 uint8_t def_hash_version; // Default hash version to use | |
87 uint8_t padding2[3]; | |
88 uint32_t default_mount_opts; | |
89 uint32_t first_meta_bg; // First metablock block group | |
90 uint32_t reserved[190]; // Padding to the end of the block | |
91 }; | |
92 | |
93 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001 | |
94 #define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002 | |
95 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004 | |
96 #define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008 | |
97 #define EXT2_FEATURE_COMPAT_RESIZE_INO 0x0010 | |
98 #define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020 | |
99 | |
100 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 | |
101 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002 | |
102 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004 | |
103 | |
104 #define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001 | |
105 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002 | |
106 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 | |
107 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 | |
108 #define EXT2_FEATURE_INCOMPAT_META_BG 0x0010 | |
109 | |
110 #define EXT2_NAME_LEN 255 | |
111 | |
112 struct ext2_dir_entry_2 { | |
113 uint32_t inode; // Inode number | |
114 uint16_t rec_len; // Directory entry length | |
115 uint8_t name_len; // Name length | |
116 uint8_t file_type; | |
117 char name[255]; // File name | |
118 }; | |
119 | |
120 // Ext2 directory file types. Only the low 3 bits are used. The | |
121 // other bits are reserved for now. | |
122 | |
123 enum { | |
124 EXT2_FT_UNKNOWN, | |
125 EXT2_FT_REG_FILE, | |
126 EXT2_FT_DIR, | |
127 EXT2_FT_CHRDEV, | |
128 EXT2_FT_BLKDEV, | |
129 EXT2_FT_FIFO, | |
130 EXT2_FT_SOCK, | |
131 EXT2_FT_SYMLINK, | |
132 EXT2_FT_MAX | |
133 }; | |
134 | |
135 | 9 |
136 // b - block size (1024, 2048, 4096) | 10 // b - block size (1024, 2048, 4096) |
137 // F - force (run on mounted device or non-block device) | 11 // F - force (run on mounted device or non-block device) |
138 // i - bytes per inode | 12 // i - bytes per inode |
139 // N - number of inodes | 13 // N - number of inodes |
149 // J - journal options (size=1024-102400 blocks,device=) | 23 // J - journal options (size=1024-102400 blocks,device=) |
150 // device=/dev/blah or LABEL=label UUID=uuid | 24 // device=/dev/blah or LABEL=label UUID=uuid |
151 | 25 |
152 // E - extended options (stride=stripe-size blocks) | 26 // E - extended options (stride=stripe-size blocks) |
153 // O - none,dir_index,filetype,has_journal,journal_dev,sparse_super | 27 // O - none,dir_index,filetype,has_journal,journal_dev,sparse_super |
154 | |
155 | |
156 // This is what's in a UUID according to the spec at | |
157 // http://www.opengroup.org/onlinepubs/9629399/apdxa.htm | |
158 | |
159 //struct uuid { | |
160 // uint32_t time_low; | |
161 // uint16_t time_mid; | |
162 // uint16_t time_hi_and_version; | |
163 // uint8_t clock_seq_hi_and_reserved; | |
164 // uint8_t clock_seq_low; | |
165 // uint8_t node[6]; | |
166 //}; | |
167 | 28 |
168 | 29 |
169 // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm | 30 // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm |
170 // we should generate a uuid structure by reading a clock with 100 nanosecond | 31 // we should generate a uuid structure by reading a clock with 100 nanosecond |
171 // precision, normalizing it to the start of the gregorian calendar in 1582, | 32 // precision, normalizing it to the start of the gregorian calendar in 1582, |
193 | 54 |
194 int mke2fs_main(void) | 55 int mke2fs_main(void) |
195 { | 56 { |
196 struct ext2_super_block *sb = xzalloc(sizeof(struct ext2_super_block)); | 57 struct ext2_super_block *sb = xzalloc(sizeof(struct ext2_super_block)); |
197 int temp; | 58 int temp; |
59 off_t length; | |
198 | 60 |
199 // Handle command line arguments. | 61 // Handle command line arguments. |
200 | 62 |
201 if (!*toys.optargs || (!CFG_MKE2FS_GEN && toys.optargs[1])) usage_exit(); | 63 if (toys.optargs[1]) { |
202 if (CFG_MKE2FS_GEN && toys.optargs[1]) { | 64 sscanf(toys.optargs[1], "%u", &(sb->inodes_count)); |
203 temp = O_RDWR|O_CREAT; | 65 temp = O_RDWR|O_CREAT; |
204 xaccess(toys.optargs[1], R_OK); | |
205 } else temp = O_RDWR; | 66 } else temp = O_RDWR; |
206 if (toy.mke2fs.blocksize!=1024 && toy.mke2fs.blocksize!=2048 | 67 |
207 && toy.mke2fs.blocksize!=4096) error_exit("bad blocksize"); | 68 // Check if filesystem is mounted |
208 | 69 |
209 // For mke?fs, open file. For gene?fs, create file. | 70 // For mke?fs, open file. For gene?fs, create file. |
210 toy.mke2fs.fsfd = xcreate(*toys.optargs, temp, 0777); | 71 length = fdlength(toy.mke2fs.fsfd = xcreate(*toys.optargs, temp, 0777)); |
211 | 72 |
212 // We don't autodetect block size from external journaling devices, instead | 73 if (toy.mke2fs.blocksize && toy.mke2fs.blocksize!=1024 |
213 // we write our block size to that journaling device. (If they want a | 74 && toy.mke2fs.blocksize!=2048 && toy.mke2fs.blocksize!=4096) |
214 // specific block size, they have the -b option.) | 75 error_exit("bad blocksize"); |
215 | 76 |
216 // What's the deal with fs_type? | 77 // Determine block size. If unspecified, use simple heuristic. |
217 // line 1059 | 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; | |
218 | 81 |
219 // We skip the first 1k (to avoid the boot sector, if any). Use this to | 82 if (!sb->inodes_count) sb->inodes_count = length/toy.mke2fs.blocksize; |
220 // figure out if this file is seekable. | |
221 if(-1 == lseek(toy.mke2fs.fsfd, 1024, SEEK_SET)) { | |
222 toy.mke2fs.noseek=1; | |
223 xwrite(toy.mke2fs.fsfd, sb, 1024); | |
224 } | |
225 | 83 |
226 // Fill out superblock structure | 84 // Fill out superblock structure |
227 | 85 |
228 sb->rev_level = SWAP_LE32(1); | 86 sb->rev_level = SWAP_LE32(1); |
229 sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE); | 87 sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE); |
230 sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); | 88 sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); |
231 | 89 |
232 // If we're called as mke3fs or mkfs.ext3, do a journal. | 90 // If we're called as mke3fs or mkfs.ext3, do a journal. |
233 | 91 |
234 if (strchr(toys.which->name,'3')) | 92 //if (strchr(toys.which->name,'3')) |
235 sb->feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; | 93 // sb->feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
94 | |
95 // We skip the first 1k (to avoid the boot sector, if any). Use this to | |
96 // figure out if this file is seekable. | |
97 if(-1 == lseek(toy.mke2fs.fsfd, 1024, SEEK_SET)) perror_exit("lseek"); | |
98 //{ toy.mke2fs.noseek=1; xwrite(toy.mke2fs.fsfd, sb, 1024); } | |
236 | 99 |
237 // Write superblock to disk. | 100 // Write superblock to disk. |
238 xwrite(toy.mke2fs.fsfd, sb, 3072); // 4096-1024 | 101 xwrite(toy.mke2fs.fsfd, sb, 3072); // 4096-1024 |
239 | 102 |
240 return 0; | 103 return 0; |