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