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.
26 #include <sys/types.h>
28 #include "make_ext4fs.h"
29 #include "output_file.h"
30 #include "ext4_utils.h"
36 #include <private/android_filesystem_config.h>
39 /* TODO: Not implemented:
40 Allocating blocks in the same block group as the file inode
41 Hash or binary tree directories
42 Special files: sockets, devices, fifos
45 static int filter_dot(const struct dirent *d)
47 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
50 static u32 build_default_directory_structure()
54 struct dentry dentries = {
55 .filename = "lost+found",
56 .file_type = EXT4_FT_DIR,
61 root_inode = make_directory(0, 1, &dentries, 1);
62 inode = make_directory(root_inode, 0, NULL, 0);
63 *dentries.inode = inode;
68 /* Read a local directory and create the same tree in the generated filesystem.
69 Calls itself recursively with each directory in the given directory */
70 static u32 build_directory_structure(const char *full_path, const char *dir_path,
71 u32 dir_inode, int android)
74 struct dentry *dentries;
75 struct dirent **namelist;
83 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
85 error_errno("scandir");
86 return EXT4_ALLOCATE_FAILED;
89 dentries = calloc(entries, sizeof(struct dentry));
91 critical_error_errno("malloc");
93 for (i = 0; i < entries; i++) {
94 dentries[i].filename = strdup(namelist[i]->d_name);
95 if (dentries[i].filename == NULL)
96 critical_error_errno("strdup");
98 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
99 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
103 ret = lstat(dentries[i].full_path, &stat);
105 error_errno("lstat");
111 dentries[i].size = stat.st_size;
112 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
115 unsigned int mode = 0;
116 unsigned int uid = 0;
117 unsigned int gid = 0;
118 int dir = S_ISDIR(stat.st_mode);
119 fs_config(dentries[i].path, dir, &uid, &gid, &mode);
120 dentries[i].mode = mode;
121 dentries[i].uid = uid;
122 dentries[i].gid = gid;
124 error("can't set android permissions - built without android support");
128 if (S_ISREG(stat.st_mode)) {
129 dentries[i].file_type = EXT4_FT_REG_FILE;
130 } else if (S_ISDIR(stat.st_mode)) {
131 dentries[i].file_type = EXT4_FT_DIR;
133 } else if (S_ISCHR(stat.st_mode)) {
134 dentries[i].file_type = EXT4_FT_CHRDEV;
135 } else if (S_ISBLK(stat.st_mode)) {
136 dentries[i].file_type = EXT4_FT_BLKDEV;
137 } else if (S_ISFIFO(stat.st_mode)) {
138 dentries[i].file_type = EXT4_FT_FIFO;
139 } else if (S_ISSOCK(stat.st_mode)) {
140 dentries[i].file_type = EXT4_FT_SOCK;
141 } else if (S_ISLNK(stat.st_mode)) {
142 dentries[i].file_type = EXT4_FT_SYMLINK;
143 dentries[i].link = calloc(info.block_size, 1);
144 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
146 error("unknown file type on %s", dentries[i].path);
153 inode = make_directory(dir_inode, entries, dentries, dirs);
155 for (i = 0; i < entries; i++) {
156 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
157 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
158 } else if (dentries[i].file_type == EXT4_FT_DIR) {
159 entry_inode = build_directory_structure(dentries[i].full_path,
160 dentries[i].path, inode, android);
161 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
162 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
164 error("unknown file type on %s", dentries[i].path);
167 *dentries[i].inode = entry_inode;
169 ret = inode_set_permissions(entry_inode, dentries[i].mode,
170 dentries[i].uid, dentries[i].gid);
172 error("failed to set permissions on %s\n", dentries[i].path);
174 free(dentries[i].path);
175 free(dentries[i].full_path);
176 free(dentries[i].link);
177 free((void *)dentries[i].filename);
184 static u32 compute_block_size()
189 static u32 compute_blocks_per_group()
191 return info.block_size * 8;
194 static u32 compute_inodes()
196 return DIV_ROUND_UP(info.len, info.block_size) / 4;
199 static u32 compute_inodes_per_group()
201 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
202 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
203 return DIV_ROUND_UP(info.inodes, block_groups);
206 static void usage(char *path)
208 fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
209 fprintf(stderr, " [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
210 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
211 fprintf(stderr, " <filename> [<directory>]\n");
214 int main(int argc, char **argv)
217 const char *filename = NULL;
218 const char *directory = NULL;
219 char *mountpoint = "";
225 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fz")) != -1) {
228 info.len = parse_num(optarg);
231 info.journal_blocks = parse_num(optarg);
234 info.block_size = parse_num(optarg);
237 info.blocks_per_group = parse_num(optarg);
240 info.inodes = parse_num(optarg);
243 info.inode_size = parse_num(optarg);
264 if (optind >= argc) {
265 fprintf(stderr, "Expected filename after options\n");
270 filename = argv[optind++];
273 directory = argv[optind++];
276 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
282 info.len = get_file_size(filename);
285 fprintf(stderr, "Need size of filesystem\n");
290 if (info.journal_blocks > 0)
291 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
293 if (info.block_size <= 0)
294 info.block_size = compute_block_size();
296 if (info.blocks_per_group <= 0)
297 info.blocks_per_group = compute_blocks_per_group();
299 if (info.inodes <= 0)
300 info.inodes = compute_inodes();
302 if (info.inode_size <= 0)
303 info.inode_size = 256;
305 if (info.label == NULL)
308 info.inodes_per_group = compute_inodes_per_group();
311 EXT4_FEATURE_COMPAT_RESIZE_INODE;
313 info.feat_ro_compat |=
314 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
315 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
317 info.feat_incompat |=
318 EXT4_FEATURE_INCOMPAT_EXTENTS |
319 EXT4_FEATURE_INCOMPAT_FILETYPE;
322 printf("Creating filesystem with parameters:\n");
323 printf(" Size: %llu\n", info.len);
324 printf(" Block size: %d\n", info.block_size);
325 printf(" Blocks per group: %d\n", info.blocks_per_group);
326 printf(" Inodes per group: %d\n", info.inodes_per_group);
327 printf(" Inode size: %d\n", info.inode_size);
328 printf(" Label: %s\n", info.label);
330 ext4_create_fs_aux_info();
332 printf(" Blocks: %llu\n", aux_info.len_blocks);
333 printf(" Block groups: %d\n", aux_info.groups);
334 printf(" Reserved block group size: %d\n", aux_info.bg_desc_reserve_blocks);
336 block_allocator_init();
340 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
341 error("failed to reserve first 10 inodes");
343 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
344 ext4_create_journal_inode();
346 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
347 ext4_create_resize_inode();
350 root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
352 root_inode_num = build_default_directory_structure();
354 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
355 inode_set_permissions(root_inode_num, root_mode, 0, 0);
359 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
360 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
361 aux_info.sb->s_inodes_count,
362 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
363 aux_info.sb->s_blocks_count_lo);
365 write_ext4_image(filename, gzip);