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