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
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
47 #include <blkid/blkid.h>
49 #include "ext2fs/ext2_fs.h"
50 #include "ext2fs/ext2fsP.h"
51 #include "uuid/uuid.h"
53 #include "support/nls-enable.h"
54 #include "support/plausible.h"
55 #include "support/profile.h"
56 #include "support/prof_err.h"
57 #include "../version.h"
58 #include "support/quotaio.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;
93 static int lazy_itable_init;
94 static int packed_meta_blocks;
96 static char *bad_blocks_filename = NULL;
97 static __u32 fs_stride;
98 /* Initialize usr/grp quotas by default */
99 static unsigned int quotatype_bits = (QUOTA_USR_BIT | QUOTA_GRP_BIT);
101 static blk64_t journal_location = ~0LL;
102 static int proceed_delay = -1;
103 static blk64_t dev_size;
105 static struct ext2_super_block fs_param;
106 static __u32 zero_buf[4];
107 static char *fs_uuid = NULL;
108 static char *creator_os;
109 static char *volume_label;
110 static char *mount_dir;
111 char *journal_device;
112 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
114 const char *src_root_dir; /* Copy files from the specified directory */
115 static char *undo_file;
117 static int android_sparse_file; /* -E android_sparse */
119 static profile_t profile;
121 static int sys_page_size = 4096;
123 static int errors_behavior = 0;
125 static void usage(void)
127 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
128 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
129 "[-J journal-options]\n"
130 "\t[-G flex-group-size] [-N number-of-inodes] "
131 "[-d root-directory]\n"
132 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
133 "\t[-g blocks-per-group] [-L volume-label] "
134 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
135 "[-r fs-revision] [-E extended-option[,...]]\n"
136 "\t[-t fs-type] [-T usage-type ] [-U UUID] [-e errors_behavior]"
138 "\t[-jnqvDFSV] device [blocks-count]\n"),
143 static int int_log2(unsigned long long arg)
155 int int_log10(unsigned long long arg)
165 * Helper function for read_bb_file and test_disk
167 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
169 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
174 * Reads the bad blocks list from a file
176 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
177 const char *bad_blocks_file)
182 f = fopen(bad_blocks_file, "r");
184 com_err("read_bad_blocks_file", errno,
185 _("while trying to open %s"), bad_blocks_file);
188 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
191 com_err("ext2fs_read_bb_FILE", retval, "%s",
192 _("while reading in list of bad blocks from file"));
198 * Runs the badblocks program to test the disk
200 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
206 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
207 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
209 (unsigned long long) ext2fs_blocks_count(fs->super)-1);
211 printf(_("Running command: %s\n"), buf);
214 com_err("popen", errno,
215 _("while trying to run '%s'"), buf);
218 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
221 com_err("ext2fs_read_bb_FILE", retval, "%s",
222 _("while processing list of bad blocks from program"));
227 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
231 unsigned must_be_good;
233 badblocks_iterate bb_iter;
243 * The primary superblock and group descriptors *must* be
244 * good; if not, abort.
246 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
247 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
248 if (ext2fs_badblocks_list_test(bb_list, i)) {
249 fprintf(stderr, _("Block %d in primary "
250 "superblock/group descriptor area bad.\n"), i);
251 fprintf(stderr, _("Blocks %u through %u must be good "
252 "in order to build a filesystem.\n"),
253 fs->super->s_first_data_block, must_be_good);
254 fputs(_("Aborting....\n"), stderr);
260 * See if any of the bad blocks are showing up in the backup
261 * superblocks and/or group descriptors. If so, issue a
262 * warning and adjust the block counts appropriately.
264 group_block = fs->super->s_first_data_block +
265 fs->super->s_blocks_per_group;
267 for (i = 1; i < fs->group_desc_count; i++) {
269 for (j=0; j < fs->desc_blocks+1; j++) {
270 if (ext2fs_badblocks_list_test(bb_list,
274 _("Warning: the backup superblock/group descriptors at block %u contain\n"
278 group = ext2fs_group_of_blk2(fs, group_block+j);
279 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
280 ext2fs_group_desc_csum_set(fs, group);
281 ext2fs_free_blocks_count_add(fs->super, 1);
284 group_block += fs->super->s_blocks_per_group;
288 * Mark all the bad blocks as used...
290 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
292 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
293 _("while marking bad blocks as used"));
296 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
297 ext2fs_mark_block_bitmap2(fs->block_map, blk);
298 ext2fs_badblocks_list_iterate_end(bb_iter);
301 static void write_reserved_inodes(ext2_filsys fs)
305 struct ext2_inode *inode;
307 retval = ext2fs_get_memzero(EXT2_INODE_SIZE(fs->super), &inode);
309 com_err("inode_init", retval, _("while allocating memory"));
313 for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++) {
314 retval = ext2fs_write_inode_full(fs, ino, inode,
315 EXT2_INODE_SIZE(fs->super));
317 com_err("ext2fs_write_inode_full", retval,
318 _("while writing reserved inodes"));
323 ext2fs_free_mem(&inode);
326 static errcode_t packed_allocate_tables(ext2_filsys fs)
332 for (i = 0; i < fs->group_desc_count; i++) {
333 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
336 ext2fs_block_alloc_stats2(fs, goal, +1);
337 ext2fs_block_bitmap_loc_set(fs, i, goal);
339 for (i = 0; i < fs->group_desc_count; i++) {
340 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
343 ext2fs_block_alloc_stats2(fs, goal, +1);
344 ext2fs_inode_bitmap_loc_set(fs, i, goal);
346 for (i = 0; i < fs->group_desc_count; i++) {
347 blk64_t end = ext2fs_blocks_count(fs->super) - 1;
348 retval = ext2fs_get_free_blocks2(fs, goal, end,
349 fs->inode_blocks_per_group,
350 fs->block_map, &goal);
353 ext2fs_block_alloc_stats_range(fs, goal,
354 fs->inode_blocks_per_group, +1);
355 ext2fs_inode_table_loc_set(fs, i, goal);
356 ext2fs_group_desc_csum_set(fs, i);
361 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
367 struct ext2fs_numeric_progress_struct progress;
369 ext2fs_numeric_progress_init(fs, &progress,
370 _("Writing inode tables: "),
371 fs->group_desc_count);
373 for (i = 0; i < fs->group_desc_count; i++) {
374 ext2fs_numeric_progress_update(fs, &progress, i);
376 blk = ext2fs_inode_table_loc(fs, i);
377 num = fs->inode_blocks_per_group;
380 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
381 ext2fs_bg_itable_unused(fs, i)) *
382 EXT2_INODE_SIZE(fs->super),
383 EXT2_BLOCK_SIZE(fs->super));
384 if (!lazy_flag || itable_zeroed) {
385 /* The kernel doesn't need to zero the itable blocks */
386 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
387 ext2fs_group_desc_csum_set(fs, i);
389 if (!itable_zeroed) {
390 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
392 fprintf(stderr, _("\nCould not write %d "
393 "blocks in inode table starting at %llu: %s\n"),
394 num, (unsigned long long) blk,
395 error_message(retval));
400 if (sync_kludge == 1)
401 io_channel_flush(fs->io);
402 else if ((i % sync_kludge) == 0)
403 io_channel_flush(fs->io);
406 ext2fs_numeric_progress_close(fs, &progress,
409 /* Reserved inodes must always have correct checksums */
410 if (ext2fs_has_feature_metadata_csum(fs->super))
411 write_reserved_inodes(fs);
414 static void create_root_dir(ext2_filsys fs)
417 struct ext2_inode inode;
419 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
421 com_err("ext2fs_mkdir", retval, "%s",
422 _("while creating root dir"));
425 if (root_uid != 0 || root_gid != 0) {
426 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
428 com_err("ext2fs_read_inode", retval, "%s",
429 _("while reading root inode"));
433 inode.i_uid = root_uid;
434 ext2fs_set_i_uid_high(inode, root_uid >> 16);
435 inode.i_gid = root_gid;
436 ext2fs_set_i_gid_high(inode, root_gid >> 16);
438 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
440 com_err("ext2fs_write_inode", retval, "%s",
441 _("while setting root inode ownership"));
447 static void create_lost_and_found(ext2_filsys fs)
449 unsigned int lpf_size = 0;
452 const char *name = "lost+found";
456 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
458 com_err("ext2fs_mkdir", retval, "%s",
459 _("while creating /lost+found"));
463 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
465 com_err("ext2_lookup", retval, "%s",
466 _("while looking up /lost+found"));
470 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
471 /* Ensure that lost+found is at least 2 blocks, so we always
472 * test large empty blocks for big-block filesystems. */
473 if ((lpf_size += fs->blocksize) >= 16*1024 &&
474 lpf_size >= 2 * fs->blocksize)
476 retval = ext2fs_expand_dir(fs, ino);
478 com_err("ext2fs_expand_dir", retval, "%s",
479 _("while expanding /lost+found"));
485 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
489 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
490 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
491 retval = ext2fs_update_bb_inode(fs, bb_list);
493 com_err("ext2fs_update_bb_inode", retval, "%s",
494 _("while setting bad block inode"));
500 static void reserve_inodes(ext2_filsys fs)
504 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
505 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
506 ext2fs_mark_ib_dirty(fs);
509 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
510 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
511 #define BSD_LABEL_OFFSET 64
513 static void zap_sector(ext2_filsys fs, int sect, int nsect)
519 buf = calloc(512, nsect);
521 printf(_("Out of memory erasing sectors %d-%d\n"),
522 sect, sect + nsect - 1);
527 /* Check for a BSD disklabel, and don't erase it if so */
528 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
531 _("Warning: could not read block 0: %s\n"),
532 error_message(retval));
534 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
535 if ((*magic == BSD_DISKMAGIC) ||
536 (*magic == BSD_MAGICDISK))
541 memset(buf, 0, 512*nsect);
542 io_channel_set_blksize(fs->io, 512);
543 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
544 io_channel_set_blksize(fs->io, fs->blocksize);
547 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
548 sect, error_message(retval));
551 static void create_journal_dev(ext2_filsys fs)
553 struct ext2fs_numeric_progress_struct progress;
556 blk64_t blk, err_blk;
557 int c, count, err_count;
558 struct ext2fs_journal_params jparams;
560 retval = ext2fs_get_journal_params(&jparams, fs);
562 com_err("create_journal_dev", retval, "%s",
563 _("while splitting the journal size"));
567 retval = ext2fs_create_journal_superblock2(fs, &jparams, 0, &buf);
569 com_err("create_journal_dev", retval, "%s",
570 _("while initializing journal superblock"));
574 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
575 goto write_superblock;
577 ext2fs_numeric_progress_init(fs, &progress,
578 _("Zeroing journal device: "),
579 ext2fs_blocks_count(fs->super));
581 count = ext2fs_blocks_count(fs->super);
587 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
589 com_err("create_journal_dev", retval,
590 _("while zeroing journal device "
591 "(block %llu, count %d)"),
592 (unsigned long long) err_blk, err_count);
597 ext2fs_numeric_progress_update(fs, &progress, blk);
600 ext2fs_numeric_progress_close(fs, &progress, NULL);
602 retval = io_channel_write_blk64(fs->io,
603 fs->super->s_first_data_block+1,
605 (void) ext2fs_free_mem(&buf);
607 com_err("create_journal_dev", retval, "%s",
608 _("while writing journal superblock"));
613 static void show_stats(ext2_filsys fs)
615 struct ext2_super_block *s = fs->super;
622 printf(_("Creating filesystem with %llu %dk blocks and "
624 (unsigned long long) ext2fs_blocks_count(s),
625 fs->blocksize >> 10, s->s_inodes_count);
629 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
630 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
631 (unsigned long long) (ext2fs_blocks_count(&fs_param) -
632 ext2fs_blocks_count(s)));
634 printf(_("Filesystem label=%.*s\n"), EXT2_LEN_STR(s->s_volume_name));
636 os = e2p_os2string(fs->super->s_creator_os);
638 printf(_("OS type: %s\n"), os);
640 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
641 s->s_log_block_size);
642 if (ext2fs_has_feature_bigalloc(fs->super))
643 printf(_("Cluster size=%u (log=%u)\n"),
644 fs->blocksize << fs->cluster_ratio_bits,
645 s->s_log_cluster_size);
647 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
648 s->s_log_cluster_size);
649 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
650 s->s_raid_stride, s->s_raid_stripe_width);
651 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
652 (unsigned long long) ext2fs_blocks_count(s));
653 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
654 (unsigned long long) ext2fs_r_blocks_count(s),
655 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
656 printf(_("First data block=%u\n"), s->s_first_data_block);
657 if (root_uid != 0 || root_gid != 0)
658 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
659 if (s->s_reserved_gdt_blocks)
660 printf(_("Maximum filesystem blocks=%lu\n"),
661 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
662 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
663 if (fs->group_desc_count > 1)
664 printf(_("%u block groups\n"), fs->group_desc_count);
666 printf(_("%u block group\n"), fs->group_desc_count);
667 if (ext2fs_has_feature_bigalloc(fs->super))
668 printf(_("%u blocks per group, %u clusters per group\n"),
669 s->s_blocks_per_group, s->s_clusters_per_group);
671 printf(_("%u blocks per group, %u fragments per group\n"),
672 s->s_blocks_per_group, s->s_clusters_per_group);
673 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
676 if (fs->group_desc_count == 1) {
681 if (!e2p_is_null_uuid(s->s_uuid))
682 printf(_("Filesystem UUID: %s\n"), e2p_uuid2str(s->s_uuid));
683 printf("%s", _("Superblock backups stored on blocks: "));
684 group_block = s->s_first_data_block;
686 for (i = 1; i < fs->group_desc_count; i++) {
687 group_block += s->s_blocks_per_group;
688 if (!ext2fs_bg_has_super(fs, i))
692 need = int_log10(group_block) + 2;
693 if (need > col_left) {
698 printf("%llu", (unsigned long long) group_block);
704 * Returns true if making a file system for the Hurd, else 0
706 static int for_hurd(const char *os)
716 return (atoi(os) == EXT2_OS_HURD);
717 return (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0);
721 * Set the S_CREATOR_OS field. Return true if OS is known,
724 static int set_os(struct ext2_super_block *sb, char *os)
727 sb->s_creator_os = atoi (os);
728 else if (strcasecmp(os, "linux") == 0)
729 sb->s_creator_os = EXT2_OS_LINUX;
730 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
731 sb->s_creator_os = EXT2_OS_HURD;
732 else if (strcasecmp(os, "freebsd") == 0)
733 sb->s_creator_os = EXT2_OS_FREEBSD;
734 else if (strcasecmp(os, "lites") == 0)
735 sb->s_creator_os = EXT2_OS_LITES;
741 #define PATH_SET "PATH=/sbin"
743 static void parse_extended_opts(struct ext2_super_block *param,
746 char *buf, *token, *next, *p, *arg, *badopt = 0;
751 char *encoding_flags = NULL;
756 fprintf(stderr, "%s",
757 _("Couldn't allocate memory to parse options!\n"));
761 for (token = buf; token && *token; token = next) {
762 p = strchr(token, ',');
768 arg = strchr(token, '=');
773 if (strcmp(token, "desc-size") == 0 ||
774 strcmp(token, "desc_size") == 0) {
777 if (!ext2fs_has_feature_64bit(&fs_param)) {
779 _("%s requires '-O 64bit'\n"), token);
783 if (param->s_reserved_gdt_blocks != 0) {
785 _("'%s' must be before 'resize=%u'\n"),
786 token, param->s_reserved_gdt_blocks);
795 desc_size = strtoul(arg, &p, 0);
796 if (*p || (desc_size & (desc_size - 1))) {
798 _("Invalid desc_size: '%s'\n"), arg);
802 param->s_desc_size = desc_size;
803 } else if (strcmp(token, "hash_seed") == 0) {
810 (unsigned char *)param->s_hash_seed) != 0) {
812 _("Invalid hash seed: %s\n"), arg);
816 } else if (strcmp(token, "offset") == 0) {
822 offset = strtoull(arg, &p, 0);
824 fprintf(stderr, _("Invalid offset: %s\n"),
829 } else if (strcmp(token, "mmp_update_interval") == 0) {
835 param->s_mmp_update_interval = strtoul(arg, &p, 0);
838 _("Invalid mmp_update_interval: %s\n"),
843 } else if (strcmp(token, "no_copy_xattrs") == 0) {
846 } else if (strcmp(token, "num_backup_sb") == 0) {
852 num_backups = strtoul(arg, &p, 0);
853 if (*p || num_backups > 2) {
855 _("Invalid # of backup "
856 "superblocks: %s\n"),
861 } else if (strcmp(token, "packed_meta_blocks") == 0) {
863 packed_meta_blocks = strtoul(arg, &p, 0);
865 packed_meta_blocks = 1;
866 if (packed_meta_blocks)
867 journal_location = 0;
868 } else if (strcmp(token, "stride") == 0) {
874 param->s_raid_stride = strtoul(arg, &p, 0);
877 _("Invalid stride parameter: %s\n"),
882 } else if (strcmp(token, "stripe-width") == 0 ||
883 strcmp(token, "stripe_width") == 0) {
889 param->s_raid_stripe_width = strtoul(arg, &p, 0);
892 _("Invalid stripe-width parameter: %s\n"),
897 } else if (!strcmp(token, "resize")) {
899 unsigned long bpg, rsv_groups;
900 unsigned long group_desc_count, desc_blocks;
901 unsigned int gdpb, blocksize;
910 resize = parse_num_blocks2(arg,
911 param->s_log_block_size);
915 _("Invalid resize parameter: %s\n"),
920 if (resize <= ext2fs_blocks_count(param)) {
921 fprintf(stderr, "%s",
922 _("The resize maximum must be greater "
923 "than the filesystem size.\n"));
928 blocksize = EXT2_BLOCK_SIZE(param);
929 bpg = param->s_blocks_per_group;
932 gdpb = EXT2_DESC_PER_BLOCK(param);
933 group_desc_count = (__u32) ext2fs_div64_ceil(
934 ext2fs_blocks_count(param), bpg);
935 desc_blocks = (group_desc_count +
937 rsv_groups = ext2fs_div64_ceil(resize, bpg);
938 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
940 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
941 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
944 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
945 fprintf(stderr, "%s",
946 _("On-line resizing not supported with revision 0 filesystems\n"));
950 ext2fs_set_feature_resize_inode(param);
952 param->s_reserved_gdt_blocks = rsv_gdb;
954 } else if (!strcmp(token, "test_fs")) {
955 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
956 } else if (!strcmp(token, "lazy_itable_init")) {
958 lazy_itable_init = strtoul(arg, &p, 0);
960 lazy_itable_init = 1;
961 } else if (!strcmp(token, "lazy_journal_init")) {
963 journal_flags |= strtoul(arg, &p, 0) ?
964 EXT2_MKJOURNAL_LAZYINIT : 0;
966 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
967 } else if (!strcmp(token, "root_owner")) {
969 root_uid = strtoul(arg, &p, 0);
972 _("Invalid root_owner: '%s'\n"),
978 root_gid = strtoul(p, &p, 0);
981 _("Invalid root_owner: '%s'\n"),
990 } else if (!strcmp(token, "discard")) {
992 } else if (!strcmp(token, "nodiscard")) {
994 } else if (!strcmp(token, "quotatype")) {
1003 ret = parse_quota_types(arg, "atype_bits, &errtok);
1007 "Failed to parse quota type at %s", errtok);
1010 com_err(program_name, ret,
1011 "while parsing quota type");
1016 } else if (!strcmp(token, "android_sparse")) {
1017 android_sparse_file = 1;
1018 } else if (!strcmp(token, "encoding")) {
1024 encoding = e2p_str2encoding(arg);
1026 fprintf(stderr, _("Invalid encoding: %s"), arg);
1030 param->s_encoding = encoding;
1031 ext2fs_set_feature_casefold(param);
1032 } else if (!strcmp(token, "encoding_flags")) {
1037 encoding_flags = arg;
1044 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
1045 "Extended options are separated by commas, "
1046 "and may take an argument which\n"
1047 "\tis set off by an equals ('=') sign.\n\n"
1048 "Valid extended options are:\n"
1049 "\tmmp_update_interval=<interval>\n"
1050 "\tnum_backup_sb=<0|1|2>\n"
1051 "\tstride=<RAID per-disk data chunk in blocks>\n"
1052 "\tstripe-width=<RAID stride * data disks in blocks>\n"
1053 "\toffset=<offset to create the file system>\n"
1054 "\tresize=<resize maximum size in blocks>\n"
1055 "\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
1056 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
1057 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
1058 "\troot_owner=<uid of root dir>:<gid of root dir>\n"
1062 "\tencoding=<encoding>\n"
1063 "\tencoding_flags=<flags>\n"
1064 "\tquotatype=<quota type(s) to be enabled>\n\n"),
1065 badopt ? badopt : "");
1069 if (param->s_raid_stride &&
1070 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
1071 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
1072 "multiple of stride %u.\n\n"),
1073 param->s_raid_stripe_width, param->s_raid_stride);
1075 if (ext2fs_has_feature_casefold(param)) {
1076 param->s_encoding_flags =
1077 e2p_get_encoding_flags(param->s_encoding);
1079 if (encoding_flags &&
1080 e2p_str2encoding_flags(param->s_encoding, encoding_flags,
1081 ¶m->s_encoding_flags)) {
1082 fprintf(stderr, _("error: Invalid encoding flag: %s\n"),
1087 } else if (encoding_flags) {
1088 fprintf(stderr, _("error: An encoding must be explicitly "
1089 "specified when passing encoding-flags\n"));
1097 static __u32 ok_features[3] = {
1099 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
1100 EXT2_FEATURE_COMPAT_RESIZE_INODE |
1101 EXT2_FEATURE_COMPAT_DIR_INDEX |
1102 EXT2_FEATURE_COMPAT_EXT_ATTR |
1103 EXT4_FEATURE_COMPAT_SPARSE_SUPER2 |
1104 EXT4_FEATURE_COMPAT_FAST_COMMIT |
1105 EXT4_FEATURE_COMPAT_STABLE_INODES,
1107 EXT2_FEATURE_INCOMPAT_FILETYPE|
1108 EXT3_FEATURE_INCOMPAT_EXTENTS|
1109 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
1110 EXT2_FEATURE_INCOMPAT_META_BG|
1111 EXT4_FEATURE_INCOMPAT_FLEX_BG|
1112 EXT4_FEATURE_INCOMPAT_EA_INODE|
1113 EXT4_FEATURE_INCOMPAT_MMP |
1114 EXT4_FEATURE_INCOMPAT_DIRDATA|
1115 EXT4_FEATURE_INCOMPAT_64BIT|
1116 EXT4_FEATURE_INCOMPAT_INLINE_DATA|
1117 EXT4_FEATURE_INCOMPAT_ENCRYPT |
1118 EXT4_FEATURE_INCOMPAT_CASEFOLD |
1119 EXT4_FEATURE_INCOMPAT_CSUM_SEED |
1120 EXT4_FEATURE_INCOMPAT_LARGEDIR,
1122 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
1123 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
1124 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1125 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
1126 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
1127 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1128 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
1129 EXT4_FEATURE_RO_COMPAT_QUOTA|
1130 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
1131 EXT4_FEATURE_RO_COMPAT_PROJECT|
1132 EXT4_FEATURE_RO_COMPAT_VERITY
1136 static void syntax_err_report(const char *filename, long err, int line_num)
1139 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
1140 filename, line_num, error_message(err));
1144 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
1146 static void edit_feature(const char *str, __u32 *compat_array)
1151 if (e2p_edit_feature(str, compat_array, ok_features)) {
1152 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1158 static void edit_mntopts(const char *str, __u32 *mntopts)
1163 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1164 fprintf(stderr, _("Invalid mount option set: %s\n"),
1176 static errcode_t init_list(struct str_list *sl)
1180 sl->list = malloc((sl->max+1) * sizeof(char *));
1187 static errcode_t push_string(struct str_list *sl, const char *str)
1191 if (sl->num >= sl->max) {
1193 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1196 sl->list = new_list;
1198 sl->list[sl->num] = malloc(strlen(str)+1);
1199 if (sl->list[sl->num] == 0)
1201 strcpy(sl->list[sl->num], str);
1203 sl->list[sl->num] = 0;
1207 static void print_str_list(char **list)
1211 for (cpp = list; *cpp; cpp++) {
1212 printf("'%s'", *cpp);
1214 fputs(", ", stdout);
1216 fputc('\n', stdout);
1220 * Return TRUE if the profile has the given subsection
1222 static int profile_has_subsection(profile_t prof, const char *section,
1223 const char *subsection)
1226 const char *names[4];
1231 names[1] = subsection;
1234 if (profile_iterator_create(prof, names,
1235 PROFILE_ITER_LIST_SECTION |
1236 PROFILE_ITER_RELATIONS_ONLY, &state))
1239 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1244 profile_iterator_free(&state);
1248 static char **parse_fs_type(const char *fs_type,
1249 const char *usage_types,
1250 struct ext2_super_block *sb,
1251 blk64_t fs_blocks_count,
1254 const char *ext_type = 0;
1256 char *profile_type = 0;
1258 const char *size_type;
1259 struct str_list list;
1260 unsigned long long meg;
1261 int is_hurd = for_hurd(creator_os);
1263 if (init_list(&list))
1270 else if (!strcmp(program_name, "mke3fs"))
1272 else if (!strcmp(program_name, "mke4fs"))
1274 else if (progname) {
1275 ext_type = strrchr(progname, '/');
1279 ext_type = progname;
1281 if (!strncmp(ext_type, "mkfs.", 5)) {
1283 if (ext_type[0] == 0)
1290 profile_get_string(profile, "defaults", "fs_type", 0,
1291 "ext2", &profile_type);
1292 ext_type = profile_type;
1293 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1298 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1299 strcmp(ext_type, "ext2")) {
1300 printf(_("\nYour mke2fs.conf file does not define the "
1301 "%s filesystem type.\n"), ext_type);
1302 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1303 !strcmp(ext_type, "ext4dev")) {
1304 printf("%s", _("You probably need to install an "
1305 "updated mke2fs.conf file.\n\n"));
1308 printf("%s", _("Aborting...\n"));
1313 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1314 if (fs_blocks_count < 3 * meg)
1315 size_type = "floppy";
1316 else if (fs_blocks_count < 512 * meg)
1317 size_type = "small";
1318 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1319 size_type = "default";
1320 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1326 usage_types = size_type;
1328 parse_str = malloc(strlen(usage_types)+1);
1334 strcpy(parse_str, usage_types);
1337 push_string(&list, ext_type);
1340 t = strchr(cp, ',');
1345 if (profile_has_subsection(profile, "fs_types", cp))
1346 push_string(&list, cp);
1347 else if (strcmp(cp, "default") != 0)
1349 _("\nWarning: the fs_type %s is not "
1350 "defined in mke2fs.conf\n\n"),
1361 push_string(&list, "hurd");
1365 char *get_string_from_profile(char **types, const char *opt,
1366 const char *def_val)
1371 for (i=0; types[i]; i++);
1372 for (i-=1; i >=0 ; i--) {
1373 profile_get_string(profile, "fs_types", types[i],
1378 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1382 int get_int_from_profile(char **types, const char *opt, int def_val)
1387 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1388 for (cpp = types; *cpp; cpp++)
1389 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1393 static unsigned int get_uint_from_profile(char **types, const char *opt,
1394 unsigned int def_val)
1399 profile_get_uint(profile, "defaults", opt, 0, def_val, &ret);
1400 for (cpp = types; *cpp; cpp++)
1401 profile_get_uint(profile, "fs_types", *cpp, opt, ret, &ret);
1405 static double get_double_from_profile(char **types, const char *opt,
1411 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1412 for (cpp = types; *cpp; cpp++)
1413 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1417 int get_bool_from_profile(char **types, const char *opt, int def_val)
1422 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1423 for (cpp = types; *cpp; cpp++)
1424 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1428 extern const char *mke2fs_default_profile;
1429 static const char *default_files[] = { "<default>", 0 };
1431 struct device_param {
1432 unsigned long min_io; /* prefered minimum IO size */
1433 unsigned long opt_io; /* optimal IO size */
1434 unsigned long alignment_offset; /* alignment offset wrt physical block size */
1435 unsigned int dax:1; /* supports dax? */
1438 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1440 * Sets the geometry of a device (stripe/stride), and returns the
1441 * device's alignment offset, if any, or a negative error.
1443 static int get_device_geometry(const char *file,
1444 unsigned int blocksize,
1445 unsigned int psector_size,
1446 struct device_param *dev_param)
1451 struct stat statbuf;
1453 memset(dev_param, 0, sizeof(*dev_param));
1455 /* Nothing to do for a regular file */
1456 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1459 pr = blkid_new_probe_from_filename(file);
1463 tp = blkid_probe_get_topology(pr);
1467 dev_param->min_io = blkid_topology_get_minimum_io_size(tp);
1468 dev_param->opt_io = blkid_topology_get_optimal_io_size(tp);
1469 if ((dev_param->min_io == 0) && (psector_size > blocksize))
1470 dev_param->min_io = psector_size;
1471 if ((dev_param->opt_io == 0) && dev_param->min_io > 0)
1472 dev_param->opt_io = dev_param->min_io;
1473 if ((dev_param->opt_io == 0) && (psector_size > blocksize))
1474 dev_param->opt_io = psector_size;
1476 dev_param->alignment_offset = blkid_topology_get_alignment_offset(tp);
1477 #ifdef HAVE_BLKID_TOPOLOGY_GET_DAX
1478 dev_param->dax = blkid_topology_get_dax(tp);
1482 blkid_free_probe(pr);
1487 static void PRS(int argc, char *argv[])
1490 int cluster_size = 0;
1492 int explicit_fssize = 0;
1494 int inode_ratio = 0;
1496 unsigned long flex_bg_size = 0;
1497 double reserved_ratio = -1.0;
1498 int lsector_size = 0, psector_size = 0;
1499 int show_version_only = 0, is_device = 0;
1500 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1502 char * oldpath = getenv("PATH");
1503 char * extended_opts = 0;
1505 char * usage_types = 0;
1507 * NOTE: A few words about fs_blocks_count and blocksize:
1509 * Initially, blocksize is set to zero, which implies 1024.
1510 * If -b is specified, blocksize is updated to the user's value.
1512 * Next, the device size or the user's "blocks" command line argument
1513 * is used to set fs_blocks_count; the units are blocksize.
1515 * Later, if blocksize hasn't been set and the profile specifies a
1516 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1517 * appropriately. Note the change in units!
1519 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1521 blk64_t fs_blocks_count = 0;
1523 int s_opt = -1, r_opt = -1;
1524 char *fs_features = 0;
1525 int fs_features_size = 0;
1528 int pathlen = sizeof(PATH_SET) + 1;
1529 struct device_param dev_param;
1532 pathlen += strlen(oldpath);
1533 newpath = malloc(pathlen);
1535 fprintf(stderr, "%s",
1536 _("Couldn't allocate memory for new PATH.\n"));
1539 strcpy(newpath, PATH_SET);
1541 /* Update our PATH to include /sbin */
1543 strcat (newpath, ":");
1544 strcat (newpath, oldpath);
1548 /* Determine the system page size if possible */
1550 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1551 #define _SC_PAGESIZE _SC_PAGE_SIZE
1554 sysval = sysconf(_SC_PAGESIZE);
1556 sys_page_size = sysval;
1557 #endif /* _SC_PAGESIZE */
1558 #endif /* HAVE_SYSCONF */
1560 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1562 profile_set_syntax_err_cb(syntax_err_report);
1563 retval = profile_init(config_fn, &profile);
1564 if (retval == ENOENT) {
1565 retval = profile_init(default_files, &profile);
1568 retval = profile_set_default(profile, mke2fs_default_profile);
1571 } else if (retval) {
1573 fprintf(stderr, _("Couldn't init profile successfully"
1574 " (error: %ld).\n"), retval);
1578 setbuf(stdout, NULL);
1579 setbuf(stderr, NULL);
1580 add_error_table(&et_ext2_error_table);
1581 add_error_table(&et_prof_error_table);
1582 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1583 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1585 if (ext2fs_is_before_linux_ver(2, 2, 0))
1586 fs_param.s_rev_level = 0;
1588 if (argc && *argv) {
1589 program_name = get_progname(*argv);
1591 /* If called as mkfs.ext3, create a journal inode */
1592 if (!strcmp(program_name, "mkfs.ext3") ||
1593 !strcmp(program_name, "mke3fs"))
1597 while ((c = getopt (argc, argv,
1598 "b:cd:e:g:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:Vz:")) != EOF) {
1601 blocksize = parse_num_blocks2(optarg, -1);
1602 b = (blocksize > 0) ? blocksize : -blocksize;
1603 if (b < EXT2_MIN_BLOCK_SIZE ||
1604 b > EXT2_MAX_BLOCK_SIZE) {
1605 com_err(program_name, 0,
1606 _("invalid block size - %s"), optarg);
1609 if (blocksize > 4096)
1610 fprintf(stderr, _("Warning: blocksize %d not "
1611 "usable on most systems.\n"),
1614 fs_param.s_log_block_size =
1615 int_log2(blocksize >>
1616 EXT2_MIN_BLOCK_LOG_SIZE);
1618 case 'c': /* Check for bad blocks */
1622 cluster_size = parse_num_blocks2(optarg, -1);
1623 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1624 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1625 com_err(program_name, 0,
1626 _("invalid cluster size - %s"),
1632 src_root_dir = optarg;
1638 com_err(program_name, 0, "%s",
1639 _("'-R' is deprecated, use '-E' instead"));
1642 extended_opts = optarg;
1645 if (strcmp(optarg, "continue") == 0)
1646 errors_behavior = EXT2_ERRORS_CONTINUE;
1647 else if (strcmp(optarg, "remount-ro") == 0)
1648 errors_behavior = EXT2_ERRORS_RO;
1649 else if (strcmp(optarg, "panic") == 0)
1650 errors_behavior = EXT2_ERRORS_PANIC;
1652 com_err(program_name, 0,
1653 _("bad error behavior - %s"),
1662 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1664 com_err(program_name, 0, "%s",
1665 _("Illegal number for blocks per group"));
1668 if ((fs_param.s_blocks_per_group % 8) != 0) {
1669 com_err(program_name, 0, "%s",
1670 _("blocks per group must be multiple of 8"));
1675 flex_bg_size = strtoul(optarg, &tmp, 0);
1677 com_err(program_name, 0, "%s",
1678 _("Illegal number for flex_bg size"));
1681 if (flex_bg_size < 1 ||
1682 (flex_bg_size & (flex_bg_size-1)) != 0) {
1683 com_err(program_name, 0, "%s",
1684 _("flex_bg size must be a power of 2"));
1687 if (flex_bg_size > MAX_32_NUM) {
1688 com_err(program_name, 0,
1689 _("flex_bg size (%lu) must be less than"
1690 " or equal to 2^31"), flex_bg_size);
1695 inode_ratio = parse_num_blocks(optarg, -1);
1696 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1697 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) {
1698 com_err(program_name, 0,
1699 _("invalid inode ratio %s (min %d/max %d)"),
1700 optarg, EXT2_MIN_BLOCK_SIZE,
1701 EXT2_MAX_BLOCK_SIZE * 1024);
1706 inode_size = strtoul(optarg, &tmp, 0);
1708 com_err(program_name, 0,
1709 _("invalid inode size - %s"), optarg);
1716 if (!journal_fc_size)
1717 journal_fc_size = -1;
1720 parse_journal_opts(optarg);
1723 fprintf(stderr, "%s",
1724 _("Warning: -K option is deprecated and "
1725 "should not be used anymore. Use "
1726 "\'-E nodiscard\' extended option "
1731 bad_blocks_filename = realloc(bad_blocks_filename,
1732 strlen(optarg) + 1);
1733 if (!bad_blocks_filename) {
1734 com_err(program_name, ENOMEM, "%s",
1735 _("in malloc for bad_blocks_filename"));
1738 strcpy(bad_blocks_filename, optarg);
1741 volume_label = optarg;
1742 if (strlen(volume_label) > EXT2_LABEL_LEN) {
1743 volume_label[EXT2_LABEL_LEN] = '\0';
1744 fprintf(stderr, _("Warning: label too long; will be truncated to '%s'\n\n"),
1749 reserved_ratio = strtod(optarg, &tmp);
1750 if ( *tmp || reserved_ratio > 50 ||
1751 reserved_ratio < 0) {
1752 com_err(program_name, 0,
1753 _("invalid reserved blocks percent - %s"),
1765 num_inodes = strtoul(optarg, &tmp, 0);
1767 com_err(program_name, 0,
1768 _("bad num inodes - %s"), optarg);
1773 creator_os = optarg;
1776 retval = ext2fs_resize_mem(fs_features_size,
1777 fs_features_size + 1 + strlen(optarg),
1780 com_err(program_name, retval,
1781 _("while allocating fs_feature string"));
1784 if (fs_features_size)
1785 strcat(fs_features, ",");
1788 strcat(fs_features, optarg);
1789 fs_features_size += 1 + strlen(optarg);
1795 r_opt = strtoul(optarg, &tmp, 0);
1797 com_err(program_name, 0,
1798 _("bad revision level - %s"), optarg);
1801 if (r_opt > EXT2_MAX_SUPP_REV) {
1802 com_err(program_name, EXT2_ET_REV_TOO_HIGH,
1803 _("while trying to create revision %d"), r_opt);
1806 fs_param.s_rev_level = r_opt;
1808 case 's': /* deprecated */
1809 s_opt = atoi(optarg);
1816 com_err(program_name, 0, "%s",
1817 _("The -t option may only be used once"));
1820 fs_type = strdup(optarg);
1824 com_err(program_name, 0, "%s",
1825 _("The -T option may only be used once"));
1828 usage_types = strdup(optarg);
1837 /* Print version number and exit */
1838 show_version_only++;
1847 if ((optind == argc) && !show_version_only)
1849 device_name = argv[optind++];
1851 if (!quiet || show_version_only)
1852 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1855 if (show_version_only) {
1856 fprintf(stderr, _("\tUsing %s\n"),
1857 error_message(EXT2_ET_BASE));
1862 * If there's no blocksize specified and there is a journal
1863 * device, use it to figure out the blocksize
1865 if (blocksize <= 0 && journal_device) {
1869 #ifdef CONFIG_TESTIO_DEBUG
1870 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1871 io_ptr = test_io_manager;
1872 test_io_backing_manager = unix_io_manager;
1875 io_ptr = unix_io_manager;
1876 retval = ext2fs_open(journal_device,
1877 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1880 com_err(program_name, retval,
1881 _("while trying to open journal device %s\n"),
1885 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1886 com_err(program_name, 0,
1887 _("Journal dev blocksize (%d) smaller than "
1888 "minimum blocksize %d\n"), jfs->blocksize,
1892 blocksize = jfs->blocksize;
1893 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1894 fs_param.s_log_block_size =
1895 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1896 ext2fs_close_free(&jfs);
1899 if (optind < argc) {
1900 fs_blocks_count = parse_num_blocks2(argv[optind++],
1901 fs_param.s_log_block_size);
1902 if (!fs_blocks_count) {
1903 com_err(program_name, 0,
1904 _("invalid blocks '%s' on device '%s'"),
1905 argv[optind - 1], device_name);
1912 profile_get_integer(profile, "options", "sync_kludge", 0, 0,
1914 tmp = getenv("MKE2FS_SYNC");
1916 sync_kludge = atoi(tmp);
1918 profile_get_integer(profile, "options", "proceed_delay", 0, 0,
1921 /* The isatty() test is so we don't break existing scripts */
1922 flags = CREATE_FILE;
1923 if (isatty(0) && isatty(1) && !offset)
1924 flags |= CHECK_FS_EXIST;
1926 flags |= VERBOSE_CREATE;
1927 if (fs_blocks_count == 0)
1930 explicit_fssize = 1;
1931 if (!check_plausibility(device_name, flags, &is_device) && !force)
1932 proceed_question(proceed_delay);
1934 check_mount(device_name, force, _("filesystem"));
1936 /* Determine the size of the device (if possible) */
1937 if (noaction && fs_blocks_count) {
1938 dev_size = fs_blocks_count;
1942 retval = ext2fs_get_device_size2(device_name,
1943 EXT2_BLOCK_SIZE(&fs_param),
1946 retval = ext2fs_get_device_size(device_name,
1947 EXT2_BLOCK_SIZE(&fs_param),
1950 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1951 com_err(program_name, retval, "%s",
1952 _("while trying to determine filesystem size"));
1955 if (!fs_blocks_count) {
1956 if (retval == EXT2_ET_UNIMPLEMENTED) {
1957 com_err(program_name, 0, "%s",
1958 _("Couldn't determine device size; you "
1959 "must specify\nthe size of the "
1963 if (dev_size == 0) {
1964 com_err(program_name, 0, "%s",
1965 _("Device size reported to be zero. "
1966 "Invalid partition specified, or\n\t"
1967 "partition table wasn't reread "
1968 "after running fdisk, due to\n\t"
1969 "a modified partition being busy "
1970 "and in use. You may need to reboot\n\t"
1971 "to re-read your partition table.\n"
1975 fs_blocks_count = dev_size;
1976 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1977 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1978 EXT2_BLOCK_SIZE(&fs_param))-1));
1980 } else if (!force && is_device && (fs_blocks_count > dev_size)) {
1981 com_err(program_name, 0, "%s",
1982 _("Filesystem larger than apparent device size."));
1983 proceed_question(proceed_delay);
1987 profile_get_string(profile, "devices", device_name,
1988 "fs_type", 0, &fs_type);
1990 profile_get_string(profile, "devices", device_name,
1991 "usage_types", 0, &usage_types);
1994 * We have the file system (or device) size, so we can now
1995 * determine the appropriate file system types so the fs can
1996 * be appropriately configured.
1998 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1999 fs_blocks_count, argv[0]);
2001 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
2005 /* Figure out what features should be enabled */
2008 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
2009 tmp = get_string_from_profile(fs_types, "base_features",
2010 "sparse_super,large_file,filetype,resize_inode,dir_index");
2011 edit_feature(tmp, &fs_param.s_feature_compat);
2014 /* And which mount options as well */
2015 tmp = get_string_from_profile(fs_types, "default_mntopts",
2017 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
2021 for (cpp = fs_types; *cpp; cpp++) {
2023 profile_get_string(profile, "fs_types", *cpp,
2024 "features", "", &tmp);
2026 edit_feature(tmp, &fs_param.s_feature_compat);
2030 tmp = get_string_from_profile(fs_types, "default_features",
2033 /* Mask off features which aren't supported by the Hurd */
2034 if (for_hurd(creator_os)) {
2035 ext2fs_clear_feature_filetype(&fs_param);
2036 ext2fs_clear_feature_huge_file(&fs_param);
2037 ext2fs_clear_feature_metadata_csum(&fs_param);
2038 ext2fs_clear_feature_ea_inode(&fs_param);
2039 ext2fs_clear_feature_casefold(&fs_param);
2041 edit_feature(fs_features ? fs_features : tmp,
2042 &fs_param.s_feature_compat);
2045 (void) ext2fs_free_mem(&fs_features);
2047 * If the user specified features incompatible with the Hurd, complain
2049 if (for_hurd(creator_os)) {
2050 if (ext2fs_has_feature_filetype(&fs_param)) {
2051 fprintf(stderr, "%s", _("The HURD does not support the "
2052 "filetype feature.\n"));
2055 if (ext2fs_has_feature_huge_file(&fs_param)) {
2056 fprintf(stderr, "%s", _("The HURD does not support the "
2057 "huge_file feature.\n"));
2060 if (ext2fs_has_feature_metadata_csum(&fs_param)) {
2061 fprintf(stderr, "%s", _("The HURD does not support the "
2062 "metadata_csum feature.\n"));
2065 if (ext2fs_has_feature_ea_inode(&fs_param)) {
2066 fprintf(stderr, "%s", _("The HURD does not support the "
2067 "ea_inode feature.\n"));
2072 /* Get the hardware sector sizes, if available */
2073 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
2075 com_err(program_name, retval, "%s",
2076 _("while trying to determine hardware sector size"));
2079 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
2081 com_err(program_name, retval, "%s",
2082 _("while trying to determine physical sector size"));
2086 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
2088 lsector_size = atoi(tmp);
2089 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
2091 psector_size = atoi(tmp);
2093 /* Older kernels may not have physical/logical distinction */
2095 psector_size = lsector_size;
2097 if (blocksize <= 0) {
2098 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
2100 if (use_bsize == -1) {
2101 use_bsize = sys_page_size;
2102 if (ext2fs_is_before_linux_ver(2, 6, 0) &&
2106 if (lsector_size && use_bsize < lsector_size)
2107 use_bsize = lsector_size;
2108 if ((blocksize < 0) && (use_bsize < (-blocksize)))
2109 use_bsize = -blocksize;
2110 blocksize = use_bsize;
2111 fs_blocks_count /= (blocksize / 1024);
2113 if (blocksize < lsector_size) { /* Impossible */
2114 com_err(program_name, EINVAL, "%s",
2115 _("while setting blocksize; too small "
2118 } else if ((blocksize < psector_size) &&
2119 (psector_size <= sys_page_size)) { /* Suboptimal */
2120 fprintf(stderr, _("Warning: specified blocksize %d is "
2121 "less than device physical sectorsize %d\n"),
2122 blocksize, psector_size);
2126 fs_param.s_log_block_size =
2127 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
2130 * We now need to do a sanity check of fs_blocks_count for
2131 * 32-bit vs 64-bit block number support.
2133 if (fs_blocks_count > MAX_32_NUM) {
2134 if (!ext2fs_has_feature_64bit(&fs_param) &&
2135 get_bool_from_profile(fs_types, "auto_64-bit_support", 0))
2136 ext2fs_set_feature_64bit(&fs_param);
2138 if (ext2fs_has_feature_64bit(&fs_param)) {
2139 ext2fs_clear_feature_resize_inode(&fs_param);
2142 _("%s: Size of device (0x%llx blocks) %s "
2143 "too big to be expressed\n\t"
2144 "in 32 bits using a blocksize of %d.\n"),
2146 (unsigned long long) fs_blocks_count,
2147 device_name, EXT2_BLOCK_SIZE(&fs_param));
2153 * Guard against group descriptor count overflowing... Mostly to avoid
2154 * strange results for absurdly large devices. This is in log2:
2155 * (blocksize) * (bits per byte) * (maximum number of block groups)
2157 if (fs_blocks_count >
2158 (1ULL << (EXT2_BLOCK_SIZE_BITS(&fs_param) + 3 + 32)) - 1) {
2159 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
2160 "too big to create\n\t"
2161 "a filesystem using a blocksize of %d.\n"),
2162 program_name, (unsigned long long) fs_blocks_count,
2163 device_name, EXT2_BLOCK_SIZE(&fs_param));
2167 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
2169 if (ext2fs_has_feature_journal_dev(&fs_param)) {
2172 for (i=0; fs_types[i]; i++) {
2176 fs_types[0] = strdup("journal");
2181 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
2182 print_str_list(fs_types);
2185 if (r_opt == EXT2_GOOD_OLD_REV &&
2186 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
2187 fs_param.s_feature_ro_compat)) {
2188 fprintf(stderr, "%s", _("Filesystem features not supported "
2189 "with revision 0 filesystems\n"));
2194 if (r_opt == EXT2_GOOD_OLD_REV) {
2195 fprintf(stderr, "%s",
2196 _("Sparse superblocks not supported "
2197 "with revision 0 filesystems\n"));
2200 ext2fs_set_feature_sparse_super(&fs_param);
2201 } else if (s_opt == 0)
2202 ext2fs_clear_feature_sparse_super(&fs_param);
2204 if (journal_size != 0) {
2205 if (r_opt == EXT2_GOOD_OLD_REV) {
2206 fprintf(stderr, "%s", _("Journals not supported with "
2207 "revision 0 filesystems\n"));
2210 ext2fs_set_feature_journal(&fs_param);
2213 /* Get reserved_ratio from profile if not specified on cmd line. */
2214 if (reserved_ratio < 0.0) {
2215 reserved_ratio = get_double_from_profile(
2216 fs_types, "reserved_ratio", 5.0);
2217 if (reserved_ratio > 50 || reserved_ratio < 0) {
2218 com_err(program_name, 0,
2219 _("invalid reserved blocks percent - %lf"),
2225 if (ext2fs_has_feature_journal_dev(&fs_param)) {
2227 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
2228 fs_param.s_feature_compat = 0;
2229 fs_param.s_feature_ro_compat &=
2230 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
2233 /* Check the user's mkfs options for 64bit */
2234 if (fs_blocks_count > MAX_32_NUM &&
2235 !ext2fs_has_feature_extents(&fs_param)) {
2236 printf("%s", _("Extents MUST be enabled for filesystems with "
2237 "over 2^32 blocks. Use '-O extents' to fix.\n"));
2241 /* Set first meta blockgroup via an environment variable */
2242 /* (this is mostly for debugging purposes) */
2243 if (ext2fs_has_feature_meta_bg(&fs_param) &&
2244 (tmp = getenv("MKE2FS_FIRST_META_BG")))
2245 fs_param.s_first_meta_bg = atoi(tmp);
2246 if (ext2fs_has_feature_bigalloc(&fs_param)) {
2248 cluster_size = get_int_from_profile(fs_types,
2251 fs_param.s_log_cluster_size =
2252 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
2253 if (fs_param.s_log_cluster_size &&
2254 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
2255 com_err(program_name, 0, "%s",
2256 _("The cluster size may not be "
2257 "smaller than the block size.\n"));
2260 } else if (cluster_size) {
2261 com_err(program_name, 0, "%s",
2262 _("specifying a cluster size requires the "
2263 "bigalloc feature"));
2266 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2268 if (inode_ratio == 0) {
2269 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2271 if (inode_ratio < blocksize)
2272 inode_ratio = blocksize;
2273 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2274 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2277 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2278 retval = get_device_geometry(device_name, blocksize,
2279 psector_size, &dev_param);
2282 _("warning: Unable to get device geometry for %s\n"),
2285 /* setting stripe/stride to blocksize is pointless */
2286 if (dev_param.min_io > blocksize)
2287 fs_param.s_raid_stride = dev_param.min_io / blocksize;
2288 if (dev_param.opt_io > blocksize) {
2289 fs_param.s_raid_stripe_width =
2290 dev_param.opt_io / blocksize;
2293 if (dev_param.alignment_offset) {
2294 printf(_("%s alignment is offset by %lu bytes.\n"),
2295 device_name, dev_param.alignment_offset);
2296 printf(_("This may result in very poor performance, "
2297 "(re)-partitioning suggested.\n"));
2300 if (dev_param.dax && blocksize != sys_page_size) {
2302 _("%s is capable of DAX but current block size "
2303 "%u is different from system page size %u so "
2304 "filesystem will not support DAX.\n"),
2305 device_name, blocksize, sys_page_size);
2310 num_backups = get_int_from_profile(fs_types, "num_backup_sb", 2);
2312 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2315 * Initialize s_desc_size so that the parse_extended_opts()
2316 * can correctly handle "-E resize=NNN" if the 64-bit option
2319 if (ext2fs_has_feature_64bit(&fs_param))
2320 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2322 /* This check should happen beyond the last assignment to blocksize */
2323 if (blocksize > sys_page_size) {
2325 com_err(program_name, 0,
2326 _("%d-byte blocks too big for system (max %d)"),
2327 blocksize, sys_page_size);
2328 proceed_question(proceed_delay);
2330 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2331 "(max %d), forced to continue\n"),
2332 blocksize, sys_page_size);
2335 /* Metadata checksumming wasn't totally stable before 3.18. */
2336 if (ext2fs_is_before_linux_ver(3, 18, 0) &&
2337 ext2fs_has_feature_metadata_csum(&fs_param))
2338 fprintf(stderr, _("Suggestion: Use Linux kernel >= 3.18 for "
2339 "improved stability of the metadata and journal "
2340 "checksum features.\n"));
2343 * On newer kernels we do have lazy_itable_init support. So pick the
2344 * right default in case ext4 module is not loaded.
2346 if (ext2fs_is_before_linux_ver(2, 6, 37))
2347 lazy_itable_init = 0;
2349 lazy_itable_init = 1;
2351 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2352 lazy_itable_init = 1;
2354 lazy_itable_init = get_bool_from_profile(fs_types,
2357 discard = get_bool_from_profile(fs_types, "discard" , discard);
2358 journal_flags |= get_bool_from_profile(fs_types,
2359 "lazy_journal_init", 0) ?
2360 EXT2_MKJOURNAL_LAZYINIT : 0;
2361 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2363 if (!journal_location_string)
2364 journal_location_string = get_string_from_profile(fs_types,
2365 "journal_location", "");
2366 if ((journal_location == ~0ULL) && journal_location_string &&
2367 *journal_location_string)
2368 journal_location = parse_num_blocks2(journal_location_string,
2369 fs_param.s_log_block_size);
2370 free(journal_location_string);
2372 packed_meta_blocks = get_bool_from_profile(fs_types,
2373 "packed_meta_blocks", 0);
2374 if (packed_meta_blocks)
2375 journal_location = 0;
2377 if (ext2fs_has_feature_casefold(&fs_param)) {
2378 char *ef, *en = get_string_from_profile(fs_types,
2379 "encoding", "utf8");
2380 int encoding = e2p_str2encoding(en);
2383 com_err(program_name, 0,
2384 _("Unknown filename encoding from profile: %s"),
2389 fs_param.s_encoding = encoding;
2390 ef = get_string_from_profile(fs_types, "encoding_flags", NULL);
2392 if (e2p_str2encoding_flags(encoding, ef,
2393 &fs_param.s_encoding_flags) < 0) {
2394 com_err(program_name, 0,
2395 _("Unknown encoding flags from profile: %s"), ef);
2400 fs_param.s_encoding_flags =
2401 e2p_get_encoding_flags(encoding);
2404 /* Get options from profile */
2405 for (cpp = fs_types; *cpp; cpp++) {
2407 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2409 parse_extended_opts(&fs_param, tmp);
2414 parse_extended_opts(&fs_param, extended_opts);
2416 if (explicit_fssize == 0 && offset > 0) {
2417 fs_blocks_count -= offset / EXT2_BLOCK_SIZE(&fs_param);
2418 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
2420 _("\nWarning: offset specified without an "
2421 "explicit file system size.\n"
2422 "Creating a file system with %llu blocks "
2424 "not be what you want.\n\n"),
2425 (unsigned long long) fs_blocks_count);
2428 if (quotatype_bits & QUOTA_PRJ_BIT)
2429 ext2fs_set_feature_project(&fs_param);
2431 if (ext2fs_has_feature_project(&fs_param)) {
2432 quotatype_bits |= QUOTA_PRJ_BIT;
2433 if (inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
2434 com_err(program_name, 0,
2435 _("%d byte inodes are too small for "
2440 if (inode_size == 0) {
2441 inode_size = get_int_from_profile(fs_types,
2443 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE*2)
2444 inode_size = EXT2_GOOD_OLD_INODE_SIZE*2;
2448 /* Don't allow user to set both metadata_csum and uninit_bg bits. */
2449 if (ext2fs_has_feature_metadata_csum(&fs_param) &&
2450 ext2fs_has_feature_gdt_csum(&fs_param))
2451 ext2fs_clear_feature_gdt_csum(&fs_param);
2453 /* Can't support bigalloc feature without extents feature */
2454 if (ext2fs_has_feature_bigalloc(&fs_param) &&
2455 !ext2fs_has_feature_extents(&fs_param)) {
2456 com_err(program_name, 0, "%s",
2457 _("Can't support bigalloc feature without "
2458 "extents feature"));
2462 if (ext2fs_has_feature_meta_bg(&fs_param) &&
2463 ext2fs_has_feature_resize_inode(&fs_param)) {
2464 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2465 "features are not compatible.\n"
2466 "They can not be both enabled "
2467 "simultaneously.\n"));
2471 if (!quiet && ext2fs_has_feature_bigalloc(&fs_param))
2472 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2473 "still under development\n"
2474 "See https://ext4.wiki.kernel.org/"
2475 "index.php/Bigalloc for more information\n\n"));
2478 * Since sparse_super is the default, we would only have a problem
2479 * here if it was explicitly disabled.
2481 if (ext2fs_has_feature_resize_inode(&fs_param) &&
2482 !ext2fs_has_feature_sparse_super(&fs_param)) {
2483 com_err(program_name, 0, "%s",
2484 _("reserved online resize blocks not supported "
2485 "on non-sparse filesystem"));
2489 if (fs_param.s_blocks_per_group) {
2490 if (fs_param.s_blocks_per_group < 256 ||
2491 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2492 com_err(program_name, 0, "%s",
2493 _("blocks per group count out of range"));
2499 * If the bigalloc feature is enabled, then the -g option will
2500 * specify the number of clusters per group.
2502 if (ext2fs_has_feature_bigalloc(&fs_param)) {
2503 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2504 fs_param.s_blocks_per_group = 0;
2507 if (inode_size == 0)
2508 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2509 if (!flex_bg_size && ext2fs_has_feature_flex_bg(&fs_param))
2510 flex_bg_size = get_uint_from_profile(fs_types,
2511 "flex_bg_size", 16);
2513 if (!ext2fs_has_feature_flex_bg(&fs_param)) {
2514 com_err(program_name, 0, "%s",
2515 _("Flex_bg feature not enabled, so "
2516 "flex_bg size may not be specified"));
2519 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2522 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2523 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2524 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2525 inode_size & (inode_size - 1)) {
2526 com_err(program_name, 0,
2527 _("invalid inode size %d (min %d/max %d)"),
2528 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2532 fs_param.s_inode_size = inode_size;
2536 * If inode size is 128 and inline data is enabled, we need
2537 * to notify users that inline data will never be useful.
2539 if (ext2fs_has_feature_inline_data(&fs_param) &&
2540 fs_param.s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
2541 com_err(program_name, 0,
2542 _("%d byte inodes are too small for inline data; "
2543 "specify larger size"),
2544 fs_param.s_inode_size);
2548 /* Make sure number of inodes specified will fit in 32 bits */
2549 if (num_inodes == 0) {
2550 unsigned long long n;
2551 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2552 if (n > MAX_32_NUM) {
2553 num_inodes = MAX_32_NUM;
2554 if (!ext2fs_has_feature_64bit(&fs_param))
2555 com_err(program_name, 0,
2556 _("too many inodes (%llu), reduced to "
2557 "%llu"), n, MAX_32_NUM);
2559 } else if (num_inodes > MAX_32_NUM) {
2560 com_err(program_name, 0,
2561 _("too many inodes (%llu), specify < 2^32 inodes"),
2562 (unsigned long long) num_inodes);
2566 * Calculate number of inodes based on the inode ratio
2568 fs_param.s_inodes_count = num_inodes ? num_inodes :
2569 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2571 if ((((unsigned long long)fs_param.s_inodes_count) *
2572 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2573 ((ext2fs_blocks_count(&fs_param)) *
2574 EXT2_BLOCK_SIZE(&fs_param))) {
2575 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2576 "(%u) too big for a\n\t"
2577 "filesystem with %llu blocks, "
2578 "specify higher inode_ratio (-i)\n\t"
2579 "or lower inode count (-N).\n"),
2580 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2581 fs_param.s_inodes_count,
2582 (unsigned long long) ext2fs_blocks_count(&fs_param));
2587 * Calculate number of blocks to reserve
2589 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2590 ext2fs_blocks_count(&fs_param) / 100.0);
2592 if (ext2fs_has_feature_sparse_super2(&fs_param)) {
2593 if (num_backups >= 1)
2594 fs_param.s_backup_bgs[0] = 1;
2595 if (num_backups >= 2)
2596 fs_param.s_backup_bgs[1] = ~0;
2603 static int should_do_undo(const char *name)
2608 struct ext2_super_block super;
2609 io_manager manager = unix_io_manager;
2610 int csum_flag, force_undo;
2612 csum_flag = ext2fs_has_feature_metadata_csum(&fs_param) ||
2613 ext2fs_has_feature_gdt_csum(&fs_param);
2614 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2615 if (!force_undo && (!csum_flag || !lazy_itable_init))
2618 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2621 * We don't handle error cases instead we
2622 * declare that the file system doesn't exist
2623 * and let the rest of mke2fs take care of
2630 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2631 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2637 #if defined(WORDS_BIGENDIAN)
2638 s_magic = ext2fs_swab16(super.s_magic);
2640 s_magic = super.s_magic;
2643 if (s_magic == EXT2_SUPER_MAGIC)
2647 io_channel_close(channel);
2654 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2656 errcode_t retval = ENOMEM;
2657 char *tdb_dir = NULL, *tdb_file = NULL;
2658 char *dev_name, *tmp_name;
2659 int free_tdb_dir = 0;
2661 /* (re)open a specific undo file */
2662 if (undo_file && undo_file[0] != 0) {
2663 retval = set_undo_io_backing_manager(*io_ptr);
2666 *io_ptr = undo_io_manager;
2667 retval = set_undo_io_backup_file(undo_file);
2670 printf(_("Overwriting existing filesystem; this can be undone "
2671 "using the command:\n"
2672 " e2undo %s %s\n\n"), undo_file, name);
2677 * Configuration via a conf file would be
2680 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2682 profile_get_string(profile, "defaults",
2683 "undo_dir", 0, "/var/lib/e2fsprogs",
2688 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2689 access(tdb_dir, W_OK)) {
2695 tmp_name = strdup(name);
2698 dev_name = basename(tmp_name);
2699 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2704 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2707 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2709 com_err(program_name, retval,
2710 _("while trying to delete %s"), tdb_file);
2714 retval = set_undo_io_backing_manager(*io_ptr);
2717 *io_ptr = undo_io_manager;
2718 retval = set_undo_io_backup_file(tdb_file);
2721 printf(_("Overwriting existing filesystem; this can be undone "
2722 "using the command:\n"
2723 " e2undo %s %s\n\n"), tdb_file, name);
2735 com_err(program_name, retval, "%s",
2736 _("while trying to setup undo file\n"));
2740 static int mke2fs_discard_device(ext2_filsys fs)
2742 struct ext2fs_numeric_progress_struct progress;
2743 blk64_t blocks = ext2fs_blocks_count(fs->super);
2744 blk64_t count = DISCARD_STEP_MB;
2749 * Let's try if discard really works on the device, so
2750 * we do not print numeric progress resulting in failure
2753 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2756 cur = fs->blocksize;
2758 count *= (1024 * 1024);
2759 count /= fs->blocksize;
2761 ext2fs_numeric_progress_init(fs, &progress,
2762 _("Discarding device blocks: "),
2764 while (cur < blocks) {
2765 ext2fs_numeric_progress_update(fs, &progress, cur);
2767 if (cur + count > blocks)
2768 count = blocks - cur;
2770 retval = io_channel_discard(fs->io, cur, count);
2777 ext2fs_numeric_progress_close(fs, &progress,
2780 printf("%s\n",error_message(retval));
2782 ext2fs_numeric_progress_close(fs, &progress,
2788 static void fix_cluster_bg_counts(ext2_filsys fs)
2790 blk64_t block, num_blocks, last_block, next;
2791 blk64_t tot_free = 0;
2796 num_blocks = ext2fs_blocks_count(fs->super);
2797 last_block = ext2fs_group_last_block2(fs, group);
2798 block = fs->super->s_first_data_block;
2799 while (block < num_blocks) {
2800 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
2801 block, last_block, &next);
2805 block = last_block + 1;
2809 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
2810 block, last_block, &next);
2812 next = last_block + 1;
2813 grp_free += EXT2FS_NUM_B2C(fs, next - block);
2814 tot_free += next - block;
2817 if (block > last_block) {
2819 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2820 ext2fs_group_desc_csum_set(fs, group);
2823 last_block = ext2fs_group_last_block2(fs, group);
2826 ext2fs_free_blocks_count_set(fs->super, tot_free);
2829 static int create_quota_inodes(ext2_filsys fs)
2834 retval = quota_init_context(&qctx, fs, quotatype_bits);
2836 com_err(program_name, retval,
2837 _("while initializing quota context"));
2840 quota_compute_usage(qctx);
2841 retval = quota_write_inode(qctx, quotatype_bits);
2843 com_err(program_name, retval,
2844 _("while writing quota inodes"));
2847 quota_release_context(&qctx);
2852 static errcode_t set_error_behavior(ext2_filsys fs)
2855 short errors = fs->super->s_errors;
2857 arg = get_string_from_profile(fs_types, "errors", NULL);
2861 if (strcmp(arg, "continue") == 0)
2862 errors = EXT2_ERRORS_CONTINUE;
2863 else if (strcmp(arg, "remount-ro") == 0)
2864 errors = EXT2_ERRORS_RO;
2865 else if (strcmp(arg, "panic") == 0)
2866 errors = EXT2_ERRORS_PANIC;
2868 com_err(program_name, 0,
2869 _("bad error behavior in profile - %s"),
2872 return EXT2_ET_INVALID_ARGUMENT;
2877 if (errors_behavior)
2878 errors = errors_behavior;
2880 fs->super->s_errors = errors;
2884 int main (int argc, char *argv[])
2886 errcode_t retval = 0;
2888 badblocks_list bb_list = 0;
2889 badblocks_iterate bb_iter;
2891 struct ext2fs_journal_params jparams = {0};
2892 unsigned int i, checkinterval;
2898 char opt_string[40];
2900 int itable_zeroed = 0;
2904 setlocale(LC_MESSAGES, "");
2905 setlocale(LC_CTYPE, "");
2906 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2907 textdomain(NLS_CAT_NAME);
2908 set_com_err_gettext(gettext);
2912 #ifdef CONFIG_TESTIO_DEBUG
2913 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2914 io_ptr = test_io_manager;
2915 test_io_backing_manager = unix_io_manager;
2918 io_ptr = unix_io_manager;
2920 if (undo_file != NULL || should_do_undo(device_name)) {
2921 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2927 * Initialize the superblock....
2929 flags = EXT2_FLAG_EXCLUSIVE;
2931 flags |= EXT2_FLAG_DIRECT_IO;
2932 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2935 flags |= EXT2_FLAG_64BITS;
2937 * By default, we print how many inode tables or block groups
2938 * or whatever we've written so far. The quiet flag disables
2939 * this, along with a lot of other output.
2942 flags |= EXT2_FLAG_PRINT_PROGRESS;
2943 if (android_sparse_file) {
2944 char *android_sparse_params = malloc(strlen(device_name) + 48);
2946 if (!android_sparse_params) {
2947 com_err(program_name, ENOMEM, "%s",
2948 _("in malloc for android_sparse_params"));
2951 sprintf(android_sparse_params, "(%s):%u:%u",
2952 device_name, fs_param.s_blocks_count,
2953 1024 << fs_param.s_log_block_size);
2954 retval = ext2fs_initialize(android_sparse_params, flags,
2955 &fs_param, sparse_io_manager, &fs);
2956 free(android_sparse_params);
2958 retval = ext2fs_initialize(device_name, flags, &fs_param,
2961 com_err(device_name, retval, "%s",
2962 _("while setting up superblock"));
2965 fs->progress_ops = &ext2fs_numeric_progress_ops;
2967 /* Set the error behavior */
2968 retval = set_error_behavior(fs);
2972 /* Check the user's mkfs options for metadata checksumming */
2974 !ext2fs_has_feature_journal_dev(fs->super) &&
2975 ext2fs_has_feature_metadata_csum(fs->super)) {
2976 if (!ext2fs_has_feature_extents(fs->super))
2978 _("Extents are not enabled. The file extent "
2979 "tree can be checksummed, whereas block maps "
2980 "cannot. Not enabling extents reduces the "
2981 "coverage of metadata checksumming. "
2982 "Pass -O extents to rectify.\n"));
2983 if (!ext2fs_has_feature_64bit(fs->super))
2985 _("64-bit filesystem support is not enabled. "
2986 "The larger fields afforded by this feature "
2987 "enable full-strength checksumming. "
2988 "Pass -O 64bit to rectify.\n"));
2991 if (ext2fs_has_feature_csum_seed(fs->super) &&
2992 !ext2fs_has_feature_metadata_csum(fs->super)) {
2993 printf("%s", _("The metadata_csum_seed feature "
2994 "requires the metadata_csum feature.\n"));
2998 if (ext2fs_has_feature_dirdata(fs->super)) {
2999 if (ext2fs_has_feature_inline_data(fs->super)) {
3000 printf("%s", _("The dirdata feature can not enabled "
3001 "with inline data feature.\n"));
3004 if (ext2fs_has_feature_casefold(fs->super)) {
3005 printf("%s", _("The dirdata feature can not enabled "
3006 "with casefold feature.\n"));
3011 /* Calculate journal blocks */
3012 if (!journal_device && ((journal_size) ||
3013 ext2fs_has_feature_journal(&fs_param)))
3014 figure_journal_size(&jparams, journal_size, journal_fc_size, fs);
3016 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
3017 32768 : fs->blocksize * 8);
3018 io_channel_set_options(fs->io, opt_string);
3020 sprintf(opt_string, "offset=%llu", (unsigned long long) offset);
3021 io_channel_set_options(fs->io, opt_string);
3024 /* Can't undo discard ... */
3025 if (!noaction && discard && dev_size && (io_ptr != undo_io_manager)) {
3026 retval = mke2fs_discard_device(fs);
3027 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
3030 _("Discard succeeded and will return "
3031 "0s - skipping inode table wipe\n"));
3032 lazy_itable_init = 1;
3038 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
3039 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
3041 if (ext2fs_has_feature_flex_bg(&fs_param) ||
3042 ext2fs_has_feature_huge_file(&fs_param) ||
3043 ext2fs_has_feature_gdt_csum(&fs_param) ||
3044 ext2fs_has_feature_dir_nlink(&fs_param) ||
3045 ext2fs_has_feature_metadata_csum(&fs_param) ||
3046 ext2fs_has_feature_extra_isize(&fs_param))
3047 fs->super->s_kbytes_written = 1;
3050 * Wipe out the old on-disk superblock
3053 zap_sector(fs, 2, 6);
3056 * Parse or generate a UUID for the filesystem
3059 if ((strcasecmp(fs_uuid, "null") == 0) ||
3060 (strcasecmp(fs_uuid, "clear") == 0)) {
3061 uuid_clear(fs->super->s_uuid);
3062 } else if (strcasecmp(fs_uuid, "time") == 0) {
3063 uuid_generate_time(fs->super->s_uuid);
3064 } else if (strcasecmp(fs_uuid, "random") == 0) {
3065 uuid_generate(fs->super->s_uuid);
3066 } else if (uuid_parse(fs_uuid, fs->super->s_uuid) != 0) {
3067 com_err(device_name, 0, "could not parse UUID: %s\n",
3072 uuid_generate(fs->super->s_uuid);
3074 if (ext2fs_has_feature_csum_seed(fs->super))
3075 fs->super->s_checksum_seed = ext2fs_crc32c_le(~0,
3076 fs->super->s_uuid, sizeof(fs->super->s_uuid));
3078 ext2fs_init_csum_seed(fs);
3081 * Initialize the directory index variables
3083 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
3085 hash_alg = e2p_string2hash(hash_alg_str);
3087 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
3090 if (memcmp(fs_param.s_hash_seed, zero_buf,
3091 sizeof(fs_param.s_hash_seed)) != 0) {
3092 memcpy(fs->super->s_hash_seed, fs_param.s_hash_seed,
3093 sizeof(fs->super->s_hash_seed));
3095 uuid_generate((unsigned char *) fs->super->s_hash_seed);
3098 * Periodic checks can be enabled/disabled via config file.
3099 * Note we override the kernel include file's idea of what the default
3100 * check interval (never) should be. It's a good idea to check at
3101 * least *occasionally*, specially since servers will never rarely get
3102 * to reboot, since Linux is so robust these days. :-)
3104 * 180 days (six months) seems like a good value.
3106 #ifdef EXT2_DFL_CHECKINTERVAL
3107 #undef EXT2_DFL_CHECKINTERVAL
3109 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
3111 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
3112 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
3113 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
3115 * Add "jitter" to the superblock's check interval so that we
3116 * don't check all the filesystems at the same time. We use a
3117 * kludgy hack of using the UUID to derive a random jitter value
3119 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
3120 val += fs->super->s_uuid[i];
3121 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
3123 fs->super->s_max_mnt_count = -1;
3126 * Override the creator OS, if applicable
3128 if (creator_os && !set_os(fs->super, creator_os)) {
3129 com_err (program_name, 0, _("unknown os - %s"), creator_os);
3134 * For the Hurd, we will turn off filetype since it doesn't
3137 if (fs->super->s_creator_os == EXT2_OS_HURD)
3138 ext2fs_clear_feature_filetype(fs->super);
3141 * Set the volume label...
3144 memset(fs->super->s_volume_name, 0,
3145 sizeof(fs->super->s_volume_name));
3146 strncpy((char *) fs->super->s_volume_name, volume_label,
3147 sizeof(fs->super->s_volume_name));
3151 * Set the last mount directory
3154 memset(fs->super->s_last_mounted, 0,
3155 sizeof(fs->super->s_last_mounted));
3156 strncpy((char *) fs->super->s_last_mounted, mount_dir,
3157 sizeof(fs->super->s_last_mounted));
3160 /* Set current default encryption algorithms for data and
3161 * filename encryption */
3162 if (ext2fs_has_feature_encrypt(fs->super)) {
3163 fs->super->s_encrypt_algos[0] =
3164 EXT4_ENCRYPTION_MODE_AES_256_XTS;
3165 fs->super->s_encrypt_algos[1] =
3166 EXT4_ENCRYPTION_MODE_AES_256_CTS;
3169 if (ext2fs_has_feature_metadata_csum(fs->super))
3170 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
3172 if (!quiet || noaction)
3178 if (ext2fs_has_feature_journal_dev(fs->super)) {
3179 create_journal_dev(fs);
3181 exit(ext2fs_close_free(&fs) ? 1 : 0);
3184 if (bad_blocks_filename)
3185 read_bb_file(fs, &bb_list, bad_blocks_filename);
3187 test_disk(fs, &bb_list);
3188 handle_bad_blocks(fs, bb_list);
3190 fs->stride = fs_stride = fs->super->s_raid_stride;
3192 printf("%s", _("Allocating group tables: "));
3193 if (ext2fs_has_feature_flex_bg(fs->super) &&
3195 retval = packed_allocate_tables(fs);
3197 retval = ext2fs_allocate_tables(fs);
3199 com_err(program_name, retval, "%s",
3200 _("while trying to allocate filesystem tables"));
3204 printf("%s", _("done \n"));
3207 * Unmark bad blocks to calculate overhead, because metadata
3208 * blocks and bad blocks can land on the same allocation cluster.
3211 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
3214 com_err("ext2fs_badblocks_list_iterate_begin", retval,
3215 "%s", _("while unmarking bad blocks"));
3218 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
3219 ext2fs_unmark_block_bitmap2(fs->block_map, blk);
3220 ext2fs_badblocks_list_iterate_end(bb_iter);
3223 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
3225 com_err(program_name, retval, "%s",
3226 _("\n\twhile converting subcluster bitmap"));
3230 retval = ext2fs_count_used_clusters(fs, fs->super->s_first_data_block,
3231 ext2fs_blocks_count(fs->super) - 1,
3234 com_err(program_name, retval, "%s",
3235 _("while calculating overhead"));
3240 retval = ext2fs_badblocks_list_iterate_begin(bb_list,
3243 com_err("ext2fs_badblocks_list_iterate_begin", retval,
3244 "%s", _("while marking bad blocks as used"));
3247 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
3248 ext2fs_mark_block_bitmap2(fs->block_map, blk);
3249 ext2fs_badblocks_list_iterate_end(bb_iter);
3253 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
3254 printf(_("%s may be further corrupted by superblock rewrite\n"),
3257 proceed_question(proceed_delay);
3258 fs->super->s_state |= EXT2_ERROR_FS;
3259 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
3261 * The command "mke2fs -S" is used to recover
3262 * corrupted file systems, so do not mark any of the
3263 * inodes as unused; we want e2fsck to consider all
3264 * inodes as potentially containing recoverable data.
3266 if (ext2fs_has_group_desc_csum(fs)) {
3267 for (i = 0; i < fs->group_desc_count; i++)
3268 ext2fs_bg_itable_unused_set(fs, i, 0);
3271 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
3272 blk64_t rsv = 65536 / fs->blocksize;
3273 blk64_t blocks = ext2fs_blocks_count(fs->super);
3277 #ifdef ZAP_BOOTBLOCK
3278 zap_sector(fs, 0, 2);
3282 * Wipe out any old MD RAID (or other) metadata at the end
3283 * of the device. This will also verify that the device is
3284 * as large as we think. Be careful with very small devices.
3286 start = (blocks & ~(rsv - 1));
3290 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
3294 com_err(program_name, retval,
3295 _("while zeroing block %llu at end of filesystem"),
3296 (unsigned long long) ret_blk);
3298 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
3299 create_root_dir(fs);
3300 create_lost_and_found(fs);
3302 create_bad_block_inode(fs, bb_list);
3303 if (ext2fs_has_feature_resize_inode(fs->super)) {
3304 retval = ext2fs_create_resize_inode(fs);
3306 com_err("ext2fs_create_resize_inode", retval,
3308 _("while reserving blocks for online resize"));
3314 if (journal_device) {
3317 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
3319 proceed_question(proceed_delay);
3320 check_mount(journal_device, force, _("journal"));
3322 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
3323 EXT2_FLAG_JOURNAL_DEV_OK, 0,
3324 fs->blocksize, unix_io_manager, &jfs);
3326 com_err(program_name, retval,
3327 _("while trying to open journal device %s\n"),
3332 printf(_("Adding journal to device %s: "),
3336 retval = ext2fs_add_journal_device(fs, jfs);
3338 com_err (program_name, retval,
3339 _("\n\twhile trying to add journal to device %s"),
3344 printf("%s", _("done\n"));
3345 ext2fs_close_free(&jfs);
3346 free(journal_device);
3347 } else if ((journal_size) ||
3348 ext2fs_has_feature_journal(&fs_param)) {
3349 overhead += EXT2FS_NUM_B2C(fs, jparams.num_journal_blocks + jparams.num_fc_blocks);
3351 printf("%s", _("Skipping journal creation in super-only mode\n"));
3352 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
3356 if (!jparams.num_journal_blocks) {
3357 ext2fs_clear_feature_journal(fs->super);
3361 printf(_("Creating journal (%u blocks): "),
3362 jparams.num_journal_blocks + jparams.num_fc_blocks);
3365 retval = ext2fs_add_journal_inode3(fs, &jparams,
3369 com_err(program_name, retval, "%s",
3370 _("\n\twhile trying to create journal"));
3374 printf("%s", _("done\n"));
3378 ext2fs_has_feature_mmp(fs->super)) {
3379 retval = ext2fs_mmp_init(fs);
3381 fprintf(stderr, "%s",
3382 _("\nError while enabling multiple "
3383 "mount protection feature."));
3387 printf(_("Multiple mount protection is enabled "
3388 "with update interval %d seconds.\n"),
3389 fs->super->s_mmp_update_interval);
3392 overhead += fs->super->s_first_data_block;
3394 fs->super->s_overhead_clusters = overhead;
3396 if (ext2fs_has_feature_bigalloc(&fs_param))
3397 fix_cluster_bg_counts(fs);
3398 if (ext2fs_has_feature_quota(&fs_param))
3399 create_quota_inodes(fs);
3401 retval = mk_hugefiles(fs, device_name);
3403 com_err(program_name, retval, "while creating huge files");
3404 /* Copy files from the specified directory */
3407 printf("%s", _("Copying files into the device: "));
3409 retval = populate_fs(fs, EXT2_ROOT_INO, src_root_dir,
3412 com_err(program_name, retval, "%s",
3413 _("while populating file system"));
3416 printf("%s", _("done\n"));
3420 printf("%s", _("Writing superblocks and "
3421 "filesystem accounting information: "));
3422 checkinterval = fs->super->s_checkinterval;
3423 max_mnt_count = fs->super->s_max_mnt_count;
3424 retval = ext2fs_close_free(&fs);
3426 com_err(program_name, retval, "%s",
3427 _("while writing out and closing file system"));
3429 } else if (!quiet) {
3430 printf("%s", _("done\n\n"));
3431 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
3432 print_check_message(max_mnt_count, checkinterval);
3435 remove_error_table(&et_ext2_error_table);
3436 remove_error_table(&et_prof_error_table);
3437 profile_release(profile);
3438 for (i=0; fs_types[i]; i++)