ext4_utils.c revision 33f96c66e9a1f2e266a75e5e84c091dffa6ef118
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 76/* Write the filesystem image to a file */ 77void write_ext4_image(const char *filename, int gz, int sparse) 78{ 79 int ret = 0; 80 struct output_file *out = open_output_file(filename, gz, sparse); 81 82 if (!out) 83 return; 84 85 /* The write_data* functions expect only block aligned calls. 86 * This is not an issue, except when we write out the super 87 * block on a system with a block size > 1K. So, we need to 88 * deal with that here. 89 */ 90 if (info.block_size > 1024) { 91 u8 buf[4096] = { 0 }; /* The larget supported ext4 block size */ 92 memcpy(buf + 1024, (u8*)aux_info.sb, 1024); 93 write_data_block(out, 0, buf, info.block_size); 94 95 } else { 96 write_data_block(out, 1024, (u8*)aux_info.sb, 1024); 97 } 98 99 write_data_block(out, (u64)(aux_info.first_data_block + 1) * info.block_size, 100 (u8*)aux_info.bg_desc, 101 aux_info.bg_desc_blocks * info.block_size); 102 103 for_each_data_block(write_data_block, write_data_file, out); 104 105 pad_output_file(out, info.len); 106 107 close_output_file(out); 108} 109 110/* Compute the rest of the parameters of the filesystem from the basic info */ 111void ext4_create_fs_aux_info() 112{ 113 aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; 114 aux_info.len_blocks = info.len / info.block_size; 115 aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size, 116 info.block_size); 117 aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, 118 info.blocks_per_group); 119 aux_info.blocks_per_ind = info.block_size / sizeof(u32); 120 aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; 121 aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; 122 123 aux_info.bg_desc_blocks = 124 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc), 125 info.block_size); 126 127 aux_info.default_i_flags = EXT4_NOATIME_FL; 128 129 u32 last_group_size = aux_info.len_blocks % info.blocks_per_group; 130 u32 last_header_size = 2 + aux_info.inode_table_blocks; 131 if (ext4_bg_has_super_block(aux_info.groups - 1)) 132 last_header_size += 1 + aux_info.bg_desc_blocks + 133 info.bg_desc_reserve_blocks; 134 if (last_group_size > 0 && last_group_size < last_header_size) { 135 aux_info.groups--; 136 aux_info.len_blocks -= last_group_size; 137 } 138 139 aux_info.sb = calloc(info.block_size, 1); 140 if (!aux_info.sb) 141 critical_error_errno("calloc"); 142 143 aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks); 144 if (!aux_info.bg_desc) 145 critical_error_errno("calloc"); 146} 147 148void ext4_free_fs_aux_info() 149{ 150 free(aux_info.sb); 151 free(aux_info.bg_desc); 152} 153 154/* Fill in the superblock memory buffer based on the filesystem parameters */ 155void ext4_fill_in_sb() 156{ 157 unsigned int i; 158 struct ext4_super_block *sb = aux_info.sb; 159 160 sb->s_inodes_count = info.inodes_per_group * aux_info.groups; 161 sb->s_blocks_count_lo = aux_info.len_blocks; 162 sb->s_r_blocks_count_lo = 0; 163 sb->s_free_blocks_count_lo = 0; 164 sb->s_free_inodes_count = 0; 165 sb->s_first_data_block = aux_info.first_data_block; 166 sb->s_log_block_size = log_2(info.block_size / 1024); 167 sb->s_obso_log_frag_size = log_2(info.block_size / 1024); 168 sb->s_blocks_per_group = info.blocks_per_group; 169 sb->s_obso_frags_per_group = info.blocks_per_group; 170 sb->s_inodes_per_group = info.inodes_per_group; 171 sb->s_mtime = 0; 172 sb->s_wtime = 0; 173 sb->s_mnt_count = 0; 174 sb->s_max_mnt_count = 0xFFFF; 175 sb->s_magic = EXT4_SUPER_MAGIC; 176 sb->s_state = EXT4_VALID_FS; 177 sb->s_errors = EXT4_ERRORS_RO; 178 sb->s_minor_rev_level = 0; 179 sb->s_lastcheck = 0; 180 sb->s_checkinterval = 0; 181 sb->s_creator_os = EXT4_OS_LINUX; 182 sb->s_rev_level = EXT4_DYNAMIC_REV; 183 sb->s_def_resuid = EXT4_DEF_RESUID; 184 sb->s_def_resgid = EXT4_DEF_RESGID; 185 186 sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 187 sb->s_inode_size = info.inode_size; 188 sb->s_block_group_nr = 0; 189 sb->s_feature_compat = info.feat_compat; 190 sb->s_feature_incompat = info.feat_incompat; 191 sb->s_feature_ro_compat = info.feat_ro_compat; 192 generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid); 193 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); 194 strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name)); 195 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); 196 sb->s_algorithm_usage_bitmap = 0; 197 198 sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks; 199 sb->s_prealloc_blocks = 0; 200 sb->s_prealloc_dir_blocks = 0; 201 202 //memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid)); 203 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) 204 sb->s_journal_inum = EXT4_JOURNAL_INO; 205 sb->s_journal_dev = 0; 206 sb->s_last_orphan = 0; 207 sb->s_hash_seed[0] = 0; /* FIXME */ 208 sb->s_def_hash_version = DX_HASH_TEA; 209 sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS; 210 sb->s_desc_size = sizeof(struct ext2_group_desc); 211 sb->s_default_mount_opts = 0; /* FIXME */ 212 sb->s_first_meta_bg = 0; 213 sb->s_mkfs_time = 0; 214 //sb->s_jnl_blocks[17]; /* FIXME */ 215 216 sb->s_blocks_count_hi = aux_info.len_blocks >> 32; 217 sb->s_r_blocks_count_hi = 0; 218 sb->s_free_blocks_count_hi = 0; 219 sb->s_min_extra_isize = sizeof(struct ext4_inode) - 220 EXT4_GOOD_OLD_INODE_SIZE; 221 sb->s_want_extra_isize = sizeof(struct ext4_inode) - 222 EXT4_GOOD_OLD_INODE_SIZE; 223 sb->s_flags = 0; 224 sb->s_raid_stride = 0; 225 sb->s_mmp_interval = 0; 226 sb->s_mmp_block = 0; 227 sb->s_raid_stripe_width = 0; 228 sb->s_log_groups_per_flex = 0; 229 sb->s_kbytes_written = 0; 230 231 for (i = 0; i < aux_info.groups; i++) { 232 u64 group_start_block = aux_info.first_data_block + i * 233 info.blocks_per_group; 234 u32 header_size = 0; 235 if (ext4_bg_has_super_block(i)) { 236 if (i != 0) { 237 queue_data_block((u8 *)sb, info.block_size, group_start_block); 238 queue_data_block((u8 *)aux_info.bg_desc, 239 aux_info.bg_desc_blocks * info.block_size, 240 group_start_block + 1); 241 } 242 header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks; 243 } 244 245 aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size; 246 aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1; 247 aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2; 248 249 aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group; 250 aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group; 251 aux_info.bg_desc[i].bg_used_dirs_count = 0; 252 } 253} 254 255void ext4_create_resize_inode() 256{ 257 struct block_allocation *reserve_inode_alloc = create_allocation(); 258 u32 reserve_inode_len = 0; 259 unsigned int i; 260 261 struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO); 262 if (inode == NULL) { 263 error("failed to get resize inode"); 264 return; 265 } 266 267 for (i = 0; i < aux_info.groups; i++) { 268 if (ext4_bg_has_super_block(i)) { 269 u64 group_start_block = aux_info.first_data_block + i * 270 info.blocks_per_group; 271 u32 reserved_block_start = group_start_block + 1 + 272 aux_info.bg_desc_blocks; 273 u32 reserved_block_len = info.bg_desc_reserve_blocks; 274 append_region(reserve_inode_alloc, reserved_block_start, 275 reserved_block_len, i); 276 reserve_inode_len += reserved_block_len; 277 } 278 } 279 280 inode_attach_resize(inode, reserve_inode_alloc); 281 282 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 283 inode->i_links_count = 1; 284 285 free_alloc(reserve_inode_alloc); 286} 287 288/* Allocate the blocks to hold a journal inode and connect them to the 289 reserved journal inode */ 290void ext4_create_journal_inode() 291{ 292 struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO); 293 if (inode == NULL) { 294 error("failed to get journal inode"); 295 return; 296 } 297 298 u8 *journal_data = inode_allocate_data_extents(inode, 299 info.journal_blocks * info.block_size, 300 info.journal_blocks * info.block_size); 301 if (!journal_data) { 302 error("failed to allocate extents for journal data"); 303 return; 304 } 305 306 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 307 inode->i_links_count = 1; 308 309 journal_superblock_t *jsb = (journal_superblock_t *)journal_data; 310 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER); 311 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2); 312 jsb->s_blocksize = htonl(info.block_size); 313 jsb->s_maxlen = htonl(info.journal_blocks); 314 jsb->s_nr_users = htonl(1); 315 jsb->s_first = htonl(1); 316 jsb->s_sequence = htonl(1); 317 318 memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block)); 319} 320 321/* Update the number of free blocks and inodes in the filesystem and in each 322 block group */ 323void ext4_update_free() 324{ 325 unsigned int i; 326 327 for (i = 0; i < aux_info.groups; i++) { 328 u32 bg_free_blocks = get_free_blocks(i); 329 u32 bg_free_inodes = get_free_inodes(i); 330 331 aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks; 332 aux_info.sb->s_free_blocks_count_lo += bg_free_blocks; 333 334 aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes; 335 aux_info.sb->s_free_inodes_count += bg_free_inodes; 336 337 aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i); 338 } 339} 340 341static u64 get_block_device_size(const char *filename) 342{ 343 int fd = open(filename, O_RDONLY); 344 u64 size = 0; 345 int ret; 346 347 if (fd < 0) 348 return 0; 349 350#if defined(__linux__) 351 ret = ioctl(fd, BLKGETSIZE64, &size); 352#elif defined(__APPLE__) && defined(__MACH__) 353 ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); 354#else 355 return 0; 356#endif 357 358 close(fd); 359 360 if (ret) 361 return 0; 362 363 return size; 364} 365 366u64 get_file_size(const char *filename) 367{ 368 struct stat buf; 369 int ret; 370 371 ret = stat(filename, &buf); 372 if (ret) 373 return 0; 374 375 if (S_ISREG(buf.st_mode)) 376 return buf.st_size; 377 else if (S_ISBLK(buf.st_mode)) 378 return get_block_device_size(filename); 379 else 380 return 0; 381} 382 383u64 parse_num(const char *arg) 384{ 385 char *endptr; 386 u64 num = strtoull(arg, &endptr, 10); 387 if (*endptr == 'k' || *endptr == 'K') 388 num *= 1024LL; 389 else if (*endptr == 'm' || *endptr == 'M') 390 num *= 1024LL * 1024LL; 391 else if (*endptr == 'g' || *endptr == 'G') 392 num *= 1024LL * 1024LL * 1024LL; 393 394 return num; 395} 396 397