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