ext4_utils.c revision f5a6b34a9a6b3827f3f2cad111bf7eae3f1f034b
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ext4_utils.h"
18#include "uuid.h"
19#include "allocate.h"
20#include "indirect.h"
21#include "extent.h"
22
23#include <sparse/sparse.h>
24
25#include <fcntl.h>
26#include <inttypes.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <stddef.h>
30#include <string.h>
31
32#ifdef USE_MINGW
33#include <winsock2.h>
34#else
35#include <arpa/inet.h>
36#include <sys/ioctl.h>
37#endif
38
39#if defined(__linux__)
40#include <linux/fs.h>
41#elif defined(__APPLE__) && defined(__MACH__)
42#include <sys/disk.h>
43#endif
44
45int force = 0;
46struct fs_info info;
47struct fs_aux_info aux_info;
48struct sparse_file *ext4_sparse_file;
49
50jmp_buf setjmp_env;
51
52/* returns 1 if a is a power of b */
53static int is_power_of(int a, int b)
54{
55	while (a > b) {
56		if (a % b)
57			return 0;
58		a /= b;
59	}
60
61	return (a == b) ? 1 : 0;
62}
63
64int bitmap_get_bit(u8 *bitmap, u32 bit)
65{
66	if (bitmap[bit / 8] & (1 << (bit % 8)))
67		return 1;
68
69	return 0;
70}
71
72void bitmap_clear_bit(u8 *bitmap, u32 bit)
73{
74	bitmap[bit / 8] &= ~(1 << (bit % 8));
75
76	return;
77}
78
79/* Returns 1 if the bg contains a backup superblock.  On filesystems with
80   the sparse_super feature, only block groups 0, 1, and powers of 3, 5,
81   and 7 have backup superblocks.  Otherwise, all block groups have backup
82   superblocks */
83int ext4_bg_has_super_block(int bg)
84{
85	/* Without sparse_super, every block group has a superblock */
86	if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
87		return 1;
88
89	if (bg == 0 || bg == 1)
90		return 1;
91
92	if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7))
93		return 1;
94
95	return 0;
96}
97
98/* Function to read the primary superblock */
99void read_sb(int fd, struct ext4_super_block *sb)
100{
101	off64_t ret;
102
103	ret = lseek64(fd, 1024, SEEK_SET);
104	if (ret < 0)
105		critical_error_errno("failed to seek to superblock");
106
107	ret = read(fd, sb, sizeof(*sb));
108	if (ret < 0)
109		critical_error_errno("failed to read superblock");
110	if (ret != sizeof(*sb))
111		critical_error("failed to read all of superblock");
112}
113
114/* Function to write a primary or backup superblock at a given offset */
115void write_sb(int fd, unsigned long long offset, struct ext4_super_block *sb)
116{
117	off64_t ret;
118
119	ret = lseek64(fd, offset, SEEK_SET);
120	if (ret < 0)
121		critical_error_errno("failed to seek to superblock");
122
123	ret = write(fd, sb, sizeof(*sb));
124	if (ret < 0)
125		critical_error_errno("failed to write superblock");
126	if (ret != sizeof(*sb))
127		critical_error("failed to write all of superblock");
128}
129
130/* Write the filesystem image to a file */
131void write_ext4_image(int fd, int gz, int sparse, int crc)
132{
133	sparse_file_write(ext4_sparse_file, fd, gz, sparse, crc);
134}
135
136/* Compute the rest of the parameters of the filesystem from the basic info */
137void ext4_create_fs_aux_info()
138{
139	aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
140	aux_info.len_blocks = info.len / info.block_size;
141	aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size,
142		info.block_size);
143	aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block,
144		info.blocks_per_group);
145	aux_info.blocks_per_ind = info.block_size / sizeof(u32);
146	aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
147	aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
148
149	aux_info.bg_desc_blocks =
150		DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc),
151			info.block_size);
152
153	aux_info.default_i_flags = EXT4_NOATIME_FL;
154
155	u32 last_group_size = aux_info.len_blocks % info.blocks_per_group;
156	u32 last_header_size = 2 + aux_info.inode_table_blocks;
157	if (ext4_bg_has_super_block(aux_info.groups - 1))
158		last_header_size += 1 + aux_info.bg_desc_blocks +
159			info.bg_desc_reserve_blocks;
160	if (last_group_size > 0 && last_group_size < last_header_size) {
161		aux_info.groups--;
162		aux_info.len_blocks -= last_group_size;
163	}
164
165	aux_info.sb = calloc(info.block_size, 1);
166	/* Alloc an array to hold the pointers to the backup superblocks */
167	aux_info.backup_sb = calloc(aux_info.groups, sizeof(char *));
168
169	if (!aux_info.sb)
170		critical_error_errno("calloc");
171
172	aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks);
173	if (!aux_info.bg_desc)
174		critical_error_errno("calloc");
175	aux_info.xattrs = NULL;
176}
177
178void ext4_free_fs_aux_info()
179{
180	unsigned int i;
181
182	for (i=0; i<aux_info.groups; i++) {
183		if (aux_info.backup_sb[i])
184			free(aux_info.backup_sb[i]);
185	}
186	free(aux_info.sb);
187	free(aux_info.bg_desc);
188}
189
190/* Fill in the superblock memory buffer based on the filesystem parameters */
191void ext4_fill_in_sb()
192{
193	unsigned int i;
194	struct ext4_super_block *sb = aux_info.sb;
195
196	sb->s_inodes_count = info.inodes_per_group * aux_info.groups;
197	sb->s_blocks_count_lo = aux_info.len_blocks;
198	sb->s_r_blocks_count_lo = 0;
199	sb->s_free_blocks_count_lo = 0;
200	sb->s_free_inodes_count = 0;
201	sb->s_first_data_block = aux_info.first_data_block;
202	sb->s_log_block_size = log_2(info.block_size / 1024);
203	sb->s_obso_log_frag_size = log_2(info.block_size / 1024);
204	sb->s_blocks_per_group = info.blocks_per_group;
205	sb->s_obso_frags_per_group = info.blocks_per_group;
206	sb->s_inodes_per_group = info.inodes_per_group;
207	sb->s_mtime = 0;
208	sb->s_wtime = 0;
209	sb->s_mnt_count = 0;
210	sb->s_max_mnt_count = 0xFFFF;
211	sb->s_magic = EXT4_SUPER_MAGIC;
212	sb->s_state = EXT4_VALID_FS;
213	sb->s_errors = EXT4_ERRORS_RO;
214	sb->s_minor_rev_level = 0;
215	sb->s_lastcheck = 0;
216	sb->s_checkinterval = 0;
217	sb->s_creator_os = EXT4_OS_LINUX;
218	sb->s_rev_level = EXT4_DYNAMIC_REV;
219	sb->s_def_resuid = EXT4_DEF_RESUID;
220	sb->s_def_resgid = EXT4_DEF_RESGID;
221
222	sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
223	sb->s_inode_size = info.inode_size;
224	sb->s_block_group_nr = 0;
225	sb->s_feature_compat = info.feat_compat;
226	sb->s_feature_incompat = info.feat_incompat;
227	sb->s_feature_ro_compat = info.feat_ro_compat;
228	generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid);
229	memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
230	strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name));
231	memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
232	sb->s_algorithm_usage_bitmap = 0;
233
234	sb->s_reserved_gdt_blocks = info.bg_desc_reserve_blocks;
235	sb->s_prealloc_blocks = 0;
236	sb->s_prealloc_dir_blocks = 0;
237
238	//memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid));
239	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
240		sb->s_journal_inum = EXT4_JOURNAL_INO;
241	sb->s_journal_dev = 0;
242	sb->s_last_orphan = 0;
243	sb->s_hash_seed[0] = 0; /* FIXME */
244	sb->s_def_hash_version = DX_HASH_TEA;
245	sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS;
246	sb->s_desc_size = sizeof(struct ext2_group_desc);
247	sb->s_default_mount_opts = 0; /* FIXME */
248	sb->s_first_meta_bg = 0;
249	sb->s_mkfs_time = 0;
250	//sb->s_jnl_blocks[17]; /* FIXME */
251
252	sb->s_blocks_count_hi = aux_info.len_blocks >> 32;
253	sb->s_r_blocks_count_hi = 0;
254	sb->s_free_blocks_count_hi = 0;
255	sb->s_min_extra_isize = sizeof(struct ext4_inode) -
256		EXT4_GOOD_OLD_INODE_SIZE;
257	sb->s_want_extra_isize = sizeof(struct ext4_inode) -
258		EXT4_GOOD_OLD_INODE_SIZE;
259	sb->s_flags = 2;
260	sb->s_raid_stride = 0;
261	sb->s_mmp_interval = 0;
262	sb->s_mmp_block = 0;
263	sb->s_raid_stripe_width = 0;
264	sb->s_log_groups_per_flex = 0;
265	sb->s_kbytes_written = 0;
266
267	for (i = 0; i < aux_info.groups; i++) {
268		u64 group_start_block = aux_info.first_data_block + i *
269			info.blocks_per_group;
270		u32 header_size = 0;
271		if (ext4_bg_has_super_block(i)) {
272			if (i != 0) {
273				aux_info.backup_sb[i] = calloc(info.block_size, 1);
274				memcpy(aux_info.backup_sb[i], sb, info.block_size);
275				/* Update the block group nr of this backup superblock */
276				aux_info.backup_sb[i]->s_block_group_nr = i;
277				sparse_file_add_data(ext4_sparse_file, aux_info.backup_sb[i],
278						info.block_size, group_start_block);
279			}
280			sparse_file_add_data(ext4_sparse_file, aux_info.bg_desc,
281				aux_info.bg_desc_blocks * info.block_size,
282				group_start_block + 1);
283			header_size = 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks;
284		}
285
286		aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size;
287		aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1;
288		aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2;
289
290		aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group;
291		aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group;
292		aux_info.bg_desc[i].bg_used_dirs_count = 0;
293	}
294}
295
296void ext4_queue_sb(void)
297{
298	/* The write_data* functions expect only block aligned calls.
299	 * This is not an issue, except when we write out the super
300	 * block on a system with a block size > 1K.  So, we need to
301	 * deal with that here.
302	 */
303	if (info.block_size > 1024) {
304		u8 *buf = calloc(info.block_size, 1);
305		memcpy(buf + 1024, (u8*)aux_info.sb, 1024);
306		sparse_file_add_data(ext4_sparse_file, buf, info.block_size, 0);
307	} else {
308		sparse_file_add_data(ext4_sparse_file, aux_info.sb, 1024, 1);
309	}
310}
311
312void ext4_parse_sb_info(struct ext4_super_block *sb)
313{
314	if (sb->s_magic != EXT4_SUPER_MAGIC)
315		error("superblock magic incorrect");
316
317	if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS)
318		error("filesystem state not valid");
319
320	ext4_parse_sb(sb, &info);
321
322	ext4_create_fs_aux_info();
323
324	memcpy(aux_info.sb, sb, sizeof(*sb));
325
326	if (aux_info.first_data_block != sb->s_first_data_block)
327		critical_error("first data block does not match");
328}
329
330void ext4_create_resize_inode()
331{
332	struct block_allocation *reserve_inode_alloc = create_allocation();
333	u32 reserve_inode_len = 0;
334	unsigned int i;
335
336	struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO);
337	if (inode == NULL) {
338		error("failed to get resize inode");
339		return;
340	}
341
342	for (i = 0; i < aux_info.groups; i++) {
343		if (ext4_bg_has_super_block(i)) {
344			u64 group_start_block = aux_info.first_data_block + i *
345				info.blocks_per_group;
346			u32 reserved_block_start = group_start_block + 1 +
347				aux_info.bg_desc_blocks;
348			u32 reserved_block_len = info.bg_desc_reserve_blocks;
349			append_region(reserve_inode_alloc, reserved_block_start,
350				reserved_block_len, i);
351			reserve_inode_len += reserved_block_len;
352		}
353	}
354
355	inode_attach_resize(inode, reserve_inode_alloc);
356
357	inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
358	inode->i_links_count = 1;
359
360	free_alloc(reserve_inode_alloc);
361}
362
363/* Allocate the blocks to hold a journal inode and connect them to the
364   reserved journal inode */
365void ext4_create_journal_inode()
366{
367	struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO);
368	if (inode == NULL) {
369		error("failed to get journal inode");
370		return;
371	}
372
373	u8 *journal_data = inode_allocate_data_extents(inode,
374			info.journal_blocks * info.block_size,
375			info.journal_blocks * info.block_size);
376	if (!journal_data) {
377		error("failed to allocate extents for journal data");
378		return;
379	}
380
381	inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
382	inode->i_links_count = 1;
383
384	journal_superblock_t *jsb = (journal_superblock_t *)journal_data;
385	jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
386	jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
387	jsb->s_blocksize = htonl(info.block_size);
388	jsb->s_maxlen = htonl(info.journal_blocks);
389	jsb->s_nr_users = htonl(1);
390	jsb->s_first = htonl(1);
391	jsb->s_sequence = htonl(1);
392
393	memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block));
394}
395
396/* Update the number of free blocks and inodes in the filesystem and in each
397   block group */
398void ext4_update_free()
399{
400	u32 i;
401
402	for (i = 0; i < aux_info.groups; i++) {
403		u32 bg_free_blocks = get_free_blocks(i);
404		u32 bg_free_inodes = get_free_inodes(i);
405		u16 crc;
406
407		aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks;
408		aux_info.sb->s_free_blocks_count_lo += bg_free_blocks;
409
410		aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes;
411		aux_info.sb->s_free_inodes_count += bg_free_inodes;
412
413		aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i);
414
415		aux_info.bg_desc[i].bg_flags = get_bg_flags(i);
416
417		crc = ext4_crc16(~0, aux_info.sb->s_uuid, sizeof(aux_info.sb->s_uuid));
418		crc = ext4_crc16(crc, &i, sizeof(i));
419		crc = ext4_crc16(crc, &aux_info.bg_desc[i], offsetof(struct ext2_group_desc, bg_checksum));
420		aux_info.bg_desc[i].bg_checksum = crc;
421	}
422}
423
424u64 get_block_device_size(int fd)
425{
426	u64 size = 0;
427	int ret;
428
429#if defined(__linux__)
430	ret = ioctl(fd, BLKGETSIZE64, &size);
431#elif defined(__APPLE__) && defined(__MACH__)
432	ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
433#else
434	close(fd);
435	return 0;
436#endif
437
438	if (ret)
439		return 0;
440
441	return size;
442}
443
444int is_block_device_fd(int fd)
445{
446#ifdef USE_MINGW
447	return 0;
448#else
449	struct stat st;
450	int ret = fstat(fd, &st);
451	if (ret < 0)
452		return 0;
453
454	return S_ISBLK(st.st_mode);
455#endif
456}
457
458u64 get_file_size(int fd)
459{
460	struct stat buf;
461	int ret;
462	u64 reserve_len = 0;
463	s64 computed_size;
464
465	ret = fstat(fd, &buf);
466	if (ret)
467		return 0;
468
469	if (info.len < 0)
470		reserve_len = -info.len;
471
472	if (S_ISREG(buf.st_mode))
473		computed_size = buf.st_size - reserve_len;
474	else if (S_ISBLK(buf.st_mode))
475		computed_size = get_block_device_size(fd) - reserve_len;
476	else
477		computed_size = 0;
478
479	if (computed_size < 0) {
480		warn("Computed filesystem size less than 0");
481		computed_size = 0;
482	}
483
484	return computed_size;
485}
486
487u64 parse_num(const char *arg)
488{
489	char *endptr;
490	u64 num = strtoull(arg, &endptr, 10);
491	if (*endptr == 'k' || *endptr == 'K')
492		num *= 1024LL;
493	else if (*endptr == 'm' || *endptr == 'M')
494		num *= 1024LL * 1024LL;
495	else if (*endptr == 'g' || *endptr == 'G')
496		num *= 1024LL * 1024LL * 1024LL;
497
498	return num;
499}
500
501int read_ext(int fd, int verbose)
502{
503	off64_t ret;
504	struct ext4_super_block sb;
505
506	read_sb(fd, &sb);
507
508	ext4_parse_sb_info(&sb);
509
510	ret = lseek64(fd, info.len, SEEK_SET);
511	if (ret < 0)
512		critical_error_errno("failed to seek to end of input image");
513
514	ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
515	if (ret < 0)
516		critical_error_errno("failed to seek to block group descriptors");
517
518	ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
519	if (ret < 0)
520		critical_error_errno("failed to read block group descriptors");
521	if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
522		critical_error("failed to read all of block group descriptors");
523
524	if (verbose) {
525		printf("Found filesystem with parameters:\n");
526		printf("    Size: %"PRIu64"\n", info.len);
527		printf("    Block size: %d\n", info.block_size);
528		printf("    Blocks per group: %d\n", info.blocks_per_group);
529		printf("    Inodes per group: %d\n", info.inodes_per_group);
530		printf("    Inode size: %d\n", info.inode_size);
531		printf("    Label: %s\n", info.label);
532		printf("    Blocks: %"PRIu64"\n", aux_info.len_blocks);
533		printf("    Block groups: %d\n", aux_info.groups);
534		printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
535		printf("    Used %d/%d inodes and %d/%d blocks\n",
536			aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
537			aux_info.sb->s_inodes_count,
538			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
539			aux_info.sb->s_blocks_count_lo);
540	}
541
542	return 0;
543}
544
545