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 /* Read a local directory and create the same tree in the generated filesystem.
49 Calls itself recursively with each directory in the given directory.
50 full_path is an absolute or relative path, with a trailing slash, to the
51 directory on disk that should be copied, or NULL if this is a directory
52 that does not exist on disk (e.g. lost+found).
53 dir_path is an absolute path, with trailing slash, to the same directory
54 if the image were mounted at the specified mount point */
55 static u32
build_directory_structure(const char *full_path
, const char *dir_path
,
56 u32 dir_inode
, fs_config_func_t fs_config_func
,
57 int verbose
, time_t fixed_time
)
60 struct dentry
*dentries
;
61 struct dirent
**namelist
= NULL
;
68 bool needs_lost_and_found
= false;
70 /* alphasort is locale-dependent; let's fix the locale to some sane value */
71 setlocale(LC_COLLATE
, "C");
74 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
77 /* The scandir function implemented in glibc has a bug that makes it
78 erroneously fail with ENOMEM under certain circumstances.
79 As a workaround we can retry the scandir call with the same arguments.
80 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
82 entries
= scandir(full_path
, &namelist
, filter_dot
, (void*)alphasort
);
85 error_errno("scandir");
86 return EXT4_ALLOCATE_FAILED
;
92 /* root directory, check if lost+found already exists */
93 for (i
= 0; i
< entries
; i
++)
94 if (strcmp(namelist
[i
]->d_name
, "lost+found") == 0)
97 needs_lost_and_found
= true;
100 dentries
= calloc(entries
, sizeof(struct dentry
));
101 if (dentries
== NULL
)
102 critical_error_errno("malloc");
104 for (i
= 0; i
< entries
; i
++) {
105 dentries
[i
].filename
= strdup(namelist
[i
]->d_name
);
106 if (dentries
[i
].filename
== NULL
)
107 critical_error_errno("strdup");
109 asprintf(&dentries
[i
].path
, "%s%s", dir_path
, namelist
[i
]->d_name
);
110 asprintf(&dentries
[i
].full_path
, "%s%s", full_path
, namelist
[i
]->d_name
);
114 ret
= lstat(dentries
[i
].full_path
, &stat
);
116 error_errno("lstat");
122 dentries
[i
].size
= stat
.st_size
;
123 dentries
[i
].mode
= stat
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXU
|S_IRWXG
|S_IRWXO
);
124 if (fixed_time
== -1) {
125 dentries
[i
].mtime
= stat
.st_mtime
;
127 dentries
[i
].mtime
= fixed_time
;
129 uint64_t capabilities
;
130 if (fs_config_func
!= NULL
) {
131 unsigned int mode
= 0;
132 unsigned int uid
= 0;
133 unsigned int gid
= 0;
134 int dir
= S_ISDIR(stat
.st_mode
);
135 if (fs_config_func(dentries
[i
].path
, dir
, &uid
, &gid
, &mode
, &capabilities
)) {
136 dentries
[i
].mode
= mode
;
137 dentries
[i
].uid
= uid
;
138 dentries
[i
].gid
= gid
;
139 dentries
[i
].capabilities
= capabilities
;
143 if (S_ISREG(stat
.st_mode
)) {
144 dentries
[i
].file_type
= EXT4_FT_REG_FILE
;
145 } else if (S_ISDIR(stat
.st_mode
)) {
146 dentries
[i
].file_type
= EXT4_FT_DIR
;
148 } else if (S_ISCHR(stat
.st_mode
)) {
149 dentries
[i
].file_type
= EXT4_FT_CHRDEV
;
150 } else if (S_ISBLK(stat
.st_mode
)) {
151 dentries
[i
].file_type
= EXT4_FT_BLKDEV
;
152 } else if (S_ISFIFO(stat
.st_mode
)) {
153 dentries
[i
].file_type
= EXT4_FT_FIFO
;
154 } else if (S_ISSOCK(stat
.st_mode
)) {
155 dentries
[i
].file_type
= EXT4_FT_SOCK
;
156 } else if (S_ISLNK(stat
.st_mode
)) {
157 dentries
[i
].file_type
= EXT4_FT_SYMLINK
;
158 dentries
[i
].link
= calloc(info
.block_size
, 1);
159 readlink(dentries
[i
].full_path
, dentries
[i
].link
, info
.block_size
- 1);
161 error("unknown file type on %s", dentries
[i
].path
);
168 if (needs_lost_and_found
) {
169 /* insert a lost+found directory at the beginning of the dentries */
170 struct dentry
*tmp
= calloc(entries
+ 1, sizeof(struct dentry
));
171 memset(tmp
, 0, sizeof(struct dentry
));
172 memcpy(tmp
+ 1, dentries
, entries
* sizeof(struct dentry
));
175 dentries
[0].filename
= strdup("lost+found");
176 asprintf(&dentries
[0].path
, "%slost+found", dir_path
);
177 dentries
[0].full_path
= NULL
;
178 dentries
[0].size
= 0;
179 dentries
[0].mode
= S_IRWXU
;
180 dentries
[0].file_type
= EXT4_FT_DIR
;
187 inode
= make_directory(dir_inode
, entries
, dentries
, dirs
);
189 for (i
= 0; i
< entries
; i
++) {
190 if (dentries
[i
].file_type
== EXT4_FT_REG_FILE
) {
191 entry_inode
= make_file(dentries
[i
].full_path
, dentries
[i
].size
);
192 } else if (dentries
[i
].file_type
== EXT4_FT_DIR
) {
193 char *subdir_full_path
= NULL
;
194 char *subdir_dir_path
;
195 if (dentries
[i
].full_path
) {
196 ret
= asprintf(&subdir_full_path
, "%s/", dentries
[i
].full_path
);
198 critical_error_errno("asprintf");
200 ret
= asprintf(&subdir_dir_path
, "%s/", dentries
[i
].path
);
202 critical_error_errno("asprintf");
203 entry_inode
= build_directory_structure(subdir_full_path
,
204 subdir_dir_path
, inode
, fs_config_func
, verbose
, fixed_time
);
205 free(subdir_full_path
);
206 free(subdir_dir_path
);
207 } else if (dentries
[i
].file_type
== EXT4_FT_SYMLINK
) {
208 entry_inode
= make_link(dentries
[i
].link
);
209 } else if (dentries
[i
].file_type
== EXT4_FT_CHRDEV
||
210 dentries
[i
].file_type
== EXT4_FT_BLKDEV
||
211 dentries
[i
].file_type
== EXT4_FT_SOCK
||
212 dentries
[i
].file_type
== EXT4_FT_FIFO
) {
213 entry_inode
= make_special(dentries
[i
].full_path
);
215 error("unknown file type on %s", dentries
[i
].path
);
218 *dentries
[i
].inode
= entry_inode
;
220 ret
= inode_set_permissions(entry_inode
, dentries
[i
].mode
,
221 dentries
[i
].uid
, dentries
[i
].gid
,
224 error("failed to set permissions on %s\n", dentries
[i
].path
);
226 ret
= inode_set_capabilities(entry_inode
, dentries
[i
].capabilities
);
228 error("failed to set capability on %s\n", dentries
[i
].path
);
230 free(dentries
[i
].path
);
231 free(dentries
[i
].full_path
);
232 free(dentries
[i
].link
);
233 free((void *)dentries
[i
].filename
);
240 static u32
compute_block_size()
245 static u32
compute_journal_blocks()
247 u32 journal_blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
) / 64;
248 if (journal_blocks
< 1024)
249 journal_blocks
= 1024;
250 if (journal_blocks
> 32768)
251 journal_blocks
= 32768;
252 return journal_blocks
;
255 static u32
compute_blocks_per_group()
257 return info
.block_size
* 8;
260 static u32
compute_inodes()
262 return DIV_ROUND_UP(info
.len
, info
.block_size
) / 4;
265 static u32
compute_inodes_per_group()
267 u32 blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
);
268 u32 block_groups
= DIV_ROUND_UP(blocks
, info
.blocks_per_group
);
269 u32 inodes
= DIV_ROUND_UP(info
.inodes
, block_groups
);
270 inodes
= EXT4_ALIGN(inodes
, (info
.block_size
/ info
.inode_size
));
272 /* After properly rounding up the number of inodes/group,
273 * make sure to update the total inodes field in the info struct.
275 info
.inodes
= inodes
* block_groups
;
280 static u32
compute_bg_desc_reserve_blocks()
282 u32 blocks
= DIV_ROUND_UP(info
.len
, info
.block_size
);
283 u32 block_groups
= DIV_ROUND_UP(blocks
, info
.blocks_per_group
);
284 u32 bg_desc_blocks
= DIV_ROUND_UP(block_groups
* sizeof(struct ext2_group_desc
),
287 u32 bg_desc_reserve_blocks
=
288 DIV_ROUND_UP(block_groups
* 1024 * sizeof(struct ext2_group_desc
),
289 info
.block_size
) - bg_desc_blocks
;
291 if (bg_desc_reserve_blocks
> info
.block_size
/ sizeof(u32
))
292 bg_desc_reserve_blocks
= info
.block_size
/ sizeof(u32
);
294 return bg_desc_reserve_blocks
;
297 /* return a newly-malloc'd string that is a copy of str. The new string
298 is guaranteed to have a trailing slash. If absolute is true, the new string
299 is also guaranteed to have a leading slash.
301 static char *canonicalize_slashes(const char *str
, bool absolute
)
304 int len
= strlen(str
);
315 if (str
[0] != '/' && absolute
) {
318 if (str
[len
- 1] != '/') {
321 ret
= malloc(newlen
+ 1);
323 critical_error("malloc");
327 if (str
[0] != '/' && absolute
) {
334 if (str
[len
- 1] != '/') {
338 if (ptr
!= ret
+ newlen
) {
339 critical_error("assertion failed\n");
347 static char *canonicalize_abs_slashes(const char *str
)
349 return canonicalize_slashes(str
, true);
352 static char *canonicalize_rel_slashes(const char *str
)
354 return canonicalize_slashes(str
, false);
357 int make_ext4fs_internal(int fd
, const char *_directory
,
358 fs_config_func_t fs_config_func
, int gzip
,
359 int sparse
, int crc
, int wipe
,
360 int verbose
, time_t fixed_time
,
361 FILE* block_list_file
)
365 char *directory
= NULL
;
367 if (setjmp(setjmp_env
))
368 return EXIT_FAILURE
; /* Handle a call to longjmp() */
370 if (_directory
== NULL
) {
371 fprintf(stderr
, "Need a source directory\n");
375 directory
= canonicalize_rel_slashes(_directory
);
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
|
415 EXT4_FEATURE_COMPAT_EXT_ATTR
;
417 info
.feat_ro_compat
|=
418 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
|
419 EXT4_FEATURE_RO_COMPAT_LARGE_FILE
|
420 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
;
422 info
.feat_incompat
|=
423 EXT4_FEATURE_INCOMPAT_EXTENTS
|
424 EXT4_FEATURE_INCOMPAT_FILETYPE
;
427 info
.bg_desc_reserve_blocks
= compute_bg_desc_reserve_blocks();
429 printf("Creating filesystem with parameters:\n");
430 printf(" Size: %"PRIu64
"\n", info
.len
);
431 printf(" Block size: %d\n", info
.block_size
);
432 printf(" Blocks per group: %d\n", info
.blocks_per_group
);
433 printf(" Inodes per group: %d\n", info
.inodes_per_group
);
434 printf(" Inode size: %d\n", info
.inode_size
);
435 printf(" Journal blocks: %d\n", info
.journal_blocks
);
436 printf(" Label: %s\n", info
.label
);
438 ext4_create_fs_aux_info();
440 printf(" Blocks: %"PRIu64
"\n", aux_info
.len_blocks
);
441 printf(" Block groups: %d\n", aux_info
.groups
);
442 printf(" Reserved blocks: %"PRIu64
"\n", (aux_info
.len_blocks
/ 100) * info
.reserve_pcnt
);
443 printf(" Reserved block group size: %d\n", info
.bg_desc_reserve_blocks
);
445 ext4_sparse_file
= sparse_file_new(info
.block_size
, info
.len
);
447 block_allocator_init();
451 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED
)
452 error("failed to reserve first 10 inodes");
454 if (info
.feat_compat
& EXT4_FEATURE_COMPAT_HAS_JOURNAL
)
455 ext4_create_journal_inode();
457 if (info
.feat_compat
& EXT4_FEATURE_COMPAT_RESIZE_INODE
)
458 ext4_create_resize_inode();
460 root_inode_num
= build_directory_structure(directory
, "", 0,
461 fs_config_func
, verbose
, fixed_time
);
463 root_mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
464 inode_set_permissions(root_inode_num
, root_mode
, 0, 0, 0);
470 if (block_list_file
) {
471 size_t dirlen
= strlen(directory
);
472 struct block_allocation
* p
= get_saved_allocation_chain();
474 if (strncmp(p
->filename
, directory
, dirlen
) == 0) {
475 fprintf(block_list_file
, "%s", p
->filename
+ dirlen
);
477 fprintf(block_list_file
, "%s", p
->filename
);
479 print_blocks(block_list_file
, p
);
480 struct block_allocation
* pn
= p
->next
;
486 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
487 aux_info
.sb
->s_inodes_count
- aux_info
.sb
->s_free_inodes_count
,
488 aux_info
.sb
->s_inodes_count
,
489 aux_info
.sb
->s_blocks_count_lo
- aux_info
.sb
->s_free_blocks_count_lo
,
490 aux_info
.sb
->s_blocks_count_lo
);
492 if (wipe
&& WIPE_IS_SUPPORTED
) {
493 wipe_block_device(fd
, info
.len
);
496 write_ext4_image(fd
, gzip
, sparse
, crc
);
498 sparse_file_destroy(ext4_sparse_file
);
499 ext4_sparse_file
= NULL
;