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