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