ext4_utils.c revision c2470654d4b4db09a7052fc5fa108ac21f1b1948
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "ext4_utils.h" 18#include "output_file.h" 19#include "backed_block.h" 20#include "uuid.h" 21#include "allocate.h" 22#include "indirect.h" 23#include "extent.h" 24 25#include <fcntl.h> 26#include <arpa/inet.h> 27#include <sys/ioctl.h> 28#include <sys/stat.h> 29#include <sys/types.h> 30#include <string.h> 31 32#if defined(__linux__) 33#include <linux/fs.h> 34#elif defined(__APPLE__) && defined(__MACH__) 35#include <sys/disk.h> 36#endif 37 38#include "ext4.h" 39#include "jbd2.h" 40 41int force = 0; 42struct fs_info info; 43struct fs_aux_info aux_info; 44 45/* returns 1 if a is a power of b */ 46static int is_power_of(int a, int b) 47{ 48 while (a > b) { 49 if (a % b) 50 return 0; 51 a /= b; 52 } 53 54 return (a == b) ? 1 : 0; 55} 56 57/* Returns 1 if the bg contains a backup superblock. On filesystems with 58 the sparse_super feature, only block groups 0, 1, and powers of 3, 5, 59 and 7 have backup superblocks. Otherwise, all block groups have backup 60 superblocks */ 61int ext4_bg_has_super_block(int bg) 62{ 63 /* Without sparse_super, every block group has a superblock */ 64 if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) 65 return 1; 66 67 if (bg == 0 || bg == 1) 68 return 1; 69 70 if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) 71 return 1; 72 73 return 0; 74} 75 76struct count_chunks { 77 u32 chunks; 78 u64 cur_ptr; 79}; 80 81void count_data_block(void *priv, u64 off, u8 *data, int len) 82{ 83 struct count_chunks *count_chunks = priv; 84 if (off > count_chunks->cur_ptr) 85 count_chunks->chunks++; 86 count_chunks->cur_ptr = off + ALIGN(len, info.block_size); 87 count_chunks->chunks++; 88} 89 90void count_file_block(void *priv, u64 off, const char *file, 91 off64_t offset, int len) 92{ 93 struct count_chunks *count_chunks = priv; 94 if (off > count_chunks->cur_ptr) 95 count_chunks->chunks++; 96 count_chunks->cur_ptr = off + ALIGN(len, info.block_size); 97 count_chunks->chunks++; 98} 99 100int count_sparse_chunks() 101{ 102 struct count_chunks count_chunks = {0, 0}; 103 104 for_each_data_block(count_data_block, count_file_block, &count_chunks); 105 106 if (count_chunks.cur_ptr != (u64) info.len) 107 count_chunks.chunks++; 108 109 return count_chunks.chunks; 110} 111 112static void ext4_write_data_block(void *priv, u64 off, u8 *data, int len) 113{ 114 write_data_block(priv, off, data, len); 115} 116static void ext4_write_data_file(void *priv, u64 off, const char *file, 117 off64_t offset, int len) 118{ 119 write_data_file(priv, off, file, offset, len); 120} 121 122/* Write the filesystem image to a file */ 123void write_ext4_image(const char *filename, int gz, int sparse, int crc, 124 int wipe) 125{ 126 int ret = 0; 127 struct output_file *out = open_output_file(filename, gz, sparse, 128 count_sparse_chunks(), crc, wipe); 129 130 if (!out) 131 return; 132 133 for_each_data_block(ext4_write_data_block, ext4_write_data_file, out); 134 135 pad_output_file(out, info.len); 136 137 close_output_file(out); 138} 139 140/* Compute the rest of the parameters of the filesystem from the basic info */ 141void ext4_create_fs_aux_info() 142{ 143 aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; 144 aux_info.len_blocks = info.len / info.block_size; 145 aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size, 146 info.block_size); 147 aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, 148 info.blocks_per_group); 149 aux_info.blocks_per_ind = info.block_size / sizeof(u32); 150 aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; 151 aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; 152 153 aux_info.bg_desc_blocks = 154 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc), 155 info.block_size); 156 157 aux_info.default_i_flags = EXT4_NOATIME_FL; 158 159 u32 last_group_size = aux_info.len_blocks % info.blocks_per_group; 160 u32 last_header_size = 2 + aux_info.inode_table_blocks; 161 if (ext4_bg_has_super_block(aux_info.groups - 1)) 162 last_header_size += 1 + aux_info.bg_desc_blocks + 163 info.bg_desc_reserve_blocks; 164 if (last_group_size > 0 && last_group_size < last_header_size) { 165 aux_info.groups--; 166 aux_info.len_blocks -= last_group_size; 167 } 168 169 aux_info.sb = calloc(info.block_size, 1); 170 if (!aux_info.sb) 171 critical_error_errno("calloc"); 172 173 aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks); 174 if (!aux_info.bg_desc) 175 critical_error_errno("calloc"); 176} 177 178void ext4_free_fs_aux_info() 179{ 180 free(aux_info.sb); 181 free(aux_info.bg_desc); 182} 183 184/* Fill in the superblock memory buffer based on the filesystem parameters */ 185void ext4_fill_in_sb() 186{ 187 unsigned int i; 188 struct ext4_super_block *sb = aux_info.sb; 189 190 sb->s_inodes_count = info.inodes_per_group * aux_info.groups; 191 sb->s_blocks_count_lo = aux_info.len_blocks; 192 sb->s_r_blocks_count_lo = 0; 193 sb->s_free_blocks_count_lo = 0; 194 sb->s_free_inodes_count = 0; 195 sb->s_first_data_block = aux_info.first_data_block; 196 sb->s_log_block_size = log_2(info.block_size / 1024); 197 sb->s_obso_log_frag_size = log_2(info.block_size / 1024); 198 sb->s_blocks_per_group = info.blocks_per_group; 199 sb->s_obso_frags_per_group = info.blocks_per_group; 200 sb->s_inodes_per_group = info.inodes_per_group; 201 sb->s_mtime = 0; 202 sb->s_wtime = 0; 203 sb->s_mnt_count = 0; 204 sb->s_max_mnt_count = 0xFFFF; 205 sb->s_magic = EXT4_SUPER_MAGIC; 206 sb->s_state = EXT4_VALID_FS; 207 sb->s_errors = EXT4_ERRORS_RO; 208 sb->s_minor_rev_level = 0; 209 sb->s_lastcheck = 0; 210 sb->s_checkinterval = 0; 211 sb->s_creator_os = EXT4_OS_LINUX; 212 sb->s_rev_level = EXT4_DYNAMIC_REV; 213 sb->s_def_resuid = EXT4_DEF_RESUID; 214 sb->s_def_resgid = EXT4_DEF_RESGID; 215 216 sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 217 sb->s_inode_size = info.inode_size; 218 sb->s_block_group_nr = 0; 219 sb->s_feature_compat = info.feat_compat; 220 sb->s_feature_incompat = info.feat_incompat; 221 sb->s_feature_ro_compat = info.feat_ro_compat; 222 generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid); 223 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); 224 strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name)); 225 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); 226 sb->s_algorithm_usage_bitmap = 0; 227 228 sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks; 229 sb->s_prealloc_blocks = 0; 230 sb->s_prealloc_dir_blocks = 0; 231 232 //memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid)); 233 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) 234 sb->s_journal_inum = EXT4_JOURNAL_INO; 235 sb->s_journal_dev = 0; 236 sb->s_last_orphan = 0; 237 sb->s_hash_seed[0] = 0; /* FIXME */ 238 sb->s_def_hash_version = DX_HASH_TEA; 239 sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS; 240 sb->s_desc_size = sizeof(struct ext2_group_desc); 241 sb->s_default_mount_opts = 0; /* FIXME */ 242 sb->s_first_meta_bg = 0; 243 sb->s_mkfs_time = 0; 244 //sb->s_jnl_blocks[17]; /* FIXME */ 245 246 sb->s_blocks_count_hi = aux_info.len_blocks >> 32; 247 sb->s_r_blocks_count_hi = 0; 248 sb->s_free_blocks_count_hi = 0; 249 sb->s_min_extra_isize = sizeof(struct ext4_inode) - 250 EXT4_GOOD_OLD_INODE_SIZE; 251 sb->s_want_extra_isize = sizeof(struct ext4_inode) - 252 EXT4_GOOD_OLD_INODE_SIZE; 253 sb->s_flags = 0; 254 sb->s_raid_stride = 0; 255 sb->s_mmp_interval = 0; 256 sb->s_mmp_block = 0; 257 sb->s_raid_stripe_width = 0; 258 sb->s_log_groups_per_flex = 0; 259 sb->s_kbytes_written = 0; 260 261 for (i = 0; i < aux_info.groups; i++) { 262 u64 group_start_block = aux_info.first_data_block + i * 263 info.blocks_per_group; 264 u32 header_size = 0; 265 if (ext4_bg_has_super_block(i)) { 266 if (i != 0) 267 queue_data_block((u8 *)sb, info.block_size, group_start_block); 268 queue_data_block((u8 *)aux_info.bg_desc, 269 aux_info.bg_desc_blocks * info.block_size, 270 group_start_block + 1); 271 header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks; 272 } 273 274 aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size; 275 aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1; 276 aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2; 277 278 aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group; 279 aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group; 280 aux_info.bg_desc[i].bg_used_dirs_count = 0; 281 } 282} 283 284void ext4_queue_sb(void) 285{ 286 /* The write_data* functions expect only block aligned calls. 287 * This is not an issue, except when we write out the super 288 * block on a system with a block size > 1K. So, we need to 289 * deal with that here. 290 */ 291 if (info.block_size > 1024) { 292 u8 *buf = calloc(info.block_size, 1); 293 memcpy(buf + 1024, (u8*)aux_info.sb, 1024); 294 queue_data_block(buf, info.block_size, 0); 295 } else { 296 queue_data_block((u8*)aux_info.sb, 1024, 1); 297 } 298} 299 300void ext4_parse_sb(struct ext4_super_block *sb) 301{ 302 if (sb->s_magic != EXT4_SUPER_MAGIC) 303 error("superblock magic incorrect"); 304 305 if (sb->s_state != EXT4_VALID_FS) 306 error("filesystem state not valid"); 307 308 info.block_size = 1024 << sb->s_log_block_size; 309 info.blocks_per_group = sb->s_blocks_per_group; 310 info.inodes_per_group = sb->s_inodes_per_group; 311 info.inode_size = sb->s_inode_size; 312 info.inodes = sb->s_inodes_count; 313 info.feat_ro_compat = sb->s_feature_ro_compat; 314 info.feat_compat = sb->s_feature_compat; 315 info.feat_incompat = sb->s_feature_incompat; 316 info.bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks; 317 info.label = sb->s_volume_name; 318 319 aux_info.len_blocks = ((u64)sb->s_blocks_count_hi << 32) + 320 sb->s_blocks_count_lo; 321 info.len = (u64)info.block_size * aux_info.len_blocks; 322 323 ext4_create_fs_aux_info(); 324 325 memcpy(aux_info.sb, sb, sizeof(*sb)); 326 327 if (aux_info.first_data_block != sb->s_first_data_block) 328 critical_error("first data block does not match"); 329} 330 331void ext4_create_resize_inode() 332{ 333 struct block_allocation *reserve_inode_alloc = create_allocation(); 334 u32 reserve_inode_len = 0; 335 unsigned int i; 336 337 struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO); 338 if (inode == NULL) { 339 error("failed to get resize inode"); 340 return; 341 } 342 343 for (i = 0; i < aux_info.groups; i++) { 344 if (ext4_bg_has_super_block(i)) { 345 u64 group_start_block = aux_info.first_data_block + i * 346 info.blocks_per_group; 347 u32 reserved_block_start = group_start_block + 1 + 348 aux_info.bg_desc_blocks; 349 u32 reserved_block_len = info.bg_desc_reserve_blocks; 350 append_region(reserve_inode_alloc, reserved_block_start, 351 reserved_block_len, i); 352 reserve_inode_len += reserved_block_len; 353 } 354 } 355 356 inode_attach_resize(inode, reserve_inode_alloc); 357 358 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 359 inode->i_links_count = 1; 360 361 free_alloc(reserve_inode_alloc); 362} 363 364/* Allocate the blocks to hold a journal inode and connect them to the 365 reserved journal inode */ 366void ext4_create_journal_inode() 367{ 368 struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO); 369 if (inode == NULL) { 370 error("failed to get journal inode"); 371 return; 372 } 373 374 u8 *journal_data = inode_allocate_data_extents(inode, 375 info.journal_blocks * info.block_size, 376 info.journal_blocks * info.block_size); 377 if (!journal_data) { 378 error("failed to allocate extents for journal data"); 379 return; 380 } 381 382 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 383 inode->i_links_count = 1; 384 385 journal_superblock_t *jsb = (journal_superblock_t *)journal_data; 386 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER); 387 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2); 388 jsb->s_blocksize = htonl(info.block_size); 389 jsb->s_maxlen = htonl(info.journal_blocks); 390 jsb->s_nr_users = htonl(1); 391 jsb->s_first = htonl(1); 392 jsb->s_sequence = htonl(1); 393 394 memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block)); 395} 396 397/* Update the number of free blocks and inodes in the filesystem and in each 398 block group */ 399void ext4_update_free() 400{ 401 unsigned int i; 402 403 for (i = 0; i < aux_info.groups; i++) { 404 u32 bg_free_blocks = get_free_blocks(i); 405 u32 bg_free_inodes = get_free_inodes(i); 406 407 aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks; 408 aux_info.sb->s_free_blocks_count_lo += bg_free_blocks; 409 410 aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes; 411 aux_info.sb->s_free_inodes_count += bg_free_inodes; 412 413 aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i); 414 } 415} 416 417static u64 get_block_device_size(const char *filename) 418{ 419 int fd = open(filename, O_RDONLY); 420 u64 size = 0; 421 int ret; 422 423 if (fd < 0) 424 return 0; 425 426#if defined(__linux__) 427 ret = ioctl(fd, BLKGETSIZE64, &size); 428#elif defined(__APPLE__) && defined(__MACH__) 429 ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); 430#else 431 return 0; 432#endif 433 434 close(fd); 435 436 if (ret) 437 return 0; 438 439 return size; 440} 441 442u64 get_file_size(const char *filename) 443{ 444 struct stat buf; 445 int ret; 446 u64 reserve_len = 0; 447 s64 computed_size; 448 449 ret = stat(filename, &buf); 450 if (ret) 451 return 0; 452 453 if (info.len < 0) 454 reserve_len = -info.len; 455 456 if (S_ISREG(buf.st_mode)) 457 computed_size = buf.st_size - reserve_len; 458 else if (S_ISBLK(buf.st_mode)) 459 computed_size = get_block_device_size(filename) - reserve_len; 460 else 461 computed_size = 0; 462 463 if (computed_size < 0) { 464 warn("Computed filesystem size less than 0"); 465 computed_size = 0; 466 } 467 468 return computed_size; 469} 470 471u64 parse_num(const char *arg) 472{ 473 char *endptr; 474 u64 num = strtoull(arg, &endptr, 10); 475 if (*endptr == 'k' || *endptr == 'K') 476 num *= 1024LL; 477 else if (*endptr == 'm' || *endptr == 'M') 478 num *= 1024LL * 1024LL; 479 else if (*endptr == 'g' || *endptr == 'G') 480 num *= 1024LL * 1024LL * 1024LL; 481 482 return num; 483} 484 485