2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 #include <arpa/inet.h>
36 #elif defined(__APPLE__) && defined(__MACH__)
40 #include "make_ext4fs.h"
41 #include "ext4_utils.h"
43 #include "ext_utils.h"
44 #include "backed_block.h"
54 #include <private/android_filesystem_config.h>
57 /* TODO: Not implemented:
58 Allocating blocks in the same block group as the file inode
59 Hash or binary tree directories
61 Special files: symbolic links, sockets, devices, fifos
67 struct fs_aux_info aux_info;
69 /* Write a contiguous region of data blocks from a memory buffer */
70 static void write_data_block(void *priv, u32 block, u8 *data, int len)
76 if (block * info.block_size + len >= info.len) {
77 error("attempted to write block %llu past end of filesystem",
78 block * info.block_size + len - info.len);
82 off = (off_t)block * info.block_size;
83 off = lseek(fd, off, SEEK_SET);
89 ret = write(fd, data, len);
93 error("incomplete write");
96 /* Write a contiguous region of data blocks from a file */
97 static void write_data_file(void *priv, u32 block, const char *file,
98 off_t offset, int len)
100 int fd = *(int*)priv;
104 if (block * info.block_size + len >= info.len) {
105 error("attempted to write block %llu past end of filesystem",
106 block * info.block_size + len - info.len);
110 int file_fd = open(file, O_RDONLY);
116 u8 *data = mmap(NULL, len, PROT_READ, MAP_SHARED, file_fd, offset);
117 if (data == MAP_FAILED) {
123 off = (off_t)block * info.block_size;
124 off = lseek(fd, off, SEEK_SET);
126 error_errno("lseek");
130 ret = write(fd, data, len);
132 error_errno("write");
134 error("incomplete write");
141 /* Write the filesystem image to a file */
142 static void write_ext4_image(const char *filename)
145 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
153 off = lseek(fd, 1024, SEEK_SET);
155 error_errno("lseek");
159 ret = write(fd, aux_info.sb, 1024);
161 error_errno("write");
163 error("incomplete write");
165 off = (aux_info.first_data_block + 1) * info.block_size;
166 off = lseek(fd, off, SEEK_SET);
168 error_errno("lseek");
172 ret = write(fd, aux_info.bg_desc,
173 aux_info.bg_desc_blocks * info.block_size);
175 error_errno("write");
176 else if (ret < (int)(aux_info.bg_desc_blocks * info.block_size))
177 error("incomplete write");
179 for_each_data_block(write_data_block, write_data_file, &fd);
182 off = lseek(fd, off, SEEK_SET);
184 error_errno("lseek");
188 ret = write(fd, "\0", 1);
190 error_errno("write");
192 error("incomplete write");
197 /* Compute the rest of the parameters of the filesystem from the basic info */
198 static void ext4_create_fs_aux_info()
200 aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1;
201 aux_info.len_blocks = info.len / info.block_size;
202 aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size,
204 aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block,
205 info.blocks_per_group);
206 aux_info.blocks_per_ind = info.block_size / sizeof(u32);
207 aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind;
208 aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind;
210 aux_info.bg_desc_blocks =
211 DIV_ROUND_UP(aux_info.groups * sizeof(struct ext2_group_desc),
214 aux_info.bg_desc_reserve_blocks =
215 DIV_ROUND_UP(aux_info.groups * 1024 * sizeof(struct ext2_group_desc),
216 info.block_size) - aux_info.bg_desc_blocks;
218 if (aux_info.bg_desc_reserve_blocks > aux_info.blocks_per_ind)
219 aux_info.bg_desc_reserve_blocks = aux_info.blocks_per_ind;
221 aux_info.default_i_flags = EXT4_NOATIME_FL;
223 u32 last_group_size = aux_info.len_blocks % info.blocks_per_group;
224 u32 last_header_size = 2 + aux_info.inode_table_blocks;
225 if (ext4_bg_has_super_block(aux_info.groups - 1))
226 last_header_size += 1 + aux_info.bg_desc_blocks +
227 aux_info.bg_desc_reserve_blocks;
228 if (last_group_size > 0 && last_group_size < last_header_size) {
230 aux_info.len_blocks -= last_group_size;
233 aux_info.sb = calloc(info.block_size, 1);
235 critical_error_errno("calloc");
237 aux_info.bg_desc = calloc(info.block_size, aux_info.bg_desc_blocks);
238 if (!aux_info.bg_desc)
239 critical_error_errno("calloc");
242 void ext4_free_fs_aux_info()
245 free(aux_info.bg_desc);
248 /* Fill in the superblock memory buffer based on the filesystem parameters */
249 static void ext4_fill_in_sb()
252 struct ext4_super_block *sb = aux_info.sb;
254 sb->s_inodes_count = info.inodes_per_group * aux_info.groups;
255 sb->s_blocks_count_lo = aux_info.len_blocks;
256 sb->s_r_blocks_count_lo = 0;
257 sb->s_free_blocks_count_lo = 0;
258 sb->s_free_inodes_count = 0;
259 sb->s_first_data_block = aux_info.first_data_block;
260 sb->s_log_block_size = log_2(info.block_size / 1024);
261 sb->s_obso_log_frag_size = log_2(info.block_size / 1024);
262 sb->s_blocks_per_group = info.blocks_per_group;
263 sb->s_obso_frags_per_group = info.blocks_per_group;
264 sb->s_inodes_per_group = info.inodes_per_group;
268 sb->s_max_mnt_count = 0xFFFF;
269 sb->s_magic = EXT4_SUPER_MAGIC;
270 sb->s_state = EXT4_VALID_FS;
271 sb->s_errors = EXT4_ERRORS_RO;
272 sb->s_minor_rev_level = 0;
274 sb->s_checkinterval = 0;
275 sb->s_creator_os = EXT4_OS_LINUX;
276 sb->s_rev_level = EXT4_DYNAMIC_REV;
277 sb->s_def_resuid = EXT4_DEF_RESUID;
278 sb->s_def_resgid = EXT4_DEF_RESGID;
280 sb->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
281 sb->s_inode_size = info.inode_size;
282 sb->s_block_group_nr = 0;
283 sb->s_feature_compat = info.feat_compat;
284 sb->s_feature_incompat = info.feat_incompat;
285 sb->s_feature_ro_compat = info.feat_ro_compat;
286 generate_uuid("extandroid/make_ext4fs", info.label, sb->s_uuid);
287 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
288 strncpy(sb->s_volume_name, info.label, sizeof(sb->s_volume_name));
289 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
290 sb->s_algorithm_usage_bitmap = 0;
292 sb->s_reserved_gdt_blocks = aux_info.bg_desc_reserve_blocks;
293 sb->s_prealloc_blocks = 0;
294 sb->s_prealloc_dir_blocks = 0;
296 //memcpy(sb->s_journal_uuid, sb->s_uuid, sizeof(sb->s_journal_uuid));
297 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
298 sb->s_journal_inum = EXT4_JOURNAL_INO;
299 sb->s_journal_dev = 0;
300 sb->s_last_orphan = 0;
301 sb->s_hash_seed[0] = 0; /* FIXME */
302 sb->s_def_hash_version = DX_HASH_TEA;
303 sb->s_reserved_char_pad = EXT4_JNL_BACKUP_BLOCKS;
304 sb->s_desc_size = sizeof(struct ext2_group_desc);
305 sb->s_default_mount_opts = 0; /* FIXME */
306 sb->s_first_meta_bg = 0;
308 //sb->s_jnl_blocks[17]; /* FIXME */
310 sb->s_blocks_count_hi = aux_info.len_blocks >> 32;
311 sb->s_r_blocks_count_hi = 0;
312 sb->s_free_blocks_count_hi = 0;
313 sb->s_min_extra_isize = sizeof(struct ext4_inode)
314 - EXT4_GOOD_OLD_INODE_SIZE;
315 sb->s_want_extra_isize = sizeof(struct ext4_inode)
316 - EXT4_GOOD_OLD_INODE_SIZE;
318 sb->s_raid_stride = 0;
319 sb->s_mmp_interval = 0;
321 sb->s_raid_stripe_width = 0;
322 sb->s_log_groups_per_flex = 0;
323 sb->s_kbytes_written = 0;
325 for (i = 0; i < aux_info.groups; i++) {
326 u64 group_start_block = aux_info.first_data_block + i
327 * info.blocks_per_group;
329 if (ext4_bg_has_super_block(i)) {
331 queue_data_block((u8 *)sb, info.block_size, group_start_block);
332 queue_data_block((u8 *)aux_info.bg_desc,
333 aux_info.bg_desc_blocks * info.block_size,
334 group_start_block + 1);
336 header_size = 1 + aux_info.bg_desc_blocks + aux_info.bg_desc_reserve_blocks;
339 aux_info.bg_desc[i].bg_block_bitmap = group_start_block + header_size;
340 aux_info.bg_desc[i].bg_inode_bitmap = group_start_block + header_size + 1;
341 aux_info.bg_desc[i].bg_inode_table = group_start_block + header_size + 2;
343 aux_info.bg_desc[i].bg_free_blocks_count = sb->s_blocks_per_group;
344 aux_info.bg_desc[i].bg_free_inodes_count = sb->s_inodes_per_group;
345 aux_info.bg_desc[i].bg_used_dirs_count = 0;
349 static void ext4_create_resize_inode()
351 struct block_allocation *reserve_inode_alloc = create_allocation();
352 u32 reserve_inode_len = 0;
355 struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO);
357 error("failed to get resize inode");
361 for (i = 0; i < aux_info.groups; i++) {
362 if (ext4_bg_has_super_block(i)) {
363 u64 group_start_block = aux_info.first_data_block + i *
364 info.blocks_per_group;
365 u32 reserved_block_start = group_start_block + 1 +
366 aux_info.bg_desc_blocks;
367 u32 reserved_block_len = aux_info.bg_desc_reserve_blocks;
368 append_region(reserve_inode_alloc, reserved_block_start,
369 reserved_block_len, i);
370 reserve_inode_len += reserved_block_len;
374 inode_attach_resize(inode, reserve_inode_alloc);
376 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
377 inode->i_links_count = 1;
379 free_alloc(reserve_inode_alloc);
382 /* Allocate the blocks to hold a journal inode and connect them to the
383 reserved journal inode */
384 static void ext4_create_journal_inode()
386 struct ext4_inode *inode = get_inode(EXT4_JOURNAL_INO);
388 error("failed to get journal inode");
392 u8 *journal_data = inode_allocate_data_extents(inode,
393 info.journal_blocks * info.block_size,
396 error("failed to allocate extents for journal data");
400 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
401 inode->i_links_count = 1;
403 journal_superblock_t *jsb = (journal_superblock_t *)journal_data;
404 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
405 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
406 jsb->s_blocksize = htonl(info.block_size);
407 jsb->s_maxlen = htonl(info.journal_blocks);
408 jsb->s_nr_users = htonl(1);
409 jsb->s_first = htonl(1);
410 jsb->s_sequence = htonl(1);
412 memcpy(aux_info.sb->s_jnl_blocks, &inode->i_block, sizeof(inode->i_block));
415 /* Update the number of free blocks and inodes in the filesystem and in each
417 static void ext4_update_free()
421 for (i = 0; i < aux_info.groups; i++) {
422 u32 bg_free_blocks = get_free_blocks(i);
423 u32 bg_free_inodes = get_free_inodes(i);
425 aux_info.bg_desc[i].bg_free_blocks_count = bg_free_blocks;
426 aux_info.sb->s_free_blocks_count_lo += bg_free_blocks;
428 aux_info.bg_desc[i].bg_free_inodes_count = bg_free_inodes;
429 aux_info.sb->s_free_inodes_count += bg_free_inodes;
431 aux_info.bg_desc[i].bg_used_dirs_count += get_directories(i);
435 static int filter_dot(const struct dirent *d)
437 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
440 static u32 build_default_directory_structure()
444 struct dentry dentries = {
445 .filename = "lost+found",
446 .file_type = EXT4_FT_DIR,
451 root_inode = make_directory(0, 1, &dentries, 1);
452 inode = make_directory(root_inode, 0, NULL, 0);
453 *dentries.inode = inode;
458 /* Read a local directory and create the same tree in the generated filesystem.
459 Calls itself recursively with each directory in the given directory */
460 static u32 build_directory_structure(const char *full_path, const char *dir_path,
461 u32 dir_inode, int android)
464 struct dentry *dentries;
465 struct dirent **namelist;
473 entries = scandir(full_path, &namelist, filter_dot, alphasort);
475 error_errno("scandir");
476 return EXT4_ALLOCATE_FAILED;
479 dentries = calloc(entries, sizeof(struct dentry));
480 if (dentries == NULL)
481 critical_error_errno("malloc");
483 for (i = 0; i < entries; i++) {
484 dentries[i].filename = strdup(namelist[i]->d_name);
485 if (dentries[i].filename == NULL)
486 critical_error_errno("strdup");
488 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
489 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
493 ret = lstat(dentries[i].full_path, &stat);
495 error_errno("lstat");
501 dentries[i].size = stat.st_size;
502 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
505 unsigned int mode = 0;
506 unsigned int uid = 0;
507 unsigned int gid = 0;
508 int dir = S_ISDIR(stat.st_mode);
509 fs_config(dentries[i].path, dir, &uid, &gid, &mode);
510 dentries[i].mode = mode;
511 dentries[i].uid = uid;
512 dentries[i].gid = gid;
514 error("can't set android permissions - built without android support");
518 if (S_ISREG(stat.st_mode)) {
519 dentries[i].file_type = EXT4_FT_REG_FILE;
520 } else if (S_ISDIR(stat.st_mode)) {
521 dentries[i].file_type = EXT4_FT_DIR;
523 } else if (S_ISCHR(stat.st_mode)) {
524 dentries[i].file_type = EXT4_FT_CHRDEV;
525 } else if (S_ISBLK(stat.st_mode)) {
526 dentries[i].file_type = EXT4_FT_BLKDEV;
527 } else if (S_ISFIFO(stat.st_mode)) {
528 dentries[i].file_type = EXT4_FT_FIFO;
529 } else if (S_ISSOCK(stat.st_mode)) {
530 dentries[i].file_type = EXT4_FT_SOCK;
531 } else if (S_ISLNK(stat.st_mode)) {
532 dentries[i].file_type = EXT4_FT_SYMLINK;
533 dentries[i].link = calloc(info.block_size, 1);
534 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
536 error("unknown file type on %s", dentries[i].path);
543 inode = make_directory(dir_inode, entries, dentries, dirs);
545 for (i = 0; i < entries; i++) {
546 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
547 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
548 } else if (dentries[i].file_type == EXT4_FT_DIR) {
549 entry_inode = build_directory_structure(dentries[i].full_path,
550 dentries[i].path, inode, android);
551 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
552 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
554 error("unknown file type on %s", dentries[i].path);
557 *dentries[i].inode = entry_inode;
559 ret = inode_set_permissions(entry_inode, dentries[i].mode,
560 dentries[i].uid, dentries[i].gid);
562 error("failed to set permissions on %s\n", dentries[i].path);
564 free(dentries[i].path);
565 free(dentries[i].full_path);
566 free(dentries[i].link);
567 free((void *)dentries[i].filename);
574 static u32 compute_block_size()
579 static u32 compute_blocks_per_group()
581 return info.block_size * 8;
584 static u32 compute_inodes()
586 return DIV_ROUND_UP(info.len, info.block_size) / 4;
589 static u32 compute_inodes_per_group()
591 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
592 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
593 return DIV_ROUND_UP(info.inodes, block_groups);
596 static u64 get_block_device_size(const char *filename)
598 int fd = open(filename, O_RDONLY);
606 ret = ioctl(fd, BLKGETSIZE64, &size);
607 #elif defined(__APPLE__) && defined(__MACH__)
608 ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
621 static u64 get_file_size(const char *filename)
626 ret = stat(filename, &buf);
630 if (S_ISREG(buf.st_mode))
632 else if (S_ISBLK(buf.st_mode))
633 return get_block_device_size(filename);
638 static void usage(char *path)
640 fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
641 fprintf(stderr, " [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
642 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
643 fprintf(stderr, " <filename> [<directory>]\n");
646 static u64 parse_num(const char *arg)
649 u64 num = strtoull(arg, &endptr, 10);
650 if (*endptr == 'k' || *endptr == 'K')
652 else if (*endptr == 'm' || *endptr == 'M')
653 num *= 1024LL * 1024LL;
654 else if (*endptr == 'g' || *endptr == 'G')
655 num *= 1024LL * 1024LL * 1024LL;
660 int main(int argc, char **argv)
663 const char *filename = NULL;
664 const char *directory = NULL;
665 char *mountpoint = "";
670 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:f")) != -1) {
673 info.len = parse_num(optarg);
676 info.journal_blocks = parse_num(optarg);
679 info.block_size = parse_num(optarg);
682 info.blocks_per_group = parse_num(optarg);
685 info.inodes = parse_num(optarg);
688 info.inode_size = parse_num(optarg);
706 if (optind >= argc) {
707 fprintf(stderr, "Expected filename after options\n");
712 filename = argv[optind++];
715 directory = argv[optind++];
718 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
724 info.len = get_file_size(filename);
727 fprintf(stderr, "Need size of filesystem\n");
732 if (info.journal_blocks > 0)
733 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
735 if (info.block_size <= 0)
736 info.block_size = compute_block_size();
738 if (info.blocks_per_group <= 0)
739 info.blocks_per_group = compute_blocks_per_group();
741 if (info.inodes <= 0)
742 info.inodes = compute_inodes();
744 if (info.inode_size <= 0)
745 info.inode_size = 256;
747 if (info.label == NULL)
750 info.inodes_per_group = compute_inodes_per_group();
753 EXT4_FEATURE_COMPAT_RESIZE_INODE;
755 info.feat_ro_compat |=
756 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
757 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
759 info.feat_incompat |=
760 EXT4_FEATURE_INCOMPAT_EXTENTS |
761 EXT4_FEATURE_INCOMPAT_FILETYPE;
764 printf("Creating filesystem with parameters:\n");
765 printf(" Size: %llu\n", info.len);
766 printf(" Block size: %d\n", info.block_size);
767 printf(" Blocks per group: %d\n", info.blocks_per_group);
768 printf(" Inodes per group: %d\n", info.inodes_per_group);
769 printf(" Inode size: %d\n", info.inode_size);
770 printf(" Label: %s\n", info.label);
772 ext4_create_fs_aux_info();
774 printf(" Blocks: %llu\n", aux_info.len_blocks);
775 printf(" Block groups: %d\n", aux_info.groups);
776 printf(" Reserved block group size: %d\n", aux_info.bg_desc_reserve_blocks);
778 block_allocator_init();
782 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
783 error("failed to reserve first 10 inodes");
785 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
786 ext4_create_journal_inode();
788 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
789 ext4_create_resize_inode();
792 root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
794 root_inode_num = build_default_directory_structure();
796 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
797 inode_set_permissions(root_inode_num, root_mode, 0, 0);
801 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
802 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
803 aux_info.sb->s_inodes_count,
804 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
805 aux_info.sb->s_blocks_count_lo);
807 write_ext4_image(filename);