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.
17 #include "make_ext4fs.h"
18 #include "ext4_utils.h"
24 #include <sparse/sparse.h>
35 #include <sys/types.h>
41 /* These match the Linux definitions of these flags.
42 L_xx is defined to avoid conflicting with the win32 versions.
44 #define L_S_IRUSR 00400
45 #define L_S_IWUSR 00200
46 #define L_S_IXUSR 00100
47 #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
51 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
55 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
56 #define S_ISUID 0004000
57 #define S_ISGID 0002000
58 #define S_ISVTX 0001000
66 /* TODO: Not implemented:
67 Allocating blocks in the same block group as the file inode
68 Hash or binary tree directories
69 Special files: sockets, devices, fifos
72 static int filter_dot(const struct dirent *d)
74 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
77 static u32 build_default_directory_structure()
81 struct dentry dentries = {
82 .filename = "lost+found",
83 .file_type = EXT4_FT_DIR,
89 root_inode = make_directory(0, 1, &dentries, 1);
90 inode = make_directory(root_inode, 0, NULL, 0);
91 *dentries.inode = inode;
92 inode_set_permissions(inode, dentries.mode,
93 dentries.uid, dentries.gid, dentries.mtime);
99 /* Read a local directory and create the same tree in the generated filesystem.
100 Calls itself recursively with each directory in the given directory */
101 static u32 build_directory_structure(const char *full_path, const char *dir_path,
102 u32 dir_inode, fs_config_func_t fs_config_func,
103 struct selabel_handle *sehnd)
106 struct dentry *dentries;
107 struct dirent **namelist = NULL;
114 bool needs_lost_and_found = false;
117 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
119 error_errno("scandir");
120 return EXT4_ALLOCATE_FAILED;
124 if (dir_inode == 0) {
125 /* root directory, check if lost+found already exists */
126 for (i = 0; i < entries; i++)
127 if (strcmp(namelist[i]->d_name, "lost+found") == 0)
130 needs_lost_and_found = true;
133 dentries = calloc(entries, sizeof(struct dentry));
134 if (dentries == NULL)
135 critical_error_errno("malloc");
137 for (i = 0; i < entries; i++) {
138 dentries[i].filename = strdup(namelist[i]->d_name);
139 if (dentries[i].filename == NULL)
140 critical_error_errno("strdup");
142 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
143 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
147 ret = lstat(dentries[i].full_path, &stat);
149 error_errno("lstat");
155 dentries[i].size = stat.st_size;
156 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
157 dentries[i].mtime = stat.st_mtime;
158 if (fs_config_func != NULL) {
160 unsigned int mode = 0;
161 unsigned int uid = 0;
162 unsigned int gid = 0;
163 int dir = S_ISDIR(stat.st_mode);
164 fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
165 dentries[i].mode = mode;
166 dentries[i].uid = uid;
167 dentries[i].gid = gid;
169 error("can't set android permissions - built without android support");
175 asprintf(&sepath, "/%s", dentries[i].path);
176 if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
177 error("cannot lookup security context for %s", sepath);
180 // TODO make this a debug flag
181 if (dentries[i].secon)
182 printf("Labeling %s as %s\n", sepath, dentries[i].secon);
188 if (S_ISREG(stat.st_mode)) {
189 dentries[i].file_type = EXT4_FT_REG_FILE;
190 } else if (S_ISDIR(stat.st_mode)) {
191 dentries[i].file_type = EXT4_FT_DIR;
193 } else if (S_ISCHR(stat.st_mode)) {
194 dentries[i].file_type = EXT4_FT_CHRDEV;
195 } else if (S_ISBLK(stat.st_mode)) {
196 dentries[i].file_type = EXT4_FT_BLKDEV;
197 } else if (S_ISFIFO(stat.st_mode)) {
198 dentries[i].file_type = EXT4_FT_FIFO;
199 } else if (S_ISSOCK(stat.st_mode)) {
200 dentries[i].file_type = EXT4_FT_SOCK;
201 } else if (S_ISLNK(stat.st_mode)) {
202 dentries[i].file_type = EXT4_FT_SYMLINK;
203 dentries[i].link = calloc(info.block_size, 1);
204 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
206 error("unknown file type on %s", dentries[i].path);
213 if (needs_lost_and_found) {
214 /* insert a lost+found directory at the beginning of the dentries */
215 struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
216 memset(tmp, 0, sizeof(struct dentry));
217 memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
220 dentries[0].filename = strdup("lost+found");
221 asprintf(&dentries[0].path, "%s/lost+found", dir_path);
222 dentries[0].full_path = NULL;
223 dentries[0].size = 0;
224 dentries[0].mode = S_IRWXU;
225 dentries[0].file_type = EXT4_FT_DIR;
230 asprintf(&sepath, "/%s", dentries[0].path);
231 if (selabel_lookup(sehnd, &dentries[0].secon, sepath, dentries[0].mode) < 0)
232 error("cannot lookup security context for %s", dentries[0].path);
239 inode = make_directory(dir_inode, entries, dentries, dirs);
241 for (i = 0; i < entries; i++) {
242 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
243 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
244 } else if (dentries[i].file_type == EXT4_FT_DIR) {
245 entry_inode = build_directory_structure(dentries[i].full_path,
246 dentries[i].path, inode, fs_config_func, sehnd);
247 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
248 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
250 error("unknown file type on %s", dentries[i].path);
253 *dentries[i].inode = entry_inode;
255 ret = inode_set_permissions(entry_inode, dentries[i].mode,
256 dentries[i].uid, dentries[i].gid,
259 error("failed to set permissions on %s\n", dentries[i].path);
260 ret = inode_set_selinux(entry_inode, dentries[i].secon);
262 error("failed to set SELinux context on %s\n", dentries[i].path);
264 free(dentries[i].path);
265 free(dentries[i].full_path);
266 free(dentries[i].link);
267 free((void *)dentries[i].filename);
268 free(dentries[i].secon);
276 static u32 compute_block_size()
281 static u32 compute_journal_blocks()
283 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
284 if (journal_blocks < 1024)
285 journal_blocks = 1024;
286 if (journal_blocks > 32768)
287 journal_blocks = 32768;
288 return journal_blocks;
291 static u32 compute_blocks_per_group()
293 return info.block_size * 8;
296 static u32 compute_inodes()
298 return DIV_ROUND_UP(info.len, info.block_size) / 4;
301 static u32 compute_inodes_per_group()
303 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
304 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
305 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
306 inodes = ALIGN(inodes, (info.block_size / info.inode_size));
308 /* After properly rounding up the number of inodes/group,
309 * make sure to update the total inodes field in the info struct.
311 info.inodes = inodes * block_groups;
316 static u32 compute_bg_desc_reserve_blocks()
318 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
319 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
320 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
323 u32 bg_desc_reserve_blocks =
324 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
325 info.block_size) - bg_desc_blocks;
327 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
328 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
330 return bg_desc_reserve_blocks;
333 void reset_ext4fs_info() {
334 // Reset all the global data structures used by make_ext4fs so it
335 // can be called again.
336 memset(&info, 0, sizeof(info));
337 memset(&aux_info, 0, sizeof(aux_info));
339 if (info.sparse_file) {
340 sparse_file_destroy(info.sparse_file);
341 info.sparse_file = NULL;
345 int make_ext4fs(const char *filename, s64 len,
346 const char *mountpoint, struct selabel_handle *sehnd)
354 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
360 status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd);
366 int make_ext4fs_internal(int fd, const char *directory,
367 const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
368 int sparse, int crc, int wipe, int init_itabs,
369 struct selabel_handle *sehnd)
374 if (setjmp(setjmp_env))
375 return EXIT_FAILURE; /* Handle a call to longjmp() */
378 info.len = get_file_size(fd);
381 fprintf(stderr, "Need size of filesystem\n");
385 if (info.block_size <= 0)
386 info.block_size = compute_block_size();
388 /* Round down the filesystem length to be a multiple of the block size */
389 info.len &= ~((u64)info.block_size - 1);
391 if (info.journal_blocks == 0)
392 info.journal_blocks = compute_journal_blocks();
394 if (info.no_journal == 0)
395 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
397 info.journal_blocks = 0;
399 if (info.blocks_per_group <= 0)
400 info.blocks_per_group = compute_blocks_per_group();
402 if (info.inodes <= 0)
403 info.inodes = compute_inodes();
405 if (info.inode_size <= 0)
406 info.inode_size = 256;
408 if (info.label == NULL)
411 info.inodes_per_group = compute_inodes_per_group();
414 EXT4_FEATURE_COMPAT_RESIZE_INODE;
416 info.feat_ro_compat |=
417 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
418 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
420 info.feat_incompat |=
421 EXT4_FEATURE_INCOMPAT_EXTENTS |
422 EXT4_FEATURE_INCOMPAT_FILETYPE;
425 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
427 printf("Creating filesystem with parameters:\n");
428 printf(" Size: %llu\n", info.len);
429 printf(" Block size: %d\n", info.block_size);
430 printf(" Blocks per group: %d\n", info.blocks_per_group);
431 printf(" Inodes per group: %d\n", info.inodes_per_group);
432 printf(" Inode size: %d\n", info.inode_size);
433 printf(" Journal blocks: %d\n", info.journal_blocks);
434 printf(" Label: %s\n", info.label);
436 ext4_create_fs_aux_info();
438 printf(" Blocks: %llu\n", aux_info.len_blocks);
439 printf(" Block groups: %d\n", aux_info.groups);
440 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
442 info.sparse_file = sparse_file_new(info.block_size, info.len);
444 block_allocator_init();
448 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
449 error("failed to reserve first 10 inodes");
451 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
452 ext4_create_journal_inode();
454 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
455 ext4_create_resize_inode();
458 // Windows needs only 'create an empty fs image' functionality
460 root_inode_num = build_default_directory_structure();
463 root_inode_num = build_directory_structure(directory, mountpoint, 0,
464 fs_config_func, sehnd);
466 root_inode_num = build_default_directory_structure();
469 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
470 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
475 char *secontext = NULL;
477 if (mountpoint[0] == '/')
478 sepath = strdup(mountpoint);
480 asprintf(&sepath, "/%s", mountpoint);
482 critical_error_errno("malloc");
483 if (selabel_lookup(sehnd, &secontext, sepath, S_IFDIR) < 0) {
484 error("cannot lookup security context for %s", sepath);
487 printf("Labeling %s as %s\n", sepath, secontext);
488 inode_set_selinux(root_inode_num, secontext);
498 init_unused_inode_tables();
502 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
503 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
504 aux_info.sb->s_inodes_count,
505 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
506 aux_info.sb->s_blocks_count_lo);
509 wipe_block_device(fd, info.len);
511 write_ext4_image(fd, gzip, sparse, crc);
513 sparse_file_destroy(info.sparse_file);
514 info.sparse_file = NULL;