Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * mkfs_ext2: utility to create EXT2 filesystem |
| 4 | * inspired by genext2fs |
| 5 | * |
| 6 | * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com> |
| 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | #include "libbb.h" |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/ext2_fs.h> |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 13 | #include <sys/user.h> /* PAGE_SIZE */ |
| 14 | #ifndef PAGE_SIZE |
| 15 | # define PAGE_SIZE 4096 |
| 16 | #endif |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 17 | #include "volume_id/volume_id_internal.h" |
| 18 | |
| 19 | #define ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT 0 |
| 20 | #define ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX 1 |
| 21 | |
| 22 | // from e2fsprogs |
| 23 | #define s_reserved_gdt_blocks s_padding1 |
| 24 | #define s_mkfs_time s_reserved[0] |
| 25 | #define s_flags s_reserved[22] |
| 26 | #define EXT2_HASH_HALF_MD4 1 |
| 27 | #define EXT2_FLAGS_SIGNED_HASH 0x0001 |
| 28 | |
| 29 | // whiteout: for writable overlays |
| 30 | //#define LINUX_S_IFWHT 0160000 |
| 31 | //#define EXT2_FEATURE_INCOMPAT_WHITEOUT 0x0020 |
| 32 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 33 | // storage helpers |
| 34 | char BUG_wrong_field_size(void); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 35 | #define STORE_LE(field, value) \ |
| 36 | do { \ |
| 37 | if (sizeof(field) == 4) \ |
| 38 | field = cpu_to_le32(value); \ |
| 39 | else if (sizeof(field) == 2) \ |
| 40 | field = cpu_to_le16(value); \ |
| 41 | else if (sizeof(field) == 1) \ |
| 42 | field = (value); \ |
| 43 | else \ |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 44 | BUG_wrong_field_size(); \ |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 45 | } while (0) |
| 46 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 47 | #define FETCH_LE32(field) \ |
| 48 | (sizeof(field) == 4 ? cpu_to_le32(field) : BUG_wrong_field_size()) |
| 49 | |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 50 | // All fields are little-endian |
| 51 | struct ext2_dir { |
| 52 | uint32_t inode1; |
| 53 | uint16_t rec_len1; |
| 54 | uint8_t name_len1; |
| 55 | uint8_t file_type1; |
| 56 | char name1[4]; |
| 57 | uint32_t inode2; |
| 58 | uint16_t rec_len2; |
| 59 | uint8_t name_len2; |
| 60 | uint8_t file_type2; |
| 61 | char name2[4]; |
| 62 | uint32_t inode3; |
| 63 | uint16_t rec_len3; |
| 64 | uint8_t name_len3; |
| 65 | uint8_t file_type3; |
| 66 | char name3[12]; |
| 67 | }; |
| 68 | |
Denys Vlasenko | 9c2708e | 2009-10-17 23:27:29 +0200 | [diff] [blame] | 69 | static unsigned int_log2(unsigned arg) |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 70 | { |
Denys Vlasenko | 9c2708e | 2009-10-17 23:27:29 +0200 | [diff] [blame] | 71 | unsigned r = 0; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 72 | while ((arg >>= 1) != 0) |
| 73 | r++; |
| 74 | return r; |
| 75 | } |
| 76 | |
| 77 | // taken from mkfs_minix.c. libbb candidate? |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 78 | // "uint32_t size", since we never use it for anything >32 bits |
| 79 | static uint32_t div_roundup(uint32_t size, uint32_t n) |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 80 | { |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 81 | // Overflow-resistant |
Denys Vlasenko | 69f2e2c | 2009-10-20 00:16:24 +0200 | [diff] [blame] | 82 | uint32_t res = size / n; |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 83 | if (res * n != size) |
| 84 | res++; |
| 85 | return res; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void allocate(uint8_t *bitmap, uint32_t blocksize, uint32_t start, uint32_t end) |
| 89 | { |
| 90 | uint32_t i; |
Denys Vlasenko | cbeb452 | 2009-10-20 16:21:29 +0200 | [diff] [blame] | 91 | |
| 92 | //bb_info_msg("ALLOC: [%u][%u][%u]: [%u-%u]:=[%x],[%x]", blocksize, start, end, start/8, blocksize - end/8 - 1, (1 << (start & 7)) - 1, (uint8_t)(0xFF00 >> (end & 7))); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 93 | memset(bitmap, 0, blocksize); |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 94 | i = start / 8; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 95 | memset(bitmap, 0xFF, i); |
Denys Vlasenko | cbeb452 | 2009-10-20 16:21:29 +0200 | [diff] [blame] | 96 | bitmap[i] = (1 << (start & 7)) - 1; //0..7 => 00000000..01111111 |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 97 | i = end / 8; |
Denys Vlasenko | cbeb452 | 2009-10-20 16:21:29 +0200 | [diff] [blame] | 98 | bitmap[blocksize - i - 1] |= 0x7F00 >> (end & 7); //0..7 => 00000000..11111110 |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 99 | memset(bitmap + blocksize - i, 0xFF, i); // N.B. no overflow here! |
| 100 | } |
| 101 | |
Denys Vlasenko | 4588775 | 2009-10-17 23:13:31 +0200 | [diff] [blame] | 102 | static uint32_t has_super(uint32_t x) |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 103 | { |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 104 | // 0, 1 and powers of 3, 5, 7 up to 2^32 limit |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 105 | static const uint32_t supers[] = { |
| 106 | 0, 1, 3, 5, 7, 9, 25, 27, 49, 81, 125, 243, 343, 625, 729, |
| 107 | 2187, 2401, 3125, 6561, 15625, 16807, 19683, 59049, 78125, |
| 108 | 117649, 177147, 390625, 531441, 823543, 1594323, 1953125, |
| 109 | 4782969, 5764801, 9765625, 14348907, 40353607, 43046721, |
| 110 | 48828125, 129140163, 244140625, 282475249, 387420489, |
| 111 | 1162261467, 1220703125, 1977326743, 3486784401/* >2^31 */, |
| 112 | }; |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 113 | const uint32_t *sp = supers + ARRAY_SIZE(supers); |
Denys Vlasenko | 7d8ab84 | 2009-10-17 23:23:45 +0200 | [diff] [blame] | 114 | while (1) { |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 115 | sp--; |
Denys Vlasenko | 7d8ab84 | 2009-10-17 23:23:45 +0200 | [diff] [blame] | 116 | if (x == *sp) |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 117 | return 1; |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 118 | if (x > *sp) |
Denys Vlasenko | 7d8ab84 | 2009-10-17 23:23:45 +0200 | [diff] [blame] | 119 | return 0; |
Denys Vlasenko | 7d8ab84 | 2009-10-17 23:23:45 +0200 | [diff] [blame] | 120 | } |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 121 | } |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 122 | |
Denys Vlasenko | 2ee2724 | 2009-10-21 11:34:32 +0200 | [diff] [blame^] | 123 | #define fd 3 /* predefined output descriptor */ |
| 124 | |
| 125 | static void PUT(uint64_t off, void *buf, uint32_t size) |
| 126 | { |
| 127 | // bb_info_msg("PUT[%llu]:[%u]", off, size); |
| 128 | xlseek(fd, off, SEEK_SET); |
| 129 | xwrite(fd, buf, size); |
| 130 | } |
| 131 | |
| 132 | // 128 and 256-byte inodes: |
| 133 | // 128-byte inode is described by struct ext2_inode. |
| 134 | // 256-byte one just has these fields appended: |
| 135 | // __u16 i_extra_isize; |
| 136 | // __u16 i_pad1; |
| 137 | // __u32 i_ctime_extra; /* extra Change time (nsec << 2 | epoch) */ |
| 138 | // __u32 i_mtime_extra; /* extra Modification time (nsec << 2 | epoch) */ |
| 139 | // __u32 i_atime_extra; /* extra Access time (nsec << 2 | epoch) */ |
| 140 | // __u32 i_crtime; /* File creation time */ |
| 141 | // __u32 i_crtime_extra; /* extra File creation time (nsec << 2 | epoch)*/ |
| 142 | // __u32 i_version_hi; /* high 32 bits for 64-bit version */ |
| 143 | // the rest is padding. |
| 144 | // |
| 145 | // linux/ext2_fs.h has "#define i_size_high i_dir_acl" which suggests that even |
| 146 | // 128-byte inode is capable of describing large files (i_dir_acl is meaningful |
| 147 | // only for directories, which never need i_size_high). |
| 148 | // |
| 149 | // Standard mke2fs creates a filesystem with 256-byte inodes if it is |
| 150 | // bigger than 0.5GB. So far, we do not do this. |
| 151 | |
| 152 | // Standard mke2fs 1.41.9: |
| 153 | // Usage: mke2fs [-c|-l filename] [-b block-size] [-f fragment-size] |
| 154 | // [-i bytes-per-inode] [-I inode-size] [-J journal-options] |
| 155 | // [-G meta group size] [-N number-of-inodes] |
| 156 | // [-m reserved-blocks-percentage] [-o creator-os] |
| 157 | // [-g blocks-per-group] [-L volume-label] [-M last-mounted-directory] |
| 158 | // [-O feature[,...]] [-r fs-revision] [-E extended-option[,...]] |
| 159 | // [-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count] |
| 160 | // |
| 161 | // Options not commented below are taken but silently ignored: |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 162 | enum { |
| 163 | OPT_c = 1 << 0, |
| 164 | OPT_l = 1 << 1, |
| 165 | OPT_b = 1 << 2, // block size, in bytes |
| 166 | OPT_f = 1 << 3, |
| 167 | OPT_i = 1 << 4, // bytes per inode |
| 168 | OPT_I = 1 << 5, |
| 169 | OPT_J = 1 << 6, |
| 170 | OPT_G = 1 << 7, |
| 171 | OPT_N = 1 << 8, |
| 172 | OPT_m = 1 << 9, // percentage of blocks reserved for superuser |
| 173 | OPT_o = 1 << 10, |
| 174 | OPT_g = 1 << 11, |
| 175 | OPT_L = 1 << 12, // label |
| 176 | OPT_M = 1 << 13, |
| 177 | OPT_O = 1 << 14, |
| 178 | OPT_r = 1 << 15, |
| 179 | OPT_E = 1 << 16, |
| 180 | OPT_T = 1 << 17, |
| 181 | OPT_U = 1 << 18, |
| 182 | OPT_j = 1 << 19, |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 183 | OPT_n = 1 << 20, // dry run: do not write anything |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 184 | OPT_q = 1 << 21, |
| 185 | OPT_v = 1 << 22, |
| 186 | OPT_F = 1 << 23, |
| 187 | OPT_S = 1 << 24, |
| 188 | //OPT_V = 1 << 25, // -V version. bbox applets don't support that |
| 189 | }; |
| 190 | |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 191 | int mkfs_ext2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 192 | int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv) |
| 193 | { |
| 194 | unsigned i, pos, n; |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 195 | unsigned bs, bpi; |
| 196 | unsigned blocksize, blocksize_log2; |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 197 | unsigned reserved_percent = 5; |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 198 | unsigned long long kilobytes; |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 199 | uint32_t nblocks, nblocks_full; |
| 200 | uint32_t nreserved; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 201 | uint32_t ngroups; |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 202 | uint32_t bytes_per_inode; |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 203 | uint32_t first_block; |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 204 | uint32_t inodes_per_group; |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 205 | uint32_t group_desc_blocks; |
| 206 | uint32_t inode_table_blocks; |
| 207 | uint32_t lost_and_found_blocks; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 208 | time_t timestamp; |
| 209 | unsigned opts; |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 210 | const char *label = ""; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 211 | struct stat st; |
| 212 | struct ext2_super_block *sb; // superblock |
| 213 | struct ext2_group_desc *gd; // group descriptors |
| 214 | struct ext2_inode *inode; |
| 215 | struct ext2_dir *dir; |
| 216 | uint8_t *buf; |
| 217 | |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 218 | opt_complementary = "-1:b+:m+:i+"; |
| 219 | opts = getopt32(argv, "cl:b:f:i:I:J:G:N:m:o:g:L:M:O:r:E:T:U:jnqvFS", |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 220 | NULL, &bs, NULL, &bpi, NULL, NULL, NULL, NULL, |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 221 | &reserved_percent, NULL, NULL, &label, NULL, NULL, NULL, NULL, NULL, NULL); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 222 | argv += optind; // argv[0] -- device |
| 223 | |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 224 | // check the device is a block device |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 225 | xmove_fd(xopen(argv[0], O_WRONLY), fd); |
| 226 | fstat(fd, &st); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 227 | if (!S_ISBLK(st.st_mode) && !(opts & OPT_F)) |
| 228 | bb_error_msg_and_die("not a block device"); |
| 229 | |
| 230 | // check if it is mounted |
| 231 | // N.B. what if we format a file? find_mount_point will return false negative since |
| 232 | // it is loop block device which mounted! |
| 233 | if (find_mount_point(argv[0], 0)) |
| 234 | bb_error_msg_and_die("can't format mounted filesystem"); |
| 235 | |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 236 | // open the device, get size in kbytes |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 237 | if (argv[1]) { |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 238 | kilobytes = xatoull(argv[1]); |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 239 | } else { |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 240 | kilobytes = (uoff_t)xlseek(fd, 0, SEEK_END) / 1024; |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 243 | bytes_per_inode = 16384; |
| 244 | if (kilobytes < 512*1024) |
| 245 | bytes_per_inode = 4096; |
| 246 | if (kilobytes < 3*1024) |
| 247 | bytes_per_inode = 8192; |
| 248 | if (opts & OPT_i) |
| 249 | bytes_per_inode = bpi; |
| 250 | |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 251 | // Determine block size |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 252 | // block size is a multiple of 1024 |
| 253 | blocksize = 1024; |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 254 | if (kilobytes >= 512*1024) // mke2fs 1.41.9 compat |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 255 | blocksize = 4096; |
| 256 | if (EXT2_MAX_BLOCK_SIZE > 4096) { |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 257 | // kilobytes >> 22 == size in 4gigabyte chunks. |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 258 | // if size >= 16k gigs, blocksize must be increased. |
| 259 | // Try "mke2fs -F image $((16 * 1024*1024*1024))" |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 260 | while ((kilobytes >> 22) >= blocksize) |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 261 | blocksize *= 2; |
| 262 | } |
| 263 | if (opts & OPT_b) |
| 264 | blocksize = bs; |
| 265 | if (blocksize < EXT2_MIN_BLOCK_SIZE |
| 266 | || blocksize > EXT2_MAX_BLOCK_SIZE |
| 267 | || (blocksize & (blocksize - 1)) // not power of 2 |
| 268 | ) { |
| 269 | bb_error_msg_and_die("blocksize %u is bad", blocksize); |
| 270 | } |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 271 | if ((int32_t)bytes_per_inode < blocksize) |
| 272 | bb_error_msg_and_die("-%c is bad", 'i'); |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 273 | // number of bits in one block, i.e. 8*blocksize |
| 274 | #define blocks_per_group (8 * blocksize) |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 275 | first_block = (EXT2_MIN_BLOCK_SIZE == blocksize); |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 276 | blocksize_log2 = int_log2(blocksize); |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 277 | |
| 278 | // Determine number of blocks |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 279 | kilobytes >>= (blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE); |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 280 | nblocks = kilobytes; |
| 281 | if (nblocks != kilobytes) |
Denys Vlasenko | f9d3a91 | 2009-10-18 18:05:27 +0200 | [diff] [blame] | 282 | bb_error_msg_and_die("block count doesn't fit in 32 bits"); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 283 | #define kilobytes kilobytes_unused_after_this |
Denys Vlasenko | 82d6433 | 2009-10-20 17:04:55 +0200 | [diff] [blame] | 284 | //compat problem |
| 285 | // if (blocksize < PAGE_SIZE) |
| 286 | // nblocks &= ~((PAGE_SIZE >> blocksize_log2)-1); |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 287 | // Experimentally, standard mke2fs won't work on images smaller than 60k |
| 288 | if (nblocks < 60) |
| 289 | bb_error_msg_and_die("need >= 60 blocks"); |
| 290 | |
| 291 | // How many reserved blocks? |
| 292 | if (reserved_percent > 50) |
| 293 | bb_error_msg_and_die("-%c is bad", 'm'); |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 294 | nreserved = (uint64_t)nblocks * reserved_percent / 100; |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 295 | |
| 296 | // N.B. killing e2fsprogs feature! Unused blocks don't account in calculations |
| 297 | nblocks_full = nblocks; |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 298 | |
| 299 | // If last block group is too small, nblocks may be decreased in order |
| 300 | // to discard it, and control returns here to recalculate some |
| 301 | // parameters. |
| 302 | // Note: blocksize and bytes_per_inode are never recalculated. |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 303 | retry: |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 304 | // N.B. a block group can have no more than blocks_per_group blocks |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 305 | ngroups = div_roundup(nblocks - first_block, blocks_per_group); |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 306 | |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 307 | group_desc_blocks = div_roundup(ngroups, blocksize / sizeof(*gd)); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 308 | // TODO: reserved blocks must be marked as such in the bitmaps, |
| 309 | // or resulting filesystem is corrupt |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 310 | if (ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT) { |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 311 | /* |
| 312 | * From e2fsprogs: Calculate the number of GDT blocks to reserve for online |
| 313 | * filesystem growth. |
| 314 | * The absolute maximum number of GDT blocks we can reserve is determined by |
| 315 | * the number of block pointers that can fit into a single block. |
| 316 | * We set it at 1024x the current filesystem size, or |
| 317 | * the upper block count limit (2^32), whichever is lower. |
| 318 | */ |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 319 | uint32_t reserved_group_desc_blocks = 0xFFFFFFFF; // maximum block number |
| 320 | if (nblocks < reserved_group_desc_blocks / 1024) |
| 321 | reserved_group_desc_blocks = nblocks * 1024; |
| 322 | reserved_group_desc_blocks = div_roundup(reserved_group_desc_blocks - first_block, blocks_per_group); |
| 323 | reserved_group_desc_blocks = div_roundup(reserved_group_desc_blocks, blocksize / sizeof(*gd)) - group_desc_blocks; |
| 324 | if (reserved_group_desc_blocks > blocksize / sizeof(uint32_t)) |
| 325 | reserved_group_desc_blocks = blocksize / sizeof(uint32_t); |
| 326 | //TODO: STORE_LE(sb->s_reserved_gdt_blocks, reserved_group_desc_blocks); |
| 327 | group_desc_blocks += reserved_group_desc_blocks; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 328 | } |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 329 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 330 | { |
| 331 | // N.B. e2fsprogs does as follows! |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 332 | uint32_t overhead, remainder; |
Denys Vlasenko | 2288d86 | 2009-10-21 00:34:27 +0200 | [diff] [blame] | 333 | // ninodes is the max number of inodes in this filesystem |
| 334 | uint32_t ninodes = ((uint64_t) nblocks_full * blocksize) / bytes_per_inode; |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 335 | if (ninodes < EXT2_GOOD_OLD_FIRST_INO+1) |
| 336 | ninodes = EXT2_GOOD_OLD_FIRST_INO+1; |
| 337 | inodes_per_group = div_roundup(ninodes, ngroups); |
| 338 | // minimum number because the first EXT2_GOOD_OLD_FIRST_INO-1 are reserved |
| 339 | if (inodes_per_group < 16) |
| 340 | inodes_per_group = 16; |
Denys Vlasenko | 2288d86 | 2009-10-21 00:34:27 +0200 | [diff] [blame] | 341 | // a block group can't have more inodes than blocks |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 342 | if (inodes_per_group > blocks_per_group) |
| 343 | inodes_per_group = blocks_per_group; |
| 344 | // adjust inodes per group so they completely fill the inode table blocks in the descriptor |
Denys Vlasenko | 2288d86 | 2009-10-21 00:34:27 +0200 | [diff] [blame] | 345 | //incompatibility on images >= 0.5GB: |
| 346 | //difference in sizeof(*inode) sometimes |
| 347 | //results in slightly bigger inodes_per_group here |
| 348 | //compared to standard mke2fs: |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 349 | inodes_per_group = (div_roundup(inodes_per_group * sizeof(*inode), blocksize) * blocksize) / sizeof(*inode); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 350 | // make sure the number of inodes per group is a multiple of 8 |
| 351 | inodes_per_group &= ~7; |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 352 | inode_table_blocks = div_roundup(inodes_per_group * sizeof(*inode), blocksize); |
| 353 | |
| 354 | // to be useful, lost+found should occupy at least 2 blocks (but not exceeding 16*1024 bytes), |
| 355 | // and at most EXT2_NDIR_BLOCKS. So reserve these blocks right now |
| 356 | /* Or e2fsprogs comment verbatim (what does it mean?): |
| 357 | * Ensure that lost+found is at least 2 blocks, so we always |
| 358 | * test large empty blocks for big-block filesystems. */ |
| 359 | lost_and_found_blocks = MIN(EXT2_NDIR_BLOCKS, 16 >> (blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE)); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 360 | |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 361 | // the last group needs more attention: isn't it too small for possible overhead? |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 362 | overhead = (has_super(ngroups - 1) ? (1/*sb*/ + group_desc_blocks) : 0) + 1/*bbmp*/ + 1/*ibmp*/ + inode_table_blocks; |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 363 | remainder = (nblocks - first_block) % blocks_per_group; |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 364 | ////can't happen, nblocks >= 60 guarantees this |
| 365 | ////if ((1 == ngroups) |
| 366 | //// && remainder |
| 367 | //// && (remainder < overhead + 1/* "/" */ + lost_and_found_blocks) |
| 368 | ////) { |
| 369 | //// bb_error_msg_and_die("way small device"); |
| 370 | ////} |
Denys Vlasenko | 2288d86 | 2009-10-21 00:34:27 +0200 | [diff] [blame] | 371 | |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 372 | // Standard mke2fs uses 50. Looks like a bug in our calculation |
Denys Vlasenko | a4e4a65 | 2009-10-20 17:49:50 +0200 | [diff] [blame] | 373 | // of "remainder" or "overhead" - we don't match standard mke2fs |
| 374 | // when we transition from one group to two groups |
| 375 | // (a bit after 8M image size), but it works for two->three groups |
| 376 | // transition (at 16M). |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 377 | if (remainder && (remainder < overhead + 50)) { |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 378 | //bb_info_msg("CHOP[%u]", remainder); |
| 379 | nblocks -= remainder; |
| 380 | goto retry; |
| 381 | } |
| 382 | } |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 383 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 384 | if (nblocks_full - nblocks) |
| 385 | printf("warning: %u blocks unused\n\n", nblocks_full - nblocks); |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 386 | printf( |
| 387 | "Filesystem label=%s\n" |
| 388 | "OS type: Linux\n" |
| 389 | "Block size=%u (log=%u)\n" |
| 390 | "Fragment size=%u (log=%u)\n" |
| 391 | "%u inodes, %u blocks\n" |
| 392 | "%u blocks (%u%%) reserved for the super user\n" |
| 393 | "First data block=%u\n" |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 394 | "Maximum filesystem blocks=%u\n" |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 395 | "%u block groups\n" |
| 396 | "%u blocks per group, %u fragments per group\n" |
| 397 | "%u inodes per group" |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 398 | , label |
| 399 | , blocksize, blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE |
| 400 | , blocksize, blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE |
| 401 | , inodes_per_group * ngroups, nblocks |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 402 | , nreserved, reserved_percent |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 403 | , first_block |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 404 | , group_desc_blocks * (blocksize / sizeof(*gd)) * blocks_per_group |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 405 | , ngroups |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 406 | , blocks_per_group, blocks_per_group |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 407 | , inodes_per_group |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 408 | ); |
| 409 | { |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 410 | const char *fmt = "\nSuperblock backups stored on blocks:\n" |
| 411 | "\t%u"; |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 412 | pos = first_block; |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 413 | for (i = 1; i < ngroups; i++) { |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 414 | pos += blocks_per_group; |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 415 | if (has_super(i)) { |
| 416 | printf(fmt, (unsigned)pos); |
| 417 | fmt = ", %u"; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | bb_putchar('\n'); |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 422 | |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 423 | if (opts & OPT_n) { |
| 424 | if (ENABLE_FEATURE_CLEAN_UP) |
| 425 | close(fd); |
| 426 | return EXIT_SUCCESS; |
| 427 | } |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 428 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 429 | // TODO: 3/5 refuse if mounted |
| 430 | // TODO: 4/5 compat options |
| 431 | // TODO: 1/5 sanity checks |
| 432 | // TODO: 0/5 more verbose error messages |
| 433 | // TODO: 4/5 bigendianness: recheck, wait for ARM reporters |
| 434 | // TODO: 2/5 reserved GDT: how to mark but not allocate? |
| 435 | // TODO: 3/5 dir_index? |
| 436 | |
| 437 | // fill the superblock |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 438 | sb = xzalloc(1024); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 439 | STORE_LE(sb->s_rev_level, 1); // revision 1 filesystem |
| 440 | STORE_LE(sb->s_magic, EXT2_SUPER_MAGIC); |
Denys Vlasenko | 2288d86 | 2009-10-21 00:34:27 +0200 | [diff] [blame] | 441 | //incompatibility: |
| 442 | //on images > 0.5GB, standard mke2fs uses 256 byte inodes. |
| 443 | //we always use 128 byte ones: |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 444 | STORE_LE(sb->s_inode_size, sizeof(*inode)); |
| 445 | STORE_LE(sb->s_first_ino, EXT2_GOOD_OLD_FIRST_INO); |
| 446 | STORE_LE(sb->s_log_block_size, blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE); |
| 447 | STORE_LE(sb->s_log_frag_size, blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE); |
| 448 | // first 1024 bytes of the device are for boot record. If block size is 1024 bytes, then |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 449 | // the first block is 1, otherwise 0 |
| 450 | STORE_LE(sb->s_first_data_block, first_block); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 451 | // block and inode bitmaps occupy no more than one block, so maximum number of blocks is |
| 452 | STORE_LE(sb->s_blocks_per_group, blocks_per_group); |
| 453 | STORE_LE(sb->s_frags_per_group, blocks_per_group); |
| 454 | // blocks |
| 455 | STORE_LE(sb->s_blocks_count, nblocks); |
| 456 | // reserve blocks for superuser |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 457 | STORE_LE(sb->s_r_blocks_count, nreserved); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 458 | // ninodes |
| 459 | STORE_LE(sb->s_inodes_per_group, inodes_per_group); |
| 460 | STORE_LE(sb->s_inodes_count, inodes_per_group * ngroups); |
| 461 | STORE_LE(sb->s_free_inodes_count, inodes_per_group * ngroups - EXT2_GOOD_OLD_FIRST_INO); |
| 462 | // timestamps |
| 463 | timestamp = time(NULL); |
| 464 | STORE_LE(sb->s_mkfs_time, timestamp); |
| 465 | STORE_LE(sb->s_wtime, timestamp); |
| 466 | STORE_LE(sb->s_lastcheck, timestamp); |
| 467 | // misc |
| 468 | STORE_LE(sb->s_state, 1); // TODO: what's 1? |
| 469 | STORE_LE(sb->s_creator_os, EXT2_OS_LINUX); |
| 470 | STORE_LE(sb->s_checkinterval, 24*60*60 * 180); // 180 days |
| 471 | STORE_LE(sb->s_errors, EXT2_ERRORS_DEFAULT); |
| 472 | STORE_LE(sb->s_feature_compat, EXT2_FEATURE_COMPAT_SUPP |
| 473 | | (EXT2_FEATURE_COMPAT_RESIZE_INO * ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT) |
| 474 | | (EXT2_FEATURE_COMPAT_DIR_INDEX * ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX) |
| 475 | ); |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 476 | // e2fsck from 1.41.9 doesn't like EXT2_FEATURE_INCOMPAT_WHITEOUT |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 477 | STORE_LE(sb->s_feature_incompat, EXT2_FEATURE_INCOMPAT_FILETYPE);// | EXT2_FEATURE_INCOMPAT_WHITEOUT; |
| 478 | STORE_LE(sb->s_feature_ro_compat, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); |
| 479 | STORE_LE(sb->s_flags, EXT2_FLAGS_SIGNED_HASH * ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX); |
| 480 | generate_uuid(sb->s_uuid); |
| 481 | if (ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX) { |
| 482 | STORE_LE(sb->s_def_hash_version, EXT2_HASH_HALF_MD4); |
| 483 | generate_uuid((uint8_t *)sb->s_hash_seed); |
| 484 | } |
| 485 | /* |
| 486 | * From e2fsprogs: add "jitter" to the superblock's check interval so that we |
| 487 | * don't check all the filesystems at the same time. We use a |
| 488 | * kludgy hack of using the UUID to derive a random jitter value. |
| 489 | */ |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 490 | STORE_LE(sb->s_max_mnt_count, |
| 491 | EXT2_DFL_MAX_MNT_COUNT |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 492 | + (sb->s_uuid[ARRAY_SIZE(sb->s_uuid)-1] % EXT2_DFL_MAX_MNT_COUNT)); |
| 493 | |
Denys Vlasenko | 95484c8 | 2009-10-20 13:34:16 +0200 | [diff] [blame] | 494 | // write the label |
| 495 | safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name)); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 496 | |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 497 | // calculate filesystem skeleton structures |
| 498 | gd = xzalloc(group_desc_blocks * blocksize); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 499 | buf = xmalloc(blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 500 | sb->s_free_blocks_count = 0; |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 501 | for (i = 0, pos = first_block, n = nblocks - first_block; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 502 | i < ngroups; |
Denys Vlasenko | e840561 | 2009-10-18 20:57:52 +0200 | [diff] [blame] | 503 | i++, pos += blocks_per_group, n -= blocks_per_group |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 504 | ) { |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 505 | uint32_t overhead = pos + (has_super(i) ? (1/*sb*/ + group_desc_blocks) : 0); |
| 506 | uint32_t free_blocks; |
| 507 | // fill group descriptors |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 508 | STORE_LE(gd[i].bg_block_bitmap, overhead + 0); |
| 509 | STORE_LE(gd[i].bg_inode_bitmap, overhead + 1); |
| 510 | STORE_LE(gd[i].bg_inode_table, overhead + 2); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 511 | overhead = overhead - pos + 1/*bbmp*/ + 1/*ibmp*/ + inode_table_blocks; |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 512 | gd[i].bg_free_inodes_count = inodes_per_group; |
| 513 | //STORE_LE(gd[i].bg_used_dirs_count, 0); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 514 | // N.B. both "/" and "/lost+found" are within the first block group |
| 515 | // "/" occupies 1 block, "/lost+found" occupies lost_and_found_blocks... |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 516 | if (0 == i) { |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 517 | // ... thus increased overhead for the first block group ... |
| 518 | overhead += 1 + lost_and_found_blocks; |
| 519 | // ... and 2 used directories |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 520 | STORE_LE(gd[i].bg_used_dirs_count, 2); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 521 | // well known reserved inodes belong to the first block too |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 522 | gd[i].bg_free_inodes_count -= EXT2_GOOD_OLD_FIRST_INO; |
| 523 | } |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 524 | |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 525 | // cache free block count of the group |
| 526 | free_blocks = (n < blocks_per_group ? n : blocks_per_group) - overhead; |
| 527 | |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 528 | // mark preallocated blocks as allocated |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 529 | //bb_info_msg("ALLOC: [%u][%u][%u]", blocksize, overhead, blocks_per_group - (free_blocks + overhead)); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 530 | allocate(buf, blocksize, |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 531 | // reserve "overhead" blocks |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 532 | overhead, |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 533 | // mark unused trailing blocks |
| 534 | blocks_per_group - (free_blocks + overhead) |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 535 | ); |
| 536 | // dump block bitmap |
| 537 | PUT((uint64_t)(FETCH_LE32(gd[i].bg_block_bitmap)) * blocksize, buf, blocksize); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 538 | STORE_LE(gd[i].bg_free_blocks_count, free_blocks); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 539 | |
| 540 | // mark preallocated inodes as allocated |
| 541 | allocate(buf, blocksize, |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 542 | // mark reserved inodes |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 543 | inodes_per_group - gd[i].bg_free_inodes_count, |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 544 | // mark unused trailing inodes |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 545 | blocks_per_group - inodes_per_group |
| 546 | ); |
| 547 | // dump inode bitmap |
| 548 | //PUT((uint64_t)(FETCH_LE32(gd[i].bg_block_bitmap)) * blocksize, buf, blocksize); |
| 549 | //but it's right after block bitmap, so we can just: |
| 550 | xwrite(fd, buf, blocksize); |
| 551 | STORE_LE(gd[i].bg_free_inodes_count, gd[i].bg_free_inodes_count); |
| 552 | |
| 553 | // count overall free blocks |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 554 | sb->s_free_blocks_count += free_blocks; |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 555 | } |
| 556 | STORE_LE(sb->s_free_blocks_count, sb->s_free_blocks_count); |
| 557 | |
| 558 | // dump filesystem skeleton structures |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 559 | // printf("Writing superblocks and filesystem accounting information: "); |
Denys Vlasenko | c708a6d | 2009-10-20 17:47:23 +0200 | [diff] [blame] | 560 | for (i = 0, pos = first_block; i < ngroups; i++, pos += blocks_per_group) { |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 561 | // dump superblock and group descriptors and their backups |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 562 | if (has_super(i)) { |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 563 | // N.B. 1024 byte blocks are special |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 564 | PUT(((uint64_t)pos * blocksize) + ((0 == i && 1024 != blocksize) ? 1024 : 0), |
| 565 | sb, 1024); |
| 566 | PUT(((uint64_t)pos * blocksize) + blocksize, |
| 567 | gd, group_desc_blocks * blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 568 | } |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // zero boot sectors |
| 572 | memset(buf, 0, blocksize); |
Denys Vlasenko | 05647b5 | 2009-10-20 00:06:03 +0200 | [diff] [blame] | 573 | PUT(0, buf, 1024); // N.B. 1024 <= blocksize, so buf[0..1023] contains zeros |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 574 | // zero inode tables |
| 575 | for (i = 0; i < ngroups; ++i) |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 576 | for (n = 0; n < inode_table_blocks; ++n) |
| 577 | PUT((uint64_t)(FETCH_LE32(gd[i].bg_inode_table) + n) * blocksize, |
| 578 | buf, blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 579 | |
| 580 | // prepare directory inode |
| 581 | inode = (struct ext2_inode *)buf; |
| 582 | STORE_LE(inode->i_mode, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 583 | STORE_LE(inode->i_mtime, timestamp); |
| 584 | STORE_LE(inode->i_atime, timestamp); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 585 | STORE_LE(inode->i_ctime, timestamp); |
| 586 | STORE_LE(inode->i_size, blocksize); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 587 | // inode->i_blocks stores the number of 512 byte data blocks |
| 588 | // (512, because it goes directly to struct stat without scaling) |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 589 | STORE_LE(inode->i_blocks, blocksize / 512); |
| 590 | |
| 591 | // dump root dir inode |
| 592 | STORE_LE(inode->i_links_count, 3); // "/.", "/..", "/lost+found/.." point to this inode |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 593 | STORE_LE(inode->i_block[0], FETCH_LE32(gd[0].bg_inode_table) + inode_table_blocks); |
| 594 | PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) * blocksize) + (EXT2_ROOT_INO-1) * sizeof(*inode), |
| 595 | buf, sizeof(*inode)); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 596 | |
| 597 | // dump lost+found dir inode |
| 598 | STORE_LE(inode->i_links_count, 2); // both "/lost+found" and "/lost+found/." point to this inode |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 599 | STORE_LE(inode->i_size, lost_and_found_blocks * blocksize); |
| 600 | STORE_LE(inode->i_blocks, (lost_and_found_blocks * blocksize) / 512); |
| 601 | n = FETCH_LE32(inode->i_block[0]) + 1; |
| 602 | for (i = 0; i < lost_and_found_blocks; ++i) |
| 603 | STORE_LE(inode->i_block[i], i + n); // use next block |
| 604 | //bb_info_msg("LAST BLOCK USED[%u]", i + n); |
| 605 | PUT(((uint64_t)FETCH_LE32(gd[0].bg_inode_table) * blocksize) + (EXT2_GOOD_OLD_FIRST_INO-1) * sizeof(*inode), |
| 606 | buf, sizeof(*inode)); |
| 607 | |
| 608 | // zero the blocks for "/lost+found" |
| 609 | memset(buf, 0, blocksize); |
| 610 | for (i = 1; i < lost_and_found_blocks; ++i) |
| 611 | PUT((uint64_t)(FETCH_LE32(gd[0].bg_inode_table) + inode_table_blocks + 1+i) * blocksize, |
| 612 | buf, blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 613 | |
| 614 | // dump directories |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 615 | dir = (struct ext2_dir *)buf; |
| 616 | |
| 617 | // dump lost+found dir block |
| 618 | STORE_LE(dir->inode1, EXT2_GOOD_OLD_FIRST_INO); |
| 619 | STORE_LE(dir->rec_len1, 12); |
| 620 | STORE_LE(dir->name_len1, 1); |
| 621 | STORE_LE(dir->file_type1, EXT2_FT_DIR); |
| 622 | dir->name1[0] = '.'; |
| 623 | STORE_LE(dir->inode2, EXT2_ROOT_INO); |
| 624 | STORE_LE(dir->rec_len2, blocksize - 12); |
| 625 | STORE_LE(dir->name_len2, 2); |
| 626 | STORE_LE(dir->file_type2, EXT2_FT_DIR); |
| 627 | dir->name2[0] = '.'; dir->name2[1] = '.'; |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 628 | PUT((uint64_t)(FETCH_LE32(gd[0].bg_inode_table) + inode_table_blocks + 1) * blocksize, buf, blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 629 | |
| 630 | // dump root dir block |
| 631 | STORE_LE(dir->inode1, EXT2_ROOT_INO); |
| 632 | STORE_LE(dir->rec_len2, 12); |
| 633 | STORE_LE(dir->inode3, EXT2_GOOD_OLD_FIRST_INO); |
| 634 | STORE_LE(dir->rec_len3, blocksize - 12 - 12); |
| 635 | STORE_LE(dir->name_len3, 10); |
| 636 | STORE_LE(dir->file_type3, EXT2_FT_DIR); |
| 637 | strcpy(dir->name3, "lost+found"); |
Denys Vlasenko | 5e1dbd5 | 2009-10-20 22:12:11 +0200 | [diff] [blame] | 638 | PUT((uint64_t)(FETCH_LE32(gd[0].bg_inode_table) + inode_table_blocks + 0) * blocksize, buf, blocksize); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 639 | |
| 640 | // cleanup |
| 641 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 642 | free(buf); |
| 643 | free(gd); |
| 644 | free(sb); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Denys Vlasenko | 77da1ca | 2009-10-18 16:29:30 +0200 | [diff] [blame] | 647 | xclose(fd); |
Vladimir Dronnikov | 823b4e6 | 2009-10-17 21:38:19 +0200 | [diff] [blame] | 648 | return EXIT_SUCCESS; |
| 649 | } |