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