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