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 "ext4_utils.h"
23 #include <sparse/sparse.h>
35 #include <sys/types.h>
37 /* TODO: Not implemented:
38 Allocating blocks in the same block group as the file inode
39 Hash or binary tree directories
42 static int filter_dot(const struct dirent *d)
44 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
47 static u32 build_default_directory_structure(const char *dir_path)
51 struct dentry dentries = {
52 .filename = "lost+found",
53 .file_type = EXT4_FT_DIR,
59 root_inode = make_directory(0, 1, &dentries, 1);
60 inode = make_directory(root_inode, 0, NULL, 0);
61 *dentries.inode = inode;
62 inode_set_permissions(inode, dentries.mode,
63 dentries.uid, dentries.gid, dentries.mtime);
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 full_path is an absolute or relative path, with a trailing slash, to the
71 directory on disk that should be copied, or NULL if this is a directory
72 that does not exist on disk (e.g. lost+found).
73 dir_path is an absolute path, with trailing slash, to the same directory
74 if the image were mounted at the specified mount point */
75 static u32 build_directory_structure(const char *full_path, const char *dir_path,
76 u32 dir_inode, fs_config_func_t fs_config_func,
77 int verbose, time_t fixed_time)
80 struct dentry *dentries;
81 struct dirent **namelist = NULL;
88 bool needs_lost_and_found = false;
91 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
94 /* The scandir function implemented in glibc has a bug that makes it
95 erroneously fail with ENOMEM under certain circumstances.
96 As a workaround we can retry the scandir call with the same arguments.
97 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
99 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
102 error_errno("scandir");
103 return EXT4_ALLOCATE_FAILED;
108 if (dir_inode == 0) {
109 /* root directory, check if lost+found already exists */
110 for (i = 0; i < entries; i++)
111 if (strcmp(namelist[i]->d_name, "lost+found") == 0)
114 needs_lost_and_found = true;
117 dentries = calloc(entries, sizeof(struct dentry));
118 if (dentries == NULL)
119 critical_error_errno("malloc");
121 for (i = 0; i < entries; i++) {
122 dentries[i].filename = strdup(namelist[i]->d_name);
123 if (dentries[i].filename == NULL)
124 critical_error_errno("strdup");
126 asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
127 asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
131 ret = lstat(dentries[i].full_path, &stat);
133 error_errno("lstat");
139 dentries[i].size = stat.st_size;
140 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
141 if (fixed_time == -1) {
142 dentries[i].mtime = stat.st_mtime;
144 dentries[i].mtime = fixed_time;
146 uint64_t capabilities;
147 if (fs_config_func != NULL) {
148 unsigned int mode = 0;
149 unsigned int uid = 0;
150 unsigned int gid = 0;
151 int dir = S_ISDIR(stat.st_mode);
152 if (fs_config_func(dentries[i].path, dir, &uid, &gid, &mode, &capabilities)) {
153 dentries[i].mode = mode;
154 dentries[i].uid = uid;
155 dentries[i].gid = gid;
156 dentries[i].capabilities = capabilities;
160 if (S_ISREG(stat.st_mode)) {
161 dentries[i].file_type = EXT4_FT_REG_FILE;
162 } else if (S_ISDIR(stat.st_mode)) {
163 dentries[i].file_type = EXT4_FT_DIR;
165 } else if (S_ISCHR(stat.st_mode)) {
166 dentries[i].file_type = EXT4_FT_CHRDEV;
167 } else if (S_ISBLK(stat.st_mode)) {
168 dentries[i].file_type = EXT4_FT_BLKDEV;
169 } else if (S_ISFIFO(stat.st_mode)) {
170 dentries[i].file_type = EXT4_FT_FIFO;
171 } else if (S_ISSOCK(stat.st_mode)) {
172 dentries[i].file_type = EXT4_FT_SOCK;
173 } else if (S_ISLNK(stat.st_mode)) {
174 dentries[i].file_type = EXT4_FT_SYMLINK;
175 dentries[i].link = calloc(info.block_size, 1);
176 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
178 error("unknown file type on %s", dentries[i].path);
185 if (needs_lost_and_found) {
186 /* insert a lost+found directory at the beginning of the dentries */
187 struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
188 memset(tmp, 0, sizeof(struct dentry));
189 memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
192 dentries[0].filename = strdup("lost+found");
193 asprintf(&dentries[0].path, "%slost+found", dir_path);
194 dentries[0].full_path = NULL;
195 dentries[0].size = 0;
196 dentries[0].mode = S_IRWXU;
197 dentries[0].file_type = EXT4_FT_DIR;
204 inode = make_directory(dir_inode, entries, dentries, dirs);
206 for (i = 0; i < entries; i++) {
207 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
208 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
209 } else if (dentries[i].file_type == EXT4_FT_DIR) {
210 char *subdir_full_path = NULL;
211 char *subdir_dir_path;
212 if (dentries[i].full_path) {
213 ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
215 critical_error_errno("asprintf");
217 ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
219 critical_error_errno("asprintf");
220 entry_inode = build_directory_structure(subdir_full_path,
221 subdir_dir_path, inode, fs_config_func, verbose, fixed_time);
222 free(subdir_full_path);
223 free(subdir_dir_path);
224 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
225 entry_inode = make_link(dentries[i].link);
226 } else if (dentries[i].file_type == EXT4_FT_CHRDEV ||
227 dentries[i].file_type == EXT4_FT_BLKDEV ||
228 dentries[i].file_type == EXT4_FT_SOCK ||
229 dentries[i].file_type == EXT4_FT_FIFO) {
230 entry_inode = make_special(dentries[i].full_path);
232 error("unknown file type on %s", dentries[i].path);
235 *dentries[i].inode = entry_inode;
237 ret = inode_set_permissions(entry_inode, dentries[i].mode,
238 dentries[i].uid, dentries[i].gid,
241 error("failed to set permissions on %s\n", dentries[i].path);
243 ret = inode_set_capabilities(entry_inode, dentries[i].capabilities);
245 error("failed to set capability on %s\n", dentries[i].path);
247 free(dentries[i].path);
248 free(dentries[i].full_path);
249 free(dentries[i].link);
250 free((void *)dentries[i].filename);
257 static u32 compute_block_size()
262 static u32 compute_journal_blocks()
264 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
265 if (journal_blocks < 1024)
266 journal_blocks = 1024;
267 if (journal_blocks > 32768)
268 journal_blocks = 32768;
269 return journal_blocks;
272 static u32 compute_blocks_per_group()
274 return info.block_size * 8;
277 static u32 compute_inodes()
279 return DIV_ROUND_UP(info.len, info.block_size) / 4;
282 static u32 compute_inodes_per_group()
284 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
285 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
286 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
287 inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size));
289 /* After properly rounding up the number of inodes/group,
290 * make sure to update the total inodes field in the info struct.
292 info.inodes = inodes * block_groups;
297 static u32 compute_bg_desc_reserve_blocks()
299 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
300 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
301 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
304 u32 bg_desc_reserve_blocks =
305 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
306 info.block_size) - bg_desc_blocks;
308 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
309 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
311 return bg_desc_reserve_blocks;
314 /* return a newly-malloc'd string that is a copy of str. The new string
315 is guaranteed to have a trailing slash. If absolute is true, the new string
316 is also guaranteed to have a leading slash.
318 static char *canonicalize_slashes(const char *str, bool absolute)
321 int len = strlen(str);
332 if (str[0] != '/' && absolute) {
335 if (str[len - 1] != '/') {
338 ret = malloc(newlen + 1);
340 critical_error("malloc");
344 if (str[0] != '/' && absolute) {
351 if (str[len - 1] != '/') {
355 if (ptr != ret + newlen) {
356 critical_error("assertion failed\n");
364 static char *canonicalize_abs_slashes(const char *str)
366 return canonicalize_slashes(str, true);
369 static char *canonicalize_rel_slashes(const char *str)
371 return canonicalize_slashes(str, false);
374 int make_ext4fs_internal(int fd, const char *_directory,
375 fs_config_func_t fs_config_func, int gzip,
376 int sparse, int crc, int wipe,
377 int verbose, time_t fixed_time,
378 FILE* block_list_file)
382 char *directory = NULL;
384 if (setjmp(setjmp_env))
385 return EXIT_FAILURE; /* Handle a call to longjmp() */
387 if (_directory == NULL) {
388 fprintf(stderr, "Need a source directory\n");
392 directory = canonicalize_rel_slashes(_directory);
395 info.len = get_file_size(fd);
398 fprintf(stderr, "Need size of filesystem\n");
402 if (info.block_size <= 0)
403 info.block_size = compute_block_size();
405 /* Round down the filesystem length to be a multiple of the block size */
406 info.len &= ~((u64)info.block_size - 1);
408 if (info.journal_blocks == 0)
409 info.journal_blocks = compute_journal_blocks();
411 if (info.no_journal == 0)
412 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
414 info.journal_blocks = 0;
416 if (info.blocks_per_group <= 0)
417 info.blocks_per_group = compute_blocks_per_group();
419 if (info.inodes <= 0)
420 info.inodes = compute_inodes();
422 if (info.inode_size <= 0)
423 info.inode_size = 256;
425 if (info.label == NULL)
428 info.inodes_per_group = compute_inodes_per_group();
431 EXT4_FEATURE_COMPAT_RESIZE_INODE |
432 EXT4_FEATURE_COMPAT_EXT_ATTR;
434 info.feat_ro_compat |=
435 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
436 EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
437 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
439 info.feat_incompat |=
440 EXT4_FEATURE_INCOMPAT_EXTENTS |
441 EXT4_FEATURE_INCOMPAT_FILETYPE;
444 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
446 printf("Creating filesystem with parameters:\n");
447 printf(" Size: %"PRIu64"\n", info.len);
448 printf(" Block size: %d\n", info.block_size);
449 printf(" Blocks per group: %d\n", info.blocks_per_group);
450 printf(" Inodes per group: %d\n", info.inodes_per_group);
451 printf(" Inode size: %d\n", info.inode_size);
452 printf(" Journal blocks: %d\n", info.journal_blocks);
453 printf(" Label: %s\n", info.label);
455 ext4_create_fs_aux_info();
457 printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks);
458 printf(" Block groups: %d\n", aux_info.groups);
459 printf(" Reserved blocks: %"PRIu64"\n", (aux_info.len_blocks / 100) * info.reserve_pcnt);
460 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
462 ext4_sparse_file = sparse_file_new(info.block_size, info.len);
464 block_allocator_init();
468 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
469 error("failed to reserve first 10 inodes");
471 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
472 ext4_create_journal_inode();
474 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
475 ext4_create_resize_inode();
477 root_inode_num = build_directory_structure(directory, "", 0,
478 fs_config_func, verbose, fixed_time);
480 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
481 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
487 if (block_list_file) {
488 size_t dirlen = strlen(directory);
489 struct block_allocation* p = get_saved_allocation_chain();
491 if (strncmp(p->filename, directory, dirlen) == 0) {
492 fprintf(block_list_file, "%s", p->filename + dirlen);
494 fprintf(block_list_file, "%s", p->filename);
496 print_blocks(block_list_file, p);
497 struct block_allocation* pn = p->next;
503 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
504 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
505 aux_info.sb->s_inodes_count,
506 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
507 aux_info.sb->s_blocks_count_lo);
509 if (wipe && WIPE_IS_SUPPORTED) {
510 wipe_block_device(fd, info.len);
513 write_ext4_image(fd, gzip, sparse, crc);
515 sparse_file_destroy(ext4_sparse_file);
516 ext4_sparse_file = NULL;