Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main (int argc, char *argv[])
- {
- errcode_t retval = 0;
- ext2_filsys fs;
- badblocks_list bb_list = 0;
- unsigned int journal_blocks;
- unsigned int i, checkinterval;
- int max_mnt_count;
- int val, hash_alg;
- int flags;
- int old_bitmaps;
- io_manager io_ptr;
- char tdb_string[40];
- char *hash_alg_str;
- int itable_zeroed = 0;
- #ifdef ENABLE_NLS
- setlocale(LC_MESSAGES, "");
- setlocale(LC_CTYPE, "");
- bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
- textdomain(NLS_CAT_NAME);
- set_com_err_gettext(gettext);
- #endif
- PRS(argc, argv);
- #ifdef CONFIG_TESTIO_DEBUG
- if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
- io_ptr = test_io_manager;
- test_io_backing_manager = unix_io_manager;
- } else
- #endif
- io_ptr = unix_io_manager;
- if (should_do_undo(device_name)) {
- retval = mke2fs_setup_tdb(device_name, &io_ptr);
- if (retval)
- exit(1);
- }
- /*
- * Initialize the superblock....
- */
- flags = EXT2_FLAG_EXCLUSIVE;
- profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
- &old_bitmaps);
- if (!old_bitmaps)
- flags |= EXT2_FLAG_64BITS;
- /*
- * By default, we print how many inode tables or block groups
- * or whatever we've written so far. The quiet flag disables
- * this, along with a lot of other output.
- */
- if (!quiet)
- flags |= EXT2_FLAG_PRINT_PROGRESS;
- retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
- if (retval) {
- com_err(device_name, retval, _("while setting up superblock"));
- exit(1);
- }
- /* Can't undo discard ... */
- if (!noaction && discard && (io_ptr != undo_io_manager)) {
- retval = mke2fs_discard_device(fs);
- if (!retval && io_channel_discard_zeroes_data(fs->io)) {
- if (verbose)
- printf(_("Discard succeeded and will return 0s "
- " - skipping inode table wipe\n"));
- lazy_itable_init = 1;
- itable_zeroed = 1;
- }
- }
- sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
- 32768 : fs->blocksize * 8);
- io_channel_set_options(fs->io, tdb_string);
- if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
- fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
- if ((fs_param.s_feature_incompat &
- (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
- (fs_param.s_feature_ro_compat &
- (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
- EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
- EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
- fs->super->s_kbytes_written = 1;
- /*
- * Wipe out the old on-disk superblock
- */
- if (!noaction)
- zap_sector(fs, 2, 6);
- /*
- * Parse or generate a UUID for the filesystem
- */
- if (fs_uuid) {
- if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
- com_err(device_name, 0, "could not parse UUID: %s\n",
- fs_uuid);
- exit(1);
- }
- } else
- uuid_generate(fs->super->s_uuid);
- /*
- * Initialize the directory index variables
- */
- hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
- "half_md4");
- hash_alg = e2p_string2hash(hash_alg_str);
- free(hash_alg_str);
- fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
- EXT2_HASH_HALF_MD4;
- uuid_generate((unsigned char *) fs->super->s_hash_seed);
- /*
- * Periodic checks can be enabled/disabled via config file.
- * Note we override the kernel include file's idea of what the default
- * check interval (never) should be. It's a good idea to check at
- * least *occasionally*, specially since servers will never rarely get
- * to reboot, since Linux is so robust these days. :-)
- *
- * 180 days (six months) seems like a good value.
- */
- #ifdef EXT2_DFL_CHECKINTERVAL
- #undef EXT2_DFL_CHECKINTERVAL
- #endif
- #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
- if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
- fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
- fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
- /*
- * Add "jitter" to the superblock's check interval so that we
- * don't check all the filesystems at the same time. We use a
- * kludgy hack of using the UUID to derive a random jitter value
- */
- for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
- val += fs->super->s_uuid[i];
- fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
- } else
- fs->super->s_max_mnt_count = -1;
- /*
- * Override the creator OS, if applicable
- */
- if (creator_os && !set_os(fs->super, creator_os)) {
- com_err (program_name, 0, _("unknown os - %s"), creator_os);
- exit(1);
- }
- /*
- * For the Hurd, we will turn off filetype since it doesn't
- * support it.
- */
- if (fs->super->s_creator_os == EXT2_OS_HURD)
- fs->super->s_feature_incompat &=
- ~EXT2_FEATURE_INCOMPAT_FILETYPE;
- /*
- * Set the volume label...
- */
- if (volume_label) {
- memset(fs->super->s_volume_name, 0,
- sizeof(fs->super->s_volume_name));
- strncpy(fs->super->s_volume_name, volume_label,
- sizeof(fs->super->s_volume_name));
- }
- /*
- * Set the last mount directory
- */
- if (mount_dir) {
- memset(fs->super->s_last_mounted, 0,
- sizeof(fs->super->s_last_mounted));
- strncpy(fs->super->s_last_mounted, mount_dir,
- sizeof(fs->super->s_last_mounted));
- }
- if (!quiet || noaction)
- show_stats(fs);
- if (noaction)
- exit(0);
- if (fs->super->s_feature_incompat &
- EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
- create_journal_dev(fs);
- exit(ext2fs_close(fs) ? 1 : 0);
- }
- if (bad_blocks_filename)
- read_bb_file(fs, &bb_list, bad_blocks_filename);
- if (cflag)
- test_disk(fs, &bb_list);
- handle_bad_blocks(fs, bb_list);
- fs->stride = fs_stride = fs->super->s_raid_stride;
- if (!quiet)
- printf(_("Allocating group tables: "));
- retval = ext2fs_allocate_tables(fs);
- if (retval) {
- com_err(program_name, retval,
- _("while trying to allocate filesystem tables"));
- exit(1);
- }
- if (!quiet)
- printf(_("done \n"));
- retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
- if (retval) {
- com_err(program_name, retval,
- _("\n\twhile converting subcluster bitmap"));
- exit(1);
- }
- if (super_only) {
- fs->super->s_state |= EXT2_ERROR_FS;
- fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
- } else {
- /* rsv must be a power of two (64kB is MD RAID sb alignment) */
- blk64_t rsv = 65536 / fs->blocksize;
- blk64_t blocks = ext2fs_blocks_count(fs->super);
- blk64_t start;
- blk64_t ret_blk;
- #ifdef ZAP_BOOTBLOCK
- zap_sector(fs, 0, 2);
- #endif
- /*
- * Wipe out any old MD RAID (or other) metadata at the end
- * of the device. This will also verify that the device is
- * as large as we think. Be careful with very small devices.
- */
- start = (blocks & ~(rsv - 1));
- if (start > rsv)
- start -= rsv;
- if (start > 0)
- retval = ext2fs_zero_blocks2(fs, start, blocks - start,
- &ret_blk, NULL);
- if (retval) {
- com_err(program_name, retval,
- _("while zeroing block %llu at end of filesystem"),
- ret_blk);
- }
- write_inode_tables(fs, lazy_itable_init, itable_zeroed);
- create_root_dir(fs);
- create_lost_and_found(fs);
- reserve_inodes(fs);
- create_bad_block_inode(fs, bb_list);
- if (fs->super->s_feature_compat &
- EXT2_FEATURE_COMPAT_RESIZE_INODE) {
- retval = ext2fs_create_resize_inode(fs);
- if (retval) {
- com_err("ext2fs_create_resize_inode", retval,
- _("while reserving blocks for online resize"));
- exit(1);
- }
- }
- }
- if (journal_device) {
- ext2_filsys jfs;
- if (!force)
- check_plausibility(journal_device);
- check_mount(journal_device, force, _("journal"));
- retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
- EXT2_FLAG_JOURNAL_DEV_OK, 0,
- fs->blocksize, unix_io_manager, &jfs);
- if (retval) {
- com_err(program_name, retval,
- _("while trying to open journal device %s\n"),
- journal_device);
- exit(1);
- }
- if (!quiet) {
- printf(_("Adding journal to device %s: "),
- journal_device);
- fflush(stdout);
- }
- retval = ext2fs_add_journal_device(fs, jfs);
- if(retval) {
- com_err (program_name, retval,
- _("\n\twhile trying to add journal to device %s"),
- journal_device);
- exit(1);
- }
- if (!quiet)
- printf(_("done\n"));
- ext2fs_close(jfs);
- free(journal_device);
- } else if ((journal_size) ||
- (fs_param.s_feature_compat &
- EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
- journal_blocks = figure_journal_size(journal_size, fs);
- if (super_only) {
- printf(_("Skipping journal creation in super-only mode\n"));
- fs->super->s_journal_inum = EXT2_JOURNAL_INO;
- goto no_journal;
- }
- if (!journal_blocks) {
- fs->super->s_feature_compat &=
- ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
- goto no_journal;
- }
- if (!quiet) {
- printf(_("Creating journal (%u blocks): "),
- journal_blocks);
- fflush(stdout);
- }
- retval = ext2fs_add_journal_inode(fs, journal_blocks,
- journal_flags);
- if (retval) {
- com_err (program_name, retval,
- _("\n\twhile trying to create journal"));
- exit(1);
- }
- if (!quiet)
- printf(_("done\n"));
- }
- no_journal:
- if (!super_only &&
- fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
- retval = ext2fs_mmp_init(fs);
- if (retval) {
- fprintf(stderr, _("\nError while enabling multiple "
- "mount protection feature."));
- exit(1);
- }
- if (!quiet)
- printf(_("Multiple mount protection is enabled "
- "with update interval %d seconds.\n"),
- fs->super->s_mmp_update_interval);
- }
- if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
- EXT4_FEATURE_RO_COMPAT_BIGALLOC))
- fix_cluster_bg_counts(fs);
- if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
- EXT4_FEATURE_RO_COMPAT_QUOTA))
- create_quota_inodes(fs);
- if (!quiet)
- printf(_("Writing superblocks and "
- "filesystem accounting information: "));
- checkinterval = fs->super->s_checkinterval;
- max_mnt_count = fs->super->s_max_mnt_count;
- retval = ext2fs_close(fs);
- if (retval) {
- fprintf(stderr,
- _("\nWarning, had trouble writing out superblocks."));
- } else if (!quiet) {
- printf(_("done\n\n"));
- if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
- print_check_message(max_mnt_count, checkinterval);
- }
- remove_error_table(&et_ext2_error_table);
- remove_error_table(&et_prof_error_table);
- profile_release(profile);
- for (i=0; fs_types[i]; i++)
- free(fs_types[i]);
- free(fs_types);
- return retval;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement