2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 /* Usage: mke2fs [options] device
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
19 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
28 #include <sys/utsname.h>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
53 #include <blkid/blkid.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fsP.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
60 #include "ext2fs/ext2fs.h"
64 #include "../version.h"
65 #include "nls-enable.h"
67 #define STRIDE_LENGTH 8
69 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
75 #define DISCARD_STEP_MB (2048)
77 extern int isatty(int);
78 extern FILE *fpopen(const char *cmd, const char *mode);
80 const char * program_name = "mke2fs";
81 const char * device_name /* = NULL */;
83 /* Command line options */
88 int discard = 1; /* attempt to discard device before fs creation */
94 char *bad_blocks_filename;
97 struct ext2_super_block fs_param;
102 char *journal_device;
103 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
108 int sys_page_size = 4096;
109 int linux_version_code = 0;
111 static void usage(void)
113 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
114 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
115 "[-J journal-options]\n"
116 "\t[-G meta group size] [-N number-of-inodes]\n"
117 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
118 "\t[-g blocks-per-group] [-L volume-label] "
119 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
120 "[-r fs-revision] [-E extended-option[,...]]\n"
121 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
126 static int int_log2(unsigned long long arg)
138 static int int_log10(unsigned long long arg)
147 static int parse_version_number(const char *s)
149 int major, minor, rev;
155 major = strtol(cp, &endptr, 10);
156 if (cp == endptr || *endptr != '.')
159 minor = strtol(cp, &endptr, 10);
160 if (cp == endptr || *endptr != '.')
163 rev = strtol(cp, &endptr, 10);
166 return ((((major * 256) + minor) * 256) + rev);
170 * Helper function for read_bb_file and test_disk
172 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
174 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
179 * Reads the bad blocks list from a file
181 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
182 const char *bad_blocks_file)
187 f = fopen(bad_blocks_file, "r");
189 com_err("read_bad_blocks_file", errno,
190 _("while trying to open %s"), bad_blocks_file);
193 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
196 com_err("ext2fs_read_bb_FILE", retval,
197 _("while reading in list of bad blocks from file"));
203 * Runs the badblocks program to test the disk
205 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
211 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
212 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
213 fs->device_name, ext2fs_blocks_count(fs->super)-1);
215 printf(_("Running command: %s\n"), buf);
218 com_err("popen", errno,
219 _("while trying to run '%s'"), buf);
222 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
225 com_err("ext2fs_read_bb_FILE", retval,
226 _("while processing list of bad blocks from program"));
231 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
235 unsigned must_be_good;
237 badblocks_iterate bb_iter;
247 * The primary superblock and group descriptors *must* be
248 * good; if not, abort.
250 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
251 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
252 if (ext2fs_badblocks_list_test(bb_list, i)) {
253 fprintf(stderr, _("Block %d in primary "
254 "superblock/group descriptor area bad.\n"), i);
255 fprintf(stderr, _("Blocks %u through %u must be good "
256 "in order to build a filesystem.\n"),
257 fs->super->s_first_data_block, must_be_good);
258 fputs(_("Aborting....\n"), stderr);
264 * See if any of the bad blocks are showing up in the backup
265 * superblocks and/or group descriptors. If so, issue a
266 * warning and adjust the block counts appropriately.
268 group_block = fs->super->s_first_data_block +
269 fs->super->s_blocks_per_group;
271 for (i = 1; i < fs->group_desc_count; i++) {
273 for (j=0; j < fs->desc_blocks+1; j++) {
274 if (ext2fs_badblocks_list_test(bb_list,
278 _("Warning: the backup superblock/group descriptors at block %u contain\n"
282 group = ext2fs_group_of_blk2(fs, group_block+j);
283 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
284 ext2fs_group_desc_csum_set(fs, group);
285 ext2fs_free_blocks_count_add(fs->super, 1);
288 group_block += fs->super->s_blocks_per_group;
292 * Mark all the bad blocks as used...
294 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
296 com_err("ext2fs_badblocks_list_iterate_begin", retval,
297 _("while marking bad blocks as used"));
300 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
301 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
302 ext2fs_badblocks_list_iterate_end(bb_iter);
305 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
311 struct ext2fs_numeric_progress_struct progress;
313 ext2fs_numeric_progress_init(fs, &progress,
314 _("Writing inode tables: "),
315 fs->group_desc_count);
317 for (i = 0; i < fs->group_desc_count; i++) {
318 ext2fs_numeric_progress_update(fs, &progress, i);
320 blk = ext2fs_inode_table_loc(fs, i);
321 num = fs->inode_blocks_per_group;
324 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
325 ext2fs_bg_itable_unused(fs, i)) *
326 EXT2_INODE_SIZE(fs->super),
327 EXT2_BLOCK_SIZE(fs->super));
328 if (!lazy_flag || itable_zeroed) {
329 /* The kernel doesn't need to zero the itable blocks */
330 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
331 ext2fs_group_desc_csum_set(fs, i);
333 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
335 fprintf(stderr, _("\nCould not write %d "
336 "blocks in inode table starting at %llu: %s\n"),
337 num, blk, error_message(retval));
341 if (sync_kludge == 1)
343 else if ((i % sync_kludge) == 0)
347 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
348 ext2fs_numeric_progress_close(fs, &progress,
352 static void create_root_dir(ext2_filsys fs)
355 struct ext2_inode inode;
358 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
360 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
364 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
366 com_err("ext2fs_read_inode", retval,
367 _("while reading root inode"));
372 ext2fs_set_i_uid_high(inode, uid >> 16);
376 ext2fs_set_i_gid_high(inode, gid >> 16);
378 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
380 com_err("ext2fs_write_inode", retval,
381 _("while setting root inode ownership"));
387 static void create_lost_and_found(ext2_filsys fs)
389 unsigned int lpf_size = 0;
392 const char *name = "lost+found";
396 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
398 com_err("ext2fs_mkdir", retval,
399 _("while creating /lost+found"));
403 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
405 com_err("ext2_lookup", retval,
406 _("while looking up /lost+found"));
410 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
411 /* Ensure that lost+found is at least 2 blocks, so we always
412 * test large empty blocks for big-block filesystems. */
413 if ((lpf_size += fs->blocksize) >= 16*1024 &&
414 lpf_size >= 2 * fs->blocksize)
416 retval = ext2fs_expand_dir(fs, ino);
418 com_err("ext2fs_expand_dir", retval,
419 _("while expanding /lost+found"));
425 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
429 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
430 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
431 retval = ext2fs_update_bb_inode(fs, bb_list);
433 com_err("ext2fs_update_bb_inode", retval,
434 _("while setting bad block inode"));
440 static void reserve_inodes(ext2_filsys fs)
444 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
445 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
446 ext2fs_mark_ib_dirty(fs);
449 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
450 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
451 #define BSD_LABEL_OFFSET 64
453 static void zap_sector(ext2_filsys fs, int sect, int nsect)
459 buf = malloc(512*nsect);
461 printf(_("Out of memory erasing sectors %d-%d\n"),
462 sect, sect + nsect - 1);
467 /* Check for a BSD disklabel, and don't erase it if so */
468 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
471 _("Warning: could not read block 0: %s\n"),
472 error_message(retval));
474 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
475 if ((*magic == BSD_DISKMAGIC) ||
476 (*magic == BSD_MAGICDISK))
481 memset(buf, 0, 512*nsect);
482 io_channel_set_blksize(fs->io, 512);
483 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
484 io_channel_set_blksize(fs->io, fs->blocksize);
487 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
488 sect, error_message(retval));
491 static void create_journal_dev(ext2_filsys fs)
493 struct ext2fs_numeric_progress_struct progress;
496 blk64_t blk, err_blk;
497 int c, count, err_count;
499 retval = ext2fs_create_journal_superblock(fs,
500 ext2fs_blocks_count(fs->super), 0, &buf);
502 com_err("create_journal_dev", retval,
503 _("while initializing journal superblock"));
507 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
508 goto write_superblock;
510 ext2fs_numeric_progress_init(fs, &progress,
511 _("Zeroing journal device: "),
512 ext2fs_blocks_count(fs->super));
514 count = ext2fs_blocks_count(fs->super);
520 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
522 com_err("create_journal_dev", retval,
523 _("while zeroing journal device "
524 "(block %llu, count %d)"),
530 ext2fs_numeric_progress_update(fs, &progress, blk);
532 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
534 ext2fs_numeric_progress_close(fs, &progress, NULL);
536 retval = io_channel_write_blk64(fs->io,
537 fs->super->s_first_data_block+1,
540 com_err("create_journal_dev", retval,
541 _("while writing journal superblock"));
546 static void show_stats(ext2_filsys fs)
548 struct ext2_super_block *s = fs->super;
555 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
556 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
557 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
559 memset(buf, 0, sizeof(buf));
560 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
561 printf(_("Filesystem label=%s\n"), buf);
562 os = e2p_os2string(fs->super->s_creator_os);
564 printf(_("OS type: %s\n"), os);
566 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
567 s->s_log_block_size);
568 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
569 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
570 printf(_("Cluster size=%u (log=%u)\n"),
571 fs->blocksize << fs->cluster_ratio_bits,
572 s->s_log_cluster_size);
574 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
575 s->s_log_cluster_size);
576 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
577 s->s_raid_stride, s->s_raid_stripe_width);
578 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
579 ext2fs_blocks_count(s));
580 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
581 ext2fs_r_blocks_count(s),
582 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
583 printf(_("First data block=%u\n"), s->s_first_data_block);
584 if (s->s_reserved_gdt_blocks)
585 printf(_("Maximum filesystem blocks=%lu\n"),
586 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
587 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
588 if (fs->group_desc_count > 1)
589 printf(_("%u block groups\n"), fs->group_desc_count);
591 printf(_("%u block group\n"), fs->group_desc_count);
592 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
593 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
594 printf(_("%u blocks per group, %u clusters per group\n"),
595 s->s_blocks_per_group, s->s_clusters_per_group);
597 printf(_("%u blocks per group, %u fragments per group\n"),
598 s->s_blocks_per_group, s->s_clusters_per_group);
599 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
601 if (fs->group_desc_count == 1) {
606 printf(_("Superblock backups stored on blocks: "));
607 group_block = s->s_first_data_block;
609 for (i = 1; i < fs->group_desc_count; i++) {
610 group_block += s->s_blocks_per_group;
611 if (!ext2fs_bg_has_super(fs, i))
615 need = int_log10(group_block) + 2;
616 if (need > col_left) {
621 printf("%llu", group_block);
627 * Set the S_CREATOR_OS field. Return true if OS is known,
630 static int set_os(struct ext2_super_block *sb, char *os)
633 sb->s_creator_os = atoi (os);
634 else if (strcasecmp(os, "linux") == 0)
635 sb->s_creator_os = EXT2_OS_LINUX;
636 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
637 sb->s_creator_os = EXT2_OS_HURD;
638 else if (strcasecmp(os, "freebsd") == 0)
639 sb->s_creator_os = EXT2_OS_FREEBSD;
640 else if (strcasecmp(os, "lites") == 0)
641 sb->s_creator_os = EXT2_OS_LITES;
647 #define PATH_SET "PATH=/sbin"
649 static void parse_extended_opts(struct ext2_super_block *param,
652 char *buf, *token, *next, *p, *arg, *badopt = 0;
660 _("Couldn't allocate memory to parse options!\n"));
664 for (token = buf; token && *token; token = next) {
665 p = strchr(token, ',');
671 arg = strchr(token, '=');
676 if (strcmp(token, "stride") == 0) {
682 param->s_raid_stride = strtoul(arg, &p, 0);
685 _("Invalid stride parameter: %s\n"),
690 } else if (strcmp(token, "stripe-width") == 0 ||
691 strcmp(token, "stripe_width") == 0) {
697 param->s_raid_stripe_width = strtoul(arg, &p, 0);
700 _("Invalid stripe-width parameter: %s\n"),
705 } else if (!strcmp(token, "resize")) {
707 unsigned long bpg, rsv_groups;
708 unsigned long group_desc_count, desc_blocks;
709 unsigned int gdpb, blocksize;
718 resize = parse_num_blocks2(arg,
719 param->s_log_block_size);
723 _("Invalid resize parameter: %s\n"),
728 if (resize <= ext2fs_blocks_count(param)) {
730 _("The resize maximum must be greater "
731 "than the filesystem size.\n"));
736 blocksize = EXT2_BLOCK_SIZE(param);
737 bpg = param->s_blocks_per_group;
740 gdpb = EXT2_DESC_PER_BLOCK(param);
741 group_desc_count = (__u32) ext2fs_div64_ceil(
742 ext2fs_blocks_count(param), bpg);
743 desc_blocks = (group_desc_count +
745 rsv_groups = ext2fs_div64_ceil(resize, bpg);
746 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
748 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
749 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
752 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
754 _("On-line resizing not supported with revision 0 filesystems\n"));
758 param->s_feature_compat |=
759 EXT2_FEATURE_COMPAT_RESIZE_INODE;
761 param->s_reserved_gdt_blocks = rsv_gdb;
763 } else if (!strcmp(token, "test_fs")) {
764 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
765 } else if (!strcmp(token, "lazy_itable_init")) {
767 lazy_itable_init = strtoul(arg, &p, 0);
769 lazy_itable_init = 1;
770 } else if (!strcmp(token, "lazy_journal_init")) {
772 journal_flags |= strtoul(arg, &p, 0) ?
773 EXT2_MKJOURNAL_LAZYINIT : 0;
775 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
776 } else if (!strcmp(token, "discard")) {
778 } else if (!strcmp(token, "nodiscard")) {
786 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
787 "Extended options are separated by commas, "
788 "and may take an argument which\n"
789 "\tis set off by an equals ('=') sign.\n\n"
790 "Valid extended options are:\n"
791 "\tstride=<RAID per-disk data chunk in blocks>\n"
792 "\tstripe-width=<RAID stride * data disks in blocks>\n"
793 "\tresize=<resize maximum size in blocks>\n"
794 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
795 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
799 badopt ? badopt : "");
803 if (param->s_raid_stride &&
804 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
805 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
806 "multiple of stride %u.\n\n"),
807 param->s_raid_stripe_width, param->s_raid_stride);
812 static __u32 ok_features[3] = {
814 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
815 EXT2_FEATURE_COMPAT_RESIZE_INODE |
816 EXT2_FEATURE_COMPAT_DIR_INDEX |
817 EXT2_FEATURE_COMPAT_EXT_ATTR,
819 EXT2_FEATURE_INCOMPAT_FILETYPE|
820 EXT3_FEATURE_INCOMPAT_EXTENTS|
821 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
822 EXT2_FEATURE_INCOMPAT_META_BG|
823 EXT4_FEATURE_INCOMPAT_FLEX_BG|
824 EXT4_FEATURE_INCOMPAT_64BIT,
826 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
827 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
828 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
829 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
830 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
831 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
832 EXT4_FEATURE_RO_COMPAT_BIGALLOC
836 static void syntax_err_report(const char *filename, long err, int line_num)
839 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
840 filename, line_num, error_message(err));
844 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
846 static void edit_feature(const char *str, __u32 *compat_array)
851 if (e2p_edit_feature(str, compat_array, ok_features)) {
852 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
858 static void edit_mntopts(const char *str, __u32 *mntopts)
863 if (e2p_edit_mntopts(str, mntopts, ~0)) {
864 fprintf(stderr, _("Invalid mount option set: %s\n"),
876 static errcode_t init_list(struct str_list *sl)
880 sl->list = malloc((sl->max+1) * sizeof(char *));
887 static errcode_t push_string(struct str_list *sl, const char *str)
891 if (sl->num >= sl->max) {
893 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
898 sl->list[sl->num] = malloc(strlen(str)+1);
899 if (sl->list[sl->num] == 0)
901 strcpy(sl->list[sl->num], str);
903 sl->list[sl->num] = 0;
907 static void print_str_list(char **list)
911 for (cpp = list; *cpp; cpp++) {
912 printf("'%s'", *cpp);
920 * Return TRUE if the profile has the given subsection
922 static int profile_has_subsection(profile_t profile, const char *section,
923 const char *subsection)
926 const char *names[4];
931 names[1] = subsection;
934 if (profile_iterator_create(profile, names,
935 PROFILE_ITER_LIST_SECTION |
936 PROFILE_ITER_RELATIONS_ONLY, &state))
939 if ((profile_iterator(&state, &name, 0) == 0) && name) {
944 profile_iterator_free(&state);
948 static char **parse_fs_type(const char *fs_type,
949 const char *usage_types,
950 struct ext2_super_block *fs_param,
951 blk64_t fs_blocks_count,
954 const char *ext_type = 0;
956 char *profile_type = 0;
958 const char *size_type;
959 struct str_list list;
960 unsigned long long meg;
963 if (init_list(&list))
966 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
967 !strcasecmp(creator_os, "hurd")))
974 else if (!strcmp(program_name, "mke3fs"))
977 ext_type = strrchr(progname, '/');
983 if (!strncmp(ext_type, "mkfs.", 5)) {
985 if (ext_type[0] == 0)
992 profile_get_string(profile, "defaults", "fs_type", 0,
993 "ext2", &profile_type);
994 ext_type = profile_type;
995 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1000 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1001 strcmp(ext_type, "ext2")) {
1002 printf(_("\nYour mke2fs.conf file does not define the "
1003 "%s filesystem type.\n"), ext_type);
1004 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1005 !strcmp(ext_type, "ext4dev")) {
1006 printf(_("You probably need to install an updated "
1007 "mke2fs.conf file.\n\n"));
1010 printf(_("Aborting...\n"));
1015 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1016 if (fs_blocks_count < 3 * meg)
1017 size_type = "floppy";
1018 else if (fs_blocks_count < 512 * meg)
1019 size_type = "small";
1020 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1021 size_type = "default";
1022 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1028 usage_types = size_type;
1030 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1036 strcpy(parse_str, usage_types);
1041 push_string(&list, ext_type);
1044 t = strchr(cp, ',');
1049 if (profile_has_subsection(profile, "fs_types", cp))
1050 push_string(&list, cp);
1051 else if (strcmp(cp, "default") != 0)
1053 _("\nWarning: the fs_type %s is not "
1054 "defined in mke2fs.conf\n\n"),
1067 push_string(&list, "hurd");
1071 static char *get_string_from_profile(char **fs_types, const char *opt,
1072 const char *def_val)
1077 for (i=0; fs_types[i]; i++);
1078 for (i-=1; i >=0 ; i--) {
1079 profile_get_string(profile, "fs_types", fs_types[i],
1084 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1088 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1093 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1094 for (cpp = fs_types; *cpp; cpp++)
1095 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1099 static double get_double_from_profile(char **fs_types, const char *opt,
1105 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1106 for (cpp = fs_types; *cpp; cpp++)
1107 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1111 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1116 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1117 for (cpp = fs_types; *cpp; cpp++)
1118 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1122 extern const char *mke2fs_default_profile;
1123 static const char *default_files[] = { "<default>", 0 };
1125 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1127 * Sets the geometry of a device (stripe/stride), and returns the
1128 * device's alignment offset, if any, or a negative error.
1130 static int get_device_geometry(const char *file,
1131 struct ext2_super_block *fs_param,
1138 unsigned long min_io;
1139 unsigned long opt_io;
1140 struct stat statbuf;
1142 /* Nothing to do for a regular file */
1143 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1146 pr = blkid_new_probe_from_filename(file);
1150 tp = blkid_probe_get_topology(pr);
1154 min_io = blkid_topology_get_minimum_io_size(tp);
1155 opt_io = blkid_topology_get_optimal_io_size(tp);
1156 blocksize = EXT2_BLOCK_SIZE(fs_param);
1157 if ((min_io == 0) && (psector_size > blocksize))
1158 min_io = psector_size;
1159 if ((opt_io == 0) && min_io)
1161 if ((opt_io == 0) && (psector_size > blocksize))
1162 opt_io = psector_size;
1164 /* setting stripe/stride to blocksize is pointless */
1165 if (min_io > blocksize)
1166 fs_param->s_raid_stride = min_io / blocksize;
1167 if (opt_io > blocksize)
1168 fs_param->s_raid_stripe_width = opt_io / blocksize;
1170 rc = blkid_topology_get_alignment_offset(tp);
1172 blkid_free_probe(pr);
1177 static void PRS(int argc, char *argv[])
1180 int cluster_size = 0;
1183 int inode_ratio = 0;
1185 unsigned long flex_bg_size = 0;
1186 double reserved_ratio = -1.0;
1187 int lsector_size = 0, psector_size = 0;
1188 int show_version_only = 0;
1189 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1191 char * oldpath = getenv("PATH");
1192 char * extended_opts = 0;
1194 char * usage_types = 0;
1196 blk64_t fs_blocks_count = 0;
1201 int s_opt = -1, r_opt = -1;
1202 char *fs_features = 0;
1205 int pathlen = sizeof(PATH_SET) + 1;
1208 pathlen += strlen(oldpath);
1209 newpath = malloc(pathlen);
1211 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1214 strcpy(newpath, PATH_SET);
1216 /* Update our PATH to include /sbin */
1218 strcat (newpath, ":");
1219 strcat (newpath, oldpath);
1223 tmp = getenv("MKE2FS_SYNC");
1225 sync_kludge = atoi(tmp);
1227 /* Determine the system page size if possible */
1229 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1230 #define _SC_PAGESIZE _SC_PAGE_SIZE
1233 sysval = sysconf(_SC_PAGESIZE);
1235 sys_page_size = sysval;
1236 #endif /* _SC_PAGESIZE */
1237 #endif /* HAVE_SYSCONF */
1239 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1241 profile_set_syntax_err_cb(syntax_err_report);
1242 retval = profile_init(config_fn, &profile);
1243 if (retval == ENOENT) {
1244 retval = profile_init(default_files, &profile);
1247 retval = profile_set_default(profile, mke2fs_default_profile);
1250 } else if (retval) {
1252 fprintf(stderr, _("Couldn't init profile successfully"
1253 " (error: %ld).\n"), retval);
1257 setbuf(stdout, NULL);
1258 setbuf(stderr, NULL);
1259 add_error_table(&et_ext2_error_table);
1260 add_error_table(&et_prof_error_table);
1261 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1262 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1269 linux_version_code = parse_version_number(ut.release);
1270 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1271 fs_param.s_rev_level = 0;
1274 if (argc && *argv) {
1275 program_name = get_progname(*argv);
1277 /* If called as mkfs.ext3, create a journal inode */
1278 if (!strcmp(program_name, "mkfs.ext3") ||
1279 !strcmp(program_name, "mke3fs"))
1283 while ((c = getopt (argc, argv,
1284 "b:cg:i:jl:m:no:qr:s:t:vC:E:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1287 blocksize = strtol(optarg, &tmp, 0);
1288 b = (blocksize > 0) ? blocksize : -blocksize;
1289 if (b < EXT2_MIN_BLOCK_SIZE ||
1290 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1291 com_err(program_name, 0,
1292 _("invalid block size - %s"), optarg);
1295 if (blocksize > 4096)
1296 fprintf(stderr, _("Warning: blocksize %d not "
1297 "usable on most systems.\n"),
1300 fs_param.s_log_block_size =
1301 int_log2(blocksize >>
1302 EXT2_MIN_BLOCK_LOG_SIZE);
1304 case 'c': /* Check for bad blocks */
1308 cluster_size = strtoul(optarg, &tmp, 0);
1309 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1310 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
1311 com_err(program_name, 0,
1312 _("invalid fragment size - %s"),
1318 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1320 com_err(program_name, 0,
1321 _("Illegal number for blocks per group"));
1324 if ((fs_param.s_blocks_per_group % 8) != 0) {
1325 com_err(program_name, 0,
1326 _("blocks per group must be multiple of 8"));
1331 flex_bg_size = strtoul(optarg, &tmp, 0);
1333 com_err(program_name, 0,
1334 _("Illegal number for flex_bg size"));
1337 if (flex_bg_size < 1 ||
1338 (flex_bg_size & (flex_bg_size-1)) != 0) {
1339 com_err(program_name, 0,
1340 _("flex_bg size must be a power of 2"));
1345 inode_ratio = strtoul(optarg, &tmp, 0);
1346 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1347 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1349 com_err(program_name, 0,
1350 _("invalid inode ratio %s (min %d/max %d)"),
1351 optarg, EXT2_MIN_BLOCK_SIZE,
1352 EXT2_MAX_BLOCK_SIZE * 1024);
1357 parse_journal_opts(optarg);
1360 fprintf(stderr, _("Warning: -K option is deprecated and "
1361 "should not be used anymore. Use "
1362 "\'-E nodiscard\' extended option "
1371 bad_blocks_filename = malloc(strlen(optarg)+1);
1372 if (!bad_blocks_filename) {
1373 com_err(program_name, ENOMEM,
1374 _("in malloc for bad_blocks_filename"));
1377 strcpy(bad_blocks_filename, optarg);
1380 reserved_ratio = strtod(optarg, &tmp);
1381 if ( *tmp || reserved_ratio > 50 ||
1382 reserved_ratio < 0) {
1383 com_err(program_name, 0,
1384 _("invalid reserved blocks percent - %s"),
1393 creator_os = optarg;
1399 r_opt = strtoul(optarg, &tmp, 0);
1401 com_err(program_name, 0,
1402 _("bad revision level - %s"), optarg);
1405 fs_param.s_rev_level = r_opt;
1407 case 's': /* deprecated */
1408 s_opt = atoi(optarg);
1411 inode_size = strtoul(optarg, &tmp, 0);
1413 com_err(program_name, 0,
1414 _("invalid inode size - %s"), optarg);
1425 volume_label = optarg;
1431 num_inodes = strtoul(optarg, &tmp, 0);
1433 com_err(program_name, 0,
1434 _("bad num inodes - %s"), optarg);
1439 fs_features = optarg;
1443 extended_opts = optarg;
1449 fs_type = strdup(optarg);
1452 usage_types = strdup(optarg);
1458 /* Print version number and exit */
1459 show_version_only++;
1465 if ((optind == argc) && !show_version_only)
1467 device_name = argv[optind++];
1469 if (!quiet || show_version_only)
1470 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1473 if (show_version_only) {
1474 fprintf(stderr, _("\tUsing %s\n"),
1475 error_message(EXT2_ET_BASE));
1480 * If there's no blocksize specified and there is a journal
1481 * device, use it to figure out the blocksize
1483 if (blocksize <= 0 && journal_device) {
1487 #ifdef CONFIG_TESTIO_DEBUG
1488 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1489 io_ptr = test_io_manager;
1490 test_io_backing_manager = unix_io_manager;
1493 io_ptr = unix_io_manager;
1494 retval = ext2fs_open(journal_device,
1495 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1498 com_err(program_name, retval,
1499 _("while trying to open journal device %s\n"),
1503 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1504 com_err(program_name, 0,
1505 _("Journal dev blocksize (%d) smaller than "
1506 "minimum blocksize %d\n"), jfs->blocksize,
1510 blocksize = jfs->blocksize;
1511 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1512 fs_param.s_log_block_size =
1513 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1517 if (blocksize > sys_page_size) {
1519 com_err(program_name, 0,
1520 _("%d-byte blocks too big for system (max %d)"),
1521 blocksize, sys_page_size);
1524 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1525 "(max %d), forced to continue\n"),
1526 blocksize, sys_page_size);
1528 if (optind < argc) {
1529 fs_blocks_count = parse_num_blocks2(argv[optind++],
1530 fs_param.s_log_block_size);
1531 if (!fs_blocks_count) {
1532 com_err(program_name, 0,
1533 _("invalid blocks '%s' on device '%s'"),
1534 argv[optind - 1], device_name);
1542 check_plausibility(device_name);
1543 check_mount(device_name, force, _("filesystem"));
1545 /* Determine the size of the device (if possible) */
1546 if (noaction && fs_blocks_count) {
1547 dev_size = fs_blocks_count;
1550 retval = ext2fs_get_device_size2(device_name,
1551 EXT2_BLOCK_SIZE(&fs_param),
1554 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1555 com_err(program_name, retval,
1556 _("while trying to determine filesystem size"));
1559 if (!fs_blocks_count) {
1560 if (retval == EXT2_ET_UNIMPLEMENTED) {
1561 com_err(program_name, 0,
1562 _("Couldn't determine device size; you "
1563 "must specify\nthe size of the "
1567 if (dev_size == 0) {
1568 com_err(program_name, 0,
1569 _("Device size reported to be zero. "
1570 "Invalid partition specified, or\n\t"
1571 "partition table wasn't reread "
1572 "after running fdisk, due to\n\t"
1573 "a modified partition being busy "
1574 "and in use. You may need to reboot\n\t"
1575 "to re-read your partition table.\n"
1579 fs_blocks_count = dev_size;
1580 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1581 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1582 EXT2_BLOCK_SIZE(&fs_param))-1));
1584 } else if (!force && (fs_blocks_count > dev_size)) {
1585 com_err(program_name, 0,
1586 _("Filesystem larger than apparent device size."));
1591 profile_get_string(profile, "devices", device_name,
1592 "fs_type", 0, &fs_type);
1594 profile_get_string(profile, "devices", device_name,
1595 "usage_types", 0, &usage_types);
1598 * We have the file system (or device) size, so we can now
1599 * determine the appropriate file system types so the fs can
1600 * be appropriately configured.
1602 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1603 fs_blocks_count ? fs_blocks_count : dev_size,
1606 fprintf(stderr, _("Failed to parse fs types list\n"));
1610 /* Figure out what features should be enabled */
1613 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1614 tmp = get_string_from_profile(fs_types, "base_features",
1615 "sparse_super,filetype,resize_inode,dir_index");
1616 edit_feature(tmp, &fs_param.s_feature_compat);
1619 /* And which mount options as well */
1620 tmp = get_string_from_profile(fs_types, "default_mntopts",
1622 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1626 for (cpp = fs_types; *cpp; cpp++) {
1628 profile_get_string(profile, "fs_types", *cpp,
1629 "features", "", &tmp);
1631 edit_feature(tmp, &fs_param.s_feature_compat);
1635 tmp = get_string_from_profile(fs_types, "default_features",
1638 edit_feature(fs_features ? fs_features : tmp,
1639 &fs_param.s_feature_compat);
1644 * We now need to do a sanity check of fs_blocks_count for
1645 * 32-bit vs 64-bit block number support.
1647 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1648 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1650 fs_param.s_log_block_size = 2;
1652 if ((fs_blocks_count > MAX_32_NUM) &&
1653 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1654 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1655 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1656 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1658 if ((fs_blocks_count > MAX_32_NUM) &&
1659 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1660 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1661 "too big to be expressed\n\t"
1662 "in 32 bits using a blocksize of %d.\n"),
1663 program_name, fs_blocks_count, device_name,
1664 EXT2_BLOCK_SIZE(&fs_param));
1668 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1670 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1671 fs_types[0] = strdup("journal");
1676 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1677 print_str_list(fs_types);
1680 if (r_opt == EXT2_GOOD_OLD_REV &&
1681 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1682 fs_param.s_feature_ro_compat)) {
1683 fprintf(stderr, _("Filesystem features not supported "
1684 "with revision 0 filesystems\n"));
1689 if (r_opt == EXT2_GOOD_OLD_REV) {
1690 fprintf(stderr, _("Sparse superblocks not supported "
1691 "with revision 0 filesystems\n"));
1694 fs_param.s_feature_ro_compat |=
1695 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1696 } else if (s_opt == 0)
1697 fs_param.s_feature_ro_compat &=
1698 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1700 if (journal_size != 0) {
1701 if (r_opt == EXT2_GOOD_OLD_REV) {
1702 fprintf(stderr, _("Journals not supported "
1703 "with revision 0 filesystems\n"));
1706 fs_param.s_feature_compat |=
1707 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1710 /* Get reserved_ratio from profile if not specified on cmd line. */
1711 if (reserved_ratio < 0.0) {
1712 reserved_ratio = get_double_from_profile(
1713 fs_types, "reserved_ratio", 5.0);
1714 if (reserved_ratio > 50 || reserved_ratio < 0) {
1715 com_err(program_name, 0,
1716 _("invalid reserved blocks percent - %lf"),
1722 if (fs_param.s_feature_incompat &
1723 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1725 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1726 fs_param.s_feature_compat = 0;
1727 fs_param.s_feature_ro_compat = 0;
1730 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1731 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1732 fprintf(stderr, _("The resize_inode and meta_bg features "
1733 "are not compatible.\n"
1734 "They can not be both enabled "
1735 "simultaneously.\n"));
1739 /* Set first meta blockgroup via an environment variable */
1740 /* (this is mostly for debugging purposes) */
1741 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1742 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1743 fs_param.s_first_meta_bg = atoi(tmp);
1745 /* Get the hardware sector sizes, if available */
1746 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1748 com_err(program_name, retval,
1749 _("while trying to determine hardware sector size"));
1752 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1754 com_err(program_name, retval,
1755 _("while trying to determine physical sector size"));
1759 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1760 lsector_size = atoi(tmp);
1761 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1762 psector_size = atoi(tmp);
1764 /* Older kernels may not have physical/logical distinction */
1766 psector_size = lsector_size;
1768 if (blocksize <= 0) {
1769 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1771 if (use_bsize == -1) {
1772 use_bsize = sys_page_size;
1773 if ((linux_version_code < (2*65536 + 6*256)) &&
1777 if (lsector_size && use_bsize < lsector_size)
1778 use_bsize = lsector_size;
1779 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1780 use_bsize = -blocksize;
1781 blocksize = use_bsize;
1782 ext2fs_blocks_count_set(&fs_param,
1783 ext2fs_blocks_count(&fs_param) /
1784 (blocksize / 1024));
1786 if (blocksize < lsector_size) { /* Impossible */
1787 com_err(program_name, EINVAL,
1788 _("while setting blocksize; too small "
1791 } else if ((blocksize < psector_size) &&
1792 (psector_size <= sys_page_size)) { /* Suboptimal */
1793 fprintf(stderr, _("Warning: specified blocksize %d is "
1794 "less than device physical sectorsize %d\n"),
1795 blocksize, psector_size);
1799 fs_param.s_log_block_size =
1800 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1801 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1803 cluster_size = get_int_from_profile(fs_types,
1806 fs_param.s_log_cluster_size =
1807 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1809 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1811 if (inode_ratio == 0) {
1812 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1814 if (inode_ratio < blocksize)
1815 inode_ratio = blocksize;
1816 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1817 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1820 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1821 retval = get_device_geometry(device_name, &fs_param, psector_size);
1824 _("warning: Unable to get device geometry for %s\n"),
1826 } else if (retval) {
1827 printf(_("%s alignment is offset by %lu bytes.\n"),
1828 device_name, retval);
1829 printf(_("This may result in very poor performance, "
1830 "(re)-partitioning suggested.\n"));
1834 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1836 lazy_itable_init = 0;
1837 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1838 lazy_itable_init = 1;
1840 lazy_itable_init = get_bool_from_profile(fs_types,
1843 discard = get_bool_from_profile(fs_types, "discard" , discard);
1844 journal_flags |= get_bool_from_profile(fs_types,
1845 "lazy_journal_init", 0) ?
1846 EXT2_MKJOURNAL_LAZYINIT : 0;
1848 /* Get options from profile */
1849 for (cpp = fs_types; *cpp; cpp++) {
1851 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1853 parse_extended_opts(&fs_param, tmp);
1858 parse_extended_opts(&fs_param, extended_opts);
1860 /* Since sparse_super is the default, we would only have a problem
1861 * here if it was explicitly disabled.
1863 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1864 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1865 com_err(program_name, 0,
1866 _("reserved online resize blocks not supported "
1867 "on non-sparse filesystem"));
1871 if (fs_param.s_blocks_per_group) {
1872 if (fs_param.s_blocks_per_group < 256 ||
1873 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1874 com_err(program_name, 0,
1875 _("blocks per group count out of range"));
1880 if (inode_size == 0)
1881 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1882 if (!flex_bg_size && (fs_param.s_feature_incompat &
1883 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1884 flex_bg_size = get_int_from_profile(fs_types,
1885 "flex_bg_size", 16);
1887 if (!(fs_param.s_feature_incompat &
1888 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1889 com_err(program_name, 0,
1890 _("Flex_bg feature not enabled, so "
1891 "flex_bg size may not be specified"));
1894 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1897 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1898 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1899 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1900 inode_size & (inode_size - 1)) {
1901 com_err(program_name, 0,
1902 _("invalid inode size %d (min %d/max %d)"),
1903 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1907 fs_param.s_inode_size = inode_size;
1910 /* Make sure number of inodes specified will fit in 32 bits */
1911 if (num_inodes == 0) {
1912 unsigned long long n;
1913 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1914 if (n > MAX_32_NUM) {
1915 if (fs_param.s_feature_incompat &
1916 EXT4_FEATURE_INCOMPAT_64BIT)
1917 num_inodes = MAX_32_NUM;
1919 com_err(program_name, 0,
1920 _("too many inodes (%llu), raise"
1921 "inode ratio?"), n);
1925 } else if (num_inodes > MAX_32_NUM) {
1926 com_err(program_name, 0,
1927 _("too many inodes (%llu), specify < 2^32 inodes"),
1932 * Calculate number of inodes based on the inode ratio
1934 fs_param.s_inodes_count = num_inodes ? num_inodes :
1935 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1937 if ((((long long)fs_param.s_inodes_count) *
1938 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1939 ((ext2fs_blocks_count(&fs_param)) *
1940 EXT2_BLOCK_SIZE(&fs_param))) {
1941 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1942 "(%u) too big for a\n\t"
1943 "filesystem with %llu blocks, "
1944 "specify higher inode_ratio (-i)\n\t"
1945 "or lower inode count (-N).\n"),
1946 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1947 fs_param.s_inodes_count,
1948 (unsigned long long) ext2fs_blocks_count(&fs_param));
1953 * Calculate number of blocks to reserve
1955 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1956 ext2fs_blocks_count(&fs_param) / 100.0);
1962 static int should_do_undo(const char *name)
1967 struct ext2_super_block super;
1968 io_manager manager = unix_io_manager;
1969 int csum_flag, force_undo;
1971 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1972 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1973 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1974 if (!force_undo && (!csum_flag || !lazy_itable_init))
1977 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1980 * We don't handle error cases instead we
1981 * declare that the file system doesn't exist
1982 * and let the rest of mke2fs take care of
1989 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1990 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1996 #if defined(WORDS_BIGENDIAN)
1997 s_magic = ext2fs_swab16(super.s_magic);
1999 s_magic = super.s_magic;
2002 if (s_magic == EXT2_SUPER_MAGIC)
2006 io_channel_close(channel);
2013 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2015 errcode_t retval = ENOMEM;
2016 char *tdb_dir, *tdb_file = NULL;
2017 char *device_name, *tmp_name;
2020 * Configuration via a conf file would be
2023 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2025 profile_get_string(profile, "defaults",
2026 "undo_dir", 0, "/var/lib/e2fsprogs",
2029 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2030 access(tdb_dir, W_OK))
2033 tmp_name = strdup(name);
2036 device_name = basename(tmp_name);
2037 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
2042 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
2045 if (!access(tdb_file, F_OK)) {
2046 if (unlink(tdb_file) < 0) {
2052 set_undo_io_backing_manager(*io_ptr);
2053 *io_ptr = undo_io_manager;
2054 retval = set_undo_io_backup_file(tdb_file);
2057 printf(_("Overwriting existing filesystem; this can be undone "
2058 "using the command:\n"
2059 " e2undo %s %s\n\n"), tdb_file, name);
2066 com_err(program_name, retval,
2067 _("while trying to setup undo file\n"));
2071 static int mke2fs_discard_device(ext2_filsys fs)
2073 struct ext2fs_numeric_progress_struct progress;
2074 blk64_t blocks = ext2fs_blocks_count(fs->super);
2075 blk64_t count = DISCARD_STEP_MB;
2079 retval = io_channel_discard(fs->io, 0, 0);
2083 count *= (1024 * 1024);
2084 count /= fs->blocksize;
2086 ext2fs_numeric_progress_init(fs, &progress,
2087 _("Discarding device blocks: "),
2089 while (cur < blocks) {
2090 ext2fs_numeric_progress_update(fs, &progress, cur);
2092 if (cur + count > blocks)
2093 count = blocks - cur;
2095 retval = io_channel_discard(fs->io, cur, count);
2102 ext2fs_numeric_progress_close(fs, &progress,
2105 printf("%s\n",error_message(retval));
2107 ext2fs_numeric_progress_close(fs, &progress,
2113 static void fix_cluster_bg_counts(ext2_filsys fs)
2115 blk64_t cluster, num_clusters, tot_free;
2116 int grp_free, num_free, group, num;
2118 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2119 tot_free = num_free = num = group = grp_free = 0;
2120 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2121 cluster < num_clusters; cluster++) {
2122 if (!ext2fs_test_block_bitmap2(fs->block_map,
2123 EXT2FS_C2B(fs, cluster))) {
2128 if ((num == fs->super->s_clusters_per_group) ||
2129 (cluster == num_clusters-1)) {
2130 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2131 ext2fs_group_desc_csum_set(fs, group);
2137 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2140 int main (int argc, char *argv[])
2142 errcode_t retval = 0;
2144 badblocks_list bb_list = 0;
2145 unsigned int journal_blocks;
2151 char tdb_string[40];
2153 int itable_zeroed = 0;
2156 setlocale(LC_MESSAGES, "");
2157 setlocale(LC_CTYPE, "");
2158 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2159 textdomain(NLS_CAT_NAME);
2163 #ifdef CONFIG_TESTIO_DEBUG
2164 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2165 io_ptr = test_io_manager;
2166 test_io_backing_manager = unix_io_manager;
2169 io_ptr = unix_io_manager;
2171 if (should_do_undo(device_name)) {
2172 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2178 * Initialize the superblock....
2180 flags = EXT2_FLAG_EXCLUSIVE;
2181 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2184 flags |= EXT2_FLAG_64BITS;
2186 * By default, we print how many inode tables or block groups
2187 * or whatever we've written so far. The quiet flag disables
2188 * this, along with a lot of other output.
2191 flags |= EXT2_FLAG_PRINT_PROGRESS;
2192 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2194 com_err(device_name, retval, _("while setting up superblock"));
2198 /* Can't undo discard ... */
2199 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2200 retval = mke2fs_discard_device(fs);
2201 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2203 printf(_("Discard succeeded and will return 0s "
2204 " - skipping inode table wipe\n"));
2205 lazy_itable_init = 1;
2210 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2211 32768 : fs->blocksize * 8);
2212 io_channel_set_options(fs->io, tdb_string);
2214 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2215 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2217 if ((fs_param.s_feature_incompat &
2218 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2219 (fs_param.s_feature_ro_compat &
2220 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2221 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2222 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2223 fs->super->s_kbytes_written = 1;
2226 * Wipe out the old on-disk superblock
2229 zap_sector(fs, 2, 6);
2232 * Parse or generate a UUID for the filesystem
2235 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2236 com_err(device_name, 0, "could not parse UUID: %s\n",
2241 uuid_generate(fs->super->s_uuid);
2244 * Initialize the directory index variables
2246 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2248 hash_alg = e2p_string2hash(hash_alg_str);
2250 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2252 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2255 * Periodic checks can be enabled/disabled via config file.
2256 * Note we override the kernel include file's idea of what the default
2257 * check interval (never) should be. It's a good idea to check at
2258 * least *occasionally*, specially since servers will never rarely get
2259 * to reboot, since Linux is so robust these days. :-)
2261 * 180 days (six months) seems like a good value.
2263 #ifdef EXT2_DFL_CHECKINTERVAL
2264 #undef EXT2_DFL_CHECKINTERVAL
2266 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2268 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2269 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2270 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2272 * Add "jitter" to the superblock's check interval so that we
2273 * don't check all the filesystems at the same time. We use a
2274 * kludgy hack of using the UUID to derive a random jitter value
2276 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2277 val += fs->super->s_uuid[i];
2278 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2282 * Override the creator OS, if applicable
2284 if (creator_os && !set_os(fs->super, creator_os)) {
2285 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2290 * For the Hurd, we will turn off filetype since it doesn't
2293 if (fs->super->s_creator_os == EXT2_OS_HURD)
2294 fs->super->s_feature_incompat &=
2295 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2298 * Set the volume label...
2301 memset(fs->super->s_volume_name, 0,
2302 sizeof(fs->super->s_volume_name));
2303 strncpy(fs->super->s_volume_name, volume_label,
2304 sizeof(fs->super->s_volume_name));
2308 * Set the last mount directory
2311 memset(fs->super->s_last_mounted, 0,
2312 sizeof(fs->super->s_last_mounted));
2313 strncpy(fs->super->s_last_mounted, mount_dir,
2314 sizeof(fs->super->s_last_mounted));
2317 if (!quiet || noaction)
2323 if (fs->super->s_feature_incompat &
2324 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2325 create_journal_dev(fs);
2326 exit(ext2fs_close(fs) ? 1 : 0);
2329 if (bad_blocks_filename)
2330 read_bb_file(fs, &bb_list, bad_blocks_filename);
2332 test_disk(fs, &bb_list);
2334 handle_bad_blocks(fs, bb_list);
2335 fs->stride = fs_stride = fs->super->s_raid_stride;
2337 printf(_("Allocating group tables: "));
2338 retval = ext2fs_allocate_tables(fs);
2340 com_err(program_name, retval,
2341 _("while trying to allocate filesystem tables"));
2345 printf(_("done \n"));
2347 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2349 com_err(program_name, retval,
2350 _("\n\twhile converting subcluster bitmap"));
2355 fs->super->s_state |= EXT2_ERROR_FS;
2356 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2358 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2359 blk64_t rsv = 65536 / fs->blocksize;
2360 blk64_t blocks = ext2fs_blocks_count(fs->super);
2364 #ifdef ZAP_BOOTBLOCK
2365 zap_sector(fs, 0, 2);
2369 * Wipe out any old MD RAID (or other) metadata at the end
2370 * of the device. This will also verify that the device is
2371 * as large as we think. Be careful with very small devices.
2373 start = (blocks & ~(rsv - 1));
2377 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2381 com_err(program_name, retval,
2382 _("while zeroing block %llu at end of filesystem"),
2385 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2386 create_root_dir(fs);
2387 create_lost_and_found(fs);
2389 create_bad_block_inode(fs, bb_list);
2390 if (fs->super->s_feature_compat &
2391 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2392 retval = ext2fs_create_resize_inode(fs);
2394 com_err("ext2fs_create_resize_inode", retval,
2395 _("while reserving blocks for online resize"));
2401 if (journal_device) {
2405 check_plausibility(journal_device);
2406 check_mount(journal_device, force, _("journal"));
2408 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2409 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2410 fs->blocksize, unix_io_manager, &jfs);
2412 com_err(program_name, retval,
2413 _("while trying to open journal device %s\n"),
2418 printf(_("Adding journal to device %s: "),
2422 retval = ext2fs_add_journal_device(fs, jfs);
2424 com_err (program_name, retval,
2425 _("\n\twhile trying to add journal to device %s"),
2430 printf(_("done\n"));
2432 free(journal_device);
2433 } else if ((journal_size) ||
2434 (fs_param.s_feature_compat &
2435 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2436 journal_blocks = figure_journal_size(journal_size, fs);
2439 printf(_("Skipping journal creation in super-only mode\n"));
2440 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2444 if (!journal_blocks) {
2445 fs->super->s_feature_compat &=
2446 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2450 printf(_("Creating journal (%u blocks): "),
2454 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2457 com_err (program_name, retval,
2458 _("\n\twhile trying to create journal"));
2462 printf(_("done\n"));
2466 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2467 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2468 fix_cluster_bg_counts(fs);
2470 printf(_("Writing superblocks and "
2471 "filesystem accounting information: "));
2472 retval = ext2fs_flush(fs);
2475 _("\nWarning, had trouble writing out superblocks."));
2478 printf(_("done\n\n"));
2479 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2480 print_check_message(fs);
2482 val = ext2fs_close(fs);
2483 remove_error_table(&et_ext2_error_table);
2484 remove_error_table(&et_prof_error_table);
2485 profile_release(profile);
2486 for (i=0; fs_types[i]; i++)
2489 return (retval || val) ? 1 : 0;