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 "et/com_err.h"
57 #include "uuid/uuid.h"
59 #include "ext2fs/ext2fs.h"
63 #include "../version.h"
64 #include "nls-enable.h"
66 #define STRIDE_LENGTH 8
72 extern int isatty(int);
73 extern FILE *fpopen(const char *cmd, const char *mode);
75 const char * program_name = "mke2fs";
76 const char * device_name /* = NULL */;
78 /* Command line options */
83 int discard = 1; /* attempt to discard device before fs creation */
89 char *bad_blocks_filename;
92 struct ext2_super_block fs_param;
98 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
103 int sys_page_size = 4096;
104 int linux_version_code = 0;
106 static void usage(void)
108 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
109 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
110 "[-J journal-options]\n"
111 "\t[-G meta group size] [-N number-of-inodes]\n"
112 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
113 "\t[-g blocks-per-group] [-L volume-label] "
114 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
115 "[-r fs-revision] [-E extended-option[,...]]\n"
116 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
121 static int int_log2(int arg)
133 static int int_log10(unsigned int arg)
142 static int parse_version_number(const char *s)
144 int major, minor, rev;
150 major = strtol(cp, &endptr, 10);
151 if (cp == endptr || *endptr != '.')
154 minor = strtol(cp, &endptr, 10);
155 if (cp == endptr || *endptr != '.')
158 rev = strtol(cp, &endptr, 10);
161 return ((((major * 256) + minor) * 256) + rev);
165 * Helper function for read_bb_file and test_disk
167 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
169 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
174 * Reads the bad blocks list from a file
176 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
177 const char *bad_blocks_file)
182 f = fopen(bad_blocks_file, "r");
184 com_err("read_bad_blocks_file", errno,
185 _("while trying to open %s"), bad_blocks_file);
188 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
191 com_err("ext2fs_read_bb_FILE", retval,
192 _("while reading in list of bad blocks from file"));
198 * Runs the badblocks program to test the disk
200 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
206 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
207 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
208 fs->device_name, fs->super->s_blocks_count-1);
210 printf(_("Running command: %s\n"), buf);
213 com_err("popen", errno,
214 _("while trying to run '%s'"), buf);
217 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
220 com_err("ext2fs_read_bb_FILE", retval,
221 _("while processing list of bad blocks from program"));
226 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
230 unsigned must_be_good;
232 badblocks_iterate bb_iter;
242 * The primary superblock and group descriptors *must* be
243 * good; if not, abort.
245 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
246 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
247 if (ext2fs_badblocks_list_test(bb_list, i)) {
248 fprintf(stderr, _("Block %d in primary "
249 "superblock/group descriptor area bad.\n"), i);
250 fprintf(stderr, _("Blocks %u through %u must be good "
251 "in order to build a filesystem.\n"),
252 fs->super->s_first_data_block, must_be_good);
253 fputs(_("Aborting....\n"), stderr);
259 * See if any of the bad blocks are showing up in the backup
260 * superblocks and/or group descriptors. If so, issue a
261 * warning and adjust the block counts appropriately.
263 group_block = fs->super->s_first_data_block +
264 fs->super->s_blocks_per_group;
266 for (i = 1; i < fs->group_desc_count; i++) {
268 for (j=0; j < fs->desc_blocks+1; j++) {
269 if (ext2fs_badblocks_list_test(bb_list,
273 _("Warning: the backup superblock/group descriptors at block %u contain\n"
277 group = ext2fs_group_of_blk(fs, group_block+j);
278 fs->group_desc[group].bg_free_blocks_count++;
279 ext2fs_group_desc_csum_set(fs, group);
280 fs->super->s_free_blocks_count++;
283 group_block += fs->super->s_blocks_per_group;
287 * Mark all the bad blocks as used...
289 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
291 com_err("ext2fs_badblocks_list_iterate_begin", retval,
292 _("while marking bad blocks as used"));
295 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
296 ext2fs_mark_block_bitmap(fs->block_map, blk);
297 ext2fs_badblocks_list_iterate_end(bb_iter);
301 * These functions implement a generalized progress meter.
303 struct progress_struct {
310 static void progress_init(struct progress_struct *progress,
311 const char *label,__u32 max)
315 memset(progress, 0, sizeof(struct progress_struct));
320 * Figure out how many digits we need
323 sprintf(progress->format, "%%%dd/%%%dld", i, i);
324 memset(progress->backup, '\b', sizeof(progress->backup)-1);
325 progress->backup[sizeof(progress->backup)-1] = 0;
326 if ((2*i)+1 < (int) sizeof(progress->backup))
327 progress->backup[(2*i)+1] = 0;
330 progress->skip_progress = 0;
331 if (getenv("MKE2FS_SKIP_PROGRESS"))
332 progress->skip_progress++;
334 fputs(label, stdout);
338 static void progress_update(struct progress_struct *progress, __u32 val)
340 if ((progress->format[0] == 0) || progress->skip_progress)
342 printf(progress->format, val, progress->max);
343 fputs(progress->backup, stdout);
346 static void progress_close(struct progress_struct *progress)
348 if (progress->format[0] == 0)
350 fputs(_("done \n"), stdout);
353 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
359 struct progress_struct progress;
362 memset(&progress, 0, sizeof(progress));
364 progress_init(&progress, _("Writing inode tables: "),
365 fs->group_desc_count);
367 for (i = 0; i < fs->group_desc_count; i++) {
368 progress_update(&progress, i);
370 blk = fs->group_desc[i].bg_inode_table;
371 num = fs->inode_blocks_per_group;
374 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
375 num = ((((fs->super->s_inodes_per_group -
376 fs->group_desc[i].bg_itable_unused) *
377 EXT2_INODE_SIZE(fs->super)) +
378 EXT2_BLOCK_SIZE(fs->super) - 1) /
379 EXT2_BLOCK_SIZE(fs->super));
381 if (!lazy_flag || itable_zeroed) {
382 /* The kernel doesn't need to zero the itable blocks */
383 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
384 ext2fs_group_desc_csum_set(fs, i);
386 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
388 fprintf(stderr, _("\nCould not write %d "
389 "blocks in inode table starting at %u: %s\n"),
390 num, blk, error_message(retval));
394 if (sync_kludge == 1)
396 else if ((i % sync_kludge) == 0)
400 ext2fs_zero_blocks(0, 0, 0, 0, 0);
401 progress_close(&progress);
404 static void create_root_dir(ext2_filsys fs)
407 struct ext2_inode inode;
410 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
412 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
416 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
418 com_err("ext2fs_read_inode", retval,
419 _("while reading root inode"));
424 ext2fs_set_i_uid_high(inode, uid >> 16);
428 ext2fs_set_i_gid_high(inode, gid >> 16);
430 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
432 com_err("ext2fs_write_inode", retval,
433 _("while setting root inode ownership"));
439 static void create_lost_and_found(ext2_filsys fs)
441 unsigned int lpf_size = 0;
444 const char *name = "lost+found";
448 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
450 com_err("ext2fs_mkdir", retval,
451 _("while creating /lost+found"));
455 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
457 com_err("ext2_lookup", retval,
458 _("while looking up /lost+found"));
462 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
463 /* Ensure that lost+found is at least 2 blocks, so we always
464 * test large empty blocks for big-block filesystems. */
465 if ((lpf_size += fs->blocksize) >= 16*1024 &&
466 lpf_size >= 2 * fs->blocksize)
468 retval = ext2fs_expand_dir(fs, ino);
470 com_err("ext2fs_expand_dir", retval,
471 _("while expanding /lost+found"));
477 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
481 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
482 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
483 retval = ext2fs_update_bb_inode(fs, bb_list);
485 com_err("ext2fs_update_bb_inode", retval,
486 _("while setting bad block inode"));
492 static void reserve_inodes(ext2_filsys fs)
496 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
497 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
498 ext2fs_mark_ib_dirty(fs);
501 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
502 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
503 #define BSD_LABEL_OFFSET 64
505 static void zap_sector(ext2_filsys fs, int sect, int nsect)
511 buf = malloc(512*nsect);
513 printf(_("Out of memory erasing sectors %d-%d\n"),
514 sect, sect + nsect - 1);
519 /* Check for a BSD disklabel, and don't erase it if so */
520 retval = io_channel_read_blk(fs->io, 0, -512, buf);
523 _("Warning: could not read block 0: %s\n"),
524 error_message(retval));
526 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
527 if ((*magic == BSD_DISKMAGIC) ||
528 (*magic == BSD_MAGICDISK))
533 memset(buf, 0, 512*nsect);
534 io_channel_set_blksize(fs->io, 512);
535 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
536 io_channel_set_blksize(fs->io, fs->blocksize);
539 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
540 sect, error_message(retval));
543 static void create_journal_dev(ext2_filsys fs)
545 struct progress_struct progress;
549 int c, count, err_count;
551 retval = ext2fs_create_journal_superblock(fs,
552 fs->super->s_blocks_count, 0, &buf);
554 com_err("create_journal_dev", retval,
555 _("while initializing journal superblock"));
559 memset(&progress, 0, sizeof(progress));
561 progress_init(&progress, _("Zeroing journal device: "),
562 fs->super->s_blocks_count);
565 count = fs->super->s_blocks_count;
571 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
573 com_err("create_journal_dev", retval,
574 _("while zeroing journal device "
575 "(block %u, count %d)"),
581 progress_update(&progress, blk);
583 ext2fs_zero_blocks(0, 0, 0, 0, 0);
585 retval = io_channel_write_blk(fs->io,
586 fs->super->s_first_data_block+1,
589 com_err("create_journal_dev", retval,
590 _("while writing journal superblock"));
593 progress_close(&progress);
596 static void show_stats(ext2_filsys fs)
598 struct ext2_super_block *s = fs->super;
605 if (fs_param.s_blocks_count != s->s_blocks_count)
606 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
607 fs_param.s_blocks_count - s->s_blocks_count);
609 memset(buf, 0, sizeof(buf));
610 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
611 printf(_("Filesystem label=%s\n"), buf);
612 fputs(_("OS type: "), stdout);
613 os = e2p_os2string(fs->super->s_creator_os);
617 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
618 s->s_log_block_size);
619 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
620 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
621 printf(_("Cluster size=%u (log=%u)\n"),
622 fs->blocksize << fs->cluster_ratio_bits,
623 s->s_log_cluster_size);
625 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
626 s->s_log_cluster_size);
627 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
628 s->s_raid_stride, s->s_raid_stripe_width);
629 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
631 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
633 100.0 * s->s_r_blocks_count / s->s_blocks_count);
634 printf(_("First data block=%u\n"), s->s_first_data_block);
635 if (s->s_reserved_gdt_blocks)
636 printf(_("Maximum filesystem blocks=%lu\n"),
637 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
638 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
639 if (fs->group_desc_count > 1)
640 printf(_("%u block groups\n"), fs->group_desc_count);
642 printf(_("%u block group\n"), fs->group_desc_count);
643 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
644 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
645 printf(_("%u blocks per group, %u clusters per group\n"),
646 s->s_blocks_per_group, s->s_clusters_per_group);
648 printf(_("%u blocks per group, %u fragments per group\n"),
649 s->s_blocks_per_group, s->s_clusters_per_group);
650 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
652 if (fs->group_desc_count == 1) {
657 printf(_("Superblock backups stored on blocks: "));
658 group_block = s->s_first_data_block;
660 for (i = 1; i < fs->group_desc_count; i++) {
661 group_block += s->s_blocks_per_group;
662 if (!ext2fs_bg_has_super(fs, i))
666 need = int_log10(group_block) + 2;
667 if (need > col_left) {
672 printf("%u", group_block);
678 * Set the S_CREATOR_OS field. Return true if OS is known,
681 static int set_os(struct ext2_super_block *sb, char *os)
684 sb->s_creator_os = atoi (os);
685 else if (strcasecmp(os, "linux") == 0)
686 sb->s_creator_os = EXT2_OS_LINUX;
687 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
688 sb->s_creator_os = EXT2_OS_HURD;
689 else if (strcasecmp(os, "freebsd") == 0)
690 sb->s_creator_os = EXT2_OS_FREEBSD;
691 else if (strcasecmp(os, "lites") == 0)
692 sb->s_creator_os = EXT2_OS_LITES;
698 #define PATH_SET "PATH=/sbin"
700 static void parse_extended_opts(struct ext2_super_block *param,
703 char *buf, *token, *next, *p, *arg, *badopt = 0;
711 _("Couldn't allocate memory to parse options!\n"));
715 for (token = buf; token && *token; token = next) {
716 p = strchr(token, ',');
722 arg = strchr(token, '=');
727 if (strcmp(token, "stride") == 0) {
733 param->s_raid_stride = strtoul(arg, &p, 0);
734 if (*p || (param->s_raid_stride == 0)) {
736 _("Invalid stride parameter: %s\n"),
741 } else if (strcmp(token, "stripe-width") == 0 ||
742 strcmp(token, "stripe_width") == 0) {
748 param->s_raid_stripe_width = strtoul(arg, &p, 0);
749 if (*p || (param->s_raid_stripe_width == 0)) {
751 _("Invalid stripe-width parameter: %s\n"),
756 } else if (!strcmp(token, "resize")) {
757 unsigned long resize, bpg, rsv_groups;
758 unsigned long group_desc_count, desc_blocks;
759 unsigned int gdpb, blocksize;
768 resize = parse_num_blocks(arg,
769 param->s_log_block_size);
773 _("Invalid resize parameter: %s\n"),
778 if (resize <= param->s_blocks_count) {
780 _("The resize maximum must be greater "
781 "than the filesystem size.\n"));
786 blocksize = EXT2_BLOCK_SIZE(param);
787 bpg = param->s_blocks_per_group;
790 gdpb = EXT2_DESC_PER_BLOCK(param);
792 ext2fs_div_ceil(param->s_blocks_count, bpg);
793 desc_blocks = (group_desc_count +
795 rsv_groups = ext2fs_div_ceil(resize, bpg);
796 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
798 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
799 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
802 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
804 _("On-line resizing not supported with revision 0 filesystems\n"));
808 param->s_feature_compat |=
809 EXT2_FEATURE_COMPAT_RESIZE_INODE;
811 param->s_reserved_gdt_blocks = rsv_gdb;
813 } else if (!strcmp(token, "test_fs")) {
814 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
815 } else if (!strcmp(token, "lazy_itable_init")) {
817 lazy_itable_init = strtoul(arg, &p, 0);
819 lazy_itable_init = 1;
820 } else if (!strcmp(token, "discard")) {
822 } else if (!strcmp(token, "nodiscard")) {
830 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
831 "Extended options are separated by commas, "
832 "and may take an argument which\n"
833 "\tis set off by an equals ('=') sign.\n\n"
834 "Valid extended options are:\n"
835 "\tstride=<RAID per-disk data chunk in blocks>\n"
836 "\tstripe-width=<RAID stride * data disks in blocks>\n"
837 "\tresize=<resize maximum size in blocks>\n"
838 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
842 badopt ? badopt : "");
846 if (param->s_raid_stride &&
847 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
848 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
849 "multiple of stride %u.\n\n"),
850 param->s_raid_stripe_width, param->s_raid_stride);
855 static __u32 ok_features[3] = {
857 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
858 EXT2_FEATURE_COMPAT_RESIZE_INODE |
859 EXT2_FEATURE_COMPAT_DIR_INDEX |
860 EXT2_FEATURE_COMPAT_EXT_ATTR,
862 EXT2_FEATURE_INCOMPAT_FILETYPE|
863 EXT3_FEATURE_INCOMPAT_EXTENTS|
864 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
865 EXT2_FEATURE_INCOMPAT_META_BG|
866 EXT4_FEATURE_INCOMPAT_FLEX_BG,
868 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
869 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
870 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
871 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
872 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
873 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
877 static void syntax_err_report(const char *filename, long err, int line_num)
880 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
881 filename, line_num, error_message(err));
885 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
887 static void edit_feature(const char *str, __u32 *compat_array)
892 if (e2p_edit_feature(str, compat_array, ok_features)) {
893 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
899 static void edit_mntopts(const char *str, __u32 *mntopts)
904 if (e2p_edit_mntopts(str, mntopts, ~0)) {
905 fprintf(stderr, _("Invalid mount option set: %s\n"),
917 static errcode_t init_list(struct str_list *sl)
921 sl->list = malloc((sl->max+1) * sizeof(char *));
928 static errcode_t push_string(struct str_list *sl, const char *str)
932 if (sl->num >= sl->max) {
934 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
939 sl->list[sl->num] = malloc(strlen(str)+1);
940 if (sl->list[sl->num] == 0)
942 strcpy(sl->list[sl->num], str);
944 sl->list[sl->num] = 0;
948 static void print_str_list(char **list)
952 for (cpp = list; *cpp; cpp++) {
953 printf("'%s'", *cpp);
961 * Return TRUE if the profile has the given subsection
963 static int profile_has_subsection(profile_t profile, const char *section,
964 const char *subsection)
967 const char *names[4];
972 names[1] = subsection;
975 if (profile_iterator_create(profile, names,
976 PROFILE_ITER_LIST_SECTION |
977 PROFILE_ITER_RELATIONS_ONLY, &state))
980 if ((profile_iterator(&state, &name, 0) == 0) && name) {
985 profile_iterator_free(&state);
989 static char **parse_fs_type(const char *fs_type,
990 const char *usage_types,
991 struct ext2_super_block *fs_param,
994 const char *ext_type = 0;
996 char *profile_type = 0;
998 const char *size_type;
999 struct str_list list;
1003 if (init_list(&list))
1006 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1007 !strcasecmp(creator_os, "hurd")))
1014 else if (!strcmp(program_name, "mke3fs"))
1016 else if (progname) {
1017 ext_type = strrchr(progname, '/');
1021 ext_type = progname;
1023 if (!strncmp(ext_type, "mkfs.", 5)) {
1025 if (ext_type[0] == 0)
1032 profile_get_string(profile, "defaults", "fs_type", 0,
1033 "ext2", &profile_type);
1034 ext_type = profile_type;
1035 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1040 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1041 strcmp(ext_type, "ext2")) {
1042 printf(_("\nYour mke2fs.conf file does not define the "
1043 "%s filesystem type.\n"), ext_type);
1044 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1045 !strcmp(ext_type, "ext4dev")) {
1046 printf(_("You probably need to install an updated "
1047 "mke2fs.conf file.\n\n"));
1050 printf(_("Aborting...\n"));
1055 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1056 if (fs_param->s_blocks_count < 3 * meg)
1057 size_type = "floppy";
1058 else if (fs_param->s_blocks_count < 512 * meg)
1059 size_type = "small";
1061 size_type = "default";
1064 usage_types = size_type;
1066 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1072 strcpy(parse_str, usage_types);
1077 push_string(&list, ext_type);
1080 t = strchr(cp, ',');
1085 if (profile_has_subsection(profile, "fs_types", cp))
1086 push_string(&list, cp);
1087 else if (strcmp(cp, "default") != 0)
1089 _("\nWarning: the fs_type %s is not "
1090 "defined in mke2fs.conf\n\n"),
1103 push_string(&list, "hurd");
1107 static char *get_string_from_profile(char **fs_types, const char *opt,
1108 const char *def_val)
1113 for (i=0; fs_types[i]; i++);
1114 for (i-=1; i >=0 ; i--) {
1115 profile_get_string(profile, "fs_types", fs_types[i],
1120 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1124 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1129 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1130 for (cpp = fs_types; *cpp; cpp++)
1131 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1135 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1140 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1141 for (cpp = fs_types; *cpp; cpp++)
1142 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1146 extern const char *mke2fs_default_profile;
1147 static const char *default_files[] = { "<default>", 0 };
1149 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1151 * Sets the geometry of a device (stripe/stride), and returns the
1152 * device's alignment offset, if any, or a negative error.
1154 static int get_device_geometry(const char *file,
1155 struct ext2_super_block *fs_param,
1162 unsigned long min_io;
1163 unsigned long opt_io;
1164 struct stat statbuf;
1166 /* Nothing to do for a regular file */
1167 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1170 pr = blkid_new_probe_from_filename(file);
1174 tp = blkid_probe_get_topology(pr);
1178 min_io = blkid_topology_get_minimum_io_size(tp);
1179 opt_io = blkid_topology_get_optimal_io_size(tp);
1180 blocksize = EXT2_BLOCK_SIZE(fs_param);
1181 if ((min_io == 0) && (psector_size > blocksize))
1182 min_io = psector_size;
1183 if ((opt_io == 0) && min_io)
1185 if ((opt_io == 0) && (psector_size > blocksize))
1186 opt_io = psector_size;
1188 /* setting stripe/stride to blocksize is pointless */
1189 if (min_io > blocksize)
1190 fs_param->s_raid_stride = min_io / blocksize;
1191 if (opt_io > blocksize)
1192 fs_param->s_raid_stripe_width = opt_io / blocksize;
1194 rc = blkid_topology_get_alignment_offset(tp);
1196 blkid_free_probe(pr);
1201 static void PRS(int argc, char *argv[])
1207 int inode_ratio = 0;
1209 unsigned long flex_bg_size = 0;
1210 double reserved_ratio = 5.0;
1211 int lsector_size = 0, psector_size = 0;
1212 int show_version_only = 0;
1213 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1215 char * oldpath = getenv("PATH");
1216 char * extended_opts = 0;
1217 const char * fs_type = 0;
1218 const char * usage_types = 0;
1224 int s_opt = -1, r_opt = -1;
1225 char *fs_features = 0;
1228 int pathlen = sizeof(PATH_SET) + 1;
1231 pathlen += strlen(oldpath);
1232 newpath = malloc(pathlen);
1233 strcpy(newpath, PATH_SET);
1235 /* Update our PATH to include /sbin */
1237 strcat (newpath, ":");
1238 strcat (newpath, oldpath);
1242 tmp = getenv("MKE2FS_SYNC");
1244 sync_kludge = atoi(tmp);
1246 /* Determine the system page size if possible */
1248 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1249 #define _SC_PAGESIZE _SC_PAGE_SIZE
1252 sysval = sysconf(_SC_PAGESIZE);
1254 sys_page_size = sysval;
1255 #endif /* _SC_PAGESIZE */
1256 #endif /* HAVE_SYSCONF */
1258 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1260 profile_set_syntax_err_cb(syntax_err_report);
1261 retval = profile_init(config_fn, &profile);
1262 if (retval == ENOENT) {
1263 profile_init(default_files, &profile);
1264 profile_set_default(profile, mke2fs_default_profile);
1267 setbuf(stdout, NULL);
1268 setbuf(stderr, NULL);
1269 add_error_table(&et_ext2_error_table);
1270 add_error_table(&et_prof_error_table);
1271 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1272 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1279 linux_version_code = parse_version_number(ut.release);
1280 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1281 fs_param.s_rev_level = 0;
1284 if (argc && *argv) {
1285 program_name = get_progname(*argv);
1287 /* If called as mkfs.ext3, create a journal inode */
1288 if (!strcmp(program_name, "mkfs.ext3") ||
1289 !strcmp(program_name, "mke3fs"))
1293 while ((c = getopt (argc, argv,
1294 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1297 blocksize = strtol(optarg, &tmp, 0);
1298 b = (blocksize > 0) ? blocksize : -blocksize;
1299 if (b < EXT2_MIN_BLOCK_SIZE ||
1300 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1301 com_err(program_name, 0,
1302 _("invalid block size - %s"), optarg);
1305 if (blocksize > 4096)
1306 fprintf(stderr, _("Warning: blocksize %d not "
1307 "usable on most systems.\n"),
1310 fs_param.s_log_block_size =
1311 int_log2(blocksize >>
1312 EXT2_MIN_BLOCK_LOG_SIZE);
1314 case 'c': /* Check for bad blocks */
1318 size = strtoul(optarg, &tmp, 0);
1319 if (size < EXT2_MIN_BLOCK_SIZE ||
1320 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1321 com_err(program_name, 0,
1322 _("invalid fragment size - %s"),
1326 fprintf(stderr, _("Warning: fragments not supported. "
1327 "Ignoring -f option\n"));
1330 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1332 com_err(program_name, 0,
1333 _("Illegal number for blocks per group"));
1336 if ((fs_param.s_blocks_per_group % 8) != 0) {
1337 com_err(program_name, 0,
1338 _("blocks per group must be multiple of 8"));
1343 flex_bg_size = strtoul(optarg, &tmp, 0);
1345 com_err(program_name, 0,
1346 _("Illegal number for flex_bg size"));
1349 if (flex_bg_size < 1 ||
1350 (flex_bg_size & (flex_bg_size-1)) != 0) {
1351 com_err(program_name, 0,
1352 _("flex_bg size must be a power of 2"));
1357 inode_ratio = strtoul(optarg, &tmp, 0);
1358 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1359 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1361 com_err(program_name, 0,
1362 _("invalid inode ratio %s (min %d/max %d)"),
1363 optarg, EXT2_MIN_BLOCK_SIZE,
1364 EXT2_MAX_BLOCK_SIZE * 1024);
1369 parse_journal_opts(optarg);
1372 fprintf(stderr, _("Warning: -K option is deprecated and "
1373 "should not be used anymore. Use "
1374 "\'-E nodiscard\' extended option "
1383 bad_blocks_filename = malloc(strlen(optarg)+1);
1384 if (!bad_blocks_filename) {
1385 com_err(program_name, ENOMEM,
1386 _("in malloc for bad_blocks_filename"));
1389 strcpy(bad_blocks_filename, optarg);
1392 reserved_ratio = strtod(optarg, &tmp);
1393 if ( *tmp || reserved_ratio > 50 ||
1394 reserved_ratio < 0) {
1395 com_err(program_name, 0,
1396 _("invalid reserved blocks percent - %s"),
1405 creator_os = optarg;
1411 r_opt = strtoul(optarg, &tmp, 0);
1413 com_err(program_name, 0,
1414 _("bad revision level - %s"), optarg);
1417 fs_param.s_rev_level = r_opt;
1419 case 's': /* deprecated */
1420 s_opt = atoi(optarg);
1423 inode_size = strtoul(optarg, &tmp, 0);
1425 com_err(program_name, 0,
1426 _("invalid inode size - %s"), optarg);
1437 volume_label = optarg;
1443 num_inodes = strtoul(optarg, &tmp, 0);
1445 com_err(program_name, 0,
1446 _("bad num inodes - %s"), optarg);
1451 fs_features = optarg;
1455 extended_opts = optarg;
1464 usage_types = optarg;
1470 /* Print version number and exit */
1471 show_version_only++;
1477 if ((optind == argc) && !show_version_only)
1479 device_name = argv[optind++];
1481 if (!quiet || show_version_only)
1482 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1485 if (show_version_only) {
1486 fprintf(stderr, _("\tUsing %s\n"),
1487 error_message(EXT2_ET_BASE));
1492 * If there's no blocksize specified and there is a journal
1493 * device, use it to figure out the blocksize
1495 if (blocksize <= 0 && journal_device) {
1499 #ifdef CONFIG_TESTIO_DEBUG
1500 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1501 io_ptr = test_io_manager;
1502 test_io_backing_manager = unix_io_manager;
1505 io_ptr = unix_io_manager;
1506 retval = ext2fs_open(journal_device,
1507 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1510 com_err(program_name, retval,
1511 _("while trying to open journal device %s\n"),
1515 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1516 com_err(program_name, 0,
1517 _("Journal dev blocksize (%d) smaller than "
1518 "minimum blocksize %d\n"), jfs->blocksize,
1522 blocksize = jfs->blocksize;
1523 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1524 fs_param.s_log_block_size =
1525 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1529 if (blocksize > sys_page_size) {
1531 com_err(program_name, 0,
1532 _("%d-byte blocks too big for system (max %d)"),
1533 blocksize, sys_page_size);
1536 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1537 "(max %d), forced to continue\n"),
1538 blocksize, sys_page_size);
1540 if (optind < argc) {
1541 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1542 fs_param.s_log_block_size);
1543 if (!fs_param.s_blocks_count) {
1544 com_err(program_name, 0,
1545 _("invalid blocks count '%s' on device '%s'"),
1546 argv[optind - 1], device_name);
1554 check_plausibility(device_name);
1555 check_mount(device_name, force, _("filesystem"));
1557 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1559 if (noaction && fs_param.s_blocks_count) {
1560 dev_size = fs_param.s_blocks_count;
1564 retval = ext2fs_get_device_size(device_name,
1565 EXT2_BLOCK_SIZE(&fs_param),
1567 if ((retval == EFBIG) &&
1569 (fs_param.s_log_block_size == 0)) {
1570 fs_param.s_log_block_size = 2;
1576 if (retval == EFBIG) {
1577 blk64_t big_dev_size;
1579 if (blocksize < 4096) {
1580 fs_param.s_log_block_size = 2;
1583 retval = ext2fs_get_device_size2(device_name,
1584 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1586 goto get_size_failure;
1587 if (big_dev_size == (1ULL << 32)) {
1588 dev_size = (blk_t) (big_dev_size - 1);
1591 fprintf(stderr, _("%s: Size of device %s too big "
1592 "to be expressed in 32 bits\n\t"
1593 "using a blocksize of %d.\n"),
1594 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1598 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1599 com_err(program_name, retval,
1600 _("while trying to determine filesystem size"));
1604 if (!fs_param.s_blocks_count) {
1605 if (retval == EXT2_ET_UNIMPLEMENTED) {
1606 com_err(program_name, 0,
1607 _("Couldn't determine device size; you "
1608 "must specify\nthe size of the "
1612 if (dev_size == 0) {
1613 com_err(program_name, 0,
1614 _("Device size reported to be zero. "
1615 "Invalid partition specified, or\n\t"
1616 "partition table wasn't reread "
1617 "after running fdisk, due to\n\t"
1618 "a modified partition being busy "
1619 "and in use. You may need to reboot\n\t"
1620 "to re-read your partition table.\n"
1624 fs_param.s_blocks_count = dev_size;
1625 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1626 fs_param.s_blocks_count &= ~((sys_page_size /
1627 EXT2_BLOCK_SIZE(&fs_param))-1);
1630 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1631 com_err(program_name, 0,
1632 _("Filesystem larger than apparent device size."));
1636 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1638 fprintf(stderr, _("Failed to parse fs types list\n"));
1642 /* Figure out what features should be enabled */
1645 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1646 tmp = get_string_from_profile(fs_types, "base_features",
1647 "sparse_super,filetype,resize_inode,dir_index");
1648 edit_feature(tmp, &fs_param.s_feature_compat);
1651 /* And which mount options as well */
1652 tmp = get_string_from_profile(fs_types, "default_mntopts",
1654 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1658 for (cpp = fs_types; *cpp; cpp++) {
1660 profile_get_string(profile, "fs_types", *cpp,
1661 "features", "", &tmp);
1663 edit_feature(tmp, &fs_param.s_feature_compat);
1666 tmp = get_string_from_profile(fs_types, "default_features",
1669 edit_feature(fs_features ? fs_features : tmp,
1670 &fs_param.s_feature_compat);
1673 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1674 fs_types[0] = strdup("journal");
1679 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1680 print_str_list(fs_types);
1683 if (r_opt == EXT2_GOOD_OLD_REV &&
1684 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1685 fs_param.s_feature_ro_compat)) {
1686 fprintf(stderr, _("Filesystem features not supported "
1687 "with revision 0 filesystems\n"));
1692 if (r_opt == EXT2_GOOD_OLD_REV) {
1693 fprintf(stderr, _("Sparse superblocks not supported "
1694 "with revision 0 filesystems\n"));
1697 fs_param.s_feature_ro_compat |=
1698 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1699 } else if (s_opt == 0)
1700 fs_param.s_feature_ro_compat &=
1701 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1703 if (journal_size != 0) {
1704 if (r_opt == EXT2_GOOD_OLD_REV) {
1705 fprintf(stderr, _("Journals not supported "
1706 "with revision 0 filesystems\n"));
1709 fs_param.s_feature_compat |=
1710 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1713 if (fs_param.s_feature_incompat &
1714 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1716 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1717 fs_param.s_feature_compat = 0;
1718 fs_param.s_feature_ro_compat = 0;
1721 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1722 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1723 fprintf(stderr, _("The resize_inode and meta_bg features "
1724 "are not compatible.\n"
1725 "They can not be both enabled "
1726 "simultaneously.\n"));
1730 /* Set first meta blockgroup via an environment variable */
1731 /* (this is mostly for debugging purposes) */
1732 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1733 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1734 fs_param.s_first_meta_bg = atoi(tmp);
1736 /* Get the hardware sector sizes, if available */
1737 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1739 com_err(program_name, retval,
1740 _("while trying to determine hardware sector size"));
1743 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1745 com_err(program_name, retval,
1746 _("while trying to determine physical sector size"));
1750 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1751 lsector_size = atoi(tmp);
1752 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1753 psector_size = atoi(tmp);
1755 /* Older kernels may not have physical/logical distinction */
1757 psector_size = lsector_size;
1759 if (blocksize <= 0) {
1760 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1762 if (use_bsize == -1) {
1763 use_bsize = sys_page_size;
1764 if ((linux_version_code < (2*65536 + 6*256)) &&
1768 if (lsector_size && use_bsize < lsector_size)
1769 use_bsize = lsector_size;
1770 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1771 use_bsize = -blocksize;
1772 blocksize = use_bsize;
1773 fs_param.s_blocks_count /= blocksize / 1024;
1775 if (blocksize < lsector_size) { /* Impossible */
1776 com_err(program_name, EINVAL,
1777 _("while setting blocksize; too small "
1780 } else if ((blocksize < psector_size) &&
1781 (psector_size <= sys_page_size)) { /* Suboptimal */
1782 fprintf(stderr, _("Warning: specified blocksize %d is "
1783 "less than device physical sectorsize %d\n"),
1784 blocksize, psector_size);
1788 if (inode_ratio == 0) {
1789 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1791 if (inode_ratio < blocksize)
1792 inode_ratio = blocksize;
1795 fs_param.s_log_cluster_size = fs_param.s_log_block_size =
1796 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1798 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1799 retval = get_device_geometry(device_name, &fs_param, psector_size);
1802 _("warning: Unable to get device geometry for %s\n"),
1804 } else if (retval) {
1805 printf(_("%s alignment is offset by %lu bytes.\n"),
1806 device_name, retval);
1807 printf(_("This may result in very poor performance, "
1808 "(re)-partitioning suggested.\n"));
1812 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1814 lazy_itable_init = 0;
1815 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1816 lazy_itable_init = 1;
1818 lazy_itable_init = get_bool_from_profile(fs_types,
1821 discard = get_bool_from_profile(fs_types, "discard" , discard);
1823 /* Get options from profile */
1824 for (cpp = fs_types; *cpp; cpp++) {
1826 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1828 parse_extended_opts(&fs_param, tmp);
1833 parse_extended_opts(&fs_param, extended_opts);
1835 /* Since sparse_super is the default, we would only have a problem
1836 * here if it was explicitly disabled.
1838 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1839 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1840 com_err(program_name, 0,
1841 _("reserved online resize blocks not supported "
1842 "on non-sparse filesystem"));
1846 if (fs_param.s_blocks_per_group) {
1847 if (fs_param.s_blocks_per_group < 256 ||
1848 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1849 com_err(program_name, 0,
1850 _("blocks per group count out of range"));
1855 if (inode_size == 0)
1856 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1857 if (!flex_bg_size && (fs_param.s_feature_incompat &
1858 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1859 flex_bg_size = get_int_from_profile(fs_types,
1860 "flex_bg_size", 16);
1862 if (!(fs_param.s_feature_incompat &
1863 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1864 com_err(program_name, 0,
1865 _("Flex_bg feature not enabled, so "
1866 "flex_bg size may not be specified"));
1869 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1872 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1873 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1874 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1875 inode_size & (inode_size - 1)) {
1876 com_err(program_name, 0,
1877 _("invalid inode size %d (min %d/max %d)"),
1878 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1882 fs_param.s_inode_size = inode_size;
1885 /* Make sure number of inodes specified will fit in 32 bits */
1886 if (num_inodes == 0) {
1887 unsigned long long n;
1888 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1890 com_err(program_name, 0,
1891 _("too many inodes (%llu), raise inode ratio?"), n);
1894 } else if (num_inodes > ~0U) {
1895 com_err(program_name, 0,
1896 _("too many inodes (%llu), specify < 2^32 inodes"),
1901 * Calculate number of inodes based on the inode ratio
1903 fs_param.s_inodes_count = num_inodes ? num_inodes :
1904 ((__u64) fs_param.s_blocks_count * blocksize)
1907 if ((((long long)fs_param.s_inodes_count) *
1908 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1909 (((long long)fs_param.s_blocks_count) *
1910 EXT2_BLOCK_SIZE(&fs_param))) {
1911 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1912 "(%u) too big for a\n\t"
1913 "filesystem with %lu blocks, "
1914 "specify higher inode_ratio (-i)\n\t"
1915 "or lower inode count (-N).\n"),
1916 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1917 fs_param.s_inodes_count,
1918 (unsigned long) fs_param.s_blocks_count);
1923 * Calculate number of blocks to reserve
1925 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1926 fs_param.s_blocks_count / 100.0);
1929 static int should_do_undo(const char *name)
1934 struct ext2_super_block super;
1935 io_manager manager = unix_io_manager;
1936 int csum_flag, force_undo;
1938 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1939 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1940 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1941 if (!force_undo && (!csum_flag || !lazy_itable_init))
1944 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1947 * We don't handle error cases instead we
1948 * declare that the file system doesn't exist
1949 * and let the rest of mke2fs take care of
1956 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1957 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1963 #if defined(WORDS_BIGENDIAN)
1964 s_magic = ext2fs_swab16(super.s_magic);
1966 s_magic = super.s_magic;
1969 if (s_magic == EXT2_SUPER_MAGIC)
1973 io_channel_close(channel);
1980 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1982 errcode_t retval = 0;
1983 char *tdb_dir, *tdb_file;
1984 char *device_name, *tmp_name;
1987 * Configuration via a conf file would be
1990 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1992 profile_get_string(profile, "defaults",
1993 "undo_dir", 0, "/var/lib/e2fsprogs",
1996 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1997 access(tdb_dir, W_OK))
2000 tmp_name = strdup(name);
2003 com_err(program_name, ENOMEM,
2004 _("Couldn't allocate memory for tdb filename\n"));
2007 device_name = basename(tmp_name);
2008 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
2011 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
2013 if (!access(tdb_file, F_OK)) {
2014 if (unlink(tdb_file) < 0) {
2016 com_err(program_name, retval,
2017 _("while trying to delete %s"),
2024 set_undo_io_backing_manager(*io_ptr);
2025 *io_ptr = undo_io_manager;
2026 set_undo_io_backup_file(tdb_file);
2027 printf(_("Overwriting existing filesystem; this can be undone "
2028 "using the command:\n"
2029 " e2undo %s %s\n\n"), tdb_file, name);
2039 #define BLKDISCARD _IO(0x12,119)
2042 #ifndef BLKDISCARDZEROES
2043 #define BLKDISCARDZEROES _IO(0x12,124)
2047 * Return zero if the discard succeeds, and -1 if the discard fails.
2049 static int mke2fs_discard_blocks(ext2_filsys fs)
2055 __uint64_t range[2];
2057 blocks = fs->super->s_blocks_count;
2058 blocksize = EXT2_BLOCK_SIZE(fs->super);
2060 range[1] = blocks * blocksize;
2062 fd = open64(fs->device_name, O_RDWR);
2065 ret = ioctl(fd, BLKDISCARD, &range);
2067 printf(_("Calling BLKDISCARD from %llu to %llu "),
2068 (unsigned long long) range[0],
2069 (unsigned long long) range[1]);
2071 printf(_("failed.\n"));
2073 printf(_("succeeded.\n"));
2080 static int mke2fs_discard_zeroes_data(ext2_filsys fs)
2084 int discard_zeroes_data = 0;
2086 fd = open64(fs->device_name, O_RDWR);
2089 ioctl(fd, BLKDISCARDZEROES, &discard_zeroes_data);
2092 return discard_zeroes_data;
2095 #define mke2fs_discard_blocks(fs) 1
2096 #define mke2fs_discard_zeroes_data(fs) 0
2099 int main (int argc, char *argv[])
2101 errcode_t retval = 0;
2103 badblocks_list bb_list = 0;
2104 unsigned int journal_blocks;
2108 char tdb_string[40];
2110 int itable_zeroed = 0;
2113 setlocale(LC_MESSAGES, "");
2114 setlocale(LC_CTYPE, "");
2115 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2116 textdomain(NLS_CAT_NAME);
2120 #ifdef CONFIG_TESTIO_DEBUG
2121 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2122 io_ptr = test_io_manager;
2123 test_io_backing_manager = unix_io_manager;
2126 io_ptr = unix_io_manager;
2128 if (should_do_undo(device_name)) {
2129 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2135 * Initialize the superblock....
2137 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
2140 com_err(device_name, retval, _("while setting up superblock"));
2144 /* Can't undo discard ... */
2145 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2146 retval = mke2fs_discard_blocks(fs);
2148 if (!retval && mke2fs_discard_zeroes_data(fs)) {
2150 printf(_("Discard succeeded and will return 0s "
2151 " - skipping inode table wipe\n"));
2152 lazy_itable_init = 1;
2157 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2158 32768 : fs->blocksize * 8);
2159 io_channel_set_options(fs->io, tdb_string);
2161 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2162 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2164 if ((fs_param.s_feature_incompat &
2165 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2166 (fs_param.s_feature_ro_compat &
2167 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2168 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2169 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2170 fs->super->s_kbytes_written = 1;
2173 * Wipe out the old on-disk superblock
2176 zap_sector(fs, 2, 6);
2179 * Parse or generate a UUID for the filesystem
2182 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2183 com_err(device_name, 0, "could not parse UUID: %s\n",
2188 uuid_generate(fs->super->s_uuid);
2191 * Initialize the directory index variables
2193 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2195 hash_alg = e2p_string2hash(hash_alg_str);
2197 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2199 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2202 * Periodic checks can be enabled/disabled via config file.
2203 * Note we override the kernel include file's idea of what the default
2204 * check interval (never) should be. It's a good idea to check at
2205 * least *occasionally*, specially since servers will never rarely get
2206 * to reboot, since Linux is so robust these days. :-)
2208 * 180 days (six months) seems like a good value.
2210 #ifdef EXT2_DFL_CHECKINTERVAL
2211 #undef EXT2_DFL_CHECKINTERVAL
2213 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2215 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2216 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2217 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2219 * Add "jitter" to the superblock's check interval so that we
2220 * don't check all the filesystems at the same time. We use a
2221 * kludgy hack of using the UUID to derive a random jitter value
2223 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2224 val += fs->super->s_uuid[i];
2225 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2229 * Override the creator OS, if applicable
2231 if (creator_os && !set_os(fs->super, creator_os)) {
2232 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2237 * For the Hurd, we will turn off filetype since it doesn't
2240 if (fs->super->s_creator_os == EXT2_OS_HURD)
2241 fs->super->s_feature_incompat &=
2242 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2245 * Set the volume label...
2248 memset(fs->super->s_volume_name, 0,
2249 sizeof(fs->super->s_volume_name));
2250 strncpy(fs->super->s_volume_name, volume_label,
2251 sizeof(fs->super->s_volume_name));
2255 * Set the last mount directory
2258 memset(fs->super->s_last_mounted, 0,
2259 sizeof(fs->super->s_last_mounted));
2260 strncpy(fs->super->s_last_mounted, mount_dir,
2261 sizeof(fs->super->s_last_mounted));
2264 if (!quiet || noaction)
2270 if (fs->super->s_feature_incompat &
2271 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2272 create_journal_dev(fs);
2273 exit(ext2fs_close(fs) ? 1 : 0);
2276 if (bad_blocks_filename)
2277 read_bb_file(fs, &bb_list, bad_blocks_filename);
2279 test_disk(fs, &bb_list);
2281 handle_bad_blocks(fs, bb_list);
2282 fs->stride = fs_stride = fs->super->s_raid_stride;
2283 retval = ext2fs_allocate_tables(fs);
2285 com_err(program_name, retval,
2286 _("while trying to allocate filesystem tables"));
2290 fs->super->s_state |= EXT2_ERROR_FS;
2291 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2293 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2294 unsigned int rsv = 65536 / fs->blocksize;
2295 unsigned long blocks = fs->super->s_blocks_count;
2296 unsigned long start;
2299 #ifdef ZAP_BOOTBLOCK
2300 zap_sector(fs, 0, 2);
2304 * Wipe out any old MD RAID (or other) metadata at the end
2305 * of the device. This will also verify that the device is
2306 * as large as we think. Be careful with very small devices.
2308 start = (blocks & ~(rsv - 1));
2312 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2316 com_err(program_name, retval,
2317 _("while zeroing block %u at end of filesystem"),
2320 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2321 create_root_dir(fs);
2322 create_lost_and_found(fs);
2324 create_bad_block_inode(fs, bb_list);
2325 if (fs->super->s_feature_compat &
2326 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2327 retval = ext2fs_create_resize_inode(fs);
2329 com_err("ext2fs_create_resize_inode", retval,
2330 _("while reserving blocks for online resize"));
2336 if (journal_device) {
2340 check_plausibility(journal_device);
2341 check_mount(journal_device, force, _("journal"));
2343 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2344 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2345 fs->blocksize, unix_io_manager, &jfs);
2347 com_err(program_name, retval,
2348 _("while trying to open journal device %s\n"),
2353 printf(_("Adding journal to device %s: "),
2357 retval = ext2fs_add_journal_device(fs, jfs);
2359 com_err (program_name, retval,
2360 _("\n\twhile trying to add journal to device %s"),
2365 printf(_("done\n"));
2367 free(journal_device);
2368 } else if ((journal_size) ||
2369 (fs_param.s_feature_compat &
2370 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2371 journal_blocks = figure_journal_size(journal_size, fs);
2374 printf(_("Skipping journal creation in super-only mode\n"));
2375 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2379 if (!journal_blocks) {
2380 fs->super->s_feature_compat &=
2381 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2385 printf(_("Creating journal (%u blocks): "),
2389 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2392 com_err (program_name, retval,
2393 _("\n\twhile trying to create journal"));
2397 printf(_("done\n"));
2402 printf(_("Writing superblocks and "
2403 "filesystem accounting information: "));
2404 retval = ext2fs_flush(fs);
2407 _("\nWarning, had trouble writing out superblocks."));
2410 printf(_("done\n\n"));
2411 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2412 print_check_message(fs);
2414 val = ext2fs_close(fs);
2415 remove_error_table(&et_ext2_error_table);
2416 remove_error_table(&et_prof_error_table);
2417 profile_release(profile);
2418 for (i=0; fs_types[i]; i++)
2421 return (retval || val) ? 1 : 0;