blob: 2160d5632a975e5e0e90d1b5e2a573e518daa8c4 [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 Crossec0a2e82010-06-11 14:21:37 -070023
Colin Crossec0a2e82010-06-11 14:21:37 -070024#include <dirent.h>
25#include <libgen.h>
Colin Cross881cca22010-06-20 23:57:06 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070032
Colin Crossec0a2e82010-06-11 14:21:37 -070033#ifdef ANDROID
34#include <private/android_filesystem_config.h>
35#endif
36
37/* TODO: Not implemented:
38 Allocating blocks in the same block group as the file inode
39 Hash or binary tree directories
Colin Cross7a8bee72010-06-20 14:53:14 -070040 Special files: sockets, devices, fifos
Colin Crossec0a2e82010-06-11 14:21:37 -070041 */
42
Colin Crossec0a2e82010-06-11 14:21:37 -070043static int filter_dot(const struct dirent *d)
44{
45 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
46}
47
48static u32 build_default_directory_structure()
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,
Colin Crossde61f982010-08-04 15:06:09 -070057 .gid = 0,
58 .mtime = 0,
Colin Crossec0a2e82010-06-11 14:21:37 -070059 };
60 root_inode = make_directory(0, 1, &dentries, 1);
61 inode = make_directory(root_inode, 0, NULL, 0);
62 *dentries.inode = inode;
Ken Sumrall75249ed2010-08-13 16:04:49 -070063 inode_set_permissions(inode, dentries.mode,
64 dentries.uid, dentries.gid, dentries.mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -070065
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 */
71static u32 build_directory_structure(const char *full_path, const char *dir_path,
72 u32 dir_inode, int android)
73{
74 int entries = 0;
75 struct dentry *dentries;
76 struct dirent **namelist;
77 struct stat stat;
78 int ret;
79 int i;
80 u32 inode;
81 u32 entry_inode;
82 u32 dirs = 0;
83
Colin Cross8aef66d2010-06-20 23:22:12 -070084 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
Colin Crossec0a2e82010-06-11 14:21:37 -070085 if (entries < 0) {
86 error_errno("scandir");
87 return EXT4_ALLOCATE_FAILED;
88 }
89
90 dentries = calloc(entries, sizeof(struct dentry));
91 if (dentries == NULL)
92 critical_error_errno("malloc");
93
94 for (i = 0; i < entries; i++) {
95 dentries[i].filename = strdup(namelist[i]->d_name);
96 if (dentries[i].filename == NULL)
97 critical_error_errno("strdup");
98
99 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
100 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
101
102 free(namelist[i]);
103
104 ret = lstat(dentries[i].full_path, &stat);
105 if (ret < 0) {
106 error_errno("lstat");
107 i--;
108 entries--;
109 continue;
110 }
111
112 dentries[i].size = stat.st_size;
113 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 -0700114 dentries[i].mtime = stat.st_mtime;
Colin Crossec0a2e82010-06-11 14:21:37 -0700115 if (android) {
116#ifdef ANDROID
117 unsigned int mode = 0;
118 unsigned int uid = 0;
119 unsigned int gid = 0;
120 int dir = S_ISDIR(stat.st_mode);
121 fs_config(dentries[i].path, dir, &uid, &gid, &mode);
122 dentries[i].mode = mode;
123 dentries[i].uid = uid;
124 dentries[i].gid = gid;
125#else
126 error("can't set android permissions - built without android support");
127#endif
128 }
129
130 if (S_ISREG(stat.st_mode)) {
131 dentries[i].file_type = EXT4_FT_REG_FILE;
132 } else if (S_ISDIR(stat.st_mode)) {
133 dentries[i].file_type = EXT4_FT_DIR;
134 dirs++;
135 } else if (S_ISCHR(stat.st_mode)) {
136 dentries[i].file_type = EXT4_FT_CHRDEV;
137 } else if (S_ISBLK(stat.st_mode)) {
138 dentries[i].file_type = EXT4_FT_BLKDEV;
139 } else if (S_ISFIFO(stat.st_mode)) {
140 dentries[i].file_type = EXT4_FT_FIFO;
141 } else if (S_ISSOCK(stat.st_mode)) {
142 dentries[i].file_type = EXT4_FT_SOCK;
143 } else if (S_ISLNK(stat.st_mode)) {
144 dentries[i].file_type = EXT4_FT_SYMLINK;
145 dentries[i].link = calloc(info.block_size, 1);
146 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
147 } else {
148 error("unknown file type on %s", dentries[i].path);
149 i--;
150 entries--;
151 }
152 }
153 free(namelist);
154
155 inode = make_directory(dir_inode, entries, dentries, dirs);
156
157 for (i = 0; i < entries; i++) {
158 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
159 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
160 } else if (dentries[i].file_type == EXT4_FT_DIR) {
161 entry_inode = build_directory_structure(dentries[i].full_path,
162 dentries[i].path, inode, android);
163 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
164 entry_inode = make_link(dentries[i].full_path, dentries[i].link);
165 } else {
166 error("unknown file type on %s", dentries[i].path);
167 entry_inode = 0;
168 }
169 *dentries[i].inode = entry_inode;
170
171 ret = inode_set_permissions(entry_inode, dentries[i].mode,
Colin Crossde61f982010-08-04 15:06:09 -0700172 dentries[i].uid, dentries[i].gid,
173 dentries[i].mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700174 if (ret)
175 error("failed to set permissions on %s\n", dentries[i].path);
176
177 free(dentries[i].path);
178 free(dentries[i].full_path);
179 free(dentries[i].link);
180 free((void *)dentries[i].filename);
181 }
182
183 free(dentries);
184 return inode;
185}
186
187static u32 compute_block_size()
188{
189 return 4096;
190}
191
Colin Crosse4b5ae82010-08-03 14:10:07 -0700192static u32 compute_journal_blocks()
193{
194 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
195 if (journal_blocks < 1024)
196 journal_blocks = 1024;
197 if (journal_blocks > 32768)
198 journal_blocks = 32768;
199 return journal_blocks;
200}
201
Colin Crossec0a2e82010-06-11 14:21:37 -0700202static u32 compute_blocks_per_group()
203{
204 return info.block_size * 8;
205}
206
207static u32 compute_inodes()
208{
209 return DIV_ROUND_UP(info.len, info.block_size) / 4;
210}
211
212static u32 compute_inodes_per_group()
213{
214 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
215 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
216 return DIV_ROUND_UP(info.inodes, block_groups);
217}
218
Colin Cross22742ce2010-12-22 16:01:52 -0800219static u32 compute_bg_desc_reserve_blocks()
220{
221 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
222 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
223 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
224 info.block_size);
225
226 u32 bg_desc_reserve_blocks =
227 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
228 info.block_size) - bg_desc_blocks;
229
230 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
231 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
232
233 return bg_desc_reserve_blocks;
234}
235
Doug Zongker263eefd2010-06-29 17:23:14 -0700236void reset_ext4fs_info() {
237 // Reset all the global data structures used by make_ext4fs so it
238 // can be called again.
239 memset(&info, 0, sizeof(info));
240 memset(&aux_info, 0, sizeof(aux_info));
241 free_data_blocks();
Colin Crossec0a2e82010-06-11 14:21:37 -0700242}
243
Doug Zongker263eefd2010-06-29 17:23:14 -0700244int make_ext4fs(const char *filename, const char *directory,
Colin Cross757ace52010-12-29 13:57:01 -0800245 char *mountpoint, int android, int gzip, int sparse,
246 int crc)
Colin Crossec0a2e82010-06-11 14:21:37 -0700247{
Doug Zongker263eefd2010-06-29 17:23:14 -0700248 u32 root_inode_num;
249 u16 root_mode;
Colin Crossec0a2e82010-06-11 14:21:37 -0700250
251 if (info.len == 0)
252 info.len = get_file_size(filename);
253
254 if (info.len <= 0) {
255 fprintf(stderr, "Need size of filesystem\n");
Doug Zongker263eefd2010-06-29 17:23:14 -0700256 return EXIT_FAILURE;
Colin Crossec0a2e82010-06-11 14:21:37 -0700257 }
258
Colin Crossec0a2e82010-06-11 14:21:37 -0700259 if (info.block_size <= 0)
260 info.block_size = compute_block_size();
261
Colin Crosse4b5ae82010-08-03 14:10:07 -0700262 if (info.journal_blocks == 0)
263 info.journal_blocks = compute_journal_blocks();
264
265 if (info.no_journal == 0)
266 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
267 else
268 info.journal_blocks = 0;
269
Colin Crossec0a2e82010-06-11 14:21:37 -0700270 if (info.blocks_per_group <= 0)
271 info.blocks_per_group = compute_blocks_per_group();
272
273 if (info.inodes <= 0)
274 info.inodes = compute_inodes();
275
276 if (info.inode_size <= 0)
277 info.inode_size = 256;
278
279 if (info.label == NULL)
280 info.label = "";
281
282 info.inodes_per_group = compute_inodes_per_group();
283
284 info.feat_compat |=
285 EXT4_FEATURE_COMPAT_RESIZE_INODE;
286
287 info.feat_ro_compat |=
288 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
289 EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
290
291 info.feat_incompat |=
292 EXT4_FEATURE_INCOMPAT_EXTENTS |
293 EXT4_FEATURE_INCOMPAT_FILETYPE;
294
295
Colin Cross22742ce2010-12-22 16:01:52 -0800296 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
297
Colin Crossec0a2e82010-06-11 14:21:37 -0700298 printf("Creating filesystem with parameters:\n");
299 printf(" Size: %llu\n", info.len);
300 printf(" Block size: %d\n", info.block_size);
301 printf(" Blocks per group: %d\n", info.blocks_per_group);
302 printf(" Inodes per group: %d\n", info.inodes_per_group);
303 printf(" Inode size: %d\n", info.inode_size);
Colin Crosse4b5ae82010-08-03 14:10:07 -0700304 printf(" Journal blocks: %d\n", info.journal_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700305 printf(" Label: %s\n", info.label);
306
307 ext4_create_fs_aux_info();
308
309 printf(" Blocks: %llu\n", aux_info.len_blocks);
310 printf(" Block groups: %d\n", aux_info.groups);
Colin Cross22742ce2010-12-22 16:01:52 -0800311 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700312
313 block_allocator_init();
314
315 ext4_fill_in_sb();
316
317 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
318 error("failed to reserve first 10 inodes");
319
320 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
321 ext4_create_journal_inode();
322
323 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
324 ext4_create_resize_inode();
325
326 if (directory)
327 root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
328 else
329 root_inode_num = build_default_directory_structure();
Doug Zongker263eefd2010-06-29 17:23:14 -0700330
Colin Crossec0a2e82010-06-11 14:21:37 -0700331 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
Colin Crossde61f982010-08-04 15:06:09 -0700332 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
Colin Crossec0a2e82010-06-11 14:21:37 -0700333
334 ext4_update_free();
335
Colin Crossb7813302010-12-22 18:41:13 -0800336 ext4_queue_sb();
337
Colin Crossec0a2e82010-06-11 14:21:37 -0700338 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
339 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
340 aux_info.sb->s_inodes_count,
341 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
342 aux_info.sb->s_blocks_count_lo);
343
Colin Cross757ace52010-12-29 13:57:01 -0800344 write_ext4_image(filename, gzip, sparse, crc);
Colin Crossec0a2e82010-06-11 14:21:37 -0700345
346 return 0;
347}