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
73 extern int isatty(int);
74 extern FILE *fpopen(const char *cmd, const char *mode);
76 const char * program_name = "mke2fs";
77 const char * device_name /* = NULL */;
79 /* Command line options */
90 char *bad_blocks_filename;
93 struct ext2_super_block fs_param;
99 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
104 int sys_page_size = 4096;
105 int linux_version_code = 0;
107 static void usage(void)
109 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
110 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
111 "[-J journal-options]\n"
112 "\t[-G meta group size] [-N number-of-inodes]\n"
113 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
114 "\t[-g blocks-per-group] [-L volume-label] "
115 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
116 "[-r fs-revision] [-E extended-option[,...]]\n"
117 "\t[-T fs-type] [-U UUID] [-jnqvFKSV] device [blocks-count]\n"),
122 static int int_log2(int arg)
134 static int int_log10(unsigned int arg)
143 static int parse_version_number(const char *s)
145 int major, minor, rev;
151 major = strtol(cp, &endptr, 10);
152 if (cp == endptr || *endptr != '.')
155 minor = strtol(cp, &endptr, 10);
156 if (cp == endptr || *endptr != '.')
159 rev = strtol(cp, &endptr, 10);
162 return ((((major * 256) + minor) * 256) + rev);
166 * Helper function for read_bb_file and test_disk
168 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
170 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
175 * Reads the bad blocks list from a file
177 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
178 const char *bad_blocks_file)
183 f = fopen(bad_blocks_file, "r");
185 com_err("read_bad_blocks_file", errno,
186 _("while trying to open %s"), bad_blocks_file);
189 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
192 com_err("ext2fs_read_bb_FILE", retval,
193 _("while reading in list of bad blocks from file"));
199 * Runs the badblocks program to test the disk
201 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
207 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
208 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
209 fs->device_name, ext2fs_blocks_count(fs->super)-1);
211 printf(_("Running command: %s\n"), buf);
214 com_err("popen", errno,
215 _("while trying to run '%s'"), buf);
218 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
221 com_err("ext2fs_read_bb_FILE", retval,
222 _("while processing list of bad blocks from program"));
227 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
231 unsigned must_be_good;
233 badblocks_iterate bb_iter;
243 * The primary superblock and group descriptors *must* be
244 * good; if not, abort.
246 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
247 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
248 if (ext2fs_badblocks_list_test(bb_list, i)) {
249 fprintf(stderr, _("Block %d in primary "
250 "superblock/group descriptor area bad.\n"), i);
251 fprintf(stderr, _("Blocks %u through %u must be good "
252 "in order to build a filesystem.\n"),
253 fs->super->s_first_data_block, must_be_good);
254 fputs(_("Aborting....\n"), stderr);
260 * See if any of the bad blocks are showing up in the backup
261 * superblocks and/or group descriptors. If so, issue a
262 * warning and adjust the block counts appropriately.
264 group_block = fs->super->s_first_data_block +
265 fs->super->s_blocks_per_group;
267 for (i = 1; i < fs->group_desc_count; i++) {
269 for (j=0; j < fs->desc_blocks+1; j++) {
270 if (ext2fs_badblocks_list_test(bb_list,
274 _("Warning: the backup superblock/group descriptors at block %u contain\n"
278 group = ext2fs_group_of_blk2(fs, group_block+j);
279 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
280 ext2fs_group_desc_csum_set(fs, group);
281 ext2fs_free_blocks_count_add(fs->super, 1);
284 group_block += fs->super->s_blocks_per_group;
288 * Mark all the bad blocks as used...
290 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
292 com_err("ext2fs_badblocks_list_iterate_begin", retval,
293 _("while marking bad blocks as used"));
296 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
297 ext2fs_mark_block_bitmap2(fs->block_map, blk);
298 ext2fs_badblocks_list_iterate_end(bb_iter);
301 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
307 struct ext2fs_numeric_progress_struct progress;
309 ext2fs_numeric_progress_init(fs, &progress,
310 _("Writing inode tables: "),
311 fs->group_desc_count);
313 for (i = 0; i < fs->group_desc_count; i++) {
314 ext2fs_numeric_progress_update(fs, &progress, i);
316 blk = ext2fs_inode_table_loc(fs, i);
317 num = fs->inode_blocks_per_group;
320 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
321 num = ((((fs->super->s_inodes_per_group -
322 ext2fs_bg_itable_unused(fs, i)) *
323 EXT2_INODE_SIZE(fs->super)) +
324 EXT2_BLOCK_SIZE(fs->super) - 1) /
325 EXT2_BLOCK_SIZE(fs->super));
327 /* The kernel doesn't need to zero the itable blocks */
328 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
329 ext2fs_group_desc_csum_set(fs, i);
331 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
333 fprintf(stderr, _("\nCould not write %d "
334 "blocks in inode table starting at %u: %s\n"),
335 num, blk, error_message(retval));
339 if (sync_kludge == 1)
341 else if ((i % sync_kludge) == 0)
345 ext2fs_zero_blocks(0, 0, 0, 0, 0);
346 ext2fs_numeric_progress_close(fs, &progress,
350 static void create_root_dir(ext2_filsys fs)
353 struct ext2_inode inode;
356 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
358 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
362 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
364 com_err("ext2fs_read_inode", retval,
365 _("while reading root inode"));
370 ext2fs_set_i_uid_high(inode, uid >> 16);
374 ext2fs_set_i_gid_high(inode, gid >> 16);
376 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
378 com_err("ext2fs_write_inode", retval,
379 _("while setting root inode ownership"));
385 static void create_lost_and_found(ext2_filsys fs)
387 unsigned int lpf_size = 0;
390 const char *name = "lost+found";
394 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
396 com_err("ext2fs_mkdir", retval,
397 _("while creating /lost+found"));
401 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
403 com_err("ext2_lookup", retval,
404 _("while looking up /lost+found"));
408 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
409 /* Ensure that lost+found is at least 2 blocks, so we always
410 * test large empty blocks for big-block filesystems. */
411 if ((lpf_size += fs->blocksize) >= 16*1024 &&
412 lpf_size >= 2 * fs->blocksize)
414 retval = ext2fs_expand_dir(fs, ino);
416 com_err("ext2fs_expand_dir", retval,
417 _("while expanding /lost+found"));
423 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
427 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
428 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
429 retval = ext2fs_update_bb_inode(fs, bb_list);
431 com_err("ext2fs_update_bb_inode", retval,
432 _("while setting bad block inode"));
438 static void reserve_inodes(ext2_filsys fs)
442 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
443 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
444 ext2fs_mark_ib_dirty(fs);
447 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
448 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
449 #define BSD_LABEL_OFFSET 64
451 static void zap_sector(ext2_filsys fs, int sect, int nsect)
457 buf = malloc(512*nsect);
459 printf(_("Out of memory erasing sectors %d-%d\n"),
460 sect, sect + nsect - 1);
465 /* Check for a BSD disklabel, and don't erase it if so */
466 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
469 _("Warning: could not read block 0: %s\n"),
470 error_message(retval));
472 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
473 if ((*magic == BSD_DISKMAGIC) ||
474 (*magic == BSD_MAGICDISK))
479 memset(buf, 0, 512*nsect);
480 io_channel_set_blksize(fs->io, 512);
481 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
482 io_channel_set_blksize(fs->io, fs->blocksize);
485 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
486 sect, error_message(retval));
489 static void create_journal_dev(ext2_filsys fs)
491 struct ext2fs_numeric_progress_struct progress;
495 int c, count, err_count;
497 retval = ext2fs_create_journal_superblock(fs,
498 ext2fs_blocks_count(fs->super), 0, &buf);
500 com_err("create_journal_dev", retval,
501 _("while initializing journal superblock"));
504 ext2fs_numeric_progress_init(fs, &progress,
505 _("Zeroing journal device: "),
506 ext2fs_blocks_count(fs->super));
508 count = ext2fs_blocks_count(fs->super);
514 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
516 com_err("create_journal_dev", retval,
517 _("while zeroing journal device "
518 "(block %u, count %d)"),
524 ext2fs_numeric_progress_update(fs, &progress, blk);
526 ext2fs_zero_blocks(0, 0, 0, 0, 0);
528 retval = io_channel_write_blk64(fs->io,
529 fs->super->s_first_data_block+1,
532 com_err("create_journal_dev", retval,
533 _("while writing journal superblock"));
536 ext2fs_numeric_progress_close(fs, &progress, NULL);
539 static void show_stats(ext2_filsys fs)
541 struct ext2_super_block *s = fs->super;
548 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
549 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
550 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
552 memset(buf, 0, sizeof(buf));
553 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
554 printf(_("Filesystem label=%s\n"), buf);
555 fputs(_("OS type: "), stdout);
556 os = e2p_os2string(fs->super->s_creator_os);
560 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
561 s->s_log_block_size);
562 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
564 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
565 s->s_raid_stride, s->s_raid_stripe_width);
566 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
567 ext2fs_blocks_count(s));
568 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
569 ext2fs_r_blocks_count(s),
570 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
571 printf(_("First data block=%u\n"), s->s_first_data_block);
572 if (s->s_reserved_gdt_blocks)
573 printf(_("Maximum filesystem blocks=%lu\n"),
574 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
575 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
576 if (fs->group_desc_count > 1)
577 printf(_("%u block groups\n"), fs->group_desc_count);
579 printf(_("%u block group\n"), fs->group_desc_count);
580 printf(_("%u blocks per group, %u fragments per group\n"),
581 s->s_blocks_per_group, s->s_frags_per_group);
582 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
584 if (fs->group_desc_count == 1) {
589 printf(_("Superblock backups stored on blocks: "));
590 group_block = s->s_first_data_block;
592 for (i = 1; i < fs->group_desc_count; i++) {
593 group_block += s->s_blocks_per_group;
594 if (!ext2fs_bg_has_super(fs, i))
598 need = int_log10(group_block) + 2;
599 if (need > col_left) {
604 printf("%u", group_block);
610 * Set the S_CREATOR_OS field. Return true if OS is known,
613 static int set_os(struct ext2_super_block *sb, char *os)
616 sb->s_creator_os = atoi (os);
617 else if (strcasecmp(os, "linux") == 0)
618 sb->s_creator_os = EXT2_OS_LINUX;
619 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
620 sb->s_creator_os = EXT2_OS_HURD;
621 else if (strcasecmp(os, "freebsd") == 0)
622 sb->s_creator_os = EXT2_OS_FREEBSD;
623 else if (strcasecmp(os, "lites") == 0)
624 sb->s_creator_os = EXT2_OS_LITES;
630 #define PATH_SET "PATH=/sbin"
632 static void parse_extended_opts(struct ext2_super_block *param,
635 char *buf, *token, *next, *p, *arg, *badopt = 0;
643 _("Couldn't allocate memory to parse options!\n"));
647 for (token = buf; token && *token; token = next) {
648 p = strchr(token, ',');
654 arg = strchr(token, '=');
659 if (strcmp(token, "stride") == 0) {
665 param->s_raid_stride = strtoul(arg, &p, 0);
666 if (*p || (param->s_raid_stride == 0)) {
668 _("Invalid stride parameter: %s\n"),
673 } else if (strcmp(token, "stripe-width") == 0 ||
674 strcmp(token, "stripe_width") == 0) {
680 param->s_raid_stripe_width = strtoul(arg, &p, 0);
681 if (*p || (param->s_raid_stripe_width == 0)) {
683 _("Invalid stripe-width parameter: %s\n"),
688 } else if (!strcmp(token, "resize")) {
689 unsigned long resize, bpg, rsv_groups;
690 unsigned long group_desc_count, desc_blocks;
691 unsigned int gdpb, blocksize;
700 resize = parse_num_blocks(arg,
701 param->s_log_block_size);
705 _("Invalid resize parameter: %s\n"),
710 if (resize <= ext2fs_blocks_count(param)) {
712 _("The resize maximum must be greater "
713 "than the filesystem size.\n"));
718 blocksize = EXT2_BLOCK_SIZE(param);
719 bpg = param->s_blocks_per_group;
722 gdpb = EXT2_DESC_PER_BLOCK(param);
723 group_desc_count = (__u32) ext2fs_div64_ceil(
724 ext2fs_blocks_count(param), bpg);
725 desc_blocks = (group_desc_count +
727 rsv_groups = ext2fs_div_ceil(resize, bpg);
728 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
730 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
731 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
734 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
736 _("On-line resizing not supported with revision 0 filesystems\n"));
740 param->s_feature_compat |=
741 EXT2_FEATURE_COMPAT_RESIZE_INODE;
743 param->s_reserved_gdt_blocks = rsv_gdb;
745 } else if (!strcmp(token, "test_fs")) {
746 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
747 } else if (!strcmp(token, "lazy_itable_init")) {
749 lazy_itable_init = strtoul(arg, &p, 0);
751 lazy_itable_init = 1;
758 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
759 "Extended options are separated by commas, "
760 "and may take an argument which\n"
761 "\tis set off by an equals ('=') sign.\n\n"
762 "Valid extended options are:\n"
763 "\tstride=<RAID per-disk data chunk in blocks>\n"
764 "\tstripe-width=<RAID stride * data disks in blocks>\n"
765 "\tresize=<resize maximum size in blocks>\n"
766 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
768 badopt ? badopt : "");
772 if (param->s_raid_stride &&
773 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
774 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
775 "multiple of stride %u.\n\n"),
776 param->s_raid_stripe_width, param->s_raid_stride);
781 static __u32 ok_features[3] = {
783 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
784 EXT2_FEATURE_COMPAT_RESIZE_INODE |
785 EXT2_FEATURE_COMPAT_DIR_INDEX |
786 EXT2_FEATURE_COMPAT_EXT_ATTR,
788 EXT2_FEATURE_INCOMPAT_FILETYPE|
789 EXT3_FEATURE_INCOMPAT_EXTENTS|
790 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
791 EXT2_FEATURE_INCOMPAT_META_BG|
792 EXT4_FEATURE_INCOMPAT_FLEX_BG,
794 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
795 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
796 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
797 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
798 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
799 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
803 static void syntax_err_report(const char *filename, long err, int line_num)
806 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
807 filename, line_num, error_message(err));
811 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
813 static void edit_feature(const char *str, __u32 *compat_array)
818 if (e2p_edit_feature(str, compat_array, ok_features)) {
819 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
831 static errcode_t init_list(struct str_list *sl)
835 sl->list = malloc((sl->max+1) * sizeof(char *));
842 static errcode_t push_string(struct str_list *sl, const char *str)
846 if (sl->num >= sl->max) {
848 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
853 sl->list[sl->num] = malloc(strlen(str)+1);
854 if (sl->list[sl->num] == 0)
856 strcpy(sl->list[sl->num], str);
858 sl->list[sl->num] = 0;
862 static void print_str_list(char **list)
866 for (cpp = list; *cpp; cpp++) {
867 printf("'%s'", *cpp);
874 static char **parse_fs_type(const char *fs_type,
875 const char *usage_types,
876 struct ext2_super_block *fs_param,
879 const char *ext_type = 0;
881 char *profile_type = 0;
883 const char *size_type;
884 struct str_list list;
888 if (init_list(&list))
891 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
892 !strcasecmp(creator_os, "hurd")))
899 else if (!strcmp(program_name, "mke3fs"))
902 ext_type = strrchr(progname, '/');
908 if (!strncmp(ext_type, "mkfs.", 5)) {
910 if (ext_type[0] == 0)
917 profile_get_string(profile, "defaults", "fs_type", 0,
918 "ext2", &profile_type);
919 ext_type = profile_type;
920 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
924 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
925 !strcmp(ext_type, "ext4dev")) {
926 profile_get_string(profile, "fs_types", ext_type, "features",
929 printf(_("\nWarning! Your mke2fs.conf file does "
930 "not define the %s filesystem type.\n"),
932 printf(_("You probably need to install an updated "
933 "mke2fs.conf file.\n\n"));
938 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
939 if (ext2fs_blocks_count(fs_param) < 3 * meg)
940 size_type = "floppy";
941 else if (ext2fs_blocks_count(fs_param) < 512 * meg)
944 size_type = "default";
947 usage_types = size_type;
949 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
955 strcpy(parse_str, usage_types);
960 push_string(&list, ext_type);
968 push_string(&list, cp);
979 push_string(&list, "hurd");
983 static char *get_string_from_profile(char **fs_types, const char *opt,
989 for (i=0; fs_types[i]; i++);
990 for (i-=1; i >=0 ; i--) {
991 profile_get_string(profile, "fs_types", fs_types[i],
996 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1000 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1005 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1006 for (cpp = fs_types; *cpp; cpp++)
1007 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1011 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1016 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1017 for (cpp = fs_types; *cpp; cpp++)
1018 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1022 extern const char *mke2fs_default_profile;
1023 static const char *default_files[] = { "<default>", 0 };
1025 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1027 * Sets the geometry of a device (stripe/stride), and returns the
1028 * device's alignment offset, if any, or a negative error.
1030 static int ext2fs_get_device_geometry(const char *file,
1031 struct ext2_super_block *fs_param)
1037 unsigned long min_io;
1038 unsigned long opt_io;
1039 struct stat statbuf;
1041 /* Nothing to do for a regular file */
1042 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1045 pr = blkid_new_probe_from_filename(file);
1049 tp = blkid_probe_get_topology(pr);
1053 min_io = blkid_topology_get_minimum_io_size(tp);
1054 opt_io = blkid_topology_get_optimal_io_size(tp);
1055 blocksize = EXT2_BLOCK_SIZE(fs_param);
1057 fs_param->s_raid_stride = min_io / blocksize;
1058 fs_param->s_raid_stripe_width = opt_io / blocksize;
1060 rc = blkid_topology_get_alignment_offset(tp);
1062 blkid_free_probe(pr);
1067 static void PRS(int argc, char *argv[])
1073 int inode_ratio = 0;
1075 unsigned long flex_bg_size = 0;
1076 double reserved_ratio = 5.0;
1077 int lsector_size = 0, psector_size = 0;
1078 int show_version_only = 0;
1079 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1081 char * oldpath = getenv("PATH");
1082 char * extended_opts = 0;
1083 const char * fs_type = 0;
1084 const char * usage_types = 0;
1090 int s_opt = -1, r_opt = -1;
1091 char *fs_features = 0;
1094 int pathlen = sizeof(PATH_SET) + 1;
1097 pathlen += strlen(oldpath);
1098 newpath = malloc(pathlen);
1099 strcpy(newpath, PATH_SET);
1101 /* Update our PATH to include /sbin */
1103 strcat (newpath, ":");
1104 strcat (newpath, oldpath);
1108 tmp = getenv("MKE2FS_SYNC");
1110 sync_kludge = atoi(tmp);
1112 /* Determine the system page size if possible */
1114 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1115 #define _SC_PAGESIZE _SC_PAGE_SIZE
1118 sysval = sysconf(_SC_PAGESIZE);
1120 sys_page_size = sysval;
1121 #endif /* _SC_PAGESIZE */
1122 #endif /* HAVE_SYSCONF */
1124 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1126 profile_set_syntax_err_cb(syntax_err_report);
1127 retval = profile_init(config_fn, &profile);
1128 if (retval == ENOENT) {
1129 profile_init(default_files, &profile);
1130 profile_set_default(profile, mke2fs_default_profile);
1133 setbuf(stdout, NULL);
1134 setbuf(stderr, NULL);
1135 add_error_table(&et_ext2_error_table);
1136 add_error_table(&et_prof_error_table);
1137 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1138 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1145 linux_version_code = parse_version_number(ut.release);
1146 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1147 fs_param.s_rev_level = 0;
1150 if (argc && *argv) {
1151 program_name = get_progname(*argv);
1153 /* If called as mkfs.ext3, create a journal inode */
1154 if (!strcmp(program_name, "mkfs.ext3") ||
1155 !strcmp(program_name, "mke3fs"))
1159 while ((c = getopt (argc, argv,
1160 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1163 blocksize = strtol(optarg, &tmp, 0);
1164 b = (blocksize > 0) ? blocksize : -blocksize;
1165 if (b < EXT2_MIN_BLOCK_SIZE ||
1166 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1167 com_err(program_name, 0,
1168 _("invalid block size - %s"), optarg);
1171 if (blocksize > 4096)
1172 fprintf(stderr, _("Warning: blocksize %d not "
1173 "usable on most systems.\n"),
1176 fs_param.s_log_block_size =
1177 int_log2(blocksize >>
1178 EXT2_MIN_BLOCK_LOG_SIZE);
1180 case 'c': /* Check for bad blocks */
1184 size = strtoul(optarg, &tmp, 0);
1185 if (size < EXT2_MIN_BLOCK_SIZE ||
1186 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1187 com_err(program_name, 0,
1188 _("invalid fragment size - %s"),
1192 fs_param.s_log_frag_size =
1193 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1194 fprintf(stderr, _("Warning: fragments not supported. "
1195 "Ignoring -f option\n"));
1198 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1200 com_err(program_name, 0,
1201 _("Illegal number for blocks per group"));
1204 if ((fs_param.s_blocks_per_group % 8) != 0) {
1205 com_err(program_name, 0,
1206 _("blocks per group must be multiple of 8"));
1211 flex_bg_size = strtoul(optarg, &tmp, 0);
1213 com_err(program_name, 0,
1214 _("Illegal number for flex_bg size"));
1217 if (flex_bg_size < 1 ||
1218 (flex_bg_size & (flex_bg_size-1)) != 0) {
1219 com_err(program_name, 0,
1220 _("flex_bg size must be a power of 2"));
1225 inode_ratio = strtoul(optarg, &tmp, 0);
1226 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1227 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1229 com_err(program_name, 0,
1230 _("invalid inode ratio %s (min %d/max %d)"),
1231 optarg, EXT2_MIN_BLOCK_SIZE,
1232 EXT2_MAX_BLOCK_SIZE);
1237 parse_journal_opts(optarg);
1247 bad_blocks_filename = malloc(strlen(optarg)+1);
1248 if (!bad_blocks_filename) {
1249 com_err(program_name, ENOMEM,
1250 _("in malloc for bad_blocks_filename"));
1253 strcpy(bad_blocks_filename, optarg);
1256 reserved_ratio = strtod(optarg, &tmp);
1257 if ( *tmp || reserved_ratio > 50 ||
1258 reserved_ratio < 0) {
1259 com_err(program_name, 0,
1260 _("invalid reserved blocks percent - %s"),
1269 creator_os = optarg;
1275 r_opt = strtoul(optarg, &tmp, 0);
1277 com_err(program_name, 0,
1278 _("bad revision level - %s"), optarg);
1281 fs_param.s_rev_level = r_opt;
1283 case 's': /* deprecated */
1284 s_opt = atoi(optarg);
1287 inode_size = strtoul(optarg, &tmp, 0);
1289 com_err(program_name, 0,
1290 _("invalid inode size - %s"), optarg);
1301 volume_label = optarg;
1307 num_inodes = strtoul(optarg, &tmp, 0);
1309 com_err(program_name, 0,
1310 _("bad num inodes - %s"), optarg);
1315 fs_features = optarg;
1319 extended_opts = optarg;
1328 usage_types = optarg;
1334 /* Print version number and exit */
1335 show_version_only++;
1341 if ((optind == argc) && !show_version_only)
1343 device_name = argv[optind++];
1345 if (!quiet || show_version_only)
1346 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1349 if (show_version_only) {
1350 fprintf(stderr, _("\tUsing %s\n"),
1351 error_message(EXT2_ET_BASE));
1356 * If there's no blocksize specified and there is a journal
1357 * device, use it to figure out the blocksize
1359 if (blocksize <= 0 && journal_device) {
1363 #ifdef CONFIG_TESTIO_DEBUG
1364 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1365 io_ptr = test_io_manager;
1366 test_io_backing_manager = unix_io_manager;
1369 io_ptr = unix_io_manager;
1370 retval = ext2fs_open(journal_device,
1371 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1374 com_err(program_name, retval,
1375 _("while trying to open journal device %s\n"),
1379 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1380 com_err(program_name, 0,
1381 _("Journal dev blocksize (%d) smaller than "
1382 "minimum blocksize %d\n"), jfs->blocksize,
1386 blocksize = jfs->blocksize;
1387 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1388 fs_param.s_log_block_size =
1389 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1393 if (blocksize > sys_page_size) {
1395 com_err(program_name, 0,
1396 _("%d-byte blocks too big for system (max %d)"),
1397 blocksize, sys_page_size);
1400 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1401 "(max %d), forced to continue\n"),
1402 blocksize, sys_page_size);
1404 if (optind < argc) {
1405 ext2fs_blocks_count_set(&fs_param, parse_num_blocks(argv[optind++],
1406 fs_param.s_log_block_size));
1407 if (!ext2fs_blocks_count(&fs_param)) {
1408 com_err(program_name, 0, _("invalid blocks count - %s"),
1417 check_plausibility(device_name);
1418 check_mount(device_name, force, _("filesystem"));
1420 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1422 if (noaction && ext2fs_blocks_count(&fs_param)) {
1423 dev_size = ext2fs_blocks_count(&fs_param);
1427 retval = ext2fs_get_device_size(device_name,
1428 EXT2_BLOCK_SIZE(&fs_param),
1430 if ((retval == EFBIG) &&
1432 (fs_param.s_log_block_size == 0)) {
1433 fs_param.s_log_block_size = 2;
1439 if (retval == EFBIG) {
1440 blk64_t big_dev_size;
1442 if (blocksize < 4096) {
1443 fs_param.s_log_block_size = 2;
1446 retval = ext2fs_get_device_size2(device_name,
1447 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1449 goto get_size_failure;
1450 if (big_dev_size == (1ULL << 32)) {
1451 dev_size = (blk_t) (big_dev_size - 1);
1454 fprintf(stderr, _("%s: Size of device %s too big "
1455 "to be expressed in 32 bits\n\t"
1456 "using a blocksize of %d.\n"),
1457 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1461 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1462 com_err(program_name, retval,
1463 _("while trying to determine filesystem size"));
1467 if (!ext2fs_blocks_count(&fs_param)) {
1468 if (retval == EXT2_ET_UNIMPLEMENTED) {
1469 com_err(program_name, 0,
1470 _("Couldn't determine device size; you "
1471 "must specify\nthe size of the "
1475 if (dev_size == 0) {
1476 com_err(program_name, 0,
1477 _("Device size reported to be zero. "
1478 "Invalid partition specified, or\n\t"
1479 "partition table wasn't reread "
1480 "after running fdisk, due to\n\t"
1481 "a modified partition being busy "
1482 "and in use. You may need to reboot\n\t"
1483 "to re-read your partition table.\n"
1487 ext2fs_blocks_count_set(&fs_param, dev_size);
1488 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) {
1489 blk64_t tmp = ext2fs_blocks_count(&fs_param);
1491 tmp &= ~((blk64_t) ((sys_page_size /
1492 EXT2_BLOCK_SIZE(&fs_param))-1));
1493 ext2fs_blocks_count_set(&fs_param, tmp);
1496 } else if (!force && (ext2fs_blocks_count(&fs_param) > dev_size)) {
1497 com_err(program_name, 0,
1498 _("Filesystem larger than apparent device size."));
1502 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1504 fprintf(stderr, _("Failed to parse fs types list\n"));
1508 /* Figure out what features should be enabled */
1511 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1512 tmp = get_string_from_profile(fs_types, "base_features",
1513 "sparse_super,filetype,resize_inode,dir_index");
1514 edit_feature(tmp, &fs_param.s_feature_compat);
1517 for (cpp = fs_types; *cpp; cpp++) {
1519 profile_get_string(profile, "fs_types", *cpp,
1520 "features", "", &tmp);
1522 edit_feature(tmp, &fs_param.s_feature_compat);
1525 tmp = get_string_from_profile(fs_types, "default_features",
1528 edit_feature(fs_features ? fs_features : tmp,
1529 &fs_param.s_feature_compat);
1532 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1533 fs_types[0] = strdup("journal");
1538 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1539 print_str_list(fs_types);
1542 if (r_opt == EXT2_GOOD_OLD_REV &&
1543 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1544 fs_param.s_feature_ro_compat)) {
1545 fprintf(stderr, _("Filesystem features not supported "
1546 "with revision 0 filesystems\n"));
1551 if (r_opt == EXT2_GOOD_OLD_REV) {
1552 fprintf(stderr, _("Sparse superblocks not supported "
1553 "with revision 0 filesystems\n"));
1556 fs_param.s_feature_ro_compat |=
1557 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1558 } else if (s_opt == 0)
1559 fs_param.s_feature_ro_compat &=
1560 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1562 if (journal_size != 0) {
1563 if (r_opt == EXT2_GOOD_OLD_REV) {
1564 fprintf(stderr, _("Journals not supported "
1565 "with revision 0 filesystems\n"));
1568 fs_param.s_feature_compat |=
1569 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1572 if (fs_param.s_feature_incompat &
1573 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1575 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1576 fs_param.s_feature_compat = 0;
1577 fs_param.s_feature_ro_compat = 0;
1580 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1581 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1582 fprintf(stderr, _("The resize_inode and meta_bg features "
1583 "are not compatible.\n"
1584 "They can not be both enabled "
1585 "simultaneously.\n"));
1589 /* Set first meta blockgroup via an environment variable */
1590 /* (this is mostly for debugging purposes) */
1591 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1592 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1593 fs_param.s_first_meta_bg = atoi(tmp);
1595 /* Get the hardware sector sizes, if available */
1596 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1598 com_err(program_name, retval,
1599 _("while trying to determine hardware sector size"));
1602 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1604 com_err(program_name, retval,
1605 _("while trying to determine physical sector size"));
1608 /* Older kernels may not have physical/logical distinction */
1610 psector_size = lsector_size;
1612 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1613 psector_size = atoi(tmp);
1615 if (blocksize <= 0) {
1616 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1618 if (use_bsize == -1) {
1619 use_bsize = sys_page_size;
1620 if ((linux_version_code < (2*65536 + 6*256)) &&
1624 if (psector_size && use_bsize < psector_size)
1625 use_bsize = psector_size;
1626 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1627 use_bsize = -blocksize;
1628 blocksize = use_bsize;
1629 ext2fs_blocks_count_set(&fs_param,
1630 ext2fs_blocks_count(&fs_param) /
1631 (blocksize / 1024));
1633 if (blocksize < lsector_size || /* Impossible */
1634 (!force && (blocksize < psector_size))) { /* Suboptimal */
1635 com_err(program_name, EINVAL,
1636 _("while setting blocksize; too small "
1639 } else if (blocksize < psector_size) {
1640 fprintf(stderr, _("Warning: specified blocksize %d is "
1641 "less than device physical sectorsize %d, "
1642 "forced to continue\n"), blocksize,
1647 if (inode_ratio == 0) {
1648 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1650 if (inode_ratio < blocksize)
1651 inode_ratio = blocksize;
1654 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1655 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1657 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1658 retval = ext2fs_get_device_geometry(device_name, &fs_param);
1661 _("warning: Unable to get device geometry for %s\n"),
1663 } else if (retval) {
1664 printf(_("%s alignment is offset by %lu bytes.\n"),
1665 device_name, retval);
1666 printf(_("This may result in very poor performance, "
1667 "(re)-partitioning suggested.\n"));
1671 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1673 lazy_itable_init = get_bool_from_profile(fs_types,
1674 "lazy_itable_init", 0);
1676 /* Get options from profile */
1677 for (cpp = fs_types; *cpp; cpp++) {
1679 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1681 parse_extended_opts(&fs_param, tmp);
1686 parse_extended_opts(&fs_param, extended_opts);
1688 /* Since sparse_super is the default, we would only have a problem
1689 * here if it was explicitly disabled.
1691 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1692 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1693 com_err(program_name, 0,
1694 _("reserved online resize blocks not supported "
1695 "on non-sparse filesystem"));
1699 if (fs_param.s_blocks_per_group) {
1700 if (fs_param.s_blocks_per_group < 256 ||
1701 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1702 com_err(program_name, 0,
1703 _("blocks per group count out of range"));
1708 if (inode_size == 0)
1709 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1710 if (!flex_bg_size && (fs_param.s_feature_incompat &
1711 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1712 flex_bg_size = get_int_from_profile(fs_types,
1713 "flex_bg_size", 16);
1715 if (!(fs_param.s_feature_incompat &
1716 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1717 com_err(program_name, 0,
1718 _("Flex_bg feature not enabled, so "
1719 "flex_bg size may not be specified"));
1722 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1725 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1726 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1727 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1728 inode_size & (inode_size - 1)) {
1729 com_err(program_name, 0,
1730 _("invalid inode size %d (min %d/max %d)"),
1731 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1735 fs_param.s_inode_size = inode_size;
1738 /* Make sure number of inodes specified will fit in 32 bits */
1739 if (num_inodes == 0) {
1740 unsigned long long n;
1741 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1743 com_err(program_name, 0,
1744 _("too many inodes (%llu), raise inode ratio?"), n);
1747 } else if (num_inodes > ~0U) {
1748 com_err(program_name, 0,
1749 _("too many inodes (%llu), specify < 2^32 inodes"),
1754 * Calculate number of inodes based on the inode ratio
1756 fs_param.s_inodes_count = num_inodes ? num_inodes :
1757 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1759 if ((((long long)fs_param.s_inodes_count) *
1760 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1761 ((ext2fs_blocks_count(&fs_param)) *
1762 EXT2_BLOCK_SIZE(&fs_param))) {
1763 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1764 "(%u) too big for a\n\t"
1765 "filesystem with %llu blocks, "
1766 "specify higher inode_ratio (-i)\n\t"
1767 "or lower inode count (-N).\n"),
1768 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1769 fs_param.s_inodes_count,
1770 (unsigned long long) ext2fs_blocks_count(&fs_param));
1775 * Calculate number of blocks to reserve
1777 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1778 ext2fs_blocks_count(&fs_param) / 100.0);
1781 static int should_do_undo(const char *name)
1786 struct ext2_super_block super;
1787 io_manager manager = unix_io_manager;
1788 int csum_flag, force_undo;
1790 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1791 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1792 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1793 if (!force_undo && (!csum_flag || !lazy_itable_init))
1796 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1799 * We don't handle error cases instead we
1800 * declare that the file system doesn't exist
1801 * and let the rest of mke2fs take care of
1808 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1809 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1815 #if defined(WORDS_BIGENDIAN)
1816 s_magic = ext2fs_swab16(super.s_magic);
1818 s_magic = super.s_magic;
1821 if (s_magic == EXT2_SUPER_MAGIC)
1825 io_channel_close(channel);
1832 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1834 errcode_t retval = 0;
1835 char *tdb_dir, *tdb_file;
1836 char *device_name, *tmp_name;
1839 * Configuration via a conf file would be
1842 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1844 profile_get_string(profile, "defaults",
1845 "undo_dir", 0, "/var/lib/e2fsprogs",
1848 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1849 access(tdb_dir, W_OK))
1852 tmp_name = strdup(name);
1855 com_err(program_name, ENOMEM,
1856 _("Couldn't allocate memory for tdb filename\n"));
1859 device_name = basename(tmp_name);
1860 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1863 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1865 if (!access(tdb_file, F_OK)) {
1866 if (unlink(tdb_file) < 0) {
1868 com_err(program_name, retval,
1869 _("while trying to delete %s"),
1876 set_undo_io_backing_manager(*io_ptr);
1877 *io_ptr = undo_io_manager;
1878 set_undo_io_backup_file(tdb_file);
1879 printf(_("Overwriting existing filesystem; this can be undone "
1880 "using the command:\n"
1881 " e2undo %s %s\n\n"), tdb_file, name);
1891 #define BLKDISCARD _IO(0x12,119)
1894 static void mke2fs_discard_blocks(ext2_filsys fs)
1900 __uint64_t range[2];
1902 blocks = ext2fs_blocks_count(fs->super);
1903 blocksize = EXT2_BLOCK_SIZE(fs->super);
1905 range[1] = blocks * blocksize;
1907 fd = open64(fs->device_name, O_RDWR);
1910 * We don't care about whether the ioctl succeeds; it's only an
1911 * optmization for SSDs or sparse storage.
1914 ret = ioctl(fd, BLKDISCARD, &range);
1916 printf(_("Calling BLKDISCARD from %llu to %llu "),
1917 (unsigned long long) range[0],
1918 (unsigned long long) range[1]);
1920 printf(_("failed.\n"));
1922 printf(_("succeeded.\n"));
1928 #define mke2fs_discard_blocks(fs)
1931 int main (int argc, char *argv[])
1933 errcode_t retval = 0;
1935 badblocks_list bb_list = 0;
1936 unsigned int journal_blocks;
1942 char tdb_string[40];
1946 setlocale(LC_MESSAGES, "");
1947 setlocale(LC_CTYPE, "");
1948 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1949 textdomain(NLS_CAT_NAME);
1953 #ifdef CONFIG_TESTIO_DEBUG
1954 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1955 io_ptr = test_io_manager;
1956 test_io_backing_manager = unix_io_manager;
1959 io_ptr = unix_io_manager;
1961 if (should_do_undo(device_name)) {
1962 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1968 * Initialize the superblock....
1970 flags = EXT2_FLAG_EXCLUSIVE;
1971 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
1974 flags |= EXT2_FLAG_64BITS;
1976 * By default, we print how many inode tables or block groups
1977 * or whatever we've written so far. The quiet flag disables
1978 * this, along with a lot of other output.
1981 flags |= EXT2_FLAG_PRINT_PROGRESS;
1982 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
1984 com_err(device_name, retval, _("while setting up superblock"));
1988 /* Can't undo discard ... */
1989 if (discard && (io_ptr != undo_io_manager))
1990 mke2fs_discard_blocks(fs);
1992 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1993 32768 : fs->blocksize * 8);
1994 io_channel_set_options(fs->io, tdb_string);
1996 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1997 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1999 if ((fs_param.s_feature_incompat &
2000 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2001 (fs_param.s_feature_ro_compat &
2002 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2003 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2004 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2005 fs->super->s_kbytes_written = 1;
2008 * Wipe out the old on-disk superblock
2011 zap_sector(fs, 2, 6);
2014 * Parse or generate a UUID for the filesystem
2017 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2018 com_err(device_name, 0, "could not parse UUID: %s\n",
2023 uuid_generate(fs->super->s_uuid);
2026 * Initialize the directory index variables
2028 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2030 hash_alg = e2p_string2hash(hash_alg_str);
2031 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2033 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2036 * Add "jitter" to the superblock's check interval so that we
2037 * don't check all the filesystems at the same time. We use a
2038 * kludgy hack of using the UUID to derive a random jitter value.
2040 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2041 val += fs->super->s_uuid[i];
2042 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2045 * Override the creator OS, if applicable
2047 if (creator_os && !set_os(fs->super, creator_os)) {
2048 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2053 * For the Hurd, we will turn off filetype since it doesn't
2056 if (fs->super->s_creator_os == EXT2_OS_HURD)
2057 fs->super->s_feature_incompat &=
2058 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2061 * Set the volume label...
2064 memset(fs->super->s_volume_name, 0,
2065 sizeof(fs->super->s_volume_name));
2066 strncpy(fs->super->s_volume_name, volume_label,
2067 sizeof(fs->super->s_volume_name));
2071 * Set the last mount directory
2074 memset(fs->super->s_last_mounted, 0,
2075 sizeof(fs->super->s_last_mounted));
2076 strncpy(fs->super->s_last_mounted, mount_dir,
2077 sizeof(fs->super->s_last_mounted));
2080 if (!quiet || noaction)
2086 if (fs->super->s_feature_incompat &
2087 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2088 create_journal_dev(fs);
2089 exit(ext2fs_close(fs) ? 1 : 0);
2092 if (bad_blocks_filename)
2093 read_bb_file(fs, &bb_list, bad_blocks_filename);
2095 test_disk(fs, &bb_list);
2097 handle_bad_blocks(fs, bb_list);
2098 fs->stride = fs_stride = fs->super->s_raid_stride;
2100 printf(_("Allocating group tables: "));
2101 retval = ext2fs_allocate_tables(fs);
2103 com_err(program_name, retval,
2104 _("while trying to allocate filesystem tables"));
2108 printf(_("done \n"));
2110 fs->super->s_state |= EXT2_ERROR_FS;
2111 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2113 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2114 unsigned int rsv = 65536 / fs->blocksize;
2115 unsigned long blocks = ext2fs_blocks_count(fs->super);
2116 unsigned long start;
2119 #ifdef ZAP_BOOTBLOCK
2120 zap_sector(fs, 0, 2);
2124 * Wipe out any old MD RAID (or other) metadata at the end
2125 * of the device. This will also verify that the device is
2126 * as large as we think. Be careful with very small devices.
2128 start = (blocks & ~(rsv - 1));
2132 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2136 com_err(program_name, retval,
2137 _("while zeroing block %u at end of filesystem"),
2140 write_inode_tables(fs, lazy_itable_init);
2141 create_root_dir(fs);
2142 create_lost_and_found(fs);
2144 create_bad_block_inode(fs, bb_list);
2145 if (fs->super->s_feature_compat &
2146 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2147 retval = ext2fs_create_resize_inode(fs);
2149 com_err("ext2fs_create_resize_inode", retval,
2150 _("while reserving blocks for online resize"));
2156 if (journal_device) {
2160 check_plausibility(journal_device);
2161 check_mount(journal_device, force, _("journal"));
2163 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2164 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2165 fs->blocksize, unix_io_manager, &jfs);
2167 com_err(program_name, retval,
2168 _("while trying to open journal device %s\n"),
2173 printf(_("Adding journal to device %s: "),
2177 retval = ext2fs_add_journal_device(fs, jfs);
2179 com_err (program_name, retval,
2180 _("\n\twhile trying to add journal to device %s"),
2185 printf(_("done\n"));
2187 free(journal_device);
2188 } else if ((journal_size) ||
2189 (fs_param.s_feature_compat &
2190 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2191 journal_blocks = figure_journal_size(journal_size, fs);
2194 printf(_("Skipping journal creation in super-only mode\n"));
2195 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2199 if (!journal_blocks) {
2200 fs->super->s_feature_compat &=
2201 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2205 printf(_("Creating journal (%u blocks): "),
2209 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2212 com_err (program_name, retval,
2213 _("\n\twhile trying to create journal"));
2217 printf(_("done\n"));
2222 printf(_("Writing superblocks and "
2223 "filesystem accounting information: "));
2224 retval = ext2fs_flush(fs);
2227 _("\nWarning, had trouble writing out superblocks."));
2230 printf(_("done\n\n"));
2231 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2232 print_check_message(fs);
2234 val = ext2fs_close(fs);
2235 remove_error_table(&et_ext2_error_table);
2236 remove_error_table(&et_prof_error_table);
2237 profile_release(profile);
2238 return (retval || val) ? 1 : 0;