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 */
29 #include <sys/utsname.h>
46 #include <sys/ioctl.h>
47 #include <sys/types.h>
51 #include <blkid/blkid.h>
53 #include "ext2fs/ext2_fs.h"
54 #include "ext2fs/ext2fsP.h"
55 #include "et/com_err.h"
56 #include "uuid/uuid.h"
58 #include "ext2fs/ext2fs.h"
62 #include "../version.h"
63 #include "nls-enable.h"
64 #include "quota/mkquota.h"
66 #define STRIDE_LENGTH 8
68 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
74 #define DISCARD_STEP_MB (2048)
76 extern int isatty(int);
77 extern FILE *fpopen(const char *cmd, const char *mode);
79 static const char * program_name = "mke2fs";
80 static const char * device_name /* = NULL */;
82 /* Command line options */
86 static int super_only;
87 static int discard = 1; /* attempt to discard device before fs creation */
91 static uid_t root_uid;
92 static gid_t root_gid;
95 static int lazy_itable_init;
96 static char *bad_blocks_filename = NULL;
97 static __u32 fs_stride;
98 static int quotatype = -1; /* Initialize both user and group quotas by default */
101 static struct ext2_super_block fs_param;
102 static char *fs_uuid = NULL;
103 static char *creator_os;
104 static char *volume_label;
105 static char *mount_dir;
106 char *journal_device;
107 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
108 static char **fs_types;
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]\n"
121 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
122 "\t[-g blocks-per-group] [-L volume-label] "
123 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
124 "[-r fs-revision] [-E extended-option[,...]]\n"
125 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
126 "[-jnqvDFKSV] device [blocks-count]\n"),
131 static int int_log2(unsigned long long arg)
143 static int int_log10(unsigned long long arg)
153 static int parse_version_number(const char *s)
155 int major, minor, rev;
161 major = strtol(cp, &endptr, 10);
162 if (cp == endptr || *endptr != '.')
165 minor = strtol(cp, &endptr, 10);
166 if (cp == endptr || *endptr != '.')
169 rev = strtol(cp, &endptr, 10);
172 return ((((major * 256) + minor) * 256) + rev);
177 * Helper function for read_bb_file and test_disk
179 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
181 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
186 * Reads the bad blocks list from a file
188 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
189 const char *bad_blocks_file)
194 f = fopen(bad_blocks_file, "r");
196 com_err("read_bad_blocks_file", errno,
197 _("while trying to open %s"), bad_blocks_file);
200 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
203 com_err("ext2fs_read_bb_FILE", retval, "%s",
204 _("while reading in list of bad blocks from file"));
210 * Runs the badblocks program to test the disk
212 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
218 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
219 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
220 fs->device_name, ext2fs_blocks_count(fs->super)-1);
222 printf(_("Running command: %s\n"), buf);
225 com_err("popen", errno,
226 _("while trying to run '%s'"), buf);
229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
232 com_err("ext2fs_read_bb_FILE", retval, "%s",
233 _("while processing list of bad blocks from program"));
238 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
242 unsigned must_be_good;
244 badblocks_iterate bb_iter;
254 * The primary superblock and group descriptors *must* be
255 * good; if not, abort.
257 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
258 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
259 if (ext2fs_badblocks_list_test(bb_list, i)) {
260 fprintf(stderr, _("Block %d in primary "
261 "superblock/group descriptor area bad.\n"), i);
262 fprintf(stderr, _("Blocks %u through %u must be good "
263 "in order to build a filesystem.\n"),
264 fs->super->s_first_data_block, must_be_good);
265 fputs(_("Aborting....\n"), stderr);
271 * See if any of the bad blocks are showing up in the backup
272 * superblocks and/or group descriptors. If so, issue a
273 * warning and adjust the block counts appropriately.
275 group_block = fs->super->s_first_data_block +
276 fs->super->s_blocks_per_group;
278 for (i = 1; i < fs->group_desc_count; i++) {
280 for (j=0; j < fs->desc_blocks+1; j++) {
281 if (ext2fs_badblocks_list_test(bb_list,
285 _("Warning: the backup superblock/group descriptors at block %u contain\n"
289 group = ext2fs_group_of_blk2(fs, group_block+j);
290 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
291 ext2fs_group_desc_csum_set(fs, group);
292 ext2fs_free_blocks_count_add(fs->super, 1);
295 group_block += fs->super->s_blocks_per_group;
299 * Mark all the bad blocks as used...
301 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
303 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
304 _("while marking bad blocks as used"));
307 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
308 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
309 ext2fs_badblocks_list_iterate_end(bb_iter);
312 static void write_reserved_inodes(ext2_filsys fs)
316 struct ext2_inode *inode;
318 retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
320 com_err("inode_init", retval,
321 "while allocating memory");
324 bzero(inode, EXT2_INODE_SIZE(fs->super));
326 for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++)
327 ext2fs_write_inode_full(fs, ino, inode,
328 EXT2_INODE_SIZE(fs->super));
330 ext2fs_free_mem(&inode);
333 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
339 struct ext2fs_numeric_progress_struct progress;
341 ext2fs_numeric_progress_init(fs, &progress,
342 _("Writing inode tables: "),
343 fs->group_desc_count);
345 for (i = 0; i < fs->group_desc_count; i++) {
346 ext2fs_numeric_progress_update(fs, &progress, i);
348 blk = ext2fs_inode_table_loc(fs, i);
349 num = fs->inode_blocks_per_group;
352 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
353 ext2fs_bg_itable_unused(fs, i)) *
354 EXT2_INODE_SIZE(fs->super),
355 EXT2_BLOCK_SIZE(fs->super));
356 if (!lazy_flag || itable_zeroed) {
357 /* The kernel doesn't need to zero the itable blocks */
358 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
359 ext2fs_group_desc_csum_set(fs, i);
361 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
363 fprintf(stderr, _("\nCould not write %d "
364 "blocks in inode table starting at %llu: %s\n"),
365 num, blk, error_message(retval));
369 if (sync_kludge == 1)
371 else if ((i % sync_kludge) == 0)
375 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
376 ext2fs_numeric_progress_close(fs, &progress,
379 /* Reserved inodes must always have correct checksums */
380 if (fs->super->s_creator_os == EXT2_OS_LINUX &&
381 fs->super->s_feature_ro_compat &
382 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
383 write_reserved_inodes(fs);
386 static void create_root_dir(ext2_filsys fs)
389 struct ext2_inode inode;
391 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
393 com_err("ext2fs_mkdir", retval, "%s",
394 _("while creating root dir"));
397 if (root_uid != 0 || root_gid != 0) {
398 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
400 com_err("ext2fs_read_inode", retval, "%s",
401 _("while reading root inode"));
405 inode.i_uid = root_uid;
406 ext2fs_set_i_uid_high(inode, root_uid >> 16);
407 inode.i_gid = root_gid;
408 ext2fs_set_i_gid_high(inode, root_gid >> 16);
410 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
412 com_err("ext2fs_write_inode", retval, "%s",
413 _("while setting root inode ownership"));
419 static void create_lost_and_found(ext2_filsys fs)
421 unsigned int lpf_size = 0;
424 const char *name = "lost+found";
428 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
430 com_err("ext2fs_mkdir", retval, "%s",
431 _("while creating /lost+found"));
435 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
437 com_err("ext2_lookup", retval, "%s",
438 _("while looking up /lost+found"));
442 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
443 /* Ensure that lost+found is at least 2 blocks, so we always
444 * test large empty blocks for big-block filesystems. */
445 if ((lpf_size += fs->blocksize) >= 16*1024 &&
446 lpf_size >= 2 * fs->blocksize)
448 retval = ext2fs_expand_dir(fs, ino);
450 com_err("ext2fs_expand_dir", retval, "%s",
451 _("while expanding /lost+found"));
457 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
461 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
462 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
463 retval = ext2fs_update_bb_inode(fs, bb_list);
465 com_err("ext2fs_update_bb_inode", retval, "%s",
466 _("while setting bad block inode"));
472 static void reserve_inodes(ext2_filsys fs)
476 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
477 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
478 ext2fs_mark_ib_dirty(fs);
481 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
482 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
483 #define BSD_LABEL_OFFSET 64
485 static void zap_sector(ext2_filsys fs, int sect, int nsect)
491 buf = malloc(512*nsect);
493 printf(_("Out of memory erasing sectors %d-%d\n"),
494 sect, sect + nsect - 1);
499 /* Check for a BSD disklabel, and don't erase it if so */
500 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
503 _("Warning: could not read block 0: %s\n"),
504 error_message(retval));
506 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
507 if ((*magic == BSD_DISKMAGIC) ||
508 (*magic == BSD_MAGICDISK))
513 memset(buf, 0, 512*nsect);
514 io_channel_set_blksize(fs->io, 512);
515 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
516 io_channel_set_blksize(fs->io, fs->blocksize);
519 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
520 sect, error_message(retval));
523 static void create_journal_dev(ext2_filsys fs)
525 struct ext2fs_numeric_progress_struct progress;
528 blk64_t blk, err_blk;
529 int c, count, err_count;
531 retval = ext2fs_create_journal_superblock(fs,
532 ext2fs_blocks_count(fs->super), 0, &buf);
534 com_err("create_journal_dev", retval, "%s",
535 _("while initializing journal superblock"));
539 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
540 goto write_superblock;
542 ext2fs_numeric_progress_init(fs, &progress,
543 _("Zeroing journal device: "),
544 ext2fs_blocks_count(fs->super));
546 count = ext2fs_blocks_count(fs->super);
552 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
554 com_err("create_journal_dev", retval,
555 _("while zeroing journal device "
556 "(block %llu, count %d)"),
562 ext2fs_numeric_progress_update(fs, &progress, blk);
564 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
566 ext2fs_numeric_progress_close(fs, &progress, NULL);
568 retval = io_channel_write_blk64(fs->io,
569 fs->super->s_first_data_block+1,
572 com_err("create_journal_dev", retval, "%s",
573 _("while writing journal superblock"));
578 static void show_stats(ext2_filsys fs)
580 struct ext2_super_block *s = fs->super;
587 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
588 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
589 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
591 memset(buf, 0, sizeof(buf));
592 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
593 printf(_("Filesystem label=%s\n"), buf);
594 os = e2p_os2string(fs->super->s_creator_os);
596 printf(_("OS type: %s\n"), os);
598 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
599 s->s_log_block_size);
600 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
601 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
602 printf(_("Cluster size=%u (log=%u)\n"),
603 fs->blocksize << fs->cluster_ratio_bits,
604 s->s_log_cluster_size);
606 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
607 s->s_log_cluster_size);
608 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
609 s->s_raid_stride, s->s_raid_stripe_width);
610 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
611 ext2fs_blocks_count(s));
612 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
613 ext2fs_r_blocks_count(s),
614 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
615 printf(_("First data block=%u\n"), s->s_first_data_block);
616 if (root_uid != 0 || root_gid != 0)
617 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
618 if (s->s_reserved_gdt_blocks)
619 printf(_("Maximum filesystem blocks=%lu\n"),
620 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
621 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
622 if (fs->group_desc_count > 1)
623 printf(_("%u block groups\n"), fs->group_desc_count);
625 printf(_("%u block group\n"), fs->group_desc_count);
626 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
627 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
628 printf(_("%u blocks per group, %u clusters per group\n"),
629 s->s_blocks_per_group, s->s_clusters_per_group);
631 printf(_("%u blocks per group, %u fragments per group\n"),
632 s->s_blocks_per_group, s->s_clusters_per_group);
633 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
635 if (fs->group_desc_count == 1) {
640 printf("%s", _("Superblock backups stored on blocks: "));
641 group_block = s->s_first_data_block;
643 for (i = 1; i < fs->group_desc_count; i++) {
644 group_block += s->s_blocks_per_group;
645 if (!ext2fs_bg_has_super(fs, i))
649 need = int_log10(group_block) + 2;
650 if (need > col_left) {
655 printf("%llu", group_block);
661 * Returns true if making a file system for the Hurd, else 0
663 static int for_hurd(const char *os)
673 return (atoi(os) == EXT2_OS_HURD);
674 return (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0);
678 * Set the S_CREATOR_OS field. Return true if OS is known,
681 static int set_os(struct ext2_super_block *sb, char *os)
684 sb->s_creator_os = atoi (os);
685 else if (strcasecmp(os, "linux") == 0)
686 sb->s_creator_os = EXT2_OS_LINUX;
687 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
688 sb->s_creator_os = EXT2_OS_HURD;
689 else if (strcasecmp(os, "freebsd") == 0)
690 sb->s_creator_os = EXT2_OS_FREEBSD;
691 else if (strcasecmp(os, "lites") == 0)
692 sb->s_creator_os = EXT2_OS_LITES;
698 #define PATH_SET "PATH=/sbin"
700 static void parse_extended_opts(struct ext2_super_block *param,
703 char *buf, *token, *next, *p, *arg, *badopt = 0;
710 fprintf(stderr, "%s",
711 _("Couldn't allocate memory to parse options!\n"));
715 for (token = buf; token && *token; token = next) {
716 p = strchr(token, ',');
722 arg = strchr(token, '=');
727 if (strcmp(token, "desc-size") == 0 ||
728 strcmp(token, "desc_size") == 0) {
731 if (!(fs_param.s_feature_incompat &
732 EXT4_FEATURE_INCOMPAT_64BIT)) {
734 _("%s requires '-O 64bit'\n"), token);
738 if (param->s_reserved_gdt_blocks != 0) {
740 _("'%s' must be before 'resize=%u'\n"),
741 token, param->s_reserved_gdt_blocks);
750 desc_size = strtoul(arg, &p, 0);
751 if (*p || (desc_size & (desc_size - 1))) {
753 _("Invalid desc_size: '%s'\n"), arg);
757 param->s_desc_size = desc_size;
758 } else if (strcmp(token, "offset") == 0) {
764 offset = strtoull(arg, &p, 0);
766 fprintf(stderr, _("Invalid offset: %s\n"),
771 } else if (strcmp(token, "mmp_update_interval") == 0) {
777 param->s_mmp_update_interval = strtoul(arg, &p, 0);
780 _("Invalid mmp_update_interval: %s\n"),
785 } else if (strcmp(token, "stride") == 0) {
791 param->s_raid_stride = strtoul(arg, &p, 0);
794 _("Invalid stride parameter: %s\n"),
799 } else if (strcmp(token, "stripe-width") == 0 ||
800 strcmp(token, "stripe_width") == 0) {
806 param->s_raid_stripe_width = strtoul(arg, &p, 0);
809 _("Invalid stripe-width parameter: %s\n"),
814 } else if (!strcmp(token, "resize")) {
816 unsigned long bpg, rsv_groups;
817 unsigned long group_desc_count, desc_blocks;
818 unsigned int gdpb, blocksize;
827 resize = parse_num_blocks2(arg,
828 param->s_log_block_size);
832 _("Invalid resize parameter: %s\n"),
837 if (resize <= ext2fs_blocks_count(param)) {
838 fprintf(stderr, "%s",
839 _("The resize maximum must be greater "
840 "than the filesystem size.\n"));
845 blocksize = EXT2_BLOCK_SIZE(param);
846 bpg = param->s_blocks_per_group;
849 gdpb = EXT2_DESC_PER_BLOCK(param);
850 group_desc_count = (__u32) ext2fs_div64_ceil(
851 ext2fs_blocks_count(param), bpg);
852 desc_blocks = (group_desc_count +
854 rsv_groups = ext2fs_div64_ceil(resize, bpg);
855 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
857 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
858 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
861 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
862 fprintf(stderr, "%s",
863 _("On-line resizing not supported with revision 0 filesystems\n"));
867 param->s_feature_compat |=
868 EXT2_FEATURE_COMPAT_RESIZE_INODE;
870 param->s_reserved_gdt_blocks = rsv_gdb;
872 } else if (!strcmp(token, "test_fs")) {
873 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
874 } else if (!strcmp(token, "lazy_itable_init")) {
876 lazy_itable_init = strtoul(arg, &p, 0);
878 lazy_itable_init = 1;
879 } else if (!strcmp(token, "lazy_journal_init")) {
881 journal_flags |= strtoul(arg, &p, 0) ?
882 EXT2_MKJOURNAL_LAZYINIT : 0;
884 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
885 } else if (!strcmp(token, "root_owner")) {
887 root_uid = strtoul(arg, &p, 0);
890 _("Invalid root_owner: '%s'\n"),
896 root_gid = strtoul(p, &p, 0);
899 _("Invalid root_owner: '%s'\n"),
908 } else if (!strcmp(token, "discard")) {
910 } else if (!strcmp(token, "nodiscard")) {
912 } else if (!strcmp(token, "quotatype")) {
918 if (!strncmp(arg, "usr", 3)) {
920 } else if (!strncmp(arg, "grp", 3)) {
924 _("Invalid quotatype parameter: %s\n"),
935 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
936 "Extended options are separated by commas, "
937 "and may take an argument which\n"
938 "\tis set off by an equals ('=') sign.\n\n"
939 "Valid extended options are:\n"
940 "\tmmp_update_interval=<interval>\n"
941 "\tstride=<RAID per-disk data chunk in blocks>\n"
942 "\tstripe-width=<RAID stride * data disks in blocks>\n"
943 "\toffset=<offset to create the file system>\n"
944 "\tresize=<resize maximum size in blocks>\n"
945 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
946 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
947 "\troot_uid=<uid of root directory>\n"
948 "\troot_gid=<gid of root directory>\n"
952 "\tquotatype=<usr OR grp>\n\n"),
953 badopt ? badopt : "");
957 if (param->s_raid_stride &&
958 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
959 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
960 "multiple of stride %u.\n\n"),
961 param->s_raid_stripe_width, param->s_raid_stride);
966 static __u32 ok_features[3] = {
968 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
969 EXT2_FEATURE_COMPAT_RESIZE_INODE |
970 EXT2_FEATURE_COMPAT_DIR_INDEX |
971 EXT2_FEATURE_COMPAT_EXT_ATTR,
973 EXT2_FEATURE_INCOMPAT_FILETYPE|
974 EXT3_FEATURE_INCOMPAT_EXTENTS|
975 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
976 EXT2_FEATURE_INCOMPAT_META_BG|
977 EXT4_FEATURE_INCOMPAT_FLEX_BG|
978 EXT4_FEATURE_INCOMPAT_MMP |
979 EXT4_FEATURE_INCOMPAT_64BIT,
981 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
982 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
983 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
984 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
985 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
986 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
987 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
989 EXT4_FEATURE_RO_COMPAT_QUOTA|
991 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
995 static void syntax_err_report(const char *filename, long err, int line_num)
998 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
999 filename, line_num, error_message(err));
1003 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
1005 static void edit_feature(const char *str, __u32 *compat_array)
1010 if (e2p_edit_feature(str, compat_array, ok_features)) {
1011 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1017 static void edit_mntopts(const char *str, __u32 *mntopts)
1022 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1023 fprintf(stderr, _("Invalid mount option set: %s\n"),
1035 static errcode_t init_list(struct str_list *sl)
1039 sl->list = malloc((sl->max+1) * sizeof(char *));
1046 static errcode_t push_string(struct str_list *sl, const char *str)
1050 if (sl->num >= sl->max) {
1052 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1055 sl->list = new_list;
1057 sl->list[sl->num] = malloc(strlen(str)+1);
1058 if (sl->list[sl->num] == 0)
1060 strcpy(sl->list[sl->num], str);
1062 sl->list[sl->num] = 0;
1066 static void print_str_list(char **list)
1070 for (cpp = list; *cpp; cpp++) {
1071 printf("'%s'", *cpp);
1073 fputs(", ", stdout);
1075 fputc('\n', stdout);
1079 * Return TRUE if the profile has the given subsection
1081 static int profile_has_subsection(profile_t prof, const char *section,
1082 const char *subsection)
1085 const char *names[4];
1090 names[1] = subsection;
1093 if (profile_iterator_create(prof, names,
1094 PROFILE_ITER_LIST_SECTION |
1095 PROFILE_ITER_RELATIONS_ONLY, &state))
1098 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1103 profile_iterator_free(&state);
1107 static char **parse_fs_type(const char *fs_type,
1108 const char *usage_types,
1109 struct ext2_super_block *sb,
1110 blk64_t fs_blocks_count,
1113 const char *ext_type = 0;
1115 char *profile_type = 0;
1117 const char *size_type;
1118 struct str_list list;
1119 unsigned long long meg;
1120 int is_hurd = for_hurd(creator_os);
1122 if (init_list(&list))
1129 else if (!strcmp(program_name, "mke3fs"))
1131 else if (!strcmp(program_name, "mke4fs"))
1133 else if (progname) {
1134 ext_type = strrchr(progname, '/');
1138 ext_type = progname;
1140 if (!strncmp(ext_type, "mkfs.", 5)) {
1142 if (ext_type[0] == 0)
1149 profile_get_string(profile, "defaults", "fs_type", 0,
1150 "ext2", &profile_type);
1151 ext_type = profile_type;
1152 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1157 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1158 strcmp(ext_type, "ext2")) {
1159 printf(_("\nYour mke2fs.conf file does not define the "
1160 "%s filesystem type.\n"), ext_type);
1161 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1162 !strcmp(ext_type, "ext4dev")) {
1163 printf("%s", _("You probably need to install an "
1164 "updated mke2fs.conf file.\n\n"));
1167 printf("%s", _("Aborting...\n"));
1172 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1173 if (fs_blocks_count < 3 * meg)
1174 size_type = "floppy";
1175 else if (fs_blocks_count < 512 * meg)
1176 size_type = "small";
1177 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1178 size_type = "default";
1179 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1185 usage_types = size_type;
1187 parse_str = malloc(strlen(usage_types)+1);
1193 strcpy(parse_str, usage_types);
1196 push_string(&list, ext_type);
1199 t = strchr(cp, ',');
1204 if (profile_has_subsection(profile, "fs_types", cp))
1205 push_string(&list, cp);
1206 else if (strcmp(cp, "default") != 0)
1208 _("\nWarning: the fs_type %s is not "
1209 "defined in mke2fs.conf\n\n"),
1220 push_string(&list, "hurd");
1224 static char *get_string_from_profile(char **types, const char *opt,
1225 const char *def_val)
1230 for (i=0; types[i]; i++);
1231 for (i-=1; i >=0 ; i--) {
1232 profile_get_string(profile, "fs_types", types[i],
1237 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1241 static int get_int_from_profile(char **types, const char *opt, int def_val)
1246 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1247 for (cpp = types; *cpp; cpp++)
1248 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1252 static double get_double_from_profile(char **types, const char *opt,
1258 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1259 for (cpp = types; *cpp; cpp++)
1260 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1264 static int get_bool_from_profile(char **types, const char *opt, int def_val)
1269 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1270 for (cpp = types; *cpp; cpp++)
1271 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1275 extern const char *mke2fs_default_profile;
1276 static const char *default_files[] = { "<default>", 0 };
1278 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1280 * Sets the geometry of a device (stripe/stride), and returns the
1281 * device's alignment offset, if any, or a negative error.
1283 static int get_device_geometry(const char *file,
1284 struct ext2_super_block *fs_param,
1291 unsigned long min_io;
1292 unsigned long opt_io;
1293 struct stat statbuf;
1295 /* Nothing to do for a regular file */
1296 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1299 pr = blkid_new_probe_from_filename(file);
1303 tp = blkid_probe_get_topology(pr);
1307 min_io = blkid_topology_get_minimum_io_size(tp);
1308 opt_io = blkid_topology_get_optimal_io_size(tp);
1309 blocksize = EXT2_BLOCK_SIZE(fs_param);
1310 if ((min_io == 0) && (psector_size > blocksize))
1311 min_io = psector_size;
1312 if ((opt_io == 0) && min_io)
1314 if ((opt_io == 0) && (psector_size > blocksize))
1315 opt_io = psector_size;
1317 /* setting stripe/stride to blocksize is pointless */
1318 if (min_io > blocksize)
1319 fs_param->s_raid_stride = min_io / blocksize;
1320 if (opt_io > blocksize)
1321 fs_param->s_raid_stripe_width = opt_io / blocksize;
1323 rc = blkid_topology_get_alignment_offset(tp);
1325 blkid_free_probe(pr);
1330 static void PRS(int argc, char *argv[])
1333 int cluster_size = 0;
1336 int inode_ratio = 0;
1338 unsigned long flex_bg_size = 0;
1339 double reserved_ratio = -1.0;
1340 int lsector_size = 0, psector_size = 0;
1341 int show_version_only = 0;
1342 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1344 char * oldpath = getenv("PATH");
1345 char * extended_opts = 0;
1347 char * usage_types = 0;
1350 * NOTE: A few words about fs_blocks_count and blocksize:
1352 * Initially, blocksize is set to zero, which implies 1024.
1353 * If -b is specified, blocksize is updated to the user's value.
1355 * Next, the device size or the user's "blocks" command line argument
1356 * is used to set fs_blocks_count; the units are blocksize.
1358 * Later, if blocksize hasn't been set and the profile specifies a
1359 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1360 * appropriately. Note the change in units!
1362 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1364 blk64_t fs_blocks_count = 0;
1369 int s_opt = -1, r_opt = -1;
1370 char *fs_features = 0;
1373 int pathlen = sizeof(PATH_SET) + 1;
1376 pathlen += strlen(oldpath);
1377 newpath = malloc(pathlen);
1379 fprintf(stderr, "%s",
1380 _("Couldn't allocate memory for new PATH.\n"));
1383 strcpy(newpath, PATH_SET);
1385 /* Update our PATH to include /sbin */
1387 strcat (newpath, ":");
1388 strcat (newpath, oldpath);
1392 tmp = getenv("MKE2FS_SYNC");
1394 sync_kludge = atoi(tmp);
1396 /* Determine the system page size if possible */
1398 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1399 #define _SC_PAGESIZE _SC_PAGE_SIZE
1402 sysval = sysconf(_SC_PAGESIZE);
1404 sys_page_size = sysval;
1405 #endif /* _SC_PAGESIZE */
1406 #endif /* HAVE_SYSCONF */
1408 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1410 profile_set_syntax_err_cb(syntax_err_report);
1411 retval = profile_init(config_fn, &profile);
1412 if (retval == ENOENT) {
1413 retval = profile_init(default_files, &profile);
1416 retval = profile_set_default(profile, mke2fs_default_profile);
1419 } else if (retval) {
1421 fprintf(stderr, _("Couldn't init profile successfully"
1422 " (error: %ld).\n"), retval);
1426 setbuf(stdout, NULL);
1427 setbuf(stderr, NULL);
1428 add_error_table(&et_ext2_error_table);
1429 add_error_table(&et_prof_error_table);
1430 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1431 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1438 linux_version_code = parse_version_number(ut.release);
1439 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1440 fs_param.s_rev_level = 0;
1443 if (argc && *argv) {
1444 program_name = get_progname(*argv);
1446 /* If called as mkfs.ext3, create a journal inode */
1447 if (!strcmp(program_name, "mkfs.ext3") ||
1448 !strcmp(program_name, "mke3fs"))
1452 while ((c = getopt (argc, argv,
1453 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1456 blocksize = parse_num_blocks2(optarg, -1);
1457 b = (blocksize > 0) ? blocksize : -blocksize;
1458 if (b < EXT2_MIN_BLOCK_SIZE ||
1459 b > EXT2_MAX_BLOCK_SIZE) {
1460 com_err(program_name, 0,
1461 _("invalid block size - %s"), optarg);
1464 if (blocksize > 4096)
1465 fprintf(stderr, _("Warning: blocksize %d not "
1466 "usable on most systems.\n"),
1469 fs_param.s_log_block_size =
1470 int_log2(blocksize >>
1471 EXT2_MIN_BLOCK_LOG_SIZE);
1473 case 'c': /* Check for bad blocks */
1477 cluster_size = parse_num_blocks2(optarg, -1);
1478 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1479 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1480 com_err(program_name, 0,
1481 _("invalid cluster size - %s"),
1490 com_err(program_name, 0, "%s",
1491 _("'-R' is deprecated, use '-E' instead"));
1494 extended_opts = optarg;
1500 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1502 com_err(program_name, 0, "%s",
1503 _("Illegal number for blocks per group"));
1506 if ((fs_param.s_blocks_per_group % 8) != 0) {
1507 com_err(program_name, 0, "%s",
1508 _("blocks per group must be multiple of 8"));
1513 flex_bg_size = strtoul(optarg, &tmp, 0);
1515 com_err(program_name, 0, "%s",
1516 _("Illegal number for flex_bg size"));
1519 if (flex_bg_size < 1 ||
1520 (flex_bg_size & (flex_bg_size-1)) != 0) {
1521 com_err(program_name, 0, "%s",
1522 _("flex_bg size must be a power of 2"));
1527 inode_ratio = strtoul(optarg, &tmp, 0);
1528 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1529 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1531 com_err(program_name, 0,
1532 _("invalid inode ratio %s (min %d/max %d)"),
1533 optarg, EXT2_MIN_BLOCK_SIZE,
1534 EXT2_MAX_BLOCK_SIZE * 1024);
1539 inode_size = strtoul(optarg, &tmp, 0);
1541 com_err(program_name, 0,
1542 _("invalid inode size - %s"), optarg);
1551 parse_journal_opts(optarg);
1554 fprintf(stderr, "%s",
1555 _("Warning: -K option is deprecated and "
1556 "should not be used anymore. Use "
1557 "\'-E nodiscard\' extended option "
1562 bad_blocks_filename = realloc(bad_blocks_filename,
1563 strlen(optarg) + 1);
1564 if (!bad_blocks_filename) {
1565 com_err(program_name, ENOMEM, "%s",
1566 _("in malloc for bad_blocks_filename"));
1569 strcpy(bad_blocks_filename, optarg);
1572 volume_label = optarg;
1575 reserved_ratio = strtod(optarg, &tmp);
1576 if ( *tmp || reserved_ratio > 50 ||
1577 reserved_ratio < 0) {
1578 com_err(program_name, 0,
1579 _("invalid reserved blocks percent - %s"),
1591 num_inodes = strtoul(optarg, &tmp, 0);
1593 com_err(program_name, 0,
1594 _("bad num inodes - %s"), optarg);
1599 creator_os = optarg;
1602 fs_features = optarg;
1608 r_opt = strtoul(optarg, &tmp, 0);
1610 com_err(program_name, 0,
1611 _("bad revision level - %s"), optarg);
1614 fs_param.s_rev_level = r_opt;
1616 case 's': /* deprecated */
1617 s_opt = atoi(optarg);
1624 com_err(program_name, 0, "%s",
1625 _("The -t option may only be used once"));
1628 fs_type = strdup(optarg);
1632 com_err(program_name, 0, "%s",
1633 _("The -T option may only be used once"));
1636 usage_types = strdup(optarg);
1645 /* Print version number and exit */
1646 show_version_only++;
1652 if ((optind == argc) && !show_version_only)
1654 device_name = argv[optind++];
1656 if (!quiet || show_version_only)
1657 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1660 if (show_version_only) {
1661 fprintf(stderr, _("\tUsing %s\n"),
1662 error_message(EXT2_ET_BASE));
1667 * If there's no blocksize specified and there is a journal
1668 * device, use it to figure out the blocksize
1670 if (blocksize <= 0 && journal_device) {
1674 #ifdef CONFIG_TESTIO_DEBUG
1675 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1676 io_ptr = test_io_manager;
1677 test_io_backing_manager = unix_io_manager;
1680 io_ptr = unix_io_manager;
1681 retval = ext2fs_open(journal_device,
1682 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1685 com_err(program_name, retval,
1686 _("while trying to open journal device %s\n"),
1690 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1691 com_err(program_name, 0,
1692 _("Journal dev blocksize (%d) smaller than "
1693 "minimum blocksize %d\n"), jfs->blocksize,
1697 blocksize = jfs->blocksize;
1698 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1699 fs_param.s_log_block_size =
1700 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1704 if (optind < argc) {
1705 fs_blocks_count = parse_num_blocks2(argv[optind++],
1706 fs_param.s_log_block_size);
1707 if (!fs_blocks_count) {
1708 com_err(program_name, 0,
1709 _("invalid blocks '%s' on device '%s'"),
1710 argv[optind - 1], device_name);
1718 check_plausibility(device_name);
1719 check_mount(device_name, force, _("filesystem"));
1721 /* Determine the size of the device (if possible) */
1722 if (noaction && fs_blocks_count) {
1723 dev_size = fs_blocks_count;
1726 retval = ext2fs_get_device_size2(device_name,
1727 EXT2_BLOCK_SIZE(&fs_param),
1730 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1731 com_err(program_name, retval, "%s",
1732 _("while trying to determine filesystem size"));
1735 if (!fs_blocks_count) {
1736 if (retval == EXT2_ET_UNIMPLEMENTED) {
1737 com_err(program_name, 0, "%s",
1738 _("Couldn't determine device size; you "
1739 "must specify\nthe size of the "
1743 if (dev_size == 0) {
1744 com_err(program_name, 0, "%s",
1745 _("Device size reported to be zero. "
1746 "Invalid partition specified, or\n\t"
1747 "partition table wasn't reread "
1748 "after running fdisk, due to\n\t"
1749 "a modified partition being busy "
1750 "and in use. You may need to reboot\n\t"
1751 "to re-read your partition table.\n"
1755 fs_blocks_count = dev_size;
1756 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1757 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1758 EXT2_BLOCK_SIZE(&fs_param))-1));
1760 } else if (!force && (fs_blocks_count > dev_size)) {
1761 com_err(program_name, 0, "%s",
1762 _("Filesystem larger than apparent device size."));
1767 profile_get_string(profile, "devices", device_name,
1768 "fs_type", 0, &fs_type);
1770 profile_get_string(profile, "devices", device_name,
1771 "usage_types", 0, &usage_types);
1774 * We have the file system (or device) size, so we can now
1775 * determine the appropriate file system types so the fs can
1776 * be appropriately configured.
1778 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1779 fs_blocks_count ? fs_blocks_count : dev_size,
1782 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1786 /* Figure out what features should be enabled */
1789 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1790 tmp = get_string_from_profile(fs_types, "base_features",
1791 "sparse_super,filetype,resize_inode,dir_index");
1792 edit_feature(tmp, &fs_param.s_feature_compat);
1795 /* And which mount options as well */
1796 tmp = get_string_from_profile(fs_types, "default_mntopts",
1798 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1802 for (cpp = fs_types; *cpp; cpp++) {
1804 profile_get_string(profile, "fs_types", *cpp,
1805 "features", "", &tmp);
1807 edit_feature(tmp, &fs_param.s_feature_compat);
1811 tmp = get_string_from_profile(fs_types, "default_features",
1814 /* Mask off features which aren't supported by the Hurd */
1815 if (for_hurd(creator_os)) {
1816 fs_param.s_feature_incompat &= ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1817 fs_param.s_feature_ro_compat &=
1818 ~(EXT4_FEATURE_RO_COMPAT_HUGE_FILE |
1819 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1821 edit_feature(fs_features ? fs_features : tmp,
1822 &fs_param.s_feature_compat);
1826 * If the user specified features incompatible with the Hurd, complain
1828 if (for_hurd(creator_os)) {
1829 if (fs_param.s_feature_incompat &
1830 EXT2_FEATURE_INCOMPAT_FILETYPE) {
1831 fprintf(stderr, _("The HURD does not support the "
1832 "filetype feature.\n"));
1835 if (fs_param.s_feature_ro_compat &
1836 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
1837 fprintf(stderr, _("The HURD does not support the "
1838 "huge_file feature.\n"));
1841 if (fs_param.s_feature_ro_compat &
1842 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
1843 fprintf(stderr, _("The HURD does not support the "
1844 "metadata_csum feature.\n"));
1849 /* Get the hardware sector sizes, if available */
1850 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1852 com_err(program_name, retval, "%s",
1853 _("while trying to determine hardware sector size"));
1856 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1858 com_err(program_name, retval, "%s",
1859 _("while trying to determine physical sector size"));
1863 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1865 lsector_size = atoi(tmp);
1866 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1868 psector_size = atoi(tmp);
1870 /* Older kernels may not have physical/logical distinction */
1872 psector_size = lsector_size;
1874 if (blocksize <= 0) {
1875 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1877 if (use_bsize == -1) {
1878 use_bsize = sys_page_size;
1879 if ((linux_version_code < (2*65536 + 6*256)) &&
1883 if (lsector_size && use_bsize < lsector_size)
1884 use_bsize = lsector_size;
1885 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1886 use_bsize = -blocksize;
1887 blocksize = use_bsize;
1888 fs_blocks_count /= (blocksize / 1024);
1890 if (blocksize < lsector_size) { /* Impossible */
1891 com_err(program_name, EINVAL, "%s",
1892 _("while setting blocksize; too small "
1895 } else if ((blocksize < psector_size) &&
1896 (psector_size <= sys_page_size)) { /* Suboptimal */
1897 fprintf(stderr, _("Warning: specified blocksize %d is "
1898 "less than device physical sectorsize %d\n"),
1899 blocksize, psector_size);
1903 fs_param.s_log_block_size =
1904 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1907 * We now need to do a sanity check of fs_blocks_count for
1908 * 32-bit vs 64-bit block number support.
1910 if ((fs_blocks_count > MAX_32_NUM) &&
1911 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1912 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1913 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1914 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1916 if ((fs_blocks_count > MAX_32_NUM) &&
1917 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1918 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1919 "too big to be expressed\n\t"
1920 "in 32 bits using a blocksize of %d.\n"),
1921 program_name, fs_blocks_count, device_name,
1922 EXT2_BLOCK_SIZE(&fs_param));
1926 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1928 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1929 fs_types[0] = strdup("journal");
1934 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1935 print_str_list(fs_types);
1938 if (r_opt == EXT2_GOOD_OLD_REV &&
1939 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1940 fs_param.s_feature_ro_compat)) {
1941 fprintf(stderr, "%s", _("Filesystem features not supported "
1942 "with revision 0 filesystems\n"));
1947 if (r_opt == EXT2_GOOD_OLD_REV) {
1948 fprintf(stderr, "%s",
1949 _("Sparse superblocks not supported "
1950 "with revision 0 filesystems\n"));
1953 fs_param.s_feature_ro_compat |=
1954 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1955 } else if (s_opt == 0)
1956 fs_param.s_feature_ro_compat &=
1957 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1959 if (journal_size != 0) {
1960 if (r_opt == EXT2_GOOD_OLD_REV) {
1961 fprintf(stderr, "%s", _("Journals not supported with "
1962 "revision 0 filesystems\n"));
1965 fs_param.s_feature_compat |=
1966 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1969 /* Get reserved_ratio from profile if not specified on cmd line. */
1970 if (reserved_ratio < 0.0) {
1971 reserved_ratio = get_double_from_profile(
1972 fs_types, "reserved_ratio", 5.0);
1973 if (reserved_ratio > 50 || reserved_ratio < 0) {
1974 com_err(program_name, 0,
1975 _("invalid reserved blocks percent - %lf"),
1981 if (fs_param.s_feature_incompat &
1982 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1984 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1985 fs_param.s_feature_compat = 0;
1986 fs_param.s_feature_ro_compat = 0;
1989 /* Check the user's mkfs options for 64bit */
1990 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1991 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1992 printf("%s", _("Extents MUST be enabled for a 64-bit "
1993 "filesystem. Pass -O extents to rectify.\n"));
1997 /* Set first meta blockgroup via an environment variable */
1998 /* (this is mostly for debugging purposes) */
1999 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2000 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
2001 fs_param.s_first_meta_bg = atoi(tmp);
2002 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2004 cluster_size = get_int_from_profile(fs_types,
2007 fs_param.s_log_cluster_size =
2008 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
2009 if (fs_param.s_log_cluster_size &&
2010 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
2011 com_err(program_name, 0, "%s",
2012 _("The cluster size may not be "
2013 "smaller than the block size.\n"));
2016 } else if (cluster_size) {
2017 com_err(program_name, 0, "%s",
2018 _("specifying a cluster size requires the "
2019 "bigalloc feature"));
2022 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2024 if (inode_ratio == 0) {
2025 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2027 if (inode_ratio < blocksize)
2028 inode_ratio = blocksize;
2029 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2030 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2033 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2034 retval = get_device_geometry(device_name, &fs_param, psector_size);
2037 _("warning: Unable to get device geometry for %s\n"),
2039 } else if (retval) {
2040 printf(_("%s alignment is offset by %lu bytes.\n"),
2041 device_name, retval);
2042 printf(_("This may result in very poor performance, "
2043 "(re)-partitioning suggested.\n"));
2047 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2050 * Initialize s_desc_size so that the parse_extended_opts()
2051 * can correctly handle "-E resize=NNN" if the 64-bit option
2054 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
2055 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2057 /* This check should happen beyond the last assignment to blocksize */
2058 if (blocksize > sys_page_size) {
2060 com_err(program_name, 0,
2061 _("%d-byte blocks too big for system (max %d)"),
2062 blocksize, sys_page_size);
2065 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2066 "(max %d), forced to continue\n"),
2067 blocksize, sys_page_size);
2070 lazy_itable_init = 0;
2071 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2072 lazy_itable_init = 1;
2074 lazy_itable_init = get_bool_from_profile(fs_types,
2077 discard = get_bool_from_profile(fs_types, "discard" , discard);
2078 journal_flags |= get_bool_from_profile(fs_types,
2079 "lazy_journal_init", 0) ?
2080 EXT2_MKJOURNAL_LAZYINIT : 0;
2081 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2083 /* Get options from profile */
2084 for (cpp = fs_types; *cpp; cpp++) {
2086 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2088 parse_extended_opts(&fs_param, tmp);
2093 parse_extended_opts(&fs_param, extended_opts);
2095 /* Don't allow user to set both metadata_csum and uninit_bg bits. */
2096 if ((fs_param.s_feature_ro_compat &
2097 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2098 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
2099 fs_param.s_feature_ro_compat &=
2100 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
2102 /* Can't support bigalloc feature without extents feature */
2103 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2104 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2105 com_err(program_name, 0, "%s",
2106 _("Can't support bigalloc feature without "
2107 "extents feature"));
2111 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2112 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2113 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2114 "features are not compatible.\n"
2115 "They can not be both enabled "
2116 "simultaneously.\n"));
2121 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2122 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2123 "still under development\n"
2124 "See https://ext4.wiki.kernel.org/"
2125 "index.php/Bigalloc for more information\n\n"));
2128 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2129 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2130 "still under development\n"
2131 "See https://ext4.wiki.kernel.org/"
2132 "index.php/Quota for more information\n\n"));
2134 /* Since sparse_super is the default, we would only have a problem
2135 * here if it was explicitly disabled.
2137 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2138 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2139 com_err(program_name, 0, "%s",
2140 _("reserved online resize blocks not supported "
2141 "on non-sparse filesystem"));
2145 if (fs_param.s_blocks_per_group) {
2146 if (fs_param.s_blocks_per_group < 256 ||
2147 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2148 com_err(program_name, 0, "%s",
2149 _("blocks per group count out of range"));
2155 * If the bigalloc feature is enabled, then the -g option will
2156 * specify the number of clusters per group.
2158 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2159 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2160 fs_param.s_blocks_per_group = 0;
2163 if (inode_size == 0)
2164 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2165 if (!flex_bg_size && (fs_param.s_feature_incompat &
2166 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2167 flex_bg_size = get_int_from_profile(fs_types,
2168 "flex_bg_size", 16);
2170 if (!(fs_param.s_feature_incompat &
2171 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2172 com_err(program_name, 0, "%s",
2173 _("Flex_bg feature not enabled, so "
2174 "flex_bg size may not be specified"));
2177 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2180 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2181 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2182 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2183 inode_size & (inode_size - 1)) {
2184 com_err(program_name, 0,
2185 _("invalid inode size %d (min %d/max %d)"),
2186 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2190 fs_param.s_inode_size = inode_size;
2193 /* Make sure number of inodes specified will fit in 32 bits */
2194 if (num_inodes == 0) {
2195 unsigned long long n;
2196 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2197 if (n > MAX_32_NUM) {
2198 if (fs_param.s_feature_incompat &
2199 EXT4_FEATURE_INCOMPAT_64BIT)
2200 num_inodes = MAX_32_NUM;
2202 com_err(program_name, 0,
2203 _("too many inodes (%llu), raise "
2204 "inode ratio?"), n);
2208 } else if (num_inodes > MAX_32_NUM) {
2209 com_err(program_name, 0,
2210 _("too many inodes (%llu), specify < 2^32 inodes"),
2215 * Calculate number of inodes based on the inode ratio
2217 fs_param.s_inodes_count = num_inodes ? num_inodes :
2218 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2220 if ((((unsigned long long)fs_param.s_inodes_count) *
2221 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2222 ((ext2fs_blocks_count(&fs_param)) *
2223 EXT2_BLOCK_SIZE(&fs_param))) {
2224 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2225 "(%u) too big for a\n\t"
2226 "filesystem with %llu blocks, "
2227 "specify higher inode_ratio (-i)\n\t"
2228 "or lower inode count (-N).\n"),
2229 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2230 fs_param.s_inodes_count,
2231 (unsigned long long) ext2fs_blocks_count(&fs_param));
2236 * Calculate number of blocks to reserve
2238 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2239 ext2fs_blocks_count(&fs_param) / 100.0);
2245 static int should_do_undo(const char *name)
2250 struct ext2_super_block super;
2251 io_manager manager = unix_io_manager;
2252 int csum_flag, force_undo;
2254 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2255 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
2256 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
2257 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2258 if (!force_undo && (!csum_flag || !lazy_itable_init))
2261 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2264 * We don't handle error cases instead we
2265 * declare that the file system doesn't exist
2266 * and let the rest of mke2fs take care of
2273 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2274 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2280 #if defined(WORDS_BIGENDIAN)
2281 s_magic = ext2fs_swab16(super.s_magic);
2283 s_magic = super.s_magic;
2286 if (s_magic == EXT2_SUPER_MAGIC)
2290 io_channel_close(channel);
2297 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2299 errcode_t retval = ENOMEM;
2300 char *tdb_dir = NULL, *tdb_file = NULL;
2301 char *dev_name, *tmp_name;
2302 int free_tdb_dir = 0;
2305 * Configuration via a conf file would be
2308 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2310 profile_get_string(profile, "defaults",
2311 "undo_dir", 0, "/var/lib/e2fsprogs",
2316 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2317 access(tdb_dir, W_OK)) {
2323 tmp_name = strdup(name);
2326 dev_name = basename(tmp_name);
2327 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2332 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2335 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2340 set_undo_io_backing_manager(*io_ptr);
2341 *io_ptr = undo_io_manager;
2342 retval = set_undo_io_backup_file(tdb_file);
2345 printf(_("Overwriting existing filesystem; this can be undone "
2346 "using the command:\n"
2347 " e2undo %s %s\n\n"), tdb_file, name);
2358 com_err(program_name, retval, "%s",
2359 _("while trying to setup undo file\n"));
2363 static int mke2fs_discard_device(ext2_filsys fs)
2365 struct ext2fs_numeric_progress_struct progress;
2366 blk64_t blocks = ext2fs_blocks_count(fs->super);
2367 blk64_t count = DISCARD_STEP_MB;
2372 * Let's try if discard really works on the device, so
2373 * we do not print numeric progress resulting in failure
2376 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2379 cur = fs->blocksize;
2381 count *= (1024 * 1024);
2382 count /= fs->blocksize;
2384 ext2fs_numeric_progress_init(fs, &progress,
2385 _("Discarding device blocks: "),
2387 while (cur < blocks) {
2388 ext2fs_numeric_progress_update(fs, &progress, cur);
2390 if (cur + count > blocks)
2391 count = blocks - cur;
2393 retval = io_channel_discard(fs->io, cur, count);
2400 ext2fs_numeric_progress_close(fs, &progress,
2403 printf("%s\n",error_message(retval));
2405 ext2fs_numeric_progress_close(fs, &progress,
2411 static void fix_cluster_bg_counts(ext2_filsys fs)
2413 blk64_t cluster, num_clusters, tot_free;
2415 int grp_free, num_free, group;
2417 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2418 tot_free = num_free = group = grp_free = 0;
2419 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2420 cluster < num_clusters; cluster++) {
2421 if (!ext2fs_test_block_bitmap2(fs->block_map,
2422 EXT2FS_C2B(fs, cluster))) {
2427 if ((num == fs->super->s_clusters_per_group) ||
2428 (cluster == num_clusters-1)) {
2429 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2430 ext2fs_group_desc_csum_set(fs, group);
2436 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2439 static int create_quota_inodes(ext2_filsys fs)
2443 quota_init_context(&qctx, fs, -1);
2444 quota_compute_usage(qctx);
2445 quota_write_inode(qctx, quotatype);
2446 quota_release_context(&qctx);
2451 int main (int argc, char *argv[])
2453 errcode_t retval = 0;
2455 badblocks_list bb_list = 0;
2456 unsigned int journal_blocks;
2457 unsigned int i, checkinterval;
2463 char opt_string[40];
2465 int itable_zeroed = 0;
2468 setlocale(LC_MESSAGES, "");
2469 setlocale(LC_CTYPE, "");
2470 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2471 textdomain(NLS_CAT_NAME);
2472 set_com_err_gettext(gettext);
2476 #ifdef CONFIG_TESTIO_DEBUG
2477 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2478 io_ptr = test_io_manager;
2479 test_io_backing_manager = unix_io_manager;
2482 io_ptr = unix_io_manager;
2484 if (should_do_undo(device_name)) {
2485 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2491 * Initialize the superblock....
2493 flags = EXT2_FLAG_EXCLUSIVE;
2495 flags |= EXT2_FLAG_DIRECT_IO;
2496 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2499 flags |= EXT2_FLAG_64BITS;
2501 * By default, we print how many inode tables or block groups
2502 * or whatever we've written so far. The quiet flag disables
2503 * this, along with a lot of other output.
2506 flags |= EXT2_FLAG_PRINT_PROGRESS;
2507 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2509 com_err(device_name, retval, "%s",
2510 _("while setting up superblock"));
2513 fs->progress_ops = &ext2fs_numeric_progress_ops;
2515 /* Check the user's mkfs options for metadata checksumming */
2517 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2518 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
2519 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2520 EXT3_FEATURE_INCOMPAT_EXTENTS))
2521 printf(_("Extents are not enabled. The file extent "
2522 "tree can be checksummed, whereas block maps "
2523 "cannot. Not enabling extents reduces the "
2524 "coverage of metadata checksumming. "
2525 "Pass -O extents to rectify.\n"));
2526 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2527 EXT4_FEATURE_INCOMPAT_64BIT))
2528 printf(_("64-bit filesystem support is not "
2529 "enabled. The larger fields afforded by "
2530 "this feature enable full-strength "
2531 "checksumming. Pass -O 64bit to rectify.\n"));
2534 /* Calculate journal blocks */
2535 if (!journal_device && ((journal_size) ||
2536 (fs_param.s_feature_compat &
2537 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2538 journal_blocks = figure_journal_size(journal_size, fs);
2540 /* Can't undo discard ... */
2541 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2542 retval = mke2fs_discard_device(fs);
2543 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2546 _("Discard succeeded and will return "
2547 "0s - skipping inode table wipe\n"));
2548 lazy_itable_init = 1;
2553 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2554 32768 : fs->blocksize * 8);
2555 io_channel_set_options(fs->io, opt_string);
2557 sprintf(opt_string, "offset=%llu", offset);
2558 io_channel_set_options(fs->io, opt_string);
2561 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2562 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2564 if ((fs_param.s_feature_incompat &
2565 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2566 (fs_param.s_feature_ro_compat &
2567 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2568 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2569 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
2570 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2571 fs->super->s_kbytes_written = 1;
2574 * Wipe out the old on-disk superblock
2577 zap_sector(fs, 2, 6);
2580 * Parse or generate a UUID for the filesystem
2583 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2584 com_err(device_name, 0, "could not parse UUID: %s\n",
2589 uuid_generate(fs->super->s_uuid);
2590 ext2fs_init_csum_seed(fs);
2593 * Initialize the directory index variables
2595 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2597 hash_alg = e2p_string2hash(hash_alg_str);
2599 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2601 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2604 * Periodic checks can be enabled/disabled via config file.
2605 * Note we override the kernel include file's idea of what the default
2606 * check interval (never) should be. It's a good idea to check at
2607 * least *occasionally*, specially since servers will never rarely get
2608 * to reboot, since Linux is so robust these days. :-)
2610 * 180 days (six months) seems like a good value.
2612 #ifdef EXT2_DFL_CHECKINTERVAL
2613 #undef EXT2_DFL_CHECKINTERVAL
2615 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2617 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2618 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2619 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2621 * Add "jitter" to the superblock's check interval so that we
2622 * don't check all the filesystems at the same time. We use a
2623 * kludgy hack of using the UUID to derive a random jitter value
2625 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2626 val += fs->super->s_uuid[i];
2627 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2629 fs->super->s_max_mnt_count = -1;
2632 * Override the creator OS, if applicable
2634 if (creator_os && !set_os(fs->super, creator_os)) {
2635 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2640 * For the Hurd, we will turn off filetype since it doesn't
2643 if (fs->super->s_creator_os == EXT2_OS_HURD)
2644 fs->super->s_feature_incompat &=
2645 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2648 * Set the volume label...
2651 memset(fs->super->s_volume_name, 0,
2652 sizeof(fs->super->s_volume_name));
2653 strncpy(fs->super->s_volume_name, volume_label,
2654 sizeof(fs->super->s_volume_name));
2658 * Set the last mount directory
2661 memset(fs->super->s_last_mounted, 0,
2662 sizeof(fs->super->s_last_mounted));
2663 strncpy(fs->super->s_last_mounted, mount_dir,
2664 sizeof(fs->super->s_last_mounted));
2667 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2668 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2669 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
2671 if (!quiet || noaction)
2677 if (fs->super->s_feature_incompat &
2678 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2679 create_journal_dev(fs);
2680 exit(ext2fs_close(fs) ? 1 : 0);
2683 if (bad_blocks_filename)
2684 read_bb_file(fs, &bb_list, bad_blocks_filename);
2686 test_disk(fs, &bb_list);
2688 handle_bad_blocks(fs, bb_list);
2689 fs->stride = fs_stride = fs->super->s_raid_stride;
2691 printf("%s", _("Allocating group tables: "));
2692 retval = ext2fs_allocate_tables(fs);
2694 com_err(program_name, retval, "%s",
2695 _("while trying to allocate filesystem tables"));
2699 printf("%s", _("done \n"));
2701 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2703 com_err(program_name, retval, "%s",
2704 _("\n\twhile converting subcluster bitmap"));
2709 fs->super->s_state |= EXT2_ERROR_FS;
2710 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2712 * The command "mke2fs -S" is used to recover
2713 * corrupted file systems, so do not mark any of the
2714 * inodes as unused; we want e2fsck to consider all
2715 * inodes as potentially containing recoverable data.
2717 if (ext2fs_has_group_desc_csum(fs)) {
2718 for (i = 0; i < fs->group_desc_count; i++)
2719 ext2fs_bg_itable_unused_set(fs, i, 0);
2722 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2723 blk64_t rsv = 65536 / fs->blocksize;
2724 blk64_t blocks = ext2fs_blocks_count(fs->super);
2728 #ifdef ZAP_BOOTBLOCK
2729 zap_sector(fs, 0, 2);
2733 * Wipe out any old MD RAID (or other) metadata at the end
2734 * of the device. This will also verify that the device is
2735 * as large as we think. Be careful with very small devices.
2737 start = (blocks & ~(rsv - 1));
2741 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2745 com_err(program_name, retval,
2746 _("while zeroing block %llu at end of filesystem"),
2749 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2750 create_root_dir(fs);
2751 create_lost_and_found(fs);
2753 create_bad_block_inode(fs, bb_list);
2754 if (fs->super->s_feature_compat &
2755 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2756 retval = ext2fs_create_resize_inode(fs);
2758 com_err("ext2fs_create_resize_inode", retval,
2760 _("while reserving blocks for online resize"));
2766 if (journal_device) {
2770 check_plausibility(journal_device);
2771 check_mount(journal_device, force, _("journal"));
2773 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2774 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2775 fs->blocksize, unix_io_manager, &jfs);
2777 com_err(program_name, retval,
2778 _("while trying to open journal device %s\n"),
2783 printf(_("Adding journal to device %s: "),
2787 retval = ext2fs_add_journal_device(fs, jfs);
2789 com_err (program_name, retval,
2790 _("\n\twhile trying to add journal to device %s"),
2795 printf("%s", _("done\n"));
2797 free(journal_device);
2798 } else if ((journal_size) ||
2799 (fs_param.s_feature_compat &
2800 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2802 printf("%s", _("Skipping journal creation in super-only mode\n"));
2803 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2807 if (!journal_blocks) {
2808 fs->super->s_feature_compat &=
2809 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2813 printf(_("Creating journal (%u blocks): "),
2817 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2820 com_err(program_name, retval, "%s",
2821 _("\n\twhile trying to create journal"));
2825 printf("%s", _("done\n"));
2829 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2830 retval = ext2fs_mmp_init(fs);
2832 fprintf(stderr, "%s",
2833 _("\nError while enabling multiple "
2834 "mount protection feature."));
2838 printf(_("Multiple mount protection is enabled "
2839 "with update interval %d seconds.\n"),
2840 fs->super->s_mmp_update_interval);
2843 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2844 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2845 fix_cluster_bg_counts(fs);
2846 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2847 EXT4_FEATURE_RO_COMPAT_QUOTA))
2848 create_quota_inodes(fs);
2851 printf("%s", _("Writing superblocks and "
2852 "filesystem accounting information: "));
2853 checkinterval = fs->super->s_checkinterval;
2854 max_mnt_count = fs->super->s_max_mnt_count;
2855 retval = ext2fs_close(fs);
2857 fprintf(stderr, "%s",
2858 _("\nWarning, had trouble writing out superblocks."));
2859 } else if (!quiet) {
2860 printf("%s", _("done\n\n"));
2861 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2862 print_check_message(max_mnt_count, checkinterval);
2865 remove_error_table(&et_ext2_error_table);
2866 remove_error_table(&et_prof_error_table);
2867 profile_release(profile);
2868 for (i=0; fs_types[i]; i++)