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 #define DISCARD_STEP_MB (2048)
77 extern int isatty(int);
78 extern FILE *fpopen(const char *cmd, const char *mode);
80 const char * program_name = "mke2fs";
81 const char * device_name /* = NULL */;
83 /* Command line options */
88 int discard = 1; /* attempt to discard device before fs creation */
94 char *bad_blocks_filename;
97 struct ext2_super_block fs_param;
102 char *journal_device;
103 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
108 int sys_page_size = 4096;
109 int linux_version_code = 0;
111 static void usage(void)
113 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
114 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
115 "[-J journal-options]\n"
116 "\t[-G meta group size] [-N number-of-inodes]\n"
117 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
118 "\t[-g blocks-per-group] [-L volume-label] "
119 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
120 "[-r fs-revision] [-E extended-option[,...]]\n"
121 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
126 static int int_log2(unsigned long long arg)
138 static int int_log10(unsigned long long arg)
147 static int parse_version_number(const char *s)
149 int major, minor, rev;
155 major = strtol(cp, &endptr, 10);
156 if (cp == endptr || *endptr != '.')
159 minor = strtol(cp, &endptr, 10);
160 if (cp == endptr || *endptr != '.')
163 rev = strtol(cp, &endptr, 10);
166 return ((((major * 256) + minor) * 256) + rev);
170 * Helper function for read_bb_file and test_disk
172 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
174 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
179 * Reads the bad blocks list from a file
181 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
182 const char *bad_blocks_file)
187 f = fopen(bad_blocks_file, "r");
189 com_err("read_bad_blocks_file", errno,
190 _("while trying to open %s"), bad_blocks_file);
193 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
196 com_err("ext2fs_read_bb_FILE", retval,
197 _("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 %llu", fs->blocksize,
212 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
213 fs->device_name, ext2fs_blocks_count(fs->super)-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"));
231 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
235 unsigned must_be_good;
237 badblocks_iterate bb_iter;
247 * The primary superblock and group descriptors *must* be
248 * good; if not, abort.
250 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
251 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
252 if (ext2fs_badblocks_list_test(bb_list, i)) {
253 fprintf(stderr, _("Block %d in primary "
254 "superblock/group descriptor area bad.\n"), i);
255 fprintf(stderr, _("Blocks %u through %u must be good "
256 "in order to build a filesystem.\n"),
257 fs->super->s_first_data_block, must_be_good);
258 fputs(_("Aborting....\n"), stderr);
264 * See if any of the bad blocks are showing up in the backup
265 * superblocks and/or group descriptors. If so, issue a
266 * warning and adjust the block counts appropriately.
268 group_block = fs->super->s_first_data_block +
269 fs->super->s_blocks_per_group;
271 for (i = 1; i < fs->group_desc_count; i++) {
273 for (j=0; j < fs->desc_blocks+1; j++) {
274 if (ext2fs_badblocks_list_test(bb_list,
278 _("Warning: the backup superblock/group descriptors at block %u contain\n"
282 group = ext2fs_group_of_blk2(fs, group_block+j);
283 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
284 ext2fs_group_desc_csum_set(fs, group);
285 ext2fs_free_blocks_count_add(fs->super, 1);
288 group_block += fs->super->s_blocks_per_group;
292 * Mark all the bad blocks as used...
294 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
296 com_err("ext2fs_badblocks_list_iterate_begin", retval,
297 _("while marking bad blocks as used"));
300 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
301 ext2fs_mark_block_bitmap2(fs->block_map, blk);
302 ext2fs_badblocks_list_iterate_end(bb_iter);
305 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
311 struct ext2fs_numeric_progress_struct progress;
313 ext2fs_numeric_progress_init(fs, &progress,
314 _("Writing inode tables: "),
315 fs->group_desc_count);
317 for (i = 0; i < fs->group_desc_count; i++) {
318 ext2fs_numeric_progress_update(fs, &progress, i);
320 blk = ext2fs_inode_table_loc(fs, i);
321 num = fs->inode_blocks_per_group;
324 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
325 ext2fs_bg_itable_unused(fs, i)) *
326 EXT2_INODE_SIZE(fs->super),
327 EXT2_BLOCK_SIZE(fs->super));
328 if (!lazy_flag || itable_zeroed) {
329 /* The kernel doesn't need to zero the itable blocks */
330 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
331 ext2fs_group_desc_csum_set(fs, i);
333 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
335 fprintf(stderr, _("\nCould not write %d "
336 "blocks in inode table starting at %llu: %s\n"),
337 num, blk, error_message(retval));
341 if (sync_kludge == 1)
343 else if ((i % sync_kludge) == 0)
347 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
348 ext2fs_numeric_progress_close(fs, &progress,
352 static void create_root_dir(ext2_filsys fs)
355 struct ext2_inode inode;
358 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
360 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
364 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
366 com_err("ext2fs_read_inode", retval,
367 _("while reading root inode"));
372 ext2fs_set_i_uid_high(inode, uid >> 16);
376 ext2fs_set_i_gid_high(inode, gid >> 16);
378 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
380 com_err("ext2fs_write_inode", retval,
381 _("while setting root inode ownership"));
387 static void create_lost_and_found(ext2_filsys fs)
389 unsigned int lpf_size = 0;
392 const char *name = "lost+found";
396 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
398 com_err("ext2fs_mkdir", retval,
399 _("while creating /lost+found"));
403 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
405 com_err("ext2_lookup", retval,
406 _("while looking up /lost+found"));
410 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
411 /* Ensure that lost+found is at least 2 blocks, so we always
412 * test large empty blocks for big-block filesystems. */
413 if ((lpf_size += fs->blocksize) >= 16*1024 &&
414 lpf_size >= 2 * fs->blocksize)
416 retval = ext2fs_expand_dir(fs, ino);
418 com_err("ext2fs_expand_dir", retval,
419 _("while expanding /lost+found"));
425 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
429 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
430 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
431 retval = ext2fs_update_bb_inode(fs, bb_list);
433 com_err("ext2fs_update_bb_inode", retval,
434 _("while setting bad block inode"));
440 static void reserve_inodes(ext2_filsys fs)
444 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
445 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
446 ext2fs_mark_ib_dirty(fs);
449 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
450 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
451 #define BSD_LABEL_OFFSET 64
453 static void zap_sector(ext2_filsys fs, int sect, int nsect)
459 buf = malloc(512*nsect);
461 printf(_("Out of memory erasing sectors %d-%d\n"),
462 sect, sect + nsect - 1);
467 /* Check for a BSD disklabel, and don't erase it if so */
468 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
471 _("Warning: could not read block 0: %s\n"),
472 error_message(retval));
474 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
475 if ((*magic == BSD_DISKMAGIC) ||
476 (*magic == BSD_MAGICDISK))
481 memset(buf, 0, 512*nsect);
482 io_channel_set_blksize(fs->io, 512);
483 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
484 io_channel_set_blksize(fs->io, fs->blocksize);
487 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
488 sect, error_message(retval));
491 static void create_journal_dev(ext2_filsys fs)
493 struct ext2fs_numeric_progress_struct progress;
496 blk64_t blk, err_blk;
497 int c, count, err_count;
499 retval = ext2fs_create_journal_superblock(fs,
500 ext2fs_blocks_count(fs->super), 0, &buf);
502 com_err("create_journal_dev", retval,
503 _("while initializing journal superblock"));
506 ext2fs_numeric_progress_init(fs, &progress,
507 _("Zeroing journal device: "),
508 ext2fs_blocks_count(fs->super));
510 count = ext2fs_blocks_count(fs->super);
516 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
518 com_err("create_journal_dev", retval,
519 _("while zeroing journal device "
520 "(block %llu, count %d)"),
526 ext2fs_numeric_progress_update(fs, &progress, blk);
528 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
530 retval = io_channel_write_blk64(fs->io,
531 fs->super->s_first_data_block+1,
534 com_err("create_journal_dev", retval,
535 _("while writing journal superblock"));
538 ext2fs_numeric_progress_close(fs, &progress, NULL);
541 static void show_stats(ext2_filsys fs)
543 struct ext2_super_block *s = fs->super;
550 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
551 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
552 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
554 memset(buf, 0, sizeof(buf));
555 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
556 printf(_("Filesystem label=%s\n"), buf);
557 os = e2p_os2string(fs->super->s_creator_os);
559 printf(_("OS type: %s\n"), os);
561 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
562 s->s_log_block_size);
563 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
564 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
565 printf(_("Cluster size=%u (log=%u)\n"),
566 fs->clustersize, s->s_log_cluster_size);
568 printf(_("Fragment size=%u (log=%u)\n"), fs->clustersize,
569 s->s_log_cluster_size);
570 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
571 s->s_raid_stride, s->s_raid_stripe_width);
572 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
573 ext2fs_blocks_count(s));
574 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
575 ext2fs_r_blocks_count(s),
576 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
577 printf(_("First data block=%u\n"), s->s_first_data_block);
578 if (s->s_reserved_gdt_blocks)
579 printf(_("Maximum filesystem blocks=%lu\n"),
580 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
581 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
582 if (fs->group_desc_count > 1)
583 printf(_("%u block groups\n"), fs->group_desc_count);
585 printf(_("%u block group\n"), fs->group_desc_count);
586 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
587 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
588 printf(_("%u blocks per group, %u clusters per group\n"),
589 s->s_blocks_per_group, s->s_clusters_per_group);
591 printf(_("%u blocks per group, %u fragments per group\n"),
592 s->s_blocks_per_group, s->s_clusters_per_group);
593 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
595 if (fs->group_desc_count == 1) {
600 printf(_("Superblock backups stored on blocks: "));
601 group_block = s->s_first_data_block;
603 for (i = 1; i < fs->group_desc_count; i++) {
604 group_block += s->s_blocks_per_group;
605 if (!ext2fs_bg_has_super(fs, i))
609 need = int_log10(group_block) + 2;
610 if (need > col_left) {
615 printf("%llu", group_block);
621 * Set the S_CREATOR_OS field. Return true if OS is known,
624 static int set_os(struct ext2_super_block *sb, char *os)
627 sb->s_creator_os = atoi (os);
628 else if (strcasecmp(os, "linux") == 0)
629 sb->s_creator_os = EXT2_OS_LINUX;
630 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
631 sb->s_creator_os = EXT2_OS_HURD;
632 else if (strcasecmp(os, "freebsd") == 0)
633 sb->s_creator_os = EXT2_OS_FREEBSD;
634 else if (strcasecmp(os, "lites") == 0)
635 sb->s_creator_os = EXT2_OS_LITES;
641 #define PATH_SET "PATH=/sbin"
643 static void parse_extended_opts(struct ext2_super_block *param,
646 char *buf, *token, *next, *p, *arg, *badopt = 0;
654 _("Couldn't allocate memory to parse options!\n"));
658 for (token = buf; token && *token; token = next) {
659 p = strchr(token, ',');
665 arg = strchr(token, '=');
670 if (strcmp(token, "stride") == 0) {
676 param->s_raid_stride = strtoul(arg, &p, 0);
677 if (*p || (param->s_raid_stride == 0)) {
679 _("Invalid stride parameter: %s\n"),
684 } else if (strcmp(token, "stripe-width") == 0 ||
685 strcmp(token, "stripe_width") == 0) {
691 param->s_raid_stripe_width = strtoul(arg, &p, 0);
692 if (*p || (param->s_raid_stripe_width == 0)) {
694 _("Invalid stripe-width parameter: %s\n"),
699 } else if (!strcmp(token, "resize")) {
701 unsigned long bpg, rsv_groups;
702 unsigned long group_desc_count, desc_blocks;
703 unsigned int gdpb, blocksize;
712 resize = parse_num_blocks2(arg,
713 param->s_log_block_size);
717 _("Invalid resize parameter: %s\n"),
722 if (resize <= ext2fs_blocks_count(param)) {
724 _("The resize maximum must be greater "
725 "than the filesystem size.\n"));
730 blocksize = EXT2_BLOCK_SIZE(param);
731 bpg = param->s_blocks_per_group;
734 gdpb = EXT2_DESC_PER_BLOCK(param);
735 group_desc_count = (__u32) ext2fs_div64_ceil(
736 ext2fs_blocks_count(param), bpg);
737 desc_blocks = (group_desc_count +
739 rsv_groups = ext2fs_div64_ceil(resize, bpg);
740 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
742 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
743 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
746 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
748 _("On-line resizing not supported with revision 0 filesystems\n"));
752 param->s_feature_compat |=
753 EXT2_FEATURE_COMPAT_RESIZE_INODE;
755 param->s_reserved_gdt_blocks = rsv_gdb;
757 } else if (!strcmp(token, "test_fs")) {
758 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
759 } else if (!strcmp(token, "lazy_itable_init")) {
761 lazy_itable_init = strtoul(arg, &p, 0);
763 lazy_itable_init = 1;
764 } else if (!strcmp(token, "discard")) {
766 } else if (!strcmp(token, "nodiscard")) {
774 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
775 "Extended options are separated by commas, "
776 "and may take an argument which\n"
777 "\tis set off by an equals ('=') sign.\n\n"
778 "Valid extended options are:\n"
779 "\tstride=<RAID per-disk data chunk in blocks>\n"
780 "\tstripe-width=<RAID stride * data disks in blocks>\n"
781 "\tresize=<resize maximum size in blocks>\n"
782 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
786 badopt ? badopt : "");
790 if (param->s_raid_stride &&
791 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
792 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
793 "multiple of stride %u.\n\n"),
794 param->s_raid_stripe_width, param->s_raid_stride);
799 static __u32 ok_features[3] = {
801 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
802 EXT2_FEATURE_COMPAT_RESIZE_INODE |
803 EXT2_FEATURE_COMPAT_DIR_INDEX |
804 EXT2_FEATURE_COMPAT_EXT_ATTR,
806 EXT2_FEATURE_INCOMPAT_FILETYPE|
807 EXT3_FEATURE_INCOMPAT_EXTENTS|
808 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
809 EXT2_FEATURE_INCOMPAT_META_BG|
810 EXT4_FEATURE_INCOMPAT_FLEX_BG|
811 EXT4_FEATURE_INCOMPAT_64BIT,
813 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
814 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
815 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
816 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
817 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
818 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
822 static void syntax_err_report(const char *filename, long err, int line_num)
825 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
826 filename, line_num, error_message(err));
830 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
832 static void edit_feature(const char *str, __u32 *compat_array)
837 if (e2p_edit_feature(str, compat_array, ok_features)) {
838 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
844 static void edit_mntopts(const char *str, __u32 *mntopts)
849 if (e2p_edit_mntopts(str, mntopts, ~0)) {
850 fprintf(stderr, _("Invalid mount option set: %s\n"),
862 static errcode_t init_list(struct str_list *sl)
866 sl->list = malloc((sl->max+1) * sizeof(char *));
873 static errcode_t push_string(struct str_list *sl, const char *str)
877 if (sl->num >= sl->max) {
879 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
884 sl->list[sl->num] = malloc(strlen(str)+1);
885 if (sl->list[sl->num] == 0)
887 strcpy(sl->list[sl->num], str);
889 sl->list[sl->num] = 0;
893 static void print_str_list(char **list)
897 for (cpp = list; *cpp; cpp++) {
898 printf("'%s'", *cpp);
906 * Return TRUE if the profile has the given subsection
908 static int profile_has_subsection(profile_t profile, const char *section,
909 const char *subsection)
912 const char *names[4];
917 names[1] = subsection;
920 if (profile_iterator_create(profile, names,
921 PROFILE_ITER_LIST_SECTION |
922 PROFILE_ITER_RELATIONS_ONLY, &state))
925 if ((profile_iterator(&state, &name, 0) == 0) && name) {
930 profile_iterator_free(&state);
934 static char **parse_fs_type(const char *fs_type,
935 const char *usage_types,
936 struct ext2_super_block *fs_param,
937 blk64_t fs_blocks_count,
940 const char *ext_type = 0;
942 char *profile_type = 0;
944 const char *size_type;
945 struct str_list list;
946 unsigned long long meg;
949 if (init_list(&list))
952 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
953 !strcasecmp(creator_os, "hurd")))
960 else if (!strcmp(program_name, "mke3fs"))
963 ext_type = strrchr(progname, '/');
969 if (!strncmp(ext_type, "mkfs.", 5)) {
971 if (ext_type[0] == 0)
978 profile_get_string(profile, "defaults", "fs_type", 0,
979 "ext2", &profile_type);
980 ext_type = profile_type;
981 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
986 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
987 strcmp(ext_type, "ext2")) {
988 printf(_("\nYour mke2fs.conf file does not define the "
989 "%s filesystem type.\n"), ext_type);
990 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
991 !strcmp(ext_type, "ext4dev")) {
992 printf(_("You probably need to install an updated "
993 "mke2fs.conf file.\n\n"));
996 printf(_("Aborting...\n"));
1001 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1002 if (fs_blocks_count < 3 * meg)
1003 size_type = "floppy";
1004 else if (fs_blocks_count < 512 * meg)
1005 size_type = "small";
1006 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1007 size_type = "default";
1008 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1014 usage_types = size_type;
1016 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1022 strcpy(parse_str, usage_types);
1027 push_string(&list, ext_type);
1030 t = strchr(cp, ',');
1035 if (profile_has_subsection(profile, "fs_types", cp))
1036 push_string(&list, cp);
1037 else if (strcmp(cp, "default") != 0)
1039 _("\nWarning: the fs_type %s is not "
1040 "defined in mke2fs.conf\n\n"),
1053 push_string(&list, "hurd");
1057 static char *get_string_from_profile(char **fs_types, const char *opt,
1058 const char *def_val)
1063 for (i=0; fs_types[i]; i++);
1064 for (i-=1; i >=0 ; i--) {
1065 profile_get_string(profile, "fs_types", fs_types[i],
1070 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1074 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1079 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1080 for (cpp = fs_types; *cpp; cpp++)
1081 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1085 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1090 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1091 for (cpp = fs_types; *cpp; cpp++)
1092 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1096 extern const char *mke2fs_default_profile;
1097 static const char *default_files[] = { "<default>", 0 };
1099 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1101 * Sets the geometry of a device (stripe/stride), and returns the
1102 * device's alignment offset, if any, or a negative error.
1104 static int get_device_geometry(const char *file,
1105 struct ext2_super_block *fs_param,
1112 unsigned long min_io;
1113 unsigned long opt_io;
1114 struct stat statbuf;
1116 /* Nothing to do for a regular file */
1117 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1120 pr = blkid_new_probe_from_filename(file);
1124 tp = blkid_probe_get_topology(pr);
1128 min_io = blkid_topology_get_minimum_io_size(tp);
1129 opt_io = blkid_topology_get_optimal_io_size(tp);
1130 blocksize = EXT2_BLOCK_SIZE(fs_param);
1131 if ((min_io == 0) && (psector_size > blocksize))
1132 min_io = psector_size;
1133 if ((opt_io == 0) && min_io)
1135 if ((opt_io == 0) && (psector_size > blocksize))
1136 opt_io = psector_size;
1138 fs_param->s_raid_stride = min_io / blocksize;
1139 fs_param->s_raid_stripe_width = opt_io / blocksize;
1141 rc = blkid_topology_get_alignment_offset(tp);
1143 blkid_free_probe(pr);
1148 static void PRS(int argc, char *argv[])
1154 int inode_ratio = 0;
1156 unsigned long flex_bg_size = 0;
1157 double reserved_ratio = 5.0;
1158 int lsector_size = 0, psector_size = 0;
1159 int show_version_only = 0;
1160 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1162 char * oldpath = getenv("PATH");
1163 char * extended_opts = 0;
1164 const char * fs_type = 0;
1165 const char * usage_types = 0;
1167 blk64_t fs_blocks_count = 0;
1172 int s_opt = -1, r_opt = -1;
1173 char *fs_features = 0;
1176 int pathlen = sizeof(PATH_SET) + 1;
1179 pathlen += strlen(oldpath);
1180 newpath = malloc(pathlen);
1182 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1185 strcpy(newpath, PATH_SET);
1187 /* Update our PATH to include /sbin */
1189 strcat (newpath, ":");
1190 strcat (newpath, oldpath);
1194 tmp = getenv("MKE2FS_SYNC");
1196 sync_kludge = atoi(tmp);
1198 /* Determine the system page size if possible */
1200 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1201 #define _SC_PAGESIZE _SC_PAGE_SIZE
1204 sysval = sysconf(_SC_PAGESIZE);
1206 sys_page_size = sysval;
1207 #endif /* _SC_PAGESIZE */
1208 #endif /* HAVE_SYSCONF */
1210 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1212 profile_set_syntax_err_cb(syntax_err_report);
1213 retval = profile_init(config_fn, &profile);
1214 if (retval == ENOENT) {
1215 retval = profile_init(default_files, &profile);
1218 retval = profile_set_default(profile, mke2fs_default_profile);
1221 } else if (retval) {
1223 fprintf(stderr, _("Couldn't init profile successfully"
1224 " (error: %ld).\n"), retval);
1228 setbuf(stdout, NULL);
1229 setbuf(stderr, NULL);
1230 add_error_table(&et_ext2_error_table);
1231 add_error_table(&et_prof_error_table);
1232 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1233 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1240 linux_version_code = parse_version_number(ut.release);
1241 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1242 fs_param.s_rev_level = 0;
1245 if (argc && *argv) {
1246 program_name = get_progname(*argv);
1248 /* If called as mkfs.ext3, create a journal inode */
1249 if (!strcmp(program_name, "mkfs.ext3") ||
1250 !strcmp(program_name, "mke3fs"))
1254 while ((c = getopt (argc, argv,
1255 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1258 blocksize = strtol(optarg, &tmp, 0);
1259 b = (blocksize > 0) ? blocksize : -blocksize;
1260 if (b < EXT2_MIN_BLOCK_SIZE ||
1261 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1262 com_err(program_name, 0,
1263 _("invalid block size - %s"), optarg);
1266 if (blocksize > 4096)
1267 fprintf(stderr, _("Warning: blocksize %d not "
1268 "usable on most systems.\n"),
1271 fs_param.s_log_block_size =
1272 int_log2(blocksize >>
1273 EXT2_MIN_BLOCK_LOG_SIZE);
1275 case 'c': /* Check for bad blocks */
1279 size = strtoul(optarg, &tmp, 0);
1280 if (size < EXT2_MIN_BLOCK_SIZE ||
1281 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1282 com_err(program_name, 0,
1283 _("invalid fragment size - %s"),
1287 fprintf(stderr, _("Warning: fragments not supported. "
1288 "Ignoring -f option\n"));
1291 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1293 com_err(program_name, 0,
1294 _("Illegal number for blocks per group"));
1297 if ((fs_param.s_blocks_per_group % 8) != 0) {
1298 com_err(program_name, 0,
1299 _("blocks per group must be multiple of 8"));
1304 flex_bg_size = strtoul(optarg, &tmp, 0);
1306 com_err(program_name, 0,
1307 _("Illegal number for flex_bg size"));
1310 if (flex_bg_size < 1 ||
1311 (flex_bg_size & (flex_bg_size-1)) != 0) {
1312 com_err(program_name, 0,
1313 _("flex_bg size must be a power of 2"));
1318 inode_ratio = strtoul(optarg, &tmp, 0);
1319 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1320 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1322 com_err(program_name, 0,
1323 _("invalid inode ratio %s (min %d/max %d)"),
1324 optarg, EXT2_MIN_BLOCK_SIZE,
1325 EXT2_MAX_BLOCK_SIZE * 1024);
1330 parse_journal_opts(optarg);
1333 fprintf(stderr, _("Warning: -K option is deprecated and "
1334 "should not be used anymore. Use "
1335 "\'-E nodiscard\' extended option "
1344 bad_blocks_filename = malloc(strlen(optarg)+1);
1345 if (!bad_blocks_filename) {
1346 com_err(program_name, ENOMEM,
1347 _("in malloc for bad_blocks_filename"));
1350 strcpy(bad_blocks_filename, optarg);
1353 reserved_ratio = strtod(optarg, &tmp);
1354 if ( *tmp || reserved_ratio > 50 ||
1355 reserved_ratio < 0) {
1356 com_err(program_name, 0,
1357 _("invalid reserved blocks percent - %s"),
1366 creator_os = optarg;
1372 r_opt = strtoul(optarg, &tmp, 0);
1374 com_err(program_name, 0,
1375 _("bad revision level - %s"), optarg);
1378 fs_param.s_rev_level = r_opt;
1380 case 's': /* deprecated */
1381 s_opt = atoi(optarg);
1384 inode_size = strtoul(optarg, &tmp, 0);
1386 com_err(program_name, 0,
1387 _("invalid inode size - %s"), optarg);
1398 volume_label = optarg;
1404 num_inodes = strtoul(optarg, &tmp, 0);
1406 com_err(program_name, 0,
1407 _("bad num inodes - %s"), optarg);
1412 fs_features = optarg;
1416 extended_opts = optarg;
1425 usage_types = optarg;
1431 /* Print version number and exit */
1432 show_version_only++;
1438 if ((optind == argc) && !show_version_only)
1440 device_name = argv[optind++];
1442 if (!quiet || show_version_only)
1443 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1446 if (show_version_only) {
1447 fprintf(stderr, _("\tUsing %s\n"),
1448 error_message(EXT2_ET_BASE));
1453 * If there's no blocksize specified and there is a journal
1454 * device, use it to figure out the blocksize
1456 if (blocksize <= 0 && journal_device) {
1460 #ifdef CONFIG_TESTIO_DEBUG
1461 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1462 io_ptr = test_io_manager;
1463 test_io_backing_manager = unix_io_manager;
1466 io_ptr = unix_io_manager;
1467 retval = ext2fs_open(journal_device,
1468 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1471 com_err(program_name, retval,
1472 _("while trying to open journal device %s\n"),
1476 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1477 com_err(program_name, 0,
1478 _("Journal dev blocksize (%d) smaller than "
1479 "minimum blocksize %d\n"), jfs->blocksize,
1483 blocksize = jfs->blocksize;
1484 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1485 fs_param.s_log_block_size =
1486 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1490 if (blocksize > sys_page_size) {
1492 com_err(program_name, 0,
1493 _("%d-byte blocks too big for system (max %d)"),
1494 blocksize, sys_page_size);
1497 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1498 "(max %d), forced to continue\n"),
1499 blocksize, sys_page_size);
1501 if (optind < argc) {
1502 fs_blocks_count = parse_num_blocks2(argv[optind++],
1503 fs_param.s_log_block_size);
1504 if (!fs_blocks_count) {
1505 com_err(program_name, 0,
1506 _("invalid blocks '%s' on device '%s'"),
1507 argv[optind - 1], device_name);
1515 check_plausibility(device_name);
1516 check_mount(device_name, force, _("filesystem"));
1518 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1520 /* Determine the size of the device (if possible) */
1521 if (noaction && fs_blocks_count) {
1522 dev_size = fs_blocks_count;
1525 retval = ext2fs_get_device_size2(device_name,
1526 EXT2_BLOCK_SIZE(&fs_param),
1529 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1530 com_err(program_name, retval,
1531 _("while trying to determine filesystem size"));
1534 if (!fs_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_blocks_count = dev_size;
1555 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1556 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1557 EXT2_BLOCK_SIZE(&fs_param))-1));
1559 } else if (!force && (fs_blocks_count > dev_size)) {
1560 com_err(program_name, 0,
1561 _("Filesystem larger than apparent device size."));
1566 * We have the file system (or device) size, so we can now
1567 * determine the appropriate file system types so the fs can
1568 * be appropriately configured.
1570 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1571 fs_blocks_count ? fs_blocks_count : dev_size,
1574 fprintf(stderr, _("Failed to parse fs types list\n"));
1578 /* Figure out what features should be enabled */
1581 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1582 tmp = get_string_from_profile(fs_types, "base_features",
1583 "sparse_super,filetype,resize_inode,dir_index");
1584 edit_feature(tmp, &fs_param.s_feature_compat);
1587 /* And which mount options as well */
1588 tmp = get_string_from_profile(fs_types, "default_mntopts",
1590 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1594 for (cpp = fs_types; *cpp; cpp++) {
1596 profile_get_string(profile, "fs_types", *cpp,
1597 "features", "", &tmp);
1599 edit_feature(tmp, &fs_param.s_feature_compat);
1603 tmp = get_string_from_profile(fs_types, "default_features",
1606 edit_feature(fs_features ? fs_features : tmp,
1607 &fs_param.s_feature_compat);
1612 * We now need to do a sanity check of fs_blocks_count for
1613 * 32-bit vs 64-bit block number support.
1615 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1616 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1618 fs_param.s_log_block_size = 2;
1620 if ((fs_blocks_count > MAX_32_NUM) &&
1621 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1622 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1623 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1624 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1626 if ((fs_blocks_count > MAX_32_NUM) &&
1627 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1628 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1629 "too big to be expressed\n\t"
1630 "in 32 bits using a blocksize of %d.\n"),
1631 program_name, fs_blocks_count, device_name,
1632 EXT2_BLOCK_SIZE(&fs_param));
1636 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1638 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1639 fs_types[0] = strdup("journal");
1644 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1645 print_str_list(fs_types);
1648 if (r_opt == EXT2_GOOD_OLD_REV &&
1649 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1650 fs_param.s_feature_ro_compat)) {
1651 fprintf(stderr, _("Filesystem features not supported "
1652 "with revision 0 filesystems\n"));
1657 if (r_opt == EXT2_GOOD_OLD_REV) {
1658 fprintf(stderr, _("Sparse superblocks not supported "
1659 "with revision 0 filesystems\n"));
1662 fs_param.s_feature_ro_compat |=
1663 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1664 } else if (s_opt == 0)
1665 fs_param.s_feature_ro_compat &=
1666 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1668 if (journal_size != 0) {
1669 if (r_opt == EXT2_GOOD_OLD_REV) {
1670 fprintf(stderr, _("Journals not supported "
1671 "with revision 0 filesystems\n"));
1674 fs_param.s_feature_compat |=
1675 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1678 if (fs_param.s_feature_incompat &
1679 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1681 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1682 fs_param.s_feature_compat = 0;
1683 fs_param.s_feature_ro_compat = 0;
1686 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1687 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1688 fprintf(stderr, _("The resize_inode and meta_bg features "
1689 "are not compatible.\n"
1690 "They can not be both enabled "
1691 "simultaneously.\n"));
1695 /* Set first meta blockgroup via an environment variable */
1696 /* (this is mostly for debugging purposes) */
1697 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1698 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1699 fs_param.s_first_meta_bg = atoi(tmp);
1701 /* Get the hardware sector sizes, if available */
1702 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1704 com_err(program_name, retval,
1705 _("while trying to determine hardware sector size"));
1708 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1710 com_err(program_name, retval,
1711 _("while trying to determine physical sector size"));
1715 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1716 lsector_size = atoi(tmp);
1717 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1718 psector_size = atoi(tmp);
1720 /* Older kernels may not have physical/logical distinction */
1722 psector_size = lsector_size;
1724 if (blocksize <= 0) {
1725 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1727 if (use_bsize == -1) {
1728 use_bsize = sys_page_size;
1729 if ((linux_version_code < (2*65536 + 6*256)) &&
1733 if (lsector_size && use_bsize < lsector_size)
1734 use_bsize = lsector_size;
1735 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1736 use_bsize = -blocksize;
1737 blocksize = use_bsize;
1738 ext2fs_blocks_count_set(&fs_param,
1739 ext2fs_blocks_count(&fs_param) /
1740 (blocksize / 1024));
1742 if (blocksize < lsector_size) { /* Impossible */
1743 com_err(program_name, EINVAL,
1744 _("while setting blocksize; too small "
1747 } else if ((blocksize < psector_size) &&
1748 (psector_size <= sys_page_size)) { /* Suboptimal */
1749 fprintf(stderr, _("Warning: specified blocksize %d is "
1750 "less than device physical sectorsize %d\n"),
1751 blocksize, psector_size);
1755 if (inode_ratio == 0) {
1756 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1758 if (inode_ratio < blocksize)
1759 inode_ratio = blocksize;
1762 fs_param.s_log_cluster_size = fs_param.s_log_block_size =
1763 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1765 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1766 retval = get_device_geometry(device_name, &fs_param, psector_size);
1769 _("warning: Unable to get device geometry for %s\n"),
1771 } else if (retval) {
1772 printf(_("%s alignment is offset by %lu bytes.\n"),
1773 device_name, retval);
1774 printf(_("This may result in very poor performance, "
1775 "(re)-partitioning suggested.\n"));
1779 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1781 lazy_itable_init = 0;
1782 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1783 lazy_itable_init = 1;
1785 lazy_itable_init = get_bool_from_profile(fs_types,
1788 discard = get_bool_from_profile(fs_types, "discard" , discard);
1790 /* Get options from profile */
1791 for (cpp = fs_types; *cpp; cpp++) {
1793 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1795 parse_extended_opts(&fs_param, tmp);
1800 parse_extended_opts(&fs_param, extended_opts);
1802 /* Since sparse_super is the default, we would only have a problem
1803 * here if it was explicitly disabled.
1805 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1806 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1807 com_err(program_name, 0,
1808 _("reserved online resize blocks not supported "
1809 "on non-sparse filesystem"));
1813 if (fs_param.s_blocks_per_group) {
1814 if (fs_param.s_blocks_per_group < 256 ||
1815 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1816 com_err(program_name, 0,
1817 _("blocks per group count out of range"));
1822 if (inode_size == 0)
1823 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1824 if (!flex_bg_size && (fs_param.s_feature_incompat &
1825 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1826 flex_bg_size = get_int_from_profile(fs_types,
1827 "flex_bg_size", 16);
1829 if (!(fs_param.s_feature_incompat &
1830 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1831 com_err(program_name, 0,
1832 _("Flex_bg feature not enabled, so "
1833 "flex_bg size may not be specified"));
1836 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1839 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1840 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1841 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1842 inode_size & (inode_size - 1)) {
1843 com_err(program_name, 0,
1844 _("invalid inode size %d (min %d/max %d)"),
1845 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1849 fs_param.s_inode_size = inode_size;
1852 /* Make sure number of inodes specified will fit in 32 bits */
1853 if (num_inodes == 0) {
1854 unsigned long long n;
1855 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1856 if (n > MAX_32_NUM) {
1857 if (fs_param.s_feature_incompat &
1858 EXT4_FEATURE_INCOMPAT_64BIT)
1859 num_inodes = MAX_32_NUM;
1861 com_err(program_name, 0,
1862 _("too many inodes (%llu), raise"
1863 "inode ratio?"), n);
1867 } else if (num_inodes > MAX_32_NUM) {
1868 com_err(program_name, 0,
1869 _("too many inodes (%llu), specify < 2^32 inodes"),
1874 * Calculate number of inodes based on the inode ratio
1876 fs_param.s_inodes_count = num_inodes ? num_inodes :
1877 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1879 if ((((long long)fs_param.s_inodes_count) *
1880 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1881 ((ext2fs_blocks_count(&fs_param)) *
1882 EXT2_BLOCK_SIZE(&fs_param))) {
1883 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1884 "(%u) too big for a\n\t"
1885 "filesystem with %llu blocks, "
1886 "specify higher inode_ratio (-i)\n\t"
1887 "or lower inode count (-N).\n"),
1888 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1889 fs_param.s_inodes_count,
1890 (unsigned long long) ext2fs_blocks_count(&fs_param));
1895 * Calculate number of blocks to reserve
1897 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1898 ext2fs_blocks_count(&fs_param) / 100.0);
1901 static int should_do_undo(const char *name)
1906 struct ext2_super_block super;
1907 io_manager manager = unix_io_manager;
1908 int csum_flag, force_undo;
1910 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1911 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1912 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1913 if (!force_undo && (!csum_flag || !lazy_itable_init))
1916 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1919 * We don't handle error cases instead we
1920 * declare that the file system doesn't exist
1921 * and let the rest of mke2fs take care of
1928 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1929 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1935 #if defined(WORDS_BIGENDIAN)
1936 s_magic = ext2fs_swab16(super.s_magic);
1938 s_magic = super.s_magic;
1941 if (s_magic == EXT2_SUPER_MAGIC)
1945 io_channel_close(channel);
1952 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1954 errcode_t retval = ENOMEM;
1955 char *tdb_dir, *tdb_file = NULL;
1956 char *device_name, *tmp_name;
1959 * Configuration via a conf file would be
1962 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1964 profile_get_string(profile, "defaults",
1965 "undo_dir", 0, "/var/lib/e2fsprogs",
1968 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1969 access(tdb_dir, W_OK))
1972 tmp_name = strdup(name);
1975 device_name = basename(tmp_name);
1976 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1981 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1984 if (!access(tdb_file, F_OK)) {
1985 if (unlink(tdb_file) < 0) {
1991 set_undo_io_backing_manager(*io_ptr);
1992 *io_ptr = undo_io_manager;
1993 retval = set_undo_io_backup_file(tdb_file);
1996 printf(_("Overwriting existing filesystem; this can be undone "
1997 "using the command:\n"
1998 " e2undo %s %s\n\n"), tdb_file, name);
2005 com_err(program_name, retval,
2006 _("while trying to setup undo file\n"));
2010 static int mke2fs_discard_device(ext2_filsys fs)
2012 struct ext2fs_numeric_progress_struct progress;
2013 blk64_t blocks = ext2fs_blocks_count(fs->super);
2014 blk64_t count = DISCARD_STEP_MB;
2018 count *= (1024 * 1024);
2019 count /= fs->blocksize;
2021 ext2fs_numeric_progress_init(fs, &progress,
2022 _("Discarding device blocks: "),
2024 while (cur < blocks) {
2025 ext2fs_numeric_progress_update(fs, &progress, cur);
2027 if (cur + count > blocks)
2028 count = blocks - cur;
2030 retval = io_channel_discard(fs->io, cur, count, fs->blocksize);
2037 ext2fs_numeric_progress_close(fs, &progress,
2040 printf("%s\n",error_message(retval));
2042 ext2fs_numeric_progress_close(fs, &progress,
2048 int main (int argc, char *argv[])
2050 errcode_t retval = 0;
2052 badblocks_list bb_list = 0;
2053 unsigned int journal_blocks;
2059 char tdb_string[40];
2061 int itable_zeroed = 0;
2064 setlocale(LC_MESSAGES, "");
2065 setlocale(LC_CTYPE, "");
2066 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2067 textdomain(NLS_CAT_NAME);
2071 #ifdef CONFIG_TESTIO_DEBUG
2072 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2073 io_ptr = test_io_manager;
2074 test_io_backing_manager = unix_io_manager;
2077 io_ptr = unix_io_manager;
2079 if (should_do_undo(device_name)) {
2080 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2086 * Initialize the superblock....
2088 flags = EXT2_FLAG_EXCLUSIVE;
2089 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2092 flags |= EXT2_FLAG_64BITS;
2094 * By default, we print how many inode tables or block groups
2095 * or whatever we've written so far. The quiet flag disables
2096 * this, along with a lot of other output.
2099 flags |= EXT2_FLAG_PRINT_PROGRESS;
2100 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2102 com_err(device_name, retval, _("while setting up superblock"));
2106 /* Can't undo discard ... */
2107 if (discard && (io_ptr != undo_io_manager)) {
2108 retval = mke2fs_discard_device(fs);
2109 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2111 printf(_("Discard succeeded and will return 0s "
2112 " - skipping inode table wipe\n"));
2113 lazy_itable_init = 1;
2118 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2119 32768 : fs->blocksize * 8);
2120 io_channel_set_options(fs->io, tdb_string);
2122 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2123 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2125 if ((fs_param.s_feature_incompat &
2126 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2127 (fs_param.s_feature_ro_compat &
2128 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2129 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2130 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2131 fs->super->s_kbytes_written = 1;
2134 * Wipe out the old on-disk superblock
2137 zap_sector(fs, 2, 6);
2140 * Parse or generate a UUID for the filesystem
2143 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2144 com_err(device_name, 0, "could not parse UUID: %s\n",
2149 uuid_generate(fs->super->s_uuid);
2152 * Initialize the directory index variables
2154 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2156 hash_alg = e2p_string2hash(hash_alg_str);
2158 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2160 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2163 * Periodic checks can be enabled/disabled via config file.
2164 * Note we override the kernel include file's idea of what the default
2165 * check interval (never) should be. It's a good idea to check at
2166 * least *occasionally*, specially since servers will never rarely get
2167 * to reboot, since Linux is so robust these days. :-)
2169 * 180 days (six months) seems like a good value.
2171 #ifdef EXT2_DFL_CHECKINTERVAL
2172 #undef EXT2_DFL_CHECKINTERVAL
2174 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2176 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2177 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2178 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2180 * Add "jitter" to the superblock's check interval so that we
2181 * don't check all the filesystems at the same time. We use a
2182 * kludgy hack of using the UUID to derive a random jitter value
2184 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2185 val += fs->super->s_uuid[i];
2186 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2190 * Override the creator OS, if applicable
2192 if (creator_os && !set_os(fs->super, creator_os)) {
2193 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2198 * For the Hurd, we will turn off filetype since it doesn't
2201 if (fs->super->s_creator_os == EXT2_OS_HURD)
2202 fs->super->s_feature_incompat &=
2203 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2206 * Set the volume label...
2209 memset(fs->super->s_volume_name, 0,
2210 sizeof(fs->super->s_volume_name));
2211 strncpy(fs->super->s_volume_name, volume_label,
2212 sizeof(fs->super->s_volume_name));
2216 * Set the last mount directory
2219 memset(fs->super->s_last_mounted, 0,
2220 sizeof(fs->super->s_last_mounted));
2221 strncpy(fs->super->s_last_mounted, mount_dir,
2222 sizeof(fs->super->s_last_mounted));
2225 if (!quiet || noaction)
2231 if (fs->super->s_feature_incompat &
2232 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2233 create_journal_dev(fs);
2234 exit(ext2fs_close(fs) ? 1 : 0);
2237 if (bad_blocks_filename)
2238 read_bb_file(fs, &bb_list, bad_blocks_filename);
2240 test_disk(fs, &bb_list);
2242 handle_bad_blocks(fs, bb_list);
2243 fs->stride = fs_stride = fs->super->s_raid_stride;
2245 printf(_("Allocating group tables: "));
2246 retval = ext2fs_allocate_tables(fs);
2248 com_err(program_name, retval,
2249 _("while trying to allocate filesystem tables"));
2253 printf(_("done \n"));
2255 fs->super->s_state |= EXT2_ERROR_FS;
2256 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2258 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2259 blk64_t rsv = 65536 / fs->blocksize;
2260 blk64_t blocks = ext2fs_blocks_count(fs->super);
2264 #ifdef ZAP_BOOTBLOCK
2265 zap_sector(fs, 0, 2);
2269 * Wipe out any old MD RAID (or other) metadata at the end
2270 * of the device. This will also verify that the device is
2271 * as large as we think. Be careful with very small devices.
2273 start = (blocks & ~(rsv - 1));
2277 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2281 com_err(program_name, retval,
2282 _("while zeroing block %llu at end of filesystem"),
2285 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2286 create_root_dir(fs);
2287 create_lost_and_found(fs);
2289 create_bad_block_inode(fs, bb_list);
2290 if (fs->super->s_feature_compat &
2291 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2292 retval = ext2fs_create_resize_inode(fs);
2294 com_err("ext2fs_create_resize_inode", retval,
2295 _("while reserving blocks for online resize"));
2301 if (journal_device) {
2305 check_plausibility(journal_device);
2306 check_mount(journal_device, force, _("journal"));
2308 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2309 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2310 fs->blocksize, unix_io_manager, &jfs);
2312 com_err(program_name, retval,
2313 _("while trying to open journal device %s\n"),
2318 printf(_("Adding journal to device %s: "),
2322 retval = ext2fs_add_journal_device(fs, jfs);
2324 com_err (program_name, retval,
2325 _("\n\twhile trying to add journal to device %s"),
2330 printf(_("done\n"));
2332 free(journal_device);
2333 } else if ((journal_size) ||
2334 (fs_param.s_feature_compat &
2335 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2336 journal_blocks = figure_journal_size(journal_size, fs);
2339 printf(_("Skipping journal creation in super-only mode\n"));
2340 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2344 if (!journal_blocks) {
2345 fs->super->s_feature_compat &=
2346 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2350 printf(_("Creating journal (%u blocks): "),
2354 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2357 com_err (program_name, retval,
2358 _("\n\twhile trying to create journal"));
2362 printf(_("done\n"));
2367 printf(_("Writing superblocks and "
2368 "filesystem accounting information: "));
2369 retval = ext2fs_flush(fs);
2372 _("\nWarning, had trouble writing out superblocks."));
2375 printf(_("done\n\n"));
2376 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2377 print_check_message(fs);
2379 val = ext2fs_close(fs);
2380 remove_error_table(&et_ext2_error_table);
2381 remove_error_table(&et_prof_error_table);
2382 profile_release(profile);
2383 for (i=0; fs_types[i]; i++)
2386 return (retval || val) ? 1 : 0;