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