1 /* 2 * initialize.c --- initialize a filesystem handle given superblock 3 * parameters. Used by mke2fs when initializing a filesystem. 4 * 5 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Library 9 * General Public License, version 2. 10 * %End-Header% 11 */ 12 13 #include "config.h" 14 #include <stdio.h> 15 #include <string.h> 16 #if HAVE_UNISTD_H 17 #include <unistd.h> 18 #endif 19 #include <fcntl.h> 20 #include <time.h> 21 #if HAVE_SYS_STAT_H 22 #include <sys/stat.h> 23 #endif 24 #if HAVE_SYS_TYPES_H 25 #include <sys/types.h> 26 #endif 27 28 #include "ext2_fs.h" 29 #include "ext2fs.h" 30 31 #ifndef O_BINARY 32 #define O_BINARY 0 33 #endif 34 35 #if defined(__linux__) && defined(EXT2_OS_LINUX) 36 #define CREATOR_OS EXT2_OS_LINUX 37 #else 38 #if defined(__GNU__) && defined(EXT2_OS_HURD) 39 #define CREATOR_OS EXT2_OS_HURD 40 #else 41 #if defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) 42 #define CREATOR_OS EXT2_OS_FREEBSD 43 #else 44 #if defined(LITES) && defined(EXT2_OS_LITES) 45 #define CREATOR_OS EXT2_OS_LITES 46 #else 47 #define CREATOR_OS EXT2_OS_LINUX /* by default */ 48 #endif /* defined(LITES) && defined(EXT2_OS_LITES) */ 49 #endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */ 50 #endif /* defined(__GNU__) && defined(EXT2_OS_HURD) */ 51 #endif /* defined(__linux__) && defined(EXT2_OS_LINUX) */ 52 53 /* 54 * Calculate the number of GDT blocks to reserve for online filesystem growth. 55 * The absolute maximum number of GDT blocks we can reserve is determined by 56 * the number of block pointers that can fit into a single block. 57 */ 58 static unsigned int calc_reserved_gdt_blocks(ext2_filsys fs) 59 { 60 struct ext2_super_block *sb = fs->super; 61 unsigned long bpg = sb->s_blocks_per_group; 62 unsigned int gdpb = EXT2_DESC_PER_BLOCK(sb); 63 unsigned long max_blocks = 0xffffffff; 64 unsigned long rsv_groups; 65 unsigned int rsv_gdb; 66 67 /* We set it at 1024x the current filesystem size, or 68 * the upper block count limit (2^32), whichever is lower. 69 */ 70 if (ext2fs_blocks_count(sb) < max_blocks / 1024) 71 max_blocks = ext2fs_blocks_count(sb) * 1024; 72 /* 73 * ext2fs_div64_ceil() is unnecessary because max_blocks is 74 * max _GDT_ blocks, which is limited to 32 bits. 75 */ 76 rsv_groups = ext2fs_div_ceil(max_blocks - sb->s_first_data_block, bpg); 77 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - fs->desc_blocks; 78 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb)) 79 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb); 80 #ifdef RES_GDT_DEBUG 81 printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %u\n", 82 max_blocks, rsv_groups, rsv_gdb); 83 #endif 84 85 return rsv_gdb; 86 } 87 88 errcode_t ext2fs_initialize(const char *name, int flags, 89 struct ext2_super_block *param, 90 io_manager manager, ext2_filsys *ret_fs) 91 { 92 ext2_filsys fs; 93 errcode_t retval; 94 struct ext2_super_block *super; 95 unsigned int rem; 96 unsigned int overhead = 0; 97 unsigned int ipg; 98 dgrp_t i; 99 blk64_t free_blocks; 100 blk_t numblocks; 101 int rsv_gdt; 102 int csum_flag; 103 int bigalloc_flag; 104 int io_flags; 105 int has_bg; 106 unsigned reserved_inos; 107 char *buf = 0; 108 char c; 109 double reserved_ratio; 110 char *time_env; 111 112 if (!param || !ext2fs_blocks_count(param)) 113 return EXT2_ET_INVALID_ARGUMENT; 114 115 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs); 116 if (retval) 117 return retval; 118 119 memset(fs, 0, sizeof(struct struct_ext2_filsys)); 120 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; 121 fs->flags = flags | EXT2_FLAG_RW; 122 fs->umask = 022; 123 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; 124 #ifdef WORDS_BIGENDIAN 125 fs->flags |= EXT2_FLAG_SWAP_BYTES; 126 #endif 127 128 time_env = getenv("E2FSPROGS_FAKE_TIME"); 129 if (time_env) 130 fs->now = strtoul(time_env, NULL, 0); 131 132 io_flags = IO_FLAG_RW; 133 if (flags & EXT2_FLAG_EXCLUSIVE) 134 io_flags |= IO_FLAG_EXCLUSIVE; 135 if (flags & EXT2_FLAG_DIRECT_IO) 136 io_flags |= IO_FLAG_DIRECT_IO; 137 io_flags |= O_BINARY; 138 retval = manager->open(name, io_flags, &fs->io); 139 if (retval) 140 goto cleanup; 141 fs->image_io = fs->io; 142 fs->io->app_data = fs; 143 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name); 144 if (retval) 145 goto cleanup; 146 147 strcpy(fs->device_name, name); 148 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super); 149 if (retval) 150 goto cleanup; 151 fs->super = super; 152 153 memset(super, 0, SUPERBLOCK_SIZE); 154 155 #define set_field(field, default) (super->field = param->field ? \ 156 param->field : (default)) 157 #define assign_field(field) (super->field = param->field) 158 159 super->s_magic = EXT2_SUPER_MAGIC; 160 super->s_state = EXT2_VALID_FS; 161 162 bigalloc_flag = ext2fs_has_feature_bigalloc(param); 163 164 assign_field(s_log_block_size); 165 166 if (bigalloc_flag) { 167 set_field(s_log_cluster_size, super->s_log_block_size+4); 168 if (super->s_log_block_size > super->s_log_cluster_size) { 169 retval = EXT2_ET_INVALID_ARGUMENT; 170 goto cleanup; 171 } 172 } else 173 super->s_log_cluster_size = super->s_log_block_size; 174 175 set_field(s_first_data_block, super->s_log_cluster_size ? 0 : 1); 176 set_field(s_max_mnt_count, 0); 177 set_field(s_errors, EXT2_ERRORS_DEFAULT); 178 set_field(s_feature_compat, 0); 179 set_field(s_feature_incompat, 0); 180 set_field(s_feature_ro_compat, 0); 181 set_field(s_default_mount_opts, 0); 182 set_field(s_first_meta_bg, 0); 183 set_field(s_raid_stride, 0); /* default stride size: 0 */ 184 set_field(s_raid_stripe_width, 0); /* default stripe width: 0 */ 185 set_field(s_log_groups_per_flex, 0); 186 set_field(s_flags, 0); 187 assign_field(s_backup_bgs[0]); 188 assign_field(s_backup_bgs[1]); 189 if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { 190 retval = EXT2_ET_UNSUPP_FEATURE; 191 goto cleanup; 192 } 193 if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { 194 retval = EXT2_ET_RO_UNSUPP_FEATURE; 195 goto cleanup; 196 } 197 198 set_field(s_rev_level, EXT2_GOOD_OLD_REV); 199 if (super->s_rev_level >= EXT2_DYNAMIC_REV) { 200 set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO); 201 set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE); 202 if (super->s_inode_size >= sizeof(struct ext2_inode_large)) { 203 int extra_isize = sizeof(struct ext2_inode_large) - 204 EXT2_GOOD_OLD_INODE_SIZE; 205 set_field(s_min_extra_isize, extra_isize); 206 set_field(s_want_extra_isize, extra_isize); 207 } 208 } else { 209 super->s_first_ino = EXT2_GOOD_OLD_FIRST_INO; 210 super->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE; 211 } 212 213 set_field(s_checkinterval, 0); 214 super->s_mkfs_time = super->s_lastcheck = fs->now ? fs->now : time(NULL); 215 216 super->s_creator_os = CREATOR_OS; 217 218 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(super); 219 fs->cluster_ratio_bits = super->s_log_cluster_size - 220 super->s_log_block_size; 221 222 if (bigalloc_flag) { 223 unsigned long long bpg; 224 225 if (param->s_blocks_per_group && 226 param->s_clusters_per_group && 227 ((param->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs)) != 228 param->s_blocks_per_group)) { 229 retval = EXT2_ET_INVALID_ARGUMENT; 230 goto cleanup; 231 } 232 if (param->s_clusters_per_group) 233 assign_field(s_clusters_per_group); 234 else if (param->s_blocks_per_group) 235 super->s_clusters_per_group = 236 param->s_blocks_per_group / 237 EXT2FS_CLUSTER_RATIO(fs); 238 else if (super->s_log_cluster_size + 15 < 32) 239 super->s_clusters_per_group = fs->blocksize * 8; 240 else 241 super->s_clusters_per_group = (fs->blocksize - 1) * 8; 242 if (super->s_clusters_per_group > EXT2_MAX_CLUSTERS_PER_GROUP(super)) 243 super->s_clusters_per_group = EXT2_MAX_CLUSTERS_PER_GROUP(super); 244 bpg = EXT2FS_C2B(fs, 245 (unsigned long long) super->s_clusters_per_group); 246 if (bpg >= (((unsigned long long) 1) << 32)) { 247 retval = EXT2_ET_INVALID_ARGUMENT; 248 goto cleanup; 249 } 250 super->s_blocks_per_group = bpg; 251 } else { 252 set_field(s_blocks_per_group, fs->blocksize * 8); 253 if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super)) 254 super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super); 255 super->s_clusters_per_group = super->s_blocks_per_group; 256 } 257 258 ext2fs_blocks_count_set(super, ext2fs_blocks_count(param) & 259 ~((blk64_t) EXT2FS_CLUSTER_MASK(fs))); 260 ext2fs_r_blocks_count_set(super, ext2fs_r_blocks_count(param)); 261 if (ext2fs_r_blocks_count(super) >= ext2fs_blocks_count(param)) { 262 retval = EXT2_ET_INVALID_ARGUMENT; 263 goto cleanup; 264 } 265 266 set_field(s_mmp_update_interval, 0); 267 268 /* 269 * If we're creating an external journal device, we don't need 270 * to bother with the rest. 271 */ 272 if (ext2fs_has_feature_journal_dev(super)) { 273 fs->group_desc_count = 0; 274 ext2fs_mark_super_dirty(fs); 275 *ret_fs = fs; 276 return 0; 277 } 278 279 retry: 280 fs->group_desc_count = (dgrp_t) ext2fs_div64_ceil( 281 ext2fs_blocks_count(super) - super->s_first_data_block, 282 EXT2_BLOCKS_PER_GROUP(super)); 283 if (fs->group_desc_count == 0) { 284 retval = EXT2_ET_TOOSMALL; 285 goto cleanup; 286 } 287 288 set_field(s_desc_size, 289 ext2fs_has_feature_64bit(super) ? 290 EXT2_MIN_DESC_SIZE_64BIT : 0); 291 292 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, 293 EXT2_DESC_PER_BLOCK(super)); 294 295 i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize; 296 297 if (ext2fs_has_feature_64bit(super) && 298 (ext2fs_blocks_count(super) / i) > (1ULL << 32)) 299 set_field(s_inodes_count, ~0U); 300 else 301 set_field(s_inodes_count, ext2fs_blocks_count(super) / i); 302 303 /* 304 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so 305 * that we have enough inodes for the filesystem(!) 306 */ 307 if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1) 308 super->s_inodes_count = EXT2_FIRST_INODE(super)+1; 309 310 /* 311 * There should be at least as many inodes as the user 312 * requested. Figure out how many inodes per group that 313 * should be. But make sure that we don't allocate more than 314 * one bitmap's worth of inodes each group. 315 */ 316 ipg = ext2fs_div_ceil(super->s_inodes_count, fs->group_desc_count); 317 if (ipg > fs->blocksize * 8) { 318 if (!bigalloc_flag && super->s_blocks_per_group >= 256) { 319 /* Try again with slightly different parameters */ 320 super->s_blocks_per_group -= 8; 321 ext2fs_blocks_count_set(super, 322 ext2fs_blocks_count(param)); 323 super->s_clusters_per_group = super->s_blocks_per_group; 324 goto retry; 325 } else { 326 retval = EXT2_ET_TOO_MANY_INODES; 327 goto cleanup; 328 } 329 } 330 331 if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super)) 332 ipg = EXT2_MAX_INODES_PER_GROUP(super); 333 334 ipg_retry: 335 super->s_inodes_per_group = ipg; 336 337 /* 338 * Make sure the number of inodes per group completely fills 339 * the inode table blocks in the descriptor. If not, add some 340 * additional inodes/group. Waste not, want not... 341 */ 342 fs->inode_blocks_per_group = (((super->s_inodes_per_group * 343 EXT2_INODE_SIZE(super)) + 344 EXT2_BLOCK_SIZE(super) - 1) / 345 EXT2_BLOCK_SIZE(super)); 346 super->s_inodes_per_group = ((fs->inode_blocks_per_group * 347 EXT2_BLOCK_SIZE(super)) / 348 EXT2_INODE_SIZE(super)); 349 /* 350 * Finally, make sure the number of inodes per group is a 351 * multiple of 8. This is needed to simplify the bitmap 352 * splicing code. 353 */ 354 if (super->s_inodes_per_group < 8) 355 super->s_inodes_per_group = 8; 356 super->s_inodes_per_group &= ~7; 357 fs->inode_blocks_per_group = (((super->s_inodes_per_group * 358 EXT2_INODE_SIZE(super)) + 359 EXT2_BLOCK_SIZE(super) - 1) / 360 EXT2_BLOCK_SIZE(super)); 361 362 /* 363 * adjust inode count to reflect the adjusted inodes_per_group 364 */ 365 if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) { 366 ipg--; 367 goto ipg_retry; 368 } 369 super->s_inodes_count = super->s_inodes_per_group * 370 fs->group_desc_count; 371 super->s_free_inodes_count = super->s_inodes_count; 372 373 /* 374 * check the number of reserved group descriptor table blocks 375 */ 376 if (ext2fs_has_feature_resize_inode(super)) 377 rsv_gdt = calc_reserved_gdt_blocks(fs); 378 else 379 rsv_gdt = 0; 380 set_field(s_reserved_gdt_blocks, rsv_gdt); 381 if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) { 382 retval = EXT2_ET_RES_GDT_BLOCKS; 383 goto cleanup; 384 } 385 386 /* 387 * Calculate the maximum number of bookkeeping blocks per 388 * group. It includes the superblock, the block group 389 * descriptors, the block bitmap, the inode bitmap, the inode 390 * table, and the reserved gdt blocks. 391 */ 392 overhead = (int) (3 + fs->inode_blocks_per_group + 393 super->s_reserved_gdt_blocks); 394 395 /* Enable meta_bg if we'd lose more than 3/4 of a BG to GDT blocks. */ 396 if (super->s_reserved_gdt_blocks + fs->desc_blocks > 397 super->s_blocks_per_group * 3 / 4) 398 ext2fs_set_feature_meta_bg(fs->super); 399 400 if (ext2fs_has_feature_meta_bg(fs->super)) 401 overhead++; 402 else 403 overhead += fs->desc_blocks; 404 405 /* This can only happen if the user requested too many inodes */ 406 if (overhead > super->s_blocks_per_group) { 407 retval = EXT2_ET_TOO_MANY_INODES; 408 goto cleanup; 409 } 410 411 /* 412 * See if the last group is big enough to support the 413 * necessary data structures. If not, we need to get rid of 414 * it. We need to recalculate the overhead for the last block 415 * group, since it might or might not have a superblock 416 * backup. 417 */ 418 overhead = (int) (2 + fs->inode_blocks_per_group); 419 has_bg = 0; 420 if (ext2fs_has_feature_sparse_super2(super)) { 421 /* 422 * We have to do this manually since 423 * super->s_backup_bgs hasn't been set up yet. 424 */ 425 if (fs->group_desc_count == 2) 426 has_bg = param->s_backup_bgs[0] != 0; 427 else 428 has_bg = param->s_backup_bgs[1] != 0; 429 } else 430 has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1); 431 if (has_bg) 432 overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks; 433 rem = ((ext2fs_blocks_count(super) - super->s_first_data_block) % 434 super->s_blocks_per_group); 435 if ((fs->group_desc_count == 1) && rem && (rem < overhead)) { 436 retval = EXT2_ET_TOOSMALL; 437 goto cleanup; 438 } 439 if (rem && (rem < overhead+50)) { 440 ext2fs_blocks_count_set(super, ext2fs_blocks_count(super) - 441 rem); 442 /* 443 * If blocks count is changed, we need to recalculate 444 * reserved blocks count not to exceed 50%. 445 */ 446 reserved_ratio = 100.0 * ext2fs_r_blocks_count(param) / 447 ext2fs_blocks_count(param); 448 ext2fs_r_blocks_count_set(super, reserved_ratio * 449 ext2fs_blocks_count(super) / 100.0); 450 451 goto retry; 452 } 453 454 /* 455 * At this point we know how big the filesystem will be. So 456 * we can do any and all allocations that depend on the block 457 * count. 458 */ 459 460 /* Set up the locations of the backup superblocks */ 461 if (ext2fs_has_feature_sparse_super2(super)) { 462 if (super->s_backup_bgs[0] >= fs->group_desc_count) 463 super->s_backup_bgs[0] = fs->group_desc_count - 1; 464 if (super->s_backup_bgs[1] >= fs->group_desc_count) 465 super->s_backup_bgs[1] = fs->group_desc_count - 1; 466 if (super->s_backup_bgs[0] == super->s_backup_bgs[1]) 467 super->s_backup_bgs[1] = 0; 468 if (super->s_backup_bgs[0] > super->s_backup_bgs[1]) { 469 __u32 t = super->s_backup_bgs[0]; 470 super->s_backup_bgs[0] = super->s_backup_bgs[1]; 471 super->s_backup_bgs[1] = t; 472 } 473 } 474 475 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf); 476 if (retval) 477 goto cleanup; 478 479 strcpy(buf, "block bitmap for "); 480 strcat(buf, fs->device_name); 481 retval = ext2fs_allocate_subcluster_bitmap(fs, buf, &fs->block_map); 482 if (retval) 483 goto cleanup; 484 485 strcpy(buf, "inode bitmap for "); 486 strcat(buf, fs->device_name); 487 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map); 488 if (retval) 489 goto cleanup; 490 491 ext2fs_free_mem(&buf); 492 493 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize, 494 &fs->group_desc); 495 if (retval) 496 goto cleanup; 497 498 memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize); 499 500 /* 501 * Reserve the superblock and group descriptors for each 502 * group, and fill in the correct group statistics for group. 503 * Note that although the block bitmap, inode bitmap, and 504 * inode table have not been allocated (and in fact won't be 505 * by this routine), they are accounted for nevertheless. 506 * 507 * If FLEX_BG meta-data grouping is used, only account for the 508 * superblock and group descriptors (the inode tables and 509 * bitmaps will be accounted for when allocated). 510 */ 511 free_blocks = 0; 512 csum_flag = ext2fs_has_group_desc_csum(fs); 513 reserved_inos = super->s_first_ino; 514 for (i = 0; i < fs->group_desc_count; i++) { 515 /* 516 * Don't set the BLOCK_UNINIT group for the last group 517 * because the block bitmap needs to be padded. 518 */ 519 if (csum_flag) { 520 if (i != fs->group_desc_count - 1) 521 ext2fs_bg_flags_set(fs, i, 522 EXT2_BG_BLOCK_UNINIT); 523 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT); 524 numblocks = super->s_inodes_per_group; 525 if (reserved_inos) { 526 if (numblocks > reserved_inos) { 527 numblocks -= reserved_inos; 528 reserved_inos = 0; 529 } else { 530 reserved_inos -= numblocks; 531 numblocks = 0; 532 } 533 } 534 ext2fs_bg_itable_unused_set(fs, i, numblocks); 535 } 536 numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map); 537 if (fs->super->s_log_groups_per_flex) 538 numblocks += 2 + fs->inode_blocks_per_group; 539 540 free_blocks += numblocks; 541 ext2fs_bg_free_blocks_count_set(fs, i, numblocks); 542 ext2fs_bg_free_inodes_count_set(fs, i, fs->super->s_inodes_per_group); 543 ext2fs_bg_used_dirs_count_set(fs, i, 0); 544 ext2fs_group_desc_csum_set(fs, i); 545 } 546 free_blocks &= ~EXT2FS_CLUSTER_MASK(fs); 547 ext2fs_free_blocks_count_set(super, free_blocks); 548 549 c = (char) 255; 550 if (((int) c) == -1) { 551 super->s_flags |= EXT2_FLAGS_SIGNED_HASH; 552 } else { 553 super->s_flags |= EXT2_FLAGS_UNSIGNED_HASH; 554 } 555 556 ext2fs_mark_super_dirty(fs); 557 ext2fs_mark_bb_dirty(fs); 558 ext2fs_mark_ib_dirty(fs); 559 560 io_channel_set_blksize(fs->io, fs->blocksize); 561 562 *ret_fs = fs; 563 return 0; 564 cleanup: 565 free(buf); 566 ext2fs_free(fs); 567 return retval; 568 } 569