• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ext4_utils.h"
19 #include "allocate.h"
20 #include "contents.h"
21 #include "uuid.h"
22 #include "wipe.h"
23 
24 #include <sparse/sparse.h>
25 
26 #include <assert.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <libgen.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 
37 #ifdef USE_MINGW
38 
39 #include <winsock2.h>
40 
41 /* These match the Linux definitions of these flags.
42    L_xx is defined to avoid conflicting with the win32 versions.
43 */
44 #define L_S_IRUSR 00400
45 #define L_S_IWUSR 00200
46 #define L_S_IXUSR 00100
47 #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
48 #define S_IRGRP 00040
49 #define S_IWGRP 00020
50 #define S_IXGRP 00010
51 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
52 #define S_IROTH 00004
53 #define S_IWOTH 00002
54 #define S_IXOTH 00001
55 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
56 #define S_ISUID 0004000
57 #define S_ISGID 0002000
58 #define S_ISVTX 0001000
59 
60 #else
61 
62 #define O_BINARY 0
63 
64 #endif
65 
66 /* TODO: Not implemented:
67    Allocating blocks in the same block group as the file inode
68    Hash or binary tree directories
69    Special files: sockets, devices, fifos
70  */
71 
filter_dot(const struct dirent * d)72 static int filter_dot(const struct dirent *d)
73 {
74 	return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
75 }
76 
build_default_directory_structure()77 static u32 build_default_directory_structure()
78 {
79 	u32 inode;
80 	u32 root_inode;
81 	struct dentry dentries = {
82 			.filename = "lost+found",
83 			.file_type = EXT4_FT_DIR,
84 			.mode = S_IRWXU,
85 			.uid = 0,
86 			.gid = 0,
87 			.mtime = 0,
88 	};
89 	root_inode = make_directory(0, 1, &dentries, 1);
90 	inode = make_directory(root_inode, 0, NULL, 0);
91 	*dentries.inode = inode;
92 	inode_set_permissions(inode, dentries.mode,
93 		dentries.uid, dentries.gid, dentries.mtime);
94 
95 	return root_inode;
96 }
97 
98 #ifndef USE_MINGW
99 /* Read a local directory and create the same tree in the generated filesystem.
100    Calls itself recursively with each directory in the given directory */
build_directory_structure(const char * full_path,const char * dir_path,u32 dir_inode,fs_config_func_t fs_config_func,struct selabel_handle * sehnd)101 static u32 build_directory_structure(const char *full_path, const char *dir_path,
102 		u32 dir_inode, fs_config_func_t fs_config_func,
103 		struct selabel_handle *sehnd)
104 {
105 	int entries = 0;
106 	struct dentry *dentries;
107 	struct dirent **namelist = NULL;
108 	struct stat stat;
109 	int ret;
110 	int i;
111 	u32 inode;
112 	u32 entry_inode;
113 	u32 dirs = 0;
114 	bool needs_lost_and_found = false;
115 
116 	if (full_path) {
117 		entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
118 		if (entries < 0) {
119 			error_errno("scandir");
120 			return EXT4_ALLOCATE_FAILED;
121 		}
122 	}
123 
124 	if (dir_inode == 0) {
125 		/* root directory, check if lost+found already exists */
126 		for (i = 0; i < entries; i++)
127 			if (strcmp(namelist[i]->d_name, "lost+found") == 0)
128 				break;
129 		if (i == entries)
130 			needs_lost_and_found = true;
131 	}
132 
133 	dentries = calloc(entries, sizeof(struct dentry));
134 	if (dentries == NULL)
135 		critical_error_errno("malloc");
136 
137 	for (i = 0; i < entries; i++) {
138 		dentries[i].filename = strdup(namelist[i]->d_name);
139 		if (dentries[i].filename == NULL)
140 			critical_error_errno("strdup");
141 
142 		asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
143 		asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
144 
145 		free(namelist[i]);
146 
147 		ret = lstat(dentries[i].full_path, &stat);
148 		if (ret < 0) {
149 			error_errno("lstat");
150 			i--;
151 			entries--;
152 			continue;
153 		}
154 
155 		dentries[i].size = stat.st_size;
156 		dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
157 		dentries[i].mtime = stat.st_mtime;
158 		if (fs_config_func != NULL) {
159 #ifdef ANDROID
160 			unsigned int mode = 0;
161 			unsigned int uid = 0;
162 			unsigned int gid = 0;
163 			int dir = S_ISDIR(stat.st_mode);
164 			fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
165 			dentries[i].mode = mode;
166 			dentries[i].uid = uid;
167 			dentries[i].gid = gid;
168 #else
169 			error("can't set android permissions - built without android support");
170 #endif
171 		}
172 #ifdef HAVE_SELINUX
173 		if (sehnd) {
174 			char *sepath = NULL;
175 			asprintf(&sepath, "/%s", dentries[i].path);
176 			if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
177 				error("cannot lookup security context for %s", sepath);
178 			}
179 			if (dentries[i].secon)
180 				printf("Labeling %s as %s\n", sepath, dentries[i].secon);
181 			free(sepath);
182 		}
183 #endif
184 
185 		if (S_ISREG(stat.st_mode)) {
186 			dentries[i].file_type = EXT4_FT_REG_FILE;
187 		} else if (S_ISDIR(stat.st_mode)) {
188 			dentries[i].file_type = EXT4_FT_DIR;
189 			dirs++;
190 		} else if (S_ISCHR(stat.st_mode)) {
191 			dentries[i].file_type = EXT4_FT_CHRDEV;
192 		} else if (S_ISBLK(stat.st_mode)) {
193 			dentries[i].file_type = EXT4_FT_BLKDEV;
194 		} else if (S_ISFIFO(stat.st_mode)) {
195 			dentries[i].file_type = EXT4_FT_FIFO;
196 		} else if (S_ISSOCK(stat.st_mode)) {
197 			dentries[i].file_type = EXT4_FT_SOCK;
198 		} else if (S_ISLNK(stat.st_mode)) {
199 			dentries[i].file_type = EXT4_FT_SYMLINK;
200 			dentries[i].link = calloc(info.block_size, 1);
201 			readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
202 		} else {
203 			error("unknown file type on %s", dentries[i].path);
204 			i--;
205 			entries--;
206 		}
207 	}
208 	free(namelist);
209 
210 	if (needs_lost_and_found) {
211 		/* insert a lost+found directory at the beginning of the dentries */
212 		struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
213 		memset(tmp, 0, sizeof(struct dentry));
214 		memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
215 		dentries = tmp;
216 
217 		dentries[0].filename = strdup("lost+found");
218 		asprintf(&dentries[0].path, "%s/lost+found", dir_path);
219 		dentries[0].full_path = NULL;
220 		dentries[0].size = 0;
221 		dentries[0].mode = S_IRWXU;
222 		dentries[0].file_type = EXT4_FT_DIR;
223 		dentries[0].uid = 0;
224 		dentries[0].gid = 0;
225 #ifdef HAVE_SELINUX
226 		if (sehnd) {
227 			char *sepath = NULL;
228 			asprintf(&sepath, "/%s", dentries[0].path);
229 			if (selabel_lookup(sehnd, &dentries[0].secon, sepath, dentries[0].mode) < 0)
230 				error("cannot lookup security context for %s", dentries[0].path);
231 			free(sepath);
232 		}
233 #endif
234 		entries++;
235 		dirs++;
236 	}
237 
238 	inode = make_directory(dir_inode, entries, dentries, dirs);
239 
240 	for (i = 0; i < entries; i++) {
241 		if (dentries[i].file_type == EXT4_FT_REG_FILE) {
242 			entry_inode = make_file(dentries[i].full_path, dentries[i].size);
243 		} else if (dentries[i].file_type == EXT4_FT_DIR) {
244 			entry_inode = build_directory_structure(dentries[i].full_path,
245 					dentries[i].path, inode, fs_config_func, sehnd);
246 		} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
247 			entry_inode = make_link(dentries[i].full_path, dentries[i].link);
248 		} else {
249 			error("unknown file type on %s", dentries[i].path);
250 			entry_inode = 0;
251 		}
252 		*dentries[i].inode = entry_inode;
253 
254 		ret = inode_set_permissions(entry_inode, dentries[i].mode,
255 			dentries[i].uid, dentries[i].gid,
256 			dentries[i].mtime);
257 		if (ret)
258 			error("failed to set permissions on %s\n", dentries[i].path);
259 		ret = inode_set_selinux(entry_inode, dentries[i].secon);
260 		if (ret)
261 			error("failed to set SELinux context on %s\n", dentries[i].path);
262 
263 		free(dentries[i].path);
264 		free(dentries[i].full_path);
265 		free(dentries[i].link);
266 		free((void *)dentries[i].filename);
267 		free(dentries[i].secon);
268 	}
269 
270 	free(dentries);
271 	return inode;
272 }
273 #endif
274 
compute_block_size()275 static u32 compute_block_size()
276 {
277 	return 4096;
278 }
279 
compute_journal_blocks()280 static u32 compute_journal_blocks()
281 {
282 	u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
283 	if (journal_blocks < 1024)
284 		journal_blocks = 1024;
285 	if (journal_blocks > 32768)
286 		journal_blocks = 32768;
287 	return journal_blocks;
288 }
289 
compute_blocks_per_group()290 static u32 compute_blocks_per_group()
291 {
292 	return info.block_size * 8;
293 }
294 
compute_inodes()295 static u32 compute_inodes()
296 {
297 	return DIV_ROUND_UP(info.len, info.block_size) / 4;
298 }
299 
compute_inodes_per_group()300 static u32 compute_inodes_per_group()
301 {
302 	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
303 	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
304 	u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
305 	inodes = ALIGN(inodes, (info.block_size / info.inode_size));
306 
307 	/* After properly rounding up the number of inodes/group,
308 	 * make sure to update the total inodes field in the info struct.
309 	 */
310 	info.inodes = inodes * block_groups;
311 
312 	return inodes;
313 }
314 
compute_bg_desc_reserve_blocks()315 static u32 compute_bg_desc_reserve_blocks()
316 {
317 	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
318 	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
319 	u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
320 			info.block_size);
321 
322 	u32 bg_desc_reserve_blocks =
323 			DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
324 					info.block_size) - bg_desc_blocks;
325 
326 	if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
327 		bg_desc_reserve_blocks = info.block_size / sizeof(u32);
328 
329 	return bg_desc_reserve_blocks;
330 }
331 
reset_ext4fs_info()332 void reset_ext4fs_info() {
333     // Reset all the global data structures used by make_ext4fs so it
334     // can be called again.
335     memset(&info, 0, sizeof(info));
336     memset(&aux_info, 0, sizeof(aux_info));
337 
338     if (info.sparse_file) {
339         sparse_file_destroy(info.sparse_file);
340         info.sparse_file = NULL;
341     }
342 }
343 
make_ext4fs(const char * filename,s64 len,const char * mountpoint,struct selabel_handle * sehnd)344 int make_ext4fs(const char *filename, s64 len,
345                 const char *mountpoint, struct selabel_handle *sehnd)
346 {
347 	int fd;
348 	int status;
349 
350 	reset_ext4fs_info();
351 	info.len = len;
352 
353 	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
354 	if (fd < 0) {
355 		error_errno("open");
356 		return EXIT_FAILURE;
357 	}
358 
359 	status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd);
360 	close(fd);
361 
362 	return status;
363 }
364 
make_ext4fs_internal(int fd,const char * directory,char * mountpoint,fs_config_func_t fs_config_func,int gzip,int sparse,int crc,int wipe,int init_itabs,struct selabel_handle * sehnd)365 int make_ext4fs_internal(int fd, const char *directory,
366                          char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
367                          int crc, int wipe, int init_itabs, struct selabel_handle *sehnd)
368 {
369 	u32 root_inode_num;
370 	u16 root_mode;
371 
372 	if (setjmp(setjmp_env))
373 		return EXIT_FAILURE; /* Handle a call to longjmp() */
374 
375 	if (info.len <= 0)
376 		info.len = get_file_size(fd);
377 
378 	if (info.len <= 0) {
379 		fprintf(stderr, "Need size of filesystem\n");
380 		return EXIT_FAILURE;
381 	}
382 
383 	if (info.block_size <= 0)
384 		info.block_size = compute_block_size();
385 
386 	/* Round down the filesystem length to be a multiple of the block size */
387 	info.len &= ~((u64)info.block_size - 1);
388 
389 	if (info.journal_blocks == 0)
390 		info.journal_blocks = compute_journal_blocks();
391 
392 	if (info.no_journal == 0)
393 		info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
394 	else
395 		info.journal_blocks = 0;
396 
397 	if (info.blocks_per_group <= 0)
398 		info.blocks_per_group = compute_blocks_per_group();
399 
400 	if (info.inodes <= 0)
401 		info.inodes = compute_inodes();
402 
403 	if (info.inode_size <= 0)
404 		info.inode_size = 256;
405 
406 	if (info.label == NULL)
407 		info.label = "";
408 
409 	info.inodes_per_group = compute_inodes_per_group();
410 
411 	info.feat_compat |=
412 			EXT4_FEATURE_COMPAT_RESIZE_INODE;
413 
414 	info.feat_ro_compat |=
415 			EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
416 			EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
417 
418 	info.feat_incompat |=
419 			EXT4_FEATURE_INCOMPAT_EXTENTS |
420 			EXT4_FEATURE_INCOMPAT_FILETYPE;
421 
422 
423 	info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
424 
425 	printf("Creating filesystem with parameters:\n");
426 	printf("    Size: %llu\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);
433 
434 	ext4_create_fs_aux_info();
435 
436 	printf("    Blocks: %llu\n", aux_info.len_blocks);
437 	printf("    Block groups: %d\n", aux_info.groups);
438 	printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
439 
440 	info.sparse_file = sparse_file_new(info.block_size, info.len);
441 
442 	block_allocator_init();
443 
444 	ext4_fill_in_sb();
445 
446 	if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
447 		error("failed to reserve first 10 inodes");
448 
449 	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
450 		ext4_create_journal_inode();
451 
452 	if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
453 		ext4_create_resize_inode();
454 
455 #ifdef USE_MINGW
456 	// Windows needs only 'create an empty fs image' functionality
457 	assert(!directory);
458 	root_inode_num = build_default_directory_structure();
459 #else
460 	if (directory)
461 		root_inode_num = build_directory_structure(directory, mountpoint, 0,
462                         fs_config_func, sehnd);
463 	else
464 		root_inode_num = build_default_directory_structure();
465 #endif
466 
467 	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
468 	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
469 
470 #ifdef HAVE_SELINUX
471 	if (sehnd) {
472 		char *sepath = NULL;
473 		char *secontext = NULL;
474 
475 		if (mountpoint[0] == '/')
476 			sepath = strdup(mountpoint);
477 		else
478 			asprintf(&sepath, "/%s", mountpoint);
479 		if (!sepath)
480 			critical_error_errno("malloc");
481 		if (selabel_lookup(sehnd, &secontext, sepath, S_IFDIR) < 0) {
482 			error("cannot lookup security context for %s", sepath);
483 		}
484 		if (secontext) {
485 			printf("Labeling %s as %s\n", sepath, secontext);
486 			inode_set_selinux(root_inode_num, secontext);
487 		}
488 		free(sepath);
489 		freecon(secontext);
490 	}
491 #endif
492 
493 	ext4_update_free();
494 
495 	if (init_itabs)
496 		init_unused_inode_tables();
497 
498 	ext4_queue_sb();
499 
500 	printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
501 			aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
502 			aux_info.sb->s_inodes_count,
503 			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
504 			aux_info.sb->s_blocks_count_lo);
505 
506 	if (wipe)
507 		wipe_block_device(fd, info.len);
508 
509 	write_ext4_image(fd, gzip, sparse, crc);
510 
511 	sparse_file_destroy(info.sparse_file);
512 	info.sparse_file = NULL;
513 
514 	return 0;
515 }
516