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 /* Read a local directory and create the same tree in the generated filesystem.
48 Calls itself recursively with each directory in the given directory.
49 full_path is an absolute or relative path, with a trailing slash, to the
50 directory on disk that should be copied, or NULL if this is a directory
51 that does not exist on disk (e.g. lost+found).
52 dir_path is an absolute path, with trailing slash, to the same directory
53 if the image were mounted at the specified mount point */
54 static u32
build_directory_structure(const char *full_path
, const char *dir_path
,
55 u32 dir_inode
, fs_config_func_t fs_config_func
,
56 int verbose
, time_t fixed_time
)
59 struct dentry
*dentries
;
60 struct dirent
**namelist
= NULL
;
67 bool needs_lost_and_found
= false;
70 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
73 /* The scandir function implemented in glibc has a bug that makes it
74 erroneously fail with ENOMEM under certain circumstances.
75 As a workaround we can retry the scandir call with the same arguments.
76 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
78 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
81 error_errno("scandir");
82 return EXT4_ALLOCATE_FAILED
;
88 /* root directory, check if lost+found already exists */
89 for (i
= 0; i
< entries
; i
++)
90 if (strcmp(namelist
[i
]->d_name
, "lost+found") == 0)
93 needs_lost_and_found
= true;
96 dentries
= calloc(entries
, sizeof(struct dentry
));
98 critical_error_errno("malloc");
100 for (i
= 0; i
< entries
; i
++) {
101 dentries
[i
].filename
= strdup(namelist
[i
]->d_name
);
102 if (dentries
[i
].filename
== NULL
)
103 critical_error_errno("strdup");
105 asprintf(&dentries
[i
].path
, "%s%s", dir_path
, namelist
[i
]->d_name
);
106 asprintf(&dentries
[i
].full_path
, "%s%s", full_path
, namelist
[i
]->d_name
);
110 ret
= lstat(dentries
[i
].full_path
, &stat
);
112 error_errno("lstat");
118 dentries
[i
].size
= stat
.st_size
;
119 dentries
[i
].mode
= stat
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
120 if (fixed_time
== -1) {
121 dentries
[i
].mtime
= stat
.st_mtime
;
123 dentries
[i
].mtime
= fixed_time
;
125 uint64_t capabilities
;
126 if (fs_config_func
!= NULL
) {
127 unsigned int mode
= 0;
128 unsigned int uid
= 0;
129 unsigned int gid
= 0;
130 int dir
= S_ISDIR(stat
.st_mode
);
131 if (fs_config_func(dentries
[i
].path
, dir
, &uid
, &gid
, &mode
, &capabilities
)) {
132 dentries
[i
].mode
= mode
;
133 dentries
[i
].uid
= uid
;
134 dentries
[i
].gid
= gid
;
135 dentries
[i
].capabilities
= capabilities
;
139 if (S_ISREG(stat
.st_mode
)) {
140 dentries
[i
].file_type
= EXT4_FT_REG_FILE
;
141 } else if (S_ISDIR(stat
.st_mode
)) {
142 dentries
[i
].file_type
= EXT4_FT_DIR
;
144 } else if (S_ISCHR(stat
.st_mode
)) {
145 dentries
[i
].file_type
= EXT4_FT_CHRDEV
;
146 } else if (S_ISBLK(stat
.st_mode
)) {
147 dentries
[i
].file_type
= EXT4_FT_BLKDEV
;
148 } else if (S_ISFIFO(stat
.st_mode
)) {
149 dentries
[i
].file_type
= EXT4_FT_FIFO
;
150 } else if (S_ISSOCK(stat
.st_mode
)) {
151 dentries
[i
].file_type
= EXT4_FT_SOCK
;
152 } else if (S_ISLNK(stat
.st_mode
)) {
153 dentries
[i
].file_type
= EXT4_FT_SYMLINK
;
154 dentries
[i
].link
= calloc(info
.block_size
, 1);
155 readlink(dentries
[i
].full_path
, dentries
[i
].link
, info
.block_size
- 1);
157 error("unknown file type on %s", dentries
[i
].path
);
164 if (needs_lost_and_found
) {
165 /* insert a lost+found directory at the beginning of the dentries */
166 struct dentry
*tmp
= calloc(entries
+ 1, sizeof(struct dentry
));
167 memset(tmp
, 0, sizeof(struct dentry
));
168 memcpy(tmp
+ 1, dentries
, entries
* sizeof(struct dentry
));
171 dentries
[0].filename
= strdup("lost+found");
172 asprintf(&dentries
[0].path
, "%slost+found", dir_path
);
173 dentries
[0].full_path
= NULL
;
174 dentries
[0].size
= 0;
175 dentries
[0].mode
= S_IRWXU
;
176 dentries
[0].file_type
= EXT4_FT_DIR
;
183 inode
= make_directory(dir_inode
, entries
, dentries
, dirs
);
185 for (i
= 0; i
< entries
; i
++) {
186 if (dentries
[i
].file_type
== EXT4_FT_REG_FILE
) {
187 entry_inode
= make_file(dentries
[i
].full_path
, dentries
[i
].size
);
188 } else if (dentries
[i
].file_type
== EXT4_FT_DIR
) {
189 char *subdir_full_path
= NULL
;
190 char *subdir_dir_path
;
191 if (dentries
[i
].full_path
) {
192 ret
= asprintf(&subdir_full_path
, "%s/", dentries
[i
].full_path
);
194 critical_error_errno("asprintf");
196 ret
= asprintf(&subdir_dir_path
, "%s/", dentries
[i
].path
);
198 critical_error_errno("asprintf");
199 entry_inode
= build_directory_structure(subdir_full_path
,
200 subdir_dir_path
, inode
, fs_config_func
, verbose
, fixed_time
);
201 free(subdir_full_path
);
202 free(subdir_dir_path
);
203 } else if (dentries
[i
].file_type
== EXT4_FT_SYMLINK
) {
204 entry_inode
= make_link(dentries
[i
].link
);
205 } else if (dentries
[i
].file_type
== EXT4_FT_CHRDEV
||
206 dentries
[i
].file_type
== EXT4_FT_BLKDEV
||
207 dentries
[i
].file_type
== EXT4_FT_SOCK
||
208 dentries
[i
].file_type
== EXT4_FT_FIFO
) {
209 entry_inode
= make_special(dentries
[i
].full_path
);
211 error("unknown file type on %s", dentries
[i
].path
);
214 *dentries
[i
].inode
= entry_inode
;
216 ret
= inode_set_permissions(entry_inode
, dentries
[i
].mode
,
217 dentries
[i
].uid
, dentries
[i
].gid
,
220 error("failed to set permissions on %s\n", dentries
[i
].path
);
222 ret
= inode_set_capabilities(entry_inode
, dentries
[i
].capabilities
);
224 error("failed to set capability on %s\n", dentries
[i
].path
);
226 free(dentries
[i
].path
);
227 free(dentries
[i
].full_path
);
228 free(dentries
[i
].link
);
229 free((void *)dentries
[i
].filename
);
236 static u32
compute_block_size()
241 static u32
compute_journal_blocks()
243 u32 journal_blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
) / 64;
244 if (journal_blocks
< 1024)
245 journal_blocks
= 1024;
246 if (journal_blocks
> 32768)
247 journal_blocks
= 32768;
248 return journal_blocks
;
251 static u32
compute_blocks_per_group()
253 return info
.block_size
* 8;
256 static u32
compute_inodes()
258 return DIV_ROUND_UP(info
.len
, info
.block_size
) / 4;
261 static u32
compute_inodes_per_group()
263 u32 blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
);
264 u32 block_groups
= DIV_ROUND_UP(blocks
, info
.blocks_per_group
);
265 u32 inodes
= DIV_ROUND_UP(info
.inodes
, block_groups
);
266 inodes
= EXT4_ALIGN(inodes
, (info
.block_size
/ info
.inode_size
));
268 /* After properly rounding up the number of inodes/group,
269 * make sure to update the total inodes field in the info struct.
271 info
.inodes
= inodes
* block_groups
;
276 static u32
compute_bg_desc_reserve_blocks()
278 u32 blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
);
279 u32 block_groups
= DIV_ROUND_UP(blocks
, info
.blocks_per_group
);
280 u32 bg_desc_blocks
= DIV_ROUND_UP(block_groups
* sizeof(struct ext2_group_desc
),
283 u32 bg_desc_reserve_blocks
=
284 DIV_ROUND_UP(block_groups
* 1024 * sizeof(struct ext2_group_desc
),
285 info
.block_size
) - bg_desc_blocks
;
287 if (bg_desc_reserve_blocks
> info
.block_size
/ sizeof(u32
))
288 bg_desc_reserve_blocks
= info
.block_size
/ sizeof(u32
);
290 return bg_desc_reserve_blocks
;
293 /* return a newly-malloc'd string that is a copy of str. The new string
294 is guaranteed to have a trailing slash. If absolute is true, the new string
295 is also guaranteed to have a leading slash.
297 static char *canonicalize_slashes(const char *str
, bool absolute
)
300 int len
= strlen(str
);
311 if (str
[0] != '/' && absolute
) {
314 if (str
[len
- 1] != '/') {
317 ret
= malloc(newlen
+ 1);
319 critical_error("malloc");
323 if (str
[0] != '/' && absolute
) {
330 if (str
[len
- 1] != '/') {
334 if (ptr
!= ret
+ newlen
) {
335 critical_error("assertion failed\n");
343 static char *canonicalize_abs_slashes(const char *str
)
345 return canonicalize_slashes(str
, true);
348 static char *canonicalize_rel_slashes(const char *str
)
350 return canonicalize_slashes(str
, false);
353 int make_ext4fs_internal(int fd
, const char *_directory
,
354 fs_config_func_t fs_config_func
, int gzip
,
355 int sparse
, int crc
, int wipe
,
356 int verbose
, time_t fixed_time
,
357 FILE* block_list_file
)
361 char *directory
= NULL
;
363 if (setjmp(setjmp_env
))
364 return EXIT_FAILURE
; /* Handle a call to longjmp() */
366 if (_directory
== NULL
) {
367 fprintf(stderr
, "Need a source directory\n");
371 directory
= canonicalize_rel_slashes(_directory
);
374 info
.len
= get_file_size(fd
);
377 fprintf(stderr
, "Need size of filesystem\n");
381 if (info
.block_size
<= 0)
382 info
.block_size
= compute_block_size();
384 /* Round down the filesystem length to be a multiple of the block size */
385 info
.len
&= ~((u64
)info
.block_size
- 1);
387 if (info
.journal_blocks
== 0)
388 info
.journal_blocks
= compute_journal_blocks();
390 if (info
.no_journal
== 0)
391 info
.feat_compat
= EXT4_FEATURE_COMPAT_HAS_JOURNAL
;
393 info
.journal_blocks
= 0;
395 if (info
.blocks_per_group
<= 0)
396 info
.blocks_per_group
= compute_blocks_per_group();
398 if (info
.inodes
<= 0)
399 info
.inodes
= compute_inodes();
401 if (info
.inode_size
<= 0)
402 info
.inode_size
= 256;
404 if (info
.label
== NULL
)
407 info
.inodes_per_group
= compute_inodes_per_group();
410 EXT4_FEATURE_COMPAT_RESIZE_INODE
|
411 EXT4_FEATURE_COMPAT_EXT_ATTR
;
413 info
.feat_ro_compat
|=
414 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
|
415 EXT4_FEATURE_RO_COMPAT_LARGE_FILE
|
416 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
;
418 info
.feat_incompat
|=
419 EXT4_FEATURE_INCOMPAT_EXTENTS
|
420 EXT4_FEATURE_INCOMPAT_FILETYPE
;
423 info
.bg_desc_reserve_blocks
= compute_bg_desc_reserve_blocks();
425 printf("Creating filesystem with parameters:\n");
426 printf(" Size: %"PRIu64
"\n", info
.len
);
427 printf(" Block size: %d\n", info
.block_size
);
428 printf(" Blocks per group: %d\n", info
.blocks_per_group
);
429 printf(" Inodes per group: %d\n", info
.inodes_per_group
);
430 printf(" Inode size: %d\n", info
.inode_size
);
431 printf(" Journal blocks: %d\n", info
.journal_blocks
);
432 printf(" Label: %s\n", info
.label
);
434 ext4_create_fs_aux_info();
436 printf(" Blocks: %"PRIu64
"\n", aux_info
.len_blocks
);
437 printf(" Block groups: %d\n", aux_info
.groups
);
438 printf(" Reserved blocks: %"PRIu64
"\n", (aux_info
.len_blocks
/ 100) * info
.reserve_pcnt
);
439 printf(" Reserved block group size: %d\n", info
.bg_desc_reserve_blocks
);
441 ext4_sparse_file
= sparse_file_new(info
.block_size
, info
.len
);
443 block_allocator_init();
447 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED
)
448 error("failed to reserve first 10 inodes");
450 if (info
.feat_compat
& EXT4_FEATURE_COMPAT_HAS_JOURNAL
)
451 ext4_create_journal_inode();
453 if (info
.feat_compat
& EXT4_FEATURE_COMPAT_RESIZE_INODE
)
454 ext4_create_resize_inode();
456 root_inode_num
= build_directory_structure(directory
, "", 0,
457 fs_config_func
, verbose
, fixed_time
);
459 root_mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
460 inode_set_permissions(root_inode_num
, root_mode
, 0, 0, 0);
466 if (block_list_file
) {
467 size_t dirlen
= strlen(directory
);
468 struct block_allocation
* p
= get_saved_allocation_chain();
470 if (strncmp(p
->filename
, directory
, dirlen
) == 0) {
471 fprintf(block_list_file
, "%s", p
->filename
+ dirlen
);
473 fprintf(block_list_file
, "%s", p
->filename
);
475 print_blocks(block_list_file
, p
);
476 struct block_allocation
* pn
= p
->next
;
482 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
483 aux_info
.sb
->s_inodes_count
- aux_info
.sb
->s_free_inodes_count
,
484 aux_info
.sb
->s_inodes_count
,
485 aux_info
.sb
->s_blocks_count_lo
- aux_info
.sb
->s_free_blocks_count_lo
,
486 aux_info
.sb
->s_blocks_count_lo
);
488 if (wipe
&& WIPE_IS_SUPPORTED
) {
489 wipe_block_device(fd
, info
.len
);
492 write_ext4_image(fd
, gzip
, sparse
, crc
);
494 sparse_file_destroy(ext4_sparse_file
);
495 ext4_sparse_file
= NULL
;