Remove dead selinux code
[project/make_ext4fs.git] / make_ext4fs.c
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 <inttypes.h>
30 #include <libgen.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 /* TODO: Not implemented:
39 Allocating blocks in the same block group as the file inode
40 Hash or binary tree directories
41 */
42
43 static int filter_dot(const struct dirent *d)
44 {
45 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
46 }
47
48 static u32 build_default_directory_structure(const char *dir_path)
49 {
50 u32 inode;
51 u32 root_inode;
52 struct dentry dentries = {
53 .filename = "lost+found",
54 .file_type = EXT4_FT_DIR,
55 .mode = S_IRWXU,
56 .uid = 0,
57 .gid = 0,
58 .mtime = 0,
59 };
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);
65
66 return root_inode;
67 }
68
69 #ifndef USE_MINGW
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.
72 full_path is an absolute or relative path, with a trailing slash, to the
73 directory on disk that should be copied, or NULL if this is a directory
74 that does not exist on disk (e.g. lost+found).
75 dir_path is an absolute path, with trailing slash, to the same directory
76 if the image were mounted at the specified mount point */
77 static u32 build_directory_structure(const char *full_path, const char *dir_path,
78 u32 dir_inode, fs_config_func_t fs_config_func,
79 int verbose, time_t fixed_time)
80 {
81 int entries = 0;
82 struct dentry *dentries;
83 struct dirent **namelist = NULL;
84 struct stat stat;
85 int ret;
86 int i;
87 u32 inode;
88 u32 entry_inode;
89 u32 dirs = 0;
90 bool needs_lost_and_found = false;
91
92 if (full_path) {
93 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
94 if (entries < 0) {
95 #ifdef __GLIBC__
96 /* The scandir function implemented in glibc has a bug that makes it
97 erroneously fail with ENOMEM under certain circumstances.
98 As a workaround we can retry the scandir call with the same arguments.
99 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
100 if (errno == ENOMEM)
101 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
102 #endif
103 if (entries < 0) {
104 error_errno("scandir");
105 return EXT4_ALLOCATE_FAILED;
106 }
107 }
108 }
109
110 if (dir_inode == 0) {
111 /* root directory, check if lost+found already exists */
112 for (i = 0; i < entries; i++)
113 if (strcmp(namelist[i]->d_name, "lost+found") == 0)
114 break;
115 if (i == entries)
116 needs_lost_and_found = true;
117 }
118
119 dentries = calloc(entries, sizeof(struct dentry));
120 if (dentries == NULL)
121 critical_error_errno("malloc");
122
123 for (i = 0; i < entries; i++) {
124 dentries[i].filename = strdup(namelist[i]->d_name);
125 if (dentries[i].filename == NULL)
126 critical_error_errno("strdup");
127
128 asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
129 asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
130
131 free(namelist[i]);
132
133 ret = lstat(dentries[i].full_path, &stat);
134 if (ret < 0) {
135 error_errno("lstat");
136 i--;
137 entries--;
138 continue;
139 }
140
141 dentries[i].size = stat.st_size;
142 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
143 if (fixed_time == -1) {
144 dentries[i].mtime = stat.st_mtime;
145 } else {
146 dentries[i].mtime = fixed_time;
147 }
148 uint64_t capabilities;
149 if (fs_config_func != NULL) {
150 unsigned int mode = 0;
151 unsigned int uid = 0;
152 unsigned int gid = 0;
153 int dir = S_ISDIR(stat.st_mode);
154 if (fs_config_func(dentries[i].path, dir, &uid, &gid, &mode, &capabilities)) {
155 dentries[i].mode = mode;
156 dentries[i].uid = uid;
157 dentries[i].gid = gid;
158 dentries[i].capabilities = capabilities;
159 }
160 }
161
162 if (S_ISREG(stat.st_mode)) {
163 dentries[i].file_type = EXT4_FT_REG_FILE;
164 } else if (S_ISDIR(stat.st_mode)) {
165 dentries[i].file_type = EXT4_FT_DIR;
166 dirs++;
167 } else if (S_ISCHR(stat.st_mode)) {
168 dentries[i].file_type = EXT4_FT_CHRDEV;
169 } else if (S_ISBLK(stat.st_mode)) {
170 dentries[i].file_type = EXT4_FT_BLKDEV;
171 } else if (S_ISFIFO(stat.st_mode)) {
172 dentries[i].file_type = EXT4_FT_FIFO;
173 } else if (S_ISSOCK(stat.st_mode)) {
174 dentries[i].file_type = EXT4_FT_SOCK;
175 } else if (S_ISLNK(stat.st_mode)) {
176 dentries[i].file_type = EXT4_FT_SYMLINK;
177 dentries[i].link = calloc(info.block_size, 1);
178 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
179 } else {
180 error("unknown file type on %s", dentries[i].path);
181 i--;
182 entries--;
183 }
184 }
185 free(namelist);
186
187 if (needs_lost_and_found) {
188 /* insert a lost+found directory at the beginning of the dentries */
189 struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
190 memset(tmp, 0, sizeof(struct dentry));
191 memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
192 dentries = tmp;
193
194 dentries[0].filename = strdup("lost+found");
195 asprintf(&dentries[0].path, "%slost+found", dir_path);
196 dentries[0].full_path = NULL;
197 dentries[0].size = 0;
198 dentries[0].mode = S_IRWXU;
199 dentries[0].file_type = EXT4_FT_DIR;
200 dentries[0].uid = 0;
201 dentries[0].gid = 0;
202 entries++;
203 dirs++;
204 }
205
206 inode = make_directory(dir_inode, entries, dentries, dirs);
207
208 for (i = 0; i < entries; i++) {
209 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
210 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
211 } else if (dentries[i].file_type == EXT4_FT_DIR) {
212 char *subdir_full_path = NULL;
213 char *subdir_dir_path;
214 if (dentries[i].full_path) {
215 ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
216 if (ret < 0)
217 critical_error_errno("asprintf");
218 }
219 ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
220 if (ret < 0)
221 critical_error_errno("asprintf");
222 entry_inode = build_directory_structure(subdir_full_path,
223 subdir_dir_path, inode, fs_config_func, verbose, fixed_time);
224 free(subdir_full_path);
225 free(subdir_dir_path);
226 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
227 entry_inode = make_link(dentries[i].link);
228 } else if (dentries[i].file_type == EXT4_FT_CHRDEV ||
229 dentries[i].file_type == EXT4_FT_BLKDEV ||
230 dentries[i].file_type == EXT4_FT_SOCK ||
231 dentries[i].file_type == EXT4_FT_FIFO) {
232 entry_inode = make_special(dentries[i].full_path);
233 } else {
234 error("unknown file type on %s", dentries[i].path);
235 entry_inode = 0;
236 }
237 *dentries[i].inode = entry_inode;
238
239 ret = inode_set_permissions(entry_inode, dentries[i].mode,
240 dentries[i].uid, dentries[i].gid,
241 dentries[i].mtime);
242 if (ret)
243 error("failed to set permissions on %s\n", dentries[i].path);
244
245 ret = inode_set_capabilities(entry_inode, dentries[i].capabilities);
246 if (ret)
247 error("failed to set capability on %s\n", dentries[i].path);
248
249 free(dentries[i].path);
250 free(dentries[i].full_path);
251 free(dentries[i].link);
252 free((void *)dentries[i].filename);
253 }
254
255 free(dentries);
256 return inode;
257 }
258 #endif
259
260 static u32 compute_block_size()
261 {
262 return 4096;
263 }
264
265 static u32 compute_journal_blocks()
266 {
267 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
268 if (journal_blocks < 1024)
269 journal_blocks = 1024;
270 if (journal_blocks > 32768)
271 journal_blocks = 32768;
272 return journal_blocks;
273 }
274
275 static u32 compute_blocks_per_group()
276 {
277 return info.block_size * 8;
278 }
279
280 static u32 compute_inodes()
281 {
282 return DIV_ROUND_UP(info.len, info.block_size) / 4;
283 }
284
285 static u32 compute_inodes_per_group()
286 {
287 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
288 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
289 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
290 inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size));
291
292 /* After properly rounding up the number of inodes/group,
293 * make sure to update the total inodes field in the info struct.
294 */
295 info.inodes = inodes * block_groups;
296
297 return inodes;
298 }
299
300 static u32 compute_bg_desc_reserve_blocks()
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 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
305 info.block_size);
306
307 u32 bg_desc_reserve_blocks =
308 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
309 info.block_size) - bg_desc_blocks;
310
311 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
312 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
313
314 return bg_desc_reserve_blocks;
315 }
316
317 void reset_ext4fs_info() {
318 // Reset all the global data structures used by make_ext4fs so it
319 // can be called again.
320 memset(&info, 0, sizeof(info));
321 memset(&aux_info, 0, sizeof(aux_info));
322
323 if (ext4_sparse_file) {
324 sparse_file_destroy(ext4_sparse_file);
325 ext4_sparse_file = NULL;
326 }
327 }
328
329 int make_ext4fs_sparse_fd(int fd, long long len,
330 const char *mountpoint)
331 {
332 reset_ext4fs_info();
333 info.len = len;
334
335 return make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 1, 0, 0, 0, -1, NULL);
336 }
337
338 int make_ext4fs(const char *filename, long long len,
339 const char *mountpoint)
340 {
341 int fd;
342 int status;
343
344 reset_ext4fs_info();
345 info.len = len;
346
347 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
348 if (fd < 0) {
349 error_errno("open");
350 return EXIT_FAILURE;
351 }
352
353 status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, -1, NULL);
354 close(fd);
355
356 return status;
357 }
358
359 /* return a newly-malloc'd string that is a copy of str. The new string
360 is guaranteed to have a trailing slash. If absolute is true, the new string
361 is also guaranteed to have a leading slash.
362 */
363 static char *canonicalize_slashes(const char *str, bool absolute)
364 {
365 char *ret;
366 int len = strlen(str);
367 int newlen = len;
368 char *ptr;
369
370 if (len == 0) {
371 if (absolute)
372 return strdup("/");
373 else
374 return strdup("");
375 }
376
377 if (str[0] != '/' && absolute) {
378 newlen++;
379 }
380 if (str[len - 1] != '/') {
381 newlen++;
382 }
383 ret = malloc(newlen + 1);
384 if (!ret) {
385 critical_error("malloc");
386 }
387
388 ptr = ret;
389 if (str[0] != '/' && absolute) {
390 *ptr++ = '/';
391 }
392
393 strcpy(ptr, str);
394 ptr += len;
395
396 if (str[len - 1] != '/') {
397 *ptr++ = '/';
398 }
399
400 if (ptr != ret + newlen) {
401 critical_error("assertion failed\n");
402 }
403
404 *ptr = '\0';
405
406 return ret;
407 }
408
409 static char *canonicalize_abs_slashes(const char *str)
410 {
411 return canonicalize_slashes(str, true);
412 }
413
414 static char *canonicalize_rel_slashes(const char *str)
415 {
416 return canonicalize_slashes(str, false);
417 }
418
419 int make_ext4fs_internal(int fd, const char *_directory,
420 const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
421 int sparse, int crc, int wipe,
422 int verbose, time_t fixed_time,
423 FILE* block_list_file)
424 {
425 u32 root_inode_num;
426 u16 root_mode;
427 char *mountpoint;
428 char *directory = NULL;
429
430 if (setjmp(setjmp_env))
431 return EXIT_FAILURE; /* Handle a call to longjmp() */
432
433 if (_mountpoint == NULL) {
434 mountpoint = strdup("");
435 } else {
436 mountpoint = canonicalize_abs_slashes(_mountpoint);
437 }
438
439 if (_directory) {
440 directory = canonicalize_rel_slashes(_directory);
441 }
442
443 if (info.len <= 0)
444 info.len = get_file_size(fd);
445
446 if (info.len <= 0) {
447 fprintf(stderr, "Need size of filesystem\n");
448 return EXIT_FAILURE;
449 }
450
451 if (info.block_size <= 0)
452 info.block_size = compute_block_size();
453
454 /* Round down the filesystem length to be a multiple of the block size */
455 info.len &= ~((u64)info.block_size - 1);
456
457 if (info.journal_blocks == 0)
458 info.journal_blocks = compute_journal_blocks();
459
460 if (info.no_journal == 0)
461 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
462 else
463 info.journal_blocks = 0;
464
465 if (info.blocks_per_group <= 0)
466 info.blocks_per_group = compute_blocks_per_group();
467
468 if (info.inodes <= 0)
469 info.inodes = compute_inodes();
470
471 if (info.inode_size <= 0)
472 info.inode_size = 256;
473
474 if (info.label == NULL)
475 info.label = "";
476
477 info.inodes_per_group = compute_inodes_per_group();
478
479 info.feat_compat |=
480 EXT4_FEATURE_COMPAT_RESIZE_INODE |
481 EXT4_FEATURE_COMPAT_EXT_ATTR;
482
483 info.feat_ro_compat |=
484 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
485 EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
486 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
487
488 info.feat_incompat |=
489 EXT4_FEATURE_INCOMPAT_EXTENTS |
490 EXT4_FEATURE_INCOMPAT_FILETYPE;
491
492
493 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
494
495 printf("Creating filesystem with parameters:\n");
496 printf(" Size: %"PRIu64"\n", info.len);
497 printf(" Block size: %d\n", info.block_size);
498 printf(" Blocks per group: %d\n", info.blocks_per_group);
499 printf(" Inodes per group: %d\n", info.inodes_per_group);
500 printf(" Inode size: %d\n", info.inode_size);
501 printf(" Journal blocks: %d\n", info.journal_blocks);
502 printf(" Label: %s\n", info.label);
503
504 ext4_create_fs_aux_info();
505
506 printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks);
507 printf(" Block groups: %d\n", aux_info.groups);
508 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
509
510 ext4_sparse_file = sparse_file_new(info.block_size, info.len);
511
512 block_allocator_init();
513
514 ext4_fill_in_sb();
515
516 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
517 error("failed to reserve first 10 inodes");
518
519 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
520 ext4_create_journal_inode();
521
522 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
523 ext4_create_resize_inode();
524
525 #ifdef USE_MINGW
526 // Windows needs only 'create an empty fs image' functionality
527 assert(!directory);
528 root_inode_num = build_default_directory_structure(mountpoint);
529 #else
530 if (directory)
531 root_inode_num = build_directory_structure(directory, mountpoint, 0,
532 fs_config_func, verbose, fixed_time);
533 else
534 root_inode_num = build_default_directory_structure(mountpoint);
535 #endif
536
537 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
538 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
539
540 ext4_update_free();
541
542 ext4_queue_sb();
543
544 if (block_list_file) {
545 size_t dirlen = directory ? strlen(directory) : 0;
546 struct block_allocation* p = get_saved_allocation_chain();
547 while (p) {
548 if (directory && strncmp(p->filename, directory, dirlen) == 0) {
549 // substitute mountpoint for the leading directory in the filename, in the output file
550 fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
551 } else {
552 fprintf(block_list_file, "%s", p->filename);
553 }
554 print_blocks(block_list_file, p);
555 struct block_allocation* pn = p->next;
556 free_alloc(p);
557 p = pn;
558 }
559 }
560
561 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
562 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
563 aux_info.sb->s_inodes_count,
564 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
565 aux_info.sb->s_blocks_count_lo);
566
567 if (wipe && WIPE_IS_SUPPORTED) {
568 wipe_block_device(fd, info.len);
569 }
570
571 write_ext4_image(fd, gzip, sparse, crc);
572
573 sparse_file_destroy(ext4_sparse_file);
574 ext4_sparse_file = NULL;
575
576 free(mountpoint);
577 free(directory);
578
579 return 0;
580 }