blob: 298a02af5bde70af44cd8cd5cdf33f8b1a4b18cd [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -07001/*
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
Colin Cross33f96c62010-12-22 18:40:14 -080017#include "make_ext4fs.h"
18#include "output_file.h"
19#include "ext4_utils.h"
20#include "allocate.h"
21#include "contents.h"
22#include "uuid.h"
Colin Cross2e905e52010-12-29 14:20:53 -080023#include "backed_block.h"
Colin Crossec0a2e82010-06-11 14:21:37 -070024
Colin Crossec0a2e82010-06-11 14:21:37 -070025#include <dirent.h>
26#include <libgen.h>
Colin Cross881cca22010-06-20 23:57:06 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <sys/stat.h>
32#include <sys/types.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070033
Colin Crossec0a2e82010-06-11 14:21:37 -070034#ifdef ANDROID
35#include <private/android_filesystem_config.h>
36#endif
37
38/* TODO: Not implemented:
39 Allocating blocks in the same block group as the file inode
40 Hash or binary tree directories
Colin Cross7a8bee72010-06-20 14:53:14 -070041 Special files: sockets, devices, fifos
Colin Crossec0a2e82010-06-11 14:21:37 -070042 */
43
Colin Crossec0a2e82010-06-11 14:21:37 -070044static int filter_dot(const struct dirent *d)
45{
46 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
47}
48
49static u32 build_default_directory_structure()
50{
51 u32 inode;
52 u32 root_inode;
53 struct dentry dentries = {
54 .filename = "lost+found",
55 .file_type = EXT4_FT_DIR,
56 .mode = S_IRWXU,
57 .uid = 0,
Colin Crossde61f982010-08-04 15:06:09 -070058 .gid = 0,
59 .mtime = 0,
Colin Crossec0a2e82010-06-11 14:21:37 -070060 };
61 root_inode = make_directory(0, 1, &dentries, 1);
62 inode = make_directory(root_inode, 0, NULL, 0);
63 *dentries.inode = inode;
Ken Sumrall75249ed2010-08-13 16:04:49 -070064 inode_set_permissions(inode, dentries.mode,
65 dentries.uid, dentries.gid, dentries.mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -070066
67 return root_inode;
68}
69
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 */
72static u32 build_directory_structure(const char *full_path, const char *dir_path,
73 u32 dir_inode, int android)
74{
75 int entries = 0;
76 struct dentry *dentries;
77 struct dirent **namelist;
78 struct stat stat;
79 int ret;
80 int i;
81 u32 inode;
82 u32 entry_inode;
83 u32 dirs = 0;
84
Colin Cross8aef66d2010-06-20 23:22:12 -070085 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
Colin Crossec0a2e82010-06-11 14:21:37 -070086 if (entries < 0) {
87 error_errno("scandir");
88 return EXT4_ALLOCATE_FAILED;
89 }
90
91 dentries = calloc(entries, sizeof(struct dentry));
92 if (dentries == NULL)
93 critical_error_errno("malloc");
94
95 for (i = 0; i < entries; i++) {
96 dentries[i].filename = strdup(namelist[i]->d_name);
97 if (dentries[i].filename == NULL)
98 critical_error_errno("strdup");
99
100 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
101 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
102
103 free(namelist[i]);
104
105 ret = lstat(dentries[i].full_path, &stat);
106 if (ret < 0) {
107 error_errno("lstat");
108 i--;
109 entries--;
110 continue;
111 }
112
113 dentries[i].size = stat.st_size;
114 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
Colin Crossde61f982010-08-04 15:06:09 -0700115 dentries[i].mtime = stat.st_mtime;
Colin Crossec0a2e82010-06-11 14:21:37 -0700116 if (android) {
117#ifdef ANDROID
118 unsigned int mode = 0;
119 unsigned int uid = 0;
120 unsigned int gid = 0;
121 int dir = S_ISDIR(stat.st_mode);
122 fs_config(dentries[i].path, dir, &uid, &gid, &mode);
123 dentries[i].mode = mode;
124 dentries[i].uid = uid;
125 dentries[i].gid = gid;
126#else
127 error("can't set android permissions - built without android support");
128#endif
129 }
130
131 if (S_ISREG(stat.st_mode)) {
132 dentries[i].file_type = EXT4_FT_REG_FILE;
133 } else if (S_ISDIR(stat.st_mode)) {
134 dentries[i].file_type = EXT4_FT_DIR;
135 dirs++;
136 } else if (S_ISCHR(stat.st_mode)) {
137 dentries[i].file_type = EXT4_FT_CHRDEV;
138 } else if (S_ISBLK(stat.st_mode)) {
139 dentries[i].file_type = EXT4_FT_BLKDEV;
140 } else if (S_ISFIFO(stat.st_mode)) {
141 dentries[i].file_type = EXT4_FT_FIFO;
142 } else if (S_ISSOCK(stat.st_mode)) {
143 dentries[i].file_type = EXT4_FT_SOCK;
144 } else if (S_ISLNK(stat.st_mode)) {
145 dentries[i].file_type = EXT4_FT_SYMLINK;
146 dentries[i].link = calloc(info.block_size, 1);
147 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
148 } else {
149 error("unknown file type on %s", dentries[i].path);
150 i--;
151 entries--;
152 }
153 }
154 free(namelist);
155
156 inode = make_directory(dir_inode, entries, dentries, dirs);
157
158 for (i = 0; i < entries; i++) {
159 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
160 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
161 } else if (dentries[i].file_type == EXT4_FT_DIR) {
162 entry_inode = build_directory_structure(dentries[i].full_path,
163 dentries[i].path, inode, android);
164 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
165 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
166 } else {
167 error("unknown file type on %s", dentries[i].path);
168 entry_inode = 0;
169 }
170 *dentries[i].inode = entry_inode;
171
172 ret = inode_set_permissions(entry_inode, dentries[i].mode,
Colin Crossde61f982010-08-04 15:06:09 -0700173 dentries[i].uid, dentries[i].gid,
174 dentries[i].mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700175 if (ret)
176 error("failed to set permissions on %s\n", dentries[i].path);
177
178 free(dentries[i].path);
179 free(dentries[i].full_path);
180 free(dentries[i].link);
181 free((void *)dentries[i].filename);
182 }
183
184 free(dentries);
185 return inode;
186}
187
188static u32 compute_block_size()
189{
190 return 4096;
191}
192
Colin Crosse4b5ae82010-08-03 14:10:07 -0700193static u32 compute_journal_blocks()
194{
195 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
196 if (journal_blocks < 1024)
197 journal_blocks = 1024;
198 if (journal_blocks > 32768)
199 journal_blocks = 32768;
200 return journal_blocks;
201}
202
Colin Crossec0a2e82010-06-11 14:21:37 -0700203static u32 compute_blocks_per_group()
204{
205 return info.block_size * 8;
206}
207
208static u32 compute_inodes()
209{
210 return DIV_ROUND_UP(info.len, info.block_size) / 4;
211}
212
213static u32 compute_inodes_per_group()
214{
215 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
216 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
Colin Cross96cc54a2011-04-29 16:45:48 -0700217 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
Ken Sumrall107a9f12011-06-29 20:28:30 -0700218 inodes = ALIGN(inodes, (info.block_size / info.inode_size));
219
220 /* After properly rounding up the number of inodes/group,
221 * make sure to update the total inodes field in the info struct.
222 */
223 info.inodes = inodes * block_groups;
224
225 return inodes;
Colin Crossec0a2e82010-06-11 14:21:37 -0700226}
227
Colin Cross22742ce2010-12-22 16:01:52 -0800228static u32 compute_bg_desc_reserve_blocks()
229{
230 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
231 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
232 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
233 info.block_size);
234
235 u32 bg_desc_reserve_blocks =
236 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
237 info.block_size) - bg_desc_blocks;
238
239 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
240 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
241
242 return bg_desc_reserve_blocks;
243}
244
Doug Zongker263eefd2010-06-29 17:23:14 -0700245void reset_ext4fs_info() {
246 // Reset all the global data structures used by make_ext4fs so it
247 // can be called again.
248 memset(&info, 0, sizeof(info));
249 memset(&aux_info, 0, sizeof(aux_info));
250 free_data_blocks();
Colin Crossec0a2e82010-06-11 14:21:37 -0700251}
252
Ken Sumrall983fb192011-01-19 17:15:42 -0800253int make_ext4fs(const char *filename, s64 len)
254{
255 reset_ext4fs_info();
256 info.len = len;
Ken Sumrall107a9f12011-06-29 20:28:30 -0700257 return make_ext4fs_internal(filename, NULL, NULL, 0, 0, 0, 0, 1, 0);
Ken Sumrall983fb192011-01-19 17:15:42 -0800258}
259
260int make_ext4fs_internal(const char *filename, const char *directory,
261 char *mountpoint, int android, int gzip, int sparse,
Ken Sumrall107a9f12011-06-29 20:28:30 -0700262 int crc, int wipe, int init_itabs)
Colin Crossec0a2e82010-06-11 14:21:37 -0700263{
Doug Zongker263eefd2010-06-29 17:23:14 -0700264 u32 root_inode_num;
265 u16 root_mode;
Colin Crossec0a2e82010-06-11 14:21:37 -0700266
Ken Sumrall2ae76632011-03-23 22:08:53 -0700267 if (setjmp(setjmp_env))
268 return EXIT_FAILURE; /* Handle a call to longjmp() */
269
Ken Sumrall435a8b62011-01-14 18:33:06 -0800270 if (info.len <= 0)
Colin Crossec0a2e82010-06-11 14:21:37 -0700271 info.len = get_file_size(filename);
272
273 if (info.len <= 0) {
274 fprintf(stderr, "Need size of filesystem\n");
Doug Zongker263eefd2010-06-29 17:23:14 -0700275 return EXIT_FAILURE;
Colin Crossec0a2e82010-06-11 14:21:37 -0700276 }
277
Colin Crossec0a2e82010-06-11 14:21:37 -0700278 if (info.block_size <= 0)
279 info.block_size = compute_block_size();
280
Colin Crosse4b5ae82010-08-03 14:10:07 -0700281 if (info.journal_blocks == 0)
282 info.journal_blocks = compute_journal_blocks();
283
284 if (info.no_journal == 0)
285 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
286 else
287 info.journal_blocks = 0;
288
Colin Crossec0a2e82010-06-11 14:21:37 -0700289 if (info.blocks_per_group <= 0)
290 info.blocks_per_group = compute_blocks_per_group();
291
292 if (info.inodes <= 0)
293 info.inodes = compute_inodes();
294
295 if (info.inode_size <= 0)
296 info.inode_size = 256;
297
298 if (info.label == NULL)
299 info.label = "";
300
301 info.inodes_per_group = compute_inodes_per_group();
302
303 info.feat_compat |=
304 EXT4_FEATURE_COMPAT_RESIZE_INODE;
305
306 info.feat_ro_compat |=
307 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
308 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
309
310 info.feat_incompat |=
311 EXT4_FEATURE_INCOMPAT_EXTENTS |
312 EXT4_FEATURE_INCOMPAT_FILETYPE;
313
314
Colin Cross22742ce2010-12-22 16:01:52 -0800315 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
316
Colin Crossec0a2e82010-06-11 14:21:37 -0700317 printf("Creating filesystem with parameters:\n");
318 printf(" Size: %llu\n", info.len);
319 printf(" Block size: %d\n", info.block_size);
320 printf(" Blocks per group: %d\n", info.blocks_per_group);
321 printf(" Inodes per group: %d\n", info.inodes_per_group);
322 printf(" Inode size: %d\n", info.inode_size);
Colin Crosse4b5ae82010-08-03 14:10:07 -0700323 printf(" Journal blocks: %d\n", info.journal_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700324 printf(" Label: %s\n", info.label);
325
326 ext4_create_fs_aux_info();
327
328 printf(" Blocks: %llu\n", aux_info.len_blocks);
329 printf(" Block groups: %d\n", aux_info.groups);
Colin Cross22742ce2010-12-22 16:01:52 -0800330 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700331
332 block_allocator_init();
333
334 ext4_fill_in_sb();
335
336 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
337 error("failed to reserve first 10 inodes");
338
339 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
340 ext4_create_journal_inode();
341
342 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
343 ext4_create_resize_inode();
344
345 if (directory)
346 root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
347 else
348 root_inode_num = build_default_directory_structure();
Doug Zongker263eefd2010-06-29 17:23:14 -0700349
Colin Crossec0a2e82010-06-11 14:21:37 -0700350 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
Colin Crossde61f982010-08-04 15:06:09 -0700351 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
Colin Crossec0a2e82010-06-11 14:21:37 -0700352
353 ext4_update_free();
354
Ken Sumrall107a9f12011-06-29 20:28:30 -0700355 if (init_itabs)
356 init_unused_inode_tables();
357
Colin Crossb7813302010-12-22 18:41:13 -0800358 ext4_queue_sb();
359
Colin Crossec0a2e82010-06-11 14:21:37 -0700360 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
361 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
362 aux_info.sb->s_inodes_count,
363 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
364 aux_info.sb->s_blocks_count_lo);
365
Colin Crossc2470652011-01-26 16:39:46 -0800366 write_ext4_image(filename, gzip, sparse, crc, wipe);
Colin Crossec0a2e82010-06-11 14:21:37 -0700367
368 return 0;
369}