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 */
27 #include <sys/utsname.h>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
52 #include "ext2fs/ext2_fs.h"
53 #include "et/com_err.h"
54 #include "uuid/uuid.h"
56 #include "ext2fs/ext2fs.h"
60 #include "../version.h"
61 #include "nls-enable.h"
63 #define STRIDE_LENGTH 8
69 extern int isatty(int);
70 extern FILE *fpopen(const char *cmd, const char *mode);
72 const char * program_name = "mke2fs";
73 const char * device_name /* = NULL */;
75 /* Command line options */
85 char *bad_blocks_filename;
88 struct ext2_super_block fs_param;
93 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
98 int sys_page_size = 4096;
99 int linux_version_code = 0;
101 static void usage(void)
103 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
104 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
105 "[-J journal-options]\n"
106 "\t[-G meta group size] [-N number-of-inodes]\n"
107 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
108 "\t[-g blocks-per-group] [-L volume-label] "
109 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
110 "[-r fs-revision] [-E extended-option[,...]]\n"
111 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
116 static int int_log2(int arg)
128 static int int_log10(unsigned int arg)
137 static int parse_version_number(const char *s)
139 int major, minor, rev;
145 major = strtol(cp, &endptr, 10);
146 if (cp == endptr || *endptr != '.')
149 minor = strtol(cp, &endptr, 10);
150 if (cp == endptr || *endptr != '.')
153 rev = strtol(cp, &endptr, 10);
156 return ((((major * 256) + minor) * 256) + rev);
160 * Helper function for read_bb_file and test_disk
162 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
164 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
169 * Reads the bad blocks list from a file
171 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
172 const char *bad_blocks_file)
177 f = fopen(bad_blocks_file, "r");
179 com_err("read_bad_blocks_file", errno,
180 _("while trying to open %s"), bad_blocks_file);
183 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
186 com_err("ext2fs_read_bb_FILE", retval,
187 _("while reading in list of bad blocks from file"));
193 * Runs the badblocks program to test the disk
195 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
201 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
202 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
203 fs->device_name, fs->super->s_blocks_count-1);
205 printf(_("Running command: %s\n"), buf);
208 com_err("popen", errno,
209 _("while trying to run '%s'"), buf);
212 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
215 com_err("ext2fs_read_bb_FILE", retval,
216 _("while processing list of bad blocks from program"));
221 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
225 unsigned must_be_good;
227 badblocks_iterate bb_iter;
237 * The primary superblock and group descriptors *must* be
238 * good; if not, abort.
240 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
241 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
242 if (ext2fs_badblocks_list_test(bb_list, i)) {
243 fprintf(stderr, _("Block %d in primary "
244 "superblock/group descriptor area bad.\n"), i);
245 fprintf(stderr, _("Blocks %u through %u must be good "
246 "in order to build a filesystem.\n"),
247 fs->super->s_first_data_block, must_be_good);
248 fputs(_("Aborting....\n"), stderr);
254 * See if any of the bad blocks are showing up in the backup
255 * superblocks and/or group descriptors. If so, issue a
256 * warning and adjust the block counts appropriately.
258 group_block = fs->super->s_first_data_block +
259 fs->super->s_blocks_per_group;
261 for (i = 1; i < fs->group_desc_count; i++) {
263 for (j=0; j < fs->desc_blocks+1; j++) {
264 if (ext2fs_badblocks_list_test(bb_list,
268 _("Warning: the backup superblock/group descriptors at block %u contain\n"
272 group = ext2fs_group_of_blk(fs, group_block+j);
273 fs->group_desc[group].bg_free_blocks_count++;
274 ext2fs_group_desc_csum_set(fs, group);
275 fs->super->s_free_blocks_count++;
278 group_block += fs->super->s_blocks_per_group;
282 * Mark all the bad blocks as used...
284 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
286 com_err("ext2fs_badblocks_list_iterate_begin", retval,
287 _("while marking bad blocks as used"));
290 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
291 ext2fs_mark_block_bitmap(fs->block_map, blk);
292 ext2fs_badblocks_list_iterate_end(bb_iter);
296 * These functions implement a generalized progress meter.
298 struct progress_struct {
305 static void progress_init(struct progress_struct *progress,
306 const char *label,__u32 max)
310 memset(progress, 0, sizeof(struct progress_struct));
315 * Figure out how many digits we need
318 sprintf(progress->format, "%%%dd/%%%dld", i, i);
319 memset(progress->backup, '\b', sizeof(progress->backup)-1);
320 progress->backup[sizeof(progress->backup)-1] = 0;
321 if ((2*i)+1 < (int) sizeof(progress->backup))
322 progress->backup[(2*i)+1] = 0;
325 progress->skip_progress = 0;
326 if (getenv("MKE2FS_SKIP_PROGRESS"))
327 progress->skip_progress++;
329 fputs(label, stdout);
333 static void progress_update(struct progress_struct *progress, __u32 val)
335 if ((progress->format[0] == 0) || progress->skip_progress)
337 printf(progress->format, val, progress->max);
338 fputs(progress->backup, stdout);
341 static void progress_close(struct progress_struct *progress)
343 if (progress->format[0] == 0)
345 fputs(_("done \n"), stdout);
348 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
354 struct progress_struct progress;
357 memset(&progress, 0, sizeof(progress));
359 progress_init(&progress, _("Writing inode tables: "),
360 fs->group_desc_count);
362 for (i = 0; i < fs->group_desc_count; i++) {
363 progress_update(&progress, i);
365 blk = fs->group_desc[i].bg_inode_table;
366 num = fs->inode_blocks_per_group;
369 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
370 num = ((((fs->super->s_inodes_per_group -
371 fs->group_desc[i].bg_itable_unused) *
372 EXT2_INODE_SIZE(fs->super)) +
373 EXT2_BLOCK_SIZE(fs->super) - 1) /
374 EXT2_BLOCK_SIZE(fs->super));
376 /* The kernel doesn't need to zero the itable blocks */
377 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
378 ext2fs_group_desc_csum_set(fs, i);
380 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
382 fprintf(stderr, _("\nCould not write %d "
383 "blocks in inode table starting at %u: %s\n"),
384 num, blk, error_message(retval));
388 if (sync_kludge == 1)
390 else if ((i % sync_kludge) == 0)
394 ext2fs_zero_blocks(0, 0, 0, 0, 0);
395 progress_close(&progress);
398 static void create_root_dir(ext2_filsys fs)
401 struct ext2_inode inode;
404 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
406 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
410 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
412 com_err("ext2fs_read_inode", retval,
413 _("while reading root inode"));
418 ext2fs_set_i_uid_high(inode, uid >> 16);
422 ext2fs_set_i_gid_high(inode, gid >> 16);
424 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
426 com_err("ext2fs_write_inode", retval,
427 _("while setting root inode ownership"));
433 static void create_lost_and_found(ext2_filsys fs)
435 unsigned int lpf_size = 0;
438 const char *name = "lost+found";
442 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
444 com_err("ext2fs_mkdir", retval,
445 _("while creating /lost+found"));
449 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
451 com_err("ext2_lookup", retval,
452 _("while looking up /lost+found"));
456 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
457 /* Ensure that lost+found is at least 2 blocks, so we always
458 * test large empty blocks for big-block filesystems. */
459 if ((lpf_size += fs->blocksize) >= 16*1024 &&
460 lpf_size >= 2 * fs->blocksize)
462 retval = ext2fs_expand_dir(fs, ino);
464 com_err("ext2fs_expand_dir", retval,
465 _("while expanding /lost+found"));
471 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
475 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
476 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
477 retval = ext2fs_update_bb_inode(fs, bb_list);
479 com_err("ext2fs_update_bb_inode", retval,
480 _("while setting bad block inode"));
486 static void reserve_inodes(ext2_filsys fs)
490 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
491 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
492 ext2fs_mark_ib_dirty(fs);
495 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
496 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
497 #define BSD_LABEL_OFFSET 64
499 static void zap_sector(ext2_filsys fs, int sect, int nsect)
505 buf = malloc(512*nsect);
507 printf(_("Out of memory erasing sectors %d-%d\n"),
508 sect, sect + nsect - 1);
513 /* Check for a BSD disklabel, and don't erase it if so */
514 retval = io_channel_read_blk(fs->io, 0, -512, buf);
517 _("Warning: could not read block 0: %s\n"),
518 error_message(retval));
520 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
521 if ((*magic == BSD_DISKMAGIC) ||
522 (*magic == BSD_MAGICDISK))
527 memset(buf, 0, 512*nsect);
528 io_channel_set_blksize(fs->io, 512);
529 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
530 io_channel_set_blksize(fs->io, fs->blocksize);
533 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
534 sect, error_message(retval));
537 static void create_journal_dev(ext2_filsys fs)
539 struct progress_struct progress;
543 int c, count, err_count;
545 retval = ext2fs_create_journal_superblock(fs,
546 fs->super->s_blocks_count, 0, &buf);
548 com_err("create_journal_dev", retval,
549 _("while initializing journal superblock"));
553 memset(&progress, 0, sizeof(progress));
555 progress_init(&progress, _("Zeroing journal device: "),
556 fs->super->s_blocks_count);
559 count = fs->super->s_blocks_count;
565 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
567 com_err("create_journal_dev", retval,
568 _("while zeroing journal device "
569 "(block %u, count %d)"),
575 progress_update(&progress, blk);
577 ext2fs_zero_blocks(0, 0, 0, 0, 0);
579 retval = io_channel_write_blk(fs->io,
580 fs->super->s_first_data_block+1,
583 com_err("create_journal_dev", retval,
584 _("while writing journal superblock"));
587 progress_close(&progress);
590 static void show_stats(ext2_filsys fs)
592 struct ext2_super_block *s = fs->super;
599 if (fs_param.s_blocks_count != s->s_blocks_count)
600 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
601 fs_param.s_blocks_count - s->s_blocks_count);
603 memset(buf, 0, sizeof(buf));
604 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
605 printf(_("Filesystem label=%s\n"), buf);
606 fputs(_("OS type: "), stdout);
607 os = e2p_os2string(fs->super->s_creator_os);
611 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
612 s->s_log_block_size);
613 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
615 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
617 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
619 100.0 * s->s_r_blocks_count / s->s_blocks_count);
620 printf(_("First data block=%u\n"), s->s_first_data_block);
621 if (s->s_reserved_gdt_blocks)
622 printf(_("Maximum filesystem blocks=%lu\n"),
623 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
624 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
625 if (fs->group_desc_count > 1)
626 printf(_("%u block groups\n"), fs->group_desc_count);
628 printf(_("%u block group\n"), fs->group_desc_count);
629 printf(_("%u blocks per group, %u fragments per group\n"),
630 s->s_blocks_per_group, s->s_frags_per_group);
631 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
633 if (fs->group_desc_count == 1) {
638 printf(_("Superblock backups stored on blocks: "));
639 group_block = s->s_first_data_block;
641 for (i = 1; i < fs->group_desc_count; i++) {
642 group_block += s->s_blocks_per_group;
643 if (!ext2fs_bg_has_super(fs, i))
647 need = int_log10(group_block) + 2;
648 if (need > col_left) {
653 printf("%u", group_block);
659 * Set the S_CREATOR_OS field. Return true if OS is known,
662 static int set_os(struct ext2_super_block *sb, char *os)
665 sb->s_creator_os = atoi (os);
666 else if (strcasecmp(os, "linux") == 0)
667 sb->s_creator_os = EXT2_OS_LINUX;
668 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
669 sb->s_creator_os = EXT2_OS_HURD;
670 else if (strcasecmp(os, "freebsd") == 0)
671 sb->s_creator_os = EXT2_OS_FREEBSD;
672 else if (strcasecmp(os, "lites") == 0)
673 sb->s_creator_os = EXT2_OS_LITES;
679 #define PATH_SET "PATH=/sbin"
681 static void parse_extended_opts(struct ext2_super_block *param,
684 char *buf, *token, *next, *p, *arg, *badopt = 0;
692 _("Couldn't allocate memory to parse options!\n"));
696 for (token = buf; token && *token; token = next) {
697 p = strchr(token, ',');
703 arg = strchr(token, '=');
708 if (strcmp(token, "stride") == 0) {
714 param->s_raid_stride = strtoul(arg, &p, 0);
715 if (*p || (param->s_raid_stride == 0)) {
717 _("Invalid stride parameter: %s\n"),
722 } else if (strcmp(token, "stripe-width") == 0 ||
723 strcmp(token, "stripe_width") == 0) {
729 param->s_raid_stripe_width = strtoul(arg, &p, 0);
730 if (*p || (param->s_raid_stripe_width == 0)) {
732 _("Invalid stripe-width parameter: %s\n"),
737 } else if (!strcmp(token, "resize")) {
738 unsigned long resize, bpg, rsv_groups;
739 unsigned long group_desc_count, desc_blocks;
740 unsigned int gdpb, blocksize;
749 resize = parse_num_blocks(arg,
750 param->s_log_block_size);
754 _("Invalid resize parameter: %s\n"),
759 if (resize <= param->s_blocks_count) {
761 _("The resize maximum must be greater "
762 "than the filesystem size.\n"));
767 blocksize = EXT2_BLOCK_SIZE(param);
768 bpg = param->s_blocks_per_group;
771 gdpb = EXT2_DESC_PER_BLOCK(param);
773 ext2fs_div_ceil(param->s_blocks_count, bpg);
774 desc_blocks = (group_desc_count +
776 rsv_groups = ext2fs_div_ceil(resize, bpg);
777 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
779 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
780 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
783 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
785 _("On-line resizing not supported with revision 0 filesystems\n"));
789 param->s_feature_compat |=
790 EXT2_FEATURE_COMPAT_RESIZE_INODE;
792 param->s_reserved_gdt_blocks = rsv_gdb;
794 } else if (!strcmp(token, "test_fs")) {
795 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
796 } else if (!strcmp(token, "lazy_itable_init")) {
798 lazy_itable_init = strtoul(arg, &p, 0);
800 lazy_itable_init = 1;
807 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
808 "Extended options are separated by commas, "
809 "and may take an argument which\n"
810 "\tis set off by an equals ('=') sign.\n\n"
811 "Valid extended options are:\n"
812 "\tstride=<RAID per-disk data chunk in blocks>\n"
813 "\tstripe-width=<RAID stride * data disks in blocks>\n"
814 "\tresize=<resize maximum size in blocks>\n"
815 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
817 badopt ? badopt : "");
821 if (param->s_raid_stride &&
822 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
823 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
824 "multiple of stride %u.\n\n"),
825 param->s_raid_stripe_width, param->s_raid_stride);
830 static __u32 ok_features[3] = {
832 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
833 EXT2_FEATURE_COMPAT_RESIZE_INODE |
834 EXT2_FEATURE_COMPAT_DIR_INDEX |
835 EXT2_FEATURE_COMPAT_EXT_ATTR,
837 EXT2_FEATURE_INCOMPAT_FILETYPE|
838 EXT3_FEATURE_INCOMPAT_EXTENTS|
839 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
840 EXT2_FEATURE_INCOMPAT_META_BG|
841 EXT4_FEATURE_INCOMPAT_FLEX_BG,
843 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
844 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
845 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
846 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
847 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
848 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
852 static void syntax_err_report(const char *filename, long err, int line_num)
855 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
856 filename, line_num, error_message(err));
860 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
862 static void edit_feature(const char *str, __u32 *compat_array)
867 if (e2p_edit_feature(str, compat_array, ok_features)) {
868 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
880 static errcode_t init_list(struct str_list *sl)
884 sl->list = malloc((sl->max+1) * sizeof(char *));
891 static errcode_t push_string(struct str_list *sl, const char *str)
895 if (sl->num >= sl->max) {
897 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
902 sl->list[sl->num] = malloc(strlen(str)+1);
903 if (sl->list[sl->num] == 0)
905 strcpy(sl->list[sl->num], str);
907 sl->list[sl->num] = 0;
911 static void print_str_list(char **list)
915 for (cpp = list; *cpp; cpp++) {
916 printf("'%s'", *cpp);
923 static char **parse_fs_type(const char *fs_type,
924 const char *usage_types,
925 struct ext2_super_block *fs_param,
928 const char *ext_type = 0;
930 char *profile_type = 0;
932 const char *size_type;
933 struct str_list list;
937 if (init_list(&list))
940 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
941 !strcasecmp(creator_os, "hurd")))
949 ext_type = strrchr(progname, '/');
955 if (!strncmp(ext_type, "mkfs.", 5)) {
957 if (ext_type[0] == 0)
964 profile_get_string(profile, "defaults", "fs_type", 0,
965 "ext2", &profile_type);
966 ext_type = profile_type;
967 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
971 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
972 !strcmp(ext_type, "ext4dev")) {
973 profile_get_string(profile, "fs_types", ext_type, "features",
976 printf(_("\nWarning! Your mke2fs.conf file does "
977 "not define the %s filesystem type.\n"),
979 printf(_("You probably need to install an updated "
980 "mke2fs.conf file.\n\n"));
985 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
986 if (fs_param->s_blocks_count < 3 * meg)
987 size_type = "floppy";
988 else if (fs_param->s_blocks_count < 512 * meg)
991 size_type = "default";
994 usage_types = size_type;
996 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1002 strcpy(parse_str, usage_types);
1007 push_string(&list, ext_type);
1010 t = strchr(cp, ',');
1015 push_string(&list, cp);
1027 push_string(&list, "hurd");
1031 static char *get_string_from_profile(char **fs_types, const char *opt,
1032 const char *def_val)
1037 for (i=0; fs_types[i]; i++);
1038 for (i-=1; i >=0 ; i--) {
1039 profile_get_string(profile, "fs_types", fs_types[i],
1044 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1048 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1053 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1054 for (cpp = fs_types; *cpp; cpp++)
1055 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1059 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1064 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1065 for (cpp = fs_types; *cpp; cpp++)
1066 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1070 extern const char *mke2fs_default_profile;
1071 static const char *default_files[] = { "<default>", 0 };
1073 static void PRS(int argc, char *argv[])
1079 int inode_ratio = 0;
1081 unsigned long flex_bg_size = 0;
1082 double reserved_ratio = 5.0;
1083 int sector_size = 0;
1084 int show_version_only = 0;
1085 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1087 char * oldpath = getenv("PATH");
1088 char * extended_opts = 0;
1089 const char * fs_type = 0;
1090 const char * usage_types = 0;
1096 int s_opt = -1, r_opt = -1;
1097 char *fs_features = 0;
1100 int pathlen = sizeof(PATH_SET) + 1;
1103 pathlen += strlen(oldpath);
1104 newpath = malloc(pathlen);
1105 strcpy(newpath, PATH_SET);
1107 /* Update our PATH to include /sbin */
1109 strcat (newpath, ":");
1110 strcat (newpath, oldpath);
1114 tmp = getenv("MKE2FS_SYNC");
1116 sync_kludge = atoi(tmp);
1118 /* Determine the system page size if possible */
1120 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1121 #define _SC_PAGESIZE _SC_PAGE_SIZE
1124 sysval = sysconf(_SC_PAGESIZE);
1126 sys_page_size = sysval;
1127 #endif /* _SC_PAGESIZE */
1128 #endif /* HAVE_SYSCONF */
1130 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1132 profile_set_syntax_err_cb(syntax_err_report);
1133 retval = profile_init(config_fn, &profile);
1134 if (retval == ENOENT) {
1135 profile_init(default_files, &profile);
1136 profile_set_default(profile, mke2fs_default_profile);
1139 setbuf(stdout, NULL);
1140 setbuf(stderr, NULL);
1141 add_error_table(&et_ext2_error_table);
1142 add_error_table(&et_prof_error_table);
1143 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1144 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1151 linux_version_code = parse_version_number(ut.release);
1152 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1153 fs_param.s_rev_level = 0;
1156 if (argc && *argv) {
1157 program_name = get_progname(*argv);
1159 /* If called as mkfs.ext3, create a journal inode */
1160 if (!strcmp(program_name, "mkfs.ext3"))
1164 while ((c = getopt (argc, argv,
1165 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
1168 blocksize = strtol(optarg, &tmp, 0);
1169 b = (blocksize > 0) ? blocksize : -blocksize;
1170 if (b < EXT2_MIN_BLOCK_SIZE ||
1171 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1172 com_err(program_name, 0,
1173 _("invalid block size - %s"), optarg);
1176 if (blocksize > 4096)
1177 fprintf(stderr, _("Warning: blocksize %d not "
1178 "usable on most systems.\n"),
1181 fs_param.s_log_block_size =
1182 int_log2(blocksize >>
1183 EXT2_MIN_BLOCK_LOG_SIZE);
1185 case 'c': /* Check for bad blocks */
1189 size = strtoul(optarg, &tmp, 0);
1190 if (size < EXT2_MIN_BLOCK_SIZE ||
1191 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1192 com_err(program_name, 0,
1193 _("invalid fragment size - %s"),
1197 fs_param.s_log_frag_size =
1198 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1199 fprintf(stderr, _("Warning: fragments not supported. "
1200 "Ignoring -f option\n"));
1203 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1205 com_err(program_name, 0,
1206 _("Illegal number for blocks per group"));
1209 if ((fs_param.s_blocks_per_group % 8) != 0) {
1210 com_err(program_name, 0,
1211 _("blocks per group must be multiple of 8"));
1216 flex_bg_size = strtoul(optarg, &tmp, 0);
1218 com_err(program_name, 0,
1219 _("Illegal number for flex_bg size"));
1222 if (flex_bg_size < 2 ||
1223 (flex_bg_size & (flex_bg_size-1)) != 0) {
1224 com_err(program_name, 0,
1225 _("flex_bg size must be a power of 2"));
1230 inode_ratio = strtoul(optarg, &tmp, 0);
1231 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1232 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1234 com_err(program_name, 0,
1235 _("invalid inode ratio %s (min %d/max %d)"),
1236 optarg, EXT2_MIN_BLOCK_SIZE,
1237 EXT2_MAX_BLOCK_SIZE);
1242 parse_journal_opts(optarg);
1249 bad_blocks_filename = malloc(strlen(optarg)+1);
1250 if (!bad_blocks_filename) {
1251 com_err(program_name, ENOMEM,
1252 _("in malloc for bad_blocks_filename"));
1255 strcpy(bad_blocks_filename, optarg);
1258 reserved_ratio = strtod(optarg, &tmp);
1259 if (reserved_ratio > 50 || *tmp) {
1260 com_err(program_name, 0,
1261 _("invalid reserved blocks percent - %s"),
1270 creator_os = optarg;
1276 r_opt = strtoul(optarg, &tmp, 0);
1278 com_err(program_name, 0,
1279 _("bad revision level - %s"), optarg);
1282 fs_param.s_rev_level = r_opt;
1284 case 's': /* deprecated */
1285 s_opt = atoi(optarg);
1288 inode_size = strtoul(optarg, &tmp, 0);
1290 com_err(program_name, 0,
1291 _("invalid inode size - %s"), optarg);
1302 volume_label = optarg;
1308 num_inodes = strtoul(optarg, &tmp, 0);
1310 com_err(program_name, 0,
1311 _("bad num inodes - %s"), optarg);
1316 fs_features = optarg;
1320 extended_opts = optarg;
1329 usage_types = optarg;
1332 /* Print version number and exit */
1333 show_version_only++;
1339 if ((optind == argc) && !show_version_only)
1341 device_name = argv[optind++];
1343 if (!quiet || show_version_only)
1344 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1347 if (show_version_only) {
1348 fprintf(stderr, _("\tUsing %s\n"),
1349 error_message(EXT2_ET_BASE));
1354 * If there's no blocksize specified and there is a journal
1355 * device, use it to figure out the blocksize
1357 if (blocksize <= 0 && journal_device) {
1361 #ifdef CONFIG_TESTIO_DEBUG
1362 io_ptr = test_io_manager;
1363 test_io_backing_manager = unix_io_manager;
1365 io_ptr = unix_io_manager;
1367 retval = ext2fs_open(journal_device,
1368 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1371 com_err(program_name, retval,
1372 _("while trying to open journal device %s\n"),
1376 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1377 com_err(program_name, 0,
1378 _("Journal dev blocksize (%d) smaller than "
1379 "minimum blocksize %d\n"), jfs->blocksize,
1383 blocksize = jfs->blocksize;
1384 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1385 fs_param.s_log_block_size =
1386 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1390 if (blocksize > sys_page_size) {
1392 com_err(program_name, 0,
1393 _("%d-byte blocks too big for system (max %d)"),
1394 blocksize, sys_page_size);
1397 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1398 "(max %d), forced to continue\n"),
1399 blocksize, sys_page_size);
1401 if (optind < argc) {
1402 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1403 fs_param.s_log_block_size);
1404 if (!fs_param.s_blocks_count) {
1405 com_err(program_name, 0, _("invalid blocks count - %s"),
1414 check_plausibility(device_name);
1415 check_mount(device_name, force, _("filesystem"));
1417 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1419 if (noaction && fs_param.s_blocks_count) {
1420 dev_size = fs_param.s_blocks_count;
1424 retval = ext2fs_get_device_size(device_name,
1425 EXT2_BLOCK_SIZE(&fs_param),
1427 if ((retval == EFBIG) &&
1429 (fs_param.s_log_block_size == 0)) {
1430 fs_param.s_log_block_size = 2;
1436 if (retval == EFBIG) {
1437 fprintf(stderr, _("%s: Size of device %s too big "
1438 "to be expressed in 32 bits\n\t"
1439 "using a blocksize of %d.\n"),
1440 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1443 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1444 com_err(program_name, retval,
1445 _("while trying to determine filesystem size"));
1448 if (!fs_param.s_blocks_count) {
1449 if (retval == EXT2_ET_UNIMPLEMENTED) {
1450 com_err(program_name, 0,
1451 _("Couldn't determine device size; you "
1452 "must specify\nthe size of the "
1456 if (dev_size == 0) {
1457 com_err(program_name, 0,
1458 _("Device size reported to be zero. "
1459 "Invalid partition specified, or\n\t"
1460 "partition table wasn't reread "
1461 "after running fdisk, due to\n\t"
1462 "a modified partition being busy "
1463 "and in use. You may need to reboot\n\t"
1464 "to re-read your partition table.\n"
1468 fs_param.s_blocks_count = dev_size;
1469 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1470 fs_param.s_blocks_count &= ~((sys_page_size /
1471 EXT2_BLOCK_SIZE(&fs_param))-1);
1474 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1475 com_err(program_name, 0,
1476 _("Filesystem larger than apparent device size."));
1480 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1482 fprintf(stderr, _("Failed to parse fs types list\n"));
1486 /* Figure out what features should be enabled */
1489 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1490 tmp = get_string_from_profile(fs_types, "base_features",
1491 "sparse_super,filetype,resize_inode,dir_index");
1492 edit_feature(tmp, &fs_param.s_feature_compat);
1495 for (cpp = fs_types; *cpp; cpp++) {
1497 profile_get_string(profile, "fs_types", *cpp,
1498 "features", "", &tmp);
1500 edit_feature(tmp, &fs_param.s_feature_compat);
1504 tmp = get_string_from_profile(fs_types, "default_features",
1507 edit_feature(fs_features ? fs_features : tmp,
1508 &fs_param.s_feature_compat);
1512 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1513 fs_types[0] = strdup("journal");
1518 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1519 print_str_list(fs_types);
1522 if (r_opt == EXT2_GOOD_OLD_REV &&
1523 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1524 fs_param.s_feature_ro_compat)) {
1525 fprintf(stderr, _("Filesystem features not supported "
1526 "with revision 0 filesystems\n"));
1531 if (r_opt == EXT2_GOOD_OLD_REV) {
1532 fprintf(stderr, _("Sparse superblocks not supported "
1533 "with revision 0 filesystems\n"));
1536 fs_param.s_feature_ro_compat |=
1537 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1538 } else if (s_opt == 0)
1539 fs_param.s_feature_ro_compat &=
1540 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1542 if (journal_size != 0) {
1543 if (r_opt == EXT2_GOOD_OLD_REV) {
1544 fprintf(stderr, _("Journals not supported "
1545 "with revision 0 filesystems\n"));
1548 fs_param.s_feature_compat |=
1549 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1552 if (fs_param.s_feature_incompat &
1553 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1555 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1556 fs_param.s_feature_compat = 0;
1557 fs_param.s_feature_ro_compat = 0;
1560 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1561 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1562 fprintf(stderr, _("The resize_inode and meta_bg features "
1563 "are not compatible.\n"
1564 "They can not be both enabled "
1565 "simultaneously.\n"));
1569 /* Set first meta blockgroup via an environment variable */
1570 /* (this is mostly for debugging purposes) */
1571 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1572 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1573 fs_param.s_first_meta_bg = atoi(tmp);
1575 /* Get the hardware sector size, if available */
1576 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1578 com_err(program_name, retval,
1579 _("while trying to determine hardware sector size"));
1583 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1584 sector_size = atoi(tmp);
1586 if (blocksize <= 0) {
1587 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1589 if (use_bsize == -1) {
1590 use_bsize = sys_page_size;
1591 if ((linux_version_code < (2*65536 + 6*256)) &&
1595 if (sector_size && use_bsize < sector_size)
1596 use_bsize = sector_size;
1597 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1598 use_bsize = -blocksize;
1599 blocksize = use_bsize;
1600 fs_param.s_blocks_count /= blocksize / 1024;
1603 if (inode_ratio == 0) {
1604 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1606 if (inode_ratio < blocksize)
1607 inode_ratio = blocksize;
1610 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1611 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1613 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1615 lazy_itable_init = get_bool_from_profile(fs_types,
1616 "lazy_itable_init", 0);
1618 /* Get options from profile */
1619 for (cpp = fs_types; *cpp; cpp++) {
1621 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1623 parse_extended_opts(&fs_param, tmp);
1629 parse_extended_opts(&fs_param, extended_opts);
1631 /* Since sparse_super is the default, we would only have a problem
1632 * here if it was explicitly disabled.
1634 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1635 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1636 com_err(program_name, 0,
1637 _("reserved online resize blocks not supported "
1638 "on non-sparse filesystem"));
1642 if (fs_param.s_blocks_per_group) {
1643 if (fs_param.s_blocks_per_group < 256 ||
1644 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1645 com_err(program_name, 0,
1646 _("blocks per group count out of range"));
1651 if (inode_size == 0)
1652 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1653 if (!flex_bg_size && (fs_param.s_feature_incompat &
1654 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1655 flex_bg_size = get_int_from_profile(fs_types,
1656 "flex_bg_size", 16);
1658 if (!(fs_param.s_feature_incompat &
1659 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1660 com_err(program_name, 0,
1661 _("Flex_bg feature not enabled, so "
1662 "flex_bg size may not be specified"));
1665 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1668 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1669 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1670 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1671 inode_size & (inode_size - 1)) {
1672 com_err(program_name, 0,
1673 _("invalid inode size %d (min %d/max %d)"),
1674 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1678 fs_param.s_inode_size = inode_size;
1681 /* Make sure number of inodes specified will fit in 32 bits */
1682 if (num_inodes == 0) {
1683 unsigned long long n;
1684 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1686 com_err(program_name, 0,
1687 _("too many inodes (%llu), raise inode ratio?"), n);
1690 } else if (num_inodes > ~0U) {
1691 com_err(program_name, 0,
1692 _("too many inodes (%llu), specify < 2^32 inodes"),
1697 * Calculate number of inodes based on the inode ratio
1699 fs_param.s_inodes_count = num_inodes ? num_inodes :
1700 ((__u64) fs_param.s_blocks_count * blocksize)
1703 if ((((long long)fs_param.s_inodes_count) *
1704 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1705 (((long long)fs_param.s_blocks_count) *
1706 EXT2_BLOCK_SIZE(&fs_param))) {
1707 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1708 "(%u) too big for a\n\t"
1709 "filesystem with %lu blocks, "
1710 "specify higher inode_ratio (-i)\n\t"
1711 "or lower inode count (-N).\n"),
1712 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1713 fs_param.s_inodes_count,
1714 (unsigned long) fs_param.s_blocks_count);
1719 * Calculate number of blocks to reserve
1721 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1722 fs_param.s_blocks_count / 100.0);
1725 static int should_do_undo(const char *name)
1730 struct ext2_super_block super;
1731 io_manager manager = unix_io_manager;
1732 int csum_flag, force_undo;
1734 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1735 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1736 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1737 if (!force_undo && (!csum_flag || !lazy_itable_init))
1740 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1743 * We don't handle error cases instead we
1744 * declare that the file system doesn't exist
1745 * and let the rest of mke2fs take care of
1752 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1753 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1759 #if defined(WORDS_BIGENDIAN)
1760 s_magic = ext2fs_swab16(super.s_magic);
1762 s_magic = super.s_magic;
1765 if (s_magic == EXT2_SUPER_MAGIC)
1769 io_channel_close(channel);
1776 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1778 errcode_t retval = 0;
1779 char *tdb_dir, tdb_file[PATH_MAX];
1780 char *device_name, *tmp_name;
1783 * Configuration via a conf file would be
1786 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1788 profile_get_string(profile, "defaults",
1789 "undo_dir", 0, "/var/lib/e2fsprogs",
1792 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1793 access(tdb_dir, W_OK))
1796 tmp_name = strdup(name);
1797 device_name = basename(tmp_name);
1798 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1800 if (!access(tdb_file, F_OK)) {
1801 if (unlink(tdb_file) < 0) {
1803 com_err(program_name, retval,
1804 _("while trying to delete %s"),
1810 set_undo_io_backing_manager(*io_ptr);
1811 *io_ptr = undo_io_manager;
1812 set_undo_io_backup_file(tdb_file);
1813 printf(_("Overwriting existing filesystem; this can be undone "
1814 "using the command:\n"
1815 " e2undo %s %s\n\n"), tdb_file, name);
1821 int main (int argc, char *argv[])
1823 errcode_t retval = 0;
1825 badblocks_list bb_list = 0;
1826 unsigned int journal_blocks;
1830 char tdb_string[40];
1833 setlocale(LC_MESSAGES, "");
1834 setlocale(LC_CTYPE, "");
1835 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1836 textdomain(NLS_CAT_NAME);
1840 #ifdef CONFIG_TESTIO_DEBUG
1841 io_ptr = test_io_manager;
1842 test_io_backing_manager = unix_io_manager;
1844 io_ptr = unix_io_manager;
1847 if (should_do_undo(device_name)) {
1848 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1854 * Initialize the superblock....
1856 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1859 com_err(device_name, retval, _("while setting up superblock"));
1862 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1863 32768 : fs->blocksize * 8);
1864 io_channel_set_options(fs->io, tdb_string);
1866 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1867 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1870 * Wipe out the old on-disk superblock
1873 zap_sector(fs, 2, 6);
1876 * Generate a UUID for it...
1878 uuid_generate(fs->super->s_uuid);
1881 * Initialize the directory index variables
1883 fs->super->s_def_hash_version = EXT2_HASH_HALF_MD4;
1884 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1887 * Add "jitter" to the superblock's check interval so that we
1888 * don't check all the filesystems at the same time. We use a
1889 * kludgy hack of using the UUID to derive a random jitter value.
1891 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1892 val += fs->super->s_uuid[i];
1893 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1896 * Override the creator OS, if applicable
1898 if (creator_os && !set_os(fs->super, creator_os)) {
1899 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1904 * For the Hurd, we will turn off filetype since it doesn't
1907 if (fs->super->s_creator_os == EXT2_OS_HURD)
1908 fs->super->s_feature_incompat &=
1909 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1912 * Set the volume label...
1915 memset(fs->super->s_volume_name, 0,
1916 sizeof(fs->super->s_volume_name));
1917 strncpy(fs->super->s_volume_name, volume_label,
1918 sizeof(fs->super->s_volume_name));
1922 * Set the last mount directory
1925 memset(fs->super->s_last_mounted, 0,
1926 sizeof(fs->super->s_last_mounted));
1927 strncpy(fs->super->s_last_mounted, mount_dir,
1928 sizeof(fs->super->s_last_mounted));
1931 if (!quiet || noaction)
1937 if (fs->super->s_feature_incompat &
1938 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1939 create_journal_dev(fs);
1940 exit(ext2fs_close(fs) ? 1 : 0);
1943 if (bad_blocks_filename)
1944 read_bb_file(fs, &bb_list, bad_blocks_filename);
1946 test_disk(fs, &bb_list);
1948 handle_bad_blocks(fs, bb_list);
1949 fs->stride = fs_stride = fs->super->s_raid_stride;
1950 retval = ext2fs_allocate_tables(fs);
1952 com_err(program_name, retval,
1953 _("while trying to allocate filesystem tables"));
1957 fs->super->s_state |= EXT2_ERROR_FS;
1958 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1960 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1961 unsigned int rsv = 65536 / fs->blocksize;
1962 unsigned long blocks = fs->super->s_blocks_count;
1963 unsigned long start;
1966 #ifdef ZAP_BOOTBLOCK
1967 zap_sector(fs, 0, 2);
1971 * Wipe out any old MD RAID (or other) metadata at the end
1972 * of the device. This will also verify that the device is
1973 * as large as we think. Be careful with very small devices.
1975 start = (blocks & ~(rsv - 1));
1979 retval = ext2fs_zero_blocks(fs, start, blocks - start,
1983 com_err(program_name, retval,
1984 _("while zeroing block %u at end of filesystem"),
1987 write_inode_tables(fs, lazy_itable_init);
1988 create_root_dir(fs);
1989 create_lost_and_found(fs);
1991 create_bad_block_inode(fs, bb_list);
1992 if (fs->super->s_feature_compat &
1993 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1994 retval = ext2fs_create_resize_inode(fs);
1996 com_err("ext2fs_create_resize_inode", retval,
1997 _("while reserving blocks for online resize"));
2003 if (journal_device) {
2007 check_plausibility(journal_device);
2008 check_mount(journal_device, force, _("journal"));
2010 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2011 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2012 fs->blocksize, unix_io_manager, &jfs);
2014 com_err(program_name, retval,
2015 _("while trying to open journal device %s\n"),
2020 printf(_("Adding journal to device %s: "),
2024 retval = ext2fs_add_journal_device(fs, jfs);
2026 com_err (program_name, retval,
2027 _("\n\twhile trying to add journal to device %s"),
2032 printf(_("done\n"));
2034 free(journal_device);
2035 } else if ((journal_size) ||
2036 (fs_param.s_feature_compat &
2037 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2038 journal_blocks = figure_journal_size(journal_size, fs);
2040 if (!journal_blocks) {
2041 fs->super->s_feature_compat &=
2042 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2046 printf(_("Creating journal (%u blocks): "),
2050 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2053 com_err (program_name, retval,
2054 _("\n\twhile trying to create journal"));
2058 printf(_("done\n"));
2063 printf(_("Writing superblocks and "
2064 "filesystem accounting information: "));
2065 retval = ext2fs_flush(fs);
2068 _("\nWarning, had trouble writing out superblocks."));
2071 printf(_("done\n\n"));
2072 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2073 print_check_message(fs);
2075 val = ext2fs_close(fs);
2076 remove_error_table(&et_ext2_error_table);
2077 remove_error_table(&et_prof_error_table);
2078 profile_release(profile);
2079 return (retval || val) ? 1 : 0;