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 */
100 static struct ext2_super_block fs_param;
101 static char *fs_uuid = NULL;
102 static char *creator_os;
103 static char *volume_label;
104 static char *mount_dir;
105 char *journal_device;
106 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
107 static char **fs_types;
109 static profile_t profile;
111 static int sys_page_size = 4096;
112 static 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, "%s",
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, "%s",
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, "%s",
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;
390 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
392 com_err("ext2fs_mkdir", retval, "%s",
393 _("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, "%s",
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, "%s",
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, "%s",
430 _("while creating /lost+found"));
434 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
436 com_err("ext2_lookup", retval, "%s",
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, "%s",
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, "%s",
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, "%s",
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, "%s",
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("%s", _("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;
709 fprintf(stderr, "%s",
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, "desc-size") == 0 ||
727 strcmp(token, "desc_size") == 0) {
730 if (!(fs_param.s_feature_incompat &
731 EXT4_FEATURE_INCOMPAT_64BIT)) {
733 _("%s requires '-O 64bit'\n"), token);
737 if (param->s_reserved_gdt_blocks != 0) {
739 _("'%s' must be before 'resize=%u'\n"),
740 token, param->s_reserved_gdt_blocks);
749 desc_size = strtoul(arg, &p, 0);
750 if (*p || (desc_size & (desc_size - 1))) {
752 _("Invalid desc_size: '%s'\n"), arg);
756 param->s_desc_size = desc_size;
757 } else if (strcmp(token, "mmp_update_interval") == 0) {
763 param->s_mmp_update_interval = strtoul(arg, &p, 0);
766 _("Invalid mmp_update_interval: %s\n"),
771 } else if (strcmp(token, "stride") == 0) {
777 param->s_raid_stride = strtoul(arg, &p, 0);
780 _("Invalid stride parameter: %s\n"),
785 } else if (strcmp(token, "stripe-width") == 0 ||
786 strcmp(token, "stripe_width") == 0) {
792 param->s_raid_stripe_width = strtoul(arg, &p, 0);
795 _("Invalid stripe-width parameter: %s\n"),
800 } else if (!strcmp(token, "resize")) {
802 unsigned long bpg, rsv_groups;
803 unsigned long group_desc_count, desc_blocks;
804 unsigned int gdpb, blocksize;
813 resize = parse_num_blocks2(arg,
814 param->s_log_block_size);
818 _("Invalid resize parameter: %s\n"),
823 if (resize <= ext2fs_blocks_count(param)) {
824 fprintf(stderr, "%s",
825 _("The resize maximum must be greater "
826 "than the filesystem size.\n"));
831 blocksize = EXT2_BLOCK_SIZE(param);
832 bpg = param->s_blocks_per_group;
835 gdpb = EXT2_DESC_PER_BLOCK(param);
836 group_desc_count = (__u32) ext2fs_div64_ceil(
837 ext2fs_blocks_count(param), bpg);
838 desc_blocks = (group_desc_count +
840 rsv_groups = ext2fs_div64_ceil(resize, bpg);
841 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
843 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
844 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
847 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
848 fprintf(stderr, "%s",
849 _("On-line resizing not supported with revision 0 filesystems\n"));
853 param->s_feature_compat |=
854 EXT2_FEATURE_COMPAT_RESIZE_INODE;
856 param->s_reserved_gdt_blocks = rsv_gdb;
858 } else if (!strcmp(token, "test_fs")) {
859 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
860 } else if (!strcmp(token, "lazy_itable_init")) {
862 lazy_itable_init = strtoul(arg, &p, 0);
864 lazy_itable_init = 1;
865 } else if (!strcmp(token, "lazy_journal_init")) {
867 journal_flags |= strtoul(arg, &p, 0) ?
868 EXT2_MKJOURNAL_LAZYINIT : 0;
870 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
871 } else if (!strcmp(token, "root_owner")) {
873 root_uid = strtoul(arg, &p, 0);
876 _("Invalid root_owner: '%s'\n"),
882 root_gid = strtoul(p, &p, 0);
885 _("Invalid root_owner: '%s'\n"),
894 } else if (!strcmp(token, "discard")) {
896 } else if (!strcmp(token, "nodiscard")) {
898 } else if (!strcmp(token, "quotatype")) {
904 if (!strncmp(arg, "usr", 3)) {
906 } else if (!strncmp(arg, "grp", 3)) {
910 _("Invalid quotatype parameter: %s\n"),
921 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
922 "Extended options are separated by commas, "
923 "and may take an argument which\n"
924 "\tis set off by an equals ('=') sign.\n\n"
925 "Valid extended options are:\n"
926 "\tstride=<RAID per-disk data chunk in blocks>\n"
927 "\tstripe-width=<RAID stride * data disks in blocks>\n"
928 "\tresize=<resize maximum size in blocks>\n"
929 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
930 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
931 "\troot_uid=<uid of root directory>\n"
932 "\troot_gid=<gid of root directory>\n"
936 "\tquotatype=<usr OR grp>\n\n"),
937 badopt ? badopt : "");
941 if (param->s_raid_stride &&
942 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
943 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
944 "multiple of stride %u.\n\n"),
945 param->s_raid_stripe_width, param->s_raid_stride);
950 static __u32 ok_features[3] = {
952 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
953 EXT2_FEATURE_COMPAT_RESIZE_INODE |
954 EXT2_FEATURE_COMPAT_DIR_INDEX |
955 EXT2_FEATURE_COMPAT_EXT_ATTR,
957 EXT2_FEATURE_INCOMPAT_FILETYPE|
958 EXT3_FEATURE_INCOMPAT_EXTENTS|
959 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
960 EXT2_FEATURE_INCOMPAT_META_BG|
961 EXT4_FEATURE_INCOMPAT_FLEX_BG|
962 EXT4_FEATURE_INCOMPAT_MMP |
963 EXT4_FEATURE_INCOMPAT_64BIT,
965 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
966 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
967 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
968 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
969 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
970 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
971 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
973 EXT4_FEATURE_RO_COMPAT_QUOTA|
975 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
979 static void syntax_err_report(const char *filename, long err, int line_num)
982 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
983 filename, line_num, error_message(err));
987 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
989 static void edit_feature(const char *str, __u32 *compat_array)
994 if (e2p_edit_feature(str, compat_array, ok_features)) {
995 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1001 static void edit_mntopts(const char *str, __u32 *mntopts)
1006 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1007 fprintf(stderr, _("Invalid mount option set: %s\n"),
1019 static errcode_t init_list(struct str_list *sl)
1023 sl->list = malloc((sl->max+1) * sizeof(char *));
1030 static errcode_t push_string(struct str_list *sl, const char *str)
1034 if (sl->num >= sl->max) {
1036 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1039 sl->list = new_list;
1041 sl->list[sl->num] = malloc(strlen(str)+1);
1042 if (sl->list[sl->num] == 0)
1044 strcpy(sl->list[sl->num], str);
1046 sl->list[sl->num] = 0;
1050 static void print_str_list(char **list)
1054 for (cpp = list; *cpp; cpp++) {
1055 printf("'%s'", *cpp);
1057 fputs(", ", stdout);
1059 fputc('\n', stdout);
1063 * Return TRUE if the profile has the given subsection
1065 static int profile_has_subsection(profile_t prof, const char *section,
1066 const char *subsection)
1069 const char *names[4];
1074 names[1] = subsection;
1077 if (profile_iterator_create(prof, names,
1078 PROFILE_ITER_LIST_SECTION |
1079 PROFILE_ITER_RELATIONS_ONLY, &state))
1082 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1087 profile_iterator_free(&state);
1091 static char **parse_fs_type(const char *fs_type,
1092 const char *usage_types,
1093 struct ext2_super_block *sb,
1094 blk64_t fs_blocks_count,
1097 const char *ext_type = 0;
1099 char *profile_type = 0;
1101 const char *size_type;
1102 struct str_list list;
1103 unsigned long long meg;
1104 int is_hurd = for_hurd(creator_os);
1106 if (init_list(&list))
1113 else if (!strcmp(program_name, "mke3fs"))
1115 else if (!strcmp(program_name, "mke4fs"))
1117 else if (progname) {
1118 ext_type = strrchr(progname, '/');
1122 ext_type = progname;
1124 if (!strncmp(ext_type, "mkfs.", 5)) {
1126 if (ext_type[0] == 0)
1133 profile_get_string(profile, "defaults", "fs_type", 0,
1134 "ext2", &profile_type);
1135 ext_type = profile_type;
1136 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1141 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1142 strcmp(ext_type, "ext2")) {
1143 printf(_("\nYour mke2fs.conf file does not define the "
1144 "%s filesystem type.\n"), ext_type);
1145 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1146 !strcmp(ext_type, "ext4dev")) {
1147 printf("%s", _("You probably need to install an "
1148 "updated mke2fs.conf file.\n\n"));
1151 printf("%s", _("Aborting...\n"));
1156 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1157 if (fs_blocks_count < 3 * meg)
1158 size_type = "floppy";
1159 else if (fs_blocks_count < 512 * meg)
1160 size_type = "small";
1161 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1162 size_type = "default";
1163 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1169 usage_types = size_type;
1171 parse_str = malloc(strlen(usage_types)+1);
1177 strcpy(parse_str, usage_types);
1180 push_string(&list, ext_type);
1183 t = strchr(cp, ',');
1188 if (profile_has_subsection(profile, "fs_types", cp))
1189 push_string(&list, cp);
1190 else if (strcmp(cp, "default") != 0)
1192 _("\nWarning: the fs_type %s is not "
1193 "defined in mke2fs.conf\n\n"),
1204 push_string(&list, "hurd");
1208 static char *get_string_from_profile(char **types, const char *opt,
1209 const char *def_val)
1214 for (i=0; types[i]; i++);
1215 for (i-=1; i >=0 ; i--) {
1216 profile_get_string(profile, "fs_types", types[i],
1221 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1225 static int get_int_from_profile(char **types, const char *opt, int def_val)
1230 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1231 for (cpp = types; *cpp; cpp++)
1232 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1236 static double get_double_from_profile(char **types, const char *opt,
1242 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1243 for (cpp = types; *cpp; cpp++)
1244 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1248 static int get_bool_from_profile(char **types, const char *opt, int def_val)
1253 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1254 for (cpp = types; *cpp; cpp++)
1255 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1259 extern const char *mke2fs_default_profile;
1260 static const char *default_files[] = { "<default>", 0 };
1262 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1264 * Sets the geometry of a device (stripe/stride), and returns the
1265 * device's alignment offset, if any, or a negative error.
1267 static int get_device_geometry(const char *file,
1268 struct ext2_super_block *fs_param,
1275 unsigned long min_io;
1276 unsigned long opt_io;
1277 struct stat statbuf;
1279 /* Nothing to do for a regular file */
1280 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1283 pr = blkid_new_probe_from_filename(file);
1287 tp = blkid_probe_get_topology(pr);
1291 min_io = blkid_topology_get_minimum_io_size(tp);
1292 opt_io = blkid_topology_get_optimal_io_size(tp);
1293 blocksize = EXT2_BLOCK_SIZE(fs_param);
1294 if ((min_io == 0) && (psector_size > blocksize))
1295 min_io = psector_size;
1296 if ((opt_io == 0) && min_io)
1298 if ((opt_io == 0) && (psector_size > blocksize))
1299 opt_io = psector_size;
1301 /* setting stripe/stride to blocksize is pointless */
1302 if (min_io > blocksize)
1303 fs_param->s_raid_stride = min_io / blocksize;
1304 if (opt_io > blocksize)
1305 fs_param->s_raid_stripe_width = opt_io / blocksize;
1307 rc = blkid_topology_get_alignment_offset(tp);
1309 blkid_free_probe(pr);
1314 static void PRS(int argc, char *argv[])
1317 int cluster_size = 0;
1320 int inode_ratio = 0;
1322 unsigned long flex_bg_size = 0;
1323 double reserved_ratio = -1.0;
1324 int lsector_size = 0, psector_size = 0;
1325 int show_version_only = 0;
1326 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1328 char * oldpath = getenv("PATH");
1329 char * extended_opts = 0;
1331 char * usage_types = 0;
1334 * NOTE: A few words about fs_blocks_count and blocksize:
1336 * Initially, blocksize is set to zero, which implies 1024.
1337 * If -b is specified, blocksize is updated to the user's value.
1339 * Next, the device size or the user's "blocks" command line argument
1340 * is used to set fs_blocks_count; the units are blocksize.
1342 * Later, if blocksize hasn't been set and the profile specifies a
1343 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1344 * appropriately. Note the change in units!
1346 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1348 blk64_t fs_blocks_count = 0;
1353 int s_opt = -1, r_opt = -1;
1354 char *fs_features = 0;
1357 int pathlen = sizeof(PATH_SET) + 1;
1360 pathlen += strlen(oldpath);
1361 newpath = malloc(pathlen);
1363 fprintf(stderr, "%s",
1364 _("Couldn't allocate memory for new PATH.\n"));
1367 strcpy(newpath, PATH_SET);
1369 /* Update our PATH to include /sbin */
1371 strcat (newpath, ":");
1372 strcat (newpath, oldpath);
1376 tmp = getenv("MKE2FS_SYNC");
1378 sync_kludge = atoi(tmp);
1380 /* Determine the system page size if possible */
1382 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1383 #define _SC_PAGESIZE _SC_PAGE_SIZE
1386 sysval = sysconf(_SC_PAGESIZE);
1388 sys_page_size = sysval;
1389 #endif /* _SC_PAGESIZE */
1390 #endif /* HAVE_SYSCONF */
1392 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1394 profile_set_syntax_err_cb(syntax_err_report);
1395 retval = profile_init(config_fn, &profile);
1396 if (retval == ENOENT) {
1397 retval = profile_init(default_files, &profile);
1400 retval = profile_set_default(profile, mke2fs_default_profile);
1403 } else if (retval) {
1405 fprintf(stderr, _("Couldn't init profile successfully"
1406 " (error: %ld).\n"), retval);
1410 setbuf(stdout, NULL);
1411 setbuf(stderr, NULL);
1412 add_error_table(&et_ext2_error_table);
1413 add_error_table(&et_prof_error_table);
1414 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1415 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1422 linux_version_code = parse_version_number(ut.release);
1423 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1424 fs_param.s_rev_level = 0;
1427 if (argc && *argv) {
1428 program_name = get_progname(*argv);
1430 /* If called as mkfs.ext3, create a journal inode */
1431 if (!strcmp(program_name, "mkfs.ext3") ||
1432 !strcmp(program_name, "mke3fs"))
1436 while ((c = getopt (argc, argv,
1437 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1440 blocksize = parse_num_blocks2(optarg, -1);
1441 b = (blocksize > 0) ? blocksize : -blocksize;
1442 if (b < EXT2_MIN_BLOCK_SIZE ||
1443 b > EXT2_MAX_BLOCK_SIZE) {
1444 com_err(program_name, 0,
1445 _("invalid block size - %s"), optarg);
1448 if (blocksize > 4096)
1449 fprintf(stderr, _("Warning: blocksize %d not "
1450 "usable on most systems.\n"),
1453 fs_param.s_log_block_size =
1454 int_log2(blocksize >>
1455 EXT2_MIN_BLOCK_LOG_SIZE);
1457 case 'c': /* Check for bad blocks */
1461 cluster_size = parse_num_blocks2(optarg, -1);
1462 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1463 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1464 com_err(program_name, 0,
1465 _("invalid cluster size - %s"),
1474 com_err(program_name, 0, "%s",
1475 _("'-R' is deprecated, use '-E' instead"));
1478 extended_opts = optarg;
1484 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1486 com_err(program_name, 0, "%s",
1487 _("Illegal number for blocks per group"));
1490 if ((fs_param.s_blocks_per_group % 8) != 0) {
1491 com_err(program_name, 0, "%s",
1492 _("blocks per group must be multiple of 8"));
1497 flex_bg_size = strtoul(optarg, &tmp, 0);
1499 com_err(program_name, 0, "%s",
1500 _("Illegal number for flex_bg size"));
1503 if (flex_bg_size < 1 ||
1504 (flex_bg_size & (flex_bg_size-1)) != 0) {
1505 com_err(program_name, 0, "%s",
1506 _("flex_bg size must be a power of 2"));
1511 inode_ratio = strtoul(optarg, &tmp, 0);
1512 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1513 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1515 com_err(program_name, 0,
1516 _("invalid inode ratio %s (min %d/max %d)"),
1517 optarg, EXT2_MIN_BLOCK_SIZE,
1518 EXT2_MAX_BLOCK_SIZE * 1024);
1523 inode_size = strtoul(optarg, &tmp, 0);
1525 com_err(program_name, 0,
1526 _("invalid inode size - %s"), optarg);
1535 parse_journal_opts(optarg);
1538 fprintf(stderr, "%s",
1539 _("Warning: -K option is deprecated and "
1540 "should not be used anymore. Use "
1541 "\'-E nodiscard\' extended option "
1546 bad_blocks_filename = realloc(bad_blocks_filename,
1547 strlen(optarg) + 1);
1548 if (!bad_blocks_filename) {
1549 com_err(program_name, ENOMEM, "%s",
1550 _("in malloc for bad_blocks_filename"));
1553 strcpy(bad_blocks_filename, optarg);
1556 volume_label = optarg;
1559 reserved_ratio = strtod(optarg, &tmp);
1560 if ( *tmp || reserved_ratio > 50 ||
1561 reserved_ratio < 0) {
1562 com_err(program_name, 0,
1563 _("invalid reserved blocks percent - %s"),
1575 num_inodes = strtoul(optarg, &tmp, 0);
1577 com_err(program_name, 0,
1578 _("bad num inodes - %s"), optarg);
1583 creator_os = optarg;
1586 fs_features = optarg;
1592 r_opt = strtoul(optarg, &tmp, 0);
1594 com_err(program_name, 0,
1595 _("bad revision level - %s"), optarg);
1598 fs_param.s_rev_level = r_opt;
1600 case 's': /* deprecated */
1601 s_opt = atoi(optarg);
1608 com_err(program_name, 0, "%s",
1609 _("The -t option may only be used once"));
1612 fs_type = strdup(optarg);
1616 com_err(program_name, 0, "%s",
1617 _("The -T option may only be used once"));
1620 usage_types = strdup(optarg);
1629 /* Print version number and exit */
1630 show_version_only++;
1636 if ((optind == argc) && !show_version_only)
1638 device_name = argv[optind++];
1640 if (!quiet || show_version_only)
1641 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1644 if (show_version_only) {
1645 fprintf(stderr, _("\tUsing %s\n"),
1646 error_message(EXT2_ET_BASE));
1651 * If there's no blocksize specified and there is a journal
1652 * device, use it to figure out the blocksize
1654 if (blocksize <= 0 && journal_device) {
1658 #ifdef CONFIG_TESTIO_DEBUG
1659 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1660 io_ptr = test_io_manager;
1661 test_io_backing_manager = unix_io_manager;
1664 io_ptr = unix_io_manager;
1665 retval = ext2fs_open(journal_device,
1666 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1669 com_err(program_name, retval,
1670 _("while trying to open journal device %s\n"),
1674 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1675 com_err(program_name, 0,
1676 _("Journal dev blocksize (%d) smaller than "
1677 "minimum blocksize %d\n"), jfs->blocksize,
1681 blocksize = jfs->blocksize;
1682 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1683 fs_param.s_log_block_size =
1684 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1688 if (optind < argc) {
1689 fs_blocks_count = parse_num_blocks2(argv[optind++],
1690 fs_param.s_log_block_size);
1691 if (!fs_blocks_count) {
1692 com_err(program_name, 0,
1693 _("invalid blocks '%s' on device '%s'"),
1694 argv[optind - 1], device_name);
1702 check_plausibility(device_name);
1703 check_mount(device_name, force, _("filesystem"));
1705 /* Determine the size of the device (if possible) */
1706 if (noaction && fs_blocks_count) {
1707 dev_size = fs_blocks_count;
1710 retval = ext2fs_get_device_size2(device_name,
1711 EXT2_BLOCK_SIZE(&fs_param),
1714 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1715 com_err(program_name, retval, "%s",
1716 _("while trying to determine filesystem size"));
1719 if (!fs_blocks_count) {
1720 if (retval == EXT2_ET_UNIMPLEMENTED) {
1721 com_err(program_name, 0, "%s",
1722 _("Couldn't determine device size; you "
1723 "must specify\nthe size of the "
1727 if (dev_size == 0) {
1728 com_err(program_name, 0, "%s",
1729 _("Device size reported to be zero. "
1730 "Invalid partition specified, or\n\t"
1731 "partition table wasn't reread "
1732 "after running fdisk, due to\n\t"
1733 "a modified partition being busy "
1734 "and in use. You may need to reboot\n\t"
1735 "to re-read your partition table.\n"
1739 fs_blocks_count = dev_size;
1740 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1741 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1742 EXT2_BLOCK_SIZE(&fs_param))-1));
1744 } else if (!force && (fs_blocks_count > dev_size)) {
1745 com_err(program_name, 0, "%s",
1746 _("Filesystem larger than apparent device size."));
1751 profile_get_string(profile, "devices", device_name,
1752 "fs_type", 0, &fs_type);
1754 profile_get_string(profile, "devices", device_name,
1755 "usage_types", 0, &usage_types);
1758 * We have the file system (or device) size, so we can now
1759 * determine the appropriate file system types so the fs can
1760 * be appropriately configured.
1762 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1763 fs_blocks_count ? fs_blocks_count : dev_size,
1766 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1770 /* Figure out what features should be enabled */
1773 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1774 tmp = get_string_from_profile(fs_types, "base_features",
1775 "sparse_super,filetype,resize_inode,dir_index");
1776 edit_feature(tmp, &fs_param.s_feature_compat);
1779 /* And which mount options as well */
1780 tmp = get_string_from_profile(fs_types, "default_mntopts",
1782 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1786 for (cpp = fs_types; *cpp; cpp++) {
1788 profile_get_string(profile, "fs_types", *cpp,
1789 "features", "", &tmp);
1791 edit_feature(tmp, &fs_param.s_feature_compat);
1795 tmp = get_string_from_profile(fs_types, "default_features",
1798 /* Mask off features which aren't supported by the Hurd */
1799 if (for_hurd(creator_os)) {
1800 fs_param.s_feature_incompat &= ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1801 fs_param.s_feature_ro_compat &=
1802 ~(EXT4_FEATURE_RO_COMPAT_HUGE_FILE |
1803 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1805 edit_feature(fs_features ? fs_features : tmp,
1806 &fs_param.s_feature_compat);
1810 * If the user specified features incompatible with the Hurd, complain
1812 if (for_hurd(creator_os)) {
1813 if (fs_param.s_feature_incompat &
1814 EXT2_FEATURE_INCOMPAT_FILETYPE) {
1815 fprintf(stderr, _("The HURD does not support the "
1816 "filetype feature.\n"));
1819 if (fs_param.s_feature_ro_compat &
1820 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
1821 fprintf(stderr, _("The HURD does not support the "
1822 "huge_file feature.\n"));
1825 if (fs_param.s_feature_ro_compat &
1826 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
1827 fprintf(stderr, _("The HURD does not support the "
1828 "metadata_csum feature.\n"));
1833 /* Get the hardware sector sizes, if available */
1834 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1836 com_err(program_name, retval, "%s",
1837 _("while trying to determine hardware sector size"));
1840 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1842 com_err(program_name, retval, "%s",
1843 _("while trying to determine physical sector size"));
1847 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1849 lsector_size = atoi(tmp);
1850 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1852 psector_size = atoi(tmp);
1854 /* Older kernels may not have physical/logical distinction */
1856 psector_size = lsector_size;
1858 if (blocksize <= 0) {
1859 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1861 if (use_bsize == -1) {
1862 use_bsize = sys_page_size;
1863 if ((linux_version_code < (2*65536 + 6*256)) &&
1867 if (lsector_size && use_bsize < lsector_size)
1868 use_bsize = lsector_size;
1869 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1870 use_bsize = -blocksize;
1871 blocksize = use_bsize;
1872 fs_blocks_count /= (blocksize / 1024);
1874 if (blocksize < lsector_size) { /* Impossible */
1875 com_err(program_name, EINVAL, "%s",
1876 _("while setting blocksize; too small "
1879 } else if ((blocksize < psector_size) &&
1880 (psector_size <= sys_page_size)) { /* Suboptimal */
1881 fprintf(stderr, _("Warning: specified blocksize %d is "
1882 "less than device physical sectorsize %d\n"),
1883 blocksize, psector_size);
1887 fs_param.s_log_block_size =
1888 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1891 * We now need to do a sanity check of fs_blocks_count for
1892 * 32-bit vs 64-bit block number support.
1894 if ((fs_blocks_count > MAX_32_NUM) &&
1895 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1896 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1897 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1898 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1900 if ((fs_blocks_count > MAX_32_NUM) &&
1901 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1902 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1903 "too big to be expressed\n\t"
1904 "in 32 bits using a blocksize of %d.\n"),
1905 program_name, fs_blocks_count, device_name,
1906 EXT2_BLOCK_SIZE(&fs_param));
1910 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1912 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1913 fs_types[0] = strdup("journal");
1918 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1919 print_str_list(fs_types);
1922 if (r_opt == EXT2_GOOD_OLD_REV &&
1923 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1924 fs_param.s_feature_ro_compat)) {
1925 fprintf(stderr, "%s", _("Filesystem features not supported "
1926 "with revision 0 filesystems\n"));
1931 if (r_opt == EXT2_GOOD_OLD_REV) {
1932 fprintf(stderr, "%s",
1933 _("Sparse superblocks not supported "
1934 "with revision 0 filesystems\n"));
1937 fs_param.s_feature_ro_compat |=
1938 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1939 } else if (s_opt == 0)
1940 fs_param.s_feature_ro_compat &=
1941 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1943 if (journal_size != 0) {
1944 if (r_opt == EXT2_GOOD_OLD_REV) {
1945 fprintf(stderr, "%s", _("Journals not supported with "
1946 "revision 0 filesystems\n"));
1949 fs_param.s_feature_compat |=
1950 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1953 /* Get reserved_ratio from profile if not specified on cmd line. */
1954 if (reserved_ratio < 0.0) {
1955 reserved_ratio = get_double_from_profile(
1956 fs_types, "reserved_ratio", 5.0);
1957 if (reserved_ratio > 50 || reserved_ratio < 0) {
1958 com_err(program_name, 0,
1959 _("invalid reserved blocks percent - %lf"),
1965 if (fs_param.s_feature_incompat &
1966 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1968 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1969 fs_param.s_feature_compat = 0;
1970 fs_param.s_feature_ro_compat = 0;
1973 /* Check the user's mkfs options for 64bit */
1974 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1975 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1976 printf("%s", _("Extents MUST be enabled for a 64-bit "
1977 "filesystem. Pass -O extents to rectify.\n"));
1981 /* Set first meta blockgroup via an environment variable */
1982 /* (this is mostly for debugging purposes) */
1983 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1984 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1985 fs_param.s_first_meta_bg = atoi(tmp);
1986 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1988 cluster_size = get_int_from_profile(fs_types,
1991 fs_param.s_log_cluster_size =
1992 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1993 if (fs_param.s_log_cluster_size &&
1994 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
1995 com_err(program_name, 0, "%s",
1996 _("The cluster size may not be "
1997 "smaller than the block size.\n"));
2000 } else if (cluster_size) {
2001 com_err(program_name, 0, "%s",
2002 _("specifying a cluster size requires the "
2003 "bigalloc feature"));
2006 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2008 if (inode_ratio == 0) {
2009 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2011 if (inode_ratio < blocksize)
2012 inode_ratio = blocksize;
2013 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2014 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2017 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2018 retval = get_device_geometry(device_name, &fs_param, psector_size);
2021 _("warning: Unable to get device geometry for %s\n"),
2023 } else if (retval) {
2024 printf(_("%s alignment is offset by %lu bytes.\n"),
2025 device_name, retval);
2026 printf(_("This may result in very poor performance, "
2027 "(re)-partitioning suggested.\n"));
2031 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2034 * Initialize s_desc_size so that the parse_extended_opts()
2035 * can correctly handle "-E resize=NNN" if the 64-bit option
2038 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
2039 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2041 /* This check should happen beyond the last assignment to blocksize */
2042 if (blocksize > sys_page_size) {
2044 com_err(program_name, 0,
2045 _("%d-byte blocks too big for system (max %d)"),
2046 blocksize, sys_page_size);
2049 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2050 "(max %d), forced to continue\n"),
2051 blocksize, sys_page_size);
2054 lazy_itable_init = 0;
2055 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2056 lazy_itable_init = 1;
2058 lazy_itable_init = get_bool_from_profile(fs_types,
2061 discard = get_bool_from_profile(fs_types, "discard" , discard);
2062 journal_flags |= get_bool_from_profile(fs_types,
2063 "lazy_journal_init", 0) ?
2064 EXT2_MKJOURNAL_LAZYINIT : 0;
2065 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2067 /* Get options from profile */
2068 for (cpp = fs_types; *cpp; cpp++) {
2070 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2072 parse_extended_opts(&fs_param, tmp);
2077 parse_extended_opts(&fs_param, extended_opts);
2079 /* Don't allow user to set both metadata_csum and uninit_bg bits. */
2080 if ((fs_param.s_feature_ro_compat &
2081 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2082 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
2083 fs_param.s_feature_ro_compat &=
2084 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
2086 /* Can't support bigalloc feature without extents feature */
2087 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2088 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2089 com_err(program_name, 0, "%s",
2090 _("Can't support bigalloc feature without "
2091 "extents feature"));
2095 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2096 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2097 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2098 "features are not compatible.\n"
2099 "They can not be both enabled "
2100 "simultaneously.\n"));
2105 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2106 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2107 "still under development\n"
2108 "See https://ext4.wiki.kernel.org/"
2109 "index.php/Bigalloc for more information\n\n"));
2112 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2113 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2114 "still under development\n"
2115 "See https://ext4.wiki.kernel.org/"
2116 "index.php/Quota for more information\n\n"));
2118 /* Since sparse_super is the default, we would only have a problem
2119 * here if it was explicitly disabled.
2121 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2122 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2123 com_err(program_name, 0, "%s",
2124 _("reserved online resize blocks not supported "
2125 "on non-sparse filesystem"));
2129 if (fs_param.s_blocks_per_group) {
2130 if (fs_param.s_blocks_per_group < 256 ||
2131 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2132 com_err(program_name, 0, "%s",
2133 _("blocks per group count out of range"));
2139 * If the bigalloc feature is enabled, then the -g option will
2140 * specify the number of clusters per group.
2142 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2143 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2144 fs_param.s_blocks_per_group = 0;
2147 if (inode_size == 0)
2148 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2149 if (!flex_bg_size && (fs_param.s_feature_incompat &
2150 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2151 flex_bg_size = get_int_from_profile(fs_types,
2152 "flex_bg_size", 16);
2154 if (!(fs_param.s_feature_incompat &
2155 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2156 com_err(program_name, 0, "%s",
2157 _("Flex_bg feature not enabled, so "
2158 "flex_bg size may not be specified"));
2161 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2164 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2165 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2166 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2167 inode_size & (inode_size - 1)) {
2168 com_err(program_name, 0,
2169 _("invalid inode size %d (min %d/max %d)"),
2170 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2174 fs_param.s_inode_size = inode_size;
2177 /* Make sure number of inodes specified will fit in 32 bits */
2178 if (num_inodes == 0) {
2179 unsigned long long n;
2180 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2181 if (n > MAX_32_NUM) {
2182 if (fs_param.s_feature_incompat &
2183 EXT4_FEATURE_INCOMPAT_64BIT)
2184 num_inodes = MAX_32_NUM;
2186 com_err(program_name, 0,
2187 _("too many inodes (%llu), raise "
2188 "inode ratio?"), n);
2192 } else if (num_inodes > MAX_32_NUM) {
2193 com_err(program_name, 0,
2194 _("too many inodes (%llu), specify < 2^32 inodes"),
2199 * Calculate number of inodes based on the inode ratio
2201 fs_param.s_inodes_count = num_inodes ? num_inodes :
2202 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2204 if ((((unsigned long long)fs_param.s_inodes_count) *
2205 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2206 ((ext2fs_blocks_count(&fs_param)) *
2207 EXT2_BLOCK_SIZE(&fs_param))) {
2208 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2209 "(%u) too big for a\n\t"
2210 "filesystem with %llu blocks, "
2211 "specify higher inode_ratio (-i)\n\t"
2212 "or lower inode count (-N).\n"),
2213 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2214 fs_param.s_inodes_count,
2215 (unsigned long long) ext2fs_blocks_count(&fs_param));
2220 * Calculate number of blocks to reserve
2222 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2223 ext2fs_blocks_count(&fs_param) / 100.0);
2229 static int should_do_undo(const char *name)
2234 struct ext2_super_block super;
2235 io_manager manager = unix_io_manager;
2236 int csum_flag, force_undo;
2238 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2239 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
2240 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
2241 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2242 if (!force_undo && (!csum_flag || !lazy_itable_init))
2245 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2248 * We don't handle error cases instead we
2249 * declare that the file system doesn't exist
2250 * and let the rest of mke2fs take care of
2257 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2258 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2264 #if defined(WORDS_BIGENDIAN)
2265 s_magic = ext2fs_swab16(super.s_magic);
2267 s_magic = super.s_magic;
2270 if (s_magic == EXT2_SUPER_MAGIC)
2274 io_channel_close(channel);
2281 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2283 errcode_t retval = ENOMEM;
2284 char *tdb_dir = NULL, *tdb_file = NULL;
2285 char *dev_name, *tmp_name;
2286 int free_tdb_dir = 0;
2289 * Configuration via a conf file would be
2292 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2294 profile_get_string(profile, "defaults",
2295 "undo_dir", 0, "/var/lib/e2fsprogs",
2300 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2301 access(tdb_dir, W_OK)) {
2307 tmp_name = strdup(name);
2310 dev_name = basename(tmp_name);
2311 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2316 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2319 if (!access(tdb_file, F_OK)) {
2320 if (unlink(tdb_file) < 0) {
2326 set_undo_io_backing_manager(*io_ptr);
2327 *io_ptr = undo_io_manager;
2328 retval = set_undo_io_backup_file(tdb_file);
2331 printf(_("Overwriting existing filesystem; this can be undone "
2332 "using the command:\n"
2333 " e2undo %s %s\n\n"), tdb_file, name);
2344 com_err(program_name, retval, "%s",
2345 _("while trying to setup undo file\n"));
2349 static int mke2fs_discard_device(ext2_filsys fs)
2351 struct ext2fs_numeric_progress_struct progress;
2352 blk64_t blocks = ext2fs_blocks_count(fs->super);
2353 blk64_t count = DISCARD_STEP_MB;
2358 * Let's try if discard really works on the device, so
2359 * we do not print numeric progress resulting in failure
2362 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2365 cur = fs->blocksize;
2367 count *= (1024 * 1024);
2368 count /= fs->blocksize;
2370 ext2fs_numeric_progress_init(fs, &progress,
2371 _("Discarding device blocks: "),
2373 while (cur < blocks) {
2374 ext2fs_numeric_progress_update(fs, &progress, cur);
2376 if (cur + count > blocks)
2377 count = blocks - cur;
2379 retval = io_channel_discard(fs->io, cur, count);
2386 ext2fs_numeric_progress_close(fs, &progress,
2389 printf("%s\n",error_message(retval));
2391 ext2fs_numeric_progress_close(fs, &progress,
2397 static void fix_cluster_bg_counts(ext2_filsys fs)
2399 blk64_t cluster, num_clusters, tot_free;
2401 int grp_free, num_free, group;
2403 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2404 tot_free = num_free = group = grp_free = 0;
2405 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2406 cluster < num_clusters; cluster++) {
2407 if (!ext2fs_test_block_bitmap2(fs->block_map,
2408 EXT2FS_C2B(fs, cluster))) {
2413 if ((num == fs->super->s_clusters_per_group) ||
2414 (cluster == num_clusters-1)) {
2415 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2416 ext2fs_group_desc_csum_set(fs, group);
2422 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2425 static int create_quota_inodes(ext2_filsys fs)
2429 quota_init_context(&qctx, fs, -1);
2430 quota_compute_usage(qctx);
2431 quota_write_inode(qctx, quotatype);
2432 quota_release_context(&qctx);
2437 int main (int argc, char *argv[])
2439 errcode_t retval = 0;
2441 badblocks_list bb_list = 0;
2442 unsigned int journal_blocks;
2443 unsigned int i, checkinterval;
2449 char tdb_string[40];
2451 int itable_zeroed = 0;
2454 setlocale(LC_MESSAGES, "");
2455 setlocale(LC_CTYPE, "");
2456 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2457 textdomain(NLS_CAT_NAME);
2458 set_com_err_gettext(gettext);
2462 #ifdef CONFIG_TESTIO_DEBUG
2463 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2464 io_ptr = test_io_manager;
2465 test_io_backing_manager = unix_io_manager;
2468 io_ptr = unix_io_manager;
2470 if (should_do_undo(device_name)) {
2471 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2477 * Initialize the superblock....
2479 flags = EXT2_FLAG_EXCLUSIVE;
2481 flags |= EXT2_FLAG_DIRECT_IO;
2482 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2485 flags |= EXT2_FLAG_64BITS;
2487 * By default, we print how many inode tables or block groups
2488 * or whatever we've written so far. The quiet flag disables
2489 * this, along with a lot of other output.
2492 flags |= EXT2_FLAG_PRINT_PROGRESS;
2493 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2495 com_err(device_name, retval, "%s",
2496 _("while setting up superblock"));
2499 fs->progress_ops = &ext2fs_numeric_progress_ops;
2501 /* Check the user's mkfs options for metadata checksumming */
2503 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2504 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
2505 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2506 EXT3_FEATURE_INCOMPAT_EXTENTS))
2507 printf(_("Extents are not enabled. The file extent "
2508 "tree can be checksummed, whereas block maps "
2509 "cannot. Not enabling extents reduces the "
2510 "coverage of metadata checksumming. "
2511 "Pass -O extents to rectify.\n"));
2512 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2513 EXT4_FEATURE_INCOMPAT_64BIT))
2514 printf(_("64-bit filesystem support is not "
2515 "enabled. The larger fields afforded by "
2516 "this feature enable full-strength "
2517 "checksumming. Pass -O 64bit to rectify.\n"));
2520 /* Calculate journal blocks */
2521 if (!journal_device && ((journal_size) ||
2522 (fs_param.s_feature_compat &
2523 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2524 journal_blocks = figure_journal_size(journal_size, fs);
2526 /* Can't undo discard ... */
2527 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2528 retval = mke2fs_discard_device(fs);
2529 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2532 _("Discard succeeded and will return "
2533 "0s - skipping inode table wipe\n"));
2534 lazy_itable_init = 1;
2539 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2540 32768 : fs->blocksize * 8);
2541 io_channel_set_options(fs->io, tdb_string);
2543 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2544 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2546 if ((fs_param.s_feature_incompat &
2547 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2548 (fs_param.s_feature_ro_compat &
2549 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2550 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2551 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|
2552 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2553 fs->super->s_kbytes_written = 1;
2556 * Wipe out the old on-disk superblock
2559 zap_sector(fs, 2, 6);
2562 * Parse or generate a UUID for the filesystem
2565 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2566 com_err(device_name, 0, "could not parse UUID: %s\n",
2571 uuid_generate(fs->super->s_uuid);
2572 ext2fs_init_csum_seed(fs);
2575 * Initialize the directory index variables
2577 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2579 hash_alg = e2p_string2hash(hash_alg_str);
2581 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2583 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2586 * Periodic checks can be enabled/disabled via config file.
2587 * Note we override the kernel include file's idea of what the default
2588 * check interval (never) should be. It's a good idea to check at
2589 * least *occasionally*, specially since servers will never rarely get
2590 * to reboot, since Linux is so robust these days. :-)
2592 * 180 days (six months) seems like a good value.
2594 #ifdef EXT2_DFL_CHECKINTERVAL
2595 #undef EXT2_DFL_CHECKINTERVAL
2597 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2599 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2600 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2601 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2603 * Add "jitter" to the superblock's check interval so that we
2604 * don't check all the filesystems at the same time. We use a
2605 * kludgy hack of using the UUID to derive a random jitter value
2607 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2608 val += fs->super->s_uuid[i];
2609 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2611 fs->super->s_max_mnt_count = -1;
2614 * Override the creator OS, if applicable
2616 if (creator_os && !set_os(fs->super, creator_os)) {
2617 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2622 * For the Hurd, we will turn off filetype since it doesn't
2625 if (fs->super->s_creator_os == EXT2_OS_HURD)
2626 fs->super->s_feature_incompat &=
2627 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2630 * Set the volume label...
2633 memset(fs->super->s_volume_name, 0,
2634 sizeof(fs->super->s_volume_name));
2635 strncpy(fs->super->s_volume_name, volume_label,
2636 sizeof(fs->super->s_volume_name));
2640 * Set the last mount directory
2643 memset(fs->super->s_last_mounted, 0,
2644 sizeof(fs->super->s_last_mounted));
2645 strncpy(fs->super->s_last_mounted, mount_dir,
2646 sizeof(fs->super->s_last_mounted));
2649 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2650 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2651 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
2653 if (!quiet || noaction)
2659 if (fs->super->s_feature_incompat &
2660 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2661 create_journal_dev(fs);
2662 exit(ext2fs_close(fs) ? 1 : 0);
2665 if (bad_blocks_filename)
2666 read_bb_file(fs, &bb_list, bad_blocks_filename);
2668 test_disk(fs, &bb_list);
2670 handle_bad_blocks(fs, bb_list);
2671 fs->stride = fs_stride = fs->super->s_raid_stride;
2673 printf("%s", _("Allocating group tables: "));
2674 retval = ext2fs_allocate_tables(fs);
2676 com_err(program_name, retval, "%s",
2677 _("while trying to allocate filesystem tables"));
2681 printf("%s", _("done \n"));
2683 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2685 com_err(program_name, retval, "%s",
2686 _("\n\twhile converting subcluster bitmap"));
2691 fs->super->s_state |= EXT2_ERROR_FS;
2692 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2694 * The command "mke2fs -S" is used to recover
2695 * corrupted file systems, so do not mark any of the
2696 * inodes as unused; we want e2fsck to consider all
2697 * inodes as potentially containing recoverable data.
2699 if (ext2fs_has_group_desc_csum(fs)) {
2700 for (i = 0; i < fs->group_desc_count; i++)
2701 ext2fs_bg_itable_unused_set(fs, i, 0);
2704 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2705 blk64_t rsv = 65536 / fs->blocksize;
2706 blk64_t blocks = ext2fs_blocks_count(fs->super);
2710 #ifdef ZAP_BOOTBLOCK
2711 zap_sector(fs, 0, 2);
2715 * Wipe out any old MD RAID (or other) metadata at the end
2716 * of the device. This will also verify that the device is
2717 * as large as we think. Be careful with very small devices.
2719 start = (blocks & ~(rsv - 1));
2723 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2727 com_err(program_name, retval,
2728 _("while zeroing block %llu at end of filesystem"),
2731 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2732 create_root_dir(fs);
2733 create_lost_and_found(fs);
2735 create_bad_block_inode(fs, bb_list);
2736 if (fs->super->s_feature_compat &
2737 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2738 retval = ext2fs_create_resize_inode(fs);
2740 com_err("ext2fs_create_resize_inode", retval,
2742 _("while reserving blocks for online resize"));
2748 if (journal_device) {
2752 check_plausibility(journal_device);
2753 check_mount(journal_device, force, _("journal"));
2755 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2756 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2757 fs->blocksize, unix_io_manager, &jfs);
2759 com_err(program_name, retval,
2760 _("while trying to open journal device %s\n"),
2765 printf(_("Adding journal to device %s: "),
2769 retval = ext2fs_add_journal_device(fs, jfs);
2771 com_err (program_name, retval,
2772 _("\n\twhile trying to add journal to device %s"),
2777 printf("%s", _("done\n"));
2779 free(journal_device);
2780 } else if ((journal_size) ||
2781 (fs_param.s_feature_compat &
2782 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2784 printf("%s", _("Skipping journal creation in super-only mode\n"));
2785 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2789 if (!journal_blocks) {
2790 fs->super->s_feature_compat &=
2791 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2795 printf(_("Creating journal (%u blocks): "),
2799 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2802 com_err(program_name, retval, "%s",
2803 _("\n\twhile trying to create journal"));
2807 printf("%s", _("done\n"));
2811 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2812 retval = ext2fs_mmp_init(fs);
2814 fprintf(stderr, "%s",
2815 _("\nError while enabling multiple "
2816 "mount protection feature."));
2820 printf(_("Multiple mount protection is enabled "
2821 "with update interval %d seconds.\n"),
2822 fs->super->s_mmp_update_interval);
2825 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2826 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2827 fix_cluster_bg_counts(fs);
2828 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2829 EXT4_FEATURE_RO_COMPAT_QUOTA))
2830 create_quota_inodes(fs);
2833 printf("%s", _("Writing superblocks and "
2834 "filesystem accounting information: "));
2835 checkinterval = fs->super->s_checkinterval;
2836 max_mnt_count = fs->super->s_max_mnt_count;
2837 retval = ext2fs_close(fs);
2839 fprintf(stderr, "%s",
2840 _("\nWarning, had trouble writing out superblocks."));
2841 } else if (!quiet) {
2842 printf("%s", _("done\n\n"));
2843 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2844 print_check_message(max_mnt_count, checkinterval);
2847 remove_error_table(&et_ext2_error_table);
2848 remove_error_table(&et_prof_error_table);
2849 profile_release(profile);
2850 for (i=0; fs_types[i]; i++)