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>
38 /* TODO: Not implemented:
39 Allocating blocks in the same block group as the file inode
40 Hash or binary tree directories
43 static int filter_dot(const struct dirent
*d
)
45 return (strcmp(d
->d_name
, "..") && strcmp(d
->d_name
, "."));
48 static u32
build_default_directory_structure(time_t fixed_time
)
52 struct dentry dentries
= {
53 .filename
= "lost+found",
54 .file_type
= EXT4_FT_DIR
,
58 .mtime
= (fixed_time
!= -1) ? fixed_time
: 0,
60 root_inode
= make_directory(0, 1, &dentries
, 1);
61 inode
= make_directory(root_inode
, 0, NULL
, 0);
62 *dentries
.inode
= inode
;
63 inode_set_permissions(inode
, dentries
.mode
,
64 dentries
.uid
, dentries
.gid
, dentries
.mtime
);
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 full_path is an absolute or relative path, with a trailing slash, to the
72 directory on disk that should be copied, or NULL if this is a directory
73 that does not exist on disk (e.g. lost+found).
74 dir_path is an absolute path, with trailing slash, to the same directory
75 if the image were mounted at the specified mount point */
76 static u32
build_directory_structure(const char *full_path
, const char *dir_path
,
77 u32 dir_inode
, fs_config_func_t fs_config_func
,
78 int verbose
, time_t fixed_time
)
81 struct dentry
*dentries
;
82 struct dirent
**namelist
= NULL
;
89 bool needs_lost_and_found
= false;
91 /* alphasort is locale-dependent; let's fix the locale to some sane value */
92 setlocale(LC_COLLATE
, "C");
95 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
98 /* The scandir function implemented in glibc has a bug that makes it
99 erroneously fail with ENOMEM under certain circumstances.
100 As a workaround we can retry the scandir call with the same arguments.
101 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
103 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
106 error_errno("scandir");
107 return EXT4_ALLOCATE_FAILED
;
112 if (dir_inode
== 0) {
113 /* root directory, check if lost+found already exists */
114 for (i
= 0; i
< entries
; i
++)
115 if (strcmp(namelist
[i
]->d_name
, "lost+found") == 0)
118 needs_lost_and_found
= true;
121 dentries
= calloc(entries
, sizeof(struct dentry
));
122 if (dentries
== NULL
)
123 critical_error_errno("malloc");
125 for (i
= 0; i
< entries
; i
++) {
126 dentries
[i
].filename
= strdup(namelist
[i
]->d_name
);
127 if (dentries
[i
].filename
== NULL
)
128 critical_error_errno("strdup");
130 asprintf(&dentries
[i
].path
, "%s%s", dir_path
, namelist
[i
]->d_name
);
131 asprintf(&dentries
[i
].full_path
, "%s%s", full_path
, namelist
[i
]->d_name
);
135 ret
= lstat(dentries
[i
].full_path
, &stat
);
137 error_errno("lstat");
143 dentries
[i
].size
= stat
.st_size
;
144 dentries
[i
].mode
= stat
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
145 if (fixed_time
== -1) {
146 dentries
[i
].mtime
= stat
.st_mtime
;
148 dentries
[i
].mtime
= fixed_time
;
150 uint64_t capabilities
;
151 if (fs_config_func
!= NULL
) {
152 unsigned int mode
= 0;
153 unsigned int uid
= 0;
154 unsigned int gid
= 0;
155 int dir
= S_ISDIR(stat
.st_mode
);
156 if (fs_config_func(dentries
[i
].path
, dir
, &uid
, &gid
, &mode
, &capabilities
)) {
157 dentries
[i
].mode
= mode
;
158 dentries
[i
].uid
= uid
;
159 dentries
[i
].gid
= gid
;
160 dentries
[i
].capabilities
= capabilities
;
164 if (S_ISREG(stat
.st_mode
)) {
165 dentries
[i
].file_type
= EXT4_FT_REG_FILE
;
166 } else if (S_ISDIR(stat
.st_mode
)) {
167 dentries
[i
].file_type
= EXT4_FT_DIR
;
169 } else if (S_ISCHR(stat
.st_mode
)) {
170 dentries
[i
].file_type
= EXT4_FT_CHRDEV
;
171 } else if (S_ISBLK(stat
.st_mode
)) {
172 dentries
[i
].file_type
= EXT4_FT_BLKDEV
;
173 } else if (S_ISFIFO(stat
.st_mode
)) {
174 dentries
[i
].file_type
= EXT4_FT_FIFO
;
175 } else if (S_ISSOCK(stat
.st_mode
)) {
176 dentries
[i
].file_type
= EXT4_FT_SOCK
;
177 } else if (S_ISLNK(stat
.st_mode
)) {
178 dentries
[i
].file_type
= EXT4_FT_SYMLINK
;
179 dentries
[i
].link
= calloc(info
.block_size
, 1);
180 readlink(dentries
[i
].full_path
, dentries
[i
].link
, info
.block_size
- 1);
182 error("unknown file type on %s", dentries
[i
].path
);
189 if (needs_lost_and_found
) {
190 /* insert a lost+found directory at the beginning of the dentries */
191 struct dentry
*tmp
= calloc(entries
+ 1, sizeof(struct dentry
));
192 memset(tmp
, 0, sizeof(struct dentry
));
193 memcpy(tmp
+ 1, dentries
, entries
* sizeof(struct dentry
));
196 dentries
[0].filename
= strdup("lost+found");
197 asprintf(&dentries
[0].path
, "%slost+found", dir_path
);
198 dentries
[0].full_path
= NULL
;
199 dentries
[0].size
= 0;
200 dentries
[0].mode
= S_IRWXU
;
201 dentries
[0].file_type
= EXT4_FT_DIR
;
208 inode
= make_directory(dir_inode
, entries
, dentries
, dirs
);
210 for (i
= 0; i
< entries
; i
++) {
211 if (dentries
[i
].file_type
== EXT4_FT_REG_FILE
) {
212 entry_inode
= make_file(dentries
[i
].full_path
, dentries
[i
].size
);
213 } else if (dentries
[i
].file_type
== EXT4_FT_DIR
) {
214 char *subdir_full_path
= NULL
;
215 char *subdir_dir_path
;
216 if (dentries
[i
].full_path
) {
217 ret
= asprintf(&subdir_full_path
, "%s/", dentries
[i
].full_path
);
219 critical_error_errno("asprintf");
221 ret
= asprintf(&subdir_dir_path
, "%s/", dentries
[i
].path
);
223 critical_error_errno("asprintf");
224 entry_inode
= build_directory_structure(subdir_full_path
,
225 subdir_dir_path
, inode
, fs_config_func
, verbose
, fixed_time
);
226 free(subdir_full_path
);
227 free(subdir_dir_path
);
228 } else if (dentries
[i
].file_type
== EXT4_FT_SYMLINK
) {
229 entry_inode
= make_link(dentries
[i
].link
);
230 } else if (dentries
[i
].file_type
== EXT4_FT_CHRDEV
||
231 dentries
[i
].file_type
== EXT4_FT_BLKDEV
||
232 dentries
[i
].file_type
== EXT4_FT_SOCK
||
233 dentries
[i
].file_type
== EXT4_FT_FIFO
) {
234 entry_inode
= make_special(dentries
[i
].full_path
);
236 error("unknown file type on %s", dentries
[i
].path
);
239 *dentries
[i
].inode
= entry_inode
;
241 ret
= inode_set_permissions(entry_inode
, dentries
[i
].mode
,
242 dentries
[i
].uid
, dentries
[i
].gid
,
245 error("failed to set permissions on %s\n", dentries
[i
].path
);
247 ret
= inode_set_capabilities(entry_inode
, dentries
[i
].capabilities
);
249 error("failed to set capability on %s\n", dentries
[i
].path
);
251 free(dentries
[i
].path
);
252 free(dentries
[i
].full_path
);
253 free(dentries
[i
].link
);
254 free((void *)dentries
[i
].filename
);
261 static u32
compute_block_size(void)
266 static u32
compute_journal_blocks(void)
268 u32 journal_blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
) / 64;
269 if (journal_blocks
< 1024)
270 journal_blocks
= 1024;
271 if (journal_blocks
> 32768)
272 journal_blocks
= 32768;
273 return journal_blocks
;
276 static u32
compute_blocks_per_group(void)
278 return info
.block_size
* 8;
281 static u32
compute_inodes(void)
283 return DIV_ROUND_UP(info
.len
, info
.block_size
) / 4;
286 static u32
compute_inodes_per_group(void)
288 u32 blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
);
289 u32 block_groups
= DIV_ROUND_UP(blocks
, info
.blocks_per_group
);
290 u32 inodes
= DIV_ROUND_UP(info
.inodes
, block_groups
);
291 inodes
= EXT4_ALIGN(inodes
, (info
.block_size
/ info
.inode_size
));
293 /* After properly rounding up the number of inodes/group,
294 * make sure to update the total inodes field in the info struct.
296 info
.inodes
= inodes
* block_groups
;
301 static u32
compute_bg_desc_reserve_blocks(void)
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 bg_desc_blocks
= DIV_ROUND_UP(block_groups
* sizeof(struct ext2_group_desc
),
308 u32 bg_desc_reserve_blocks
=
309 DIV_ROUND_UP(block_groups
* 1024 * sizeof(struct ext2_group_desc
),
310 info
.block_size
) - bg_desc_blocks
;
312 if (bg_desc_reserve_blocks
> info
.block_size
/ sizeof(u32
))
313 bg_desc_reserve_blocks
= info
.block_size
/ sizeof(u32
);
315 return bg_desc_reserve_blocks
;
318 /* return a newly-malloc'd string that is a copy of str. The new string
319 is guaranteed to have a trailing slash. If absolute is true, the new string
320 is also guaranteed to have a leading slash.
322 static char *canonicalize_slashes(const char *str
, bool absolute
)
325 int len
= strlen(str
);
336 if (str
[0] != '/' && absolute
) {
339 if (str
[len
- 1] != '/') {
342 ret
= malloc(newlen
+ 1);
344 critical_error("malloc");
348 if (str
[0] != '/' && absolute
) {
355 if (str
[len
- 1] != '/') {
359 if (ptr
!= ret
+ newlen
) {
360 critical_error("assertion failed\n");
368 static char *canonicalize_abs_slashes(const char *str
)
370 return canonicalize_slashes(str
, true);
373 static char *canonicalize_rel_slashes(const char *str
)
375 return canonicalize_slashes(str
, false);
378 int make_ext4fs_internal(int fd
, const char *_directory
,
379 fs_config_func_t fs_config_func
, int gzip
,
380 int sparse
, int crc
, int wipe
,
381 int verbose
, time_t fixed_time
,
382 FILE* block_list_file
)
386 char *directory
= NULL
;
388 if (setjmp(setjmp_env
))
389 return EXIT_FAILURE
; /* Handle a call to longjmp() */
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();
478 root_inode_num
= build_directory_structure(directory
, "", 0,
479 fs_config_func
, verbose
, fixed_time
);
481 root_inode_num
= build_default_directory_structure(fixed_time
);
483 root_mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
484 inode_set_permissions(root_inode_num
, root_mode
, 0, 0,
485 (fixed_time
!= 1) ? fixed_time
: 0);
491 if (block_list_file
) {
492 size_t dirlen
= strlen(directory
);
493 struct block_allocation
* p
= get_saved_allocation_chain();
495 if (strncmp(p
->filename
, directory
, dirlen
) == 0) {
496 fprintf(block_list_file
, "%s", p
->filename
+ dirlen
);
498 fprintf(block_list_file
, "%s", p
->filename
);
500 print_blocks(block_list_file
, p
);
501 struct block_allocation
* pn
= p
->next
;
507 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
508 aux_info
.sb
->s_inodes_count
- aux_info
.sb
->s_free_inodes_count
,
509 aux_info
.sb
->s_inodes_count
,
510 aux_info
.sb
->s_blocks_count_lo
- aux_info
.sb
->s_free_blocks_count_lo
,
511 aux_info
.sb
->s_blocks_count_lo
);
513 if (wipe
&& WIPE_IS_SUPPORTED
) {
514 wipe_block_device(fd
, info
.len
);
517 write_ext4_image(fd
, gzip
, sparse
, crc
);
519 sparse_file_destroy(ext4_sparse_file
);
520 ext4_sparse_file
= NULL
;