ext4_utils.c revision a7ed433f2dc0116627a93b18fbb260f0665ca0cb
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
458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* returns 1 if a is a power of b */
468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstatic int is_power_of(int a, int b)
478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	while (a > b) {
498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		if (a % b)
508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			return 0;
518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		a /= b;
528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	return (a == b) ? 1 : 0;
558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Returns 1 if the bg contains a backup superblock.  On filesystems with
588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland   the sparse_super feature, only block groups 0, 1, and powers of 3, 5,
598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland   and 7 have backup superblocks.  Otherwise, all block groups have backup
608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland   superblocks */
618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandint ext4_bg_has_super_block(int bg)
628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	/* Without sparse_super, every block group has a superblock */
648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 1;
668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (bg == 0 || bg == 1)
688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 1;
698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7))
718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 1;
728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	return 0;
748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
76b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstruct count_chunks {
77b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	u32 chunks;
78b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	u64 cur_ptr;
79b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross};
80b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
81b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid count_data_block(void *priv, u64 off, u8 *data, int len)
82b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
83b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	struct count_chunks *count_chunks = priv;
84b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	if (off > count_chunks->cur_ptr)
85b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		count_chunks->chunks++;
86b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
87b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	count_chunks->chunks++;
88b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
89b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
90b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid count_file_block(void *priv, u64 off, const char *file,
91b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		off64_t offset, int len)
92b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
93b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	struct count_chunks *count_chunks = priv;
94b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	if (off > count_chunks->cur_ptr)
95b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		count_chunks->chunks++;
96b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
97b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	count_chunks->chunks++;
98b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
99b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
100b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossint count_sparse_chunks()
101b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
102b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	struct count_chunks count_chunks = {0, 0};
103b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
104b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	for_each_data_block(count_data_block, count_file_block, &count_chunks);
105b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
106b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	if (count_chunks.cur_ptr != info.len)
107b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		count_chunks.chunks++;
108b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
109b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	return count_chunks.chunks;
110b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
111b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
112b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstatic void ext4_write_data_block(void *priv, u64 off, u8 *data, int len)
113b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
114b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	write_data_block(priv, off, data, len);
115b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
116b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossstatic void ext4_write_data_file(void *priv, u64 off, const char *file,
117b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		off64_t offset, int len)
118b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
119b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	write_data_file(priv, off, file, offset, len);
120b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
121b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
1228642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Write the filesystem image to a file */
123757ace516d8e4350616b5fd10da0c982d3d5ec74Colin Crossvoid write_ext4_image(const char *filename, int gz, int sparse, int crc)
1248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
1258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	int ret = 0;
126b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	struct output_file *out = open_output_file(filename, gz, sparse,
127757ace516d8e4350616b5fd10da0c982d3d5ec74Colin Cross	        count_sparse_chunks(), crc);
1288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (!out)
1308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return;
1318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
132b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	for_each_data_block(ext4_write_data_block, ext4_write_data_file, out);
1338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	pad_output_file(out, info.len);
1358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	close_output_file(out);
1378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
1388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Compute the rest of the parameters of the filesystem from the basic info */
1408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_fs_aux_info()
1418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
1428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
1438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.len_blocks = info.len / info.block_size;
1448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size,
1458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		info.block_size);
1468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block,
1478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		info.blocks_per_group);
1488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.blocks_per_ind = info.block_size / sizeof(u32);
1498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
1508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
1518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.bg_desc_blocks =
1538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc),
1548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			info.block_size);
1558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.default_i_flags = EXT4_NOATIME_FL;
1578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u32 last_group_size = aux_info.len_blocks % info.blocks_per_group;
1598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u32 last_header_size = 2 + aux_info.inode_table_blocks;
1608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (ext4_bg_has_super_block(aux_info.groups - 1))
1618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		last_header_size += 1 + aux_info.bg_desc_blocks +
16222742ce739a046a079b2e1b03342a25472dfa352Colin Cross			info.bg_desc_reserve_blocks;
1638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (last_group_size > 0 && last_group_size < last_header_size) {
1648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.groups--;
1658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.len_blocks -= last_group_size;
1668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
1678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.sb = calloc(info.block_size, 1);
1698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (!aux_info.sb)
1708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		critical_error_errno("calloc");
1718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks);
1738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (!aux_info.bg_desc)
1748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		critical_error_errno("calloc");
1758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
1768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_free_fs_aux_info()
1788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
1798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	free(aux_info.sb);
1808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	free(aux_info.bg_desc);
1818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
1828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Fill in the superblock memory buffer based on the filesystem parameters */
1848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_fill_in_sb()
1858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
1868642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	unsigned int i;
1878642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	struct ext4_super_block *sb = aux_info.sb;
1888642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
1898642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_inodes_count = info.inodes_per_group * aux_info.groups;
1908642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_blocks_count_lo = aux_info.len_blocks;
1918642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_r_blocks_count_lo = 0;
1928642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_free_blocks_count_lo = 0;
1938642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_free_inodes_count = 0;
1948642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_first_data_block = aux_info.first_data_block;
1958642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_log_block_size = log_2(info.block_size / 1024);
1968642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_obso_log_frag_size = log_2(info.block_size / 1024);
1978642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_blocks_per_group = info.blocks_per_group;
1988642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_obso_frags_per_group = info.blocks_per_group;
1998642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_inodes_per_group = info.inodes_per_group;
2008642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_mtime = 0;
2018642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_wtime = 0;
2028642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_mnt_count = 0;
2038642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_max_mnt_count = 0xFFFF;
2048642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_magic = EXT4_SUPER_MAGIC;
2058642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_state = EXT4_VALID_FS;
2068642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_errors = EXT4_ERRORS_RO;
2078642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_minor_rev_level = 0;
2088642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_lastcheck = 0;
2098642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_checkinterval = 0;
2108642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_creator_os = EXT4_OS_LINUX;
2118642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_rev_level = EXT4_DYNAMIC_REV;
2128642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_def_resuid = EXT4_DEF_RESUID;
2138642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_def_resgid = EXT4_DEF_RESGID;
2148642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2158642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2168642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_inode_size = info.inode_size;
2178642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_block_group_nr = 0;
2188642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_feature_compat = info.feat_compat;
2198642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_feature_incompat = info.feat_incompat;
2208642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_feature_ro_compat = info.feat_ro_compat;
2218642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid);
2228642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2238642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name));
2248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_algorithm_usage_bitmap = 0;
2268642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
22722742ce739a046a079b2e1b03342a25472dfa352Colin Cross	sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks;
2288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_prealloc_blocks = 0;
2298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_prealloc_dir_blocks = 0;
2308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	//memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid));
2328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
2338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		sb->s_journal_inum = EXT4_JOURNAL_INO;
2348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_journal_dev = 0;
2358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_last_orphan = 0;
2368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_hash_seed[0] = 0; /* FIXME */
2378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_def_hash_version = DX_HASH_TEA;
2388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS;
2398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_desc_size = sizeof(struct ext2_group_desc);
2408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_default_mount_opts = 0; /* FIXME */
2418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_first_meta_bg = 0;
2428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_mkfs_time = 0;
2438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	//sb->s_jnl_blocks[17]; /* FIXME */
2448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_blocks_count_hi = aux_info.len_blocks >> 32;
2468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_r_blocks_count_hi = 0;
2478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_free_blocks_count_hi = 0;
2488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_min_extra_isize = sizeof(struct ext4_inode) -
2498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		EXT4_GOOD_OLD_INODE_SIZE;
2508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_want_extra_isize = sizeof(struct ext4_inode) -
2518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		EXT4_GOOD_OLD_INODE_SIZE;
2528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_flags = 0;
2538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_raid_stride = 0;
2548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_mmp_interval = 0;
2558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_mmp_block = 0;
2568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_raid_stripe_width = 0;
2578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_log_groups_per_flex = 0;
2588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	sb->s_kbytes_written = 0;
2598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	for (i = 0; i < aux_info.groups; i++) {
2618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		u64 group_start_block = aux_info.first_data_block + i *
2628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			info.blocks_per_group;
2638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		u32 header_size = 0;
2648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		if (ext4_bg_has_super_block(i)) {
265b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross			if (i != 0)
2668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland				queue_data_block((u8 *)sb, info.block_size, group_start_block);
267b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross			queue_data_block((u8 *)aux_info.bg_desc,
268b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross				aux_info.bg_desc_blocks * info.block_size,
269b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross				group_start_block + 1);
27022742ce739a046a079b2e1b03342a25472dfa352Colin Cross			header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks;
2718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		}
2728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size;
2748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1;
2758642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2;
2768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
2778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group;
2788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group;
2798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_used_dirs_count = 0;
2808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
2818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
2828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
283b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Crossvoid ext4_queue_sb(void)
284b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross{
285b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	/* The write_data* functions expect only block aligned calls.
286b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	 * This is not an issue, except when we write out the super
287b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	 * block on a system with a block size > 1K.  So, we need to
288b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	 * deal with that here.
289b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	 */
290b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	if (info.block_size > 1024) {
291b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		u8 *buf = calloc(info.block_size, 1);
292b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		memcpy(buf + 1024, (u8*)aux_info.sb, 1024);
293b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		queue_data_block(buf, info.block_size, 0);
294b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	} else {
295b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross		queue_data_block((u8*)aux_info.sb, 1024, 1);
296b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	}
297b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross}
298b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
299a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Crossvoid ext4_parse_sb(struct ext4_super_block *sb)
300a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross{
301a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	if (sb->s_magic != EXT4_SUPER_MAGIC)
302a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross		error("superblock magic incorrect");
303a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
304a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	if (sb->s_state != EXT4_VALID_FS)
305a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross		error("filesystem state not valid");
306a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
307a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.block_size = 1024 << sb->s_log_block_size;
308a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.blocks_per_group = sb->s_blocks_per_group;
309a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.inodes_per_group = sb->s_inodes_per_group;
310a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.inode_size = sb->s_inode_size;
311a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.inodes = sb->s_inodes_count;
312a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.feat_ro_compat = sb->s_feature_ro_compat;
313a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.feat_compat = sb->s_feature_compat;
314a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.feat_incompat = sb->s_feature_incompat;
315a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks;
316a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.label = sb->s_volume_name;
317a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
318a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	aux_info.len_blocks = ((u64)sb->s_blocks_count_hi << 32) +
319a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross			sb->s_blocks_count_lo;
320a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	info.len = (u64)info.block_size * aux_info.len_blocks;
321a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
322a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	ext4_create_fs_aux_info();
323a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
324a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	memcpy(aux_info.sb, sb, sizeof(*sb));
325a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
326a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross	if (aux_info.first_data_block != sb->s_first_data_block)
327a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross		critical_error("first data block does not match");
328a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross}
329a7ed433f2dc0116627a93b18fbb260f0665ca0cbColin Cross
3308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_resize_inode()
3318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
3328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	struct block_allocation *reserve_inode_alloc = create_allocation();
3338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u32 reserve_inode_len = 0;
3348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	unsigned int i;
3358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO);
3378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (inode == NULL) {
3388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		error("failed to get resize inode");
3398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return;
3408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
3418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	for (i = 0; i < aux_info.groups; i++) {
3438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		if (ext4_bg_has_super_block(i)) {
3448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			u64 group_start_block = aux_info.first_data_block + i *
3458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland				info.blocks_per_group;
3468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			u32 reserved_block_start = group_start_block + 1 +
3478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland				aux_info.bg_desc_blocks;
34822742ce739a046a079b2e1b03342a25472dfa352Colin Cross			u32 reserved_block_len = info.bg_desc_reserve_blocks;
3498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			append_region(reserve_inode_alloc, reserved_block_start,
3508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland				reserved_block_len, i);
3518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			reserve_inode_len += reserved_block_len;
3528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		}
3538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
3548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	inode_attach_resize(inode, reserve_inode_alloc);
3568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
3588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	inode->i_links_count = 1;
3598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	free_alloc(reserve_inode_alloc);
3618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
3628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Allocate the blocks to hold a journal inode and connect them to the
3648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland   reserved journal inode */
3658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_create_journal_inode()
3668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
3678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO);
3688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (inode == NULL) {
3698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		error("failed to get journal inode");
3708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return;
3718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
3728642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3738642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u8 *journal_data = inode_allocate_data_extents(inode,
3748642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland			info.journal_blocks * info.block_size,
3754b8b59a7cd0e1413dcf9973c4bbd7eb65a444c4cBrian Swetland			info.journal_blocks * info.block_size);
3768642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (!journal_data) {
3778642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		error("failed to allocate extents for journal data");
3788642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return;
3798642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
3808642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3818642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
3828642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	inode->i_links_count = 1;
3838642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3848642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	journal_superblock_t *jsb = (journal_superblock_t *)journal_data;
3858642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
3868642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
3878642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_blocksize = htonl(info.block_size);
3888642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_maxlen = htonl(info.journal_blocks);
3898642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_nr_users = htonl(1);
3908642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_first = htonl(1);
3918642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	jsb->s_sequence = htonl(1);
3928642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3938642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block));
3948642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
3958642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
3968642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland/* Update the number of free blocks and inodes in the filesystem and in each
3978642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland   block group */
3988642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandvoid ext4_update_free()
3998642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
4008642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	unsigned int i;
4018642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4028642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	for (i = 0; i < aux_info.groups; i++) {
4038642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		u32 bg_free_blocks = get_free_blocks(i);
4048642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		u32 bg_free_inodes = get_free_inodes(i);
4058642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4068642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks;
4078642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.sb->s_free_blocks_count_lo += bg_free_blocks;
4088642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4098642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes;
4108642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.sb->s_free_inodes_count += bg_free_inodes;
4118642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4128642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i);
4138642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	}
4148642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
4158642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4168642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandstatic u64 get_block_device_size(const char *filename)
4178642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
4188642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	int fd = open(filename, O_RDONLY);
4198642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u64 size = 0;
4208642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	int ret;
4218642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4228642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (fd < 0)
4238642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 0;
4248642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4258642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#if defined(__linux__)
4268642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	ret = ioctl(fd, BLKGETSIZE64, &size);
4278642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#elif defined(__APPLE__) && defined(__MACH__)
4288642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
4298642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#else
4308642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	return 0;
4318642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland#endif
4328642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4338642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	close(fd);
4348642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4358642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (ret)
4368642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 0;
4378642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4388642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	return size;
4398642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
4408642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4418642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandu64 get_file_size(const char *filename)
4428642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
4438642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	struct stat buf;
4448642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	int ret;
4458642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4468642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	ret = stat(filename, &buf);
4478642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (ret)
4488642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 0;
4498642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4508642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (S_ISREG(buf.st_mode))
4518642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return buf.st_size;
4528642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	else if (S_ISBLK(buf.st_mode))
4538642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return get_block_device_size(filename);
4548642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	else
4558642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		return 0;
4568642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
4578642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4588642b7fba54727a38f751516bcdc452fb09ef610Brian Swetlandu64 parse_num(const char *arg)
4598642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland{
4608642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	char *endptr;
4618642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	u64 num = strtoull(arg, &endptr, 10);
4628642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	if (*endptr == 'k' || *endptr == 'K')
4638642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		num *= 1024LL;
4648642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	else if (*endptr == 'm' || *endptr == 'M')
4658642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		num *= 1024LL * 1024LL;
4668642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	else if (*endptr == 'g' || *endptr == 'G')
4678642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland		num *= 1024LL * 1024LL * 1024LL;
4688642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
4698642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland	return num;
4708642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland}
4718642b7fba54727a38f751516bcdc452fb09ef610Brian Swetland
472