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 */
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)
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 /* The kernel doesn't need to zero the itable blocks */
382 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
383 ext2fs_group_desc_csum_set(fs, i);
385 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
387 fprintf(stderr, _("\nCould not write %d "
388 "blocks in inode table starting at %u: %s\n"),
389 num, blk, error_message(retval));
393 if (sync_kludge == 1)
395 else if ((i % sync_kludge) == 0)
399 ext2fs_zero_blocks(0, 0, 0, 0, 0);
400 progress_close(&progress);
403 static void create_root_dir(ext2_filsys fs)
406 struct ext2_inode inode;
409 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
411 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
415 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
417 com_err("ext2fs_read_inode", retval,
418 _("while reading root inode"));
423 ext2fs_set_i_uid_high(inode, uid >> 16);
427 ext2fs_set_i_gid_high(inode, gid >> 16);
429 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
431 com_err("ext2fs_write_inode", retval,
432 _("while setting root inode ownership"));
438 static void create_lost_and_found(ext2_filsys fs)
440 unsigned int lpf_size = 0;
443 const char *name = "lost+found";
447 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
449 com_err("ext2fs_mkdir", retval,
450 _("while creating /lost+found"));
454 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
456 com_err("ext2_lookup", retval,
457 _("while looking up /lost+found"));
461 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
462 /* Ensure that lost+found is at least 2 blocks, so we always
463 * test large empty blocks for big-block filesystems. */
464 if ((lpf_size += fs->blocksize) >= 16*1024 &&
465 lpf_size >= 2 * fs->blocksize)
467 retval = ext2fs_expand_dir(fs, ino);
469 com_err("ext2fs_expand_dir", retval,
470 _("while expanding /lost+found"));
476 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
480 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
481 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
482 retval = ext2fs_update_bb_inode(fs, bb_list);
484 com_err("ext2fs_update_bb_inode", retval,
485 _("while setting bad block inode"));
491 static void reserve_inodes(ext2_filsys fs)
495 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
496 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
497 ext2fs_mark_ib_dirty(fs);
500 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
501 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
502 #define BSD_LABEL_OFFSET 64
504 static void zap_sector(ext2_filsys fs, int sect, int nsect)
510 buf = malloc(512*nsect);
512 printf(_("Out of memory erasing sectors %d-%d\n"),
513 sect, sect + nsect - 1);
518 /* Check for a BSD disklabel, and don't erase it if so */
519 retval = io_channel_read_blk(fs->io, 0, -512, buf);
522 _("Warning: could not read block 0: %s\n"),
523 error_message(retval));
525 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
526 if ((*magic == BSD_DISKMAGIC) ||
527 (*magic == BSD_MAGICDISK))
532 memset(buf, 0, 512*nsect);
533 io_channel_set_blksize(fs->io, 512);
534 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
535 io_channel_set_blksize(fs->io, fs->blocksize);
538 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
539 sect, error_message(retval));
542 static void create_journal_dev(ext2_filsys fs)
544 struct progress_struct progress;
548 int c, count, err_count;
550 retval = ext2fs_create_journal_superblock(fs,
551 fs->super->s_blocks_count, 0, &buf);
553 com_err("create_journal_dev", retval,
554 _("while initializing journal superblock"));
558 memset(&progress, 0, sizeof(progress));
560 progress_init(&progress, _("Zeroing journal device: "),
561 fs->super->s_blocks_count);
564 count = fs->super->s_blocks_count;
570 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
572 com_err("create_journal_dev", retval,
573 _("while zeroing journal device "
574 "(block %u, count %d)"),
580 progress_update(&progress, blk);
582 ext2fs_zero_blocks(0, 0, 0, 0, 0);
584 retval = io_channel_write_blk(fs->io,
585 fs->super->s_first_data_block+1,
588 com_err("create_journal_dev", retval,
589 _("while writing journal superblock"));
592 progress_close(&progress);
595 static void show_stats(ext2_filsys fs)
597 struct ext2_super_block *s = fs->super;
604 if (fs_param.s_blocks_count != s->s_blocks_count)
605 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
606 fs_param.s_blocks_count - s->s_blocks_count);
608 memset(buf, 0, sizeof(buf));
609 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
610 printf(_("Filesystem label=%s\n"), buf);
611 fputs(_("OS type: "), stdout);
612 os = e2p_os2string(fs->super->s_creator_os);
616 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
617 s->s_log_block_size);
618 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
620 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
621 s->s_raid_stride, s->s_raid_stripe_width);
622 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
624 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
626 100.0 * s->s_r_blocks_count / s->s_blocks_count);
627 printf(_("First data block=%u\n"), s->s_first_data_block);
628 if (s->s_reserved_gdt_blocks)
629 printf(_("Maximum filesystem blocks=%lu\n"),
630 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
631 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
632 if (fs->group_desc_count > 1)
633 printf(_("%u block groups\n"), fs->group_desc_count);
635 printf(_("%u block group\n"), fs->group_desc_count);
636 printf(_("%u blocks per group, %u fragments per group\n"),
637 s->s_blocks_per_group, s->s_frags_per_group);
638 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
640 if (fs->group_desc_count == 1) {
645 printf(_("Superblock backups stored on blocks: "));
646 group_block = s->s_first_data_block;
648 for (i = 1; i < fs->group_desc_count; i++) {
649 group_block += s->s_blocks_per_group;
650 if (!ext2fs_bg_has_super(fs, i))
654 need = int_log10(group_block) + 2;
655 if (need > col_left) {
660 printf("%u", group_block);
666 * Set the S_CREATOR_OS field. Return true if OS is known,
669 static int set_os(struct ext2_super_block *sb, char *os)
672 sb->s_creator_os = atoi (os);
673 else if (strcasecmp(os, "linux") == 0)
674 sb->s_creator_os = EXT2_OS_LINUX;
675 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
676 sb->s_creator_os = EXT2_OS_HURD;
677 else if (strcasecmp(os, "freebsd") == 0)
678 sb->s_creator_os = EXT2_OS_FREEBSD;
679 else if (strcasecmp(os, "lites") == 0)
680 sb->s_creator_os = EXT2_OS_LITES;
686 #define PATH_SET "PATH=/sbin"
688 static void parse_extended_opts(struct ext2_super_block *param,
691 char *buf, *token, *next, *p, *arg, *badopt = 0;
699 _("Couldn't allocate memory to parse options!\n"));
703 for (token = buf; token && *token; token = next) {
704 p = strchr(token, ',');
710 arg = strchr(token, '=');
715 if (strcmp(token, "stride") == 0) {
721 param->s_raid_stride = strtoul(arg, &p, 0);
722 if (*p || (param->s_raid_stride == 0)) {
724 _("Invalid stride parameter: %s\n"),
729 } else if (strcmp(token, "stripe-width") == 0 ||
730 strcmp(token, "stripe_width") == 0) {
736 param->s_raid_stripe_width = strtoul(arg, &p, 0);
737 if (*p || (param->s_raid_stripe_width == 0)) {
739 _("Invalid stripe-width parameter: %s\n"),
744 } else if (!strcmp(token, "resize")) {
745 unsigned long resize, bpg, rsv_groups;
746 unsigned long group_desc_count, desc_blocks;
747 unsigned int gdpb, blocksize;
756 resize = parse_num_blocks(arg,
757 param->s_log_block_size);
761 _("Invalid resize parameter: %s\n"),
766 if (resize <= param->s_blocks_count) {
768 _("The resize maximum must be greater "
769 "than the filesystem size.\n"));
774 blocksize = EXT2_BLOCK_SIZE(param);
775 bpg = param->s_blocks_per_group;
778 gdpb = EXT2_DESC_PER_BLOCK(param);
780 ext2fs_div_ceil(param->s_blocks_count, bpg);
781 desc_blocks = (group_desc_count +
783 rsv_groups = ext2fs_div_ceil(resize, bpg);
784 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
786 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
787 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
790 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
792 _("On-line resizing not supported with revision 0 filesystems\n"));
796 param->s_feature_compat |=
797 EXT2_FEATURE_COMPAT_RESIZE_INODE;
799 param->s_reserved_gdt_blocks = rsv_gdb;
801 } else if (!strcmp(token, "test_fs")) {
802 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
803 } else if (!strcmp(token, "lazy_itable_init")) {
805 lazy_itable_init = strtoul(arg, &p, 0);
807 lazy_itable_init = 1;
814 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
815 "Extended options are separated by commas, "
816 "and may take an argument which\n"
817 "\tis set off by an equals ('=') sign.\n\n"
818 "Valid extended options are:\n"
819 "\tstride=<RAID per-disk data chunk in blocks>\n"
820 "\tstripe-width=<RAID stride * data disks in blocks>\n"
821 "\tresize=<resize maximum size in blocks>\n"
822 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
824 badopt ? badopt : "");
828 if (param->s_raid_stride &&
829 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
830 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
831 "multiple of stride %u.\n\n"),
832 param->s_raid_stripe_width, param->s_raid_stride);
837 static __u32 ok_features[3] = {
839 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
840 EXT2_FEATURE_COMPAT_RESIZE_INODE |
841 EXT2_FEATURE_COMPAT_DIR_INDEX |
842 EXT2_FEATURE_COMPAT_EXT_ATTR,
844 EXT2_FEATURE_INCOMPAT_FILETYPE|
845 EXT3_FEATURE_INCOMPAT_EXTENTS|
846 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
847 EXT2_FEATURE_INCOMPAT_META_BG|
848 EXT4_FEATURE_INCOMPAT_FLEX_BG,
850 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
851 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
852 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
853 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
854 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
855 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
859 static void syntax_err_report(const char *filename, long err, int line_num)
862 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
863 filename, line_num, error_message(err));
867 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
869 static void edit_feature(const char *str, __u32 *compat_array)
874 if (e2p_edit_feature(str, compat_array, ok_features)) {
875 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
887 static errcode_t init_list(struct str_list *sl)
891 sl->list = malloc((sl->max+1) * sizeof(char *));
898 static errcode_t push_string(struct str_list *sl, const char *str)
902 if (sl->num >= sl->max) {
904 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
909 sl->list[sl->num] = malloc(strlen(str)+1);
910 if (sl->list[sl->num] == 0)
912 strcpy(sl->list[sl->num], str);
914 sl->list[sl->num] = 0;
918 static void print_str_list(char **list)
922 for (cpp = list; *cpp; cpp++) {
923 printf("'%s'", *cpp);
930 static char **parse_fs_type(const char *fs_type,
931 const char *usage_types,
932 struct ext2_super_block *fs_param,
935 const char *ext_type = 0;
937 char *profile_type = 0;
939 const char *size_type;
940 struct str_list list;
944 if (init_list(&list))
947 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
948 !strcasecmp(creator_os, "hurd")))
955 else if (!strcmp(program_name, "mke3fs"))
958 ext_type = strrchr(progname, '/');
964 if (!strncmp(ext_type, "mkfs.", 5)) {
966 if (ext_type[0] == 0)
973 profile_get_string(profile, "defaults", "fs_type", 0,
974 "ext2", &profile_type);
975 ext_type = profile_type;
976 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
980 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
981 !strcmp(ext_type, "ext4dev")) {
982 profile_get_string(profile, "fs_types", ext_type, "features",
985 printf(_("\nWarning! Your mke2fs.conf file does "
986 "not define the %s filesystem type.\n"),
988 printf(_("You probably need to install an updated "
989 "mke2fs.conf file.\n\n"));
994 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
995 if (fs_param->s_blocks_count < 3 * meg)
996 size_type = "floppy";
997 else if (fs_param->s_blocks_count < 512 * meg)
1000 size_type = "default";
1003 usage_types = size_type;
1005 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1011 strcpy(parse_str, usage_types);
1016 push_string(&list, ext_type);
1019 t = strchr(cp, ',');
1024 push_string(&list, cp);
1035 push_string(&list, "hurd");
1039 static char *get_string_from_profile(char **fs_types, const char *opt,
1040 const char *def_val)
1045 for (i=0; fs_types[i]; i++);
1046 for (i-=1; i >=0 ; i--) {
1047 profile_get_string(profile, "fs_types", fs_types[i],
1052 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1056 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1061 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1062 for (cpp = fs_types; *cpp; cpp++)
1063 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1067 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1072 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1073 for (cpp = fs_types; *cpp; cpp++)
1074 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1078 extern const char *mke2fs_default_profile;
1079 static const char *default_files[] = { "<default>", 0 };
1081 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1083 * Sets the geometry of a device (stripe/stride), and returns the
1084 * device's alignment offset, if any, or a negative error.
1086 static int ext2fs_get_device_geometry(const char *file,
1087 struct ext2_super_block *fs_param)
1093 unsigned long min_io;
1094 unsigned long opt_io;
1095 struct stat statbuf;
1097 /* Nothing to do for a regular file */
1098 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1101 pr = blkid_new_probe_from_filename(file);
1105 tp = blkid_probe_get_topology(pr);
1109 min_io = blkid_topology_get_minimum_io_size(tp);
1110 opt_io = blkid_topology_get_optimal_io_size(tp);
1111 blocksize = EXT2_BLOCK_SIZE(fs_param);
1113 fs_param->s_raid_stride = min_io / blocksize;
1114 fs_param->s_raid_stripe_width = opt_io / blocksize;
1116 rc = blkid_topology_get_alignment_offset(tp);
1118 blkid_free_probe(pr);
1123 static void PRS(int argc, char *argv[])
1129 int inode_ratio = 0;
1131 unsigned long flex_bg_size = 0;
1132 double reserved_ratio = 5.0;
1133 int lsector_size = 0, psector_size = 0;
1134 int show_version_only = 0;
1135 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1137 char * oldpath = getenv("PATH");
1138 char * extended_opts = 0;
1139 const char * fs_type = 0;
1140 const char * usage_types = 0;
1146 int s_opt = -1, r_opt = -1;
1147 char *fs_features = 0;
1150 int pathlen = sizeof(PATH_SET) + 1;
1153 pathlen += strlen(oldpath);
1154 newpath = malloc(pathlen);
1155 strcpy(newpath, PATH_SET);
1157 /* Update our PATH to include /sbin */
1159 strcat (newpath, ":");
1160 strcat (newpath, oldpath);
1164 tmp = getenv("MKE2FS_SYNC");
1166 sync_kludge = atoi(tmp);
1168 /* Determine the system page size if possible */
1170 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1171 #define _SC_PAGESIZE _SC_PAGE_SIZE
1174 sysval = sysconf(_SC_PAGESIZE);
1176 sys_page_size = sysval;
1177 #endif /* _SC_PAGESIZE */
1178 #endif /* HAVE_SYSCONF */
1180 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1182 profile_set_syntax_err_cb(syntax_err_report);
1183 retval = profile_init(config_fn, &profile);
1184 if (retval == ENOENT) {
1185 profile_init(default_files, &profile);
1186 profile_set_default(profile, mke2fs_default_profile);
1189 setbuf(stdout, NULL);
1190 setbuf(stderr, NULL);
1191 add_error_table(&et_ext2_error_table);
1192 add_error_table(&et_prof_error_table);
1193 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1194 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1201 linux_version_code = parse_version_number(ut.release);
1202 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1203 fs_param.s_rev_level = 0;
1206 if (argc && *argv) {
1207 program_name = get_progname(*argv);
1209 /* If called as mkfs.ext3, create a journal inode */
1210 if (!strcmp(program_name, "mkfs.ext3") ||
1211 !strcmp(program_name, "mke3fs"))
1215 while ((c = getopt (argc, argv,
1216 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1219 blocksize = strtol(optarg, &tmp, 0);
1220 b = (blocksize > 0) ? blocksize : -blocksize;
1221 if (b < EXT2_MIN_BLOCK_SIZE ||
1222 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1223 com_err(program_name, 0,
1224 _("invalid block size - %s"), optarg);
1227 if (blocksize > 4096)
1228 fprintf(stderr, _("Warning: blocksize %d not "
1229 "usable on most systems.\n"),
1232 fs_param.s_log_block_size =
1233 int_log2(blocksize >>
1234 EXT2_MIN_BLOCK_LOG_SIZE);
1236 case 'c': /* Check for bad blocks */
1240 size = strtoul(optarg, &tmp, 0);
1241 if (size < EXT2_MIN_BLOCK_SIZE ||
1242 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1243 com_err(program_name, 0,
1244 _("invalid fragment size - %s"),
1248 fs_param.s_log_frag_size =
1249 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1250 fprintf(stderr, _("Warning: fragments not supported. "
1251 "Ignoring -f option\n"));
1254 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1256 com_err(program_name, 0,
1257 _("Illegal number for blocks per group"));
1260 if ((fs_param.s_blocks_per_group % 8) != 0) {
1261 com_err(program_name, 0,
1262 _("blocks per group must be multiple of 8"));
1267 flex_bg_size = strtoul(optarg, &tmp, 0);
1269 com_err(program_name, 0,
1270 _("Illegal number for flex_bg size"));
1273 if (flex_bg_size < 1 ||
1274 (flex_bg_size & (flex_bg_size-1)) != 0) {
1275 com_err(program_name, 0,
1276 _("flex_bg size must be a power of 2"));
1281 inode_ratio = strtoul(optarg, &tmp, 0);
1282 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1283 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1285 com_err(program_name, 0,
1286 _("invalid inode ratio %s (min %d/max %d)"),
1287 optarg, EXT2_MIN_BLOCK_SIZE,
1288 EXT2_MAX_BLOCK_SIZE * 1024);
1293 parse_journal_opts(optarg);
1303 bad_blocks_filename = malloc(strlen(optarg)+1);
1304 if (!bad_blocks_filename) {
1305 com_err(program_name, ENOMEM,
1306 _("in malloc for bad_blocks_filename"));
1309 strcpy(bad_blocks_filename, optarg);
1312 reserved_ratio = strtod(optarg, &tmp);
1313 if ( *tmp || reserved_ratio > 50 ||
1314 reserved_ratio < 0) {
1315 com_err(program_name, 0,
1316 _("invalid reserved blocks percent - %s"),
1325 creator_os = optarg;
1331 r_opt = strtoul(optarg, &tmp, 0);
1333 com_err(program_name, 0,
1334 _("bad revision level - %s"), optarg);
1337 fs_param.s_rev_level = r_opt;
1339 case 's': /* deprecated */
1340 s_opt = atoi(optarg);
1343 inode_size = strtoul(optarg, &tmp, 0);
1345 com_err(program_name, 0,
1346 _("invalid inode size - %s"), optarg);
1357 volume_label = optarg;
1363 num_inodes = strtoul(optarg, &tmp, 0);
1365 com_err(program_name, 0,
1366 _("bad num inodes - %s"), optarg);
1371 fs_features = optarg;
1375 extended_opts = optarg;
1384 usage_types = optarg;
1390 /* Print version number and exit */
1391 show_version_only++;
1397 if ((optind == argc) && !show_version_only)
1399 device_name = argv[optind++];
1401 if (!quiet || show_version_only)
1402 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1405 if (show_version_only) {
1406 fprintf(stderr, _("\tUsing %s\n"),
1407 error_message(EXT2_ET_BASE));
1412 * If there's no blocksize specified and there is a journal
1413 * device, use it to figure out the blocksize
1415 if (blocksize <= 0 && journal_device) {
1419 #ifdef CONFIG_TESTIO_DEBUG
1420 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1421 io_ptr = test_io_manager;
1422 test_io_backing_manager = unix_io_manager;
1425 io_ptr = unix_io_manager;
1426 retval = ext2fs_open(journal_device,
1427 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1430 com_err(program_name, retval,
1431 _("while trying to open journal device %s\n"),
1435 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1436 com_err(program_name, 0,
1437 _("Journal dev blocksize (%d) smaller than "
1438 "minimum blocksize %d\n"), jfs->blocksize,
1442 blocksize = jfs->blocksize;
1443 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1444 fs_param.s_log_block_size =
1445 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1449 if (blocksize > sys_page_size) {
1451 com_err(program_name, 0,
1452 _("%d-byte blocks too big for system (max %d)"),
1453 blocksize, sys_page_size);
1456 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1457 "(max %d), forced to continue\n"),
1458 blocksize, sys_page_size);
1460 if (optind < argc) {
1461 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1462 fs_param.s_log_block_size);
1463 if (!fs_param.s_blocks_count) {
1464 com_err(program_name, 0, _("invalid blocks count - %s"),
1473 check_plausibility(device_name);
1474 check_mount(device_name, force, _("filesystem"));
1476 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1478 if (noaction && fs_param.s_blocks_count) {
1479 dev_size = fs_param.s_blocks_count;
1483 retval = ext2fs_get_device_size(device_name,
1484 EXT2_BLOCK_SIZE(&fs_param),
1486 if ((retval == EFBIG) &&
1488 (fs_param.s_log_block_size == 0)) {
1489 fs_param.s_log_block_size = 2;
1495 if (retval == EFBIG) {
1496 blk64_t big_dev_size;
1498 if (blocksize < 4096) {
1499 fs_param.s_log_block_size = 2;
1502 retval = ext2fs_get_device_size2(device_name,
1503 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1505 goto get_size_failure;
1506 if (big_dev_size == (1ULL << 32)) {
1507 dev_size = (blk_t) (big_dev_size - 1);
1510 fprintf(stderr, _("%s: Size of device %s too big "
1511 "to be expressed in 32 bits\n\t"
1512 "using a blocksize of %d.\n"),
1513 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1517 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1518 com_err(program_name, retval,
1519 _("while trying to determine filesystem size"));
1523 if (!fs_param.s_blocks_count) {
1524 if (retval == EXT2_ET_UNIMPLEMENTED) {
1525 com_err(program_name, 0,
1526 _("Couldn't determine device size; you "
1527 "must specify\nthe size of the "
1531 if (dev_size == 0) {
1532 com_err(program_name, 0,
1533 _("Device size reported to be zero. "
1534 "Invalid partition specified, or\n\t"
1535 "partition table wasn't reread "
1536 "after running fdisk, due to\n\t"
1537 "a modified partition being busy "
1538 "and in use. You may need to reboot\n\t"
1539 "to re-read your partition table.\n"
1543 fs_param.s_blocks_count = dev_size;
1544 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1545 fs_param.s_blocks_count &= ~((sys_page_size /
1546 EXT2_BLOCK_SIZE(&fs_param))-1);
1549 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1550 com_err(program_name, 0,
1551 _("Filesystem larger than apparent device size."));
1555 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1557 fprintf(stderr, _("Failed to parse fs types list\n"));
1561 /* Figure out what features should be enabled */
1564 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1565 tmp = get_string_from_profile(fs_types, "base_features",
1566 "sparse_super,filetype,resize_inode,dir_index");
1567 edit_feature(tmp, &fs_param.s_feature_compat);
1570 for (cpp = fs_types; *cpp; cpp++) {
1572 profile_get_string(profile, "fs_types", *cpp,
1573 "features", "", &tmp);
1575 edit_feature(tmp, &fs_param.s_feature_compat);
1578 tmp = get_string_from_profile(fs_types, "default_features",
1581 edit_feature(fs_features ? fs_features : tmp,
1582 &fs_param.s_feature_compat);
1585 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1586 fs_types[0] = strdup("journal");
1591 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1592 print_str_list(fs_types);
1595 if (r_opt == EXT2_GOOD_OLD_REV &&
1596 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1597 fs_param.s_feature_ro_compat)) {
1598 fprintf(stderr, _("Filesystem features not supported "
1599 "with revision 0 filesystems\n"));
1604 if (r_opt == EXT2_GOOD_OLD_REV) {
1605 fprintf(stderr, _("Sparse superblocks not supported "
1606 "with revision 0 filesystems\n"));
1609 fs_param.s_feature_ro_compat |=
1610 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1611 } else if (s_opt == 0)
1612 fs_param.s_feature_ro_compat &=
1613 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1615 if (journal_size != 0) {
1616 if (r_opt == EXT2_GOOD_OLD_REV) {
1617 fprintf(stderr, _("Journals not supported "
1618 "with revision 0 filesystems\n"));
1621 fs_param.s_feature_compat |=
1622 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1625 if (fs_param.s_feature_incompat &
1626 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1628 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1629 fs_param.s_feature_compat = 0;
1630 fs_param.s_feature_ro_compat = 0;
1633 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1634 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1635 fprintf(stderr, _("The resize_inode and meta_bg features "
1636 "are not compatible.\n"
1637 "They can not be both enabled "
1638 "simultaneously.\n"));
1642 /* Set first meta blockgroup via an environment variable */
1643 /* (this is mostly for debugging purposes) */
1644 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1645 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1646 fs_param.s_first_meta_bg = atoi(tmp);
1648 /* Get the hardware sector sizes, if available */
1649 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1651 com_err(program_name, retval,
1652 _("while trying to determine hardware sector size"));
1655 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1657 com_err(program_name, retval,
1658 _("while trying to determine physical sector size"));
1661 /* Older kernels may not have physical/logical distinction */
1663 psector_size = lsector_size;
1665 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1666 psector_size = atoi(tmp);
1668 if (blocksize <= 0) {
1669 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1671 if (use_bsize == -1) {
1672 use_bsize = sys_page_size;
1673 if ((linux_version_code < (2*65536 + 6*256)) &&
1677 if (psector_size && use_bsize < psector_size)
1678 use_bsize = psector_size;
1679 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1680 use_bsize = -blocksize;
1681 blocksize = use_bsize;
1682 fs_param.s_blocks_count /= blocksize / 1024;
1684 if (blocksize < lsector_size || /* Impossible */
1685 (!force && (blocksize < psector_size))) { /* Suboptimal */
1686 com_err(program_name, EINVAL,
1687 _("while setting blocksize; too small "
1690 } else if (blocksize < psector_size) {
1691 fprintf(stderr, _("Warning: specified blocksize %d is "
1692 "less than device physical sectorsize %d, "
1693 "forced to continue\n"), blocksize,
1698 if (inode_ratio == 0) {
1699 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1701 if (inode_ratio < blocksize)
1702 inode_ratio = blocksize;
1705 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1706 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1708 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1709 retval = ext2fs_get_device_geometry(device_name, &fs_param);
1712 _("warning: Unable to get device geometry for %s\n"),
1714 } else if (retval) {
1715 printf(_("%s alignment is offset by %lu bytes.\n"),
1716 device_name, retval);
1717 printf(_("This may result in very poor performance, "
1718 "(re)-partitioning suggested.\n"));
1722 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1724 lazy_itable_init = get_bool_from_profile(fs_types,
1725 "lazy_itable_init", 0);
1727 /* Get options from profile */
1728 for (cpp = fs_types; *cpp; cpp++) {
1730 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1732 parse_extended_opts(&fs_param, tmp);
1737 parse_extended_opts(&fs_param, extended_opts);
1739 /* Since sparse_super is the default, we would only have a problem
1740 * here if it was explicitly disabled.
1742 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1743 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1744 com_err(program_name, 0,
1745 _("reserved online resize blocks not supported "
1746 "on non-sparse filesystem"));
1750 if (fs_param.s_blocks_per_group) {
1751 if (fs_param.s_blocks_per_group < 256 ||
1752 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1753 com_err(program_name, 0,
1754 _("blocks per group count out of range"));
1759 if (inode_size == 0)
1760 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1761 if (!flex_bg_size && (fs_param.s_feature_incompat &
1762 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1763 flex_bg_size = get_int_from_profile(fs_types,
1764 "flex_bg_size", 16);
1766 if (!(fs_param.s_feature_incompat &
1767 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1768 com_err(program_name, 0,
1769 _("Flex_bg feature not enabled, so "
1770 "flex_bg size may not be specified"));
1773 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1776 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1777 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1778 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1779 inode_size & (inode_size - 1)) {
1780 com_err(program_name, 0,
1781 _("invalid inode size %d (min %d/max %d)"),
1782 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1786 fs_param.s_inode_size = inode_size;
1789 /* Make sure number of inodes specified will fit in 32 bits */
1790 if (num_inodes == 0) {
1791 unsigned long long n;
1792 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1794 com_err(program_name, 0,
1795 _("too many inodes (%llu), raise inode ratio?"), n);
1798 } else if (num_inodes > ~0U) {
1799 com_err(program_name, 0,
1800 _("too many inodes (%llu), specify < 2^32 inodes"),
1805 * Calculate number of inodes based on the inode ratio
1807 fs_param.s_inodes_count = num_inodes ? num_inodes :
1808 ((__u64) fs_param.s_blocks_count * blocksize)
1811 if ((((long long)fs_param.s_inodes_count) *
1812 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1813 (((long long)fs_param.s_blocks_count) *
1814 EXT2_BLOCK_SIZE(&fs_param))) {
1815 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1816 "(%u) too big for a\n\t"
1817 "filesystem with %lu blocks, "
1818 "specify higher inode_ratio (-i)\n\t"
1819 "or lower inode count (-N).\n"),
1820 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1821 fs_param.s_inodes_count,
1822 (unsigned long) fs_param.s_blocks_count);
1827 * Calculate number of blocks to reserve
1829 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1830 fs_param.s_blocks_count / 100.0);
1833 static int should_do_undo(const char *name)
1838 struct ext2_super_block super;
1839 io_manager manager = unix_io_manager;
1840 int csum_flag, force_undo;
1842 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1843 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1844 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1845 if (!force_undo && (!csum_flag || !lazy_itable_init))
1848 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1851 * We don't handle error cases instead we
1852 * declare that the file system doesn't exist
1853 * and let the rest of mke2fs take care of
1860 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1861 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1867 #if defined(WORDS_BIGENDIAN)
1868 s_magic = ext2fs_swab16(super.s_magic);
1870 s_magic = super.s_magic;
1873 if (s_magic == EXT2_SUPER_MAGIC)
1877 io_channel_close(channel);
1884 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1886 errcode_t retval = 0;
1887 char *tdb_dir, *tdb_file;
1888 char *device_name, *tmp_name;
1891 * Configuration via a conf file would be
1894 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1896 profile_get_string(profile, "defaults",
1897 "undo_dir", 0, "/var/lib/e2fsprogs",
1900 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1901 access(tdb_dir, W_OK))
1904 tmp_name = strdup(name);
1907 com_err(program_name, ENOMEM,
1908 _("Couldn't allocate memory for tdb filename\n"));
1911 device_name = basename(tmp_name);
1912 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1915 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1917 if (!access(tdb_file, F_OK)) {
1918 if (unlink(tdb_file) < 0) {
1920 com_err(program_name, retval,
1921 _("while trying to delete %s"),
1928 set_undo_io_backing_manager(*io_ptr);
1929 *io_ptr = undo_io_manager;
1930 set_undo_io_backup_file(tdb_file);
1931 printf(_("Overwriting existing filesystem; this can be undone "
1932 "using the command:\n"
1933 " e2undo %s %s\n\n"), tdb_file, name);
1943 #define BLKDISCARD _IO(0x12,119)
1946 static void mke2fs_discard_blocks(ext2_filsys fs)
1952 __uint64_t range[2];
1954 blocks = fs->super->s_blocks_count;
1955 blocksize = EXT2_BLOCK_SIZE(fs->super);
1957 range[1] = blocks * blocksize;
1959 fd = open64(fs->device_name, O_RDWR);
1962 * We don't care about whether the ioctl succeeds; it's only an
1963 * optmization for SSDs or sparse storage.
1966 ret = ioctl(fd, BLKDISCARD, &range);
1968 printf(_("Calling BLKDISCARD from %llu to %llu "),
1969 (unsigned long long) range[0],
1970 (unsigned long long) range[1]);
1972 printf(_("failed.\n"));
1974 printf(_("succeeded.\n"));
1980 #define mke2fs_discard_blocks(fs)
1983 int main (int argc, char *argv[])
1985 errcode_t retval = 0;
1987 badblocks_list bb_list = 0;
1988 unsigned int journal_blocks;
1992 char tdb_string[40];
1996 setlocale(LC_MESSAGES, "");
1997 setlocale(LC_CTYPE, "");
1998 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1999 textdomain(NLS_CAT_NAME);
2003 #ifdef CONFIG_TESTIO_DEBUG
2004 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2005 io_ptr = test_io_manager;
2006 test_io_backing_manager = unix_io_manager;
2009 io_ptr = unix_io_manager;
2011 if (should_do_undo(device_name)) {
2012 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2018 * Initialize the superblock....
2020 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
2023 com_err(device_name, retval, _("while setting up superblock"));
2027 /* Can't undo discard ... */
2028 if (discard && (io_ptr != undo_io_manager))
2029 mke2fs_discard_blocks(fs);
2031 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2032 32768 : fs->blocksize * 8);
2033 io_channel_set_options(fs->io, tdb_string);
2035 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2036 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2038 if ((fs_param.s_feature_incompat &
2039 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2040 (fs_param.s_feature_ro_compat &
2041 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2042 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2043 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2044 fs->super->s_kbytes_written = 1;
2047 * Wipe out the old on-disk superblock
2050 zap_sector(fs, 2, 6);
2053 * Parse or generate a UUID for the filesystem
2056 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2057 com_err(device_name, 0, "could not parse UUID: %s\n",
2062 uuid_generate(fs->super->s_uuid);
2065 * Initialize the directory index variables
2067 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2069 hash_alg = e2p_string2hash(hash_alg_str);
2070 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2072 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2075 * Add "jitter" to the superblock's check interval so that we
2076 * don't check all the filesystems at the same time. We use a
2077 * kludgy hack of using the UUID to derive a random jitter value.
2079 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2080 val += fs->super->s_uuid[i];
2081 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2084 * Override the creator OS, if applicable
2086 if (creator_os && !set_os(fs->super, creator_os)) {
2087 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2092 * For the Hurd, we will turn off filetype since it doesn't
2095 if (fs->super->s_creator_os == EXT2_OS_HURD)
2096 fs->super->s_feature_incompat &=
2097 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2100 * Set the volume label...
2103 memset(fs->super->s_volume_name, 0,
2104 sizeof(fs->super->s_volume_name));
2105 strncpy(fs->super->s_volume_name, volume_label,
2106 sizeof(fs->super->s_volume_name));
2110 * Set the last mount directory
2113 memset(fs->super->s_last_mounted, 0,
2114 sizeof(fs->super->s_last_mounted));
2115 strncpy(fs->super->s_last_mounted, mount_dir,
2116 sizeof(fs->super->s_last_mounted));
2119 if (!quiet || noaction)
2125 if (fs->super->s_feature_incompat &
2126 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2127 create_journal_dev(fs);
2128 exit(ext2fs_close(fs) ? 1 : 0);
2131 if (bad_blocks_filename)
2132 read_bb_file(fs, &bb_list, bad_blocks_filename);
2134 test_disk(fs, &bb_list);
2136 handle_bad_blocks(fs, bb_list);
2137 fs->stride = fs_stride = fs->super->s_raid_stride;
2138 retval = ext2fs_allocate_tables(fs);
2140 com_err(program_name, retval,
2141 _("while trying to allocate filesystem tables"));
2145 fs->super->s_state |= EXT2_ERROR_FS;
2146 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2148 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2149 unsigned int rsv = 65536 / fs->blocksize;
2150 unsigned long blocks = fs->super->s_blocks_count;
2151 unsigned long start;
2154 #ifdef ZAP_BOOTBLOCK
2155 zap_sector(fs, 0, 2);
2159 * Wipe out any old MD RAID (or other) metadata at the end
2160 * of the device. This will also verify that the device is
2161 * as large as we think. Be careful with very small devices.
2163 start = (blocks & ~(rsv - 1));
2167 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2171 com_err(program_name, retval,
2172 _("while zeroing block %u at end of filesystem"),
2175 write_inode_tables(fs, lazy_itable_init);
2176 create_root_dir(fs);
2177 create_lost_and_found(fs);
2179 create_bad_block_inode(fs, bb_list);
2180 if (fs->super->s_feature_compat &
2181 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2182 retval = ext2fs_create_resize_inode(fs);
2184 com_err("ext2fs_create_resize_inode", retval,
2185 _("while reserving blocks for online resize"));
2191 if (journal_device) {
2195 check_plausibility(journal_device);
2196 check_mount(journal_device, force, _("journal"));
2198 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2199 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2200 fs->blocksize, unix_io_manager, &jfs);
2202 com_err(program_name, retval,
2203 _("while trying to open journal device %s\n"),
2208 printf(_("Adding journal to device %s: "),
2212 retval = ext2fs_add_journal_device(fs, jfs);
2214 com_err (program_name, retval,
2215 _("\n\twhile trying to add journal to device %s"),
2220 printf(_("done\n"));
2222 free(journal_device);
2223 } else if ((journal_size) ||
2224 (fs_param.s_feature_compat &
2225 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2226 journal_blocks = figure_journal_size(journal_size, fs);
2229 printf(_("Skipping journal creation in super-only mode\n"));
2230 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2234 if (!journal_blocks) {
2235 fs->super->s_feature_compat &=
2236 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2240 printf(_("Creating journal (%u blocks): "),
2244 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2247 com_err (program_name, retval,
2248 _("\n\twhile trying to create journal"));
2252 printf(_("done\n"));
2257 printf(_("Writing superblocks and "
2258 "filesystem accounting information: "));
2259 retval = ext2fs_flush(fs);
2262 _("\nWarning, had trouble writing out superblocks."));
2265 printf(_("done\n\n"));
2266 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2267 print_check_message(fs);
2269 val = ext2fs_close(fs);
2270 remove_error_table(&et_ext2_error_table);
2271 remove_error_table(&et_prof_error_table);
2272 profile_release(profile);
2273 return (retval || val) ? 1 : 0;