mke2fs.c revision 493024ea1d74e4cb48aac3a24111f5c8da343e9f
1/* 2 * mke2fs.c - Make a ext2fs filesystem. 3 * 4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 5 * 2003, 2004, 2005 by Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * License. 10 * %End-Header% 11 */ 12 13/* Usage: mke2fs [options] device 14 * 15 * The device may be a block device or a image of one, but this isn't 16 * enforced (but it's not much fun on a character device :-). 17 */ 18 19#define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */ 20 21#include <stdio.h> 22#include <string.h> 23#include <strings.h> 24#include <fcntl.h> 25#include <ctype.h> 26#include <time.h> 27#ifdef __linux__ 28#include <sys/utsname.h> 29#endif 30#ifdef HAVE_GETOPT_H 31#include <getopt.h> 32#else 33extern char *optarg; 34extern int optind; 35#endif 36#ifdef HAVE_UNISTD_H 37#include <unistd.h> 38#endif 39#ifdef HAVE_STDLIB_H 40#include <stdlib.h> 41#endif 42#ifdef HAVE_ERRNO_H 43#include <errno.h> 44#endif 45#ifdef HAVE_MNTENT_H 46#include <mntent.h> 47#endif 48#include <sys/ioctl.h> 49#include <sys/types.h> 50#include <sys/stat.h> 51#include <libgen.h> 52#include <limits.h> 53#include <blkid/blkid.h> 54 55#include "ext2fs/ext2_fs.h" 56#include "ext2fs/ext2fsP.h" 57#include "et/com_err.h" 58#include "uuid/uuid.h" 59#include "e2p/e2p.h" 60#include "ext2fs/ext2fs.h" 61#include "util.h" 62#include "profile.h" 63#include "prof_err.h" 64#include "../version.h" 65#include "nls-enable.h" 66 67#define STRIDE_LENGTH 8 68 69#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) 70 71#ifndef __sparc__ 72#define ZAP_BOOTBLOCK 73#endif 74 75extern int isatty(int); 76extern FILE *fpopen(const char *cmd, const char *mode); 77 78const char * program_name = "mke2fs"; 79const char * device_name /* = NULL */; 80 81/* Command line options */ 82int cflag; 83int verbose; 84int quiet; 85int super_only; 86int discard = 1; 87int force; 88int noaction; 89int journal_size; 90int journal_flags; 91int lazy_itable_init; 92char *bad_blocks_filename; 93__u32 fs_stride; 94 95struct ext2_super_block fs_param; 96char *fs_uuid = NULL; 97char *creator_os; 98char *volume_label; 99char *mount_dir; 100char *journal_device; 101int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ 102char **fs_types; 103 104profile_t profile; 105 106int sys_page_size = 4096; 107int linux_version_code = 0; 108 109static void usage(void) 110{ 111 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] " 112 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] " 113 "[-J journal-options]\n" 114 "\t[-G meta group size] [-N number-of-inodes]\n" 115 "\t[-m reserved-blocks-percentage] [-o creator-os]\n" 116 "\t[-g blocks-per-group] [-L volume-label] " 117 "[-M last-mounted-directory]\n\t[-O feature[,...]] " 118 "[-r fs-revision] [-E extended-option[,...]]\n" 119 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"), 120 program_name); 121 exit(1); 122} 123 124static int int_log2(unsigned long long arg) 125{ 126 int l = 0; 127 128 arg >>= 1; 129 while (arg) { 130 l++; 131 arg >>= 1; 132 } 133 return l; 134} 135 136static int int_log10(unsigned long long arg) 137{ 138 int l; 139 140 for (l=0; arg ; l++) 141 arg = arg / 10; 142 return l; 143} 144 145static int parse_version_number(const char *s) 146{ 147 int major, minor, rev; 148 char *endptr; 149 const char *cp = s; 150 151 if (!s) 152 return 0; 153 major = strtol(cp, &endptr, 10); 154 if (cp == endptr || *endptr != '.') 155 return 0; 156 cp = endptr + 1; 157 minor = strtol(cp, &endptr, 10); 158 if (cp == endptr || *endptr != '.') 159 return 0; 160 cp = endptr + 1; 161 rev = strtol(cp, &endptr, 10); 162 if (cp == endptr) 163 return 0; 164 return ((((major * 256) + minor) * 256) + rev); 165} 166 167/* 168 * Helper function for read_bb_file and test_disk 169 */ 170static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) 171{ 172 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk); 173 return; 174} 175 176/* 177 * Reads the bad blocks list from a file 178 */ 179static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, 180 const char *bad_blocks_file) 181{ 182 FILE *f; 183 errcode_t retval; 184 185 f = fopen(bad_blocks_file, "r"); 186 if (!f) { 187 com_err("read_bad_blocks_file", errno, 188 _("while trying to open %s"), bad_blocks_file); 189 exit(1); 190 } 191 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 192 fclose (f); 193 if (retval) { 194 com_err("ext2fs_read_bb_FILE", retval, 195 _("while reading in list of bad blocks from file")); 196 exit(1); 197 } 198} 199 200/* 201 * Runs the badblocks program to test the disk 202 */ 203static void test_disk(ext2_filsys fs, badblocks_list *bb_list) 204{ 205 FILE *f; 206 errcode_t retval; 207 char buf[1024]; 208 209 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize, 210 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", 211 fs->device_name, ext2fs_blocks_count(fs->super)-1); 212 if (verbose) 213 printf(_("Running command: %s\n"), buf); 214 f = popen(buf, "r"); 215 if (!f) { 216 com_err("popen", errno, 217 _("while trying to run '%s'"), buf); 218 exit(1); 219 } 220 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 221 pclose(f); 222 if (retval) { 223 com_err("ext2fs_read_bb_FILE", retval, 224 _("while processing list of bad blocks from program")); 225 exit(1); 226 } 227} 228 229static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) 230{ 231 dgrp_t i; 232 blk_t j; 233 unsigned must_be_good; 234 blk_t blk; 235 badblocks_iterate bb_iter; 236 errcode_t retval; 237 blk_t group_block; 238 int group; 239 int group_bad; 240 241 if (!bb_list) 242 return; 243 244 /* 245 * The primary superblock and group descriptors *must* be 246 * good; if not, abort. 247 */ 248 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; 249 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { 250 if (ext2fs_badblocks_list_test(bb_list, i)) { 251 fprintf(stderr, _("Block %d in primary " 252 "superblock/group descriptor area bad.\n"), i); 253 fprintf(stderr, _("Blocks %u through %u must be good " 254 "in order to build a filesystem.\n"), 255 fs->super->s_first_data_block, must_be_good); 256 fputs(_("Aborting....\n"), stderr); 257 exit(1); 258 } 259 } 260 261 /* 262 * See if any of the bad blocks are showing up in the backup 263 * superblocks and/or group descriptors. If so, issue a 264 * warning and adjust the block counts appropriately. 265 */ 266 group_block = fs->super->s_first_data_block + 267 fs->super->s_blocks_per_group; 268 269 for (i = 1; i < fs->group_desc_count; i++) { 270 group_bad = 0; 271 for (j=0; j < fs->desc_blocks+1; j++) { 272 if (ext2fs_badblocks_list_test(bb_list, 273 group_block + j)) { 274 if (!group_bad) 275 fprintf(stderr, 276_("Warning: the backup superblock/group descriptors at block %u contain\n" 277" bad blocks.\n\n"), 278 group_block); 279 group_bad++; 280 group = ext2fs_group_of_blk2(fs, group_block+j); 281 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1); 282 ext2fs_group_desc_csum_set(fs, group); 283 ext2fs_free_blocks_count_add(fs->super, 1); 284 } 285 } 286 group_block += fs->super->s_blocks_per_group; 287 } 288 289 /* 290 * Mark all the bad blocks as used... 291 */ 292 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); 293 if (retval) { 294 com_err("ext2fs_badblocks_list_iterate_begin", retval, 295 _("while marking bad blocks as used")); 296 exit(1); 297 } 298 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) 299 ext2fs_mark_block_bitmap2(fs->block_map, blk); 300 ext2fs_badblocks_list_iterate_end(bb_iter); 301} 302 303static void write_inode_tables(ext2_filsys fs, int lazy_flag) 304{ 305 errcode_t retval; 306 blk64_t blk; 307 dgrp_t i; 308 int num, ipb; 309 struct ext2fs_numeric_progress_struct progress; 310 311 ext2fs_numeric_progress_init(fs, &progress, 312 _("Writing inode tables: "), 313 fs->group_desc_count); 314 315 for (i = 0; i < fs->group_desc_count; i++) { 316 ext2fs_numeric_progress_update(fs, &progress, i); 317 318 blk = ext2fs_inode_table_loc(fs, i); 319 num = fs->inode_blocks_per_group; 320 321 if (lazy_flag) { 322 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super); 323 num = ((((fs->super->s_inodes_per_group - 324 ext2fs_bg_itable_unused(fs, i)) * 325 EXT2_INODE_SIZE(fs->super)) + 326 EXT2_BLOCK_SIZE(fs->super) - 1) / 327 EXT2_BLOCK_SIZE(fs->super)); 328 } else { 329 /* The kernel doesn't need to zero the itable blocks */ 330 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED); 331 ext2fs_group_desc_csum_set(fs, i); 332 } 333 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num); 334 if (retval) { 335 fprintf(stderr, _("\nCould not write %d " 336 "blocks in inode table starting at %llu: %s\n"), 337 num, blk, error_message(retval)); 338 exit(1); 339 } 340 if (sync_kludge) { 341 if (sync_kludge == 1) 342 sync(); 343 else if ((i % sync_kludge) == 0) 344 sync(); 345 } 346 } 347 ext2fs_zero_blocks2(0, 0, 0, 0, 0); 348 ext2fs_numeric_progress_close(fs, &progress, 349 _("done \n")); 350} 351 352static void create_root_dir(ext2_filsys fs) 353{ 354 errcode_t retval; 355 struct ext2_inode inode; 356 __u32 uid, gid; 357 358 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); 359 if (retval) { 360 com_err("ext2fs_mkdir", retval, _("while creating root dir")); 361 exit(1); 362 } 363 if (geteuid()) { 364 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); 365 if (retval) { 366 com_err("ext2fs_read_inode", retval, 367 _("while reading root inode")); 368 exit(1); 369 } 370 uid = getuid(); 371 inode.i_uid = uid; 372 ext2fs_set_i_uid_high(inode, uid >> 16); 373 if (uid) { 374 gid = getgid(); 375 inode.i_gid = gid; 376 ext2fs_set_i_gid_high(inode, gid >> 16); 377 } 378 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); 379 if (retval) { 380 com_err("ext2fs_write_inode", retval, 381 _("while setting root inode ownership")); 382 exit(1); 383 } 384 } 385} 386 387static void create_lost_and_found(ext2_filsys fs) 388{ 389 unsigned int lpf_size = 0; 390 errcode_t retval; 391 ext2_ino_t ino; 392 const char *name = "lost+found"; 393 int i; 394 395 fs->umask = 077; 396 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); 397 if (retval) { 398 com_err("ext2fs_mkdir", retval, 399 _("while creating /lost+found")); 400 exit(1); 401 } 402 403 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); 404 if (retval) { 405 com_err("ext2_lookup", retval, 406 _("while looking up /lost+found")); 407 exit(1); 408 } 409 410 for (i=1; i < EXT2_NDIR_BLOCKS; i++) { 411 /* Ensure that lost+found is at least 2 blocks, so we always 412 * test large empty blocks for big-block filesystems. */ 413 if ((lpf_size += fs->blocksize) >= 16*1024 && 414 lpf_size >= 2 * fs->blocksize) 415 break; 416 retval = ext2fs_expand_dir(fs, ino); 417 if (retval) { 418 com_err("ext2fs_expand_dir", retval, 419 _("while expanding /lost+found")); 420 exit(1); 421 } 422 } 423} 424 425static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) 426{ 427 errcode_t retval; 428 429 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO); 430 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0); 431 retval = ext2fs_update_bb_inode(fs, bb_list); 432 if (retval) { 433 com_err("ext2fs_update_bb_inode", retval, 434 _("while setting bad block inode")); 435 exit(1); 436 } 437 438} 439 440static void reserve_inodes(ext2_filsys fs) 441{ 442 ext2_ino_t i; 443 444 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) 445 ext2fs_inode_alloc_stats2(fs, i, +1, 0); 446 ext2fs_mark_ib_dirty(fs); 447} 448 449#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */ 450#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */ 451#define BSD_LABEL_OFFSET 64 452 453static void zap_sector(ext2_filsys fs, int sect, int nsect) 454{ 455 char *buf; 456 int retval; 457 unsigned int *magic; 458 459 buf = malloc(512*nsect); 460 if (!buf) { 461 printf(_("Out of memory erasing sectors %d-%d\n"), 462 sect, sect + nsect - 1); 463 exit(1); 464 } 465 466 if (sect == 0) { 467 /* Check for a BSD disklabel, and don't erase it if so */ 468 retval = io_channel_read_blk64(fs->io, 0, -512, buf); 469 if (retval) 470 fprintf(stderr, 471 _("Warning: could not read block 0: %s\n"), 472 error_message(retval)); 473 else { 474 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); 475 if ((*magic == BSD_DISKMAGIC) || 476 (*magic == BSD_MAGICDISK)) 477 return; 478 } 479 } 480 481 memset(buf, 0, 512*nsect); 482 io_channel_set_blksize(fs->io, 512); 483 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf); 484 io_channel_set_blksize(fs->io, fs->blocksize); 485 free(buf); 486 if (retval) 487 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"), 488 sect, error_message(retval)); 489} 490 491static void create_journal_dev(ext2_filsys fs) 492{ 493 struct ext2fs_numeric_progress_struct progress; 494 errcode_t retval; 495 char *buf; 496 blk64_t blk, err_blk; 497 int c, count, err_count; 498 499 retval = ext2fs_create_journal_superblock(fs, 500 ext2fs_blocks_count(fs->super), 0, &buf); 501 if (retval) { 502 com_err("create_journal_dev", retval, 503 _("while initializing journal superblock")); 504 exit(1); 505 } 506 ext2fs_numeric_progress_init(fs, &progress, 507 _("Zeroing journal device: "), 508 ext2fs_blocks_count(fs->super)); 509 blk = 0; 510 count = ext2fs_blocks_count(fs->super); 511 while (count > 0) { 512 if (count > 1024) 513 c = 1024; 514 else 515 c = count; 516 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count); 517 if (retval) { 518 com_err("create_journal_dev", retval, 519 _("while zeroing journal device " 520 "(block %llu, count %d)"), 521 err_blk, err_count); 522 exit(1); 523 } 524 blk += c; 525 count -= c; 526 ext2fs_numeric_progress_update(fs, &progress, blk); 527 } 528 ext2fs_zero_blocks2(0, 0, 0, 0, 0); 529 530 retval = io_channel_write_blk64(fs->io, 531 fs->super->s_first_data_block+1, 532 1, buf); 533 if (retval) { 534 com_err("create_journal_dev", retval, 535 _("while writing journal superblock")); 536 exit(1); 537 } 538 ext2fs_numeric_progress_close(fs, &progress, NULL); 539} 540 541static void show_stats(ext2_filsys fs) 542{ 543 struct ext2_super_block *s = fs->super; 544 char buf[80]; 545 char *os; 546 blk64_t group_block; 547 dgrp_t i; 548 int need, col_left; 549 550 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s)) 551 fprintf(stderr, _("warning: %llu blocks unused.\n\n"), 552 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s)); 553 554 memset(buf, 0, sizeof(buf)); 555 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); 556 printf(_("Filesystem label=%s\n"), buf); 557 fputs(_("OS type: "), stdout); 558 os = e2p_os2string(fs->super->s_creator_os); 559 fputs(os, stdout); 560 free(os); 561 printf("\n"); 562 printf(_("Block size=%u (log=%u)\n"), fs->blocksize, 563 s->s_log_block_size); 564 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize, 565 s->s_log_frag_size); 566 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"), 567 s->s_raid_stride, s->s_raid_stripe_width); 568 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count, 569 ext2fs_blocks_count(s)); 570 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"), 571 ext2fs_r_blocks_count(s), 572 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s)); 573 printf(_("First data block=%u\n"), s->s_first_data_block); 574 if (s->s_reserved_gdt_blocks) 575 printf(_("Maximum filesystem blocks=%lu\n"), 576 (s->s_reserved_gdt_blocks + fs->desc_blocks) * 577 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group); 578 if (fs->group_desc_count > 1) 579 printf(_("%u block groups\n"), fs->group_desc_count); 580 else 581 printf(_("%u block group\n"), fs->group_desc_count); 582 printf(_("%u blocks per group, %u fragments per group\n"), 583 s->s_blocks_per_group, s->s_frags_per_group); 584 printf(_("%u inodes per group\n"), s->s_inodes_per_group); 585 586 if (fs->group_desc_count == 1) { 587 printf("\n"); 588 return; 589 } 590 591 printf(_("Superblock backups stored on blocks: ")); 592 group_block = s->s_first_data_block; 593 col_left = 0; 594 for (i = 1; i < fs->group_desc_count; i++) { 595 group_block += s->s_blocks_per_group; 596 if (!ext2fs_bg_has_super(fs, i)) 597 continue; 598 if (i != 1) 599 printf(", "); 600 need = int_log10(group_block) + 2; 601 if (need > col_left) { 602 printf("\n\t"); 603 col_left = 72; 604 } 605 col_left -= need; 606 printf("%llu", group_block); 607 } 608 printf("\n\n"); 609} 610 611/* 612 * Set the S_CREATOR_OS field. Return true if OS is known, 613 * otherwise, 0. 614 */ 615static int set_os(struct ext2_super_block *sb, char *os) 616{ 617 if (isdigit (*os)) 618 sb->s_creator_os = atoi (os); 619 else if (strcasecmp(os, "linux") == 0) 620 sb->s_creator_os = EXT2_OS_LINUX; 621 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) 622 sb->s_creator_os = EXT2_OS_HURD; 623 else if (strcasecmp(os, "freebsd") == 0) 624 sb->s_creator_os = EXT2_OS_FREEBSD; 625 else if (strcasecmp(os, "lites") == 0) 626 sb->s_creator_os = EXT2_OS_LITES; 627 else 628 return 0; 629 return 1; 630} 631 632#define PATH_SET "PATH=/sbin" 633 634static void parse_extended_opts(struct ext2_super_block *param, 635 const char *opts) 636{ 637 char *buf, *token, *next, *p, *arg, *badopt = 0; 638 int len; 639 int r_usage = 0; 640 641 len = strlen(opts); 642 buf = malloc(len+1); 643 if (!buf) { 644 fprintf(stderr, 645 _("Couldn't allocate memory to parse options!\n")); 646 exit(1); 647 } 648 strcpy(buf, opts); 649 for (token = buf; token && *token; token = next) { 650 p = strchr(token, ','); 651 next = 0; 652 if (p) { 653 *p = 0; 654 next = p+1; 655 } 656 arg = strchr(token, '='); 657 if (arg) { 658 *arg = 0; 659 arg++; 660 } 661 if (strcmp(token, "stride") == 0) { 662 if (!arg) { 663 r_usage++; 664 badopt = token; 665 continue; 666 } 667 param->s_raid_stride = strtoul(arg, &p, 0); 668 if (*p || (param->s_raid_stride == 0)) { 669 fprintf(stderr, 670 _("Invalid stride parameter: %s\n"), 671 arg); 672 r_usage++; 673 continue; 674 } 675 } else if (strcmp(token, "stripe-width") == 0 || 676 strcmp(token, "stripe_width") == 0) { 677 if (!arg) { 678 r_usage++; 679 badopt = token; 680 continue; 681 } 682 param->s_raid_stripe_width = strtoul(arg, &p, 0); 683 if (*p || (param->s_raid_stripe_width == 0)) { 684 fprintf(stderr, 685 _("Invalid stripe-width parameter: %s\n"), 686 arg); 687 r_usage++; 688 continue; 689 } 690 } else if (!strcmp(token, "resize")) { 691 blk64_t resize; 692 unsigned long bpg, rsv_groups; 693 unsigned long group_desc_count, desc_blocks; 694 unsigned int gdpb, blocksize; 695 int rsv_gdb; 696 697 if (!arg) { 698 r_usage++; 699 badopt = token; 700 continue; 701 } 702 703 resize = parse_num_blocks2(arg, 704 param->s_log_block_size); 705 706 if (resize == 0) { 707 fprintf(stderr, 708 _("Invalid resize parameter: %s\n"), 709 arg); 710 r_usage++; 711 continue; 712 } 713 if (resize <= ext2fs_blocks_count(param)) { 714 fprintf(stderr, 715 _("The resize maximum must be greater " 716 "than the filesystem size.\n")); 717 r_usage++; 718 continue; 719 } 720 721 blocksize = EXT2_BLOCK_SIZE(param); 722 bpg = param->s_blocks_per_group; 723 if (!bpg) 724 bpg = blocksize * 8; 725 gdpb = EXT2_DESC_PER_BLOCK(param); 726 group_desc_count = (__u32) ext2fs_div64_ceil( 727 ext2fs_blocks_count(param), bpg); 728 desc_blocks = (group_desc_count + 729 gdpb - 1) / gdpb; 730 rsv_groups = ext2fs_div64_ceil(resize, bpg); 731 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - 732 desc_blocks; 733 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param)) 734 rsv_gdb = EXT2_ADDR_PER_BLOCK(param); 735 736 if (rsv_gdb > 0) { 737 if (param->s_rev_level == EXT2_GOOD_OLD_REV) { 738 fprintf(stderr, 739 _("On-line resizing not supported with revision 0 filesystems\n")); 740 free(buf); 741 exit(1); 742 } 743 param->s_feature_compat |= 744 EXT2_FEATURE_COMPAT_RESIZE_INODE; 745 746 param->s_reserved_gdt_blocks = rsv_gdb; 747 } 748 } else if (!strcmp(token, "test_fs")) { 749 param->s_flags |= EXT2_FLAGS_TEST_FILESYS; 750 } else if (!strcmp(token, "lazy_itable_init")) { 751 if (arg) 752 lazy_itable_init = strtoul(arg, &p, 0); 753 else 754 lazy_itable_init = 1; 755 } else { 756 r_usage++; 757 badopt = token; 758 } 759 } 760 if (r_usage) { 761 fprintf(stderr, _("\nBad option(s) specified: %s\n\n" 762 "Extended options are separated by commas, " 763 "and may take an argument which\n" 764 "\tis set off by an equals ('=') sign.\n\n" 765 "Valid extended options are:\n" 766 "\tstride=<RAID per-disk data chunk in blocks>\n" 767 "\tstripe-width=<RAID stride * data disks in blocks>\n" 768 "\tresize=<resize maximum size in blocks>\n" 769 "\tlazy_itable_init=<0 to disable, 1 to enable>\n" 770 "\ttest_fs\n\n"), 771 badopt ? badopt : ""); 772 free(buf); 773 exit(1); 774 } 775 if (param->s_raid_stride && 776 (param->s_raid_stripe_width % param->s_raid_stride) != 0) 777 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even " 778 "multiple of stride %u.\n\n"), 779 param->s_raid_stripe_width, param->s_raid_stride); 780 781 free(buf); 782} 783 784static __u32 ok_features[3] = { 785 /* Compat */ 786 EXT3_FEATURE_COMPAT_HAS_JOURNAL | 787 EXT2_FEATURE_COMPAT_RESIZE_INODE | 788 EXT2_FEATURE_COMPAT_DIR_INDEX | 789 EXT2_FEATURE_COMPAT_EXT_ATTR, 790 /* Incompat */ 791 EXT2_FEATURE_INCOMPAT_FILETYPE| 792 EXT3_FEATURE_INCOMPAT_EXTENTS| 793 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV| 794 EXT2_FEATURE_INCOMPAT_META_BG| 795 EXT4_FEATURE_INCOMPAT_FLEX_BG| 796 EXT4_FEATURE_INCOMPAT_64BIT, 797 /* R/O compat */ 798 EXT2_FEATURE_RO_COMPAT_LARGE_FILE| 799 EXT4_FEATURE_RO_COMPAT_HUGE_FILE| 800 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| 801 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE| 802 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| 803 EXT4_FEATURE_RO_COMPAT_GDT_CSUM 804}; 805 806 807static void syntax_err_report(const char *filename, long err, int line_num) 808{ 809 fprintf(stderr, 810 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"), 811 filename, line_num, error_message(err)); 812 exit(1); 813} 814 815static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 }; 816 817static void edit_feature(const char *str, __u32 *compat_array) 818{ 819 if (!str) 820 return; 821 822 if (e2p_edit_feature(str, compat_array, ok_features)) { 823 fprintf(stderr, _("Invalid filesystem option set: %s\n"), 824 str); 825 exit(1); 826 } 827} 828 829struct str_list { 830 char **list; 831 int num; 832 int max; 833}; 834 835static errcode_t init_list(struct str_list *sl) 836{ 837 sl->num = 0; 838 sl->max = 0; 839 sl->list = malloc((sl->max+1) * sizeof(char *)); 840 if (!sl->list) 841 return ENOMEM; 842 sl->list[0] = 0; 843 return 0; 844} 845 846static errcode_t push_string(struct str_list *sl, const char *str) 847{ 848 char **new_list; 849 850 if (sl->num >= sl->max) { 851 sl->max += 2; 852 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *)); 853 if (!new_list) 854 return ENOMEM; 855 sl->list = new_list; 856 } 857 sl->list[sl->num] = malloc(strlen(str)+1); 858 if (sl->list[sl->num] == 0) 859 return ENOMEM; 860 strcpy(sl->list[sl->num], str); 861 sl->num++; 862 sl->list[sl->num] = 0; 863 return 0; 864} 865 866static void print_str_list(char **list) 867{ 868 char **cpp; 869 870 for (cpp = list; *cpp; cpp++) { 871 printf("'%s'", *cpp); 872 if (cpp[1]) 873 fputs(", ", stdout); 874 } 875 fputc('\n', stdout); 876} 877 878static char **parse_fs_type(const char *fs_type, 879 const char *usage_types, 880 struct ext2_super_block *fs_param, 881 blk64_t fs_blocks_count, 882 char *progname) 883{ 884 const char *ext_type = 0; 885 char *parse_str; 886 char *profile_type = 0; 887 char *cp, *t; 888 const char *size_type; 889 struct str_list list; 890 unsigned long long meg; 891 int is_hurd = 0; 892 893 if (init_list(&list)) 894 return 0; 895 896 if (creator_os && (!strcasecmp(creator_os, "GNU") || 897 !strcasecmp(creator_os, "hurd"))) 898 is_hurd = 1; 899 900 if (fs_type) 901 ext_type = fs_type; 902 else if (is_hurd) 903 ext_type = "ext2"; 904 else if (!strcmp(program_name, "mke3fs")) 905 ext_type = "ext3"; 906 else if (progname) { 907 ext_type = strrchr(progname, '/'); 908 if (ext_type) 909 ext_type++; 910 else 911 ext_type = progname; 912 913 if (!strncmp(ext_type, "mkfs.", 5)) { 914 ext_type += 5; 915 if (ext_type[0] == 0) 916 ext_type = 0; 917 } else 918 ext_type = 0; 919 } 920 921 if (!ext_type) { 922 profile_get_string(profile, "defaults", "fs_type", 0, 923 "ext2", &profile_type); 924 ext_type = profile_type; 925 if (!strcmp(ext_type, "ext2") && (journal_size != 0)) 926 ext_type = "ext3"; 927 } 928 929 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") || 930 !strcmp(ext_type, "ext4dev")) { 931 profile_get_string(profile, "fs_types", ext_type, "features", 932 0, &t); 933 if (!t) { 934 printf(_("\nWarning! Your mke2fs.conf file does " 935 "not define the %s filesystem type.\n"), 936 ext_type); 937 printf(_("You probably need to install an updated " 938 "mke2fs.conf file.\n\n")); 939 sleep(5); 940 } 941 } 942 943 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param); 944 if (fs_blocks_count < 3 * meg) 945 size_type = "floppy"; 946 else if (fs_blocks_count < 512 * meg) 947 size_type = "small"; 948 else if (fs_blocks_count >= 4 * 1024 * 1024 * meg) 949 size_type = "big"; 950 else if (fs_blocks_count >= 16 * 1024 * 1024 * meg) 951 size_type = "huge"; 952 else 953 size_type = "default"; 954 955 if (!usage_types) 956 usage_types = size_type; 957 958 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1); 959 if (!parse_str) { 960 free(list.list); 961 return 0; 962 } 963 if (usage_types) 964 strcpy(parse_str, usage_types); 965 else 966 *parse_str = '\0'; 967 968 if (ext_type) 969 push_string(&list, ext_type); 970 cp = parse_str; 971 while (1) { 972 t = strchr(cp, ','); 973 if (t) 974 *t = '\0'; 975 976 if (*cp) 977 push_string(&list, cp); 978 if (t) 979 cp = t+1; 980 else { 981 cp = ""; 982 break; 983 } 984 } 985 free(parse_str); 986 free(profile_type); 987 if (is_hurd) 988 push_string(&list, "hurd"); 989 return (list.list); 990} 991 992static char *get_string_from_profile(char **fs_types, const char *opt, 993 const char *def_val) 994{ 995 char *ret = 0; 996 int i; 997 998 for (i=0; fs_types[i]; i++); 999 for (i-=1; i >=0 ; i--) { 1000 profile_get_string(profile, "fs_types", fs_types[i], 1001 opt, 0, &ret); 1002 if (ret) 1003 return ret; 1004 } 1005 profile_get_string(profile, "defaults", opt, 0, def_val, &ret); 1006 return (ret); 1007} 1008 1009static int get_int_from_profile(char **fs_types, const char *opt, int def_val) 1010{ 1011 int ret; 1012 char **cpp; 1013 1014 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret); 1015 for (cpp = fs_types; *cpp; cpp++) 1016 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret); 1017 return ret; 1018} 1019 1020static int get_bool_from_profile(char **fs_types, const char *opt, int def_val) 1021{ 1022 int ret; 1023 char **cpp; 1024 1025 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret); 1026 for (cpp = fs_types; *cpp; cpp++) 1027 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret); 1028 return ret; 1029} 1030 1031extern const char *mke2fs_default_profile; 1032static const char *default_files[] = { "<default>", 0 }; 1033 1034#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY 1035/* 1036 * Sets the geometry of a device (stripe/stride), and returns the 1037 * device's alignment offset, if any, or a negative error. 1038 */ 1039static int ext2fs_get_device_geometry(const char *file, 1040 struct ext2_super_block *fs_param) 1041{ 1042 int rc = -1; 1043 int blocksize; 1044 blkid_probe pr; 1045 blkid_topology tp; 1046 unsigned long min_io; 1047 unsigned long opt_io; 1048 struct stat statbuf; 1049 1050 /* Nothing to do for a regular file */ 1051 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode)) 1052 return 0; 1053 1054 pr = blkid_new_probe_from_filename(file); 1055 if (!pr) 1056 goto out; 1057 1058 tp = blkid_probe_get_topology(pr); 1059 if (!tp) 1060 goto out; 1061 1062 min_io = blkid_topology_get_minimum_io_size(tp); 1063 opt_io = blkid_topology_get_optimal_io_size(tp); 1064 blocksize = EXT2_BLOCK_SIZE(fs_param); 1065 1066 fs_param->s_raid_stride = min_io / blocksize; 1067 fs_param->s_raid_stripe_width = opt_io / blocksize; 1068 1069 rc = blkid_topology_get_alignment_offset(tp); 1070out: 1071 blkid_free_probe(pr); 1072 return rc; 1073} 1074#endif 1075 1076static void PRS(int argc, char *argv[]) 1077{ 1078 int b, c; 1079 int size; 1080 char *tmp, **cpp; 1081 int blocksize = 0; 1082 int inode_ratio = 0; 1083 int inode_size = 0; 1084 unsigned long flex_bg_size = 0; 1085 double reserved_ratio = 5.0; 1086 int lsector_size = 0, psector_size = 0; 1087 int show_version_only = 0; 1088 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */ 1089 errcode_t retval; 1090 char * oldpath = getenv("PATH"); 1091 char * extended_opts = 0; 1092 const char * fs_type = 0; 1093 const char * usage_types = 0; 1094 blk64_t dev_size; 1095 blk64_t fs_blocks_count = 0; 1096#ifdef __linux__ 1097 struct utsname ut; 1098#endif 1099 long sysval; 1100 int s_opt = -1, r_opt = -1; 1101 char *fs_features = 0; 1102 int use_bsize; 1103 char *newpath; 1104 int pathlen = sizeof(PATH_SET) + 1; 1105 1106 if (oldpath) 1107 pathlen += strlen(oldpath); 1108 newpath = malloc(pathlen); 1109 strcpy(newpath, PATH_SET); 1110 1111 /* Update our PATH to include /sbin */ 1112 if (oldpath) { 1113 strcat (newpath, ":"); 1114 strcat (newpath, oldpath); 1115 } 1116 putenv (newpath); 1117 1118 tmp = getenv("MKE2FS_SYNC"); 1119 if (tmp) 1120 sync_kludge = atoi(tmp); 1121 1122 /* Determine the system page size if possible */ 1123#ifdef HAVE_SYSCONF 1124#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) 1125#define _SC_PAGESIZE _SC_PAGE_SIZE 1126#endif 1127#ifdef _SC_PAGESIZE 1128 sysval = sysconf(_SC_PAGESIZE); 1129 if (sysval > 0) 1130 sys_page_size = sysval; 1131#endif /* _SC_PAGESIZE */ 1132#endif /* HAVE_SYSCONF */ 1133 1134 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL) 1135 config_fn[0] = tmp; 1136 profile_set_syntax_err_cb(syntax_err_report); 1137 retval = profile_init(config_fn, &profile); 1138 if (retval == ENOENT) { 1139 profile_init(default_files, &profile); 1140 profile_set_default(profile, mke2fs_default_profile); 1141 } 1142 1143 setbuf(stdout, NULL); 1144 setbuf(stderr, NULL); 1145 add_error_table(&et_ext2_error_table); 1146 add_error_table(&et_prof_error_table); 1147 memset(&fs_param, 0, sizeof(struct ext2_super_block)); 1148 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */ 1149 1150#ifdef __linux__ 1151 if (uname(&ut)) { 1152 perror("uname"); 1153 exit(1); 1154 } 1155 linux_version_code = parse_version_number(ut.release); 1156 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) 1157 fs_param.s_rev_level = 0; 1158#endif 1159 1160 if (argc && *argv) { 1161 program_name = get_progname(*argv); 1162 1163 /* If called as mkfs.ext3, create a journal inode */ 1164 if (!strcmp(program_name, "mkfs.ext3") || 1165 !strcmp(program_name, "mke3fs")) 1166 journal_size = -1; 1167 } 1168 1169 while ((c = getopt (argc, argv, 1170 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) { 1171 switch (c) { 1172 case 'b': 1173 blocksize = strtol(optarg, &tmp, 0); 1174 b = (blocksize > 0) ? blocksize : -blocksize; 1175 if (b < EXT2_MIN_BLOCK_SIZE || 1176 b > EXT2_MAX_BLOCK_SIZE || *tmp) { 1177 com_err(program_name, 0, 1178 _("invalid block size - %s"), optarg); 1179 exit(1); 1180 } 1181 if (blocksize > 4096) 1182 fprintf(stderr, _("Warning: blocksize %d not " 1183 "usable on most systems.\n"), 1184 blocksize); 1185 if (blocksize > 0) 1186 fs_param.s_log_block_size = 1187 int_log2(blocksize >> 1188 EXT2_MIN_BLOCK_LOG_SIZE); 1189 break; 1190 case 'c': /* Check for bad blocks */ 1191 cflag++; 1192 break; 1193 case 'f': 1194 size = strtoul(optarg, &tmp, 0); 1195 if (size < EXT2_MIN_BLOCK_SIZE || 1196 size > EXT2_MAX_BLOCK_SIZE || *tmp) { 1197 com_err(program_name, 0, 1198 _("invalid fragment size - %s"), 1199 optarg); 1200 exit(1); 1201 } 1202 fs_param.s_log_frag_size = 1203 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); 1204 fprintf(stderr, _("Warning: fragments not supported. " 1205 "Ignoring -f option\n")); 1206 break; 1207 case 'g': 1208 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0); 1209 if (*tmp) { 1210 com_err(program_name, 0, 1211 _("Illegal number for blocks per group")); 1212 exit(1); 1213 } 1214 if ((fs_param.s_blocks_per_group % 8) != 0) { 1215 com_err(program_name, 0, 1216 _("blocks per group must be multiple of 8")); 1217 exit(1); 1218 } 1219 break; 1220 case 'G': 1221 flex_bg_size = strtoul(optarg, &tmp, 0); 1222 if (*tmp) { 1223 com_err(program_name, 0, 1224 _("Illegal number for flex_bg size")); 1225 exit(1); 1226 } 1227 if (flex_bg_size < 1 || 1228 (flex_bg_size & (flex_bg_size-1)) != 0) { 1229 com_err(program_name, 0, 1230 _("flex_bg size must be a power of 2")); 1231 exit(1); 1232 } 1233 break; 1234 case 'i': 1235 inode_ratio = strtoul(optarg, &tmp, 0); 1236 if (inode_ratio < EXT2_MIN_BLOCK_SIZE || 1237 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 || 1238 *tmp) { 1239 com_err(program_name, 0, 1240 _("invalid inode ratio %s (min %d/max %d)"), 1241 optarg, EXT2_MIN_BLOCK_SIZE, 1242 EXT2_MAX_BLOCK_SIZE); 1243 exit(1); 1244 } 1245 break; 1246 case 'J': 1247 parse_journal_opts(optarg); 1248 break; 1249 case 'K': 1250 discard = 0; 1251 break; 1252 case 'j': 1253 if (!journal_size) 1254 journal_size = -1; 1255 break; 1256 case 'l': 1257 bad_blocks_filename = malloc(strlen(optarg)+1); 1258 if (!bad_blocks_filename) { 1259 com_err(program_name, ENOMEM, 1260 _("in malloc for bad_blocks_filename")); 1261 exit(1); 1262 } 1263 strcpy(bad_blocks_filename, optarg); 1264 break; 1265 case 'm': 1266 reserved_ratio = strtod(optarg, &tmp); 1267 if ( *tmp || reserved_ratio > 50 || 1268 reserved_ratio < 0) { 1269 com_err(program_name, 0, 1270 _("invalid reserved blocks percent - %s"), 1271 optarg); 1272 exit(1); 1273 } 1274 break; 1275 case 'n': 1276 noaction++; 1277 break; 1278 case 'o': 1279 creator_os = optarg; 1280 break; 1281 case 'q': 1282 quiet = 1; 1283 break; 1284 case 'r': 1285 r_opt = strtoul(optarg, &tmp, 0); 1286 if (*tmp) { 1287 com_err(program_name, 0, 1288 _("bad revision level - %s"), optarg); 1289 exit(1); 1290 } 1291 fs_param.s_rev_level = r_opt; 1292 break; 1293 case 's': /* deprecated */ 1294 s_opt = atoi(optarg); 1295 break; 1296 case 'I': 1297 inode_size = strtoul(optarg, &tmp, 0); 1298 if (*tmp) { 1299 com_err(program_name, 0, 1300 _("invalid inode size - %s"), optarg); 1301 exit(1); 1302 } 1303 break; 1304 case 'v': 1305 verbose = 1; 1306 break; 1307 case 'F': 1308 force++; 1309 break; 1310 case 'L': 1311 volume_label = optarg; 1312 break; 1313 case 'M': 1314 mount_dir = optarg; 1315 break; 1316 case 'N': 1317 num_inodes = strtoul(optarg, &tmp, 0); 1318 if (*tmp) { 1319 com_err(program_name, 0, 1320 _("bad num inodes - %s"), optarg); 1321 exit(1); 1322 } 1323 break; 1324 case 'O': 1325 fs_features = optarg; 1326 break; 1327 case 'E': 1328 case 'R': 1329 extended_opts = optarg; 1330 break; 1331 case 'S': 1332 super_only = 1; 1333 break; 1334 case 't': 1335 fs_type = optarg; 1336 break; 1337 case 'T': 1338 usage_types = optarg; 1339 break; 1340 case 'U': 1341 fs_uuid = optarg; 1342 break; 1343 case 'V': 1344 /* Print version number and exit */ 1345 show_version_only++; 1346 break; 1347 default: 1348 usage(); 1349 } 1350 } 1351 if ((optind == argc) && !show_version_only) 1352 usage(); 1353 device_name = argv[optind++]; 1354 1355 if (!quiet || show_version_only) 1356 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION, 1357 E2FSPROGS_DATE); 1358 1359 if (show_version_only) { 1360 fprintf(stderr, _("\tUsing %s\n"), 1361 error_message(EXT2_ET_BASE)); 1362 exit(0); 1363 } 1364 1365 /* 1366 * If there's no blocksize specified and there is a journal 1367 * device, use it to figure out the blocksize 1368 */ 1369 if (blocksize <= 0 && journal_device) { 1370 ext2_filsys jfs; 1371 io_manager io_ptr; 1372 1373#ifdef CONFIG_TESTIO_DEBUG 1374 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { 1375 io_ptr = test_io_manager; 1376 test_io_backing_manager = unix_io_manager; 1377 } else 1378#endif 1379 io_ptr = unix_io_manager; 1380 retval = ext2fs_open(journal_device, 1381 EXT2_FLAG_JOURNAL_DEV_OK, 0, 1382 0, io_ptr, &jfs); 1383 if (retval) { 1384 com_err(program_name, retval, 1385 _("while trying to open journal device %s\n"), 1386 journal_device); 1387 exit(1); 1388 } 1389 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { 1390 com_err(program_name, 0, 1391 _("Journal dev blocksize (%d) smaller than " 1392 "minimum blocksize %d\n"), jfs->blocksize, 1393 -blocksize); 1394 exit(1); 1395 } 1396 blocksize = jfs->blocksize; 1397 printf(_("Using journal device's blocksize: %d\n"), blocksize); 1398 fs_param.s_log_block_size = 1399 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); 1400 ext2fs_close(jfs); 1401 } 1402 1403 if (blocksize > sys_page_size) { 1404 if (!force) { 1405 com_err(program_name, 0, 1406 _("%d-byte blocks too big for system (max %d)"), 1407 blocksize, sys_page_size); 1408 proceed_question(); 1409 } 1410 fprintf(stderr, _("Warning: %d-byte blocks too big for system " 1411 "(max %d), forced to continue\n"), 1412 blocksize, sys_page_size); 1413 } 1414 if (optind < argc) { 1415 fs_blocks_count = parse_num_blocks2(argv[optind++], 1416 fs_param.s_log_block_size); 1417 if (!fs_blocks_count) { 1418 com_err(program_name, 0, _("invalid blocks count - %s"), 1419 argv[optind - 1]); 1420 exit(1); 1421 } 1422 } 1423 if (optind < argc) 1424 usage(); 1425 1426 if (!force) 1427 check_plausibility(device_name); 1428 check_mount(device_name, force, _("filesystem")); 1429 1430 fs_param.s_log_frag_size = fs_param.s_log_block_size; 1431 1432 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, fs_blocks_count, 1433 argv[0]); 1434 if (!fs_types) { 1435 fprintf(stderr, _("Failed to parse fs types list\n")); 1436 exit(1); 1437 } 1438 1439 /* Figure out what features should be enabled */ 1440 1441 tmp = NULL; 1442 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) { 1443 tmp = get_string_from_profile(fs_types, "base_features", 1444 "sparse_super,filetype,resize_inode,dir_index"); 1445 edit_feature(tmp, &fs_param.s_feature_compat); 1446 free(tmp); 1447 1448 for (cpp = fs_types; *cpp; cpp++) { 1449 tmp = NULL; 1450 profile_get_string(profile, "fs_types", *cpp, 1451 "features", "", &tmp); 1452 if (tmp && *tmp) 1453 edit_feature(tmp, &fs_param.s_feature_compat); 1454 if (tmp) 1455 free(tmp); 1456 } 1457 tmp = get_string_from_profile(fs_types, "default_features", 1458 ""); 1459 } 1460 edit_feature(fs_features ? fs_features : tmp, 1461 &fs_param.s_feature_compat); 1462 if (tmp) 1463 free(tmp); 1464 1465 if (noaction && fs_blocks_count) { 1466 dev_size = fs_blocks_count; 1467 retval = 0; 1468 } else 1469 retval = ext2fs_get_device_size2(device_name, 1470 EXT2_BLOCK_SIZE(&fs_param), 1471 &dev_size); 1472 1473 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { 1474 com_err(program_name, retval, 1475 _("while trying to determine filesystem size")); 1476 exit(1); 1477 } 1478 if (!fs_blocks_count) { 1479 if (retval == EXT2_ET_UNIMPLEMENTED) { 1480 com_err(program_name, 0, 1481 _("Couldn't determine device size; you " 1482 "must specify\nthe size of the " 1483 "filesystem\n")); 1484 exit(1); 1485 } else { 1486 if (dev_size == 0) { 1487 com_err(program_name, 0, 1488 _("Device size reported to be zero. " 1489 "Invalid partition specified, or\n\t" 1490 "partition table wasn't reread " 1491 "after running fdisk, due to\n\t" 1492 "a modified partition being busy " 1493 "and in use. You may need to reboot\n\t" 1494 "to re-read your partition table.\n" 1495 )); 1496 exit(1); 1497 } 1498 fs_blocks_count = dev_size; 1499 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) 1500 fs_blocks_count &= ~((blk64_t) ((sys_page_size / 1501 EXT2_BLOCK_SIZE(&fs_param))-1)); 1502 } 1503 } else if (!force && (fs_blocks_count > dev_size)) { 1504 com_err(program_name, 0, 1505 _("Filesystem larger than apparent device size.")); 1506 proceed_question(); 1507 } 1508 1509 /* 1510 * We now need to do a sanity check of fs_blocks_count for 1511 * 32-bit vs 64-bit block number support. 1512 */ 1513 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) { 1514 fs_blocks_count /= 4; /* Try using a 4k blocksize */ 1515 blocksize = 4096; 1516 fs_param.s_log_block_size = 2; 1517 } 1518 if ((fs_blocks_count > MAX_32_NUM) && 1519 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) && 1520 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) { 1521 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT; 1522 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE; 1523 } 1524 if ((fs_blocks_count > MAX_32_NUM) && 1525 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) { 1526 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s " 1527 "too big to be expressed\n\t" 1528 "in 32 bits using a blocksize of %d.\n"), 1529 program_name, fs_blocks_count, device_name, 1530 EXT2_BLOCK_SIZE(&fs_param)); 1531 exit(1); 1532 } 1533 1534 ext2fs_blocks_count_set(&fs_param, fs_blocks_count); 1535 1536 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 1537 fs_types[0] = strdup("journal"); 1538 fs_types[1] = 0; 1539 } 1540 1541 if (verbose) { 1542 fputs(_("fs_types for mke2fs.conf resolution: "), stdout); 1543 print_str_list(fs_types); 1544 } 1545 1546 if (r_opt == EXT2_GOOD_OLD_REV && 1547 (fs_param.s_feature_compat || fs_param.s_feature_incompat || 1548 fs_param.s_feature_ro_compat)) { 1549 fprintf(stderr, _("Filesystem features not supported " 1550 "with revision 0 filesystems\n")); 1551 exit(1); 1552 } 1553 1554 if (s_opt > 0) { 1555 if (r_opt == EXT2_GOOD_OLD_REV) { 1556 fprintf(stderr, _("Sparse superblocks not supported " 1557 "with revision 0 filesystems\n")); 1558 exit(1); 1559 } 1560 fs_param.s_feature_ro_compat |= 1561 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 1562 } else if (s_opt == 0) 1563 fs_param.s_feature_ro_compat &= 1564 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 1565 1566 if (journal_size != 0) { 1567 if (r_opt == EXT2_GOOD_OLD_REV) { 1568 fprintf(stderr, _("Journals not supported " 1569 "with revision 0 filesystems\n")); 1570 exit(1); 1571 } 1572 fs_param.s_feature_compat |= 1573 EXT3_FEATURE_COMPAT_HAS_JOURNAL; 1574 } 1575 1576 if (fs_param.s_feature_incompat & 1577 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 1578 reserved_ratio = 0; 1579 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; 1580 fs_param.s_feature_compat = 0; 1581 fs_param.s_feature_ro_compat = 0; 1582 } 1583 1584 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && 1585 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) { 1586 fprintf(stderr, _("The resize_inode and meta_bg features " 1587 "are not compatible.\n" 1588 "They can not be both enabled " 1589 "simultaneously.\n")); 1590 exit(1); 1591 } 1592 1593 /* Set first meta blockgroup via an environment variable */ 1594 /* (this is mostly for debugging purposes) */ 1595 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && 1596 ((tmp = getenv("MKE2FS_FIRST_META_BG")))) 1597 fs_param.s_first_meta_bg = atoi(tmp); 1598 1599 /* Get the hardware sector sizes, if available */ 1600 retval = ext2fs_get_device_sectsize(device_name, &lsector_size); 1601 if (retval) { 1602 com_err(program_name, retval, 1603 _("while trying to determine hardware sector size")); 1604 exit(1); 1605 } 1606 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size); 1607 if (retval) { 1608 com_err(program_name, retval, 1609 _("while trying to determine physical sector size")); 1610 exit(1); 1611 } 1612 /* Older kernels may not have physical/logical distinction */ 1613 if (!psector_size) 1614 psector_size = lsector_size; 1615 1616 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) 1617 psector_size = atoi(tmp); 1618 1619 if (blocksize <= 0) { 1620 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096); 1621 1622 if (use_bsize == -1) { 1623 use_bsize = sys_page_size; 1624 if ((linux_version_code < (2*65536 + 6*256)) && 1625 (use_bsize > 4096)) 1626 use_bsize = 4096; 1627 } 1628 if (psector_size && use_bsize < psector_size) 1629 use_bsize = psector_size; 1630 if ((blocksize < 0) && (use_bsize < (-blocksize))) 1631 use_bsize = -blocksize; 1632 blocksize = use_bsize; 1633 ext2fs_blocks_count_set(&fs_param, 1634 ext2fs_blocks_count(&fs_param) / 1635 (blocksize / 1024)); 1636 } else { 1637 if (blocksize < lsector_size || /* Impossible */ 1638 (!force && (blocksize < psector_size))) { /* Suboptimal */ 1639 com_err(program_name, EINVAL, 1640 _("while setting blocksize; too small " 1641 "for device\n")); 1642 exit(1); 1643 } else if (blocksize < psector_size) { 1644 fprintf(stderr, _("Warning: specified blocksize %d is " 1645 "less than device physical sectorsize %d, " 1646 "forced to continue\n"), blocksize, 1647 psector_size); 1648 } 1649 } 1650 1651 if (inode_ratio == 0) { 1652 inode_ratio = get_int_from_profile(fs_types, "inode_ratio", 1653 8192); 1654 if (inode_ratio < blocksize) 1655 inode_ratio = blocksize; 1656 } 1657 1658 fs_param.s_log_frag_size = fs_param.s_log_block_size = 1659 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); 1660 1661#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY 1662 retval = ext2fs_get_device_geometry(device_name, &fs_param); 1663 if (retval < 0) { 1664 fprintf(stderr, 1665 _("warning: Unable to get device geometry for %s\n"), 1666 device_name); 1667 } else if (retval) { 1668 printf(_("%s alignment is offset by %lu bytes.\n"), 1669 device_name, retval); 1670 printf(_("This may result in very poor performance, " 1671 "(re)-partitioning suggested.\n")); 1672 } 1673#endif 1674 1675 blocksize = EXT2_BLOCK_SIZE(&fs_param); 1676 1677 lazy_itable_init = get_bool_from_profile(fs_types, 1678 "lazy_itable_init", 0); 1679 1680 /* Get options from profile */ 1681 for (cpp = fs_types; *cpp; cpp++) { 1682 tmp = NULL; 1683 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp); 1684 if (tmp && *tmp) 1685 parse_extended_opts(&fs_param, tmp); 1686 free(tmp); 1687 } 1688 1689 if (extended_opts) 1690 parse_extended_opts(&fs_param, extended_opts); 1691 1692 /* Since sparse_super is the default, we would only have a problem 1693 * here if it was explicitly disabled. 1694 */ 1695 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && 1696 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 1697 com_err(program_name, 0, 1698 _("reserved online resize blocks not supported " 1699 "on non-sparse filesystem")); 1700 exit(1); 1701 } 1702 1703 if (fs_param.s_blocks_per_group) { 1704 if (fs_param.s_blocks_per_group < 256 || 1705 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) { 1706 com_err(program_name, 0, 1707 _("blocks per group count out of range")); 1708 exit(1); 1709 } 1710 } 1711 1712 if (inode_size == 0) 1713 inode_size = get_int_from_profile(fs_types, "inode_size", 0); 1714 if (!flex_bg_size && (fs_param.s_feature_incompat & 1715 EXT4_FEATURE_INCOMPAT_FLEX_BG)) 1716 flex_bg_size = get_int_from_profile(fs_types, 1717 "flex_bg_size", 16); 1718 if (flex_bg_size) { 1719 if (!(fs_param.s_feature_incompat & 1720 EXT4_FEATURE_INCOMPAT_FLEX_BG)) { 1721 com_err(program_name, 0, 1722 _("Flex_bg feature not enabled, so " 1723 "flex_bg size may not be specified")); 1724 exit(1); 1725 } 1726 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size); 1727 } 1728 1729 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) { 1730 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE || 1731 inode_size > EXT2_BLOCK_SIZE(&fs_param) || 1732 inode_size & (inode_size - 1)) { 1733 com_err(program_name, 0, 1734 _("invalid inode size %d (min %d/max %d)"), 1735 inode_size, EXT2_GOOD_OLD_INODE_SIZE, 1736 blocksize); 1737 exit(1); 1738 } 1739 fs_param.s_inode_size = inode_size; 1740 } 1741 1742 /* Make sure number of inodes specified will fit in 32 bits */ 1743 if (num_inodes == 0) { 1744 unsigned long long n; 1745 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio; 1746 if (n > MAX_32_NUM) { 1747 if (fs_param.s_feature_incompat & 1748 EXT4_FEATURE_INCOMPAT_64BIT) 1749 num_inodes = MAX_32_NUM; 1750 else { 1751 com_err(program_name, 0, 1752 _("too many inodes (%llu), raise" 1753 "inode ratio?"), n); 1754 exit(1); 1755 } 1756 } 1757 } else if (num_inodes > MAX_32_NUM) { 1758 com_err(program_name, 0, 1759 _("too many inodes (%llu), specify < 2^32 inodes"), 1760 num_inodes); 1761 exit(1); 1762 } 1763 /* 1764 * Calculate number of inodes based on the inode ratio 1765 */ 1766 fs_param.s_inodes_count = num_inodes ? num_inodes : 1767 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio; 1768 1769 if ((((long long)fs_param.s_inodes_count) * 1770 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >= 1771 ((ext2fs_blocks_count(&fs_param)) * 1772 EXT2_BLOCK_SIZE(&fs_param))) { 1773 com_err(program_name, 0, _("inode_size (%u) * inodes_count " 1774 "(%u) too big for a\n\t" 1775 "filesystem with %llu blocks, " 1776 "specify higher inode_ratio (-i)\n\t" 1777 "or lower inode count (-N).\n"), 1778 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE, 1779 fs_param.s_inodes_count, 1780 (unsigned long long) ext2fs_blocks_count(&fs_param)); 1781 exit(1); 1782 } 1783 1784 /* 1785 * Calculate number of blocks to reserve 1786 */ 1787 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio * 1788 ext2fs_blocks_count(&fs_param) / 100.0); 1789} 1790 1791static int should_do_undo(const char *name) 1792{ 1793 errcode_t retval; 1794 io_channel channel; 1795 __u16 s_magic; 1796 struct ext2_super_block super; 1797 io_manager manager = unix_io_manager; 1798 int csum_flag, force_undo; 1799 1800 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param, 1801 EXT4_FEATURE_RO_COMPAT_GDT_CSUM); 1802 force_undo = get_int_from_profile(fs_types, "force_undo", 0); 1803 if (!force_undo && (!csum_flag || !lazy_itable_init)) 1804 return 0; 1805 1806 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel); 1807 if (retval) { 1808 /* 1809 * We don't handle error cases instead we 1810 * declare that the file system doesn't exist 1811 * and let the rest of mke2fs take care of 1812 * error 1813 */ 1814 retval = 0; 1815 goto open_err_out; 1816 } 1817 1818 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET); 1819 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super); 1820 if (retval) { 1821 retval = 0; 1822 goto err_out; 1823 } 1824 1825#if defined(WORDS_BIGENDIAN) 1826 s_magic = ext2fs_swab16(super.s_magic); 1827#else 1828 s_magic = super.s_magic; 1829#endif 1830 1831 if (s_magic == EXT2_SUPER_MAGIC) 1832 retval = 1; 1833 1834err_out: 1835 io_channel_close(channel); 1836 1837open_err_out: 1838 1839 return retval; 1840} 1841 1842static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr) 1843{ 1844 errcode_t retval = 0; 1845 char *tdb_dir, *tdb_file; 1846 char *device_name, *tmp_name; 1847 1848 /* 1849 * Configuration via a conf file would be 1850 * nice 1851 */ 1852 tdb_dir = getenv("E2FSPROGS_UNDO_DIR"); 1853 if (!tdb_dir) 1854 profile_get_string(profile, "defaults", 1855 "undo_dir", 0, "/var/lib/e2fsprogs", 1856 &tdb_dir); 1857 1858 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || 1859 access(tdb_dir, W_OK)) 1860 return 0; 1861 1862 tmp_name = strdup(name); 1863 if (!tmp_name) { 1864 alloc_fn_fail: 1865 com_err(program_name, ENOMEM, 1866 _("Couldn't allocate memory for tdb filename\n")); 1867 return ENOMEM; 1868 } 1869 device_name = basename(tmp_name); 1870 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1); 1871 if (!tdb_file) 1872 goto alloc_fn_fail; 1873 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name); 1874 1875 if (!access(tdb_file, F_OK)) { 1876 if (unlink(tdb_file) < 0) { 1877 retval = errno; 1878 com_err(program_name, retval, 1879 _("while trying to delete %s"), 1880 tdb_file); 1881 free(tdb_file); 1882 return retval; 1883 } 1884 } 1885 1886 set_undo_io_backing_manager(*io_ptr); 1887 *io_ptr = undo_io_manager; 1888 set_undo_io_backup_file(tdb_file); 1889 printf(_("Overwriting existing filesystem; this can be undone " 1890 "using the command:\n" 1891 " e2undo %s %s\n\n"), tdb_file, name); 1892 1893 free(tdb_file); 1894 free(tmp_name); 1895 return retval; 1896} 1897 1898#ifdef __linux__ 1899 1900#ifndef BLKDISCARD 1901#define BLKDISCARD _IO(0x12,119) 1902#endif 1903 1904static void mke2fs_discard_blocks(ext2_filsys fs) 1905{ 1906 int fd; 1907 int ret; 1908 int blocksize; 1909 __u64 blocks; 1910 __uint64_t range[2]; 1911 1912 blocks = ext2fs_blocks_count(fs->super); 1913 blocksize = EXT2_BLOCK_SIZE(fs->super); 1914 range[0] = 0; 1915 range[1] = blocks * blocksize; 1916 1917 fd = open64(fs->device_name, O_RDWR); 1918 1919 /* 1920 * We don't care about whether the ioctl succeeds; it's only an 1921 * optmization for SSDs or sparse storage. 1922 */ 1923 if (fd > 0) { 1924 ret = ioctl(fd, BLKDISCARD, &range); 1925 if (verbose) { 1926 printf(_("Calling BLKDISCARD from %llu to %llu "), 1927 (unsigned long long) range[0], 1928 (unsigned long long) range[1]); 1929 if (ret) 1930 printf(_("failed.\n")); 1931 else 1932 printf(_("succeeded.\n")); 1933 } 1934 close(fd); 1935 } 1936} 1937#else 1938#define mke2fs_discard_blocks(fs) 1939#endif 1940 1941int main (int argc, char *argv[]) 1942{ 1943 errcode_t retval = 0; 1944 ext2_filsys fs; 1945 badblocks_list bb_list = 0; 1946 unsigned int journal_blocks; 1947 unsigned int i; 1948 int val, hash_alg; 1949 int flags; 1950 int old_bitmaps; 1951 io_manager io_ptr; 1952 char tdb_string[40]; 1953 char *hash_alg_str; 1954 1955#ifdef ENABLE_NLS 1956 setlocale(LC_MESSAGES, ""); 1957 setlocale(LC_CTYPE, ""); 1958 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 1959 textdomain(NLS_CAT_NAME); 1960#endif 1961 PRS(argc, argv); 1962 1963#ifdef CONFIG_TESTIO_DEBUG 1964 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { 1965 io_ptr = test_io_manager; 1966 test_io_backing_manager = unix_io_manager; 1967 } else 1968#endif 1969 io_ptr = unix_io_manager; 1970 1971 if (should_do_undo(device_name)) { 1972 retval = mke2fs_setup_tdb(device_name, &io_ptr); 1973 if (retval) 1974 exit(1); 1975 } 1976 1977 /* 1978 * Initialize the superblock.... 1979 */ 1980 flags = EXT2_FLAG_EXCLUSIVE; 1981 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0, 1982 &old_bitmaps); 1983 if (!old_bitmaps) 1984 flags |= EXT2_FLAG_64BITS; 1985 /* 1986 * By default, we print how many inode tables or block groups 1987 * or whatever we've written so far. The quiet flag disables 1988 * this, along with a lot of other output. 1989 */ 1990 if (!quiet) 1991 flags |= EXT2_FLAG_PRINT_PROGRESS; 1992 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs); 1993 if (retval) { 1994 com_err(device_name, retval, _("while setting up superblock")); 1995 exit(1); 1996 } 1997 1998 /* Can't undo discard ... */ 1999 if (discard && (io_ptr != undo_io_manager)) 2000 mke2fs_discard_blocks(fs); 2001 2002 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ? 2003 32768 : fs->blocksize * 8); 2004 io_channel_set_options(fs->io, tdb_string); 2005 2006 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS) 2007 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS; 2008 2009 if ((fs_param.s_feature_incompat & 2010 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) || 2011 (fs_param.s_feature_ro_compat & 2012 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM| 2013 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| 2014 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE))) 2015 fs->super->s_kbytes_written = 1; 2016 2017 /* 2018 * Wipe out the old on-disk superblock 2019 */ 2020 if (!noaction) 2021 zap_sector(fs, 2, 6); 2022 2023 /* 2024 * Parse or generate a UUID for the filesystem 2025 */ 2026 if (fs_uuid) { 2027 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) { 2028 com_err(device_name, 0, "could not parse UUID: %s\n", 2029 fs_uuid); 2030 exit(1); 2031 } 2032 } else 2033 uuid_generate(fs->super->s_uuid); 2034 2035 /* 2036 * Initialize the directory index variables 2037 */ 2038 hash_alg_str = get_string_from_profile(fs_types, "hash_alg", 2039 "half_md4"); 2040 hash_alg = e2p_string2hash(hash_alg_str); 2041 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg : 2042 EXT2_HASH_HALF_MD4; 2043 uuid_generate((unsigned char *) fs->super->s_hash_seed); 2044 2045 /* 2046 * Add "jitter" to the superblock's check interval so that we 2047 * don't check all the filesystems at the same time. We use a 2048 * kludgy hack of using the UUID to derive a random jitter value. 2049 */ 2050 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++) 2051 val += fs->super->s_uuid[i]; 2052 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; 2053 2054 /* 2055 * Override the creator OS, if applicable 2056 */ 2057 if (creator_os && !set_os(fs->super, creator_os)) { 2058 com_err (program_name, 0, _("unknown os - %s"), creator_os); 2059 exit(1); 2060 } 2061 2062 /* 2063 * For the Hurd, we will turn off filetype since it doesn't 2064 * support it. 2065 */ 2066 if (fs->super->s_creator_os == EXT2_OS_HURD) 2067 fs->super->s_feature_incompat &= 2068 ~EXT2_FEATURE_INCOMPAT_FILETYPE; 2069 2070 /* 2071 * Set the volume label... 2072 */ 2073 if (volume_label) { 2074 memset(fs->super->s_volume_name, 0, 2075 sizeof(fs->super->s_volume_name)); 2076 strncpy(fs->super->s_volume_name, volume_label, 2077 sizeof(fs->super->s_volume_name)); 2078 } 2079 2080 /* 2081 * Set the last mount directory 2082 */ 2083 if (mount_dir) { 2084 memset(fs->super->s_last_mounted, 0, 2085 sizeof(fs->super->s_last_mounted)); 2086 strncpy(fs->super->s_last_mounted, mount_dir, 2087 sizeof(fs->super->s_last_mounted)); 2088 } 2089 2090 if (!quiet || noaction) 2091 show_stats(fs); 2092 2093 if (noaction) 2094 exit(0); 2095 2096 if (fs->super->s_feature_incompat & 2097 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 2098 create_journal_dev(fs); 2099 exit(ext2fs_close(fs) ? 1 : 0); 2100 } 2101 2102 if (bad_blocks_filename) 2103 read_bb_file(fs, &bb_list, bad_blocks_filename); 2104 if (cflag) 2105 test_disk(fs, &bb_list); 2106 2107 handle_bad_blocks(fs, bb_list); 2108 fs->stride = fs_stride = fs->super->s_raid_stride; 2109 if (!quiet) 2110 printf(_("Allocating group tables: ")); 2111 retval = ext2fs_allocate_tables(fs); 2112 if (retval) { 2113 com_err(program_name, retval, 2114 _("while trying to allocate filesystem tables")); 2115 exit(1); 2116 } 2117 if (!quiet) 2118 printf(_("done \n")); 2119 if (super_only) { 2120 fs->super->s_state |= EXT2_ERROR_FS; 2121 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); 2122 } else { 2123 /* rsv must be a power of two (64kB is MD RAID sb alignment) */ 2124 blk64_t rsv = 65536 / fs->blocksize; 2125 blk64_t blocks = ext2fs_blocks_count(fs->super); 2126 blk64_t start; 2127 blk64_t ret_blk; 2128 2129#ifdef ZAP_BOOTBLOCK 2130 zap_sector(fs, 0, 2); 2131#endif 2132 2133 /* 2134 * Wipe out any old MD RAID (or other) metadata at the end 2135 * of the device. This will also verify that the device is 2136 * as large as we think. Be careful with very small devices. 2137 */ 2138 start = (blocks & ~(rsv - 1)); 2139 if (start > rsv) 2140 start -= rsv; 2141 if (start > 0) 2142 retval = ext2fs_zero_blocks2(fs, start, blocks - start, 2143 &ret_blk, NULL); 2144 2145 if (retval) { 2146 com_err(program_name, retval, 2147 _("while zeroing block %llu at end of filesystem"), 2148 ret_blk); 2149 } 2150 write_inode_tables(fs, lazy_itable_init); 2151 create_root_dir(fs); 2152 create_lost_and_found(fs); 2153 reserve_inodes(fs); 2154 create_bad_block_inode(fs, bb_list); 2155 if (fs->super->s_feature_compat & 2156 EXT2_FEATURE_COMPAT_RESIZE_INODE) { 2157 retval = ext2fs_create_resize_inode(fs); 2158 if (retval) { 2159 com_err("ext2fs_create_resize_inode", retval, 2160 _("while reserving blocks for online resize")); 2161 exit(1); 2162 } 2163 } 2164 } 2165 2166 if (journal_device) { 2167 ext2_filsys jfs; 2168 2169 if (!force) 2170 check_plausibility(journal_device); 2171 check_mount(journal_device, force, _("journal")); 2172 2173 retval = ext2fs_open(journal_device, EXT2_FLAG_RW| 2174 EXT2_FLAG_JOURNAL_DEV_OK, 0, 2175 fs->blocksize, unix_io_manager, &jfs); 2176 if (retval) { 2177 com_err(program_name, retval, 2178 _("while trying to open journal device %s\n"), 2179 journal_device); 2180 exit(1); 2181 } 2182 if (!quiet) { 2183 printf(_("Adding journal to device %s: "), 2184 journal_device); 2185 fflush(stdout); 2186 } 2187 retval = ext2fs_add_journal_device(fs, jfs); 2188 if(retval) { 2189 com_err (program_name, retval, 2190 _("\n\twhile trying to add journal to device %s"), 2191 journal_device); 2192 exit(1); 2193 } 2194 if (!quiet) 2195 printf(_("done\n")); 2196 ext2fs_close(jfs); 2197 free(journal_device); 2198 } else if ((journal_size) || 2199 (fs_param.s_feature_compat & 2200 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { 2201 journal_blocks = figure_journal_size(journal_size, fs); 2202 2203 if (super_only) { 2204 printf(_("Skipping journal creation in super-only mode\n")); 2205 fs->super->s_journal_inum = EXT2_JOURNAL_INO; 2206 goto no_journal; 2207 } 2208 2209 if (!journal_blocks) { 2210 fs->super->s_feature_compat &= 2211 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; 2212 goto no_journal; 2213 } 2214 if (!quiet) { 2215 printf(_("Creating journal (%u blocks): "), 2216 journal_blocks); 2217 fflush(stdout); 2218 } 2219 retval = ext2fs_add_journal_inode(fs, journal_blocks, 2220 journal_flags); 2221 if (retval) { 2222 com_err (program_name, retval, 2223 _("\n\twhile trying to create journal")); 2224 exit(1); 2225 } 2226 if (!quiet) 2227 printf(_("done\n")); 2228 } 2229no_journal: 2230 2231 if (!quiet) 2232 printf(_("Writing superblocks and " 2233 "filesystem accounting information: ")); 2234 retval = ext2fs_flush(fs); 2235 if (retval) { 2236 fprintf(stderr, 2237 _("\nWarning, had trouble writing out superblocks.")); 2238 } 2239 if (!quiet) { 2240 printf(_("done\n\n")); 2241 if (!getenv("MKE2FS_SKIP_CHECK_MSG")) 2242 print_check_message(fs); 2243 } 2244 val = ext2fs_close(fs); 2245 remove_error_table(&et_ext2_error_table); 2246 remove_error_table(&et_prof_error_table); 2247 profile_release(profile); 2248 return (retval || val) ? 1 : 0; 2249} 2250