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