make_ext4fs.c revision 93eb1dc9e68d2e9dea94f56d8bce478c1a52b354
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "make_ext4fs.h"
18#include "output_file.h"
19#include "ext4_utils.h"
20#include "allocate.h"
21#include "contents.h"
22#include "uuid.h"
23#include "backed_block.h"
24
25#include <dirent.h>
26#include <fcntl.h>
27#include <libgen.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34
35#ifdef ANDROID
36#include <private/android_filesystem_config.h>
37#endif
38
39/* TODO: Not implemented:
40   Allocating blocks in the same block group as the file inode
41   Hash or binary tree directories
42   Special files: sockets, devices, fifos
43 */
44
45static int filter_dot(const struct dirent *d)
46{
47	return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
48}
49
50static u32 build_default_directory_structure()
51{
52	u32 inode;
53	u32 root_inode;
54	struct dentry dentries = {
55			.filename = "lost+found",
56			.file_type = EXT4_FT_DIR,
57			.mode = S_IRWXU,
58			.uid = 0,
59			.gid = 0,
60			.mtime = 0,
61	};
62	root_inode = make_directory(0, 1, &dentries, 1);
63	inode = make_directory(root_inode, 0, NULL, 0);
64	*dentries.inode = inode;
65	inode_set_permissions(inode, dentries.mode,
66		dentries.uid, dentries.gid, dentries.mtime);
67
68	return root_inode;
69}
70
71/* Read a local directory and create the same tree in the generated filesystem.
72   Calls itself recursively with each directory in the given directory */
73static u32 build_directory_structure(const char *full_path, const char *dir_path,
74		u32 dir_inode, int android)
75{
76	int entries = 0;
77	struct dentry *dentries;
78	struct dirent **namelist;
79	struct stat stat;
80	int ret;
81	int i;
82	u32 inode;
83	u32 entry_inode;
84	u32 dirs = 0;
85
86	entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
87	if (entries < 0) {
88		error_errno("scandir");
89		return EXT4_ALLOCATE_FAILED;
90	}
91
92	dentries = calloc(entries, sizeof(struct dentry));
93	if (dentries == NULL)
94		critical_error_errno("malloc");
95
96	for (i = 0; i < entries; i++) {
97		dentries[i].filename = strdup(namelist[i]->d_name);
98		if (dentries[i].filename == NULL)
99			critical_error_errno("strdup");
100
101		asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
102		asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
103
104		free(namelist[i]);
105
106		ret = lstat(dentries[i].full_path, &stat);
107		if (ret < 0) {
108			error_errno("lstat");
109			i--;
110			entries--;
111			continue;
112		}
113
114		dentries[i].size = stat.st_size;
115		dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
116		dentries[i].mtime = stat.st_mtime;
117		if (android) {
118#ifdef ANDROID
119			unsigned int mode = 0;
120			unsigned int uid = 0;
121			unsigned int gid = 0;
122			int dir = S_ISDIR(stat.st_mode);
123			fs_config(dentries[i].path, dir, &uid, &gid, &mode);
124			dentries[i].mode = mode;
125			dentries[i].uid = uid;
126			dentries[i].gid = gid;
127#else
128			error("can't set android permissions - built without android support");
129#endif
130		}
131
132		if (S_ISREG(stat.st_mode)) {
133			dentries[i].file_type = EXT4_FT_REG_FILE;
134		} else if (S_ISDIR(stat.st_mode)) {
135			dentries[i].file_type = EXT4_FT_DIR;
136			dirs++;
137		} else if (S_ISCHR(stat.st_mode)) {
138			dentries[i].file_type = EXT4_FT_CHRDEV;
139		} else if (S_ISBLK(stat.st_mode)) {
140			dentries[i].file_type = EXT4_FT_BLKDEV;
141		} else if (S_ISFIFO(stat.st_mode)) {
142			dentries[i].file_type = EXT4_FT_FIFO;
143		} else if (S_ISSOCK(stat.st_mode)) {
144			dentries[i].file_type = EXT4_FT_SOCK;
145		} else if (S_ISLNK(stat.st_mode)) {
146			dentries[i].file_type = EXT4_FT_SYMLINK;
147			dentries[i].link = calloc(info.block_size, 1);
148			readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
149		} else {
150			error("unknown file type on %s", dentries[i].path);
151			i--;
152			entries--;
153		}
154	}
155	free(namelist);
156
157	inode = make_directory(dir_inode, entries, dentries, dirs);
158
159	for (i = 0; i < entries; i++) {
160		if (dentries[i].file_type == EXT4_FT_REG_FILE) {
161			entry_inode = make_file(dentries[i].full_path, dentries[i].size);
162		} else if (dentries[i].file_type == EXT4_FT_DIR) {
163			entry_inode = build_directory_structure(dentries[i].full_path,
164					dentries[i].path, inode, android);
165		} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
166			entry_inode = make_link(dentries[i].full_path, dentries[i].link);
167		} else {
168			error("unknown file type on %s", dentries[i].path);
169			entry_inode = 0;
170		}
171		*dentries[i].inode = entry_inode;
172
173		ret = inode_set_permissions(entry_inode, dentries[i].mode,
174			dentries[i].uid, dentries[i].gid,
175			dentries[i].mtime);
176		if (ret)
177			error("failed to set permissions on %s\n", dentries[i].path);
178
179		free(dentries[i].path);
180		free(dentries[i].full_path);
181		free(dentries[i].link);
182		free((void *)dentries[i].filename);
183	}
184
185	free(dentries);
186	return inode;
187}
188
189static u32 compute_block_size()
190{
191	return 4096;
192}
193
194static u32 compute_journal_blocks()
195{
196	u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
197	if (journal_blocks < 1024)
198		journal_blocks = 1024;
199	if (journal_blocks > 32768)
200		journal_blocks = 32768;
201	return journal_blocks;
202}
203
204static u32 compute_blocks_per_group()
205{
206	return info.block_size * 8;
207}
208
209static u32 compute_inodes()
210{
211	return DIV_ROUND_UP(info.len, info.block_size) / 4;
212}
213
214static u32 compute_inodes_per_group()
215{
216	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
217	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
218	u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
219	inodes = ALIGN(inodes, (info.block_size / info.inode_size));
220
221	/* After properly rounding up the number of inodes/group,
222	 * make sure to update the total inodes field in the info struct.
223	 */
224	info.inodes = inodes * block_groups;
225
226	return inodes;
227}
228
229static u32 compute_bg_desc_reserve_blocks()
230{
231	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
232	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
233	u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
234			info.block_size);
235
236	u32 bg_desc_reserve_blocks =
237			DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
238					info.block_size) - bg_desc_blocks;
239
240	if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
241		bg_desc_reserve_blocks = info.block_size / sizeof(u32);
242
243	return bg_desc_reserve_blocks;
244}
245
246void reset_ext4fs_info() {
247    // Reset all the global data structures used by make_ext4fs so it
248    // can be called again.
249    memset(&info, 0, sizeof(info));
250    memset(&aux_info, 0, sizeof(aux_info));
251    free_data_blocks();
252}
253
254int make_ext4fs(const char *filename, s64 len)
255{
256	int fd;
257	int status;
258
259	reset_ext4fs_info();
260	info.len = len;
261
262	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
263	if (fd < 0) {
264		error_errno("open");
265		return EXIT_FAILURE;
266	}
267
268	status = make_ext4fs_internal(fd, NULL, NULL, 0, 0, 0, 0, 1, 0);
269	close(fd);
270
271	return status;
272}
273
274int make_ext4fs_internal(int fd, const char *directory,
275                         char *mountpoint, int android, int gzip, int sparse,
276                         int crc, int wipe, int init_itabs)
277{
278	u32 root_inode_num;
279	u16 root_mode;
280
281	if (setjmp(setjmp_env))
282		return EXIT_FAILURE; /* Handle a call to longjmp() */
283
284	if (info.len <= 0)
285		info.len = get_file_size(fd);
286
287	if (info.len <= 0) {
288		fprintf(stderr, "Need size of filesystem\n");
289		return EXIT_FAILURE;
290	}
291
292	if (info.block_size <= 0)
293		info.block_size = compute_block_size();
294
295	/* Round down the filesystem length to be a multiple of the block size */
296	info.len &= ~((u64)info.block_size - 1);
297
298	if (info.journal_blocks == 0)
299		info.journal_blocks = compute_journal_blocks();
300
301	if (info.no_journal == 0)
302		info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
303	else
304		info.journal_blocks = 0;
305
306	if (info.blocks_per_group <= 0)
307		info.blocks_per_group = compute_blocks_per_group();
308
309	if (info.inodes <= 0)
310		info.inodes = compute_inodes();
311
312	if (info.inode_size <= 0)
313		info.inode_size = 256;
314
315	if (info.label == NULL)
316		info.label = "";
317
318	info.inodes_per_group = compute_inodes_per_group();
319
320	info.feat_compat |=
321			EXT4_FEATURE_COMPAT_RESIZE_INODE;
322
323	info.feat_ro_compat |=
324			EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
325			EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
326
327	info.feat_incompat |=
328			EXT4_FEATURE_INCOMPAT_EXTENTS |
329			EXT4_FEATURE_INCOMPAT_FILETYPE;
330
331
332	info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
333
334	printf("Creating filesystem with parameters:\n");
335	printf("    Size: %llu\n", info.len);
336	printf("    Block size: %d\n", info.block_size);
337	printf("    Blocks per group: %d\n", info.blocks_per_group);
338	printf("    Inodes per group: %d\n", info.inodes_per_group);
339	printf("    Inode size: %d\n", info.inode_size);
340	printf("    Journal blocks: %d\n", info.journal_blocks);
341	printf("    Label: %s\n", info.label);
342
343	ext4_create_fs_aux_info();
344
345	printf("    Blocks: %llu\n", aux_info.len_blocks);
346	printf("    Block groups: %d\n", aux_info.groups);
347	printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
348
349	block_allocator_init();
350
351	ext4_fill_in_sb();
352
353	if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
354		error("failed to reserve first 10 inodes");
355
356	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
357		ext4_create_journal_inode();
358
359	if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
360		ext4_create_resize_inode();
361
362	if (directory)
363		root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
364	else
365		root_inode_num = build_default_directory_structure();
366
367	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
368	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
369
370	ext4_update_free();
371
372	if (init_itabs)
373		init_unused_inode_tables();
374
375	ext4_queue_sb();
376
377	printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
378			aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
379			aux_info.sb->s_inodes_count,
380			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
381			aux_info.sb->s_blocks_count_lo);
382
383	write_ext4_image(fd, gzip, sparse, crc, wipe);
384
385	return 0;
386}
387