2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 /* Usage: mke2fs [options] device
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
19 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
28 #include <sys/utsname.h>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
53 #include <blkid/blkid.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fsP.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
60 #include "ext2fs/ext2fs.h"
64 #include "../version.h"
65 #include "nls-enable.h"
67 #define STRIDE_LENGTH 8
69 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
75 extern int isatty(int);
76 extern FILE *fpopen(const char *cmd, const char *mode);
78 const char * program_name = "mke2fs";
79 const char * device_name /* = NULL */;
81 /* Command line options */
86 int discard = 1; /* attempt to discard device before fs creation */
92 char *bad_blocks_filename;
95 struct ext2_super_block fs_param;
100 char *journal_device;
101 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
106 int sys_page_size = 4096;
107 int linux_version_code = 0;
109 static void usage(void)
111 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
112 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
113 "[-J journal-options]\n"
114 "\t[-G meta group size] [-N number-of-inodes]\n"
115 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
116 "\t[-g blocks-per-group] [-L volume-label] "
117 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
118 "[-r fs-revision] [-E extended-option[,...]]\n"
119 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
124 static int int_log2(unsigned long long arg)
136 static int int_log10(unsigned long long arg)
145 static int parse_version_number(const char *s)
147 int major, minor, rev;
153 major = strtol(cp, &endptr, 10);
154 if (cp == endptr || *endptr != '.')
157 minor = strtol(cp, &endptr, 10);
158 if (cp == endptr || *endptr != '.')
161 rev = strtol(cp, &endptr, 10);
164 return ((((major * 256) + minor) * 256) + rev);
168 * Helper function for read_bb_file and test_disk
170 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
172 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
177 * Reads the bad blocks list from a file
179 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
180 const char *bad_blocks_file)
185 f = fopen(bad_blocks_file, "r");
187 com_err("read_bad_blocks_file", errno,
188 _("while trying to open %s"), bad_blocks_file);
191 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
194 com_err("ext2fs_read_bb_FILE", retval,
195 _("while reading in list of bad blocks from file"));
201 * Runs the badblocks program to test the disk
203 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
209 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
210 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
211 fs->device_name, ext2fs_blocks_count(fs->super)-1);
213 printf(_("Running command: %s\n"), buf);
216 com_err("popen", errno,
217 _("while trying to run '%s'"), buf);
220 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
223 com_err("ext2fs_read_bb_FILE", retval,
224 _("while processing list of bad blocks from program"));
229 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
233 unsigned must_be_good;
235 badblocks_iterate bb_iter;
245 * The primary superblock and group descriptors *must* be
246 * good; if not, abort.
248 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
249 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
250 if (ext2fs_badblocks_list_test(bb_list, i)) {
251 fprintf(stderr, _("Block %d in primary "
252 "superblock/group descriptor area bad.\n"), i);
253 fprintf(stderr, _("Blocks %u through %u must be good "
254 "in order to build a filesystem.\n"),
255 fs->super->s_first_data_block, must_be_good);
256 fputs(_("Aborting....\n"), stderr);
262 * See if any of the bad blocks are showing up in the backup
263 * superblocks and/or group descriptors. If so, issue a
264 * warning and adjust the block counts appropriately.
266 group_block = fs->super->s_first_data_block +
267 fs->super->s_blocks_per_group;
269 for (i = 1; i < fs->group_desc_count; i++) {
271 for (j=0; j < fs->desc_blocks+1; j++) {
272 if (ext2fs_badblocks_list_test(bb_list,
276 _("Warning: the backup superblock/group descriptors at block %u contain\n"
280 group = ext2fs_group_of_blk2(fs, group_block+j);
281 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
282 ext2fs_group_desc_csum_set(fs, group);
283 ext2fs_free_blocks_count_add(fs->super, 1);
286 group_block += fs->super->s_blocks_per_group;
290 * Mark all the bad blocks as used...
292 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
294 com_err("ext2fs_badblocks_list_iterate_begin", retval,
295 _("while marking bad blocks as used"));
298 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
299 ext2fs_mark_block_bitmap2(fs->block_map, blk);
300 ext2fs_badblocks_list_iterate_end(bb_iter);
303 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
309 struct ext2fs_numeric_progress_struct progress;
311 ext2fs_numeric_progress_init(fs, &progress,
312 _("Writing inode tables: "),
313 fs->group_desc_count);
315 for (i = 0; i < fs->group_desc_count; i++) {
316 ext2fs_numeric_progress_update(fs, &progress, i);
318 blk = ext2fs_inode_table_loc(fs, i);
319 num = fs->inode_blocks_per_group;
322 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
323 ext2fs_bg_itable_unused(fs, i)) *
324 EXT2_INODE_SIZE(fs->super),
325 EXT2_BLOCK_SIZE(fs->super));
326 if (!lazy_flag || itable_zeroed) {
327 /* The kernel doesn't need to zero the itable blocks */
328 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
329 ext2fs_group_desc_csum_set(fs, i);
331 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
333 fprintf(stderr, _("\nCould not write %d "
334 "blocks in inode table starting at %llu: %s\n"),
335 num, blk, error_message(retval));
339 if (sync_kludge == 1)
341 else if ((i % sync_kludge) == 0)
345 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
346 ext2fs_numeric_progress_close(fs, &progress,
350 static void create_root_dir(ext2_filsys fs)
353 struct ext2_inode inode;
356 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
358 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
362 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
364 com_err("ext2fs_read_inode", retval,
365 _("while reading root inode"));
370 ext2fs_set_i_uid_high(inode, uid >> 16);
374 ext2fs_set_i_gid_high(inode, gid >> 16);
376 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
378 com_err("ext2fs_write_inode", retval,
379 _("while setting root inode ownership"));
385 static void create_lost_and_found(ext2_filsys fs)
387 unsigned int lpf_size = 0;
390 const char *name = "lost+found";
394 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
396 com_err("ext2fs_mkdir", retval,
397 _("while creating /lost+found"));
401 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
403 com_err("ext2_lookup", retval,
404 _("while looking up /lost+found"));
408 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
409 /* Ensure that lost+found is at least 2 blocks, so we always
410 * test large empty blocks for big-block filesystems. */
411 if ((lpf_size += fs->blocksize) >= 16*1024 &&
412 lpf_size >= 2 * fs->blocksize)
414 retval = ext2fs_expand_dir(fs, ino);
416 com_err("ext2fs_expand_dir", retval,
417 _("while expanding /lost+found"));
423 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
427 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
428 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
429 retval = ext2fs_update_bb_inode(fs, bb_list);
431 com_err("ext2fs_update_bb_inode", retval,
432 _("while setting bad block inode"));
438 static void reserve_inodes(ext2_filsys fs)
442 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
443 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
444 ext2fs_mark_ib_dirty(fs);
447 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
448 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
449 #define BSD_LABEL_OFFSET 64
451 static void zap_sector(ext2_filsys fs, int sect, int nsect)
457 buf = malloc(512*nsect);
459 printf(_("Out of memory erasing sectors %d-%d\n"),
460 sect, sect + nsect - 1);
465 /* Check for a BSD disklabel, and don't erase it if so */
466 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
469 _("Warning: could not read block 0: %s\n"),
470 error_message(retval));
472 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
473 if ((*magic == BSD_DISKMAGIC) ||
474 (*magic == BSD_MAGICDISK))
479 memset(buf, 0, 512*nsect);
480 io_channel_set_blksize(fs->io, 512);
481 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
482 io_channel_set_blksize(fs->io, fs->blocksize);
485 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
486 sect, error_message(retval));
489 static void create_journal_dev(ext2_filsys fs)
491 struct ext2fs_numeric_progress_struct progress;
494 blk64_t blk, err_blk;
495 int c, count, err_count;
497 retval = ext2fs_create_journal_superblock(fs,
498 ext2fs_blocks_count(fs->super), 0, &buf);
500 com_err("create_journal_dev", retval,
501 _("while initializing journal superblock"));
504 ext2fs_numeric_progress_init(fs, &progress,
505 _("Zeroing journal device: "),
506 ext2fs_blocks_count(fs->super));
508 count = ext2fs_blocks_count(fs->super);
514 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
516 com_err("create_journal_dev", retval,
517 _("while zeroing journal device "
518 "(block %llu, count %d)"),
524 ext2fs_numeric_progress_update(fs, &progress, blk);
526 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
528 retval = io_channel_write_blk64(fs->io,
529 fs->super->s_first_data_block+1,
532 com_err("create_journal_dev", retval,
533 _("while writing journal superblock"));
536 ext2fs_numeric_progress_close(fs, &progress, NULL);
539 static void show_stats(ext2_filsys fs)
541 struct ext2_super_block *s = fs->super;
548 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
549 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
550 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
552 memset(buf, 0, sizeof(buf));
553 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
554 printf(_("Filesystem label=%s\n"), buf);
555 os = e2p_os2string(fs->super->s_creator_os);
557 printf(_("OS type: %s\n"), os);
559 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
560 s->s_log_block_size);
561 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
563 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
564 s->s_raid_stride, s->s_raid_stripe_width);
565 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
566 ext2fs_blocks_count(s));
567 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
568 ext2fs_r_blocks_count(s),
569 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
570 printf(_("First data block=%u\n"), s->s_first_data_block);
571 if (s->s_reserved_gdt_blocks)
572 printf(_("Maximum filesystem blocks=%lu\n"),
573 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
574 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
575 if (fs->group_desc_count > 1)
576 printf(_("%u block groups\n"), fs->group_desc_count);
578 printf(_("%u block group\n"), fs->group_desc_count);
579 printf(_("%u blocks per group, %u fragments per group\n"),
580 s->s_blocks_per_group, s->s_frags_per_group);
581 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
583 if (fs->group_desc_count == 1) {
588 printf(_("Superblock backups stored on blocks: "));
589 group_block = s->s_first_data_block;
591 for (i = 1; i < fs->group_desc_count; i++) {
592 group_block += s->s_blocks_per_group;
593 if (!ext2fs_bg_has_super(fs, i))
597 need = int_log10(group_block) + 2;
598 if (need > col_left) {
603 printf("%llu", group_block);
609 * Set the S_CREATOR_OS field. Return true if OS is known,
612 static int set_os(struct ext2_super_block *sb, char *os)
615 sb->s_creator_os = atoi (os);
616 else if (strcasecmp(os, "linux") == 0)
617 sb->s_creator_os = EXT2_OS_LINUX;
618 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
619 sb->s_creator_os = EXT2_OS_HURD;
620 else if (strcasecmp(os, "freebsd") == 0)
621 sb->s_creator_os = EXT2_OS_FREEBSD;
622 else if (strcasecmp(os, "lites") == 0)
623 sb->s_creator_os = EXT2_OS_LITES;
629 #define PATH_SET "PATH=/sbin"
631 static void parse_extended_opts(struct ext2_super_block *param,
634 char *buf, *token, *next, *p, *arg, *badopt = 0;
642 _("Couldn't allocate memory to parse options!\n"));
646 for (token = buf; token && *token; token = next) {
647 p = strchr(token, ',');
653 arg = strchr(token, '=');
658 if (strcmp(token, "stride") == 0) {
664 param->s_raid_stride = strtoul(arg, &p, 0);
665 if (*p || (param->s_raid_stride == 0)) {
667 _("Invalid stride parameter: %s\n"),
672 } else if (strcmp(token, "stripe-width") == 0 ||
673 strcmp(token, "stripe_width") == 0) {
679 param->s_raid_stripe_width = strtoul(arg, &p, 0);
680 if (*p || (param->s_raid_stripe_width == 0)) {
682 _("Invalid stripe-width parameter: %s\n"),
687 } else if (!strcmp(token, "resize")) {
689 unsigned long bpg, rsv_groups;
690 unsigned long group_desc_count, desc_blocks;
691 unsigned int gdpb, blocksize;
700 resize = parse_num_blocks2(arg,
701 param->s_log_block_size);
705 _("Invalid resize parameter: %s\n"),
710 if (resize <= ext2fs_blocks_count(param)) {
712 _("The resize maximum must be greater "
713 "than the filesystem size.\n"));
718 blocksize = EXT2_BLOCK_SIZE(param);
719 bpg = param->s_blocks_per_group;
722 gdpb = EXT2_DESC_PER_BLOCK(param);
723 group_desc_count = (__u32) ext2fs_div64_ceil(
724 ext2fs_blocks_count(param), bpg);
725 desc_blocks = (group_desc_count +
727 rsv_groups = ext2fs_div64_ceil(resize, bpg);
728 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
730 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
731 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
734 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
736 _("On-line resizing not supported with revision 0 filesystems\n"));
740 param->s_feature_compat |=
741 EXT2_FEATURE_COMPAT_RESIZE_INODE;
743 param->s_reserved_gdt_blocks = rsv_gdb;
745 } else if (!strcmp(token, "test_fs")) {
746 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
747 } else if (!strcmp(token, "lazy_itable_init")) {
749 lazy_itable_init = strtoul(arg, &p, 0);
751 lazy_itable_init = 1;
752 } else if (!strcmp(token, "discard")) {
754 } else if (!strcmp(token, "nodiscard")) {
762 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
763 "Extended options are separated by commas, "
764 "and may take an argument which\n"
765 "\tis set off by an equals ('=') sign.\n\n"
766 "Valid extended options are:\n"
767 "\tstride=<RAID per-disk data chunk in blocks>\n"
768 "\tstripe-width=<RAID stride * data disks in blocks>\n"
769 "\tresize=<resize maximum size in blocks>\n"
770 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
774 badopt ? badopt : "");
778 if (param->s_raid_stride &&
779 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
780 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
781 "multiple of stride %u.\n\n"),
782 param->s_raid_stripe_width, param->s_raid_stride);
787 static __u32 ok_features[3] = {
789 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
790 EXT2_FEATURE_COMPAT_RESIZE_INODE |
791 EXT2_FEATURE_COMPAT_DIR_INDEX |
792 EXT2_FEATURE_COMPAT_EXT_ATTR,
794 EXT2_FEATURE_INCOMPAT_FILETYPE|
795 EXT3_FEATURE_INCOMPAT_EXTENTS|
796 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
797 EXT2_FEATURE_INCOMPAT_META_BG|
798 EXT4_FEATURE_INCOMPAT_FLEX_BG|
799 EXT4_FEATURE_INCOMPAT_64BIT,
801 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
802 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
803 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
804 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
805 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
806 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
810 static void syntax_err_report(const char *filename, long err, int line_num)
813 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
814 filename, line_num, error_message(err));
818 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
820 static void edit_feature(const char *str, __u32 *compat_array)
825 if (e2p_edit_feature(str, compat_array, ok_features)) {
826 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
838 static errcode_t init_list(struct str_list *sl)
842 sl->list = malloc((sl->max+1) * sizeof(char *));
849 static errcode_t push_string(struct str_list *sl, const char *str)
853 if (sl->num >= sl->max) {
855 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
860 sl->list[sl->num] = malloc(strlen(str)+1);
861 if (sl->list[sl->num] == 0)
863 strcpy(sl->list[sl->num], str);
865 sl->list[sl->num] = 0;
869 static void print_str_list(char **list)
873 for (cpp = list; *cpp; cpp++) {
874 printf("'%s'", *cpp);
882 * Return TRUE if the profile has the given subsection
884 static int profile_has_subsection(profile_t profile, const char *section,
885 const char *subsection)
888 const char *names[4];
893 names[1] = subsection;
896 if (profile_iterator_create(profile, names,
897 PROFILE_ITER_LIST_SECTION |
898 PROFILE_ITER_RELATIONS_ONLY, &state))
901 if ((profile_iterator(&state, &name, 0) == 0) && name) {
906 profile_iterator_free(&state);
910 static char **parse_fs_type(const char *fs_type,
911 const char *usage_types,
912 struct ext2_super_block *fs_param,
913 blk64_t fs_blocks_count,
916 const char *ext_type = 0;
918 char *profile_type = 0;
920 const char *size_type;
921 struct str_list list;
922 unsigned long long meg;
925 if (init_list(&list))
928 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
929 !strcasecmp(creator_os, "hurd")))
936 else if (!strcmp(program_name, "mke3fs"))
939 ext_type = strrchr(progname, '/');
945 if (!strncmp(ext_type, "mkfs.", 5)) {
947 if (ext_type[0] == 0)
954 profile_get_string(profile, "defaults", "fs_type", 0,
955 "ext2", &profile_type);
956 ext_type = profile_type;
957 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
962 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
963 strcmp(ext_type, "ext2")) {
964 printf(_("\nYour mke2fs.conf file does not define the "
965 "%s filesystem type.\n"), ext_type);
966 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
967 !strcmp(ext_type, "ext4dev")) {
968 printf(_("You probably need to install an updated "
969 "mke2fs.conf file.\n\n"));
972 printf(_("Aborting...\n"));
977 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
978 if (fs_blocks_count < 3 * meg)
979 size_type = "floppy";
980 else if (fs_blocks_count < 512 * meg)
982 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
983 size_type = "default";
984 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
990 usage_types = size_type;
992 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
998 strcpy(parse_str, usage_types);
1003 push_string(&list, ext_type);
1006 t = strchr(cp, ',');
1011 if (!profile_has_subsection(profile, "fs_types", cp))
1013 _("\nWarning: the fs_type %s is not "
1014 "defined in /etc/mke2fs.conf\n\n"),
1017 push_string(&list, cp);
1029 push_string(&list, "hurd");
1033 static char *get_string_from_profile(char **fs_types, const char *opt,
1034 const char *def_val)
1039 for (i=0; fs_types[i]; i++);
1040 for (i-=1; i >=0 ; i--) {
1041 profile_get_string(profile, "fs_types", fs_types[i],
1046 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1050 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1055 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1056 for (cpp = fs_types; *cpp; cpp++)
1057 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1061 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1066 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1067 for (cpp = fs_types; *cpp; cpp++)
1068 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1072 extern const char *mke2fs_default_profile;
1073 static const char *default_files[] = { "<default>", 0 };
1075 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1077 * Sets the geometry of a device (stripe/stride), and returns the
1078 * device's alignment offset, if any, or a negative error.
1080 static int get_device_geometry(const char *file,
1081 struct ext2_super_block *fs_param,
1088 unsigned long min_io;
1089 unsigned long opt_io;
1090 struct stat statbuf;
1092 /* Nothing to do for a regular file */
1093 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1096 pr = blkid_new_probe_from_filename(file);
1100 tp = blkid_probe_get_topology(pr);
1104 min_io = blkid_topology_get_minimum_io_size(tp);
1105 opt_io = blkid_topology_get_optimal_io_size(tp);
1106 blocksize = EXT2_BLOCK_SIZE(fs_param);
1107 if ((min_io == 0) && (psector_size > blocksize))
1108 min_io = psector_size;
1109 if ((opt_io == 0) && min_io)
1111 if ((opt_io == 0) && (psector_size > blocksize))
1112 opt_io = psector_size;
1114 fs_param->s_raid_stride = min_io / blocksize;
1115 fs_param->s_raid_stripe_width = opt_io / blocksize;
1117 rc = blkid_topology_get_alignment_offset(tp);
1119 blkid_free_probe(pr);
1124 static void PRS(int argc, char *argv[])
1130 int inode_ratio = 0;
1132 unsigned long flex_bg_size = 0;
1133 double reserved_ratio = 5.0;
1134 int lsector_size = 0, psector_size = 0;
1135 int show_version_only = 0;
1136 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1138 char * oldpath = getenv("PATH");
1139 char * extended_opts = 0;
1140 const char * fs_type = 0;
1141 const char * usage_types = 0;
1143 blk64_t fs_blocks_count = 0;
1148 int s_opt = -1, r_opt = -1;
1149 char *fs_features = 0;
1152 int pathlen = sizeof(PATH_SET) + 1;
1155 pathlen += strlen(oldpath);
1156 newpath = malloc(pathlen);
1158 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
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 retval = profile_init(default_files, &profile);
1194 retval = profile_set_default(profile, mke2fs_default_profile);
1197 } else if (retval) {
1199 fprintf(stderr, _("Couldn't init profile successfully"
1200 " (error: %ld).\n"), retval);
1204 setbuf(stdout, NULL);
1205 setbuf(stderr, NULL);
1206 add_error_table(&et_ext2_error_table);
1207 add_error_table(&et_prof_error_table);
1208 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1209 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1216 linux_version_code = parse_version_number(ut.release);
1217 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1218 fs_param.s_rev_level = 0;
1221 if (argc && *argv) {
1222 program_name = get_progname(*argv);
1224 /* If called as mkfs.ext3, create a journal inode */
1225 if (!strcmp(program_name, "mkfs.ext3") ||
1226 !strcmp(program_name, "mke3fs"))
1230 while ((c = getopt (argc, argv,
1231 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1234 blocksize = strtol(optarg, &tmp, 0);
1235 b = (blocksize > 0) ? blocksize : -blocksize;
1236 if (b < EXT2_MIN_BLOCK_SIZE ||
1237 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1238 com_err(program_name, 0,
1239 _("invalid block size - %s"), optarg);
1242 if (blocksize > 4096)
1243 fprintf(stderr, _("Warning: blocksize %d not "
1244 "usable on most systems.\n"),
1247 fs_param.s_log_block_size =
1248 int_log2(blocksize >>
1249 EXT2_MIN_BLOCK_LOG_SIZE);
1251 case 'c': /* Check for bad blocks */
1255 size = strtoul(optarg, &tmp, 0);
1256 if (size < EXT2_MIN_BLOCK_SIZE ||
1257 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1258 com_err(program_name, 0,
1259 _("invalid fragment size - %s"),
1263 fs_param.s_log_frag_size =
1264 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1265 fprintf(stderr, _("Warning: fragments not supported. "
1266 "Ignoring -f option\n"));
1269 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1271 com_err(program_name, 0,
1272 _("Illegal number for blocks per group"));
1275 if ((fs_param.s_blocks_per_group % 8) != 0) {
1276 com_err(program_name, 0,
1277 _("blocks per group must be multiple of 8"));
1282 flex_bg_size = strtoul(optarg, &tmp, 0);
1284 com_err(program_name, 0,
1285 _("Illegal number for flex_bg size"));
1288 if (flex_bg_size < 1 ||
1289 (flex_bg_size & (flex_bg_size-1)) != 0) {
1290 com_err(program_name, 0,
1291 _("flex_bg size must be a power of 2"));
1296 inode_ratio = strtoul(optarg, &tmp, 0);
1297 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1298 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1300 com_err(program_name, 0,
1301 _("invalid inode ratio %s (min %d/max %d)"),
1302 optarg, EXT2_MIN_BLOCK_SIZE,
1303 EXT2_MAX_BLOCK_SIZE * 1024);
1308 parse_journal_opts(optarg);
1311 fprintf(stderr, _("Warning: -K option is deprecated and "
1312 "should not be used anymore. Use "
1313 "\'-E nodiscard\' extended option "
1322 bad_blocks_filename = malloc(strlen(optarg)+1);
1323 if (!bad_blocks_filename) {
1324 com_err(program_name, ENOMEM,
1325 _("in malloc for bad_blocks_filename"));
1328 strcpy(bad_blocks_filename, optarg);
1331 reserved_ratio = strtod(optarg, &tmp);
1332 if ( *tmp || reserved_ratio > 50 ||
1333 reserved_ratio < 0) {
1334 com_err(program_name, 0,
1335 _("invalid reserved blocks percent - %s"),
1344 creator_os = optarg;
1350 r_opt = strtoul(optarg, &tmp, 0);
1352 com_err(program_name, 0,
1353 _("bad revision level - %s"), optarg);
1356 fs_param.s_rev_level = r_opt;
1358 case 's': /* deprecated */
1359 s_opt = atoi(optarg);
1362 inode_size = strtoul(optarg, &tmp, 0);
1364 com_err(program_name, 0,
1365 _("invalid inode size - %s"), optarg);
1376 volume_label = optarg;
1382 num_inodes = strtoul(optarg, &tmp, 0);
1384 com_err(program_name, 0,
1385 _("bad num inodes - %s"), optarg);
1390 fs_features = optarg;
1394 extended_opts = optarg;
1403 usage_types = optarg;
1409 /* Print version number and exit */
1410 show_version_only++;
1416 if ((optind == argc) && !show_version_only)
1418 device_name = argv[optind++];
1420 if (!quiet || show_version_only)
1421 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1424 if (show_version_only) {
1425 fprintf(stderr, _("\tUsing %s\n"),
1426 error_message(EXT2_ET_BASE));
1431 * If there's no blocksize specified and there is a journal
1432 * device, use it to figure out the blocksize
1434 if (blocksize <= 0 && journal_device) {
1438 #ifdef CONFIG_TESTIO_DEBUG
1439 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1440 io_ptr = test_io_manager;
1441 test_io_backing_manager = unix_io_manager;
1444 io_ptr = unix_io_manager;
1445 retval = ext2fs_open(journal_device,
1446 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1449 com_err(program_name, retval,
1450 _("while trying to open journal device %s\n"),
1454 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1455 com_err(program_name, 0,
1456 _("Journal dev blocksize (%d) smaller than "
1457 "minimum blocksize %d\n"), jfs->blocksize,
1461 blocksize = jfs->blocksize;
1462 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1463 fs_param.s_log_block_size =
1464 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1468 if (blocksize > sys_page_size) {
1470 com_err(program_name, 0,
1471 _("%d-byte blocks too big for system (max %d)"),
1472 blocksize, sys_page_size);
1475 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1476 "(max %d), forced to continue\n"),
1477 blocksize, sys_page_size);
1479 if (optind < argc) {
1480 fs_blocks_count = parse_num_blocks2(argv[optind++],
1481 fs_param.s_log_block_size);
1482 if (!fs_blocks_count) {
1483 com_err(program_name, 0,
1484 _("invalid blocks '%s' on device '%s'"),
1485 argv[optind - 1], device_name);
1493 check_plausibility(device_name);
1494 check_mount(device_name, force, _("filesystem"));
1496 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1498 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, fs_blocks_count,
1501 fprintf(stderr, _("Failed to parse fs types list\n"));
1505 /* Figure out what features should be enabled */
1508 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1509 tmp = get_string_from_profile(fs_types, "base_features",
1510 "sparse_super,filetype,resize_inode,dir_index");
1511 edit_feature(tmp, &fs_param.s_feature_compat);
1514 for (cpp = fs_types; *cpp; cpp++) {
1516 profile_get_string(profile, "fs_types", *cpp,
1517 "features", "", &tmp);
1519 edit_feature(tmp, &fs_param.s_feature_compat);
1523 tmp = get_string_from_profile(fs_types, "default_features",
1526 edit_feature(fs_features ? fs_features : tmp,
1527 &fs_param.s_feature_compat);
1531 if (noaction && fs_blocks_count) {
1532 dev_size = fs_blocks_count;
1535 retval = ext2fs_get_device_size2(device_name,
1536 EXT2_BLOCK_SIZE(&fs_param),
1539 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1540 com_err(program_name, retval,
1541 _("while trying to determine filesystem size"));
1544 if (!fs_blocks_count) {
1545 if (retval == EXT2_ET_UNIMPLEMENTED) {
1546 com_err(program_name, 0,
1547 _("Couldn't determine device size; you "
1548 "must specify\nthe size of the "
1552 if (dev_size == 0) {
1553 com_err(program_name, 0,
1554 _("Device size reported to be zero. "
1555 "Invalid partition specified, or\n\t"
1556 "partition table wasn't reread "
1557 "after running fdisk, due to\n\t"
1558 "a modified partition being busy "
1559 "and in use. You may need to reboot\n\t"
1560 "to re-read your partition table.\n"
1564 fs_blocks_count = dev_size;
1565 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1566 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1567 EXT2_BLOCK_SIZE(&fs_param))-1));
1569 } else if (!force && (fs_blocks_count > dev_size)) {
1570 com_err(program_name, 0,
1571 _("Filesystem larger than apparent device size."));
1576 * We now need to do a sanity check of fs_blocks_count for
1577 * 32-bit vs 64-bit block number support.
1579 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1580 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1582 fs_param.s_log_block_size = 2;
1584 if ((fs_blocks_count > MAX_32_NUM) &&
1585 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1586 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1587 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1588 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1590 if ((fs_blocks_count > MAX_32_NUM) &&
1591 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1592 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1593 "too big to be expressed\n\t"
1594 "in 32 bits using a blocksize of %d.\n"),
1595 program_name, fs_blocks_count, device_name,
1596 EXT2_BLOCK_SIZE(&fs_param));
1600 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1602 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1603 fs_types[0] = strdup("journal");
1608 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1609 print_str_list(fs_types);
1612 if (r_opt == EXT2_GOOD_OLD_REV &&
1613 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1614 fs_param.s_feature_ro_compat)) {
1615 fprintf(stderr, _("Filesystem features not supported "
1616 "with revision 0 filesystems\n"));
1621 if (r_opt == EXT2_GOOD_OLD_REV) {
1622 fprintf(stderr, _("Sparse superblocks not supported "
1623 "with revision 0 filesystems\n"));
1626 fs_param.s_feature_ro_compat |=
1627 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1628 } else if (s_opt == 0)
1629 fs_param.s_feature_ro_compat &=
1630 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1632 if (journal_size != 0) {
1633 if (r_opt == EXT2_GOOD_OLD_REV) {
1634 fprintf(stderr, _("Journals not supported "
1635 "with revision 0 filesystems\n"));
1638 fs_param.s_feature_compat |=
1639 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1642 if (fs_param.s_feature_incompat &
1643 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1645 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1646 fs_param.s_feature_compat = 0;
1647 fs_param.s_feature_ro_compat = 0;
1650 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1651 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1652 fprintf(stderr, _("The resize_inode and meta_bg features "
1653 "are not compatible.\n"
1654 "They can not be both enabled "
1655 "simultaneously.\n"));
1659 /* Set first meta blockgroup via an environment variable */
1660 /* (this is mostly for debugging purposes) */
1661 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1662 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1663 fs_param.s_first_meta_bg = atoi(tmp);
1665 /* Get the hardware sector sizes, if available */
1666 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1668 com_err(program_name, retval,
1669 _("while trying to determine hardware sector size"));
1672 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1674 com_err(program_name, retval,
1675 _("while trying to determine physical sector size"));
1679 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1680 lsector_size = atoi(tmp);
1681 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1682 psector_size = atoi(tmp);
1684 /* Older kernels may not have physical/logical distinction */
1686 psector_size = lsector_size;
1688 if (blocksize <= 0) {
1689 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1691 if (use_bsize == -1) {
1692 use_bsize = sys_page_size;
1693 if ((linux_version_code < (2*65536 + 6*256)) &&
1697 if (lsector_size && use_bsize < lsector_size)
1698 use_bsize = lsector_size;
1699 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1700 use_bsize = -blocksize;
1701 blocksize = use_bsize;
1702 ext2fs_blocks_count_set(&fs_param,
1703 ext2fs_blocks_count(&fs_param) /
1704 (blocksize / 1024));
1706 if (blocksize < lsector_size) { /* Impossible */
1707 com_err(program_name, EINVAL,
1708 _("while setting blocksize; too small "
1711 } else if ((blocksize < psector_size) &&
1712 (psector_size <= sys_page_size)) { /* Suboptimal */
1713 fprintf(stderr, _("Warning: specified blocksize %d is "
1714 "less than device physical sectorsize %d\n"),
1715 blocksize, psector_size);
1719 if (inode_ratio == 0) {
1720 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1722 if (inode_ratio < blocksize)
1723 inode_ratio = blocksize;
1726 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1727 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1729 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1730 retval = get_device_geometry(device_name, &fs_param, psector_size);
1733 _("warning: Unable to get device geometry for %s\n"),
1735 } else if (retval) {
1736 printf(_("%s alignment is offset by %lu bytes.\n"),
1737 device_name, retval);
1738 printf(_("This may result in very poor performance, "
1739 "(re)-partitioning suggested.\n"));
1743 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1745 lazy_itable_init = 0;
1746 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1747 lazy_itable_init = 1;
1749 lazy_itable_init = get_bool_from_profile(fs_types,
1752 discard = get_bool_from_profile(fs_types, "discard" , discard);
1754 /* Get options from profile */
1755 for (cpp = fs_types; *cpp; cpp++) {
1757 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1759 parse_extended_opts(&fs_param, tmp);
1764 parse_extended_opts(&fs_param, extended_opts);
1766 /* Since sparse_super is the default, we would only have a problem
1767 * here if it was explicitly disabled.
1769 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1770 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1771 com_err(program_name, 0,
1772 _("reserved online resize blocks not supported "
1773 "on non-sparse filesystem"));
1777 if (fs_param.s_blocks_per_group) {
1778 if (fs_param.s_blocks_per_group < 256 ||
1779 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1780 com_err(program_name, 0,
1781 _("blocks per group count out of range"));
1786 if (inode_size == 0)
1787 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1788 if (!flex_bg_size && (fs_param.s_feature_incompat &
1789 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1790 flex_bg_size = get_int_from_profile(fs_types,
1791 "flex_bg_size", 16);
1793 if (!(fs_param.s_feature_incompat &
1794 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1795 com_err(program_name, 0,
1796 _("Flex_bg feature not enabled, so "
1797 "flex_bg size may not be specified"));
1800 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1803 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1804 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1805 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1806 inode_size & (inode_size - 1)) {
1807 com_err(program_name, 0,
1808 _("invalid inode size %d (min %d/max %d)"),
1809 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1813 fs_param.s_inode_size = inode_size;
1816 /* Make sure number of inodes specified will fit in 32 bits */
1817 if (num_inodes == 0) {
1818 unsigned long long n;
1819 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1820 if (n > MAX_32_NUM) {
1821 if (fs_param.s_feature_incompat &
1822 EXT4_FEATURE_INCOMPAT_64BIT)
1823 num_inodes = MAX_32_NUM;
1825 com_err(program_name, 0,
1826 _("too many inodes (%llu), raise"
1827 "inode ratio?"), n);
1831 } else if (num_inodes > MAX_32_NUM) {
1832 com_err(program_name, 0,
1833 _("too many inodes (%llu), specify < 2^32 inodes"),
1838 * Calculate number of inodes based on the inode ratio
1840 fs_param.s_inodes_count = num_inodes ? num_inodes :
1841 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1843 if ((((long long)fs_param.s_inodes_count) *
1844 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1845 ((ext2fs_blocks_count(&fs_param)) *
1846 EXT2_BLOCK_SIZE(&fs_param))) {
1847 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1848 "(%u) too big for a\n\t"
1849 "filesystem with %llu blocks, "
1850 "specify higher inode_ratio (-i)\n\t"
1851 "or lower inode count (-N).\n"),
1852 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1853 fs_param.s_inodes_count,
1854 (unsigned long long) ext2fs_blocks_count(&fs_param));
1859 * Calculate number of blocks to reserve
1861 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1862 ext2fs_blocks_count(&fs_param) / 100.0);
1865 static int should_do_undo(const char *name)
1870 struct ext2_super_block super;
1871 io_manager manager = unix_io_manager;
1872 int csum_flag, force_undo;
1874 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1875 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1876 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1877 if (!force_undo && (!csum_flag || !lazy_itable_init))
1880 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1883 * We don't handle error cases instead we
1884 * declare that the file system doesn't exist
1885 * and let the rest of mke2fs take care of
1892 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1893 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1899 #if defined(WORDS_BIGENDIAN)
1900 s_magic = ext2fs_swab16(super.s_magic);
1902 s_magic = super.s_magic;
1905 if (s_magic == EXT2_SUPER_MAGIC)
1909 io_channel_close(channel);
1916 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1918 errcode_t retval = ENOMEM;
1919 char *tdb_dir, *tdb_file = NULL;
1920 char *device_name, *tmp_name;
1923 * Configuration via a conf file would be
1926 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1928 profile_get_string(profile, "defaults",
1929 "undo_dir", 0, "/var/lib/e2fsprogs",
1932 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1933 access(tdb_dir, W_OK))
1936 tmp_name = strdup(name);
1939 device_name = basename(tmp_name);
1940 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1945 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1948 if (!access(tdb_file, F_OK)) {
1949 if (unlink(tdb_file) < 0) {
1955 set_undo_io_backing_manager(*io_ptr);
1956 *io_ptr = undo_io_manager;
1957 retval = set_undo_io_backup_file(tdb_file);
1960 printf(_("Overwriting existing filesystem; this can be undone "
1961 "using the command:\n"
1962 " e2undo %s %s\n\n"), tdb_file, name);
1969 com_err(program_name, retval,
1970 _("while trying to setup undo file\n"));
1974 int main (int argc, char *argv[])
1976 errcode_t retval = 0;
1978 badblocks_list bb_list = 0;
1979 unsigned int journal_blocks;
1985 char tdb_string[40];
1987 int itable_zeroed = 0;
1990 setlocale(LC_MESSAGES, "");
1991 setlocale(LC_CTYPE, "");
1992 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1993 textdomain(NLS_CAT_NAME);
1997 #ifdef CONFIG_TESTIO_DEBUG
1998 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1999 io_ptr = test_io_manager;
2000 test_io_backing_manager = unix_io_manager;
2003 io_ptr = unix_io_manager;
2005 if (should_do_undo(device_name)) {
2006 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2012 * Initialize the superblock....
2014 flags = EXT2_FLAG_EXCLUSIVE;
2015 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2018 flags |= EXT2_FLAG_64BITS;
2020 * By default, we print how many inode tables or block groups
2021 * or whatever we've written so far. The quiet flag disables
2022 * this, along with a lot of other output.
2025 flags |= EXT2_FLAG_PRINT_PROGRESS;
2026 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2028 com_err(device_name, retval, _("while setting up superblock"));
2032 /* Can't undo discard ... */
2033 if (discard && (io_ptr != undo_io_manager)) {
2034 blk64_t blocks = ext2fs_blocks_count(fs->super);
2036 printf(_("Calling BLKDISCARD from 0 to %llu... "),
2037 (unsigned long long) blocks);
2038 retval = io_channel_discard(fs->io, 0, blocks, fs->blocksize);
2041 printf(_("failed (%s)\n"),
2042 error_message(retval));
2044 printf(_("succeeded\n"));
2047 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2049 printf(_("Discard succeeded and will return 0s "
2050 " - skipping inode table wipe\n"));
2051 lazy_itable_init = 1;
2056 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2057 32768 : fs->blocksize * 8);
2058 io_channel_set_options(fs->io, tdb_string);
2060 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2061 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2063 if ((fs_param.s_feature_incompat &
2064 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2065 (fs_param.s_feature_ro_compat &
2066 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2067 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2068 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2069 fs->super->s_kbytes_written = 1;
2072 * Wipe out the old on-disk superblock
2075 zap_sector(fs, 2, 6);
2078 * Parse or generate a UUID for the filesystem
2081 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2082 com_err(device_name, 0, "could not parse UUID: %s\n",
2087 uuid_generate(fs->super->s_uuid);
2090 * Initialize the directory index variables
2092 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2094 hash_alg = e2p_string2hash(hash_alg_str);
2096 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2098 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2101 * Add "jitter" to the superblock's check interval so that we
2102 * don't check all the filesystems at the same time. We use a
2103 * kludgy hack of using the UUID to derive a random jitter value.
2105 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2106 val += fs->super->s_uuid[i];
2107 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2110 * Override the creator OS, if applicable
2112 if (creator_os && !set_os(fs->super, creator_os)) {
2113 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2118 * For the Hurd, we will turn off filetype since it doesn't
2121 if (fs->super->s_creator_os == EXT2_OS_HURD)
2122 fs->super->s_feature_incompat &=
2123 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2126 * Set the volume label...
2129 memset(fs->super->s_volume_name, 0,
2130 sizeof(fs->super->s_volume_name));
2131 strncpy(fs->super->s_volume_name, volume_label,
2132 sizeof(fs->super->s_volume_name));
2136 * Set the last mount directory
2139 memset(fs->super->s_last_mounted, 0,
2140 sizeof(fs->super->s_last_mounted));
2141 strncpy(fs->super->s_last_mounted, mount_dir,
2142 sizeof(fs->super->s_last_mounted));
2145 if (!quiet || noaction)
2151 if (fs->super->s_feature_incompat &
2152 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2153 create_journal_dev(fs);
2154 exit(ext2fs_close(fs) ? 1 : 0);
2157 if (bad_blocks_filename)
2158 read_bb_file(fs, &bb_list, bad_blocks_filename);
2160 test_disk(fs, &bb_list);
2162 handle_bad_blocks(fs, bb_list);
2163 fs->stride = fs_stride = fs->super->s_raid_stride;
2165 printf(_("Allocating group tables: "));
2166 retval = ext2fs_allocate_tables(fs);
2168 com_err(program_name, retval,
2169 _("while trying to allocate filesystem tables"));
2173 printf(_("done \n"));
2175 fs->super->s_state |= EXT2_ERROR_FS;
2176 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2178 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2179 blk64_t rsv = 65536 / fs->blocksize;
2180 blk64_t blocks = ext2fs_blocks_count(fs->super);
2184 #ifdef ZAP_BOOTBLOCK
2185 zap_sector(fs, 0, 2);
2189 * Wipe out any old MD RAID (or other) metadata at the end
2190 * of the device. This will also verify that the device is
2191 * as large as we think. Be careful with very small devices.
2193 start = (blocks & ~(rsv - 1));
2197 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2201 com_err(program_name, retval,
2202 _("while zeroing block %llu at end of filesystem"),
2205 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2206 create_root_dir(fs);
2207 create_lost_and_found(fs);
2209 create_bad_block_inode(fs, bb_list);
2210 if (fs->super->s_feature_compat &
2211 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2212 retval = ext2fs_create_resize_inode(fs);
2214 com_err("ext2fs_create_resize_inode", retval,
2215 _("while reserving blocks for online resize"));
2221 if (journal_device) {
2225 check_plausibility(journal_device);
2226 check_mount(journal_device, force, _("journal"));
2228 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2229 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2230 fs->blocksize, unix_io_manager, &jfs);
2232 com_err(program_name, retval,
2233 _("while trying to open journal device %s\n"),
2238 printf(_("Adding journal to device %s: "),
2242 retval = ext2fs_add_journal_device(fs, jfs);
2244 com_err (program_name, retval,
2245 _("\n\twhile trying to add journal to device %s"),
2250 printf(_("done\n"));
2252 free(journal_device);
2253 } else if ((journal_size) ||
2254 (fs_param.s_feature_compat &
2255 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2256 journal_blocks = figure_journal_size(journal_size, fs);
2259 printf(_("Skipping journal creation in super-only mode\n"));
2260 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2264 if (!journal_blocks) {
2265 fs->super->s_feature_compat &=
2266 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2270 printf(_("Creating journal (%u blocks): "),
2274 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2277 com_err (program_name, retval,
2278 _("\n\twhile trying to create journal"));
2282 printf(_("done\n"));
2287 printf(_("Writing superblocks and "
2288 "filesystem accounting information: "));
2289 retval = ext2fs_flush(fs);
2292 _("\nWarning, had trouble writing out superblocks."));
2295 printf(_("done\n\n"));
2296 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2297 print_check_message(fs);
2299 val = ext2fs_close(fs);
2300 remove_error_table(&et_ext2_error_table);
2301 remove_error_table(&et_prof_error_table);
2302 profile_release(profile);
2303 for (i=0; fs_types[i]; i++)
2306 return (retval || val) ? 1 : 0;