2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 /* Usage: mke2fs [options] device
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 :-).
19 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
28 #include <sys/utsname.h>
29 #include <linux/version.h>
46 #include <sys/ioctl.h>
49 #include <blkid/blkid.h>
51 #include "ext2fs/ext2_fs.h"
52 #include "ext2fs/ext2fsP.h"
53 #include "uuid/uuid.h"
57 #include "../version.h"
58 #include "quota/mkquota.h"
60 #include "create_inode.h"
62 #define STRIDE_LENGTH 8
64 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
70 #define DISCARD_STEP_MB (2048)
72 extern int isatty(int);
73 extern FILE *fpopen(const char *cmd, const char *mode);
75 const char * program_name = "mke2fs";
76 static const char * device_name /* = NULL */;
78 /* Command line options */
82 static int super_only;
83 static int discard = 1; /* attempt to discard device before fs creation */
87 static int num_backups = 2; /* number of backup bg's for sparse_super2 */
88 static uid_t root_uid;
89 static gid_t root_gid;
92 static int lazy_itable_init;
93 static int packed_meta_blocks;
94 static char *bad_blocks_filename = NULL;
95 static __u32 fs_stride;
96 static int quotatype = -1; /* Initialize both user and group quotas by default */
98 static blk64_t journal_location = ~0LL;
100 static struct ext2_super_block fs_param;
101 static char *fs_uuid = NULL;
102 static char *creator_os;
103 static char *volume_label;
104 static char *mount_dir;
105 char *journal_device;
106 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
108 const char *root_dir; /* Copy files from the specified directory */
110 static profile_t profile;
112 static int sys_page_size = 4096;
113 static int linux_version_code = 0;
115 static void usage(void)
117 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
118 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
119 "[-J journal-options]\n"
120 "\t[-G flex-group-size] [-N number-of-inodes] "
121 "[-d root-directory]\n"
122 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
123 "\t[-g blocks-per-group] [-L volume-label] "
124 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
125 "[-r fs-revision] [-E extended-option[,...]]\n"
126 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
127 "[-jnqvDFKSV] device [blocks-count]\n"),
132 static int int_log2(unsigned long long arg)
144 int int_log10(unsigned long long arg)
154 static int parse_version_number(const char *s)
156 int major, minor, rev;
162 major = strtol(cp, &endptr, 10);
163 if (cp == endptr || *endptr != '.')
166 minor = strtol(cp, &endptr, 10);
167 if (cp == endptr || *endptr != '.')
170 rev = strtol(cp, &endptr, 10);
173 return KERNEL_VERSION(major, minor, rev);
176 static int is_before_linux_ver(unsigned int major, unsigned int minor)
179 static int linux_version_code = -1;
185 if (linux_version_code < 0)
186 linux_version_code = parse_version_number(ut.release);
187 if (linux_version_code == 0)
190 return linux_version_code < KERNEL_VERSION(major, minor, 0);
193 static int is_before_linux_ver(unsigned int major, unsigned int minor)
200 * Helper function for read_bb_file and test_disk
202 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
204 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
209 * Reads the bad blocks list from a file
211 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
212 const char *bad_blocks_file)
217 f = fopen(bad_blocks_file, "r");
219 com_err("read_bad_blocks_file", errno,
220 _("while trying to open %s"), bad_blocks_file);
223 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
226 com_err("ext2fs_read_bb_FILE", retval, "%s",
227 _("while reading in list of bad blocks from file"));
233 * Runs the badblocks program to test the disk
235 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
241 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
242 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
243 fs->device_name, ext2fs_blocks_count(fs->super)-1);
245 printf(_("Running command: %s\n"), buf);
248 com_err("popen", errno,
249 _("while trying to run '%s'"), buf);
252 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
255 com_err("ext2fs_read_bb_FILE", retval, "%s",
256 _("while processing list of bad blocks from program"));
261 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
265 unsigned must_be_good;
267 badblocks_iterate bb_iter;
277 * The primary superblock and group descriptors *must* be
278 * good; if not, abort.
280 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
281 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
282 if (ext2fs_badblocks_list_test(bb_list, i)) {
283 fprintf(stderr, _("Block %d in primary "
284 "superblock/group descriptor area bad.\n"), i);
285 fprintf(stderr, _("Blocks %u through %u must be good "
286 "in order to build a filesystem.\n"),
287 fs->super->s_first_data_block, must_be_good);
288 fputs(_("Aborting....\n"), stderr);
294 * See if any of the bad blocks are showing up in the backup
295 * superblocks and/or group descriptors. If so, issue a
296 * warning and adjust the block counts appropriately.
298 group_block = fs->super->s_first_data_block +
299 fs->super->s_blocks_per_group;
301 for (i = 1; i < fs->group_desc_count; i++) {
303 for (j=0; j < fs->desc_blocks+1; j++) {
304 if (ext2fs_badblocks_list_test(bb_list,
308 _("Warning: the backup superblock/group descriptors at block %u contain\n"
312 group = ext2fs_group_of_blk2(fs, group_block+j);
313 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
314 ext2fs_group_desc_csum_set(fs, group);
315 ext2fs_free_blocks_count_add(fs->super, 1);
318 group_block += fs->super->s_blocks_per_group;
322 * Mark all the bad blocks as used...
324 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
326 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
327 _("while marking bad blocks as used"));
330 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
331 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
332 ext2fs_badblocks_list_iterate_end(bb_iter);
335 static void write_reserved_inodes(ext2_filsys fs)
339 struct ext2_inode *inode;
341 retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
343 com_err("inode_init", retval,
344 "while allocating memory");
347 bzero(inode, EXT2_INODE_SIZE(fs->super));
349 for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++)
350 ext2fs_write_inode_full(fs, ino, inode,
351 EXT2_INODE_SIZE(fs->super));
353 ext2fs_free_mem(&inode);
356 static errcode_t packed_allocate_tables(ext2_filsys fs)
362 for (i = 0; i < fs->group_desc_count; i++) {
363 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
366 ext2fs_block_alloc_stats2(fs, goal, +1);
367 ext2fs_block_bitmap_loc_set(fs, i, goal);
369 for (i = 0; i < fs->group_desc_count; i++) {
370 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
373 ext2fs_block_alloc_stats2(fs, goal, +1);
374 ext2fs_inode_bitmap_loc_set(fs, i, goal);
376 for (i = 0; i < fs->group_desc_count; i++) {
377 blk64_t end = ext2fs_blocks_count(fs->super) - 1;
378 retval = ext2fs_get_free_blocks2(fs, goal, end,
379 fs->inode_blocks_per_group,
380 fs->block_map, &goal);
383 ext2fs_block_alloc_stats_range(fs, goal,
384 fs->inode_blocks_per_group, +1);
385 ext2fs_inode_table_loc_set(fs, i, goal);
390 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
396 struct ext2fs_numeric_progress_struct progress;
398 ext2fs_numeric_progress_init(fs, &progress,
399 _("Writing inode tables: "),
400 fs->group_desc_count);
402 for (i = 0; i < fs->group_desc_count; i++) {
403 ext2fs_numeric_progress_update(fs, &progress, i);
405 blk = ext2fs_inode_table_loc(fs, i);
406 num = fs->inode_blocks_per_group;
409 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
410 ext2fs_bg_itable_unused(fs, i)) *
411 EXT2_INODE_SIZE(fs->super),
412 EXT2_BLOCK_SIZE(fs->super));
413 if (!lazy_flag || itable_zeroed) {
414 /* The kernel doesn't need to zero the itable blocks */
415 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
416 ext2fs_group_desc_csum_set(fs, i);
418 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
420 fprintf(stderr, _("\nCould not write %d "
421 "blocks in inode table starting at %llu: %s\n"),
422 num, blk, error_message(retval));
426 if (sync_kludge == 1)
428 else if ((i % sync_kludge) == 0)
432 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
433 ext2fs_numeric_progress_close(fs, &progress,
436 /* Reserved inodes must always have correct checksums */
437 if (fs->super->s_creator_os == EXT2_OS_LINUX &&
438 fs->super->s_feature_ro_compat &
439 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
440 write_reserved_inodes(fs);
443 static void create_root_dir(ext2_filsys fs)
446 struct ext2_inode inode;
448 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
450 com_err("ext2fs_mkdir", retval, "%s",
451 _("while creating root dir"));
454 if (root_uid != 0 || root_gid != 0) {
455 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
457 com_err("ext2fs_read_inode", retval, "%s",
458 _("while reading root inode"));
462 inode.i_uid = root_uid;
463 ext2fs_set_i_uid_high(inode, root_uid >> 16);
464 inode.i_gid = root_gid;
465 ext2fs_set_i_gid_high(inode, root_gid >> 16);
467 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
469 com_err("ext2fs_write_inode", retval, "%s",
470 _("while setting root inode ownership"));
476 static void create_lost_and_found(ext2_filsys fs)
478 unsigned int lpf_size = 0;
481 const char *name = "lost+found";
485 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
487 com_err("ext2fs_mkdir", retval, "%s",
488 _("while creating /lost+found"));
492 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
494 com_err("ext2_lookup", retval, "%s",
495 _("while looking up /lost+found"));
499 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
500 /* Ensure that lost+found is at least 2 blocks, so we always
501 * test large empty blocks for big-block filesystems. */
502 if ((lpf_size += fs->blocksize) >= 16*1024 &&
503 lpf_size >= 2 * fs->blocksize)
505 retval = ext2fs_expand_dir(fs, ino);
507 com_err("ext2fs_expand_dir", retval, "%s",
508 _("while expanding /lost+found"));
514 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
518 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
519 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
520 retval = ext2fs_update_bb_inode(fs, bb_list);
522 com_err("ext2fs_update_bb_inode", retval, "%s",
523 _("while setting bad block inode"));
529 static void reserve_inodes(ext2_filsys fs)
533 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
534 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
535 ext2fs_mark_ib_dirty(fs);
538 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
539 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
540 #define BSD_LABEL_OFFSET 64
542 static void zap_sector(ext2_filsys fs, int sect, int nsect)
548 buf = malloc(512*nsect);
550 printf(_("Out of memory erasing sectors %d-%d\n"),
551 sect, sect + nsect - 1);
556 /* Check for a BSD disklabel, and don't erase it if so */
557 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
560 _("Warning: could not read block 0: %s\n"),
561 error_message(retval));
563 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
564 if ((*magic == BSD_DISKMAGIC) ||
565 (*magic == BSD_MAGICDISK))
570 memset(buf, 0, 512*nsect);
571 io_channel_set_blksize(fs->io, 512);
572 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
573 io_channel_set_blksize(fs->io, fs->blocksize);
576 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
577 sect, error_message(retval));
580 static void create_journal_dev(ext2_filsys fs)
582 struct ext2fs_numeric_progress_struct progress;
585 blk64_t blk, err_blk;
586 int c, count, err_count;
588 retval = ext2fs_create_journal_superblock(fs,
589 ext2fs_blocks_count(fs->super), 0, &buf);
591 com_err("create_journal_dev", retval, "%s",
592 _("while initializing journal superblock"));
596 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
597 goto write_superblock;
599 ext2fs_numeric_progress_init(fs, &progress,
600 _("Zeroing journal device: "),
601 ext2fs_blocks_count(fs->super));
603 count = ext2fs_blocks_count(fs->super);
609 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
611 com_err("create_journal_dev", retval,
612 _("while zeroing journal device "
613 "(block %llu, count %d)"),
619 ext2fs_numeric_progress_update(fs, &progress, blk);
621 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
623 ext2fs_numeric_progress_close(fs, &progress, NULL);
625 retval = io_channel_write_blk64(fs->io,
626 fs->super->s_first_data_block+1,
629 com_err("create_journal_dev", retval, "%s",
630 _("while writing journal superblock"));
635 static void show_stats(ext2_filsys fs)
637 struct ext2_super_block *s = fs->super;
644 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
645 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
646 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
648 memset(buf, 0, sizeof(buf));
649 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
650 printf(_("Filesystem label=%s\n"), buf);
651 os = e2p_os2string(fs->super->s_creator_os);
653 printf(_("OS type: %s\n"), os);
655 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
656 s->s_log_block_size);
657 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
658 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
659 printf(_("Cluster size=%u (log=%u)\n"),
660 fs->blocksize << fs->cluster_ratio_bits,
661 s->s_log_cluster_size);
663 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
664 s->s_log_cluster_size);
665 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
666 s->s_raid_stride, s->s_raid_stripe_width);
667 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
668 ext2fs_blocks_count(s));
669 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
670 ext2fs_r_blocks_count(s),
671 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
672 printf(_("First data block=%u\n"), s->s_first_data_block);
673 if (root_uid != 0 || root_gid != 0)
674 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
675 if (s->s_reserved_gdt_blocks)
676 printf(_("Maximum filesystem blocks=%lu\n"),
677 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
678 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
679 if (fs->group_desc_count > 1)
680 printf(_("%u block groups\n"), fs->group_desc_count);
682 printf(_("%u block group\n"), fs->group_desc_count);
683 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
684 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
685 printf(_("%u blocks per group, %u clusters per group\n"),
686 s->s_blocks_per_group, s->s_clusters_per_group);
688 printf(_("%u blocks per group, %u fragments per group\n"),
689 s->s_blocks_per_group, s->s_clusters_per_group);
690 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
692 if (fs->group_desc_count == 1) {
697 printf("%s", _("Superblock backups stored on blocks: "));
698 group_block = s->s_first_data_block;
700 for (i = 1; i < fs->group_desc_count; i++) {
701 group_block += s->s_blocks_per_group;
702 if (!ext2fs_bg_has_super(fs, i))
706 need = int_log10(group_block) + 2;
707 if (need > col_left) {
712 printf("%llu", group_block);
718 * Returns true if making a file system for the Hurd, else 0
720 static int for_hurd(const char *os)
730 return (atoi(os) == EXT2_OS_HURD);
731 return (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0);
735 * Set the S_CREATOR_OS field. Return true if OS is known,
738 static int set_os(struct ext2_super_block *sb, char *os)
741 sb->s_creator_os = atoi (os);
742 else if (strcasecmp(os, "linux") == 0)
743 sb->s_creator_os = EXT2_OS_LINUX;
744 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
745 sb->s_creator_os = EXT2_OS_HURD;
746 else if (strcasecmp(os, "freebsd") == 0)
747 sb->s_creator_os = EXT2_OS_FREEBSD;
748 else if (strcasecmp(os, "lites") == 0)
749 sb->s_creator_os = EXT2_OS_LITES;
755 #define PATH_SET "PATH=/sbin"
757 static void parse_extended_opts(struct ext2_super_block *param,
760 char *buf, *token, *next, *p, *arg, *badopt = 0;
767 fprintf(stderr, "%s",
768 _("Couldn't allocate memory to parse options!\n"));
772 for (token = buf; token && *token; token = next) {
773 p = strchr(token, ',');
779 arg = strchr(token, '=');
784 if (strcmp(token, "desc-size") == 0 ||
785 strcmp(token, "desc_size") == 0) {
788 if (!(fs_param.s_feature_incompat &
789 EXT4_FEATURE_INCOMPAT_64BIT)) {
791 _("%s requires '-O 64bit'\n"), token);
795 if (param->s_reserved_gdt_blocks != 0) {
797 _("'%s' must be before 'resize=%u'\n"),
798 token, param->s_reserved_gdt_blocks);
807 desc_size = strtoul(arg, &p, 0);
808 if (*p || (desc_size & (desc_size - 1))) {
810 _("Invalid desc_size: '%s'\n"), arg);
814 param->s_desc_size = desc_size;
815 } else if (strcmp(token, "offset") == 0) {
821 offset = strtoull(arg, &p, 0);
823 fprintf(stderr, _("Invalid offset: %s\n"),
828 } else if (strcmp(token, "mmp_update_interval") == 0) {
834 param->s_mmp_update_interval = strtoul(arg, &p, 0);
837 _("Invalid mmp_update_interval: %s\n"),
842 } else if (strcmp(token, "num_backup_sb") == 0) {
848 num_backups = strtoul(arg, &p, 0);
849 if (*p || num_backups > 2) {
851 _("Invalid # of backup "
857 } else if (strcmp(token, "packed_meta_blocks") == 0) {
859 packed_meta_blocks = strtoul(arg, &p, 0);
861 packed_meta_blocks = 1;
862 if (packed_meta_blocks)
863 journal_location = 0;
864 } else if (strcmp(token, "stride") == 0) {
870 param->s_raid_stride = strtoul(arg, &p, 0);
873 _("Invalid stride parameter: %s\n"),
878 } else if (strcmp(token, "stripe-width") == 0 ||
879 strcmp(token, "stripe_width") == 0) {
885 param->s_raid_stripe_width = strtoul(arg, &p, 0);
888 _("Invalid stripe-width parameter: %s\n"),
893 } else if (!strcmp(token, "resize")) {
895 unsigned long bpg, rsv_groups;
896 unsigned long group_desc_count, desc_blocks;
897 unsigned int gdpb, blocksize;
906 resize = parse_num_blocks2(arg,
907 param->s_log_block_size);
911 _("Invalid resize parameter: %s\n"),
916 if (resize <= ext2fs_blocks_count(param)) {
917 fprintf(stderr, "%s",
918 _("The resize maximum must be greater "
919 "than the filesystem size.\n"));
924 blocksize = EXT2_BLOCK_SIZE(param);
925 bpg = param->s_blocks_per_group;
928 gdpb = EXT2_DESC_PER_BLOCK(param);
929 group_desc_count = (__u32) ext2fs_div64_ceil(
930 ext2fs_blocks_count(param), bpg);
931 desc_blocks = (group_desc_count +
933 rsv_groups = ext2fs_div64_ceil(resize, bpg);
934 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
936 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
937 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
940 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
941 fprintf(stderr, "%s",
942 _("On-line resizing not supported with revision 0 filesystems\n"));
946 param->s_feature_compat |=
947 EXT2_FEATURE_COMPAT_RESIZE_INODE;
949 param->s_reserved_gdt_blocks = rsv_gdb;
951 } else if (!strcmp(token, "test_fs")) {
952 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
953 } else if (!strcmp(token, "lazy_itable_init")) {
955 lazy_itable_init = strtoul(arg, &p, 0);
957 lazy_itable_init = 1;
958 } else if (!strcmp(token, "lazy_journal_init")) {
960 journal_flags |= strtoul(arg, &p, 0) ?
961 EXT2_MKJOURNAL_LAZYINIT : 0;
963 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
964 } else if (!strcmp(token, "root_owner")) {
966 root_uid = strtoul(arg, &p, 0);
969 _("Invalid root_owner: '%s'\n"),
975 root_gid = strtoul(p, &p, 0);
978 _("Invalid root_owner: '%s'\n"),
987 } else if (!strcmp(token, "discard")) {
989 } else if (!strcmp(token, "nodiscard")) {
991 } else if (!strcmp(token, "quotatype")) {
997 if (!strncmp(arg, "usr", 3)) {
999 } else if (!strncmp(arg, "grp", 3)) {
1003 _("Invalid quotatype parameter: %s\n"),
1014 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
1015 "Extended options are separated by commas, "
1016 "and may take an argument which\n"
1017 "\tis set off by an equals ('=') sign.\n\n"
1018 "Valid extended options are:\n"
1019 "\tmmp_update_interval=<interval>\n"
1020 "\tnum_backup_sb=<0|1|2>\n"
1021 "\tstride=<RAID per-disk data chunk in blocks>\n"
1022 "\tstripe-width=<RAID stride * data disks in blocks>\n"
1023 "\toffset=<offset to create the file system>\n"
1024 "\tresize=<resize maximum size in blocks>\n"
1025 "\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
1026 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
1027 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
1028 "\troot_uid=<uid of root directory>\n"
1029 "\troot_gid=<gid of root directory>\n"
1033 "\tquotatype=<usr OR grp>\n\n"),
1034 badopt ? badopt : "");
1038 if (param->s_raid_stride &&
1039 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
1040 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
1041 "multiple of stride %u.\n\n"),
1042 param->s_raid_stripe_width, param->s_raid_stride);
1047 static __u32 ok_features[3] = {
1049 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
1050 EXT2_FEATURE_COMPAT_RESIZE_INODE |
1051 EXT2_FEATURE_COMPAT_DIR_INDEX |
1052 EXT2_FEATURE_COMPAT_EXT_ATTR |
1053 EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
1055 EXT2_FEATURE_INCOMPAT_FILETYPE|
1056 EXT3_FEATURE_INCOMPAT_EXTENTS|
1057 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
1058 EXT2_FEATURE_INCOMPAT_META_BG|
1059 EXT4_FEATURE_INCOMPAT_FLEX_BG|
1060 EXT4_FEATURE_INCOMPAT_MMP |
1061 EXT4_FEATURE_INCOMPAT_64BIT|
1062 EXT4_FEATURE_INCOMPAT_INLINE_DATA,
1064 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
1065 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
1066 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1067 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
1068 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
1069 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1070 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
1072 EXT4_FEATURE_RO_COMPAT_QUOTA|
1074 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
1078 static void syntax_err_report(const char *filename, long err, int line_num)
1081 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
1082 filename, line_num, error_message(err));
1086 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
1088 static void edit_feature(const char *str, __u32 *compat_array)
1093 if (e2p_edit_feature(str, compat_array, ok_features)) {
1094 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1100 static void edit_mntopts(const char *str, __u32 *mntopts)
1105 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1106 fprintf(stderr, _("Invalid mount option set: %s\n"),
1118 static errcode_t init_list(struct str_list *sl)
1122 sl->list = malloc((sl->max+1) * sizeof(char *));
1129 static errcode_t push_string(struct str_list *sl, const char *str)
1133 if (sl->num >= sl->max) {
1135 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1138 sl->list = new_list;
1140 sl->list[sl->num] = malloc(strlen(str)+1);
1141 if (sl->list[sl->num] == 0)
1143 strcpy(sl->list[sl->num], str);
1145 sl->list[sl->num] = 0;
1149 static void print_str_list(char **list)
1153 for (cpp = list; *cpp; cpp++) {
1154 printf("'%s'", *cpp);
1156 fputs(", ", stdout);
1158 fputc('\n', stdout);
1162 * Return TRUE if the profile has the given subsection
1164 static int profile_has_subsection(profile_t prof, const char *section,
1165 const char *subsection)
1168 const char *names[4];
1173 names[1] = subsection;
1176 if (profile_iterator_create(prof, names,
1177 PROFILE_ITER_LIST_SECTION |
1178 PROFILE_ITER_RELATIONS_ONLY, &state))
1181 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1186 profile_iterator_free(&state);
1190 static char **parse_fs_type(const char *fs_type,
1191 const char *usage_types,
1192 struct ext2_super_block *sb,
1193 blk64_t fs_blocks_count,
1196 const char *ext_type = 0;
1198 char *profile_type = 0;
1200 const char *size_type;
1201 struct str_list list;
1202 unsigned long long meg;
1203 int is_hurd = for_hurd(creator_os);
1205 if (init_list(&list))
1212 else if (!strcmp(program_name, "mke3fs"))
1214 else if (!strcmp(program_name, "mke4fs"))
1216 else if (progname) {
1217 ext_type = strrchr(progname, '/');
1221 ext_type = progname;
1223 if (!strncmp(ext_type, "mkfs.", 5)) {
1225 if (ext_type[0] == 0)
1232 profile_get_string(profile, "defaults", "fs_type", 0,
1233 "ext2", &profile_type);
1234 ext_type = profile_type;
1235 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1240 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1241 strcmp(ext_type, "ext2")) {
1242 printf(_("\nYour mke2fs.conf file does not define the "
1243 "%s filesystem type.\n"), ext_type);
1244 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1245 !strcmp(ext_type, "ext4dev")) {
1246 printf("%s", _("You probably need to install an "
1247 "updated mke2fs.conf file.\n\n"));
1250 printf("%s", _("Aborting...\n"));
1255 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1256 if (fs_blocks_count < 3 * meg)
1257 size_type = "floppy";
1258 else if (fs_blocks_count < 512 * meg)
1259 size_type = "small";
1260 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1261 size_type = "default";
1262 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1268 usage_types = size_type;
1270 parse_str = malloc(strlen(usage_types)+1);
1276 strcpy(parse_str, usage_types);
1279 push_string(&list, ext_type);
1282 t = strchr(cp, ',');
1287 if (profile_has_subsection(profile, "fs_types", cp))
1288 push_string(&list, cp);
1289 else if (strcmp(cp, "default") != 0)
1291 _("\nWarning: the fs_type %s is not "
1292 "defined in mke2fs.conf\n\n"),
1303 push_string(&list, "hurd");
1307 char *get_string_from_profile(char **types, const char *opt,
1308 const char *def_val)
1313 for (i=0; types[i]; i++);
1314 for (i-=1; i >=0 ; i--) {
1315 profile_get_string(profile, "fs_types", types[i],
1320 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1324 int get_int_from_profile(char **types, const char *opt, int def_val)
1329 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1330 for (cpp = types; *cpp; cpp++)
1331 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1335 static double get_double_from_profile(char **types, const char *opt,
1341 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1342 for (cpp = types; *cpp; cpp++)
1343 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1347 int get_bool_from_profile(char **types, const char *opt, int def_val)
1352 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1353 for (cpp = types; *cpp; cpp++)
1354 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1358 extern const char *mke2fs_default_profile;
1359 static const char *default_files[] = { "<default>", 0 };
1361 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1363 * Sets the geometry of a device (stripe/stride), and returns the
1364 * device's alignment offset, if any, or a negative error.
1366 static int get_device_geometry(const char *file,
1367 struct ext2_super_block *fs_param,
1374 unsigned long min_io;
1375 unsigned long opt_io;
1376 struct stat statbuf;
1378 /* Nothing to do for a regular file */
1379 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1382 pr = blkid_new_probe_from_filename(file);
1386 tp = blkid_probe_get_topology(pr);
1390 min_io = blkid_topology_get_minimum_io_size(tp);
1391 opt_io = blkid_topology_get_optimal_io_size(tp);
1392 blocksize = EXT2_BLOCK_SIZE(fs_param);
1393 if ((min_io == 0) && (psector_size > blocksize))
1394 min_io = psector_size;
1395 if ((opt_io == 0) && min_io)
1397 if ((opt_io == 0) && (psector_size > blocksize))
1398 opt_io = psector_size;
1400 /* setting stripe/stride to blocksize is pointless */
1401 if (min_io > blocksize)
1402 fs_param->s_raid_stride = min_io / blocksize;
1403 if (opt_io > blocksize)
1404 fs_param->s_raid_stripe_width = opt_io / blocksize;
1406 rc = blkid_topology_get_alignment_offset(tp);
1408 blkid_free_probe(pr);
1413 static void PRS(int argc, char *argv[])
1416 int cluster_size = 0;
1419 int inode_ratio = 0;
1421 unsigned long flex_bg_size = 0;
1422 double reserved_ratio = -1.0;
1423 int lsector_size = 0, psector_size = 0;
1424 int show_version_only = 0;
1425 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1427 char * oldpath = getenv("PATH");
1428 char * extended_opts = 0;
1430 char * usage_types = 0;
1433 * NOTE: A few words about fs_blocks_count and blocksize:
1435 * Initially, blocksize is set to zero, which implies 1024.
1436 * If -b is specified, blocksize is updated to the user's value.
1438 * Next, the device size or the user's "blocks" command line argument
1439 * is used to set fs_blocks_count; the units are blocksize.
1441 * Later, if blocksize hasn't been set and the profile specifies a
1442 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1443 * appropriately. Note the change in units!
1445 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1447 blk64_t fs_blocks_count = 0;
1449 int s_opt = -1, r_opt = -1;
1450 char *fs_features = 0;
1453 int pathlen = sizeof(PATH_SET) + 1;
1456 pathlen += strlen(oldpath);
1457 newpath = malloc(pathlen);
1459 fprintf(stderr, "%s",
1460 _("Couldn't allocate memory for new PATH.\n"));
1463 strcpy(newpath, PATH_SET);
1465 /* Update our PATH to include /sbin */
1467 strcat (newpath, ":");
1468 strcat (newpath, oldpath);
1472 tmp = getenv("MKE2FS_SYNC");
1474 sync_kludge = atoi(tmp);
1476 /* Determine the system page size if possible */
1478 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1479 #define _SC_PAGESIZE _SC_PAGE_SIZE
1482 sysval = sysconf(_SC_PAGESIZE);
1484 sys_page_size = sysval;
1485 #endif /* _SC_PAGESIZE */
1486 #endif /* HAVE_SYSCONF */
1488 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1490 profile_set_syntax_err_cb(syntax_err_report);
1491 retval = profile_init(config_fn, &profile);
1492 if (retval == ENOENT) {
1493 retval = profile_init(default_files, &profile);
1496 retval = profile_set_default(profile, mke2fs_default_profile);
1499 } else if (retval) {
1501 fprintf(stderr, _("Couldn't init profile successfully"
1502 " (error: %ld).\n"), retval);
1506 setbuf(stdout, NULL);
1507 setbuf(stderr, NULL);
1508 add_error_table(&et_ext2_error_table);
1509 add_error_table(&et_prof_error_table);
1510 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1511 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1513 if (is_before_linux_ver(2, 2))
1514 fs_param.s_rev_level = 0;
1516 if (argc && *argv) {
1517 program_name = get_progname(*argv);
1519 /* If called as mkfs.ext3, create a journal inode */
1520 if (!strcmp(program_name, "mkfs.ext3") ||
1521 !strcmp(program_name, "mke3fs"))
1525 while ((c = getopt (argc, argv,
1526 "b:cg:i:jl:m:no:qr:s:t:d:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1529 blocksize = parse_num_blocks2(optarg, -1);
1530 b = (blocksize > 0) ? blocksize : -blocksize;
1531 if (b < EXT2_MIN_BLOCK_SIZE ||
1532 b > EXT2_MAX_BLOCK_SIZE) {
1533 com_err(program_name, 0,
1534 _("invalid block size - %s"), optarg);
1537 if (blocksize > 4096)
1538 fprintf(stderr, _("Warning: blocksize %d not "
1539 "usable on most systems.\n"),
1542 fs_param.s_log_block_size =
1543 int_log2(blocksize >>
1544 EXT2_MIN_BLOCK_LOG_SIZE);
1546 case 'c': /* Check for bad blocks */
1550 cluster_size = parse_num_blocks2(optarg, -1);
1551 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1552 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1553 com_err(program_name, 0,
1554 _("invalid cluster size - %s"),
1563 com_err(program_name, 0, "%s",
1564 _("'-R' is deprecated, use '-E' instead"));
1567 extended_opts = optarg;
1573 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1575 com_err(program_name, 0, "%s",
1576 _("Illegal number for blocks per group"));
1579 if ((fs_param.s_blocks_per_group % 8) != 0) {
1580 com_err(program_name, 0, "%s",
1581 _("blocks per group must be multiple of 8"));
1586 flex_bg_size = strtoul(optarg, &tmp, 0);
1588 com_err(program_name, 0, "%s",
1589 _("Illegal number for flex_bg size"));
1592 if (flex_bg_size < 1 ||
1593 (flex_bg_size & (flex_bg_size-1)) != 0) {
1594 com_err(program_name, 0, "%s",
1595 _("flex_bg size must be a power of 2"));
1600 inode_ratio = strtoul(optarg, &tmp, 0);
1601 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1602 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1604 com_err(program_name, 0,
1605 _("invalid inode ratio %s (min %d/max %d)"),
1606 optarg, EXT2_MIN_BLOCK_SIZE,
1607 EXT2_MAX_BLOCK_SIZE * 1024);
1612 inode_size = strtoul(optarg, &tmp, 0);
1614 com_err(program_name, 0,
1615 _("invalid inode size - %s"), optarg);
1624 parse_journal_opts(optarg);
1627 fprintf(stderr, "%s",
1628 _("Warning: -K option is deprecated and "
1629 "should not be used anymore. Use "
1630 "\'-E nodiscard\' extended option "
1635 bad_blocks_filename = realloc(bad_blocks_filename,
1636 strlen(optarg) + 1);
1637 if (!bad_blocks_filename) {
1638 com_err(program_name, ENOMEM, "%s",
1639 _("in malloc for bad_blocks_filename"));
1642 strcpy(bad_blocks_filename, optarg);
1645 volume_label = optarg;
1648 reserved_ratio = strtod(optarg, &tmp);
1649 if ( *tmp || reserved_ratio > 50 ||
1650 reserved_ratio < 0) {
1651 com_err(program_name, 0,
1652 _("invalid reserved blocks percent - %s"),
1664 num_inodes = strtoul(optarg, &tmp, 0);
1666 com_err(program_name, 0,
1667 _("bad num inodes - %s"), optarg);
1672 creator_os = optarg;
1675 fs_features = optarg;
1681 r_opt = strtoul(optarg, &tmp, 0);
1683 com_err(program_name, 0,
1684 _("bad revision level - %s"), optarg);
1687 fs_param.s_rev_level = r_opt;
1689 case 's': /* deprecated */
1690 s_opt = atoi(optarg);
1697 com_err(program_name, 0, "%s",
1698 _("The -t option may only be used once"));
1701 fs_type = strdup(optarg);
1705 com_err(program_name, 0, "%s",
1706 _("The -T option may only be used once"));
1709 usage_types = strdup(optarg);
1721 /* Print version number and exit */
1722 show_version_only++;
1728 if ((optind == argc) && !show_version_only)
1730 device_name = argv[optind++];
1732 if (!quiet || show_version_only)
1733 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1736 if (show_version_only) {
1737 fprintf(stderr, _("\tUsing %s\n"),
1738 error_message(EXT2_ET_BASE));
1743 * If there's no blocksize specified and there is a journal
1744 * device, use it to figure out the blocksize
1746 if (blocksize <= 0 && journal_device) {
1750 #ifdef CONFIG_TESTIO_DEBUG
1751 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1752 io_ptr = test_io_manager;
1753 test_io_backing_manager = unix_io_manager;
1756 io_ptr = unix_io_manager;
1757 retval = ext2fs_open(journal_device,
1758 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1761 com_err(program_name, retval,
1762 _("while trying to open journal device %s\n"),
1766 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1767 com_err(program_name, 0,
1768 _("Journal dev blocksize (%d) smaller than "
1769 "minimum blocksize %d\n"), jfs->blocksize,
1773 blocksize = jfs->blocksize;
1774 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1775 fs_param.s_log_block_size =
1776 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1780 if (optind < argc) {
1781 fs_blocks_count = parse_num_blocks2(argv[optind++],
1782 fs_param.s_log_block_size);
1783 if (!fs_blocks_count) {
1784 com_err(program_name, 0,
1785 _("invalid blocks '%s' on device '%s'"),
1786 argv[optind - 1], device_name);
1794 check_plausibility(device_name);
1795 check_mount(device_name, force, _("filesystem"));
1797 /* Determine the size of the device (if possible) */
1798 if (noaction && fs_blocks_count) {
1799 dev_size = fs_blocks_count;
1802 retval = ext2fs_get_device_size2(device_name,
1803 EXT2_BLOCK_SIZE(&fs_param),
1806 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1807 com_err(program_name, retval, "%s",
1808 _("while trying to determine filesystem size"));
1811 if (!fs_blocks_count) {
1812 if (retval == EXT2_ET_UNIMPLEMENTED) {
1813 com_err(program_name, 0, "%s",
1814 _("Couldn't determine device size; you "
1815 "must specify\nthe size of the "
1819 if (dev_size == 0) {
1820 com_err(program_name, 0, "%s",
1821 _("Device size reported to be zero. "
1822 "Invalid partition specified, or\n\t"
1823 "partition table wasn't reread "
1824 "after running fdisk, due to\n\t"
1825 "a modified partition being busy "
1826 "and in use. You may need to reboot\n\t"
1827 "to re-read your partition table.\n"
1831 fs_blocks_count = dev_size;
1832 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1833 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1834 EXT2_BLOCK_SIZE(&fs_param))-1));
1836 } else if (!force && (fs_blocks_count > dev_size)) {
1837 com_err(program_name, 0, "%s",
1838 _("Filesystem larger than apparent device size."));
1843 profile_get_string(profile, "devices", device_name,
1844 "fs_type", 0, &fs_type);
1846 profile_get_string(profile, "devices", device_name,
1847 "usage_types", 0, &usage_types);
1850 * We have the file system (or device) size, so we can now
1851 * determine the appropriate file system types so the fs can
1852 * be appropriately configured.
1854 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1855 fs_blocks_count ? fs_blocks_count : dev_size,
1858 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1862 /* Figure out what features should be enabled */
1865 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1866 tmp = get_string_from_profile(fs_types, "base_features",
1867 "sparse_super,filetype,resize_inode,dir_index");
1868 edit_feature(tmp, &fs_param.s_feature_compat);
1871 /* And which mount options as well */
1872 tmp = get_string_from_profile(fs_types, "default_mntopts",
1874 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1878 for (cpp = fs_types; *cpp; cpp++) {
1880 profile_get_string(profile, "fs_types", *cpp,
1881 "features", "", &tmp);
1883 edit_feature(tmp, &fs_param.s_feature_compat);
1887 tmp = get_string_from_profile(fs_types, "default_features",
1890 /* Mask off features which aren't supported by the Hurd */
1891 if (for_hurd(creator_os)) {
1892 fs_param.s_feature_incompat &= ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1893 fs_param.s_feature_ro_compat &=
1894 ~(EXT4_FEATURE_RO_COMPAT_HUGE_FILE |
1895 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1897 edit_feature(fs_features ? fs_features : tmp,
1898 &fs_param.s_feature_compat);
1902 * If the user specified features incompatible with the Hurd, complain
1904 if (for_hurd(creator_os)) {
1905 if (fs_param.s_feature_incompat &
1906 EXT2_FEATURE_INCOMPAT_FILETYPE) {
1907 fprintf(stderr, _("The HURD does not support the "
1908 "filetype feature.\n"));
1911 if (fs_param.s_feature_ro_compat &
1912 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
1913 fprintf(stderr, _("The HURD does not support the "
1914 "huge_file feature.\n"));
1917 if (fs_param.s_feature_ro_compat &
1918 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
1919 fprintf(stderr, _("The HURD does not support the "
1920 "metadata_csum feature.\n"));
1925 /* Get the hardware sector sizes, if available */
1926 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1928 com_err(program_name, retval, "%s",
1929 _("while trying to determine hardware sector size"));
1932 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1934 com_err(program_name, retval, "%s",
1935 _("while trying to determine physical sector size"));
1939 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1941 lsector_size = atoi(tmp);
1942 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1944 psector_size = atoi(tmp);
1946 /* Older kernels may not have physical/logical distinction */
1948 psector_size = lsector_size;
1950 if (blocksize <= 0) {
1951 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1953 if (use_bsize == -1) {
1954 use_bsize = sys_page_size;
1955 if (is_before_linux_ver(2, 6) && use_bsize > 4096)
1958 if (lsector_size && use_bsize < lsector_size)
1959 use_bsize = lsector_size;
1960 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1961 use_bsize = -blocksize;
1962 blocksize = use_bsize;
1963 fs_blocks_count /= (blocksize / 1024);
1965 if (blocksize < lsector_size) { /* Impossible */
1966 com_err(program_name, EINVAL, "%s",
1967 _("while setting blocksize; too small "
1970 } else if ((blocksize < psector_size) &&
1971 (psector_size <= sys_page_size)) { /* Suboptimal */
1972 fprintf(stderr, _("Warning: specified blocksize %d is "
1973 "less than device physical sectorsize %d\n"),
1974 blocksize, psector_size);
1978 fs_param.s_log_block_size =
1979 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1982 * We now need to do a sanity check of fs_blocks_count for
1983 * 32-bit vs 64-bit block number support.
1985 if ((fs_blocks_count > MAX_32_NUM) &&
1986 (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT))
1987 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1988 if ((fs_blocks_count > MAX_32_NUM) &&
1989 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1990 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1991 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1992 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1994 if ((fs_blocks_count > MAX_32_NUM) &&
1995 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1996 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1997 "too big to be expressed\n\t"
1998 "in 32 bits using a blocksize of %d.\n"),
1999 program_name, fs_blocks_count, device_name,
2000 EXT2_BLOCK_SIZE(&fs_param));
2004 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
2006 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2007 fs_types[0] = strdup("journal");
2012 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
2013 print_str_list(fs_types);
2016 if (r_opt == EXT2_GOOD_OLD_REV &&
2017 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
2018 fs_param.s_feature_ro_compat)) {
2019 fprintf(stderr, "%s", _("Filesystem features not supported "
2020 "with revision 0 filesystems\n"));
2025 if (r_opt == EXT2_GOOD_OLD_REV) {
2026 fprintf(stderr, "%s",
2027 _("Sparse superblocks not supported "
2028 "with revision 0 filesystems\n"));
2031 fs_param.s_feature_ro_compat |=
2032 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2033 } else if (s_opt == 0)
2034 fs_param.s_feature_ro_compat &=
2035 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2037 if (journal_size != 0) {
2038 if (r_opt == EXT2_GOOD_OLD_REV) {
2039 fprintf(stderr, "%s", _("Journals not supported with "
2040 "revision 0 filesystems\n"));
2043 fs_param.s_feature_compat |=
2044 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2047 /* Get reserved_ratio from profile if not specified on cmd line. */
2048 if (reserved_ratio < 0.0) {
2049 reserved_ratio = get_double_from_profile(
2050 fs_types, "reserved_ratio", 5.0);
2051 if (reserved_ratio > 50 || reserved_ratio < 0) {
2052 com_err(program_name, 0,
2053 _("invalid reserved blocks percent - %lf"),
2059 if (fs_param.s_feature_incompat &
2060 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2062 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
2063 fs_param.s_feature_compat = 0;
2064 fs_param.s_feature_ro_compat = 0;
2067 /* Check the user's mkfs options for 64bit */
2068 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
2069 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2070 printf("%s", _("Extents MUST be enabled for a 64-bit "
2071 "filesystem. Pass -O extents to rectify.\n"));
2075 /* Set first meta blockgroup via an environment variable */
2076 /* (this is mostly for debugging purposes) */
2077 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2078 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
2079 fs_param.s_first_meta_bg = atoi(tmp);
2080 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2082 cluster_size = get_int_from_profile(fs_types,
2085 fs_param.s_log_cluster_size =
2086 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
2087 if (fs_param.s_log_cluster_size &&
2088 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
2089 com_err(program_name, 0, "%s",
2090 _("The cluster size may not be "
2091 "smaller than the block size.\n"));
2094 } else if (cluster_size) {
2095 com_err(program_name, 0, "%s",
2096 _("specifying a cluster size requires the "
2097 "bigalloc feature"));
2100 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2102 if (inode_ratio == 0) {
2103 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2105 if (inode_ratio < blocksize)
2106 inode_ratio = blocksize;
2107 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2108 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2111 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2112 retval = get_device_geometry(device_name, &fs_param, psector_size);
2115 _("warning: Unable to get device geometry for %s\n"),
2117 } else if (retval) {
2118 printf(_("%s alignment is offset by %lu bytes.\n"),
2119 device_name, retval);
2120 printf(_("This may result in very poor performance, "
2121 "(re)-partitioning suggested.\n"));
2125 num_backups = get_int_from_profile(fs_types, "num_backup_sb", 2);
2127 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2130 * Initialize s_desc_size so that the parse_extended_opts()
2131 * can correctly handle "-E resize=NNN" if the 64-bit option
2134 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
2135 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2137 /* This check should happen beyond the last assignment to blocksize */
2138 if (blocksize > sys_page_size) {
2140 com_err(program_name, 0,
2141 _("%d-byte blocks too big for system (max %d)"),
2142 blocksize, sys_page_size);
2145 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2146 "(max %d), forced to continue\n"),
2147 blocksize, sys_page_size);
2150 lazy_itable_init = 0;
2151 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2152 lazy_itable_init = 1;
2154 lazy_itable_init = get_bool_from_profile(fs_types,
2157 discard = get_bool_from_profile(fs_types, "discard" , discard);
2158 journal_flags |= get_bool_from_profile(fs_types,
2159 "lazy_journal_init", 0) ?
2160 EXT2_MKJOURNAL_LAZYINIT : 0;
2161 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2163 if (!journal_location_string)
2164 journal_location_string = get_string_from_profile(fs_types,
2165 "journal_location", "");
2166 if ((journal_location == ~0ULL) && journal_location_string &&
2167 *journal_location_string)
2168 journal_location = parse_num_blocks2(journal_location_string,
2169 fs_param.s_log_block_size);
2170 free(journal_location_string);
2172 packed_meta_blocks = get_bool_from_profile(fs_types,
2173 "packed_meta_blocks", 0);
2174 if (packed_meta_blocks)
2175 journal_location = 0;
2177 /* Get options from profile */
2178 for (cpp = fs_types; *cpp; cpp++) {
2180 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2182 parse_extended_opts(&fs_param, tmp);
2187 parse_extended_opts(&fs_param, extended_opts);
2189 /* Don't allow user to set both metadata_csum and uninit_bg bits. */
2190 if ((fs_param.s_feature_ro_compat &
2191 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2192 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
2193 fs_param.s_feature_ro_compat &=
2194 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
2196 /* Can't support bigalloc feature without extents feature */
2197 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2198 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2199 com_err(program_name, 0, "%s",
2200 _("Can't support bigalloc feature without "
2201 "extents feature"));
2205 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2206 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2207 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2208 "features are not compatible.\n"
2209 "They can not be both enabled "
2210 "simultaneously.\n"));
2215 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2216 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2217 "still under development\n"
2218 "See https://ext4.wiki.kernel.org/"
2219 "index.php/Bigalloc for more information\n\n"));
2222 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2223 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2224 "still under development\n"
2225 "See https://ext4.wiki.kernel.org/"
2226 "index.php/Quota for more information\n\n"));
2229 * Since sparse_super is the default, we would only have a problem
2230 * here if it was explicitly disabled.
2232 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2233 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2234 com_err(program_name, 0, "%s",
2235 _("reserved online resize blocks not supported "
2236 "on non-sparse filesystem"));
2240 if (fs_param.s_blocks_per_group) {
2241 if (fs_param.s_blocks_per_group < 256 ||
2242 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2243 com_err(program_name, 0, "%s",
2244 _("blocks per group count out of range"));
2250 * If the bigalloc feature is enabled, then the -g option will
2251 * specify the number of clusters per group.
2253 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2254 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2255 fs_param.s_blocks_per_group = 0;
2258 if (inode_size == 0)
2259 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2260 if (!flex_bg_size && (fs_param.s_feature_incompat &
2261 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2262 flex_bg_size = get_int_from_profile(fs_types,
2263 "flex_bg_size", 16);
2265 if (!(fs_param.s_feature_incompat &
2266 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2267 com_err(program_name, 0, "%s",
2268 _("Flex_bg feature not enabled, so "
2269 "flex_bg size may not be specified"));
2272 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2275 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2276 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2277 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2278 inode_size & (inode_size - 1)) {
2279 com_err(program_name, 0,
2280 _("invalid inode size %d (min %d/max %d)"),
2281 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2286 * If inode size is 128 and inline data is enabled, we need
2287 * to notify users that inline data will never be useful.
2289 if ((fs_param.s_feature_incompat &
2290 EXT4_FEATURE_INCOMPAT_INLINE_DATA) &&
2291 inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
2292 com_err(program_name, 0,
2293 _("inode size is %d, inline data is useless"),
2297 fs_param.s_inode_size = inode_size;
2300 /* Make sure number of inodes specified will fit in 32 bits */
2301 if (num_inodes == 0) {
2302 unsigned long long n;
2303 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2304 if (n > MAX_32_NUM) {
2305 if (fs_param.s_feature_incompat &
2306 EXT4_FEATURE_INCOMPAT_64BIT)
2307 num_inodes = MAX_32_NUM;
2309 com_err(program_name, 0,
2310 _("too many inodes (%llu), raise "
2311 "inode ratio?"), n);
2315 } else if (num_inodes > MAX_32_NUM) {
2316 com_err(program_name, 0,
2317 _("too many inodes (%llu), specify < 2^32 inodes"),
2322 * Calculate number of inodes based on the inode ratio
2324 fs_param.s_inodes_count = num_inodes ? num_inodes :
2325 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2327 if ((((unsigned long long)fs_param.s_inodes_count) *
2328 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2329 ((ext2fs_blocks_count(&fs_param)) *
2330 EXT2_BLOCK_SIZE(&fs_param))) {
2331 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2332 "(%u) too big for a\n\t"
2333 "filesystem with %llu blocks, "
2334 "specify higher inode_ratio (-i)\n\t"
2335 "or lower inode count (-N).\n"),
2336 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2337 fs_param.s_inodes_count,
2338 (unsigned long long) ext2fs_blocks_count(&fs_param));
2343 * Calculate number of blocks to reserve
2345 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2346 ext2fs_blocks_count(&fs_param) / 100.0);
2348 if (fs_param.s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
2349 if (num_backups >= 1)
2350 fs_param.s_backup_bgs[0] = 1;
2351 if (num_backups >= 2)
2352 fs_param.s_backup_bgs[1] = ~0;
2359 static int should_do_undo(const char *name)
2364 struct ext2_super_block super;
2365 io_manager manager = unix_io_manager;
2366 int csum_flag, force_undo;
2368 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2369 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
2370 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
2371 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2372 if (!force_undo && (!csum_flag || !lazy_itable_init))
2375 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2378 * We don't handle error cases instead we
2379 * declare that the file system doesn't exist
2380 * and let the rest of mke2fs take care of
2387 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2388 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2394 #if defined(WORDS_BIGENDIAN)
2395 s_magic = ext2fs_swab16(super.s_magic);
2397 s_magic = super.s_magic;
2400 if (s_magic == EXT2_SUPER_MAGIC)
2404 io_channel_close(channel);
2411 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2413 errcode_t retval = ENOMEM;
2414 char *tdb_dir = NULL, *tdb_file = NULL;
2415 char *dev_name, *tmp_name;
2416 int free_tdb_dir = 0;
2419 * Configuration via a conf file would be
2422 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2424 profile_get_string(profile, "defaults",
2425 "undo_dir", 0, "/var/lib/e2fsprogs",
2430 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2431 access(tdb_dir, W_OK)) {
2437 tmp_name = strdup(name);
2440 dev_name = basename(tmp_name);
2441 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2446 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2449 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2454 set_undo_io_backing_manager(*io_ptr);
2455 *io_ptr = undo_io_manager;
2456 retval = set_undo_io_backup_file(tdb_file);
2459 printf(_("Overwriting existing filesystem; this can be undone "
2460 "using the command:\n"
2461 " e2undo %s %s\n\n"), tdb_file, name);
2472 com_err(program_name, retval, "%s",
2473 _("while trying to setup undo file\n"));
2477 static int mke2fs_discard_device(ext2_filsys fs)
2479 struct ext2fs_numeric_progress_struct progress;
2480 blk64_t blocks = ext2fs_blocks_count(fs->super);
2481 blk64_t count = DISCARD_STEP_MB;
2486 * Let's try if discard really works on the device, so
2487 * we do not print numeric progress resulting in failure
2490 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2493 cur = fs->blocksize;
2495 count *= (1024 * 1024);
2496 count /= fs->blocksize;
2498 ext2fs_numeric_progress_init(fs, &progress,
2499 _("Discarding device blocks: "),
2501 while (cur < blocks) {
2502 ext2fs_numeric_progress_update(fs, &progress, cur);
2504 if (cur + count > blocks)
2505 count = blocks - cur;
2507 retval = io_channel_discard(fs->io, cur, count);
2514 ext2fs_numeric_progress_close(fs, &progress,
2517 printf("%s\n",error_message(retval));
2519 ext2fs_numeric_progress_close(fs, &progress,
2525 static void fix_cluster_bg_counts(ext2_filsys fs)
2527 blk64_t block, num_blocks, last_block, next;
2528 blk64_t tot_free = 0;
2533 num_blocks = ext2fs_blocks_count(fs->super);
2534 last_block = ext2fs_group_last_block2(fs, group);
2535 block = fs->super->s_first_data_block;
2536 while (block < num_blocks) {
2537 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
2538 block, last_block, &next);
2542 block = last_block + 1;
2546 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
2547 block, last_block, &next);
2549 next = last_block + 1;
2550 grp_free += EXT2FS_NUM_B2C(fs, next - block);
2551 tot_free += next - block;
2554 if (block > last_block) {
2556 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2557 ext2fs_group_desc_csum_set(fs, group);
2560 last_block = ext2fs_group_last_block2(fs, group);
2563 ext2fs_free_blocks_count_set(fs->super, tot_free);
2566 static int create_quota_inodes(ext2_filsys fs)
2570 quota_init_context(&qctx, fs, -1);
2571 quota_compute_usage(qctx);
2572 quota_write_inode(qctx, quotatype);
2573 quota_release_context(&qctx);
2578 int main (int argc, char *argv[])
2580 errcode_t retval = 0;
2582 badblocks_list bb_list = 0;
2583 unsigned int journal_blocks;
2584 unsigned int i, checkinterval;
2590 char opt_string[40];
2592 int itable_zeroed = 0;
2595 setlocale(LC_MESSAGES, "");
2596 setlocale(LC_CTYPE, "");
2597 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2598 textdomain(NLS_CAT_NAME);
2599 set_com_err_gettext(gettext);
2603 #ifdef CONFIG_TESTIO_DEBUG
2604 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2605 io_ptr = test_io_manager;
2606 test_io_backing_manager = unix_io_manager;
2609 io_ptr = unix_io_manager;
2611 if (should_do_undo(device_name)) {
2612 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2618 * Initialize the superblock....
2620 flags = EXT2_FLAG_EXCLUSIVE;
2622 flags |= EXT2_FLAG_DIRECT_IO;
2623 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2626 flags |= EXT2_FLAG_64BITS;
2628 * By default, we print how many inode tables or block groups
2629 * or whatever we've written so far. The quiet flag disables
2630 * this, along with a lot of other output.
2633 flags |= EXT2_FLAG_PRINT_PROGRESS;
2634 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2636 com_err(device_name, retval, "%s",
2637 _("while setting up superblock"));
2640 fs->progress_ops = &ext2fs_numeric_progress_ops;
2642 /* Check the user's mkfs options for metadata checksumming */
2644 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2645 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
2646 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2647 EXT3_FEATURE_INCOMPAT_EXTENTS))
2648 printf(_("Extents are not enabled. The file extent "
2649 "tree can be checksummed, whereas block maps "
2650 "cannot. Not enabling extents reduces the "
2651 "coverage of metadata checksumming. "
2652 "Pass -O extents to rectify.\n"));
2653 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2654 EXT4_FEATURE_INCOMPAT_64BIT))
2655 printf(_("64-bit filesystem support is not "
2656 "enabled. The larger fields afforded by "
2657 "this feature enable full-strength "
2658 "checksumming. Pass -O 64bit to rectify.\n"));
2661 /* Calculate journal blocks */
2662 if (!journal_device && ((journal_size) ||
2663 (fs_param.s_feature_compat &
2664 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2665 journal_blocks = figure_journal_size(journal_size, fs);
2667 /* Can't undo discard ... */
2668 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2669 retval = mke2fs_discard_device(fs);
2670 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2673 _("Discard succeeded and will return "
2674 "0s - skipping inode table wipe\n"));
2675 lazy_itable_init = 1;
2681 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2682 32768 : fs->blocksize * 8);
2683 io_channel_set_options(fs->io, opt_string);
2685 sprintf(opt_string, "offset=%llu", offset);
2686 io_channel_set_options(fs->io, opt_string);
2689 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2690 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2692 if ((fs_param.s_feature_incompat &
2693 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2694 (fs_param.s_feature_ro_compat &
2695 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2696 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2697 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
2698 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2699 fs->super->s_kbytes_written = 1;
2702 * Wipe out the old on-disk superblock
2705 zap_sector(fs, 2, 6);
2708 * Parse or generate a UUID for the filesystem
2711 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2712 com_err(device_name, 0, "could not parse UUID: %s\n",
2717 uuid_generate(fs->super->s_uuid);
2718 ext2fs_init_csum_seed(fs);
2721 * Initialize the directory index variables
2723 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2725 hash_alg = e2p_string2hash(hash_alg_str);
2727 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2729 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2732 * Periodic checks can be enabled/disabled via config file.
2733 * Note we override the kernel include file's idea of what the default
2734 * check interval (never) should be. It's a good idea to check at
2735 * least *occasionally*, specially since servers will never rarely get
2736 * to reboot, since Linux is so robust these days. :-)
2738 * 180 days (six months) seems like a good value.
2740 #ifdef EXT2_DFL_CHECKINTERVAL
2741 #undef EXT2_DFL_CHECKINTERVAL
2743 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2745 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2746 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2747 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2749 * Add "jitter" to the superblock's check interval so that we
2750 * don't check all the filesystems at the same time. We use a
2751 * kludgy hack of using the UUID to derive a random jitter value
2753 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2754 val += fs->super->s_uuid[i];
2755 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2757 fs->super->s_max_mnt_count = -1;
2760 * Override the creator OS, if applicable
2762 if (creator_os && !set_os(fs->super, creator_os)) {
2763 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2768 * For the Hurd, we will turn off filetype since it doesn't
2771 if (fs->super->s_creator_os == EXT2_OS_HURD)
2772 fs->super->s_feature_incompat &=
2773 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2776 * Set the volume label...
2779 memset(fs->super->s_volume_name, 0,
2780 sizeof(fs->super->s_volume_name));
2781 strncpy(fs->super->s_volume_name, volume_label,
2782 sizeof(fs->super->s_volume_name));
2786 * Set the last mount directory
2789 memset(fs->super->s_last_mounted, 0,
2790 sizeof(fs->super->s_last_mounted));
2791 strncpy(fs->super->s_last_mounted, mount_dir,
2792 sizeof(fs->super->s_last_mounted));
2795 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2796 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2797 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
2799 if (!quiet || noaction)
2805 if (fs->super->s_feature_incompat &
2806 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2807 create_journal_dev(fs);
2808 exit(ext2fs_close(fs) ? 1 : 0);
2811 if (bad_blocks_filename)
2812 read_bb_file(fs, &bb_list, bad_blocks_filename);
2814 test_disk(fs, &bb_list);
2815 handle_bad_blocks(fs, bb_list);
2817 fs->stride = fs_stride = fs->super->s_raid_stride;
2819 printf("%s", _("Allocating group tables: "));
2820 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
2822 retval = packed_allocate_tables(fs);
2824 retval = ext2fs_allocate_tables(fs);
2826 com_err(program_name, retval, "%s",
2827 _("while trying to allocate filesystem tables"));
2831 printf("%s", _("done \n"));
2833 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2835 com_err(program_name, retval, "%s",
2836 _("\n\twhile converting subcluster bitmap"));
2841 fs->super->s_state |= EXT2_ERROR_FS;
2842 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2844 * The command "mke2fs -S" is used to recover
2845 * corrupted file systems, so do not mark any of the
2846 * inodes as unused; we want e2fsck to consider all
2847 * inodes as potentially containing recoverable data.
2849 if (ext2fs_has_group_desc_csum(fs)) {
2850 for (i = 0; i < fs->group_desc_count; i++)
2851 ext2fs_bg_itable_unused_set(fs, i, 0);
2854 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2855 blk64_t rsv = 65536 / fs->blocksize;
2856 blk64_t blocks = ext2fs_blocks_count(fs->super);
2860 #ifdef ZAP_BOOTBLOCK
2861 zap_sector(fs, 0, 2);
2865 * Wipe out any old MD RAID (or other) metadata at the end
2866 * of the device. This will also verify that the device is
2867 * as large as we think. Be careful with very small devices.
2869 start = (blocks & ~(rsv - 1));
2873 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2877 com_err(program_name, retval,
2878 _("while zeroing block %llu at end of filesystem"),
2881 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2882 create_root_dir(fs);
2883 create_lost_and_found(fs);
2885 create_bad_block_inode(fs, bb_list);
2886 if (fs->super->s_feature_compat &
2887 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2888 retval = ext2fs_create_resize_inode(fs);
2890 com_err("ext2fs_create_resize_inode", retval,
2892 _("while reserving blocks for online resize"));
2898 if (journal_device) {
2902 check_plausibility(journal_device);
2903 check_mount(journal_device, force, _("journal"));
2905 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2906 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2907 fs->blocksize, unix_io_manager, &jfs);
2909 com_err(program_name, retval,
2910 _("while trying to open journal device %s\n"),
2915 printf(_("Adding journal to device %s: "),
2919 retval = ext2fs_add_journal_device(fs, jfs);
2921 com_err (program_name, retval,
2922 _("\n\twhile trying to add journal to device %s"),
2927 printf("%s", _("done\n"));
2929 free(journal_device);
2930 } else if ((journal_size) ||
2931 (fs_param.s_feature_compat &
2932 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2934 printf("%s", _("Skipping journal creation in super-only mode\n"));
2935 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2939 if (!journal_blocks) {
2940 fs->super->s_feature_compat &=
2941 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2945 printf(_("Creating journal (%u blocks): "),
2949 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
2953 com_err(program_name, retval, "%s",
2954 _("\n\twhile trying to create journal"));
2958 printf("%s", _("done\n"));
2962 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2963 retval = ext2fs_mmp_init(fs);
2965 fprintf(stderr, "%s",
2966 _("\nError while enabling multiple "
2967 "mount protection feature."));
2971 printf(_("Multiple mount protection is enabled "
2972 "with update interval %d seconds.\n"),
2973 fs->super->s_mmp_update_interval);
2976 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2977 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2978 fix_cluster_bg_counts(fs);
2979 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2980 EXT4_FEATURE_RO_COMPAT_QUOTA))
2981 create_quota_inodes(fs);
2983 retval = mk_hugefiles(fs);
2985 com_err(program_name, retval, "while creating huge files");
2986 /* Copy files from the specified directory */
2989 printf("%s", _("Copying files into the device: "));
2992 * Allocate memory for the hardlinks, we don't need free()
2993 * since the lifespan will be over after the fs populated.
2995 if ((hdlinks.hdl = (struct hdlink_s *)
2996 malloc(hdlink_cnt * sizeof(struct hdlink_s))) == NULL) {
2997 fprintf(stderr, "%s", _("\nNot enough memory\n"));
2998 retval = ext2fs_close(fs);
3004 root = EXT2_ROOT_INO;
3005 retval = populate_fs(root, root_dir);
3007 fprintf(stderr, "%s",
3008 _("\nError while populating file system"));
3010 printf("%s", _("done\n"));
3014 printf("%s", _("Writing superblocks and "
3015 "filesystem accounting information: "));
3016 checkinterval = fs->super->s_checkinterval;
3017 max_mnt_count = fs->super->s_max_mnt_count;
3018 retval = ext2fs_close(fs);
3020 fprintf(stderr, "%s",
3021 _("\nWarning, had trouble writing out superblocks."));
3022 } else if (!quiet) {
3023 printf("%s", _("done\n\n"));
3024 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
3025 print_check_message(max_mnt_count, checkinterval);
3028 remove_error_table(&et_ext2_error_table);
3029 remove_error_table(&et_prof_error_table);
3030 profile_release(profile);
3031 for (i=0; fs_types[i]; i++)