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,
62 root_inode = make_directory(0, 1, &dentries, 1);
63 inode = make_directory(root_inode, 0, NULL, 0);
64 *dentries.inode = inode;
69 /* Read a local directory and create the same tree in the generated filesystem.
70 Calls itself recursively with each directory in the given directory */
71 static u32 build_directory_structure(const char *full_path, const char *dir_path,
72 u32 dir_inode, int android)
75 struct dentry *dentries;
76 struct dirent **namelist;
84 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
86 error_errno("scandir");
87 return EXT4_ALLOCATE_FAILED;
90 dentries = calloc(entries, sizeof(struct dentry));
92 critical_error_errno("malloc");
94 for (i = 0; i < entries; i++) {
95 dentries[i].filename = strdup(namelist[i]->d_name);
96 if (dentries[i].filename == NULL)
97 critical_error_errno("strdup");
99 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
100 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
104 ret = lstat(dentries[i].full_path, &stat);
106 error_errno("lstat");
112 dentries[i].size = stat.st_size;
113 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
114 dentries[i].mtime = stat.st_mtime;
117 unsigned int mode = 0;
118 unsigned int uid = 0;
119 unsigned int gid = 0;
120 int dir = S_ISDIR(stat.st_mode);
121 fs_config(dentries[i].path, dir, &uid, &gid, &mode);
122 dentries[i].mode = mode;
123 dentries[i].uid = uid;
124 dentries[i].gid = gid;
126 error("can't set android permissions - built without android support");
130 if (S_ISREG(stat.st_mode)) {
131 dentries[i].file_type = EXT4_FT_REG_FILE;
132 } else if (S_ISDIR(stat.st_mode)) {
133 dentries[i].file_type = EXT4_FT_DIR;
135 } else if (S_ISCHR(stat.st_mode)) {
136 dentries[i].file_type = EXT4_FT_CHRDEV;
137 } else if (S_ISBLK(stat.st_mode)) {
138 dentries[i].file_type = EXT4_FT_BLKDEV;
139 } else if (S_ISFIFO(stat.st_mode)) {
140 dentries[i].file_type = EXT4_FT_FIFO;
141 } else if (S_ISSOCK(stat.st_mode)) {
142 dentries[i].file_type = EXT4_FT_SOCK;
143 } else if (S_ISLNK(stat.st_mode)) {
144 dentries[i].file_type = EXT4_FT_SYMLINK;
145 dentries[i].link = calloc(info.block_size, 1);
146 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
148 error("unknown file type on %s", dentries[i].path);
155 inode = make_directory(dir_inode, entries, dentries, dirs);
157 for (i = 0; i < entries; i++) {
158 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
159 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
160 } else if (dentries[i].file_type == EXT4_FT_DIR) {
161 entry_inode = build_directory_structure(dentries[i].full_path,
162 dentries[i].path, inode, android);
163 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
164 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
166 error("unknown file type on %s", dentries[i].path);
169 *dentries[i].inode = entry_inode;
171 ret = inode_set_permissions(entry_inode, dentries[i].mode,
172 dentries[i].uid, dentries[i].gid,
175 error("failed to set permissions on %s\n", dentries[i].path);
177 free(dentries[i].path);
178 free(dentries[i].full_path);
179 free(dentries[i].link);
180 free((void *)dentries[i].filename);
187 static u32 compute_block_size()
192 static u32 compute_journal_blocks()
194 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
195 if (journal_blocks < 1024)
196 journal_blocks = 1024;
197 if (journal_blocks > 32768)
198 journal_blocks = 32768;
199 return journal_blocks;
202 static u32 compute_blocks_per_group()
204 return info.block_size * 8;
207 static u32 compute_inodes()
209 return DIV_ROUND_UP(info.len, info.block_size) / 4;
212 static u32 compute_inodes_per_group()
214 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
215 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
216 return DIV_ROUND_UP(info.inodes, block_groups);
219 void reset_ext4fs_info() {
220 // Reset all the global data structures used by make_ext4fs so it
221 // can be called again.
222 memset(&info, 0, sizeof(info));
223 memset(&aux_info, 0, sizeof(aux_info));
227 int make_ext4fs(const char *filename, const char *directory,
228 char *mountpoint, int android, int gzip)
234 info.len = get_file_size(filename);
237 fprintf(stderr, "Need size of filesystem\n");
241 if (info.block_size <= 0)
242 info.block_size = compute_block_size();
244 if (info.journal_blocks == 0)
245 info.journal_blocks = compute_journal_blocks();
247 if (info.no_journal == 0)
248 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
250 info.journal_blocks = 0;
252 if (info.blocks_per_group <= 0)
253 info.blocks_per_group = compute_blocks_per_group();
255 if (info.inodes <= 0)
256 info.inodes = compute_inodes();
258 if (info.inode_size <= 0)
259 info.inode_size = 256;
261 if (info.label == NULL)
264 info.inodes_per_group = compute_inodes_per_group();
267 EXT4_FEATURE_COMPAT_RESIZE_INODE;
269 info.feat_ro_compat |=
270 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
271 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
273 info.feat_incompat |=
274 EXT4_FEATURE_INCOMPAT_EXTENTS |
275 EXT4_FEATURE_INCOMPAT_FILETYPE;
278 printf("Creating filesystem with parameters:\n");
279 printf(" Size: %llu\n", info.len);
280 printf(" Block size: %d\n", info.block_size);
281 printf(" Blocks per group: %d\n", info.blocks_per_group);
282 printf(" Inodes per group: %d\n", info.inodes_per_group);
283 printf(" Inode size: %d\n", info.inode_size);
284 printf(" Journal blocks: %d\n", info.journal_blocks);
285 printf(" Label: %s\n", info.label);
287 ext4_create_fs_aux_info();
289 printf(" Blocks: %llu\n", aux_info.len_blocks);
290 printf(" Block groups: %d\n", aux_info.groups);
291 printf(" Reserved block group size: %d\n", aux_info.bg_desc_reserve_blocks);
293 block_allocator_init();
297 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
298 error("failed to reserve first 10 inodes");
300 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
301 ext4_create_journal_inode();
303 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
304 ext4_create_resize_inode();
307 root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
309 root_inode_num = build_default_directory_structure();
311 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
312 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
316 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
317 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
318 aux_info.sb->s_inodes_count,
319 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
320 aux_info.sb->s_blocks_count_lo);
322 write_ext4_image(filename, gzip);