make_ext4fs.c revision e4b5ae8ab07e698b95f004c9226000b02f853abc
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#define _GNU_SOURCE 18 19#include <dirent.h> 20#include <libgen.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include <string.h> 24#include <unistd.h> 25#include <sys/stat.h> 26#include <sys/types.h> 27 28#include "make_ext4fs.h" 29#include "output_file.h" 30#include "ext4_utils.h" 31#include "allocate.h" 32#include "contents.h" 33#include "uuid.h" 34 35#ifdef ANDROID 36#include <private/android_filesystem_config.h> 37#endif 38 39/* TODO: Not implemented: 40 Allocating blocks in the same block group as the file inode 41 Hash or binary tree directories 42 Special files: sockets, devices, fifos 43 */ 44 45static int filter_dot(const struct dirent *d) 46{ 47 return (strcmp(d->d_name, "..") && strcmp(d->d_name, ".")); 48} 49 50static u32 build_default_directory_structure() 51{ 52 u32 inode; 53 u32 root_inode; 54 struct dentry dentries = { 55 .filename = "lost+found", 56 .file_type = EXT4_FT_DIR, 57 .mode = S_IRWXU, 58 .uid = 0, 59 .gid = 0 60 }; 61 root_inode = make_directory(0, 1, &dentries, 1); 62 inode = make_directory(root_inode, 0, NULL, 0); 63 *dentries.inode = inode; 64 65 return root_inode; 66} 67 68/* Read a local directory and create the same tree in the generated filesystem. 69 Calls itself recursively with each directory in the given directory */ 70static u32 build_directory_structure(const char *full_path, const char *dir_path, 71 u32 dir_inode, int android) 72{ 73 int entries = 0; 74 struct dentry *dentries; 75 struct dirent **namelist; 76 struct stat stat; 77 int ret; 78 int i; 79 u32 inode; 80 u32 entry_inode; 81 u32 dirs = 0; 82 83 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); 84 if (entries < 0) { 85 error_errno("scandir"); 86 return EXT4_ALLOCATE_FAILED; 87 } 88 89 dentries = calloc(entries, sizeof(struct dentry)); 90 if (dentries == NULL) 91 critical_error_errno("malloc"); 92 93 for (i = 0; i < entries; i++) { 94 dentries[i].filename = strdup(namelist[i]->d_name); 95 if (dentries[i].filename == NULL) 96 critical_error_errno("strdup"); 97 98 asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name); 99 asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name); 100 101 free(namelist[i]); 102 103 ret = lstat(dentries[i].full_path, &stat); 104 if (ret < 0) { 105 error_errno("lstat"); 106 i--; 107 entries--; 108 continue; 109 } 110 111 dentries[i].size = stat.st_size; 112 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); 113 if (android) { 114#ifdef ANDROID 115 unsigned int mode = 0; 116 unsigned int uid = 0; 117 unsigned int gid = 0; 118 int dir = S_ISDIR(stat.st_mode); 119 fs_config(dentries[i].path, dir, &uid, &gid, &mode); 120 dentries[i].mode = mode; 121 dentries[i].uid = uid; 122 dentries[i].gid = gid; 123#else 124 error("can't set android permissions - built without android support"); 125#endif 126 } 127 128 if (S_ISREG(stat.st_mode)) { 129 dentries[i].file_type = EXT4_FT_REG_FILE; 130 } else if (S_ISDIR(stat.st_mode)) { 131 dentries[i].file_type = EXT4_FT_DIR; 132 dirs++; 133 } else if (S_ISCHR(stat.st_mode)) { 134 dentries[i].file_type = EXT4_FT_CHRDEV; 135 } else if (S_ISBLK(stat.st_mode)) { 136 dentries[i].file_type = EXT4_FT_BLKDEV; 137 } else if (S_ISFIFO(stat.st_mode)) { 138 dentries[i].file_type = EXT4_FT_FIFO; 139 } else if (S_ISSOCK(stat.st_mode)) { 140 dentries[i].file_type = EXT4_FT_SOCK; 141 } else if (S_ISLNK(stat.st_mode)) { 142 dentries[i].file_type = EXT4_FT_SYMLINK; 143 dentries[i].link = calloc(info.block_size, 1); 144 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1); 145 } else { 146 error("unknown file type on %s", dentries[i].path); 147 i--; 148 entries--; 149 } 150 } 151 free(namelist); 152 153 inode = make_directory(dir_inode, entries, dentries, dirs); 154 155 for (i = 0; i < entries; i++) { 156 if (dentries[i].file_type == EXT4_FT_REG_FILE) { 157 entry_inode = make_file(dentries[i].full_path, dentries[i].size); 158 } else if (dentries[i].file_type == EXT4_FT_DIR) { 159 entry_inode = build_directory_structure(dentries[i].full_path, 160 dentries[i].path, inode, android); 161 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) { 162 entry_inode = make_link(dentries[i].full_path, dentries[i].link); 163 } else { 164 error("unknown file type on %s", dentries[i].path); 165 entry_inode = 0; 166 } 167 *dentries[i].inode = entry_inode; 168 169 ret = inode_set_permissions(entry_inode, dentries[i].mode, 170 dentries[i].uid, dentries[i].gid); 171 if (ret) 172 error("failed to set permissions on %s\n", dentries[i].path); 173 174 free(dentries[i].path); 175 free(dentries[i].full_path); 176 free(dentries[i].link); 177 free((void *)dentries[i].filename); 178 } 179 180 free(dentries); 181 return inode; 182} 183 184static u32 compute_block_size() 185{ 186 return 4096; 187} 188 189static u32 compute_journal_blocks() 190{ 191 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64; 192 if (journal_blocks < 1024) 193 journal_blocks = 1024; 194 if (journal_blocks > 32768) 195 journal_blocks = 32768; 196 return journal_blocks; 197} 198 199static u32 compute_blocks_per_group() 200{ 201 return info.block_size * 8; 202} 203 204static u32 compute_inodes() 205{ 206 return DIV_ROUND_UP(info.len, info.block_size) / 4; 207} 208 209static u32 compute_inodes_per_group() 210{ 211 u32 blocks = DIV_ROUND_UP(info.len, info.block_size); 212 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group); 213 return DIV_ROUND_UP(info.inodes, block_groups); 214} 215 216void reset_ext4fs_info() { 217 // Reset all the global data structures used by make_ext4fs so it 218 // can be called again. 219 memset(&info, 0, sizeof(info)); 220 memset(&aux_info, 0, sizeof(aux_info)); 221 free_data_blocks(); 222} 223 224int make_ext4fs(const char *filename, const char *directory, 225 char *mountpoint, int android, int gzip) 226{ 227 u32 root_inode_num; 228 u16 root_mode; 229 230 if (info.len == 0) 231 info.len = get_file_size(filename); 232 233 if (info.len <= 0) { 234 fprintf(stderr, "Need size of filesystem\n"); 235 return EXIT_FAILURE; 236 } 237 238 if (info.block_size <= 0) 239 info.block_size = compute_block_size(); 240 241 if (info.journal_blocks == 0) 242 info.journal_blocks = compute_journal_blocks(); 243 244 if (info.no_journal == 0) 245 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL; 246 else 247 info.journal_blocks = 0; 248 249 if (info.blocks_per_group <= 0) 250 info.blocks_per_group = compute_blocks_per_group(); 251 252 if (info.inodes <= 0) 253 info.inodes = compute_inodes(); 254 255 if (info.inode_size <= 0) 256 info.inode_size = 256; 257 258 if (info.label == NULL) 259 info.label = ""; 260 261 info.inodes_per_group = compute_inodes_per_group(); 262 263 info.feat_compat |= 264 EXT4_FEATURE_COMPAT_RESIZE_INODE; 265 266 info.feat_ro_compat |= 267 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | 268 EXT4_FEATURE_RO_COMPAT_LARGE_FILE; 269 270 info.feat_incompat |= 271 EXT4_FEATURE_INCOMPAT_EXTENTS | 272 EXT4_FEATURE_INCOMPAT_FILETYPE; 273 274 275 printf("Creating filesystem with parameters:\n"); 276 printf(" Size: %llu\n", info.len); 277 printf(" Block size: %d\n", info.block_size); 278 printf(" Blocks per group: %d\n", info.blocks_per_group); 279 printf(" Inodes per group: %d\n", info.inodes_per_group); 280 printf(" Inode size: %d\n", info.inode_size); 281 printf(" Journal blocks: %d\n", info.journal_blocks); 282 printf(" Label: %s\n", info.label); 283 284 ext4_create_fs_aux_info(); 285 286 printf(" Blocks: %llu\n", aux_info.len_blocks); 287 printf(" Block groups: %d\n", aux_info.groups); 288 printf(" Reserved block group size: %d\n", aux_info.bg_desc_reserve_blocks); 289 290 block_allocator_init(); 291 292 ext4_fill_in_sb(); 293 294 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED) 295 error("failed to reserve first 10 inodes"); 296 297 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) 298 ext4_create_journal_inode(); 299 300 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE) 301 ext4_create_resize_inode(); 302 303 if (directory) 304 root_inode_num = build_directory_structure(directory, mountpoint, 0, android); 305 else 306 root_inode_num = build_default_directory_structure(); 307 308 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 309 inode_set_permissions(root_inode_num, root_mode, 0, 0); 310 311 ext4_update_free(); 312 313 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n", 314 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, 315 aux_info.sb->s_inodes_count, 316 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, 317 aux_info.sb->s_blocks_count_lo); 318 319 write_ext4_image(filename, gzip); 320 321 return 0; 322} 323