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