ext4_utils.c revision 2ae7663e1e064000356ee9e49ebd08bdddc545f9
18642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* 28642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * Copyright (C) 2010 The Android Open Source Project 38642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * 48642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * Licensed under the Apache License, Version 2.0 (the "License"); 58642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * you may not use this file except in compliance with the License. 68642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * You may obtain a copy of the License at 78642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * 88642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * http://www.apache.org/licenses/LICENSE-2.0 98642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * 108642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * Unless required by applicable law or agreed to in writing, software 118642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * distributed under the License is distributed on an "AS IS" BASIS, 128642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * See the License for the specific language governing permissions and 148642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland * limitations under the License. 158642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland */ 168642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1733f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "ext4_utils.h" 1833f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "output_file.h" 1933f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "backed_block.h" 2033f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "uuid.h" 2133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "allocate.h" 2233f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "indirect.h" 2333f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "extent.h" 2433f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross 258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <fcntl.h> 268642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <arpa/inet.h> 278642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <sys/ioctl.h> 288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <sys/stat.h> 298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <sys/types.h> 308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <string.h> 318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#if defined(__linux__) 338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <linux/fs.h> 348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#elif defined(__APPLE__) && defined(__MACH__) 358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include <sys/disk.h> 368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#endif 378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include "ext4.h" 398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#include "jbd2.h" 408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandint force = 0; 428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstruct fs_info info; 438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstruct fs_aux_info aux_info; 448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 452ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumralljmp_buf setjmp_env; 462ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall 478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* returns 1 if a is a power of b */ 488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstatic int is_power_of(int a, int b) 498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland while (a > b) { 518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (a % b) 528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland a /= b; 548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return (a == b) ? 1 : 0; 578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Returns 1 if the bg contains a backup superblock. On filesystems with 608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland the sparse_super feature, only block groups 0, 1, and powers of 3, 5, 618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland and 7 have backup superblocks. Otherwise, all block groups have backup 628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland superblocks */ 638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandint ext4_bg_has_super_block(int bg) 648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland /* Without sparse_super, every block group has a superblock */ 668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) 678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 1; 688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (bg == 0 || bg == 1) 708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 1; 718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) 738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 1; 748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 78b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstruct count_chunks { 79b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross u32 chunks; 80b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross u64 cur_ptr; 81b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}; 82b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 83b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid count_data_block(void *priv, u64 off, u8 *data, int len) 84b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 85b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross struct count_chunks *count_chunks = priv; 86b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross if (off > count_chunks->cur_ptr) 87b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->chunks++; 88b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->cur_ptr = off + ALIGN(len, info.block_size); 89b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->chunks++; 90b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 91b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 92b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid count_file_block(void *priv, u64 off, const char *file, 93b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross off64_t offset, int len) 94b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 95b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross struct count_chunks *count_chunks = priv; 96b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross if (off > count_chunks->cur_ptr) 97b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->chunks++; 98b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->cur_ptr = off + ALIGN(len, info.block_size); 99b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks->chunks++; 100b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 101b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 102b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossint count_sparse_chunks() 103b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 104b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross struct count_chunks count_chunks = {0, 0}; 105b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 106b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross for_each_data_block(count_data_block, count_file_block, &count_chunks); 107b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 108435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall if (count_chunks.cur_ptr != (u64) info.len) 109b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross count_chunks.chunks++; 110b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 111b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross return count_chunks.chunks; 112b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 113b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 114b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstatic void ext4_write_data_block(void *priv, u64 off, u8 *data, int len) 115b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 116b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross write_data_block(priv, off, data, len); 117b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 118b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstatic void ext4_write_data_file(void *priv, u64 off, const char *file, 119b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross off64_t offset, int len) 120b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 121b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross write_data_file(priv, off, file, offset, len); 122b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 123b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 1248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Write the filesystem image to a file */ 125c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Crossvoid write_ext4_image(const char *filename, int gz, int sparse, int crc, 126c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross int wipe) 1278642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 1288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland int ret = 0; 129b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross struct output_file *out = open_output_file(filename, gz, sparse, 130c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross count_sparse_chunks(), crc, wipe); 1318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (!out) 1338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return; 1348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 135b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross for_each_data_block(ext4_write_data_block, ext4_write_data_file, out); 1368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland pad_output_file(out, info.len); 1388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland close_output_file(out); 1408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 1418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Compute the rest of the parameters of the filesystem from the basic info */ 1438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_fs_aux_info() 1448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 1458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; 1468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.len_blocks = info.len / info.block_size; 1478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size, 1488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.block_size); 1498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, 1508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.blocks_per_group); 1518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.blocks_per_ind = info.block_size / sizeof(u32); 1528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; 1538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; 1548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc_blocks = 1568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc), 1578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.block_size); 1588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.default_i_flags = EXT4_NOATIME_FL; 1608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 last_group_size = aux_info.len_blocks % info.blocks_per_group; 1628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 last_header_size = 2 + aux_info.inode_table_blocks; 1638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (ext4_bg_has_super_block(aux_info.groups - 1)) 1648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland last_header_size += 1 + aux_info.bg_desc_blocks + 16522742ce739a046a079b2e1b03342a25472dfa352Colin Cross info.bg_desc_reserve_blocks; 1668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (last_group_size > 0 && last_group_size < last_header_size) { 1678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.groups--; 1688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.len_blocks -= last_group_size; 1698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 1708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.sb = calloc(info.block_size, 1); 1728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (!aux_info.sb) 1738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland critical_error_errno("calloc"); 1748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks); 1768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (!aux_info.bg_desc) 1778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland critical_error_errno("calloc"); 1788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 1798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_free_fs_aux_info() 1818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 1828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland free(aux_info.sb); 1838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland free(aux_info.bg_desc); 1848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 1858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1868642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Fill in the superblock memory buffer based on the filesystem parameters */ 1878642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_fill_in_sb() 1888642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 1898642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland unsigned int i; 1908642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland struct ext4_super_block *sb = aux_info.sb; 1918642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 1928642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_inodes_count = info.inodes_per_group * aux_info.groups; 1938642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_blocks_count_lo = aux_info.len_blocks; 1948642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_r_blocks_count_lo = 0; 1958642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_free_blocks_count_lo = 0; 1968642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_free_inodes_count = 0; 1978642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_first_data_block = aux_info.first_data_block; 1988642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_log_block_size = log_2(info.block_size / 1024); 1998642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_obso_log_frag_size = log_2(info.block_size / 1024); 2008642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_blocks_per_group = info.blocks_per_group; 2018642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_obso_frags_per_group = info.blocks_per_group; 2028642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_inodes_per_group = info.inodes_per_group; 2038642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_mtime = 0; 2048642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_wtime = 0; 2058642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_mnt_count = 0; 2068642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_max_mnt_count = 0xFFFF; 2078642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_magic = EXT4_SUPER_MAGIC; 2088642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_state = EXT4_VALID_FS; 2098642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_errors = EXT4_ERRORS_RO; 2108642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_minor_rev_level = 0; 2118642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_lastcheck = 0; 2128642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_checkinterval = 0; 2138642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_creator_os = EXT4_OS_LINUX; 2148642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_rev_level = EXT4_DYNAMIC_REV; 2158642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_def_resuid = EXT4_DEF_RESUID; 2168642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_def_resgid = EXT4_DEF_RESGID; 2178642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2188642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 2198642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_inode_size = info.inode_size; 2208642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_block_group_nr = 0; 2218642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_feature_compat = info.feat_compat; 2228642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_feature_incompat = info.feat_incompat; 2238642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_feature_ro_compat = info.feat_ro_compat; 2248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid); 2258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); 2268642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name)); 2278642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); 2288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_algorithm_usage_bitmap = 0; 2298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 23022742ce739a046a079b2e1b03342a25472dfa352Colin Cross sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks; 2318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_prealloc_blocks = 0; 2328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_prealloc_dir_blocks = 0; 2338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland //memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid)); 2358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) 2368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_journal_inum = EXT4_JOURNAL_INO; 2378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_journal_dev = 0; 2388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_last_orphan = 0; 2398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_hash_seed[0] = 0; /* FIXME */ 2408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_def_hash_version = DX_HASH_TEA; 2418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS; 2428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_desc_size = sizeof(struct ext2_group_desc); 2438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_default_mount_opts = 0; /* FIXME */ 2448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_first_meta_bg = 0; 2458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_mkfs_time = 0; 2468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland //sb->s_jnl_blocks[17]; /* FIXME */ 2478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_blocks_count_hi = aux_info.len_blocks >> 32; 2498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_r_blocks_count_hi = 0; 2508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_free_blocks_count_hi = 0; 2518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_min_extra_isize = sizeof(struct ext4_inode) - 2528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland EXT4_GOOD_OLD_INODE_SIZE; 2538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_want_extra_isize = sizeof(struct ext4_inode) - 2548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland EXT4_GOOD_OLD_INODE_SIZE; 2558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_flags = 0; 2568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_raid_stride = 0; 2578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_mmp_interval = 0; 2588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_mmp_block = 0; 2598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_raid_stripe_width = 0; 2608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_log_groups_per_flex = 0; 2618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland sb->s_kbytes_written = 0; 2628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland for (i = 0; i < aux_info.groups; i++) { 2648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u64 group_start_block = aux_info.first_data_block + i * 2658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.blocks_per_group; 2668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 header_size = 0; 2678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (ext4_bg_has_super_block(i)) { 268b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross if (i != 0) 2698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland queue_data_block((u8 *)sb, info.block_size, group_start_block); 270b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross queue_data_block((u8 *)aux_info.bg_desc, 271b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross aux_info.bg_desc_blocks * info.block_size, 272b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross group_start_block + 1); 27322742ce739a046a079b2e1b03342a25472dfa352Colin Cross header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks; 2748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 2758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size; 2778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1; 2788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2; 2798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 2808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group; 2818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group; 2828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_used_dirs_count = 0; 2838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 2848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 2858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 286b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid ext4_queue_sb(void) 287b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{ 288b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross /* The write_data* functions expect only block aligned calls. 289b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross * This is not an issue, except when we write out the super 290b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross * block on a system with a block size > 1K. So, we need to 291b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross * deal with that here. 292b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross */ 293b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross if (info.block_size > 1024) { 294b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross u8 *buf = calloc(info.block_size, 1); 295b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross memcpy(buf + 1024, (u8*)aux_info.sb, 1024); 296b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross queue_data_block(buf, info.block_size, 0); 297b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross } else { 298b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross queue_data_block((u8*)aux_info.sb, 1024, 1); 299b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross } 300b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross} 301b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross 302a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Crossvoid ext4_parse_sb(struct ext4_super_block *sb) 303a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross{ 304a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross if (sb->s_magic != EXT4_SUPER_MAGIC) 305a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross error("superblock magic incorrect"); 306a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 307a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross if (sb->s_state != EXT4_VALID_FS) 308a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross error("filesystem state not valid"); 309a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 310a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.block_size = 1024 << sb->s_log_block_size; 311a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.blocks_per_group = sb->s_blocks_per_group; 312a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.inodes_per_group = sb->s_inodes_per_group; 313a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.inode_size = sb->s_inode_size; 314a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.inodes = sb->s_inodes_count; 315a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.feat_ro_compat = sb->s_feature_ro_compat; 316a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.feat_compat = sb->s_feature_compat; 317a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.feat_incompat = sb->s_feature_incompat; 318a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks; 319a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.label = sb->s_volume_name; 320a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 321a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross aux_info.len_blocks = ((u64)sb->s_blocks_count_hi << 32) + 322a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross sb->s_blocks_count_lo; 323a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross info.len = (u64)info.block_size * aux_info.len_blocks; 324a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 325a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross ext4_create_fs_aux_info(); 326a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 327a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross memcpy(aux_info.sb, sb, sizeof(*sb)); 328a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 329a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross if (aux_info.first_data_block != sb->s_first_data_block) 330a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross critical_error("first data block does not match"); 331a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross} 332a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross 3338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_resize_inode() 3348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 3358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland struct block_allocation *reserve_inode_alloc = create_allocation(); 3368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 reserve_inode_len = 0; 3378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland unsigned int i; 3388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO); 3408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (inode == NULL) { 3418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland error("failed to get resize inode"); 3428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return; 3438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 3448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland for (i = 0; i < aux_info.groups; i++) { 3468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (ext4_bg_has_super_block(i)) { 3478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u64 group_start_block = aux_info.first_data_block + i * 3488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.blocks_per_group; 3498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 reserved_block_start = group_start_block + 1 + 3508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc_blocks; 35122742ce739a046a079b2e1b03342a25472dfa352Colin Cross u32 reserved_block_len = info.bg_desc_reserve_blocks; 3528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland append_region(reserve_inode_alloc, reserved_block_start, 3538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland reserved_block_len, i); 3548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland reserve_inode_len += reserved_block_len; 3558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 3568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 3578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland inode_attach_resize(inode, reserve_inode_alloc); 3598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 3618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland inode->i_links_count = 1; 3628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland free_alloc(reserve_inode_alloc); 3648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 3658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Allocate the blocks to hold a journal inode and connect them to the 3678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland reserved journal inode */ 3688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_journal_inode() 3698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 3708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO); 3718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (inode == NULL) { 3728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland error("failed to get journal inode"); 3738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return; 3748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 3758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u8 *journal_data = inode_allocate_data_extents(inode, 3778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland info.journal_blocks * info.block_size, 3784b8b59a7cd0e1413dcf9973c4bbd7eb65a444c4cBrian Swetland info.journal_blocks * info.block_size); 3798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (!journal_data) { 3808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland error("failed to allocate extents for journal data"); 3818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return; 3828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 3838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; 3858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland inode->i_links_count = 1; 3868642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3878642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland journal_superblock_t *jsb = (journal_superblock_t *)journal_data; 3888642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER); 3898642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2); 3908642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_blocksize = htonl(info.block_size); 3918642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_maxlen = htonl(info.journal_blocks); 3928642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_nr_users = htonl(1); 3938642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_first = htonl(1); 3948642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland jsb->s_sequence = htonl(1); 3958642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3968642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block)); 3978642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 3988642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 3998642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Update the number of free blocks and inodes in the filesystem and in each 4008642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland block group */ 4018642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_update_free() 4028642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 4038642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland unsigned int i; 4048642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4058642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland for (i = 0; i < aux_info.groups; i++) { 4068642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 bg_free_blocks = get_free_blocks(i); 4078642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u32 bg_free_inodes = get_free_inodes(i); 4088642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4098642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks; 4108642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.sb->s_free_blocks_count_lo += bg_free_blocks; 4118642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4128642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes; 4138642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.sb->s_free_inodes_count += bg_free_inodes; 4148642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4158642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i); 4168642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland } 4178642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 4188642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4198642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstatic u64 get_block_device_size(const char *filename) 4208642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 4218642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland int fd = open(filename, O_RDONLY); 4228642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u64 size = 0; 4238642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland int ret; 4248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (fd < 0) 4268642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 4278642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#if defined(__linux__) 4298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland ret = ioctl(fd, BLKGETSIZE64, &size); 4308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#elif defined(__APPLE__) && defined(__MACH__) 4318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); 4328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#else 4338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 4348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#endif 4358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland close(fd); 4378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (ret) 4398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 4408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return size; 4428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 4438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandu64 get_file_size(const char *filename) 4458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 4468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland struct stat buf; 4478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland int ret; 448435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall u64 reserve_len = 0; 449435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall s64 computed_size; 4508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland ret = stat(filename, &buf); 4528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (ret) 4538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return 0; 4548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 455435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall if (info.len < 0) 456435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall reserve_len = -info.len; 457435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall 4588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (S_ISREG(buf.st_mode)) 459435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall computed_size = buf.st_size - reserve_len; 4608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland else if (S_ISBLK(buf.st_mode)) 461435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall computed_size = get_block_device_size(filename) - reserve_len; 4628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland else 463435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall computed_size = 0; 464435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall 465435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall if (computed_size < 0) { 466435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall warn("Computed filesystem size less than 0"); 467435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall computed_size = 0; 468435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall } 469435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall 470435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall return computed_size; 4718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 4728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandu64 parse_num(const char *arg) 4748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{ 4758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland char *endptr; 4768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland u64 num = strtoull(arg, &endptr, 10); 4778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland if (*endptr == 'k' || *endptr == 'K') 4788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland num *= 1024LL; 4798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland else if (*endptr == 'm' || *endptr == 'M') 4808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland num *= 1024LL * 1024LL; 4818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland else if (*endptr == 'g' || *endptr == 'G') 4828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland num *= 1024LL * 1024LL * 1024LL; 4838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 4848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland return num; 4858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland} 4868642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland 487