OSDN Git Service

am c3dc8929: Use updated stop supplicant API
[android-x86/system-extras.git] / ext4_utils / 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 <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
72 static int filter_dot(const struct dirent *d)
73 {
74         return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
75 }
76
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 */
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;
108         struct stat stat;
109         int ret;
110         int i;
111         u32 inode;
112         u32 entry_inode;
113         u32 dirs = 0;
114
115         entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
116         if (entries < 0) {
117                 error_errno("scandir");
118                 return EXT4_ALLOCATE_FAILED;
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                 dentries[i].mtime = stat.st_mtime;
146                 if (fs_config_func != NULL) {
147 #ifdef ANDROID
148                         unsigned int mode = 0;
149                         unsigned int uid = 0;
150                         unsigned int gid = 0;
151                         int dir = S_ISDIR(stat.st_mode);
152                         fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
153                         dentries[i].mode = mode;
154                         dentries[i].uid = uid;
155                         dentries[i].gid = gid;
156 #else
157                         error("can't set android permissions - built without android support");
158 #endif
159                 }
160 #ifndef USE_MINGW
161                 if (sehnd) {
162                         char *sepath = NULL;
163                         asprintf(&sepath, "/%s", dentries[i].path);
164                         if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
165                                 error("cannot lookup security context for %s", sepath);
166                         }
167 #if 0
168                         // TODO make this a debug flag
169                         if (dentries[i].secon)
170                                 printf("Labeling %s as %s\n", sepath, dentries[i].secon);
171 #endif
172                         free(sepath);
173                 }
174 #endif
175
176                 if (S_ISREG(stat.st_mode)) {
177                         dentries[i].file_type = EXT4_FT_REG_FILE;
178                 } else if (S_ISDIR(stat.st_mode)) {
179                         dentries[i].file_type = EXT4_FT_DIR;
180                         dirs++;
181                 } else if (S_ISCHR(stat.st_mode)) {
182                         dentries[i].file_type = EXT4_FT_CHRDEV;
183                 } else if (S_ISBLK(stat.st_mode)) {
184                         dentries[i].file_type = EXT4_FT_BLKDEV;
185                 } else if (S_ISFIFO(stat.st_mode)) {
186                         dentries[i].file_type = EXT4_FT_FIFO;
187                 } else if (S_ISSOCK(stat.st_mode)) {
188                         dentries[i].file_type = EXT4_FT_SOCK;
189                 } else if (S_ISLNK(stat.st_mode)) {
190                         dentries[i].file_type = EXT4_FT_SYMLINK;
191                         dentries[i].link = calloc(info.block_size, 1);
192                         readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
193                 } else {
194                         error("unknown file type on %s", dentries[i].path);
195                         i--;
196                         entries--;
197                 }
198         }
199         free(namelist);
200
201         inode = make_directory(dir_inode, entries, dentries, dirs);
202
203         for (i = 0; i < entries; i++) {
204                 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
205                         entry_inode = make_file(dentries[i].full_path, dentries[i].size);
206                 } else if (dentries[i].file_type == EXT4_FT_DIR) {
207                         entry_inode = build_directory_structure(dentries[i].full_path,
208                                         dentries[i].path, inode, fs_config_func, sehnd);
209                 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
210                         entry_inode = make_link(dentries[i].full_path, dentries[i].link);
211                 } else {
212                         error("unknown file type on %s", dentries[i].path);
213                         entry_inode = 0;
214                 }
215                 *dentries[i].inode = entry_inode;
216
217                 ret = inode_set_permissions(entry_inode, dentries[i].mode,
218                         dentries[i].uid, dentries[i].gid,
219                         dentries[i].mtime);
220                 if (ret)
221                         error("failed to set permissions on %s\n", dentries[i].path);
222                 ret = inode_set_selinux(entry_inode, dentries[i].secon);
223                 if (ret)
224                         error("failed to set SELinux context on %s\n", dentries[i].path);
225
226                 free(dentries[i].path);
227                 free(dentries[i].full_path);
228                 free(dentries[i].link);
229                 free((void *)dentries[i].filename);
230                 free(dentries[i].secon);
231         }
232
233         free(dentries);
234         return inode;
235 }
236 #endif
237
238 static u32 compute_block_size()
239 {
240         return 4096;
241 }
242
243 static u32 compute_journal_blocks()
244 {
245         u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
246         if (journal_blocks < 1024)
247                 journal_blocks = 1024;
248         if (journal_blocks > 32768)
249                 journal_blocks = 32768;
250         return journal_blocks;
251 }
252
253 static u32 compute_blocks_per_group()
254 {
255         return info.block_size * 8;
256 }
257
258 static u32 compute_inodes()
259 {
260         return DIV_ROUND_UP(info.len, info.block_size) / 4;
261 }
262
263 static u32 compute_inodes_per_group()
264 {
265         u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
266         u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
267         u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
268         inodes = ALIGN(inodes, (info.block_size / info.inode_size));
269
270         /* After properly rounding up the number of inodes/group,
271          * make sure to update the total inodes field in the info struct.
272          */
273         info.inodes = inodes * block_groups;
274
275         return inodes;
276 }
277
278 static u32 compute_bg_desc_reserve_blocks()
279 {
280         u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
281         u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
282         u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
283                         info.block_size);
284
285         u32 bg_desc_reserve_blocks =
286                         DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
287                                         info.block_size) - bg_desc_blocks;
288
289         if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
290                 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
291
292         return bg_desc_reserve_blocks;
293 }
294
295 void reset_ext4fs_info() {
296     // Reset all the global data structures used by make_ext4fs so it
297     // can be called again.
298     memset(&info, 0, sizeof(info));
299     memset(&aux_info, 0, sizeof(aux_info));
300
301     if (info.sparse_file) {
302         sparse_file_destroy(info.sparse_file);
303         info.sparse_file = NULL;
304     }
305 }
306
307 int make_ext4fs(const char *filename, s64 len,
308                 const char *mountpoint, struct selabel_handle *sehnd)
309 {
310         int fd;
311         int status;
312
313         reset_ext4fs_info();
314         info.len = len;
315
316         fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
317         if (fd < 0) {
318                 error_errno("open");
319                 return EXIT_FAILURE;
320         }
321
322         status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd);
323         close(fd);
324
325         return status;
326 }
327
328 int make_ext4fs_internal(int fd, const char *directory,
329                          const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
330                          int sparse, int crc, int wipe, int init_itabs,
331                          struct selabel_handle *sehnd)
332 {
333         u32 root_inode_num;
334         u16 root_mode;
335
336         if (setjmp(setjmp_env))
337                 return EXIT_FAILURE; /* Handle a call to longjmp() */
338
339         if (info.len <= 0)
340                 info.len = get_file_size(fd);
341
342         if (info.len <= 0) {
343                 fprintf(stderr, "Need size of filesystem\n");
344                 return EXIT_FAILURE;
345         }
346
347         if (info.block_size <= 0)
348                 info.block_size = compute_block_size();
349
350         /* Round down the filesystem length to be a multiple of the block size */
351         info.len &= ~((u64)info.block_size - 1);
352
353         if (info.journal_blocks == 0)
354                 info.journal_blocks = compute_journal_blocks();
355
356         if (info.no_journal == 0)
357                 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
358         else
359                 info.journal_blocks = 0;
360
361         if (info.blocks_per_group <= 0)
362                 info.blocks_per_group = compute_blocks_per_group();
363
364         if (info.inodes <= 0)
365                 info.inodes = compute_inodes();
366
367         if (info.inode_size <= 0)
368                 info.inode_size = 256;
369
370         if (info.label == NULL)
371                 info.label = "";
372
373         info.inodes_per_group = compute_inodes_per_group();
374
375         info.feat_compat |=
376                         EXT4_FEATURE_COMPAT_RESIZE_INODE;
377
378         info.feat_ro_compat |=
379                         EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
380                         EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
381
382         info.feat_incompat |=
383                         EXT4_FEATURE_INCOMPAT_EXTENTS |
384                         EXT4_FEATURE_INCOMPAT_FILETYPE;
385
386
387         info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
388
389         printf("Creating filesystem with parameters:\n");
390         printf("    Size: %llu\n", info.len);
391         printf("    Block size: %d\n", info.block_size);
392         printf("    Blocks per group: %d\n", info.blocks_per_group);
393         printf("    Inodes per group: %d\n", info.inodes_per_group);
394         printf("    Inode size: %d\n", info.inode_size);
395         printf("    Journal blocks: %d\n", info.journal_blocks);
396         printf("    Label: %s\n", info.label);
397
398         ext4_create_fs_aux_info();
399
400         printf("    Blocks: %llu\n", aux_info.len_blocks);
401         printf("    Block groups: %d\n", aux_info.groups);
402         printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
403
404         info.sparse_file = sparse_file_new(info.block_size, info.len);
405
406         block_allocator_init();
407
408         ext4_fill_in_sb();
409
410         if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
411                 error("failed to reserve first 10 inodes");
412
413         if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
414                 ext4_create_journal_inode();
415
416         if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
417                 ext4_create_resize_inode();
418
419 #ifdef USE_MINGW
420         // Windows needs only 'create an empty fs image' functionality
421         assert(!directory);
422         root_inode_num = build_default_directory_structure();
423 #else
424         if (directory)
425                 root_inode_num = build_directory_structure(directory, mountpoint, 0,
426                         fs_config_func, sehnd);
427         else
428                 root_inode_num = build_default_directory_structure();
429 #endif
430
431         root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
432         inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
433
434 #ifndef USE_MINGW
435         if (sehnd) {
436                 char *sepath = NULL;
437                 char *secontext = NULL;
438
439                 if (mountpoint[0] == '/')
440                         sepath = strdup(mountpoint);
441                 else
442                         asprintf(&sepath, "/%s", mountpoint);
443                 if (!sepath)
444                         critical_error_errno("malloc");
445                 if (selabel_lookup(sehnd, &secontext, sepath, S_IFDIR) < 0) {
446                         error("cannot lookup security context for %s", sepath);
447                 }
448                 if (secontext) {
449                         printf("Labeling %s as %s\n", sepath, secontext);
450                         inode_set_selinux(root_inode_num, secontext);
451                 }
452                 free(sepath);
453                 freecon(secontext);
454         }
455 #endif
456
457         ext4_update_free();
458
459         if (init_itabs)
460                 init_unused_inode_tables();
461
462         ext4_queue_sb();
463
464         printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
465                         aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
466                         aux_info.sb->s_inodes_count,
467                         aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
468                         aux_info.sb->s_blocks_count_lo);
469
470         if (wipe)
471                 wipe_block_device(fd, info.len);
472
473         write_ext4_image(fd, gzip, sparse, crc);
474
475         sparse_file_destroy(info.sparse_file);
476         info.sparse_file = NULL;
477
478         return 0;
479 }