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 #ifndef ROOT_SYSCONFDIR
73 #define ROOT_SYSCONFDIR "/etc"
76 extern int isatty(int);
77 extern FILE *fpopen(const char *cmd, const char *mode);
79 const char * program_name = "mke2fs";
80 const char * device_name /* = NULL */;
82 /* Command line options */
93 char *bad_blocks_filename;
96 struct ext2_super_block fs_param;
101 char *journal_device;
102 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
107 int sys_page_size = 4096;
108 int linux_version_code = 0;
110 static void usage(void)
112 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
113 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
114 "[-J journal-options]\n"
115 "\t[-G meta group size] [-N number-of-inodes]\n"
116 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
117 "\t[-g blocks-per-group] [-L volume-label] "
118 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
119 "[-r fs-revision] [-E extended-option[,...]]\n"
120 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
125 static int int_log2(int arg)
137 static int int_log10(unsigned int arg)
146 static int parse_version_number(const char *s)
148 int major, minor, rev;
154 major = strtol(cp, &endptr, 10);
155 if (cp == endptr || *endptr != '.')
158 minor = strtol(cp, &endptr, 10);
159 if (cp == endptr || *endptr != '.')
162 rev = strtol(cp, &endptr, 10);
165 return ((((major * 256) + minor) * 256) + rev);
169 * Helper function for read_bb_file and test_disk
171 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
173 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
178 * Reads the bad blocks list from a file
180 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
181 const char *bad_blocks_file)
186 f = fopen(bad_blocks_file, "r");
188 com_err("read_bad_blocks_file", errno,
189 _("while trying to open %s"), bad_blocks_file);
192 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
195 com_err("ext2fs_read_bb_FILE", retval,
196 _("while reading in list of bad blocks from file"));
203 * Runs the badblocks program to test the disk
205 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
211 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
212 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
213 fs->device_name, fs->super->s_blocks_count-1);
215 printf(_("Running command: %s\n"), buf);
218 com_err("popen", errno,
219 _("while trying to run '%s'"), buf);
222 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
225 com_err("ext2fs_read_bb_FILE", retval,
226 _("while processing list of bad blocks from program"));
232 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
236 unsigned must_be_good;
238 badblocks_iterate bb_iter;
248 * The primary superblock and group descriptors *must* be
249 * good; if not, abort.
251 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
252 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
253 if (ext2fs_badblocks_list_test(bb_list, i)) {
254 fprintf(stderr, _("Block %d in primary "
255 "superblock/group descriptor area bad.\n"), i);
256 fprintf(stderr, _("Blocks %u through %u must be good "
257 "in order to build a filesystem.\n"),
258 fs->super->s_first_data_block, must_be_good);
259 fputs(_("Aborting....\n"), stderr);
265 * See if any of the bad blocks are showing up in the backup
266 * superblocks and/or group descriptors. If so, issue a
267 * warning and adjust the block counts appropriately.
269 group_block = fs->super->s_first_data_block +
270 fs->super->s_blocks_per_group;
272 for (i = 1; i < fs->group_desc_count; i++) {
274 for (j=0; j < fs->desc_blocks+1; j++) {
275 if (ext2fs_badblocks_list_test(bb_list,
279 _("Warning: the backup superblock/group descriptors at block %u contain\n"
283 group = ext2fs_group_of_blk(fs, group_block+j);
284 fs->group_desc[group].bg_free_blocks_count++;
285 ext2fs_group_desc_csum_set(fs, group);
286 fs->super->s_free_blocks_count++;
289 group_block += fs->super->s_blocks_per_group;
293 * Mark all the bad blocks as used...
295 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
297 com_err("ext2fs_badblocks_list_iterate_begin", retval,
298 _("while marking bad blocks as used"));
301 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
302 ext2fs_mark_block_bitmap(fs->block_map, blk);
303 ext2fs_badblocks_list_iterate_end(bb_iter);
307 * These functions implement a generalized progress meter.
309 struct progress_struct {
316 static void progress_init(struct progress_struct *progress,
317 const char *label,__u32 max)
321 memset(progress, 0, sizeof(struct progress_struct));
326 * Figure out how many digits we need
329 sprintf(progress->format, "%%%dd/%%%dld", i, i);
330 memset(progress->backup, '\b', sizeof(progress->backup)-1);
331 progress->backup[sizeof(progress->backup)-1] = 0;
332 if ((2*i)+1 < (int) sizeof(progress->backup))
333 progress->backup[(2*i)+1] = 0;
336 progress->skip_progress = 0;
337 if (getenv("MKE2FS_SKIP_PROGRESS"))
338 progress->skip_progress++;
340 fputs(label, stdout);
344 static void progress_update(struct progress_struct *progress, __u32 val)
346 if ((progress->format[0] == 0) || progress->skip_progress)
348 printf(progress->format, val, progress->max);
349 fputs(progress->backup, stdout);
352 static void progress_close(struct progress_struct *progress)
354 if (progress->format[0] == 0)
356 fputs(_("done \n"), stdout);
359 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
365 struct progress_struct progress;
368 memset(&progress, 0, sizeof(progress));
370 progress_init(&progress, _("Writing inode tables: "),
371 fs->group_desc_count);
373 for (i = 0; i < fs->group_desc_count; i++) {
374 progress_update(&progress, i);
376 blk = fs->group_desc[i].bg_inode_table;
377 num = fs->inode_blocks_per_group;
380 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
381 num = ((((fs->super->s_inodes_per_group -
382 fs->group_desc[i].bg_itable_unused) *
383 EXT2_INODE_SIZE(fs->super)) +
384 EXT2_BLOCK_SIZE(fs->super) - 1) /
385 EXT2_BLOCK_SIZE(fs->super));
387 /* The kernel doesn't need to zero the itable blocks */
388 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
389 ext2fs_group_desc_csum_set(fs, i);
391 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
393 fprintf(stderr, _("\nCould not write %d "
394 "blocks in inode table starting at %u: %s\n"),
395 num, blk, error_message(retval));
399 if (sync_kludge == 1)
401 else if ((i % sync_kludge) == 0)
405 ext2fs_zero_blocks(0, 0, 0, 0, 0);
406 progress_close(&progress);
409 static void create_root_dir(ext2_filsys fs)
412 struct ext2_inode inode;
415 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
417 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
421 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
423 com_err("ext2fs_read_inode", retval,
424 _("while reading root inode"));
429 ext2fs_set_i_uid_high(inode, uid >> 16);
433 ext2fs_set_i_gid_high(inode, gid >> 16);
435 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
437 com_err("ext2fs_write_inode", retval,
438 _("while setting root inode ownership"));
444 static void create_lost_and_found(ext2_filsys fs)
446 unsigned int lpf_size = 0;
449 const char *name = "lost+found";
453 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
455 com_err("ext2fs_mkdir", retval,
456 _("while creating /lost+found"));
460 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
462 com_err("ext2_lookup", retval,
463 _("while looking up /lost+found"));
467 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
468 /* Ensure that lost+found is at least 2 blocks, so we always
469 * test large empty blocks for big-block filesystems. */
470 if ((lpf_size += fs->blocksize) >= 16*1024 &&
471 lpf_size >= 2 * fs->blocksize)
473 retval = ext2fs_expand_dir(fs, ino);
475 com_err("ext2fs_expand_dir", retval,
476 _("while expanding /lost+found"));
482 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
486 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
487 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
488 retval = ext2fs_update_bb_inode(fs, bb_list);
490 com_err("ext2fs_update_bb_inode", retval,
491 _("while setting bad block inode"));
497 static void reserve_inodes(ext2_filsys fs)
501 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
502 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
503 ext2fs_mark_ib_dirty(fs);
506 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
507 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
508 #define BSD_LABEL_OFFSET 64
510 static void zap_sector(ext2_filsys fs, int sect, int nsect)
516 buf = malloc(512*nsect);
518 printf(_("Out of memory erasing sectors %d-%d\n"),
519 sect, sect + nsect - 1);
524 /* Check for a BSD disklabel, and don't erase it if so */
525 retval = io_channel_read_blk(fs->io, 0, -512, buf);
528 _("Warning: could not read block 0: %s\n"),
529 error_message(retval));
531 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
532 if ((*magic == BSD_DISKMAGIC) ||
533 (*magic == BSD_MAGICDISK))
538 memset(buf, 0, 512*nsect);
539 io_channel_set_blksize(fs->io, 512);
540 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
541 io_channel_set_blksize(fs->io, fs->blocksize);
544 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
545 sect, error_message(retval));
548 static void create_journal_dev(ext2_filsys fs)
550 struct progress_struct progress;
554 int c, count, err_count;
556 retval = ext2fs_create_journal_superblock(fs,
557 fs->super->s_blocks_count, 0, &buf);
559 com_err("create_journal_dev", retval,
560 _("while initializing journal superblock"));
564 memset(&progress, 0, sizeof(progress));
566 progress_init(&progress, _("Zeroing journal device: "),
567 fs->super->s_blocks_count);
570 count = fs->super->s_blocks_count;
576 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
578 com_err("create_journal_dev", retval,
579 _("while zeroing journal device "
580 "(block %u, count %d)"),
586 progress_update(&progress, blk);
588 ext2fs_zero_blocks(0, 0, 0, 0, 0);
590 retval = io_channel_write_blk(fs->io,
591 fs->super->s_first_data_block+1,
594 com_err("create_journal_dev", retval,
595 _("while writing journal superblock"));
598 progress_close(&progress);
601 static void show_stats(ext2_filsys fs)
603 struct ext2_super_block *s = fs->super;
610 if (fs_param.s_blocks_count != s->s_blocks_count)
611 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
612 fs_param.s_blocks_count - s->s_blocks_count);
614 memset(buf, 0, sizeof(buf));
615 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
616 printf(_("Filesystem label=%s\n"), buf);
617 fputs(_("OS type: "), stdout);
618 os = e2p_os2string(fs->super->s_creator_os);
622 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
623 s->s_log_block_size);
624 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
626 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
627 s->s_raid_stride, s->s_raid_stripe_width);
628 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
630 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
632 100.0 * s->s_r_blocks_count / s->s_blocks_count);
633 printf(_("First data block=%u\n"), s->s_first_data_block);
634 if (s->s_reserved_gdt_blocks)
635 printf(_("Maximum filesystem blocks=%lu\n"),
636 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
637 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
638 if (fs->group_desc_count > 1)
639 printf(_("%u block groups\n"), fs->group_desc_count);
641 printf(_("%u block group\n"), fs->group_desc_count);
642 printf(_("%u blocks per group, %u fragments per group\n"),
643 s->s_blocks_per_group, s->s_frags_per_group);
644 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
646 if (fs->group_desc_count == 1) {
651 printf(_("Superblock backups stored on blocks: "));
652 group_block = s->s_first_data_block;
654 for (i = 1; i < fs->group_desc_count; i++) {
655 group_block += s->s_blocks_per_group;
656 if (!ext2fs_bg_has_super(fs, i))
660 need = int_log10(group_block) + 2;
661 if (need > col_left) {
666 printf("%u", group_block);
672 * Set the S_CREATOR_OS field. Return true if OS is known,
675 static int set_os(struct ext2_super_block *sb, char *os)
678 sb->s_creator_os = atoi (os);
679 else if (strcasecmp(os, "linux") == 0)
680 sb->s_creator_os = EXT2_OS_LINUX;
681 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
682 sb->s_creator_os = EXT2_OS_HURD;
683 else if (strcasecmp(os, "freebsd") == 0)
684 sb->s_creator_os = EXT2_OS_FREEBSD;
685 else if (strcasecmp(os, "lites") == 0)
686 sb->s_creator_os = EXT2_OS_LITES;
692 #define PATH_SET "PATH=/sbin"
694 static void parse_extended_opts(struct ext2_super_block *param,
697 char *buf, *token, *next, *p, *arg, *badopt = 0;
705 _("Couldn't allocate memory to parse options!\n"));
709 for (token = buf; token && *token; token = next) {
710 p = strchr(token, ',');
716 arg = strchr(token, '=');
721 if (strcmp(token, "stride") == 0) {
727 param->s_raid_stride = strtoul(arg, &p, 0);
728 if (*p || (param->s_raid_stride == 0)) {
730 _("Invalid stride parameter: %s\n"),
735 } else if (strcmp(token, "stripe-width") == 0 ||
736 strcmp(token, "stripe_width") == 0) {
742 param->s_raid_stripe_width = strtoul(arg, &p, 0);
743 if (*p || (param->s_raid_stripe_width == 0)) {
745 _("Invalid stripe-width parameter: %s\n"),
750 } else if (!strcmp(token, "resize")) {
751 unsigned long resize, bpg, rsv_groups;
752 unsigned long group_desc_count, desc_blocks;
753 unsigned int gdpb, blocksize;
762 resize = parse_num_blocks(arg,
763 param->s_log_block_size);
767 _("Invalid resize parameter: %s\n"),
772 if (resize <= param->s_blocks_count) {
774 _("The resize maximum must be greater "
775 "than the filesystem size.\n"));
780 blocksize = EXT2_BLOCK_SIZE(param);
781 bpg = param->s_blocks_per_group;
784 gdpb = EXT2_DESC_PER_BLOCK(param);
786 ext2fs_div_ceil(param->s_blocks_count, bpg);
787 desc_blocks = (group_desc_count +
789 rsv_groups = ext2fs_div_ceil(resize, bpg);
790 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
792 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
793 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
796 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
798 _("On-line resizing not supported with revision 0 filesystems\n"));
802 param->s_feature_compat |=
803 EXT2_FEATURE_COMPAT_RESIZE_INODE;
805 param->s_reserved_gdt_blocks = rsv_gdb;
807 } else if (!strcmp(token, "test_fs")) {
808 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
809 } else if (!strcmp(token, "lazy_itable_init")) {
811 lazy_itable_init = strtoul(arg, &p, 0);
813 lazy_itable_init = 1;
820 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
821 "Extended options are separated by commas, "
822 "and may take an argument which\n"
823 "\tis set off by an equals ('=') sign.\n\n"
824 "Valid extended options are:\n"
825 "\tstride=<RAID per-disk data chunk in blocks>\n"
826 "\tstripe-width=<RAID stride * data disks in blocks>\n"
827 "\tresize=<resize maximum size in blocks>\n"
828 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
830 badopt ? badopt : "");
834 if (param->s_raid_stride &&
835 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
836 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
837 "multiple of stride %u.\n\n"),
838 param->s_raid_stripe_width, param->s_raid_stride);
843 static __u32 ok_features[3] = {
845 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
846 EXT2_FEATURE_COMPAT_RESIZE_INODE |
847 EXT2_FEATURE_COMPAT_DIR_INDEX |
848 EXT2_FEATURE_COMPAT_EXT_ATTR,
850 EXT2_FEATURE_INCOMPAT_FILETYPE|
851 EXT3_FEATURE_INCOMPAT_EXTENTS|
852 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
853 EXT2_FEATURE_INCOMPAT_META_BG|
854 EXT4_FEATURE_INCOMPAT_FLEX_BG,
856 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
857 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
858 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
859 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
860 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
861 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
865 static void syntax_err_report(const char *filename, long err, int line_num)
868 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
869 filename, line_num, error_message(err));
873 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
875 static void edit_feature(const char *str, __u32 *compat_array)
880 if (e2p_edit_feature(str, compat_array, ok_features)) {
881 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
893 static errcode_t init_list(struct str_list *sl)
897 sl->list = malloc((sl->max+1) * sizeof(char *));
904 static errcode_t push_string(struct str_list *sl, const char *str)
908 if (sl->num >= sl->max) {
910 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
915 sl->list[sl->num] = malloc(strlen(str)+1);
916 if (sl->list[sl->num] == 0)
918 strcpy(sl->list[sl->num], str);
920 sl->list[sl->num] = 0;
924 static void print_str_list(char **list)
928 for (cpp = list; *cpp; cpp++) {
929 printf("'%s'", *cpp);
936 static char **parse_fs_type(const char *fs_type,
937 const char *usage_types,
938 struct ext2_super_block *fs_param,
941 const char *ext_type = 0;
943 char *profile_type = 0;
945 const char *size_type;
946 struct str_list list;
950 if (init_list(&list))
953 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
954 !strcasecmp(creator_os, "hurd")))
961 else if (!strcmp(program_name, "mke3fs"))
964 ext_type = strrchr(progname, '/');
970 if (!strncmp(ext_type, "mkfs.", 5)) {
972 if (ext_type[0] == 0)
979 profile_get_string(profile, "defaults", "fs_type", 0,
980 "ext2", &profile_type);
981 ext_type = profile_type;
982 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
986 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
987 !strcmp(ext_type, "ext4dev")) {
988 profile_get_string(profile, "fs_types", ext_type, "features",
991 printf(_("\nWarning! Your mke2fs.conf file does "
992 "not define the %s filesystem type.\n"),
994 printf(_("You probably need to install an updated "
995 "mke2fs.conf file.\n\n"));
1000 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1001 if (fs_param->s_blocks_count < 3 * meg)
1002 size_type = "floppy";
1003 else if (fs_param->s_blocks_count < 512 * meg)
1004 size_type = "small";
1006 size_type = "default";
1009 usage_types = size_type;
1011 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1017 strcpy(parse_str, usage_types);
1022 push_string(&list, ext_type);
1025 t = strchr(cp, ',');
1030 push_string(&list, cp);
1041 push_string(&list, "hurd");
1045 static char *get_string_from_profile(char **fs_types, const char *opt,
1046 const char *def_val)
1051 for (i=0; fs_types[i]; i++);
1052 for (i-=1; i >=0 ; i--) {
1053 profile_get_string(profile, "fs_types", fs_types[i],
1058 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1062 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1067 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1068 for (cpp = fs_types; *cpp; cpp++)
1069 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1073 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1078 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1079 for (cpp = fs_types; *cpp; cpp++)
1080 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1084 extern const char *mke2fs_default_profile;
1085 static const char *default_files[] = { "<default>", 0 };
1087 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1089 * Sets the geometry of a device (stripe/stride), and returns the
1090 * device's alignment offset, if any, or a negative error.
1092 static int ext2fs_get_device_geometry(const char *file,
1093 struct ext2_super_block *fs_param)
1099 unsigned long min_io;
1100 unsigned long opt_io;
1101 struct stat statbuf;
1103 /* Nothing to do for a regular file */
1104 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1107 pr = blkid_new_probe_from_filename(file);
1111 tp = blkid_probe_get_topology(pr);
1115 min_io = blkid_topology_get_minimum_io_size(tp);
1116 opt_io = blkid_topology_get_optimal_io_size(tp);
1117 blocksize = EXT2_BLOCK_SIZE(fs_param);
1119 fs_param->s_raid_stride = min_io / blocksize;
1120 fs_param->s_raid_stripe_width = opt_io / blocksize;
1122 rc = blkid_topology_get_alignment_offset(tp);
1124 blkid_free_probe(pr);
1129 static void PRS(int argc, char *argv[])
1135 int inode_ratio = 0;
1137 unsigned long flex_bg_size = 0;
1138 double reserved_ratio = 5.0;
1139 int sector_size = 0;
1140 int show_version_only = 0;
1141 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1143 char * oldpath = getenv("PATH");
1144 char * extended_opts = 0;
1145 const char * fs_type = 0;
1146 const char * usage_types = 0;
1152 int s_opt = -1, r_opt = -1;
1153 char *fs_features = 0;
1156 int pathlen = sizeof(PATH_SET) + 1;
1159 pathlen += strlen(oldpath);
1160 newpath = malloc(pathlen);
1161 strcpy(newpath, PATH_SET);
1163 /* Update our PATH to include /sbin */
1165 strcat (newpath, ":");
1166 strcat (newpath, oldpath);
1170 tmp = getenv("MKE2FS_SYNC");
1172 sync_kludge = atoi(tmp);
1174 /* Determine the system page size if possible */
1176 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1177 #define _SC_PAGESIZE _SC_PAGE_SIZE
1180 sysval = sysconf(_SC_PAGESIZE);
1182 sys_page_size = sysval;
1183 #endif /* _SC_PAGESIZE */
1184 #endif /* HAVE_SYSCONF */
1186 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1188 profile_set_syntax_err_cb(syntax_err_report);
1189 retval = profile_init(config_fn, &profile);
1190 if (retval == ENOENT) {
1191 profile_init(default_files, &profile);
1192 profile_set_default(profile, mke2fs_default_profile);
1195 setbuf(stdout, NULL);
1196 setbuf(stderr, NULL);
1197 add_error_table(&et_ext2_error_table);
1198 add_error_table(&et_prof_error_table);
1199 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1200 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1207 linux_version_code = parse_version_number(ut.release);
1208 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1209 fs_param.s_rev_level = 0;
1212 if (argc && *argv) {
1213 program_name = get_progname(*argv);
1215 /* If called as mkfs.ext3, create a journal inode */
1216 if (!strcmp(program_name, "mkfs.ext3") ||
1217 !strcmp(program_name, "mke3fs"))
1221 while ((c = getopt (argc, argv,
1222 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1225 blocksize = strtol(optarg, &tmp, 0);
1226 b = (blocksize > 0) ? blocksize : -blocksize;
1227 if (b < EXT2_MIN_BLOCK_SIZE ||
1228 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1229 com_err(program_name, 0,
1230 _("invalid block size - %s"), optarg);
1233 if (blocksize > 4096)
1234 fprintf(stderr, _("Warning: blocksize %d not "
1235 "usable on most systems.\n"),
1238 fs_param.s_log_block_size =
1239 int_log2(blocksize >>
1240 EXT2_MIN_BLOCK_LOG_SIZE);
1242 case 'c': /* Check for bad blocks */
1246 com_err(program_name, 0, _("check for bad blocks disabled"));
1251 size = strtoul(optarg, &tmp, 0);
1252 if (size < EXT2_MIN_BLOCK_SIZE ||
1253 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1254 com_err(program_name, 0,
1255 _("invalid fragment size - %s"),
1259 fs_param.s_log_frag_size =
1260 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1261 fprintf(stderr, _("Warning: fragments not supported. "
1262 "Ignoring -f option\n"));
1265 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1267 com_err(program_name, 0,
1268 _("Illegal number for blocks per group"));
1271 if ((fs_param.s_blocks_per_group % 8) != 0) {
1272 com_err(program_name, 0,
1273 _("blocks per group must be multiple of 8"));
1278 flex_bg_size = strtoul(optarg, &tmp, 0);
1280 com_err(program_name, 0,
1281 _("Illegal number for flex_bg size"));
1284 if (flex_bg_size < 2 ||
1285 (flex_bg_size & (flex_bg_size-1)) != 0) {
1286 com_err(program_name, 0,
1287 _("flex_bg size must be a power of 2"));
1292 inode_ratio = strtoul(optarg, &tmp, 0);
1293 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1294 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1296 com_err(program_name, 0,
1297 _("invalid inode ratio %s (min %d/max %d)"),
1298 optarg, EXT2_MIN_BLOCK_SIZE,
1299 EXT2_MAX_BLOCK_SIZE);
1304 parse_journal_opts(optarg);
1314 bad_blocks_filename = malloc(strlen(optarg)+1);
1315 if (!bad_blocks_filename) {
1316 com_err(program_name, ENOMEM,
1317 _("in malloc for bad_blocks_filename"));
1320 strcpy(bad_blocks_filename, optarg);
1323 reserved_ratio = strtod(optarg, &tmp);
1324 if ( *tmp || reserved_ratio > 50 ||
1325 reserved_ratio < 0) {
1326 com_err(program_name, 0,
1327 _("invalid reserved blocks percent - %s"),
1336 creator_os = optarg;
1342 r_opt = strtoul(optarg, &tmp, 0);
1344 com_err(program_name, 0,
1345 _("bad revision level - %s"), optarg);
1348 fs_param.s_rev_level = r_opt;
1350 case 's': /* deprecated */
1351 s_opt = atoi(optarg);
1354 inode_size = strtoul(optarg, &tmp, 0);
1356 com_err(program_name, 0,
1357 _("invalid inode size - %s"), optarg);
1368 volume_label = optarg;
1374 num_inodes = strtoul(optarg, &tmp, 0);
1376 com_err(program_name, 0,
1377 _("bad num inodes - %s"), optarg);
1382 fs_features = optarg;
1386 extended_opts = optarg;
1395 usage_types = optarg;
1401 /* Print version number and exit */
1402 show_version_only++;
1408 if ((optind == argc) && !show_version_only)
1410 device_name = argv[optind++];
1412 if (!quiet || show_version_only)
1413 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1416 if (show_version_only) {
1417 fprintf(stderr, _("\tUsing %s\n"),
1418 error_message(EXT2_ET_BASE));
1423 * If there's no blocksize specified and there is a journal
1424 * device, use it to figure out the blocksize
1426 if (blocksize <= 0 && journal_device) {
1430 #ifdef CONFIG_TESTIO_DEBUG
1431 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1432 io_ptr = test_io_manager;
1433 test_io_backing_manager = unix_io_manager;
1436 io_ptr = unix_io_manager;
1437 retval = ext2fs_open(journal_device,
1438 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1441 com_err(program_name, retval,
1442 _("while trying to open journal device %s\n"),
1446 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1447 com_err(program_name, 0,
1448 _("Journal dev blocksize (%d) smaller than "
1449 "minimum blocksize %d\n"), jfs->blocksize,
1453 blocksize = jfs->blocksize;
1454 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1455 fs_param.s_log_block_size =
1456 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1460 if (blocksize > sys_page_size) {
1462 com_err(program_name, 0,
1463 _("%d-byte blocks too big for system (max %d)"),
1464 blocksize, sys_page_size);
1467 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1468 "(max %d), forced to continue\n"),
1469 blocksize, sys_page_size);
1471 if (optind < argc) {
1472 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1473 fs_param.s_log_block_size);
1474 if (!fs_param.s_blocks_count) {
1475 com_err(program_name, 0, _("invalid blocks count - %s"),
1484 check_plausibility(device_name);
1485 check_mount(device_name, force, _("filesystem"));
1487 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1489 if (noaction && fs_param.s_blocks_count) {
1490 dev_size = fs_param.s_blocks_count;
1494 retval = ext2fs_get_device_size(device_name,
1495 EXT2_BLOCK_SIZE(&fs_param),
1497 if ((retval == EFBIG) &&
1499 (fs_param.s_log_block_size == 0)) {
1500 fs_param.s_log_block_size = 2;
1506 if (retval == EFBIG) {
1507 blk64_t big_dev_size;
1509 if (blocksize < 4096) {
1510 fs_param.s_log_block_size = 2;
1513 retval = ext2fs_get_device_size2(device_name,
1514 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1516 goto get_size_failure;
1517 if (big_dev_size == (1ULL << 32)) {
1518 dev_size = (blk_t) (big_dev_size - 1);
1521 fprintf(stderr, _("%s: Size of device %s too big "
1522 "to be expressed in 32 bits\n\t"
1523 "using a blocksize of %d.\n"),
1524 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1528 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1529 com_err(program_name, retval,
1530 _("while trying to determine filesystem size"));
1534 if (!fs_param.s_blocks_count) {
1535 if (retval == EXT2_ET_UNIMPLEMENTED) {
1536 com_err(program_name, 0,
1537 _("Couldn't determine device size; you "
1538 "must specify\nthe size of the "
1542 if (dev_size == 0) {
1543 com_err(program_name, 0,
1544 _("Device size reported to be zero. "
1545 "Invalid partition specified, or\n\t"
1546 "partition table wasn't reread "
1547 "after running fdisk, due to\n\t"
1548 "a modified partition being busy "
1549 "and in use. You may need to reboot\n\t"
1550 "to re-read your partition table.\n"
1554 fs_param.s_blocks_count = dev_size;
1555 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1556 fs_param.s_blocks_count &= ~((sys_page_size /
1557 EXT2_BLOCK_SIZE(&fs_param))-1);
1560 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1561 com_err(program_name, 0,
1562 _("Filesystem larger than apparent device size."));
1566 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1568 fprintf(stderr, _("Failed to parse fs types list\n"));
1572 /* Figure out what features should be enabled */
1575 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1576 tmp = get_string_from_profile(fs_types, "base_features",
1577 "sparse_super,filetype,resize_inode,dir_index");
1578 edit_feature(tmp, &fs_param.s_feature_compat);
1581 for (cpp = fs_types; *cpp; cpp++) {
1583 profile_get_string(profile, "fs_types", *cpp,
1584 "features", "", &tmp);
1586 edit_feature(tmp, &fs_param.s_feature_compat);
1589 tmp = get_string_from_profile(fs_types, "default_features",
1592 edit_feature(fs_features ? fs_features : tmp,
1593 &fs_param.s_feature_compat);
1596 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1597 fs_types[0] = strdup("journal");
1602 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1603 print_str_list(fs_types);
1606 if (r_opt == EXT2_GOOD_OLD_REV &&
1607 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1608 fs_param.s_feature_ro_compat)) {
1609 fprintf(stderr, _("Filesystem features not supported "
1610 "with revision 0 filesystems\n"));
1615 if (r_opt == EXT2_GOOD_OLD_REV) {
1616 fprintf(stderr, _("Sparse superblocks not supported "
1617 "with revision 0 filesystems\n"));
1620 fs_param.s_feature_ro_compat |=
1621 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1622 } else if (s_opt == 0)
1623 fs_param.s_feature_ro_compat &=
1624 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1626 if (journal_size != 0) {
1627 if (r_opt == EXT2_GOOD_OLD_REV) {
1628 fprintf(stderr, _("Journals not supported "
1629 "with revision 0 filesystems\n"));
1632 fs_param.s_feature_compat |=
1633 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1636 if (fs_param.s_feature_incompat &
1637 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1639 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1640 fs_param.s_feature_compat = 0;
1641 fs_param.s_feature_ro_compat = 0;
1644 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1645 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1646 fprintf(stderr, _("The resize_inode and meta_bg features "
1647 "are not compatible.\n"
1648 "They can not be both enabled "
1649 "simultaneously.\n"));
1653 /* Set first meta blockgroup via an environment variable */
1654 /* (this is mostly for debugging purposes) */
1655 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1656 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1657 fs_param.s_first_meta_bg = atoi(tmp);
1659 /* Get the hardware sector size, if available */
1660 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1662 com_err(program_name, retval,
1663 _("while trying to determine hardware sector size"));
1667 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1668 sector_size = atoi(tmp);
1670 if (blocksize <= 0) {
1671 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1673 if (use_bsize == -1) {
1674 use_bsize = sys_page_size;
1675 if ((linux_version_code < (2*65536 + 6*256)) &&
1679 if (sector_size && use_bsize < sector_size)
1680 use_bsize = sector_size;
1681 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1682 use_bsize = -blocksize;
1683 blocksize = use_bsize;
1684 fs_param.s_blocks_count /= blocksize / 1024;
1687 if (inode_ratio == 0) {
1688 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1690 if (inode_ratio < blocksize)
1691 inode_ratio = blocksize;
1694 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1695 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1697 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1698 retval = ext2fs_get_device_geometry(device_name, &fs_param);
1701 _("warning: Unable to get device geometry for %s\n"),
1703 } else if (retval) {
1704 printf(_("%s alignment is offset by %lu bytes.\n"),
1705 device_name, retval);
1706 printf(_("This may result in very poor performance, "
1707 "(re)-partitioning suggested.\n"));
1711 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1713 lazy_itable_init = get_bool_from_profile(fs_types,
1714 "lazy_itable_init", 0);
1716 /* Get options from profile */
1717 for (cpp = fs_types; *cpp; cpp++) {
1719 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1721 parse_extended_opts(&fs_param, tmp);
1726 parse_extended_opts(&fs_param, extended_opts);
1728 /* Since sparse_super is the default, we would only have a problem
1729 * here if it was explicitly disabled.
1731 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1732 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1733 com_err(program_name, 0,
1734 _("reserved online resize blocks not supported "
1735 "on non-sparse filesystem"));
1739 if (fs_param.s_blocks_per_group) {
1740 if (fs_param.s_blocks_per_group < 256 ||
1741 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1742 com_err(program_name, 0,
1743 _("blocks per group count out of range"));
1748 if (inode_size == 0)
1749 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1750 if (!flex_bg_size && (fs_param.s_feature_incompat &
1751 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1752 flex_bg_size = get_int_from_profile(fs_types,
1753 "flex_bg_size", 16);
1755 if (!(fs_param.s_feature_incompat &
1756 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1757 com_err(program_name, 0,
1758 _("Flex_bg feature not enabled, so "
1759 "flex_bg size may not be specified"));
1762 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1765 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1766 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1767 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1768 inode_size & (inode_size - 1)) {
1769 com_err(program_name, 0,
1770 _("invalid inode size %d (min %d/max %d)"),
1771 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1775 fs_param.s_inode_size = inode_size;
1778 /* Make sure number of inodes specified will fit in 32 bits */
1779 if (num_inodes == 0) {
1780 unsigned long long n;
1781 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1783 com_err(program_name, 0,
1784 _("too many inodes (%llu), raise inode ratio?"), n);
1787 } else if (num_inodes > ~0U) {
1788 com_err(program_name, 0,
1789 _("too many inodes (%llu), specify < 2^32 inodes"),
1794 * Calculate number of inodes based on the inode ratio
1796 fs_param.s_inodes_count = num_inodes ? num_inodes :
1797 ((__u64) fs_param.s_blocks_count * blocksize)
1800 if ((((long long)fs_param.s_inodes_count) *
1801 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1802 (((long long)fs_param.s_blocks_count) *
1803 EXT2_BLOCK_SIZE(&fs_param))) {
1804 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1805 "(%u) too big for a\n\t"
1806 "filesystem with %lu blocks, "
1807 "specify higher inode_ratio (-i)\n\t"
1808 "or lower inode count (-N).\n"),
1809 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1810 fs_param.s_inodes_count,
1811 (unsigned long) fs_param.s_blocks_count);
1816 * Calculate number of blocks to reserve
1818 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1819 fs_param.s_blocks_count / 100.0);
1822 static int should_do_undo(const char *name)
1827 struct ext2_super_block super;
1828 io_manager manager = unix_io_manager;
1829 int csum_flag, force_undo;
1831 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1832 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1833 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1834 if (!force_undo && (!csum_flag || !lazy_itable_init))
1837 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1840 * We don't handle error cases instead we
1841 * declare that the file system doesn't exist
1842 * and let the rest of mke2fs take care of
1849 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1850 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1856 #if defined(WORDS_BIGENDIAN)
1857 s_magic = ext2fs_swab16(super.s_magic);
1859 s_magic = super.s_magic;
1862 if (s_magic == EXT2_SUPER_MAGIC)
1866 io_channel_close(channel);
1873 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1875 errcode_t retval = 0;
1876 char *tdb_dir, *tdb_file;
1877 char *device_name, *tmp_name;
1880 * Configuration via a conf file would be
1883 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1885 profile_get_string(profile, "defaults",
1886 "undo_dir", 0, "/var/lib/e2fsprogs",
1889 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1890 access(tdb_dir, W_OK))
1893 tmp_name = strdup(name);
1896 com_err(program_name, ENOMEM,
1897 _("Couldn't allocate memory for tdb filename\n"));
1900 device_name = basename(tmp_name);
1901 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1904 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1906 if (!access(tdb_file, F_OK)) {
1907 if (unlink(tdb_file) < 0) {
1909 com_err(program_name, retval,
1910 _("while trying to delete %s"),
1917 set_undo_io_backing_manager(*io_ptr);
1918 *io_ptr = undo_io_manager;
1919 set_undo_io_backup_file(tdb_file);
1920 printf(_("Overwriting existing filesystem; this can be undone "
1921 "using the command:\n"
1922 " e2undo %s %s\n\n"), tdb_file, name);
1932 #define BLKDISCARD _IO(0x12,119)
1935 static void mke2fs_discard_blocks(ext2_filsys fs)
1941 __uint64_t range[2];
1943 blocks = fs->super->s_blocks_count;
1944 blocksize = EXT2_BLOCK_SIZE(fs->super);
1946 range[1] = blocks * blocksize;
1949 fd = open64(fs->device_name, O_RDWR);
1951 fd = open(fs->device_name, O_RDWR);
1955 * We don't care about whether the ioctl succeeds; it's only an
1956 * optmization for SSDs or sparse storage.
1959 ret = ioctl(fd, BLKDISCARD, &range);
1961 printf(_("Calling BLKDISCARD from %llu to %llu "),
1962 range[0], range[1]);
1964 printf(_("failed.\n"));
1966 printf(_("succeeded.\n"));
1972 #define mke2fs_discard_blocks(fs)
1975 int main (int argc, char *argv[])
1977 errcode_t retval = 0;
1979 badblocks_list bb_list = 0;
1980 unsigned int journal_blocks;
1984 char tdb_string[40];
1988 setlocale(LC_MESSAGES, "");
1989 setlocale(LC_CTYPE, "");
1990 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1991 textdomain(NLS_CAT_NAME);
1995 #ifdef CONFIG_TESTIO_DEBUG
1996 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1997 io_ptr = test_io_manager;
1998 test_io_backing_manager = unix_io_manager;
2001 io_ptr = unix_io_manager;
2003 if (should_do_undo(device_name)) {
2004 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2010 * Initialize the superblock....
2012 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
2015 com_err(device_name, retval, _("while setting up superblock"));
2019 /* Can't undo discard ... */
2020 if (discard && (io_ptr != undo_io_manager))
2021 mke2fs_discard_blocks(fs);
2023 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2024 32768 : fs->blocksize * 8);
2025 io_channel_set_options(fs->io, tdb_string);
2027 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2028 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2030 if ((fs_param.s_feature_incompat &
2031 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2032 (fs_param.s_feature_ro_compat &
2033 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2034 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2035 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2036 fs->super->s_kbytes_written = 1;
2039 * Wipe out the old on-disk superblock
2042 zap_sector(fs, 2, 6);
2045 * Parse or generate a UUID for the filesystem
2048 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2049 com_err(device_name, 0, "could not parse UUID: %s\n",
2054 uuid_generate(fs->super->s_uuid);
2057 * Initialize the directory index variables
2059 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2061 hash_alg = e2p_string2hash(hash_alg_str);
2062 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2064 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2067 * Add "jitter" to the superblock's check interval so that we
2068 * don't check all the filesystems at the same time. We use a
2069 * kludgy hack of using the UUID to derive a random jitter value.
2071 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2072 val += fs->super->s_uuid[i];
2073 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2076 * Override the creator OS, if applicable
2078 if (creator_os && !set_os(fs->super, creator_os)) {
2079 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2084 * For the Hurd, we will turn off filetype since it doesn't
2087 if (fs->super->s_creator_os == EXT2_OS_HURD)
2088 fs->super->s_feature_incompat &=
2089 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2092 * Set the volume label...
2095 memset(fs->super->s_volume_name, 0,
2096 sizeof(fs->super->s_volume_name));
2097 strncpy(fs->super->s_volume_name, volume_label,
2098 sizeof(fs->super->s_volume_name));
2102 * Set the last mount directory
2105 memset(fs->super->s_last_mounted, 0,
2106 sizeof(fs->super->s_last_mounted));
2107 strncpy(fs->super->s_last_mounted, mount_dir,
2108 sizeof(fs->super->s_last_mounted));
2111 if (!quiet || noaction)
2117 if (fs->super->s_feature_incompat &
2118 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2119 create_journal_dev(fs);
2120 exit(ext2fs_close(fs) ? 1 : 0);
2123 if (bad_blocks_filename)
2124 read_bb_file(fs, &bb_list, bad_blocks_filename);
2128 test_disk(fs, &bb_list);
2131 handle_bad_blocks(fs, bb_list);
2132 fs->stride = fs_stride = fs->super->s_raid_stride;
2133 retval = ext2fs_allocate_tables(fs);
2135 com_err(program_name, retval,
2136 _("while trying to allocate filesystem tables"));
2140 fs->super->s_state |= EXT2_ERROR_FS;
2141 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2143 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2144 unsigned int rsv = 65536 / fs->blocksize;
2145 unsigned long blocks = fs->super->s_blocks_count;
2146 unsigned long start;
2149 #ifdef ZAP_BOOTBLOCK
2150 zap_sector(fs, 0, 2);
2154 * Wipe out any old MD RAID (or other) metadata at the end
2155 * of the device. This will also verify that the device is
2156 * as large as we think. Be careful with very small devices.
2158 start = (blocks & ~(rsv - 1));
2162 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2166 com_err(program_name, retval,
2167 _("while zeroing block %u at end of filesystem"),
2170 write_inode_tables(fs, lazy_itable_init);
2171 create_root_dir(fs);
2172 create_lost_and_found(fs);
2174 create_bad_block_inode(fs, bb_list);
2175 if (fs->super->s_feature_compat &
2176 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2177 retval = ext2fs_create_resize_inode(fs);
2179 com_err("ext2fs_create_resize_inode", retval,
2180 _("while reserving blocks for online resize"));
2186 if (journal_device) {
2190 check_plausibility(journal_device);
2191 check_mount(journal_device, force, _("journal"));
2193 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2194 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2195 fs->blocksize, unix_io_manager, &jfs);
2197 com_err(program_name, retval,
2198 _("while trying to open journal device %s\n"),
2203 printf(_("Adding journal to device %s: "),
2207 retval = ext2fs_add_journal_device(fs, jfs);
2209 com_err (program_name, retval,
2210 _("\n\twhile trying to add journal to device %s"),
2215 printf(_("done\n"));
2217 free(journal_device);
2218 } else if ((journal_size) ||
2219 (fs_param.s_feature_compat &
2220 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2221 journal_blocks = figure_journal_size(journal_size, fs);
2224 printf(_("Skipping journal creation in super-only mode\n"));
2225 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2229 if (!journal_blocks) {
2230 fs->super->s_feature_compat &=
2231 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2235 printf(_("Creating journal (%u blocks): "),
2239 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2242 com_err (program_name, retval,
2243 _("\n\twhile trying to create journal"));
2247 printf(_("done\n"));
2252 printf(_("Writing superblocks and "
2253 "filesystem accounting information: "));
2254 retval = ext2fs_flush(fs);
2257 _("\nWarning, had trouble writing out superblocks."));
2260 printf(_("done\n\n"));
2261 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2262 print_check_message(fs);
2264 val = ext2fs_close(fs);
2265 remove_error_table(&et_ext2_error_table);
2266 remove_error_table(&et_prof_error_table);
2267 profile_release(profile);
2268 return (retval || val) ? 1 : 0;