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