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 */
29 #include <sys/utsname.h>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
54 #include <blkid/blkid.h>
56 #include "ext2fs/ext2_fs.h"
57 #include "ext2fs/ext2fsP.h"
58 #include "et/com_err.h"
59 #include "uuid/uuid.h"
61 #include "ext2fs/ext2fs.h"
65 #include "../version.h"
66 #include "nls-enable.h"
67 #include "quota/mkquota.h"
69 #define STRIDE_LENGTH 8
71 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
77 #define DISCARD_STEP_MB (2048)
79 extern int isatty(int);
80 extern FILE *fpopen(const char *cmd, const char *mode);
82 const char * program_name = "mke2fs";
83 const char * device_name /* = NULL */;
85 /* Command line options */
90 int discard = 1; /* attempt to discard device before fs creation */
96 char *bad_blocks_filename;
99 struct ext2_super_block fs_param;
100 char *fs_uuid = NULL;
104 char *journal_device;
105 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
110 int sys_page_size = 4096;
111 int linux_version_code = 0;
113 static void usage(void)
115 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
116 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
117 "[-J journal-options]\n"
118 "\t[-G meta group size] [-N number-of-inodes]\n"
119 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
120 "\t[-g blocks-per-group] [-L volume-label] "
121 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
122 "[-r fs-revision] [-E extended-option[,...]]\n"
123 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
128 static int int_log2(unsigned long long arg)
140 static int int_log10(unsigned long long arg)
149 static int parse_version_number(const char *s)
151 int major, minor, rev;
157 major = strtol(cp, &endptr, 10);
158 if (cp == endptr || *endptr != '.')
161 minor = strtol(cp, &endptr, 10);
162 if (cp == endptr || *endptr != '.')
165 rev = strtol(cp, &endptr, 10);
168 return ((((major * 256) + minor) * 256) + rev);
172 * Helper function for read_bb_file and test_disk
174 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
176 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
181 * Reads the bad blocks list from a file
183 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
184 const char *bad_blocks_file)
189 f = fopen(bad_blocks_file, "r");
191 com_err("read_bad_blocks_file", errno,
192 _("while trying to open %s"), bad_blocks_file);
195 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
198 com_err("ext2fs_read_bb_FILE", retval,
199 _("while reading in list of bad blocks from file"));
205 * Runs the badblocks program to test the disk
207 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
213 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
214 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
215 fs->device_name, ext2fs_blocks_count(fs->super)-1);
217 printf(_("Running command: %s\n"), buf);
220 com_err("popen", errno,
221 _("while trying to run '%s'"), buf);
224 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
227 com_err("ext2fs_read_bb_FILE", retval,
228 _("while processing list of bad blocks from program"));
233 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
237 unsigned must_be_good;
239 badblocks_iterate bb_iter;
249 * The primary superblock and group descriptors *must* be
250 * good; if not, abort.
252 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
253 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
254 if (ext2fs_badblocks_list_test(bb_list, i)) {
255 fprintf(stderr, _("Block %d in primary "
256 "superblock/group descriptor area bad.\n"), i);
257 fprintf(stderr, _("Blocks %u through %u must be good "
258 "in order to build a filesystem.\n"),
259 fs->super->s_first_data_block, must_be_good);
260 fputs(_("Aborting....\n"), stderr);
266 * See if any of the bad blocks are showing up in the backup
267 * superblocks and/or group descriptors. If so, issue a
268 * warning and adjust the block counts appropriately.
270 group_block = fs->super->s_first_data_block +
271 fs->super->s_blocks_per_group;
273 for (i = 1; i < fs->group_desc_count; i++) {
275 for (j=0; j < fs->desc_blocks+1; j++) {
276 if (ext2fs_badblocks_list_test(bb_list,
280 _("Warning: the backup superblock/group descriptors at block %u contain\n"
284 group = ext2fs_group_of_blk2(fs, group_block+j);
285 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
286 ext2fs_group_desc_csum_set(fs, group);
287 ext2fs_free_blocks_count_add(fs->super, 1);
290 group_block += fs->super->s_blocks_per_group;
294 * Mark all the bad blocks as used...
296 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
298 com_err("ext2fs_badblocks_list_iterate_begin", retval,
299 _("while marking bad blocks as used"));
302 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
303 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
304 ext2fs_badblocks_list_iterate_end(bb_iter);
307 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
313 struct ext2fs_numeric_progress_struct progress;
315 ext2fs_numeric_progress_init(fs, &progress,
316 _("Writing inode tables: "),
317 fs->group_desc_count);
319 for (i = 0; i < fs->group_desc_count; i++) {
320 ext2fs_numeric_progress_update(fs, &progress, i);
322 blk = ext2fs_inode_table_loc(fs, i);
323 num = fs->inode_blocks_per_group;
326 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
327 ext2fs_bg_itable_unused(fs, i)) *
328 EXT2_INODE_SIZE(fs->super),
329 EXT2_BLOCK_SIZE(fs->super));
330 if (!lazy_flag || itable_zeroed) {
331 /* The kernel doesn't need to zero the itable blocks */
332 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
333 ext2fs_group_desc_csum_set(fs, i);
335 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
337 fprintf(stderr, _("\nCould not write %d "
338 "blocks in inode table starting at %llu: %s\n"),
339 num, blk, error_message(retval));
343 if (sync_kludge == 1)
345 else if ((i % sync_kludge) == 0)
349 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
350 ext2fs_numeric_progress_close(fs, &progress,
354 static void create_root_dir(ext2_filsys fs)
357 struct ext2_inode inode;
360 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
362 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
366 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
368 com_err("ext2fs_read_inode", retval,
369 _("while reading root inode"));
374 ext2fs_set_i_uid_high(inode, uid >> 16);
378 ext2fs_set_i_gid_high(inode, gid >> 16);
380 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
382 com_err("ext2fs_write_inode", retval,
383 _("while setting root inode ownership"));
389 static void create_lost_and_found(ext2_filsys fs)
391 unsigned int lpf_size = 0;
394 const char *name = "lost+found";
398 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
400 com_err("ext2fs_mkdir", retval,
401 _("while creating /lost+found"));
405 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
407 com_err("ext2_lookup", retval,
408 _("while looking up /lost+found"));
412 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
413 /* Ensure that lost+found is at least 2 blocks, so we always
414 * test large empty blocks for big-block filesystems. */
415 if ((lpf_size += fs->blocksize) >= 16*1024 &&
416 lpf_size >= 2 * fs->blocksize)
418 retval = ext2fs_expand_dir(fs, ino);
420 com_err("ext2fs_expand_dir", retval,
421 _("while expanding /lost+found"));
427 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
431 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
432 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
433 retval = ext2fs_update_bb_inode(fs, bb_list);
435 com_err("ext2fs_update_bb_inode", retval,
436 _("while setting bad block inode"));
442 static void reserve_inodes(ext2_filsys fs)
446 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
447 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
448 ext2fs_mark_ib_dirty(fs);
451 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
452 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
453 #define BSD_LABEL_OFFSET 64
455 static void zap_sector(ext2_filsys fs, int sect, int nsect)
461 buf = malloc(512*nsect);
463 printf(_("Out of memory erasing sectors %d-%d\n"),
464 sect, sect + nsect - 1);
469 /* Check for a BSD disklabel, and don't erase it if so */
470 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
473 _("Warning: could not read block 0: %s\n"),
474 error_message(retval));
476 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
477 if ((*magic == BSD_DISKMAGIC) ||
478 (*magic == BSD_MAGICDISK))
483 memset(buf, 0, 512*nsect);
484 io_channel_set_blksize(fs->io, 512);
485 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
486 io_channel_set_blksize(fs->io, fs->blocksize);
489 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
490 sect, error_message(retval));
493 static void create_journal_dev(ext2_filsys fs)
495 struct ext2fs_numeric_progress_struct progress;
498 blk64_t blk, err_blk;
499 int c, count, err_count;
501 retval = ext2fs_create_journal_superblock(fs,
502 ext2fs_blocks_count(fs->super), 0, &buf);
504 com_err("create_journal_dev", retval,
505 _("while initializing journal superblock"));
509 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
510 goto write_superblock;
512 ext2fs_numeric_progress_init(fs, &progress,
513 _("Zeroing journal device: "),
514 ext2fs_blocks_count(fs->super));
516 count = ext2fs_blocks_count(fs->super);
522 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
524 com_err("create_journal_dev", retval,
525 _("while zeroing journal device "
526 "(block %llu, count %d)"),
532 ext2fs_numeric_progress_update(fs, &progress, blk);
534 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
536 ext2fs_numeric_progress_close(fs, &progress, NULL);
538 retval = io_channel_write_blk64(fs->io,
539 fs->super->s_first_data_block+1,
542 com_err("create_journal_dev", retval,
543 _("while writing journal superblock"));
548 static void show_stats(ext2_filsys fs)
550 struct ext2_super_block *s = fs->super;
557 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
558 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
559 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
561 memset(buf, 0, sizeof(buf));
562 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
563 printf(_("Filesystem label=%s\n"), buf);
564 os = e2p_os2string(fs->super->s_creator_os);
566 printf(_("OS type: %s\n"), os);
568 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
569 s->s_log_block_size);
570 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
571 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
572 printf(_("Cluster size=%u (log=%u)\n"),
573 fs->blocksize << fs->cluster_ratio_bits,
574 s->s_log_cluster_size);
576 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
577 s->s_log_cluster_size);
578 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
579 s->s_raid_stride, s->s_raid_stripe_width);
580 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
581 ext2fs_blocks_count(s));
582 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
583 ext2fs_r_blocks_count(s),
584 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
585 printf(_("First data block=%u\n"), s->s_first_data_block);
586 if (s->s_reserved_gdt_blocks)
587 printf(_("Maximum filesystem blocks=%lu\n"),
588 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
589 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
590 if (fs->group_desc_count > 1)
591 printf(_("%u block groups\n"), fs->group_desc_count);
593 printf(_("%u block group\n"), fs->group_desc_count);
594 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
595 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
596 printf(_("%u blocks per group, %u clusters per group\n"),
597 s->s_blocks_per_group, s->s_clusters_per_group);
599 printf(_("%u blocks per group, %u fragments per group\n"),
600 s->s_blocks_per_group, s->s_clusters_per_group);
601 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
603 if (fs->group_desc_count == 1) {
608 printf(_("Superblock backups stored on blocks: "));
609 group_block = s->s_first_data_block;
611 for (i = 1; i < fs->group_desc_count; i++) {
612 group_block += s->s_blocks_per_group;
613 if (!ext2fs_bg_has_super(fs, i))
617 need = int_log10(group_block) + 2;
618 if (need > col_left) {
623 printf("%llu", group_block);
629 * Set the S_CREATOR_OS field. Return true if OS is known,
632 static int set_os(struct ext2_super_block *sb, char *os)
635 sb->s_creator_os = atoi (os);
636 else if (strcasecmp(os, "linux") == 0)
637 sb->s_creator_os = EXT2_OS_LINUX;
638 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
639 sb->s_creator_os = EXT2_OS_HURD;
640 else if (strcasecmp(os, "freebsd") == 0)
641 sb->s_creator_os = EXT2_OS_FREEBSD;
642 else if (strcasecmp(os, "lites") == 0)
643 sb->s_creator_os = EXT2_OS_LITES;
649 #define PATH_SET "PATH=/sbin"
651 static void parse_extended_opts(struct ext2_super_block *param,
654 char *buf, *token, *next, *p, *arg, *badopt = 0;
662 _("Couldn't allocate memory to parse options!\n"));
666 for (token = buf; token && *token; token = next) {
667 p = strchr(token, ',');
673 arg = strchr(token, '=');
678 if (strcmp(token, "mmp_update_interval") == 0) {
684 param->s_mmp_update_interval = strtoul(arg, &p, 0);
687 _("Invalid mmp_update_interval: %s\n"),
692 } else if (strcmp(token, "stride") == 0) {
698 param->s_raid_stride = strtoul(arg, &p, 0);
701 _("Invalid stride parameter: %s\n"),
706 } else if (strcmp(token, "stripe-width") == 0 ||
707 strcmp(token, "stripe_width") == 0) {
713 param->s_raid_stripe_width = strtoul(arg, &p, 0);
716 _("Invalid stripe-width parameter: %s\n"),
721 } else if (!strcmp(token, "resize")) {
723 unsigned long bpg, rsv_groups;
724 unsigned long group_desc_count, desc_blocks;
725 unsigned int gdpb, blocksize;
734 resize = parse_num_blocks2(arg,
735 param->s_log_block_size);
739 _("Invalid resize parameter: %s\n"),
744 if (resize <= ext2fs_blocks_count(param)) {
746 _("The resize maximum must be greater "
747 "than the filesystem size.\n"));
752 blocksize = EXT2_BLOCK_SIZE(param);
753 bpg = param->s_blocks_per_group;
756 gdpb = EXT2_DESC_PER_BLOCK(param);
757 group_desc_count = (__u32) ext2fs_div64_ceil(
758 ext2fs_blocks_count(param), bpg);
759 desc_blocks = (group_desc_count +
761 rsv_groups = ext2fs_div64_ceil(resize, bpg);
762 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
764 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
765 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
768 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
770 _("On-line resizing not supported with revision 0 filesystems\n"));
774 param->s_feature_compat |=
775 EXT2_FEATURE_COMPAT_RESIZE_INODE;
777 param->s_reserved_gdt_blocks = rsv_gdb;
779 } else if (!strcmp(token, "test_fs")) {
780 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
781 } else if (!strcmp(token, "lazy_itable_init")) {
783 lazy_itable_init = strtoul(arg, &p, 0);
785 lazy_itable_init = 1;
786 } else if (!strcmp(token, "lazy_journal_init")) {
788 journal_flags |= strtoul(arg, &p, 0) ?
789 EXT2_MKJOURNAL_LAZYINIT : 0;
791 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
792 } else if (!strcmp(token, "discard")) {
794 } else if (!strcmp(token, "nodiscard")) {
802 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
803 "Extended options are separated by commas, "
804 "and may take an argument which\n"
805 "\tis set off by an equals ('=') sign.\n\n"
806 "Valid extended options are:\n"
807 "\tstride=<RAID per-disk data chunk in blocks>\n"
808 "\tstripe-width=<RAID stride * data disks in blocks>\n"
809 "\tresize=<resize maximum size in blocks>\n"
810 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
811 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
815 badopt ? badopt : "");
819 if (param->s_raid_stride &&
820 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
821 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
822 "multiple of stride %u.\n\n"),
823 param->s_raid_stripe_width, param->s_raid_stride);
828 static __u32 ok_features[3] = {
830 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
831 EXT2_FEATURE_COMPAT_RESIZE_INODE |
832 EXT2_FEATURE_COMPAT_DIR_INDEX |
833 EXT2_FEATURE_COMPAT_EXT_ATTR,
835 EXT2_FEATURE_INCOMPAT_FILETYPE|
836 EXT3_FEATURE_INCOMPAT_EXTENTS|
837 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
838 EXT2_FEATURE_INCOMPAT_META_BG|
839 EXT4_FEATURE_INCOMPAT_FLEX_BG|
840 EXT4_FEATURE_INCOMPAT_MMP |
841 EXT4_FEATURE_INCOMPAT_64BIT,
843 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
844 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
845 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
846 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
847 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
848 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
849 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
850 EXT4_FEATURE_RO_COMPAT_QUOTA
854 static void syntax_err_report(const char *filename, long err, int line_num)
857 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
858 filename, line_num, error_message(err));
862 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
864 static void edit_feature(const char *str, __u32 *compat_array)
869 if (e2p_edit_feature(str, compat_array, ok_features)) {
870 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
876 static void edit_mntopts(const char *str, __u32 *mntopts)
881 if (e2p_edit_mntopts(str, mntopts, ~0)) {
882 fprintf(stderr, _("Invalid mount option set: %s\n"),
894 static errcode_t init_list(struct str_list *sl)
898 sl->list = malloc((sl->max+1) * sizeof(char *));
905 static errcode_t push_string(struct str_list *sl, const char *str)
909 if (sl->num >= sl->max) {
911 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
916 sl->list[sl->num] = malloc(strlen(str)+1);
917 if (sl->list[sl->num] == 0)
919 strcpy(sl->list[sl->num], str);
921 sl->list[sl->num] = 0;
925 static void print_str_list(char **list)
929 for (cpp = list; *cpp; cpp++) {
930 printf("'%s'", *cpp);
938 * Return TRUE if the profile has the given subsection
940 static int profile_has_subsection(profile_t profile, const char *section,
941 const char *subsection)
944 const char *names[4];
949 names[1] = subsection;
952 if (profile_iterator_create(profile, names,
953 PROFILE_ITER_LIST_SECTION |
954 PROFILE_ITER_RELATIONS_ONLY, &state))
957 if ((profile_iterator(&state, &name, 0) == 0) && name) {
962 profile_iterator_free(&state);
966 static char **parse_fs_type(const char *fs_type,
967 const char *usage_types,
968 struct ext2_super_block *fs_param,
969 blk64_t fs_blocks_count,
972 const char *ext_type = 0;
974 char *profile_type = 0;
976 const char *size_type;
977 struct str_list list;
978 unsigned long long meg;
981 if (init_list(&list))
984 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
985 !strcasecmp(creator_os, "hurd")))
992 else if (!strcmp(program_name, "mke3fs"))
995 ext_type = strrchr(progname, '/');
1001 if (!strncmp(ext_type, "mkfs.", 5)) {
1003 if (ext_type[0] == 0)
1010 profile_get_string(profile, "defaults", "fs_type", 0,
1011 "ext2", &profile_type);
1012 ext_type = profile_type;
1013 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1018 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1019 strcmp(ext_type, "ext2")) {
1020 printf(_("\nYour mke2fs.conf file does not define the "
1021 "%s filesystem type.\n"), ext_type);
1022 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1023 !strcmp(ext_type, "ext4dev")) {
1024 printf(_("You probably need to install an updated "
1025 "mke2fs.conf file.\n\n"));
1028 printf(_("Aborting...\n"));
1033 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1034 if (fs_blocks_count < 3 * meg)
1035 size_type = "floppy";
1036 else if (fs_blocks_count < 512 * meg)
1037 size_type = "small";
1038 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1039 size_type = "default";
1040 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1046 usage_types = size_type;
1048 parse_str = malloc(strlen(usage_types)+1);
1053 strcpy(parse_str, usage_types);
1056 push_string(&list, ext_type);
1059 t = strchr(cp, ',');
1064 if (profile_has_subsection(profile, "fs_types", cp))
1065 push_string(&list, cp);
1066 else if (strcmp(cp, "default") != 0)
1068 _("\nWarning: the fs_type %s is not "
1069 "defined in mke2fs.conf\n\n"),
1082 push_string(&list, "hurd");
1086 static char *get_string_from_profile(char **fs_types, const char *opt,
1087 const char *def_val)
1092 for (i=0; fs_types[i]; i++);
1093 for (i-=1; i >=0 ; i--) {
1094 profile_get_string(profile, "fs_types", fs_types[i],
1099 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1103 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1108 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1109 for (cpp = fs_types; *cpp; cpp++)
1110 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1114 static double get_double_from_profile(char **fs_types, const char *opt,
1120 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1121 for (cpp = fs_types; *cpp; cpp++)
1122 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1126 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1131 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1132 for (cpp = fs_types; *cpp; cpp++)
1133 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1137 extern const char *mke2fs_default_profile;
1138 static const char *default_files[] = { "<default>", 0 };
1140 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1142 * Sets the geometry of a device (stripe/stride), and returns the
1143 * device's alignment offset, if any, or a negative error.
1145 static int get_device_geometry(const char *file,
1146 struct ext2_super_block *fs_param,
1153 unsigned long min_io;
1154 unsigned long opt_io;
1155 struct stat statbuf;
1157 /* Nothing to do for a regular file */
1158 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1161 pr = blkid_new_probe_from_filename(file);
1165 tp = blkid_probe_get_topology(pr);
1169 min_io = blkid_topology_get_minimum_io_size(tp);
1170 opt_io = blkid_topology_get_optimal_io_size(tp);
1171 blocksize = EXT2_BLOCK_SIZE(fs_param);
1172 if ((min_io == 0) && (psector_size > blocksize))
1173 min_io = psector_size;
1174 if ((opt_io == 0) && min_io)
1176 if ((opt_io == 0) && (psector_size > blocksize))
1177 opt_io = psector_size;
1179 /* setting stripe/stride to blocksize is pointless */
1180 if (min_io > blocksize)
1181 fs_param->s_raid_stride = min_io / blocksize;
1182 if (opt_io > blocksize)
1183 fs_param->s_raid_stripe_width = opt_io / blocksize;
1185 rc = blkid_topology_get_alignment_offset(tp);
1187 blkid_free_probe(pr);
1192 static void PRS(int argc, char *argv[])
1195 int cluster_size = 0;
1198 int inode_ratio = 0;
1200 unsigned long flex_bg_size = 0;
1201 double reserved_ratio = -1.0;
1202 int lsector_size = 0, psector_size = 0;
1203 int show_version_only = 0;
1204 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1206 char * oldpath = getenv("PATH");
1207 char * extended_opts = 0;
1209 char * usage_types = 0;
1211 blk64_t fs_blocks_count = 0;
1216 int s_opt = -1, r_opt = -1;
1217 char *fs_features = 0;
1220 int pathlen = sizeof(PATH_SET) + 1;
1223 pathlen += strlen(oldpath);
1224 newpath = malloc(pathlen);
1226 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1229 strcpy(newpath, PATH_SET);
1231 /* Update our PATH to include /sbin */
1233 strcat (newpath, ":");
1234 strcat (newpath, oldpath);
1238 tmp = getenv("MKE2FS_SYNC");
1240 sync_kludge = atoi(tmp);
1242 /* Determine the system page size if possible */
1244 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1245 #define _SC_PAGESIZE _SC_PAGE_SIZE
1248 sysval = sysconf(_SC_PAGESIZE);
1250 sys_page_size = sysval;
1251 #endif /* _SC_PAGESIZE */
1252 #endif /* HAVE_SYSCONF */
1254 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1256 profile_set_syntax_err_cb(syntax_err_report);
1257 retval = profile_init(config_fn, &profile);
1258 if (retval == ENOENT) {
1259 retval = profile_init(default_files, &profile);
1262 retval = profile_set_default(profile, mke2fs_default_profile);
1265 } else if (retval) {
1267 fprintf(stderr, _("Couldn't init profile successfully"
1268 " (error: %ld).\n"), retval);
1272 setbuf(stdout, NULL);
1273 setbuf(stderr, NULL);
1274 add_error_table(&et_ext2_error_table);
1275 add_error_table(&et_prof_error_table);
1276 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1277 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1284 linux_version_code = parse_version_number(ut.release);
1285 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1286 fs_param.s_rev_level = 0;
1289 if (argc && *argv) {
1290 program_name = get_progname(*argv);
1292 /* If called as mkfs.ext3, create a journal inode */
1293 if (!strcmp(program_name, "mkfs.ext3") ||
1294 !strcmp(program_name, "mke3fs"))
1298 while ((c = getopt (argc, argv,
1299 "b:cg:i:jl:m:no:qr:s:t:vC:E:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1302 blocksize = strtol(optarg, &tmp, 0);
1303 b = (blocksize > 0) ? blocksize : -blocksize;
1304 if (b < EXT2_MIN_BLOCK_SIZE ||
1305 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1306 com_err(program_name, 0,
1307 _("invalid block size - %s"), optarg);
1310 if (blocksize > 4096)
1311 fprintf(stderr, _("Warning: blocksize %d not "
1312 "usable on most systems.\n"),
1315 fs_param.s_log_block_size =
1316 int_log2(blocksize >>
1317 EXT2_MIN_BLOCK_LOG_SIZE);
1319 case 'c': /* Check for bad blocks */
1323 cluster_size = strtoul(optarg, &tmp, 0);
1324 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1325 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
1326 com_err(program_name, 0,
1327 _("invalid cluster size - %s"),
1333 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1335 com_err(program_name, 0,
1336 _("Illegal number for blocks per group"));
1339 if ((fs_param.s_blocks_per_group % 8) != 0) {
1340 com_err(program_name, 0,
1341 _("blocks per group must be multiple of 8"));
1346 flex_bg_size = strtoul(optarg, &tmp, 0);
1348 com_err(program_name, 0,
1349 _("Illegal number for flex_bg size"));
1352 if (flex_bg_size < 1 ||
1353 (flex_bg_size & (flex_bg_size-1)) != 0) {
1354 com_err(program_name, 0,
1355 _("flex_bg size must be a power of 2"));
1360 inode_ratio = strtoul(optarg, &tmp, 0);
1361 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1362 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1364 com_err(program_name, 0,
1365 _("invalid inode ratio %s (min %d/max %d)"),
1366 optarg, EXT2_MIN_BLOCK_SIZE,
1367 EXT2_MAX_BLOCK_SIZE * 1024);
1372 parse_journal_opts(optarg);
1375 fprintf(stderr, _("Warning: -K option is deprecated and "
1376 "should not be used anymore. Use "
1377 "\'-E nodiscard\' extended option "
1386 bad_blocks_filename = malloc(strlen(optarg)+1);
1387 if (!bad_blocks_filename) {
1388 com_err(program_name, ENOMEM,
1389 _("in malloc for bad_blocks_filename"));
1392 strcpy(bad_blocks_filename, optarg);
1395 reserved_ratio = strtod(optarg, &tmp);
1396 if ( *tmp || reserved_ratio > 50 ||
1397 reserved_ratio < 0) {
1398 com_err(program_name, 0,
1399 _("invalid reserved blocks percent - %s"),
1408 creator_os = optarg;
1414 r_opt = strtoul(optarg, &tmp, 0);
1416 com_err(program_name, 0,
1417 _("bad revision level - %s"), optarg);
1420 fs_param.s_rev_level = r_opt;
1422 case 's': /* deprecated */
1423 s_opt = atoi(optarg);
1426 inode_size = strtoul(optarg, &tmp, 0);
1428 com_err(program_name, 0,
1429 _("invalid inode size - %s"), optarg);
1440 volume_label = optarg;
1446 num_inodes = strtoul(optarg, &tmp, 0);
1448 com_err(program_name, 0,
1449 _("bad num inodes - %s"), optarg);
1454 fs_features = optarg;
1458 extended_opts = optarg;
1465 com_err(program_name, 0,
1466 _("The -t option may only be used once"));
1469 fs_type = strdup(optarg);
1473 com_err(program_name, 0,
1474 _("The -T option may only be used once"));
1477 usage_types = strdup(optarg);
1483 /* Print version number and exit */
1484 show_version_only++;
1490 if ((optind == argc) && !show_version_only)
1492 device_name = argv[optind++];
1494 if (!quiet || show_version_only)
1495 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1498 if (show_version_only) {
1499 fprintf(stderr, _("\tUsing %s\n"),
1500 error_message(EXT2_ET_BASE));
1505 * If there's no blocksize specified and there is a journal
1506 * device, use it to figure out the blocksize
1508 if (blocksize <= 0 && journal_device) {
1512 #ifdef CONFIG_TESTIO_DEBUG
1513 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1514 io_ptr = test_io_manager;
1515 test_io_backing_manager = unix_io_manager;
1518 io_ptr = unix_io_manager;
1519 retval = ext2fs_open(journal_device,
1520 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1523 com_err(program_name, retval,
1524 _("while trying to open journal device %s\n"),
1528 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1529 com_err(program_name, 0,
1530 _("Journal dev blocksize (%d) smaller than "
1531 "minimum blocksize %d\n"), jfs->blocksize,
1535 blocksize = jfs->blocksize;
1536 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1537 fs_param.s_log_block_size =
1538 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1542 if (optind < argc) {
1543 fs_blocks_count = parse_num_blocks2(argv[optind++],
1544 fs_param.s_log_block_size);
1545 if (!fs_blocks_count) {
1546 com_err(program_name, 0,
1547 _("invalid blocks '%s' on device '%s'"),
1548 argv[optind - 1], device_name);
1556 check_plausibility(device_name);
1557 check_mount(device_name, force, _("filesystem"));
1559 /* Determine the size of the device (if possible) */
1560 if (noaction && fs_blocks_count) {
1561 dev_size = fs_blocks_count;
1564 retval = ext2fs_get_device_size2(device_name,
1565 EXT2_BLOCK_SIZE(&fs_param),
1568 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1569 com_err(program_name, retval,
1570 _("while trying to determine filesystem size"));
1573 if (!fs_blocks_count) {
1574 if (retval == EXT2_ET_UNIMPLEMENTED) {
1575 com_err(program_name, 0,
1576 _("Couldn't determine device size; you "
1577 "must specify\nthe size of the "
1581 if (dev_size == 0) {
1582 com_err(program_name, 0,
1583 _("Device size reported to be zero. "
1584 "Invalid partition specified, or\n\t"
1585 "partition table wasn't reread "
1586 "after running fdisk, due to\n\t"
1587 "a modified partition being busy "
1588 "and in use. You may need to reboot\n\t"
1589 "to re-read your partition table.\n"
1593 fs_blocks_count = dev_size;
1594 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1595 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1596 EXT2_BLOCK_SIZE(&fs_param))-1));
1598 } else if (!force && (fs_blocks_count > dev_size)) {
1599 com_err(program_name, 0,
1600 _("Filesystem larger than apparent device size."));
1605 profile_get_string(profile, "devices", device_name,
1606 "fs_type", 0, &fs_type);
1608 profile_get_string(profile, "devices", device_name,
1609 "usage_types", 0, &usage_types);
1612 * We have the file system (or device) size, so we can now
1613 * determine the appropriate file system types so the fs can
1614 * be appropriately configured.
1616 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1617 fs_blocks_count ? fs_blocks_count : dev_size,
1620 fprintf(stderr, _("Failed to parse fs types list\n"));
1624 /* Figure out what features should be enabled */
1627 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1628 tmp = get_string_from_profile(fs_types, "base_features",
1629 "sparse_super,filetype,resize_inode,dir_index");
1630 edit_feature(tmp, &fs_param.s_feature_compat);
1633 /* And which mount options as well */
1634 tmp = get_string_from_profile(fs_types, "default_mntopts",
1636 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1640 for (cpp = fs_types; *cpp; cpp++) {
1642 profile_get_string(profile, "fs_types", *cpp,
1643 "features", "", &tmp);
1645 edit_feature(tmp, &fs_param.s_feature_compat);
1649 tmp = get_string_from_profile(fs_types, "default_features",
1652 edit_feature(fs_features ? fs_features : tmp,
1653 &fs_param.s_feature_compat);
1658 * We now need to do a sanity check of fs_blocks_count for
1659 * 32-bit vs 64-bit block number support.
1661 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1662 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1664 fs_param.s_log_block_size = 2;
1666 if ((fs_blocks_count > MAX_32_NUM) &&
1667 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1668 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1669 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1670 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1672 if ((fs_blocks_count > MAX_32_NUM) &&
1673 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1674 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1675 "too big to be expressed\n\t"
1676 "in 32 bits using a blocksize of %d.\n"),
1677 program_name, fs_blocks_count, device_name,
1678 EXT2_BLOCK_SIZE(&fs_param));
1682 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1684 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1685 fs_types[0] = strdup("journal");
1690 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1691 print_str_list(fs_types);
1694 if (r_opt == EXT2_GOOD_OLD_REV &&
1695 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1696 fs_param.s_feature_ro_compat)) {
1697 fprintf(stderr, _("Filesystem features not supported "
1698 "with revision 0 filesystems\n"));
1703 if (r_opt == EXT2_GOOD_OLD_REV) {
1704 fprintf(stderr, _("Sparse superblocks not supported "
1705 "with revision 0 filesystems\n"));
1708 fs_param.s_feature_ro_compat |=
1709 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1710 } else if (s_opt == 0)
1711 fs_param.s_feature_ro_compat &=
1712 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1714 if (journal_size != 0) {
1715 if (r_opt == EXT2_GOOD_OLD_REV) {
1716 fprintf(stderr, _("Journals not supported "
1717 "with revision 0 filesystems\n"));
1720 fs_param.s_feature_compat |=
1721 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1724 /* Get reserved_ratio from profile if not specified on cmd line. */
1725 if (reserved_ratio < 0.0) {
1726 reserved_ratio = get_double_from_profile(
1727 fs_types, "reserved_ratio", 5.0);
1728 if (reserved_ratio > 50 || reserved_ratio < 0) {
1729 com_err(program_name, 0,
1730 _("invalid reserved blocks percent - %lf"),
1736 if (fs_param.s_feature_incompat &
1737 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1739 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1740 fs_param.s_feature_compat = 0;
1741 fs_param.s_feature_ro_compat = 0;
1744 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1745 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1746 fprintf(stderr, _("The resize_inode and meta_bg features "
1747 "are not compatible.\n"
1748 "They can not be both enabled "
1749 "simultaneously.\n"));
1753 /* Set first meta blockgroup via an environment variable */
1754 /* (this is mostly for debugging purposes) */
1755 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1756 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1757 fs_param.s_first_meta_bg = atoi(tmp);
1759 /* Get the hardware sector sizes, if available */
1760 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1762 com_err(program_name, retval,
1763 _("while trying to determine hardware sector size"));
1766 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1768 com_err(program_name, retval,
1769 _("while trying to determine physical sector size"));
1773 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1774 lsector_size = atoi(tmp);
1775 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1776 psector_size = atoi(tmp);
1778 /* Older kernels may not have physical/logical distinction */
1780 psector_size = lsector_size;
1782 if (blocksize <= 0) {
1783 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1785 if (use_bsize == -1) {
1786 use_bsize = sys_page_size;
1787 if ((linux_version_code < (2*65536 + 6*256)) &&
1791 if (lsector_size && use_bsize < lsector_size)
1792 use_bsize = lsector_size;
1793 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1794 use_bsize = -blocksize;
1795 blocksize = use_bsize;
1796 ext2fs_blocks_count_set(&fs_param,
1797 ext2fs_blocks_count(&fs_param) /
1798 (blocksize / 1024));
1800 if (blocksize < lsector_size) { /* Impossible */
1801 com_err(program_name, EINVAL,
1802 _("while setting blocksize; too small "
1805 } else if ((blocksize < psector_size) &&
1806 (psector_size <= sys_page_size)) { /* Suboptimal */
1807 fprintf(stderr, _("Warning: specified blocksize %d is "
1808 "less than device physical sectorsize %d\n"),
1809 blocksize, psector_size);
1813 fs_param.s_log_block_size =
1814 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1815 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1817 cluster_size = get_int_from_profile(fs_types,
1820 fs_param.s_log_cluster_size =
1821 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1823 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1825 if (inode_ratio == 0) {
1826 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1828 if (inode_ratio < blocksize)
1829 inode_ratio = blocksize;
1830 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1831 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1834 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1835 retval = get_device_geometry(device_name, &fs_param, psector_size);
1838 _("warning: Unable to get device geometry for %s\n"),
1840 } else if (retval) {
1841 printf(_("%s alignment is offset by %lu bytes.\n"),
1842 device_name, retval);
1843 printf(_("This may result in very poor performance, "
1844 "(re)-partitioning suggested.\n"));
1848 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1850 /* This check should happen beyond the last assignment to blocksize */
1851 if (blocksize > sys_page_size) {
1853 com_err(program_name, 0,
1854 _("%d-byte blocks too big for system (max %d)"),
1855 blocksize, sys_page_size);
1858 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1859 "(max %d), forced to continue\n"),
1860 blocksize, sys_page_size);
1863 lazy_itable_init = 0;
1864 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1865 lazy_itable_init = 1;
1867 lazy_itable_init = get_bool_from_profile(fs_types,
1870 discard = get_bool_from_profile(fs_types, "discard" , discard);
1871 journal_flags |= get_bool_from_profile(fs_types,
1872 "lazy_journal_init", 0) ?
1873 EXT2_MKJOURNAL_LAZYINIT : 0;
1875 /* Get options from profile */
1876 for (cpp = fs_types; *cpp; cpp++) {
1878 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1880 parse_extended_opts(&fs_param, tmp);
1885 parse_extended_opts(&fs_param, extended_opts);
1887 /* Since sparse_super is the default, we would only have a problem
1888 * here if it was explicitly disabled.
1890 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1891 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1892 com_err(program_name, 0,
1893 _("reserved online resize blocks not supported "
1894 "on non-sparse filesystem"));
1898 if (fs_param.s_blocks_per_group) {
1899 if (fs_param.s_blocks_per_group < 256 ||
1900 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1901 com_err(program_name, 0,
1902 _("blocks per group count out of range"));
1907 if (inode_size == 0)
1908 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1909 if (!flex_bg_size && (fs_param.s_feature_incompat &
1910 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1911 flex_bg_size = get_int_from_profile(fs_types,
1912 "flex_bg_size", 16);
1914 if (!(fs_param.s_feature_incompat &
1915 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1916 com_err(program_name, 0,
1917 _("Flex_bg feature not enabled, so "
1918 "flex_bg size may not be specified"));
1921 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1924 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1925 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1926 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1927 inode_size & (inode_size - 1)) {
1928 com_err(program_name, 0,
1929 _("invalid inode size %d (min %d/max %d)"),
1930 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1934 fs_param.s_inode_size = inode_size;
1937 /* Make sure number of inodes specified will fit in 32 bits */
1938 if (num_inodes == 0) {
1939 unsigned long long n;
1940 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1941 if (n > MAX_32_NUM) {
1942 if (fs_param.s_feature_incompat &
1943 EXT4_FEATURE_INCOMPAT_64BIT)
1944 num_inodes = MAX_32_NUM;
1946 com_err(program_name, 0,
1947 _("too many inodes (%llu), raise "
1948 "inode ratio?"), n);
1952 } else if (num_inodes > MAX_32_NUM) {
1953 com_err(program_name, 0,
1954 _("too many inodes (%llu), specify < 2^32 inodes"),
1959 * Calculate number of inodes based on the inode ratio
1961 fs_param.s_inodes_count = num_inodes ? num_inodes :
1962 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1964 if ((((long long)fs_param.s_inodes_count) *
1965 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1966 ((ext2fs_blocks_count(&fs_param)) *
1967 EXT2_BLOCK_SIZE(&fs_param))) {
1968 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1969 "(%u) too big for a\n\t"
1970 "filesystem with %llu blocks, "
1971 "specify higher inode_ratio (-i)\n\t"
1972 "or lower inode count (-N).\n"),
1973 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1974 fs_param.s_inodes_count,
1975 (unsigned long long) ext2fs_blocks_count(&fs_param));
1980 * Calculate number of blocks to reserve
1982 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1983 ext2fs_blocks_count(&fs_param) / 100.0);
1989 static int should_do_undo(const char *name)
1994 struct ext2_super_block super;
1995 io_manager manager = unix_io_manager;
1996 int csum_flag, force_undo;
1998 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1999 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2000 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2001 if (!force_undo && (!csum_flag || !lazy_itable_init))
2004 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2007 * We don't handle error cases instead we
2008 * declare that the file system doesn't exist
2009 * and let the rest of mke2fs take care of
2016 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2017 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2023 #if defined(WORDS_BIGENDIAN)
2024 s_magic = ext2fs_swab16(super.s_magic);
2026 s_magic = super.s_magic;
2029 if (s_magic == EXT2_SUPER_MAGIC)
2033 io_channel_close(channel);
2040 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2042 errcode_t retval = ENOMEM;
2043 char *tdb_dir = NULL, *tdb_file = NULL;
2044 char *device_name, *tmp_name;
2045 int free_tdb_dir = 0;
2048 * Configuration via a conf file would be
2051 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2053 profile_get_string(profile, "defaults",
2054 "undo_dir", 0, "/var/lib/e2fsprogs",
2059 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2060 access(tdb_dir, W_OK))
2063 tmp_name = strdup(name);
2066 device_name = basename(tmp_name);
2067 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
2072 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
2075 if (!access(tdb_file, F_OK)) {
2076 if (unlink(tdb_file) < 0) {
2082 set_undo_io_backing_manager(*io_ptr);
2083 *io_ptr = undo_io_manager;
2084 retval = set_undo_io_backup_file(tdb_file);
2087 printf(_("Overwriting existing filesystem; this can be undone "
2088 "using the command:\n"
2089 " e2undo %s %s\n\n"), tdb_file, name);
2100 com_err(program_name, retval,
2101 _("while trying to setup undo file\n"));
2105 static int mke2fs_discard_device(ext2_filsys fs)
2107 struct ext2fs_numeric_progress_struct progress;
2108 blk64_t blocks = ext2fs_blocks_count(fs->super);
2109 blk64_t count = DISCARD_STEP_MB;
2114 * Let's try if discard really works on the device, so
2115 * we do not print numeric progress resulting in failure
2118 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2121 cur = fs->blocksize;
2123 count *= (1024 * 1024);
2124 count /= fs->blocksize;
2126 ext2fs_numeric_progress_init(fs, &progress,
2127 _("Discarding device blocks: "),
2129 while (cur < blocks) {
2130 ext2fs_numeric_progress_update(fs, &progress, cur);
2132 if (cur + count > blocks)
2133 count = blocks - cur;
2135 retval = io_channel_discard(fs->io, cur, count);
2142 ext2fs_numeric_progress_close(fs, &progress,
2145 printf("%s\n",error_message(retval));
2147 ext2fs_numeric_progress_close(fs, &progress,
2153 static void fix_cluster_bg_counts(ext2_filsys fs)
2155 blk64_t cluster, num_clusters, tot_free;
2156 int grp_free, num_free, group, num;
2158 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2159 tot_free = num_free = num = group = grp_free = 0;
2160 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2161 cluster < num_clusters; cluster++) {
2162 if (!ext2fs_test_block_bitmap2(fs->block_map,
2163 EXT2FS_C2B(fs, cluster))) {
2168 if ((num == fs->super->s_clusters_per_group) ||
2169 (cluster == num_clusters-1)) {
2170 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2171 ext2fs_group_desc_csum_set(fs, group);
2177 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2180 static int create_quota_inodes(ext2_filsys fs)
2184 init_quota_context(&qctx, fs, -1);
2185 compute_quota(qctx, -1);
2186 write_quota_inode(qctx, USRQUOTA);
2187 write_quota_inode(qctx, GRPQUOTA);
2188 release_quota_context(&qctx);
2193 int main (int argc, char *argv[])
2195 errcode_t retval = 0;
2197 badblocks_list bb_list = 0;
2198 unsigned int journal_blocks;
2199 unsigned int i, checkinterval;
2205 char tdb_string[40];
2207 int itable_zeroed = 0;
2210 setlocale(LC_MESSAGES, "");
2211 setlocale(LC_CTYPE, "");
2212 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2213 textdomain(NLS_CAT_NAME);
2214 set_com_err_gettext(gettext);
2218 #ifdef CONFIG_TESTIO_DEBUG
2219 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2220 io_ptr = test_io_manager;
2221 test_io_backing_manager = unix_io_manager;
2224 io_ptr = unix_io_manager;
2226 if (should_do_undo(device_name)) {
2227 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2233 * Initialize the superblock....
2235 flags = EXT2_FLAG_EXCLUSIVE;
2236 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2239 flags |= EXT2_FLAG_64BITS;
2241 * By default, we print how many inode tables or block groups
2242 * or whatever we've written so far. The quiet flag disables
2243 * this, along with a lot of other output.
2246 flags |= EXT2_FLAG_PRINT_PROGRESS;
2247 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2249 com_err(device_name, retval, _("while setting up superblock"));
2253 /* Can't undo discard ... */
2254 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2255 retval = mke2fs_discard_device(fs);
2256 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2258 printf(_("Discard succeeded and will return 0s "
2259 " - skipping inode table wipe\n"));
2260 lazy_itable_init = 1;
2265 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2266 32768 : fs->blocksize * 8);
2267 io_channel_set_options(fs->io, tdb_string);
2269 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2270 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2272 if ((fs_param.s_feature_incompat &
2273 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2274 (fs_param.s_feature_ro_compat &
2275 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2276 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2277 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2278 fs->super->s_kbytes_written = 1;
2281 * Wipe out the old on-disk superblock
2284 zap_sector(fs, 2, 6);
2287 * Parse or generate a UUID for the filesystem
2290 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2291 com_err(device_name, 0, "could not parse UUID: %s\n",
2296 uuid_generate(fs->super->s_uuid);
2299 * Initialize the directory index variables
2301 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2303 hash_alg = e2p_string2hash(hash_alg_str);
2305 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2307 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2310 * Periodic checks can be enabled/disabled via config file.
2311 * Note we override the kernel include file's idea of what the default
2312 * check interval (never) should be. It's a good idea to check at
2313 * least *occasionally*, specially since servers will never rarely get
2314 * to reboot, since Linux is so robust these days. :-)
2316 * 180 days (six months) seems like a good value.
2318 #ifdef EXT2_DFL_CHECKINTERVAL
2319 #undef EXT2_DFL_CHECKINTERVAL
2321 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2323 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2324 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2325 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2327 * Add "jitter" to the superblock's check interval so that we
2328 * don't check all the filesystems at the same time. We use a
2329 * kludgy hack of using the UUID to derive a random jitter value
2331 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2332 val += fs->super->s_uuid[i];
2333 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2335 fs->super->s_max_mnt_count = -1;
2338 * Override the creator OS, if applicable
2340 if (creator_os && !set_os(fs->super, creator_os)) {
2341 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2346 * For the Hurd, we will turn off filetype since it doesn't
2349 if (fs->super->s_creator_os == EXT2_OS_HURD)
2350 fs->super->s_feature_incompat &=
2351 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2354 * Set the volume label...
2357 memset(fs->super->s_volume_name, 0,
2358 sizeof(fs->super->s_volume_name));
2359 strncpy(fs->super->s_volume_name, volume_label,
2360 sizeof(fs->super->s_volume_name));
2364 * Set the last mount directory
2367 memset(fs->super->s_last_mounted, 0,
2368 sizeof(fs->super->s_last_mounted));
2369 strncpy(fs->super->s_last_mounted, mount_dir,
2370 sizeof(fs->super->s_last_mounted));
2373 if (!quiet || noaction)
2379 if (fs->super->s_feature_incompat &
2380 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2381 create_journal_dev(fs);
2382 exit(ext2fs_close(fs) ? 1 : 0);
2385 if (bad_blocks_filename)
2386 read_bb_file(fs, &bb_list, bad_blocks_filename);
2388 test_disk(fs, &bb_list);
2390 handle_bad_blocks(fs, bb_list);
2391 fs->stride = fs_stride = fs->super->s_raid_stride;
2393 printf(_("Allocating group tables: "));
2394 retval = ext2fs_allocate_tables(fs);
2396 com_err(program_name, retval,
2397 _("while trying to allocate filesystem tables"));
2401 printf(_("done \n"));
2403 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2405 com_err(program_name, retval,
2406 _("\n\twhile converting subcluster bitmap"));
2411 fs->super->s_state |= EXT2_ERROR_FS;
2412 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2414 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2415 blk64_t rsv = 65536 / fs->blocksize;
2416 blk64_t blocks = ext2fs_blocks_count(fs->super);
2420 #ifdef ZAP_BOOTBLOCK
2421 zap_sector(fs, 0, 2);
2425 * Wipe out any old MD RAID (or other) metadata at the end
2426 * of the device. This will also verify that the device is
2427 * as large as we think. Be careful with very small devices.
2429 start = (blocks & ~(rsv - 1));
2433 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2437 com_err(program_name, retval,
2438 _("while zeroing block %llu at end of filesystem"),
2441 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2442 create_root_dir(fs);
2443 create_lost_and_found(fs);
2445 create_bad_block_inode(fs, bb_list);
2446 if (fs->super->s_feature_compat &
2447 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2448 retval = ext2fs_create_resize_inode(fs);
2450 com_err("ext2fs_create_resize_inode", retval,
2451 _("while reserving blocks for online resize"));
2457 if (journal_device) {
2461 check_plausibility(journal_device);
2462 check_mount(journal_device, force, _("journal"));
2464 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2465 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2466 fs->blocksize, unix_io_manager, &jfs);
2468 com_err(program_name, retval,
2469 _("while trying to open journal device %s\n"),
2474 printf(_("Adding journal to device %s: "),
2478 retval = ext2fs_add_journal_device(fs, jfs);
2480 com_err (program_name, retval,
2481 _("\n\twhile trying to add journal to device %s"),
2486 printf(_("done\n"));
2488 free(journal_device);
2489 } else if ((journal_size) ||
2490 (fs_param.s_feature_compat &
2491 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2492 journal_blocks = figure_journal_size(journal_size, fs);
2495 printf(_("Skipping journal creation in super-only mode\n"));
2496 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2500 if (!journal_blocks) {
2501 fs->super->s_feature_compat &=
2502 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2506 printf(_("Creating journal (%u blocks): "),
2510 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2513 com_err (program_name, retval,
2514 _("\n\twhile trying to create journal"));
2518 printf(_("done\n"));
2522 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2523 retval = ext2fs_mmp_init(fs);
2525 fprintf(stderr, _("\nError while enabling multiple "
2526 "mount protection feature."));
2530 printf(_("Multiple mount protection is enabled "
2531 "with update interval %d seconds.\n"),
2532 fs->super->s_mmp_update_interval);
2535 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2536 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2537 fix_cluster_bg_counts(fs);
2538 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2539 EXT4_FEATURE_RO_COMPAT_QUOTA))
2540 create_quota_inodes(fs);
2543 printf(_("Writing superblocks and "
2544 "filesystem accounting information: "));
2545 checkinterval = fs->super->s_checkinterval;
2546 max_mnt_count = fs->super->s_max_mnt_count;
2547 retval = ext2fs_close(fs);
2550 _("\nWarning, had trouble writing out superblocks."));
2551 } else if (!quiet) {
2552 printf(_("done\n\n"));
2553 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2554 print_check_message(max_mnt_count, checkinterval);
2557 remove_error_table(&et_ext2_error_table);
2558 remove_error_table(&et_prof_error_table);
2559 profile_release(profile);
2560 for (i=0; fs_types[i]; i++)