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 const char * program_name = "mke2fs";
80 const char * device_name /* = NULL */;
82 /* Command line options */
87 int discard = 1; /* attempt to discard device before fs creation */
96 char *bad_blocks_filename;
98 int quotatype = -1; /* Initialize both user and group quotas by default */
100 struct ext2_super_block fs_param;
101 char *fs_uuid = NULL;
105 char *journal_device;
106 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
111 int sys_page_size = 4096;
112 int linux_version_code = 0;
114 static void usage(void)
116 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
117 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
118 "[-J journal-options]\n"
119 "\t[-G flex-group-size] [-N number-of-inodes]\n"
120 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
121 "\t[-g blocks-per-group] [-L volume-label] "
122 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
123 "[-r fs-revision] [-E extended-option[,...]]\n"
124 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
125 "[-jnqvDFKSV] device [blocks-count]\n"),
130 static int int_log2(unsigned long long arg)
142 static int int_log10(unsigned long long arg)
152 static int parse_version_number(const char *s)
154 int major, minor, rev;
160 major = strtol(cp, &endptr, 10);
161 if (cp == endptr || *endptr != '.')
164 minor = strtol(cp, &endptr, 10);
165 if (cp == endptr || *endptr != '.')
168 rev = strtol(cp, &endptr, 10);
171 return ((((major * 256) + minor) * 256) + rev);
176 * Helper function for read_bb_file and test_disk
178 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
180 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
185 * Reads the bad blocks list from a file
187 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
188 const char *bad_blocks_file)
193 f = fopen(bad_blocks_file, "r");
195 com_err("read_bad_blocks_file", errno,
196 _("while trying to open %s"), bad_blocks_file);
199 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
202 com_err("ext2fs_read_bb_FILE", retval,
203 _("while reading in list of bad blocks from file"));
209 * Runs the badblocks program to test the disk
211 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
217 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
218 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
219 fs->device_name, ext2fs_blocks_count(fs->super)-1);
221 printf(_("Running command: %s\n"), buf);
224 com_err("popen", errno,
225 _("while trying to run '%s'"), buf);
228 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
231 com_err("ext2fs_read_bb_FILE", retval,
232 _("while processing list of bad blocks from program"));
237 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
241 unsigned must_be_good;
243 badblocks_iterate bb_iter;
253 * The primary superblock and group descriptors *must* be
254 * good; if not, abort.
256 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
257 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
258 if (ext2fs_badblocks_list_test(bb_list, i)) {
259 fprintf(stderr, _("Block %d in primary "
260 "superblock/group descriptor area bad.\n"), i);
261 fprintf(stderr, _("Blocks %u through %u must be good "
262 "in order to build a filesystem.\n"),
263 fs->super->s_first_data_block, must_be_good);
264 fputs(_("Aborting....\n"), stderr);
270 * See if any of the bad blocks are showing up in the backup
271 * superblocks and/or group descriptors. If so, issue a
272 * warning and adjust the block counts appropriately.
274 group_block = fs->super->s_first_data_block +
275 fs->super->s_blocks_per_group;
277 for (i = 1; i < fs->group_desc_count; i++) {
279 for (j=0; j < fs->desc_blocks+1; j++) {
280 if (ext2fs_badblocks_list_test(bb_list,
284 _("Warning: the backup superblock/group descriptors at block %u contain\n"
288 group = ext2fs_group_of_blk2(fs, group_block+j);
289 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
290 ext2fs_group_desc_csum_set(fs, group);
291 ext2fs_free_blocks_count_add(fs->super, 1);
294 group_block += fs->super->s_blocks_per_group;
298 * Mark all the bad blocks as used...
300 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
302 com_err("ext2fs_badblocks_list_iterate_begin", retval,
303 _("while marking bad blocks as used"));
306 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
307 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
308 ext2fs_badblocks_list_iterate_end(bb_iter);
311 static void write_reserved_inodes(ext2_filsys fs)
315 struct ext2_inode *inode;
317 retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode);
319 com_err("inode_init", retval,
320 "while allocating memory");
323 bzero(inode, EXT2_INODE_SIZE(fs->super));
325 for (ino = 1; ino < EXT2_FIRST_INO(fs->super); ino++)
326 ext2fs_write_inode_full(fs, ino, inode,
327 EXT2_INODE_SIZE(fs->super));
329 ext2fs_free_mem(&inode);
332 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
338 struct ext2fs_numeric_progress_struct progress;
340 ext2fs_numeric_progress_init(fs, &progress,
341 _("Writing inode tables: "),
342 fs->group_desc_count);
344 for (i = 0; i < fs->group_desc_count; i++) {
345 ext2fs_numeric_progress_update(fs, &progress, i);
347 blk = ext2fs_inode_table_loc(fs, i);
348 num = fs->inode_blocks_per_group;
351 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
352 ext2fs_bg_itable_unused(fs, i)) *
353 EXT2_INODE_SIZE(fs->super),
354 EXT2_BLOCK_SIZE(fs->super));
355 if (!lazy_flag || itable_zeroed) {
356 /* The kernel doesn't need to zero the itable blocks */
357 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
358 ext2fs_group_desc_csum_set(fs, i);
360 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
362 fprintf(stderr, _("\nCould not write %d "
363 "blocks in inode table starting at %llu: %s\n"),
364 num, blk, error_message(retval));
368 if (sync_kludge == 1)
370 else if ((i % sync_kludge) == 0)
374 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
375 ext2fs_numeric_progress_close(fs, &progress,
378 /* Reserved inodes must always have correct checksums */
379 if (fs->super->s_creator_os == EXT2_OS_LINUX &&
380 fs->super->s_feature_ro_compat &
381 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
382 write_reserved_inodes(fs);
385 static void create_root_dir(ext2_filsys fs)
388 struct ext2_inode inode;
391 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
393 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
396 if (root_uid != 0 || root_gid != 0) {
397 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
399 com_err("ext2fs_read_inode", retval,
400 _("while reading root inode"));
404 inode.i_uid = root_uid;
405 ext2fs_set_i_uid_high(inode, root_uid >> 16);
406 inode.i_gid = root_gid;
407 ext2fs_set_i_gid_high(inode, root_gid >> 16);
409 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
411 com_err("ext2fs_write_inode", retval,
412 _("while setting root inode ownership"));
418 static void create_lost_and_found(ext2_filsys fs)
420 unsigned int lpf_size = 0;
423 const char *name = "lost+found";
427 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
429 com_err("ext2fs_mkdir", retval,
430 _("while creating /lost+found"));
434 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
436 com_err("ext2_lookup", retval,
437 _("while looking up /lost+found"));
441 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
442 /* Ensure that lost+found is at least 2 blocks, so we always
443 * test large empty blocks for big-block filesystems. */
444 if ((lpf_size += fs->blocksize) >= 16*1024 &&
445 lpf_size >= 2 * fs->blocksize)
447 retval = ext2fs_expand_dir(fs, ino);
449 com_err("ext2fs_expand_dir", retval,
450 _("while expanding /lost+found"));
456 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
460 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
461 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
462 retval = ext2fs_update_bb_inode(fs, bb_list);
464 com_err("ext2fs_update_bb_inode", retval,
465 _("while setting bad block inode"));
471 static void reserve_inodes(ext2_filsys fs)
475 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
476 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
477 ext2fs_mark_ib_dirty(fs);
480 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
481 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
482 #define BSD_LABEL_OFFSET 64
484 static void zap_sector(ext2_filsys fs, int sect, int nsect)
490 buf = malloc(512*nsect);
492 printf(_("Out of memory erasing sectors %d-%d\n"),
493 sect, sect + nsect - 1);
498 /* Check for a BSD disklabel, and don't erase it if so */
499 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
502 _("Warning: could not read block 0: %s\n"),
503 error_message(retval));
505 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
506 if ((*magic == BSD_DISKMAGIC) ||
507 (*magic == BSD_MAGICDISK))
512 memset(buf, 0, 512*nsect);
513 io_channel_set_blksize(fs->io, 512);
514 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
515 io_channel_set_blksize(fs->io, fs->blocksize);
518 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
519 sect, error_message(retval));
522 static void create_journal_dev(ext2_filsys fs)
524 struct ext2fs_numeric_progress_struct progress;
527 blk64_t blk, err_blk;
528 int c, count, err_count;
530 retval = ext2fs_create_journal_superblock(fs,
531 ext2fs_blocks_count(fs->super), 0, &buf);
533 com_err("create_journal_dev", retval,
534 _("while initializing journal superblock"));
538 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
539 goto write_superblock;
541 ext2fs_numeric_progress_init(fs, &progress,
542 _("Zeroing journal device: "),
543 ext2fs_blocks_count(fs->super));
545 count = ext2fs_blocks_count(fs->super);
551 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
553 com_err("create_journal_dev", retval,
554 _("while zeroing journal device "
555 "(block %llu, count %d)"),
561 ext2fs_numeric_progress_update(fs, &progress, blk);
563 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
565 ext2fs_numeric_progress_close(fs, &progress, NULL);
567 retval = io_channel_write_blk64(fs->io,
568 fs->super->s_first_data_block+1,
571 com_err("create_journal_dev", retval,
572 _("while writing journal superblock"));
577 static void show_stats(ext2_filsys fs)
579 struct ext2_super_block *s = fs->super;
586 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
587 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
588 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
590 memset(buf, 0, sizeof(buf));
591 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
592 printf(_("Filesystem label=%s\n"), buf);
593 os = e2p_os2string(fs->super->s_creator_os);
595 printf(_("OS type: %s\n"), os);
597 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
598 s->s_log_block_size);
599 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
600 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
601 printf(_("Cluster size=%u (log=%u)\n"),
602 fs->blocksize << fs->cluster_ratio_bits,
603 s->s_log_cluster_size);
605 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
606 s->s_log_cluster_size);
607 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
608 s->s_raid_stride, s->s_raid_stripe_width);
609 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
610 ext2fs_blocks_count(s));
611 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
612 ext2fs_r_blocks_count(s),
613 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
614 printf(_("First data block=%u\n"), s->s_first_data_block);
615 if (root_uid != 0 || root_gid != 0)
616 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
617 if (s->s_reserved_gdt_blocks)
618 printf(_("Maximum filesystem blocks=%lu\n"),
619 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
620 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
621 if (fs->group_desc_count > 1)
622 printf(_("%u block groups\n"), fs->group_desc_count);
624 printf(_("%u block group\n"), fs->group_desc_count);
625 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
626 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
627 printf(_("%u blocks per group, %u clusters per group\n"),
628 s->s_blocks_per_group, s->s_clusters_per_group);
630 printf(_("%u blocks per group, %u fragments per group\n"),
631 s->s_blocks_per_group, s->s_clusters_per_group);
632 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
634 if (fs->group_desc_count == 1) {
639 printf(_("Superblock backups stored on blocks: "));
640 group_block = s->s_first_data_block;
642 for (i = 1; i < fs->group_desc_count; i++) {
643 group_block += s->s_blocks_per_group;
644 if (!ext2fs_bg_has_super(fs, i))
648 need = int_log10(group_block) + 2;
649 if (need > col_left) {
654 printf("%llu", group_block);
660 * Returns true if making a file system for the Hurd, else 0
662 static int for_hurd(const char *os)
672 return (atoi(os) == EXT2_OS_HURD);
673 return (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0);
677 * Set the S_CREATOR_OS field. Return true if OS is known,
680 static int set_os(struct ext2_super_block *sb, char *os)
683 sb->s_creator_os = atoi (os);
684 else if (strcasecmp(os, "linux") == 0)
685 sb->s_creator_os = EXT2_OS_LINUX;
686 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
687 sb->s_creator_os = EXT2_OS_HURD;
688 else if (strcasecmp(os, "freebsd") == 0)
689 sb->s_creator_os = EXT2_OS_FREEBSD;
690 else if (strcasecmp(os, "lites") == 0)
691 sb->s_creator_os = EXT2_OS_LITES;
697 #define PATH_SET "PATH=/sbin"
699 static void parse_extended_opts(struct ext2_super_block *param,
702 char *buf, *token, *next, *p, *arg, *badopt = 0;
710 _("Couldn't allocate memory to parse options!\n"));
714 for (token = buf; token && *token; token = next) {
715 p = strchr(token, ',');
721 arg = strchr(token, '=');
726 if (strcmp(token, "mmp_update_interval") == 0) {
732 param->s_mmp_update_interval = strtoul(arg, &p, 0);
735 _("Invalid mmp_update_interval: %s\n"),
740 } else if (strcmp(token, "stride") == 0) {
746 param->s_raid_stride = strtoul(arg, &p, 0);
749 _("Invalid stride parameter: %s\n"),
754 } else if (strcmp(token, "stripe-width") == 0 ||
755 strcmp(token, "stripe_width") == 0) {
761 param->s_raid_stripe_width = strtoul(arg, &p, 0);
764 _("Invalid stripe-width parameter: %s\n"),
769 } else if (!strcmp(token, "resize")) {
771 unsigned long bpg, rsv_groups;
772 unsigned long group_desc_count, desc_blocks;
773 unsigned int gdpb, blocksize;
782 resize = parse_num_blocks2(arg,
783 param->s_log_block_size);
787 _("Invalid resize parameter: %s\n"),
792 if (resize <= ext2fs_blocks_count(param)) {
794 _("The resize maximum must be greater "
795 "than the filesystem size.\n"));
800 blocksize = EXT2_BLOCK_SIZE(param);
801 bpg = param->s_blocks_per_group;
804 gdpb = EXT2_DESC_PER_BLOCK(param);
805 group_desc_count = (__u32) ext2fs_div64_ceil(
806 ext2fs_blocks_count(param), bpg);
807 desc_blocks = (group_desc_count +
809 rsv_groups = ext2fs_div64_ceil(resize, bpg);
810 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
812 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
813 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
816 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
818 _("On-line resizing not supported with revision 0 filesystems\n"));
822 param->s_feature_compat |=
823 EXT2_FEATURE_COMPAT_RESIZE_INODE;
825 param->s_reserved_gdt_blocks = rsv_gdb;
827 } else if (!strcmp(token, "test_fs")) {
828 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
829 } else if (!strcmp(token, "lazy_itable_init")) {
831 lazy_itable_init = strtoul(arg, &p, 0);
833 lazy_itable_init = 1;
834 } else if (!strcmp(token, "lazy_journal_init")) {
836 journal_flags |= strtoul(arg, &p, 0) ?
837 EXT2_MKJOURNAL_LAZYINIT : 0;
839 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
840 } else if (!strcmp(token, "root_owner")) {
842 root_uid = strtoul(arg, &p, 0);
845 _("Invalid root_owner: '%s'\n"),
851 root_gid = strtoul(p, &p, 0);
854 _("Invalid root_owner: '%s'\n"),
863 } else if (!strcmp(token, "discard")) {
865 } else if (!strcmp(token, "nodiscard")) {
867 } else if (!strcmp(token, "quotatype")) {
873 if (!strncmp(arg, "usr", 3)) {
875 } else if (!strncmp(arg, "grp", 3)) {
879 _("Invalid quotatype parameter: %s\n"),
890 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
891 "Extended options are separated by commas, "
892 "and may take an argument which\n"
893 "\tis set off by an equals ('=') sign.\n\n"
894 "Valid extended options are:\n"
895 "\tstride=<RAID per-disk data chunk in blocks>\n"
896 "\tstripe-width=<RAID stride * data disks in blocks>\n"
897 "\tresize=<resize maximum size in blocks>\n"
898 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
899 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
900 "\troot_uid=<uid of root directory>\n"
901 "\troot_gid=<gid of root directory>\n"
905 "\tquotatype=<usr OR grp>\n\n"),
906 badopt ? badopt : "");
910 if (param->s_raid_stride &&
911 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
912 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
913 "multiple of stride %u.\n\n"),
914 param->s_raid_stripe_width, param->s_raid_stride);
919 static __u32 ok_features[3] = {
921 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
922 EXT2_FEATURE_COMPAT_RESIZE_INODE |
923 EXT2_FEATURE_COMPAT_DIR_INDEX |
924 EXT2_FEATURE_COMPAT_EXT_ATTR,
926 EXT2_FEATURE_INCOMPAT_FILETYPE|
927 EXT3_FEATURE_INCOMPAT_EXTENTS|
928 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
929 EXT2_FEATURE_INCOMPAT_META_BG|
930 EXT4_FEATURE_INCOMPAT_FLEX_BG|
931 EXT4_FEATURE_INCOMPAT_MMP |
932 EXT4_FEATURE_INCOMPAT_64BIT,
934 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
935 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
936 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
937 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
938 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
939 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
940 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
942 EXT4_FEATURE_RO_COMPAT_QUOTA|
944 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
948 static void syntax_err_report(const char *filename, long err, int line_num)
951 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
952 filename, line_num, error_message(err));
956 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
958 static void edit_feature(const char *str, __u32 *compat_array)
963 if (e2p_edit_feature(str, compat_array, ok_features)) {
964 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
970 static void edit_mntopts(const char *str, __u32 *mntopts)
975 if (e2p_edit_mntopts(str, mntopts, ~0)) {
976 fprintf(stderr, _("Invalid mount option set: %s\n"),
988 static errcode_t init_list(struct str_list *sl)
992 sl->list = malloc((sl->max+1) * sizeof(char *));
999 static errcode_t push_string(struct str_list *sl, const char *str)
1003 if (sl->num >= sl->max) {
1005 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1008 sl->list = new_list;
1010 sl->list[sl->num] = malloc(strlen(str)+1);
1011 if (sl->list[sl->num] == 0)
1013 strcpy(sl->list[sl->num], str);
1015 sl->list[sl->num] = 0;
1019 static void print_str_list(char **list)
1023 for (cpp = list; *cpp; cpp++) {
1024 printf("'%s'", *cpp);
1026 fputs(", ", stdout);
1028 fputc('\n', stdout);
1032 * Return TRUE if the profile has the given subsection
1034 static int profile_has_subsection(profile_t prof, const char *section,
1035 const char *subsection)
1038 const char *names[4];
1043 names[1] = subsection;
1046 if (profile_iterator_create(prof, names,
1047 PROFILE_ITER_LIST_SECTION |
1048 PROFILE_ITER_RELATIONS_ONLY, &state))
1051 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1056 profile_iterator_free(&state);
1060 static char **parse_fs_type(const char *fs_type,
1061 const char *usage_types,
1062 struct ext2_super_block *sb,
1063 blk64_t fs_blocks_count,
1066 const char *ext_type = 0;
1068 char *profile_type = 0;
1070 const char *size_type;
1071 struct str_list list;
1072 unsigned long long meg;
1073 int is_hurd = for_hurd(creator_os);
1075 if (init_list(&list))
1082 else if (!strcmp(program_name, "mke3fs"))
1084 else if (!strcmp(program_name, "mke4fs"))
1086 else if (progname) {
1087 ext_type = strrchr(progname, '/');
1091 ext_type = progname;
1093 if (!strncmp(ext_type, "mkfs.", 5)) {
1095 if (ext_type[0] == 0)
1102 profile_get_string(profile, "defaults", "fs_type", 0,
1103 "ext2", &profile_type);
1104 ext_type = profile_type;
1105 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1110 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1111 strcmp(ext_type, "ext2")) {
1112 printf(_("\nYour mke2fs.conf file does not define the "
1113 "%s filesystem type.\n"), ext_type);
1114 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1115 !strcmp(ext_type, "ext4dev")) {
1116 printf(_("You probably need to install an updated "
1117 "mke2fs.conf file.\n\n"));
1120 printf(_("Aborting...\n"));
1125 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1126 if (fs_blocks_count < 3 * meg)
1127 size_type = "floppy";
1128 else if (fs_blocks_count < 512 * meg)
1129 size_type = "small";
1130 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1131 size_type = "default";
1132 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1138 usage_types = size_type;
1140 parse_str = malloc(strlen(usage_types)+1);
1145 strcpy(parse_str, usage_types);
1148 push_string(&list, ext_type);
1151 t = strchr(cp, ',');
1156 if (profile_has_subsection(profile, "fs_types", cp))
1157 push_string(&list, cp);
1158 else if (strcmp(cp, "default") != 0)
1160 _("\nWarning: the fs_type %s is not "
1161 "defined in mke2fs.conf\n\n"),
1172 push_string(&list, "hurd");
1176 static char *get_string_from_profile(char **types, const char *opt,
1177 const char *def_val)
1182 for (i=0; types[i]; i++);
1183 for (i-=1; i >=0 ; i--) {
1184 profile_get_string(profile, "fs_types", types[i],
1189 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1193 static int get_int_from_profile(char **types, const char *opt, int def_val)
1198 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1199 for (cpp = types; *cpp; cpp++)
1200 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1204 static double get_double_from_profile(char **types, const char *opt,
1210 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1211 for (cpp = types; *cpp; cpp++)
1212 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1216 static int get_bool_from_profile(char **types, const char *opt, int def_val)
1221 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1222 for (cpp = types; *cpp; cpp++)
1223 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1227 extern const char *mke2fs_default_profile;
1228 static const char *default_files[] = { "<default>", 0 };
1230 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1232 * Sets the geometry of a device (stripe/stride), and returns the
1233 * device's alignment offset, if any, or a negative error.
1235 static int get_device_geometry(const char *file,
1236 struct ext2_super_block *fs_param,
1243 unsigned long min_io;
1244 unsigned long opt_io;
1245 struct stat statbuf;
1247 /* Nothing to do for a regular file */
1248 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1251 pr = blkid_new_probe_from_filename(file);
1255 tp = blkid_probe_get_topology(pr);
1259 min_io = blkid_topology_get_minimum_io_size(tp);
1260 opt_io = blkid_topology_get_optimal_io_size(tp);
1261 blocksize = EXT2_BLOCK_SIZE(fs_param);
1262 if ((min_io == 0) && (psector_size > blocksize))
1263 min_io = psector_size;
1264 if ((opt_io == 0) && min_io)
1266 if ((opt_io == 0) && (psector_size > blocksize))
1267 opt_io = psector_size;
1269 /* setting stripe/stride to blocksize is pointless */
1270 if (min_io > blocksize)
1271 fs_param->s_raid_stride = min_io / blocksize;
1272 if (opt_io > blocksize)
1273 fs_param->s_raid_stripe_width = opt_io / blocksize;
1275 rc = blkid_topology_get_alignment_offset(tp);
1277 blkid_free_probe(pr);
1282 static void PRS(int argc, char *argv[])
1285 int cluster_size = 0;
1288 int inode_ratio = 0;
1290 unsigned long flex_bg_size = 0;
1291 double reserved_ratio = -1.0;
1292 int lsector_size = 0, psector_size = 0;
1293 int show_version_only = 0;
1294 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1296 char * oldpath = getenv("PATH");
1297 char * extended_opts = 0;
1299 char * usage_types = 0;
1301 blk64_t fs_blocks_count = 0;
1306 int s_opt = -1, r_opt = -1;
1307 char *fs_features = 0;
1310 int pathlen = sizeof(PATH_SET) + 1;
1313 pathlen += strlen(oldpath);
1314 newpath = malloc(pathlen);
1316 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1319 strcpy(newpath, PATH_SET);
1321 /* Update our PATH to include /sbin */
1323 strcat (newpath, ":");
1324 strcat (newpath, oldpath);
1328 tmp = getenv("MKE2FS_SYNC");
1330 sync_kludge = atoi(tmp);
1332 /* Determine the system page size if possible */
1334 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1335 #define _SC_PAGESIZE _SC_PAGE_SIZE
1338 sysval = sysconf(_SC_PAGESIZE);
1340 sys_page_size = sysval;
1341 #endif /* _SC_PAGESIZE */
1342 #endif /* HAVE_SYSCONF */
1344 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1346 profile_set_syntax_err_cb(syntax_err_report);
1347 retval = profile_init(config_fn, &profile);
1348 if (retval == ENOENT) {
1349 retval = profile_init(default_files, &profile);
1352 retval = profile_set_default(profile, mke2fs_default_profile);
1355 } else if (retval) {
1357 fprintf(stderr, _("Couldn't init profile successfully"
1358 " (error: %ld).\n"), retval);
1362 setbuf(stdout, NULL);
1363 setbuf(stderr, NULL);
1364 add_error_table(&et_ext2_error_table);
1365 add_error_table(&et_prof_error_table);
1366 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1367 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1374 linux_version_code = parse_version_number(ut.release);
1375 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1376 fs_param.s_rev_level = 0;
1379 if (argc && *argv) {
1380 program_name = get_progname(*argv);
1382 /* If called as mkfs.ext3, create a journal inode */
1383 if (!strcmp(program_name, "mkfs.ext3") ||
1384 !strcmp(program_name, "mke3fs"))
1388 while ((c = getopt (argc, argv,
1389 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1392 blocksize = parse_num_blocks2(optarg, -1);
1393 b = (blocksize > 0) ? blocksize : -blocksize;
1394 if (b < EXT2_MIN_BLOCK_SIZE ||
1395 b > EXT2_MAX_BLOCK_SIZE) {
1396 com_err(program_name, 0,
1397 _("invalid block size - %s"), optarg);
1400 if (blocksize > 4096)
1401 fprintf(stderr, _("Warning: blocksize %d not "
1402 "usable on most systems.\n"),
1405 fs_param.s_log_block_size =
1406 int_log2(blocksize >>
1407 EXT2_MIN_BLOCK_LOG_SIZE);
1409 case 'c': /* Check for bad blocks */
1413 cluster_size = parse_num_blocks2(optarg, -1);
1414 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1415 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1416 com_err(program_name, 0,
1417 _("invalid cluster size - %s"),
1426 com_err(program_name, 0,
1427 _("'-R' is deprecated, use '-E' instead"));
1430 extended_opts = optarg;
1436 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1438 com_err(program_name, 0,
1439 _("Illegal number for blocks per group"));
1442 if ((fs_param.s_blocks_per_group % 8) != 0) {
1443 com_err(program_name, 0,
1444 _("blocks per group must be multiple of 8"));
1449 flex_bg_size = strtoul(optarg, &tmp, 0);
1451 com_err(program_name, 0,
1452 _("Illegal number for flex_bg size"));
1455 if (flex_bg_size < 1 ||
1456 (flex_bg_size & (flex_bg_size-1)) != 0) {
1457 com_err(program_name, 0,
1458 _("flex_bg size must be a power of 2"));
1463 inode_ratio = strtoul(optarg, &tmp, 0);
1464 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1465 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1467 com_err(program_name, 0,
1468 _("invalid inode ratio %s (min %d/max %d)"),
1469 optarg, EXT2_MIN_BLOCK_SIZE,
1470 EXT2_MAX_BLOCK_SIZE * 1024);
1475 inode_size = strtoul(optarg, &tmp, 0);
1477 com_err(program_name, 0,
1478 _("invalid inode size - %s"), optarg);
1487 parse_journal_opts(optarg);
1490 fprintf(stderr, _("Warning: -K option is deprecated and "
1491 "should not be used anymore. Use "
1492 "\'-E nodiscard\' extended option "
1497 bad_blocks_filename = malloc(strlen(optarg)+1);
1498 if (!bad_blocks_filename) {
1499 com_err(program_name, ENOMEM,
1500 _("in malloc for bad_blocks_filename"));
1503 strcpy(bad_blocks_filename, optarg);
1506 volume_label = optarg;
1509 reserved_ratio = strtod(optarg, &tmp);
1510 if ( *tmp || reserved_ratio > 50 ||
1511 reserved_ratio < 0) {
1512 com_err(program_name, 0,
1513 _("invalid reserved blocks percent - %s"),
1525 num_inodes = strtoul(optarg, &tmp, 0);
1527 com_err(program_name, 0,
1528 _("bad num inodes - %s"), optarg);
1533 creator_os = optarg;
1536 fs_features = optarg;
1542 r_opt = strtoul(optarg, &tmp, 0);
1544 com_err(program_name, 0,
1545 _("bad revision level - %s"), optarg);
1548 fs_param.s_rev_level = r_opt;
1550 case 's': /* deprecated */
1551 s_opt = atoi(optarg);
1558 com_err(program_name, 0,
1559 _("The -t option may only be used once"));
1562 fs_type = strdup(optarg);
1566 com_err(program_name, 0,
1567 _("The -T option may only be used once"));
1570 usage_types = strdup(optarg);
1579 /* Print version number and exit */
1580 show_version_only++;
1586 if ((optind == argc) && !show_version_only)
1588 device_name = argv[optind++];
1590 if (!quiet || show_version_only)
1591 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1594 if (show_version_only) {
1595 fprintf(stderr, _("\tUsing %s\n"),
1596 error_message(EXT2_ET_BASE));
1601 * If there's no blocksize specified and there is a journal
1602 * device, use it to figure out the blocksize
1604 if (blocksize <= 0 && journal_device) {
1608 #ifdef CONFIG_TESTIO_DEBUG
1609 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1610 io_ptr = test_io_manager;
1611 test_io_backing_manager = unix_io_manager;
1614 io_ptr = unix_io_manager;
1615 retval = ext2fs_open(journal_device,
1616 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1619 com_err(program_name, retval,
1620 _("while trying to open journal device %s\n"),
1624 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1625 com_err(program_name, 0,
1626 _("Journal dev blocksize (%d) smaller than "
1627 "minimum blocksize %d\n"), jfs->blocksize,
1631 blocksize = jfs->blocksize;
1632 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1633 fs_param.s_log_block_size =
1634 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1638 if (optind < argc) {
1639 fs_blocks_count = parse_num_blocks2(argv[optind++],
1640 fs_param.s_log_block_size);
1641 if (!fs_blocks_count) {
1642 com_err(program_name, 0,
1643 _("invalid blocks '%s' on device '%s'"),
1644 argv[optind - 1], device_name);
1652 check_plausibility(device_name);
1653 check_mount(device_name, force, _("filesystem"));
1655 /* Determine the size of the device (if possible) */
1656 if (noaction && fs_blocks_count) {
1657 dev_size = fs_blocks_count;
1660 retval = ext2fs_get_device_size2(device_name,
1661 EXT2_BLOCK_SIZE(&fs_param),
1664 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1665 com_err(program_name, retval,
1666 _("while trying to determine filesystem size"));
1669 if (!fs_blocks_count) {
1670 if (retval == EXT2_ET_UNIMPLEMENTED) {
1671 com_err(program_name, 0,
1672 _("Couldn't determine device size; you "
1673 "must specify\nthe size of the "
1677 if (dev_size == 0) {
1678 com_err(program_name, 0,
1679 _("Device size reported to be zero. "
1680 "Invalid partition specified, or\n\t"
1681 "partition table wasn't reread "
1682 "after running fdisk, due to\n\t"
1683 "a modified partition being busy "
1684 "and in use. You may need to reboot\n\t"
1685 "to re-read your partition table.\n"
1689 fs_blocks_count = dev_size;
1690 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1691 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1692 EXT2_BLOCK_SIZE(&fs_param))-1));
1694 } else if (!force && (fs_blocks_count > dev_size)) {
1695 com_err(program_name, 0,
1696 _("Filesystem larger than apparent device size."));
1701 profile_get_string(profile, "devices", device_name,
1702 "fs_type", 0, &fs_type);
1704 profile_get_string(profile, "devices", device_name,
1705 "usage_types", 0, &usage_types);
1708 * We have the file system (or device) size, so we can now
1709 * determine the appropriate file system types so the fs can
1710 * be appropriately configured.
1712 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1713 fs_blocks_count ? fs_blocks_count : dev_size,
1716 fprintf(stderr, _("Failed to parse fs types list\n"));
1720 /* Figure out what features should be enabled */
1723 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1724 tmp = get_string_from_profile(fs_types, "base_features",
1725 "sparse_super,filetype,resize_inode,dir_index");
1726 edit_feature(tmp, &fs_param.s_feature_compat);
1729 /* And which mount options as well */
1730 tmp = get_string_from_profile(fs_types, "default_mntopts",
1732 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1736 for (cpp = fs_types; *cpp; cpp++) {
1738 profile_get_string(profile, "fs_types", *cpp,
1739 "features", "", &tmp);
1741 edit_feature(tmp, &fs_param.s_feature_compat);
1745 tmp = get_string_from_profile(fs_types, "default_features",
1748 /* Mask off features which aren't supported by the Hurd */
1749 if (for_hurd(creator_os)) {
1750 fs_param.s_feature_incompat &= ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1751 fs_param.s_feature_ro_compat &=
1752 ~(EXT4_FEATURE_RO_COMPAT_HUGE_FILE |
1753 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1755 edit_feature(fs_features ? fs_features : tmp,
1756 &fs_param.s_feature_compat);
1760 * If the user specified features incompatible with the Hurd, complain
1762 if (for_hurd(creator_os)) {
1763 if (fs_param.s_feature_incompat &
1764 EXT2_FEATURE_INCOMPAT_FILETYPE) {
1765 fprintf(stderr, _("The HURD does not support the "
1766 "filetype feature.\n"));
1769 if (fs_param.s_feature_ro_compat &
1770 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
1771 fprintf(stderr, _("The HURD does not support the "
1772 "huge_file feature.\n"));
1775 if (fs_param.s_feature_ro_compat &
1776 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
1777 fprintf(stderr, _("The HURD does not support the "
1778 "metadata_csum feature.\n"));
1784 * We now need to do a sanity check of fs_blocks_count for
1785 * 32-bit vs 64-bit block number support.
1787 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1788 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1790 fs_param.s_log_block_size = 2;
1792 if ((fs_blocks_count > MAX_32_NUM) &&
1793 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1794 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1795 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1796 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1798 if ((fs_blocks_count > MAX_32_NUM) &&
1799 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1800 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1801 "too big to be expressed\n\t"
1802 "in 32 bits using a blocksize of %d.\n"),
1803 program_name, fs_blocks_count, device_name,
1804 EXT2_BLOCK_SIZE(&fs_param));
1808 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1810 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1811 fs_types[0] = strdup("journal");
1816 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1817 print_str_list(fs_types);
1820 if (r_opt == EXT2_GOOD_OLD_REV &&
1821 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1822 fs_param.s_feature_ro_compat)) {
1823 fprintf(stderr, _("Filesystem features not supported "
1824 "with revision 0 filesystems\n"));
1829 if (r_opt == EXT2_GOOD_OLD_REV) {
1830 fprintf(stderr, _("Sparse superblocks not supported "
1831 "with revision 0 filesystems\n"));
1834 fs_param.s_feature_ro_compat |=
1835 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1836 } else if (s_opt == 0)
1837 fs_param.s_feature_ro_compat &=
1838 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1840 if (journal_size != 0) {
1841 if (r_opt == EXT2_GOOD_OLD_REV) {
1842 fprintf(stderr, _("Journals not supported "
1843 "with revision 0 filesystems\n"));
1846 fs_param.s_feature_compat |=
1847 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1850 /* Get reserved_ratio from profile if not specified on cmd line. */
1851 if (reserved_ratio < 0.0) {
1852 reserved_ratio = get_double_from_profile(
1853 fs_types, "reserved_ratio", 5.0);
1854 if (reserved_ratio > 50 || reserved_ratio < 0) {
1855 com_err(program_name, 0,
1856 _("invalid reserved blocks percent - %lf"),
1862 if (fs_param.s_feature_incompat &
1863 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1865 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1866 fs_param.s_feature_compat = 0;
1867 fs_param.s_feature_ro_compat = 0;
1870 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1871 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1872 fprintf(stderr, _("The resize_inode and meta_bg features "
1873 "are not compatible.\n"
1874 "They can not be both enabled "
1875 "simultaneously.\n"));
1879 /* Set first meta blockgroup via an environment variable */
1880 /* (this is mostly for debugging purposes) */
1881 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1882 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1883 fs_param.s_first_meta_bg = atoi(tmp);
1885 /* Get the hardware sector sizes, if available */
1886 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1888 com_err(program_name, retval,
1889 _("while trying to determine hardware sector size"));
1892 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1894 com_err(program_name, retval,
1895 _("while trying to determine physical sector size"));
1899 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1900 lsector_size = atoi(tmp);
1901 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1902 psector_size = atoi(tmp);
1904 /* Older kernels may not have physical/logical distinction */
1906 psector_size = lsector_size;
1908 if (blocksize <= 0) {
1909 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1911 if (use_bsize == -1) {
1912 use_bsize = sys_page_size;
1913 if ((linux_version_code < (2*65536 + 6*256)) &&
1917 if (lsector_size && use_bsize < lsector_size)
1918 use_bsize = lsector_size;
1919 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1920 use_bsize = -blocksize;
1921 blocksize = use_bsize;
1922 ext2fs_blocks_count_set(&fs_param,
1923 ext2fs_blocks_count(&fs_param) /
1924 (blocksize / 1024));
1926 if (blocksize < lsector_size) { /* Impossible */
1927 com_err(program_name, EINVAL,
1928 _("while setting blocksize; too small "
1931 } else if ((blocksize < psector_size) &&
1932 (psector_size <= sys_page_size)) { /* Suboptimal */
1933 fprintf(stderr, _("Warning: specified blocksize %d is "
1934 "less than device physical sectorsize %d\n"),
1935 blocksize, psector_size);
1939 fs_param.s_log_block_size =
1940 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1941 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1943 cluster_size = get_int_from_profile(fs_types,
1946 fs_param.s_log_cluster_size =
1947 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1948 if (fs_param.s_log_cluster_size &&
1949 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
1950 com_err(program_name, 0,
1951 _("The cluster size may not be "
1952 "smaller than the block size.\n"));
1955 } else if (cluster_size) {
1956 com_err(program_name, 0,
1957 _("specifying a cluster size requires the "
1958 "bigalloc feature"));
1961 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1963 if (inode_ratio == 0) {
1964 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1966 if (inode_ratio < blocksize)
1967 inode_ratio = blocksize;
1968 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1969 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1972 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1973 retval = get_device_geometry(device_name, &fs_param, psector_size);
1976 _("warning: Unable to get device geometry for %s\n"),
1978 } else if (retval) {
1979 printf(_("%s alignment is offset by %lu bytes.\n"),
1980 device_name, retval);
1981 printf(_("This may result in very poor performance, "
1982 "(re)-partitioning suggested.\n"));
1986 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1989 * Initialize s_desc_size so that the parse_extended_opts()
1990 * can correctly handle "-E resize=NNN" if the 64-bit option
1993 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
1994 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
1996 /* This check should happen beyond the last assignment to blocksize */
1997 if (blocksize > sys_page_size) {
1999 com_err(program_name, 0,
2000 _("%d-byte blocks too big for system (max %d)"),
2001 blocksize, sys_page_size);
2004 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2005 "(max %d), forced to continue\n"),
2006 blocksize, sys_page_size);
2009 lazy_itable_init = 0;
2010 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2011 lazy_itable_init = 1;
2013 lazy_itable_init = get_bool_from_profile(fs_types,
2016 discard = get_bool_from_profile(fs_types, "discard" , discard);
2017 journal_flags |= get_bool_from_profile(fs_types,
2018 "lazy_journal_init", 0) ?
2019 EXT2_MKJOURNAL_LAZYINIT : 0;
2020 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2022 /* Get options from profile */
2023 for (cpp = fs_types; *cpp; cpp++) {
2025 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2027 parse_extended_opts(&fs_param, tmp);
2032 parse_extended_opts(&fs_param, extended_opts);
2034 /* Don't allow user to set both metadata_csum and uninit_bg bits. */
2035 if ((fs_param.s_feature_ro_compat &
2036 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2037 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
2038 fs_param.s_feature_ro_compat &=
2039 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
2041 /* Can't support bigalloc feature without extents feature */
2042 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2043 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2044 com_err(program_name, 0,
2045 _("Can't support bigalloc feature without "
2046 "extents feature"));
2051 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2052 fprintf(stderr, _("\nWarning: the bigalloc feature is still "
2053 "under development\n"
2054 "See https://ext4.wiki.kernel.org/"
2055 "index.php/Bigalloc for more information\n\n"));
2058 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2059 fprintf(stderr, _("\nWarning: the quota feature is still "
2060 "under development\n"
2061 "See https://ext4.wiki.kernel.org/"
2062 "index.php/Quota for more information\n\n"));
2064 /* Since sparse_super is the default, we would only have a problem
2065 * here if it was explicitly disabled.
2067 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2068 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2069 com_err(program_name, 0,
2070 _("reserved online resize blocks not supported "
2071 "on non-sparse filesystem"));
2075 if (fs_param.s_blocks_per_group) {
2076 if (fs_param.s_blocks_per_group < 256 ||
2077 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2078 com_err(program_name, 0,
2079 _("blocks per group count out of range"));
2085 * If the bigalloc feature is enabled, then the -g option will
2086 * specify the number of clusters per group.
2088 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2089 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2090 fs_param.s_blocks_per_group = 0;
2093 if (inode_size == 0)
2094 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2095 if (!flex_bg_size && (fs_param.s_feature_incompat &
2096 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2097 flex_bg_size = get_int_from_profile(fs_types,
2098 "flex_bg_size", 16);
2100 if (!(fs_param.s_feature_incompat &
2101 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2102 com_err(program_name, 0,
2103 _("Flex_bg feature not enabled, so "
2104 "flex_bg size may not be specified"));
2107 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2110 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2111 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2112 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2113 inode_size & (inode_size - 1)) {
2114 com_err(program_name, 0,
2115 _("invalid inode size %d (min %d/max %d)"),
2116 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2120 fs_param.s_inode_size = inode_size;
2123 /* Make sure number of inodes specified will fit in 32 bits */
2124 if (num_inodes == 0) {
2125 unsigned long long n;
2126 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2127 if (n > MAX_32_NUM) {
2128 if (fs_param.s_feature_incompat &
2129 EXT4_FEATURE_INCOMPAT_64BIT)
2130 num_inodes = MAX_32_NUM;
2132 com_err(program_name, 0,
2133 _("too many inodes (%llu), raise "
2134 "inode ratio?"), n);
2138 } else if (num_inodes > MAX_32_NUM) {
2139 com_err(program_name, 0,
2140 _("too many inodes (%llu), specify < 2^32 inodes"),
2145 * Calculate number of inodes based on the inode ratio
2147 fs_param.s_inodes_count = num_inodes ? num_inodes :
2148 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2150 if ((((unsigned long long)fs_param.s_inodes_count) *
2151 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2152 ((ext2fs_blocks_count(&fs_param)) *
2153 EXT2_BLOCK_SIZE(&fs_param))) {
2154 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2155 "(%u) too big for a\n\t"
2156 "filesystem with %llu blocks, "
2157 "specify higher inode_ratio (-i)\n\t"
2158 "or lower inode count (-N).\n"),
2159 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2160 fs_param.s_inodes_count,
2161 (unsigned long long) ext2fs_blocks_count(&fs_param));
2166 * Calculate number of blocks to reserve
2168 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2169 ext2fs_blocks_count(&fs_param) / 100.0);
2175 static int should_do_undo(const char *name)
2180 struct ext2_super_block super;
2181 io_manager manager = unix_io_manager;
2182 int csum_flag, force_undo;
2184 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2185 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
2186 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
2187 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2188 if (!force_undo && (!csum_flag || !lazy_itable_init))
2191 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2194 * We don't handle error cases instead we
2195 * declare that the file system doesn't exist
2196 * and let the rest of mke2fs take care of
2203 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2204 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2210 #if defined(WORDS_BIGENDIAN)
2211 s_magic = ext2fs_swab16(super.s_magic);
2213 s_magic = super.s_magic;
2216 if (s_magic == EXT2_SUPER_MAGIC)
2220 io_channel_close(channel);
2227 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2229 errcode_t retval = ENOMEM;
2230 char *tdb_dir = NULL, *tdb_file = NULL;
2231 char *dev_name, *tmp_name;
2232 int free_tdb_dir = 0;
2235 * Configuration via a conf file would be
2238 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2240 profile_get_string(profile, "defaults",
2241 "undo_dir", 0, "/var/lib/e2fsprogs",
2246 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2247 access(tdb_dir, W_OK))
2250 tmp_name = strdup(name);
2253 dev_name = basename(tmp_name);
2254 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2259 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2262 if (!access(tdb_file, F_OK)) {
2263 if (unlink(tdb_file) < 0) {
2269 set_undo_io_backing_manager(*io_ptr);
2270 *io_ptr = undo_io_manager;
2271 retval = set_undo_io_backup_file(tdb_file);
2274 printf(_("Overwriting existing filesystem; this can be undone "
2275 "using the command:\n"
2276 " e2undo %s %s\n\n"), tdb_file, name);
2287 com_err(program_name, retval,
2288 _("while trying to setup undo file\n"));
2292 static int mke2fs_discard_device(ext2_filsys fs)
2294 struct ext2fs_numeric_progress_struct progress;
2295 blk64_t blocks = ext2fs_blocks_count(fs->super);
2296 blk64_t count = DISCARD_STEP_MB;
2301 * Let's try if discard really works on the device, so
2302 * we do not print numeric progress resulting in failure
2305 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2308 cur = fs->blocksize;
2310 count *= (1024 * 1024);
2311 count /= fs->blocksize;
2313 ext2fs_numeric_progress_init(fs, &progress,
2314 _("Discarding device blocks: "),
2316 while (cur < blocks) {
2317 ext2fs_numeric_progress_update(fs, &progress, cur);
2319 if (cur + count > blocks)
2320 count = blocks - cur;
2322 retval = io_channel_discard(fs->io, cur, count);
2329 ext2fs_numeric_progress_close(fs, &progress,
2332 printf("%s\n",error_message(retval));
2334 ext2fs_numeric_progress_close(fs, &progress,
2340 static void fix_cluster_bg_counts(ext2_filsys fs)
2342 blk64_t cluster, num_clusters, tot_free;
2344 int grp_free, num_free, group;
2346 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2347 tot_free = num_free = group = grp_free = 0;
2348 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2349 cluster < num_clusters; cluster++) {
2350 if (!ext2fs_test_block_bitmap2(fs->block_map,
2351 EXT2FS_C2B(fs, cluster))) {
2356 if ((num == fs->super->s_clusters_per_group) ||
2357 (cluster == num_clusters-1)) {
2358 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2359 ext2fs_group_desc_csum_set(fs, group);
2365 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2368 static int create_quota_inodes(ext2_filsys fs)
2372 quota_init_context(&qctx, fs, -1);
2373 quota_compute_usage(qctx);
2374 quota_write_inode(qctx, quotatype);
2375 quota_release_context(&qctx);
2380 int main (int argc, char *argv[])
2382 errcode_t retval = 0;
2384 badblocks_list bb_list = 0;
2385 unsigned int journal_blocks;
2386 unsigned int i, checkinterval;
2392 char tdb_string[40];
2394 int itable_zeroed = 0;
2397 setlocale(LC_MESSAGES, "");
2398 setlocale(LC_CTYPE, "");
2399 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2400 textdomain(NLS_CAT_NAME);
2401 set_com_err_gettext(gettext);
2405 #ifdef CONFIG_TESTIO_DEBUG
2406 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2407 io_ptr = test_io_manager;
2408 test_io_backing_manager = unix_io_manager;
2411 io_ptr = unix_io_manager;
2413 if (should_do_undo(device_name)) {
2414 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2420 * Initialize the superblock....
2422 flags = EXT2_FLAG_EXCLUSIVE;
2424 flags |= EXT2_FLAG_DIRECT_IO;
2425 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2428 flags |= EXT2_FLAG_64BITS;
2430 * By default, we print how many inode tables or block groups
2431 * or whatever we've written so far. The quiet flag disables
2432 * this, along with a lot of other output.
2435 flags |= EXT2_FLAG_PRINT_PROGRESS;
2436 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2438 com_err(device_name, retval, _("while setting up superblock"));
2441 fs->progress_ops = &ext2fs_numeric_progress_ops;
2443 /* Check the user's mkfs options for metadata checksumming */
2445 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2446 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
2447 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2448 EXT3_FEATURE_INCOMPAT_EXTENTS))
2449 printf(_("Extents are not enabled. The file extent "
2450 "tree can be checksummed, whereas block maps "
2451 "cannot. Not enabling extents reduces the "
2452 "coverage of metadata checksumming. "
2453 "Pass -O extents to rectify.\n"));
2454 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2455 EXT4_FEATURE_INCOMPAT_64BIT))
2456 printf(_("64-bit filesystem support is not "
2457 "enabled. The larger fields afforded by "
2458 "this feature enable full-strength "
2459 "checksumming. Pass -O 64bit to rectify.\n"));
2462 /* Calculate journal blocks */
2463 if (!journal_device && ((journal_size) ||
2464 (fs_param.s_feature_compat &
2465 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2466 journal_blocks = figure_journal_size(journal_size, fs);
2468 /* Can't undo discard ... */
2469 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2470 retval = mke2fs_discard_device(fs);
2471 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2473 printf(_("Discard succeeded and will return 0s "
2474 " - skipping inode table wipe\n"));
2475 lazy_itable_init = 1;
2480 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2481 32768 : fs->blocksize * 8);
2482 io_channel_set_options(fs->io, tdb_string);
2484 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2485 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2487 if ((fs_param.s_feature_incompat &
2488 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2489 (fs_param.s_feature_ro_compat &
2490 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2491 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2492 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
2493 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2494 fs->super->s_kbytes_written = 1;
2497 * Wipe out the old on-disk superblock
2500 zap_sector(fs, 2, 6);
2503 * Parse or generate a UUID for the filesystem
2506 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2507 com_err(device_name, 0, "could not parse UUID: %s\n",
2512 uuid_generate(fs->super->s_uuid);
2513 ext2fs_init_csum_seed(fs);
2516 * Initialize the directory index variables
2518 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2520 hash_alg = e2p_string2hash(hash_alg_str);
2522 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2524 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2527 * Periodic checks can be enabled/disabled via config file.
2528 * Note we override the kernel include file's idea of what the default
2529 * check interval (never) should be. It's a good idea to check at
2530 * least *occasionally*, specially since servers will never rarely get
2531 * to reboot, since Linux is so robust these days. :-)
2533 * 180 days (six months) seems like a good value.
2535 #ifdef EXT2_DFL_CHECKINTERVAL
2536 #undef EXT2_DFL_CHECKINTERVAL
2538 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2540 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2541 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2542 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2544 * Add "jitter" to the superblock's check interval so that we
2545 * don't check all the filesystems at the same time. We use a
2546 * kludgy hack of using the UUID to derive a random jitter value
2548 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2549 val += fs->super->s_uuid[i];
2550 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2552 fs->super->s_max_mnt_count = -1;
2555 * Override the creator OS, if applicable
2557 if (creator_os && !set_os(fs->super, creator_os)) {
2558 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2563 * For the Hurd, we will turn off filetype since it doesn't
2566 if (fs->super->s_creator_os == EXT2_OS_HURD)
2567 fs->super->s_feature_incompat &=
2568 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2571 * Set the volume label...
2574 memset(fs->super->s_volume_name, 0,
2575 sizeof(fs->super->s_volume_name));
2576 strncpy(fs->super->s_volume_name, volume_label,
2577 sizeof(fs->super->s_volume_name));
2581 * Set the last mount directory
2584 memset(fs->super->s_last_mounted, 0,
2585 sizeof(fs->super->s_last_mounted));
2586 strncpy(fs->super->s_last_mounted, mount_dir,
2587 sizeof(fs->super->s_last_mounted));
2590 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2591 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2592 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
2594 if (!quiet || noaction)
2600 if (fs->super->s_feature_incompat &
2601 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2602 create_journal_dev(fs);
2603 exit(ext2fs_close(fs) ? 1 : 0);
2606 if (bad_blocks_filename)
2607 read_bb_file(fs, &bb_list, bad_blocks_filename);
2609 test_disk(fs, &bb_list);
2611 handle_bad_blocks(fs, bb_list);
2612 fs->stride = fs_stride = fs->super->s_raid_stride;
2614 printf(_("Allocating group tables: "));
2615 retval = ext2fs_allocate_tables(fs);
2617 com_err(program_name, retval,
2618 _("while trying to allocate filesystem tables"));
2622 printf(_("done \n"));
2624 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2626 com_err(program_name, retval,
2627 _("\n\twhile converting subcluster bitmap"));
2632 fs->super->s_state |= EXT2_ERROR_FS;
2633 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2635 * The command "mke2fs -S" is used to recover
2636 * corrupted file systems, so do not mark any of the
2637 * inodes as unused; we want e2fsck to consider all
2638 * inodes as potentially containing recoverable data.
2640 if (ext2fs_has_group_desc_csum(fs)) {
2641 for (i = 0; i < fs->group_desc_count; i++)
2642 ext2fs_bg_itable_unused_set(fs, i, 0);
2645 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2646 blk64_t rsv = 65536 / fs->blocksize;
2647 blk64_t blocks = ext2fs_blocks_count(fs->super);
2651 #ifdef ZAP_BOOTBLOCK
2652 zap_sector(fs, 0, 2);
2656 * Wipe out any old MD RAID (or other) metadata at the end
2657 * of the device. This will also verify that the device is
2658 * as large as we think. Be careful with very small devices.
2660 start = (blocks & ~(rsv - 1));
2664 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2668 com_err(program_name, retval,
2669 _("while zeroing block %llu at end of filesystem"),
2672 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2673 create_root_dir(fs);
2674 create_lost_and_found(fs);
2676 create_bad_block_inode(fs, bb_list);
2677 if (fs->super->s_feature_compat &
2678 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2679 retval = ext2fs_create_resize_inode(fs);
2681 com_err("ext2fs_create_resize_inode", retval,
2682 _("while reserving blocks for online resize"));
2688 if (journal_device) {
2692 check_plausibility(journal_device);
2693 check_mount(journal_device, force, _("journal"));
2695 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2696 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2697 fs->blocksize, unix_io_manager, &jfs);
2699 com_err(program_name, retval,
2700 _("while trying to open journal device %s\n"),
2705 printf(_("Adding journal to device %s: "),
2709 retval = ext2fs_add_journal_device(fs, jfs);
2711 com_err (program_name, retval,
2712 _("\n\twhile trying to add journal to device %s"),
2717 printf(_("done\n"));
2719 free(journal_device);
2720 } else if ((journal_size) ||
2721 (fs_param.s_feature_compat &
2722 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2724 printf(_("Skipping journal creation in super-only mode\n"));
2725 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2729 if (!journal_blocks) {
2730 fs->super->s_feature_compat &=
2731 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2735 printf(_("Creating journal (%u blocks): "),
2739 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2742 com_err (program_name, retval,
2743 _("\n\twhile trying to create journal"));
2747 printf(_("done\n"));
2751 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2752 retval = ext2fs_mmp_init(fs);
2754 fprintf(stderr, _("\nError while enabling multiple "
2755 "mount protection feature."));
2759 printf(_("Multiple mount protection is enabled "
2760 "with update interval %d seconds.\n"),
2761 fs->super->s_mmp_update_interval);
2764 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2765 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2766 fix_cluster_bg_counts(fs);
2767 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2768 EXT4_FEATURE_RO_COMPAT_QUOTA))
2769 create_quota_inodes(fs);
2772 printf(_("Writing superblocks and "
2773 "filesystem accounting information: "));
2774 checkinterval = fs->super->s_checkinterval;
2775 max_mnt_count = fs->super->s_max_mnt_count;
2776 retval = ext2fs_close(fs);
2779 _("\nWarning, had trouble writing out superblocks."));
2780 } else if (!quiet) {
2781 printf(_("done\n\n"));
2782 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2783 print_check_message(max_mnt_count, checkinterval);
2786 remove_error_table(&et_ext2_error_table);
2787 remove_error_table(&et_prof_error_table);
2788 profile_release(profile);
2789 for (i=0; fs_types[i]; i++)