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