2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
62 #include "blkid/blkid.h"
63 #include "quota/quotaio.h"
65 #include "../version.h"
66 #include "nls-enable.h"
68 #define QOPT_ENABLE (1)
69 #define QOPT_DISABLE (-1)
71 extern int ask_yn(const char *string, int def);
73 const char *program_name = "tune2fs";
75 char *new_label, *new_last_mounted, *new_UUID;
77 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
78 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
81 static time_t last_check_time;
82 static int print_label;
83 static int max_mount_count, mount_count, mount_flags;
84 static unsigned long interval;
85 static blk64_t reserved_blocks;
86 static double reserved_ratio;
87 static unsigned long resgid, resuid;
88 static unsigned short errors;
90 static char *features_cmd;
91 static char *mntopts_cmd;
92 static int stride, stripe_width;
93 static int stride_set, stripe_width_set;
94 static char *extended_cmd;
95 static unsigned long new_inode_size;
96 static char *ext_mount_opts;
97 static int usrquota, grpquota;
99 int journal_size, journal_flags;
100 char *journal_device;
101 static blk64_t journal_location = ~0LL;
103 static struct list_head blk_move_list;
106 struct list_head list;
112 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
114 #ifdef CONFIG_BUILD_FINDFS
115 void do_findfs(int argc, char **argv);
118 static void usage(void)
121 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
123 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
124 "\t[-m reserved_blocks_percent] "
125 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
126 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
127 "[-L volume_label]\n"
128 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
130 "\t[-Q quota_options]\n"
132 "\t[-E extended-option[,...]] [-T last_check_time] "
133 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
137 static __u32 ok_features[3] = {
139 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
140 EXT2_FEATURE_COMPAT_DIR_INDEX,
142 EXT2_FEATURE_INCOMPAT_FILETYPE |
143 EXT3_FEATURE_INCOMPAT_EXTENTS |
144 EXT4_FEATURE_INCOMPAT_FLEX_BG |
145 EXT4_FEATURE_INCOMPAT_EA_INODE|
146 EXT4_FEATURE_INCOMPAT_MMP|
147 EXT4_FEATURE_INCOMPAT_DIRDATA,
149 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
150 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
151 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
152 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
153 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
155 EXT4_FEATURE_RO_COMPAT_QUOTA |
157 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
160 static __u32 clear_ok_features[3] = {
162 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
163 EXT2_FEATURE_COMPAT_RESIZE_INODE |
164 EXT2_FEATURE_COMPAT_DIR_INDEX,
166 EXT2_FEATURE_INCOMPAT_FILETYPE |
167 EXT4_FEATURE_INCOMPAT_FLEX_BG |
168 EXT4_FEATURE_INCOMPAT_EA_INODE|
169 EXT4_FEATURE_INCOMPAT_MMP|
170 EXT4_FEATURE_INCOMPAT_DIRDATA,
172 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
173 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
174 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
175 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
177 EXT4_FEATURE_RO_COMPAT_QUOTA |
179 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
183 * Try to get journal super block if any
185 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
188 journal_superblock_t *jsb;
190 if (!(jfs->super->s_feature_incompat &
191 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
192 return EXT2_ET_UNSUPP_FEATURE;
195 /* Get the journal superblock */
196 if ((retval = io_channel_read_blk64(jfs->io,
197 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
198 com_err(program_name, retval, "%s",
199 _("while reading journal superblock"));
203 jsb = (journal_superblock_t *) buf;
204 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
205 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
206 fputs(_("Journal superblock not found!\n"), stderr);
207 return EXT2_ET_BAD_MAGIC;
213 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
217 for (i = 0; i < nr_users; i++) {
218 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
219 return &s_users[i * UUID_SIZE];
226 * Remove an external journal from the filesystem
228 static int remove_journal_device(ext2_filsys fs)
232 char buf[SUPERBLOCK_SIZE];
233 journal_superblock_t *jsb;
236 int commit_remove_journal = 0;
240 commit_remove_journal = 1; /* force removal even if error */
242 uuid_unparse(fs->super->s_journal_uuid, buf);
243 journal_path = blkid_get_devname(NULL, "UUID", buf);
247 ext2fs_find_block_device(fs->super->s_journal_dev);
249 goto no_valid_journal;
252 #ifdef CONFIG_TESTIO_DEBUG
253 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
254 io_ptr = test_io_manager;
255 test_io_backing_manager = unix_io_manager;
258 io_ptr = unix_io_manager;
259 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
260 EXT2_FLAG_JOURNAL_DEV_OK, 0,
261 fs->blocksize, io_ptr, &jfs);
263 com_err(program_name, retval, "%s",
264 _("while trying to open external journal"));
265 goto no_valid_journal;
268 if ((retval = get_journal_sb(jfs, buf))) {
269 if (retval == EXT2_ET_UNSUPP_FEATURE)
270 fprintf(stderr, _("%s is not a journal device.\n"),
272 goto no_valid_journal;
275 jsb = (journal_superblock_t *) buf;
276 /* Find the filesystem UUID */
277 nr_users = ntohl(jsb->s_nr_users);
279 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
280 fputs(_("Filesystem's UUID not found on journal device.\n"),
282 commit_remove_journal = 1;
283 goto no_valid_journal;
286 for (i = 0; i < nr_users; i++)
287 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
288 jsb->s_nr_users = htonl(nr_users);
290 /* Write back the journal superblock */
291 retval = io_channel_write_blk64(jfs->io,
292 ext2fs_journal_sb_start(fs->blocksize),
293 -SUPERBLOCK_SIZE, buf);
295 com_err(program_name, retval,
296 "while writing journal superblock.");
297 goto no_valid_journal;
300 commit_remove_journal = 1;
303 if (commit_remove_journal == 0) {
304 fputs(_("Cannot locate journal device. It was NOT removed\n"
305 "Use -f option to remove missing journal device.\n"),
309 fs->super->s_journal_dev = 0;
310 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
311 uuid_clear(fs->super->s_journal_uuid);
312 ext2fs_mark_super_dirty(fs);
313 fputs(_("Journal removed\n"), stdout);
319 /* Helper function for remove_journal_inode */
320 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
321 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
322 blk64_t ref_block EXT2FS_ATTR((unused)),
323 int ref_offset EXT2FS_ATTR((unused)),
324 void *private EXT2FS_ATTR((unused)))
330 ext2fs_unmark_block_bitmap2(fs->block_map, block);
331 group = ext2fs_group_of_blk2(fs, block);
332 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
333 ext2fs_group_desc_csum_set(fs, group);
334 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
339 * Remove the journal inode from the filesystem
341 static errcode_t remove_journal_inode(ext2_filsys fs)
343 struct ext2_inode inode;
345 ino_t ino = fs->super->s_journal_inum;
347 retval = ext2fs_read_inode(fs, ino, &inode);
349 com_err(program_name, retval, "%s",
350 _("while reading journal inode"));
353 if (ino == EXT2_JOURNAL_INO) {
354 retval = ext2fs_read_bitmaps(fs);
356 com_err(program_name, retval, "%s",
357 _("while reading bitmaps"));
360 retval = ext2fs_block_iterate3(fs, ino,
361 BLOCK_FLAG_READ_ONLY, NULL,
362 release_blocks_proc, NULL);
364 com_err(program_name, retval, "%s",
365 _("while clearing journal inode"));
368 memset(&inode, 0, sizeof(inode));
369 ext2fs_mark_bb_dirty(fs);
370 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
372 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
373 retval = ext2fs_write_inode(fs, ino, &inode);
375 com_err(program_name, retval, "%s",
376 _("while writing journal inode"));
379 fs->super->s_journal_inum = 0;
380 ext2fs_mark_super_dirty(fs);
386 * Update the default mount options
388 static int update_mntopts(ext2_filsys fs, char *mntopts)
390 struct ext2_super_block *sb = fs->super;
392 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
393 fprintf(stderr, _("Invalid mount option set: %s\n"),
397 ext2fs_mark_super_dirty(fs);
402 static int check_fsck_needed(ext2_filsys fs)
404 if (fs->super->s_state & EXT2_VALID_FS)
406 printf("\n%s\n", _(please_fsck));
407 if (mount_flags & EXT2_MF_READONLY)
408 printf("%s", _("(and reboot afterwards!)\n"));
412 static void request_fsck_afterwards(ext2_filsys fs)
414 static int requested = 0;
418 fs->super->s_state &= ~EXT2_VALID_FS;
419 printf("\n%s\n", _(please_fsck));
420 if (mount_flags & EXT2_MF_READONLY)
421 printf("%s", _("(and reboot afterwards!)\n"));
425 * Update the feature set as provided by the user.
427 static int update_feature_set(ext2_filsys fs, char *features)
429 struct ext2_super_block *sb = fs->super;
430 struct ext2_group_desc *gd;
431 __u32 old_features[3];
434 unsigned int mask_err;
436 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
437 ((&sb->s_feature_compat)[(type)] & (mask)))
438 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
439 !((&sb->s_feature_compat)[(type)] & (mask)))
440 #define FEATURE_CHANGED(type, mask) ((mask) & \
441 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
443 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
444 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
445 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
447 if (e2p_edit_feature2(features, &sb->s_feature_compat,
448 ok_features, clear_ok_features,
449 &type_err, &mask_err)) {
452 _("Invalid filesystem option set: %s\n"),
454 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
455 fprintf(stderr, _("Clearing filesystem feature '%s' "
457 e2p_feature2string(type_err &
458 E2P_FEATURE_TYPE_MASK,
461 fprintf(stderr, _("Setting filesystem feature '%s' "
463 e2p_feature2string(type_err, mask_err));
467 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
468 if ((mount_flags & EXT2_MF_MOUNTED) &&
469 !(mount_flags & EXT2_MF_READONLY)) {
470 fputs(_("The has_journal feature may only be "
471 "cleared when the filesystem is\n"
472 "unmounted or mounted "
473 "read-only.\n"), stderr);
476 if ((sb->s_feature_incompat &
477 EXT3_FEATURE_INCOMPAT_RECOVER) &&
479 fputs(_("The needs_recovery flag is set. "
480 "Please run e2fsck before clearing\n"
481 "the has_journal flag.\n"), stderr);
484 if (sb->s_journal_inum) {
485 if (remove_journal_inode(fs))
488 if (sb->s_journal_dev) {
489 if (remove_journal_device(fs))
494 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
495 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
496 if (sb->s_feature_incompat &
497 EXT2_FEATURE_INCOMPAT_META_BG) {
498 fputs(_("Setting filesystem feature 'sparse_super' "
499 "not supported\nfor filesystems with "
500 "the meta_bg feature enabled.\n"),
506 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
509 if ((mount_flags & EXT2_MF_MOUNTED) ||
510 (mount_flags & EXT2_MF_READONLY)) {
511 fputs(_("The multiple mount protection feature can't\n"
512 "be set if the filesystem is mounted or\n"
513 "read-only.\n"), stderr);
517 error = ext2fs_mmp_init(fs);
519 fputs(_("\nError while enabling multiple mount "
520 "protection feature."), stderr);
525 * We want to update group desc with the new free blocks count
527 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
529 printf(_("Multiple mount protection has been enabled "
530 "with update interval %ds.\n"),
531 sb->s_mmp_update_interval);
534 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
537 if (mount_flags & EXT2_MF_READONLY) {
538 fputs(_("The multiple mount protection feature cannot\n"
539 "be disabled if the filesystem is readonly.\n"),
544 error = ext2fs_read_bitmaps(fs);
546 fputs(_("Error while reading bitmaps\n"), stderr);
550 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
552 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
554 if (error == EXT2_ET_MMP_MAGIC_INVALID)
555 printf(_("Magic number in MMP block does not "
556 "match. expected: %x, actual: %x\n"),
557 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
559 com_err(program_name, error, "%s",
560 _("while reading MMP block."));
564 /* We need to force out the group descriptors as well */
565 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
566 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
569 sb->s_mmp_update_interval = 0;
572 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_EA_INODE))
573 sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_EA_INODE;
575 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
577 * If adding a journal flag, let the create journal
578 * code below handle setting the flag and creating the
579 * journal. We supply a default size if necessary.
583 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
586 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
587 if (!sb->s_def_hash_version)
588 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
589 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
590 uuid_generate((unsigned char *) sb->s_hash_seed);
593 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
594 if (ext2fs_check_desc(fs)) {
595 fputs(_("Clearing the flex_bg flag would "
596 "cause the the filesystem to be\n"
597 "inconsistent.\n"), stderr);
602 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
603 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
604 if ((mount_flags & EXT2_MF_MOUNTED) &&
605 !(mount_flags & EXT2_MF_READONLY)) {
606 fputs(_("The huge_file feature may only be "
607 "cleared when the filesystem is\n"
608 "unmounted or mounted "
609 "read-only.\n"), stderr);
614 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
615 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
616 for (i = 0; i < fs->group_desc_count; i++) {
617 gd = ext2fs_group_desc(fs, fs->group_desc, i);
618 gd->bg_itable_unused = 0;
619 gd->bg_flags = EXT2_BG_INODE_ZEROED;
620 ext2fs_group_desc_csum_set(fs, i);
622 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
625 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
626 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
627 for (i = 0; i < fs->group_desc_count; i++) {
628 gd = ext2fs_group_desc(fs, fs->group_desc, i);
629 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
631 * XXX what we really should do is zap
632 * uninitialized inode tables instead.
634 request_fsck_afterwards(fs);
637 gd->bg_itable_unused = 0;
641 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
644 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
645 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
647 * Set the Q_flag here and handle the quota options in the code
652 /* Enable both user quota and group quota by default */
653 usrquota = QOPT_ENABLE;
654 grpquota = QOPT_ENABLE;
656 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
659 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
660 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
662 * Set the Q_flag here and handle the quota options in the code
666 fputs(_("\nWarning: '^quota' option overrides '-Q'"
667 "arguments.\n"), stderr);
669 /* Disable both user quota and group quota by default */
670 usrquota = QOPT_DISABLE;
671 grpquota = QOPT_DISABLE;
674 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
675 (sb->s_feature_compat || sb->s_feature_ro_compat ||
676 sb->s_feature_incompat))
677 ext2fs_update_dynamic_rev(fs);
679 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
680 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
681 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
682 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
683 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
684 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
685 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
686 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
687 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
688 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
689 request_fsck_afterwards(fs);
691 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
692 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
693 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
694 ext2fs_mark_super_dirty(fs);
700 * Add a journal to the filesystem.
702 static int add_journal(ext2_filsys fs)
704 unsigned long journal_blocks;
709 if (fs->super->s_feature_compat &
710 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
711 fputs(_("The filesystem already has a journal.\n"), stderr);
714 if (journal_device) {
715 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
717 proceed_question(-1);
718 check_mount(journal_device, 0, _("journal"));
719 #ifdef CONFIG_TESTIO_DEBUG
720 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
721 io_ptr = test_io_manager;
722 test_io_backing_manager = unix_io_manager;
725 io_ptr = unix_io_manager;
726 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
727 EXT2_FLAG_JOURNAL_DEV_OK, 0,
728 fs->blocksize, io_ptr, &jfs);
730 com_err(program_name, retval,
731 _("\n\twhile trying to open journal on %s\n"),
735 printf(_("Creating journal on device %s: "),
739 retval = ext2fs_add_journal_device(fs, jfs);
740 ext2fs_close_free(&jfs);
742 com_err(program_name, retval,
743 _("while adding filesystem to journal on %s"),
747 fputs(_("done\n"), stdout);
748 } else if (journal_size) {
749 fputs(_("Creating journal inode: "), stdout);
751 journal_blocks = figure_journal_size(journal_size, fs);
753 if (journal_location_string)
755 parse_num_blocks2(journal_location_string,
756 fs->super->s_log_block_size);
757 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
761 fprintf(stderr, "\n");
762 com_err(program_name, retval, "%s",
763 _("\n\twhile trying to create journal file"));
766 fputs(_("done\n"), stdout);
768 * If the filesystem wasn't mounted, we need to force
769 * the block group descriptors out.
771 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
772 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
774 print_check_message(fs->super->s_max_mnt_count,
775 fs->super->s_checkinterval);
779 free(journal_device);
783 static void handle_quota_options(ext2_filsys fs)
788 if (!usrquota && !grpquota)
792 quota_init_context(&qctx, fs, -1);
794 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
795 quota_compute_usage(qctx);
797 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
798 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
800 quota_update_limits(qctx, qf_ino, USRQUOTA);
801 quota_write_inode(qctx, USRQUOTA);
802 } else if (usrquota == QOPT_DISABLE) {
803 quota_remove_inode(fs, USRQUOTA);
806 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
807 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
809 quota_update_limits(qctx, qf_ino, GRPQUOTA);
810 quota_write_inode(qctx, GRPQUOTA);
811 } else if (grpquota == QOPT_DISABLE) {
812 quota_remove_inode(fs, GRPQUOTA);
815 quota_release_context(&qctx);
817 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
818 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
819 ext2fs_mark_super_dirty(fs);
820 } else if (!fs->super->s_usr_quota_inum &&
821 !fs->super->s_grp_quota_inum) {
822 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
823 ext2fs_mark_super_dirty(fs);
830 static void parse_quota_opts(const char *opts)
832 char *buf, *token, *next, *p;
838 fputs(_("Couldn't allocate memory to parse quota "
839 "options!\n"), stderr);
843 for (token = buf; token && *token; token = next) {
844 p = strchr(token, ',');
851 if (strcmp(token, "usrquota") == 0) {
852 usrquota = QOPT_ENABLE;
853 } else if (strcmp(token, "^usrquota") == 0) {
854 usrquota = QOPT_DISABLE;
855 } else if (strcmp(token, "grpquota") == 0) {
856 grpquota = QOPT_ENABLE;
857 } else if (strcmp(token, "^grpquota") == 0) {
858 grpquota = QOPT_DISABLE;
860 fputs(_("\nBad quota options specified.\n\n"
861 "Following valid quota options are available "
862 "(pass by separating with comma):\n"
874 static void parse_e2label_options(int argc, char ** argv)
876 if ((argc < 2) || (argc > 3)) {
877 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
880 io_options = strchr(argv[1], '?');
883 device_name = blkid_get_devname(NULL, argv[1], NULL);
885 com_err("e2label", 0, _("Unable to resolve '%s'"),
889 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
891 open_flag |= EXT2_FLAG_RW;
898 static time_t parse_time(char *str)
902 if (strcmp(str, "now") == 0) {
905 memset(&ts, 0, sizeof(ts));
907 strptime(str, "%Y%m%d%H%M%S", &ts);
909 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
910 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
913 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
914 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
915 ts.tm_min > 59 || ts.tm_sec > 61)
918 if (ts.tm_mday == 0) {
919 com_err(program_name, 0,
920 _("Couldn't parse date/time specifier: %s"),
925 return (mktime(&ts));
928 static void parse_tune2fs_options(int argc, char **argv)
934 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
937 strcat(optstring, "Q:");
941 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
942 while ((c = getopt(argc, argv, optstring)) != EOF)
945 max_mount_count = strtol(optarg, &tmp, 0);
946 if (*tmp || max_mount_count > 16000) {
947 com_err(program_name, 0,
948 _("bad mounts count - %s"),
952 if (max_mount_count == 0)
953 max_mount_count = -1;
955 open_flag = EXT2_FLAG_RW;
958 mount_count = strtoul(optarg, &tmp, 0);
959 if (*tmp || mount_count > 16000) {
960 com_err(program_name, 0,
961 _("bad mounts count - %s"),
966 open_flag = EXT2_FLAG_RW;
969 if (strcmp(optarg, "continue") == 0)
970 errors = EXT2_ERRORS_CONTINUE;
971 else if (strcmp(optarg, "remount-ro") == 0)
972 errors = EXT2_ERRORS_RO;
973 else if (strcmp(optarg, "panic") == 0)
974 errors = EXT2_ERRORS_PANIC;
976 com_err(program_name, 0,
977 _("bad error behavior - %s"),
982 open_flag = EXT2_FLAG_RW;
985 extended_cmd = optarg;
986 open_flag |= EXT2_FLAG_RW;
988 case 'f': /* Force */
992 resgid = strtoul(optarg, &tmp, 0);
994 gr = getgrnam(optarg);
1003 com_err(program_name, 0,
1004 _("bad gid/group name - %s"),
1009 open_flag = EXT2_FLAG_RW;
1012 interval = strtoul(optarg, &tmp, 0);
1019 case 'D': /* days */
1025 case 'M': /* months! */
1026 interval *= 86400 * 30;
1030 case 'W': /* weeks */
1031 interval *= 86400 * 7;
1036 com_err(program_name, 0,
1037 _("bad interval - %s"), optarg);
1041 open_flag = EXT2_FLAG_RW;
1046 open_flag = EXT2_FLAG_RW;
1049 parse_journal_opts(optarg);
1050 open_flag = EXT2_FLAG_RW;
1058 open_flag |= EXT2_FLAG_RW |
1059 EXT2_FLAG_JOURNAL_DEV_OK;
1062 reserved_ratio = strtod(optarg, &tmp);
1063 if (*tmp || reserved_ratio > 50 ||
1064 reserved_ratio < 0) {
1065 com_err(program_name, 0,
1066 _("bad reserved block ratio - %s"),
1071 open_flag = EXT2_FLAG_RW;
1074 new_last_mounted = optarg;
1076 open_flag = EXT2_FLAG_RW;
1080 com_err(program_name, 0, "%s",
1081 _("-o may only be specified once"));
1084 mntopts_cmd = optarg;
1085 open_flag = EXT2_FLAG_RW;
1089 com_err(program_name, 0, "%s",
1090 _("-O may only be specified once"));
1093 features_cmd = optarg;
1094 open_flag = EXT2_FLAG_RW;
1099 parse_quota_opts(optarg);
1100 open_flag = EXT2_FLAG_RW;
1104 reserved_blocks = strtoul(optarg, &tmp, 0);
1106 com_err(program_name, 0,
1107 _("bad reserved blocks count - %s"),
1112 open_flag = EXT2_FLAG_RW;
1114 case 's': /* Deprecated */
1115 s_flag = atoi(optarg);
1116 open_flag = EXT2_FLAG_RW;
1120 last_check_time = parse_time(optarg);
1121 open_flag = EXT2_FLAG_RW;
1124 resuid = strtoul(optarg, &tmp, 0);
1126 pw = getpwnam(optarg);
1130 resuid = pw->pw_uid;
1135 com_err(program_name, 0,
1136 _("bad uid/user name - %s"),
1141 open_flag = EXT2_FLAG_RW;
1146 open_flag = EXT2_FLAG_RW |
1147 EXT2_FLAG_JOURNAL_DEV_OK;
1150 new_inode_size = strtoul(optarg, &tmp, 0);
1152 com_err(program_name, 0,
1153 _("bad inode size - %s"),
1157 if (!((new_inode_size &
1158 (new_inode_size - 1)) == 0)) {
1159 com_err(program_name, 0,
1160 _("Inode size must be a "
1161 "power of two- %s"),
1165 open_flag = EXT2_FLAG_RW;
1171 if (optind < argc - 1 || optind == argc)
1173 if (!open_flag && !l_flag)
1175 io_options = strchr(argv[optind], '?');
1178 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1180 com_err(program_name, 0, _("Unable to resolve '%s'"),
1186 #ifdef CONFIG_BUILD_FINDFS
1187 void do_findfs(int argc, char **argv)
1192 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1193 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1196 dev = blkid_get_devname(NULL, argv[1], NULL);
1198 com_err("findfs", 0, _("Unable to resolve '%s'"),
1207 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1209 char *buf, *token, *next, *p, *arg;
1214 buf = malloc(len+1);
1216 fprintf(stderr, "%s",
1217 _("Couldn't allocate memory to parse options!\n"));
1221 for (token = buf; token && *token; token = next) {
1222 p = strchr(token, ',');
1228 arg = strchr(token, '=');
1233 if (strcmp(token, "clear-mmp") == 0 ||
1234 strcmp(token, "clear_mmp") == 0) {
1236 } else if (strcmp(token, "mmp_update_interval") == 0) {
1242 intv = strtoul(arg, &p, 0);
1245 _("Invalid mmp_update_interval: %s\n"),
1251 intv = EXT4_MMP_UPDATE_INTERVAL;
1252 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1254 _("mmp_update_interval too big: %lu\n"),
1259 printf(P_("Setting multiple mount protection update "
1260 "interval to %lu second\n",
1261 "Setting multiple mount protection update "
1262 "interval to %lu seconds\n", intv),
1264 fs->super->s_mmp_update_interval = intv;
1265 ext2fs_mark_super_dirty(fs);
1266 } else if (!strcmp(token, "test_fs")) {
1267 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1268 printf("Setting test filesystem flag\n");
1269 ext2fs_mark_super_dirty(fs);
1270 } else if (!strcmp(token, "^test_fs")) {
1271 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1272 printf("Clearing test filesystem flag\n");
1273 ext2fs_mark_super_dirty(fs);
1274 } else if (strcmp(token, "stride") == 0) {
1279 stride = strtoul(arg, &p, 0);
1282 _("Invalid RAID stride: %s\n"),
1288 } else if (strcmp(token, "stripe-width") == 0 ||
1289 strcmp(token, "stripe_width") == 0) {
1294 stripe_width = strtoul(arg, &p, 0);
1297 _("Invalid RAID stripe-width: %s\n"),
1302 stripe_width_set = 1;
1303 } else if (strcmp(token, "hash_alg") == 0 ||
1304 strcmp(token, "hash-alg") == 0) {
1309 hash_alg = e2p_string2hash(arg);
1312 _("Invalid hash algorithm: %s\n"),
1317 fs->super->s_def_hash_version = hash_alg;
1318 printf(_("Setting default hash algorithm "
1321 ext2fs_mark_super_dirty(fs);
1322 } else if (!strcmp(token, "mount_opts")) {
1327 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1329 "Extended mount options too long\n");
1332 ext_mount_opts = strdup(arg);
1337 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1338 "Extended options are separated by commas, "
1339 "and may take an argument which\n"
1340 "\tis set off by an equals ('=') sign.\n\n"
1341 "Valid extended options are:\n"
1343 "\thash_alg=<hash algorithm>\n"
1344 "\tmount_opts=<extended default mount options>\n"
1345 "\tstride=<RAID per-disk chunk size in blocks>\n"
1346 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1358 * Fill in the block bitmap bmap with the information regarding the
1359 * blocks to be moved
1361 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1362 ext2fs_block_bitmap bmap)
1366 ext2_badblocks_list bb_list = 0;
1367 blk64_t j, needed_blocks = 0;
1368 blk64_t start_blk, end_blk;
1370 retval = ext2fs_read_bb_inode(fs, &bb_list);
1374 for (i = 0; i < fs->group_desc_count; i++) {
1375 start_blk = ext2fs_inode_table_loc(fs, i) +
1376 fs->inode_blocks_per_group;
1378 end_blk = ext2fs_inode_table_loc(fs, i) +
1379 new_ino_blks_per_grp;
1381 for (j = start_blk; j < end_blk; j++) {
1382 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1384 * IF the block is a bad block we fail
1386 if (ext2fs_badblocks_list_test(bb_list, j)) {
1387 ext2fs_badblocks_list_free(bb_list);
1391 ext2fs_mark_block_bitmap2(bmap, j);
1394 * We are going to use this block for
1395 * inode table. So mark them used.
1397 ext2fs_mark_block_bitmap2(fs->block_map, j);
1400 needed_blocks += end_blk - start_blk;
1403 ext2fs_badblocks_list_free(bb_list);
1404 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1410 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
1413 group = ext2fs_group_of_blk2(fs, blk);
1414 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1416 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1421 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
1423 blk64_t start_blk, end_blk;
1424 start_blk = fs->super->s_first_data_block +
1425 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
1427 * We cannot get new block beyond end_blk for for the last block group
1428 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1430 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1431 if (blk >= start_blk && blk <= end_blk)
1436 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1443 blk64_t blk, new_blk, goal;
1444 struct blk_move *bmv;
1446 retval = ext2fs_get_mem(fs->blocksize, &buf);
1450 for (new_blk = blk = fs->super->s_first_data_block;
1451 blk < ext2fs_blocks_count(fs->super); blk++) {
1452 if (!ext2fs_test_block_bitmap2(bmap, blk))
1455 if (ext2fs_is_meta_block(fs, blk)) {
1457 * If the block is mapping a fs meta data block
1458 * like group desc/block bitmap/inode bitmap. We
1459 * should find a block in the same group and fix
1460 * the respective fs metadata pointers. Otherwise
1463 group = ext2fs_group_of_blk2(fs, blk);
1464 goal = ext2fs_group_first_block2(fs, group);
1470 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1474 /* new fs meta data block should be in the same group */
1475 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1480 /* Mark this block as allocated */
1481 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1483 /* Add it to block move list */
1484 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1489 bmv->new_loc = new_blk;
1491 list_add(&(bmv->list), &blk_move_list);
1493 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1497 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1503 ext2fs_free_mem(&buf);
1507 static blk64_t translate_block(blk64_t blk)
1509 struct list_head *entry;
1510 struct blk_move *bmv;
1512 list_for_each(entry, &blk_move_list) {
1513 bmv = list_entry(entry, struct blk_move, list);
1514 if (bmv->old_loc == blk)
1515 return bmv->new_loc;
1521 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1523 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1524 blk64_t ref_block EXT2FS_ATTR((unused)),
1525 int ref_offset EXT2FS_ATTR((unused)),
1530 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1532 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1534 new_blk = translate_block(*block_nr);
1536 *block_nr = new_blk;
1538 * This will force the ext2fs_write_inode in the iterator
1540 ret |= BLOCK_CHANGED;
1546 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1548 errcode_t retval = 0;
1551 char *block_buf = 0;
1552 struct ext2_inode *inode;
1554 ext2_inode_scan scan = NULL;
1556 inode_size = EXT2_INODE_SIZE(fs->super);
1557 retval = ext2fs_get_mem(inode_size, &inode);
1561 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1565 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1570 retval = ext2fs_get_next_inode_full(scan, &ino,
1578 if (inode->i_links_count == 0)
1579 continue; /* inode not in use */
1582 * If we end up modifying the journal inode
1583 * the sb->s_jnl_blocks will differ. But a
1584 * subsequent e2fsck fixes that.
1585 * Do we need to fix this ??
1588 if (ext2fs_file_acl_block(fs, inode) &&
1589 ext2fs_test_block_bitmap2(bmap,
1590 ext2fs_file_acl_block(fs, inode))) {
1591 blk = translate_block(ext2fs_file_acl_block(fs,
1596 ext2fs_file_acl_block_set(fs, inode, blk);
1599 * Write the inode to disk so that inode table
1602 retval = ext2fs_write_inode(fs, ino, inode);
1607 if (!ext2fs_inode_has_valid_blocks2(fs, inode))
1610 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1611 process_block, bmap);
1618 ext2fs_free_mem(&inode);
1619 ext2fs_free_mem(&block_buf);
1620 ext2fs_close_inode_scan(scan);
1626 * We need to scan for inode and block bitmaps that may need to be
1627 * moved. This can take place if the filesystem was formatted for
1628 * RAID arrays using the mke2fs's extended option "stride".
1630 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1633 blk64_t blk, new_blk;
1635 for (i = 0; i < fs->group_desc_count; i++) {
1636 blk = ext2fs_block_bitmap_loc(fs, i);
1637 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1638 new_blk = translate_block(blk);
1641 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1644 blk = ext2fs_inode_bitmap_loc(fs, i);
1645 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1646 new_blk = translate_block(blk);
1649 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1655 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1660 int new_ino_blks_per_grp;
1662 char *old_itable = NULL, *new_itable = NULL;
1663 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1664 unsigned long old_ino_size;
1665 int old_itable_size, new_itable_size;
1667 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1668 old_ino_size = EXT2_INODE_SIZE(fs->super);
1670 new_ino_blks_per_grp = ext2fs_div_ceil(
1671 EXT2_INODES_PER_GROUP(fs->super) *
1675 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1677 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1681 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1685 tmp_old_itable = old_itable;
1686 tmp_new_itable = new_itable;
1688 for (i = 0; i < fs->group_desc_count; i++) {
1689 blk = ext2fs_inode_table_loc(fs, i);
1690 retval = io_channel_read_blk64(fs->io, blk,
1691 fs->inode_blocks_per_group, old_itable);
1695 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1696 memcpy(new_itable, old_itable, old_ino_size);
1698 memset(new_itable+old_ino_size, 0,
1699 new_ino_size - old_ino_size);
1701 new_itable += new_ino_size;
1702 old_itable += old_ino_size;
1705 /* reset the pointer */
1706 old_itable = tmp_old_itable;
1707 new_itable = tmp_new_itable;
1709 retval = io_channel_write_blk64(fs->io, blk,
1710 new_ino_blks_per_grp, new_itable);
1715 /* Update the meta data */
1716 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1717 fs->super->s_inode_size = new_ino_size;
1721 ext2fs_free_mem(&old_itable);
1724 ext2fs_free_mem(&new_itable);
1729 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1733 unsigned int group = 0;
1734 unsigned int count = 0;
1739 * First calculate the block statistics
1741 for (blk = fs->super->s_first_data_block;
1742 blk < ext2fs_blocks_count(fs->super); blk++) {
1743 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1748 if ((count == fs->super->s_blocks_per_group) ||
1749 (blk == ext2fs_blocks_count(fs->super)-1)) {
1750 ext2fs_bg_free_blocks_count_set(fs, group++,
1756 total_free = EXT2FS_C2B(fs, total_free);
1757 ext2fs_free_blocks_count_set(fs->super, total_free);
1760 * Next, calculate the inode statistics
1767 /* Protect loop from wrap-around if s_inodes_count maxed */
1768 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1769 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1774 if ((count == fs->super->s_inodes_per_group) ||
1775 (ino == fs->super->s_inodes_count)) {
1776 ext2fs_bg_free_inodes_count_set(fs, group++,
1782 fs->super->s_free_inodes_count = total_free;
1783 ext2fs_mark_super_dirty(fs);
1787 #define list_for_each_safe(pos, pnext, head) \
1788 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1789 pos = pnext, pnext = pos->next)
1791 static void free_blk_move_list(void)
1793 struct list_head *entry, *tmp;
1794 struct blk_move *bmv;
1796 list_for_each_safe(entry, tmp, &blk_move_list) {
1797 bmv = list_entry(entry, struct blk_move, list);
1799 ext2fs_free_mem(&bmv);
1804 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1807 int new_ino_blks_per_grp;
1808 ext2fs_block_bitmap bmap;
1810 retval = ext2fs_read_inode_bitmap(fs);
1812 fputs(_("Failed to read inode bitmap\n"), stderr);
1815 retval = ext2fs_read_block_bitmap(fs);
1817 fputs(_("Failed to read block bitmap\n"), stderr);
1820 INIT_LIST_HEAD(&blk_move_list);
1823 new_ino_blks_per_grp = ext2fs_div_ceil(
1824 EXT2_INODES_PER_GROUP(fs->super)*
1828 /* We may change the file system.
1829 * Mark the file system as invalid so that
1830 * the user is prompted to run fsck.
1832 fs->super->s_state &= ~EXT2_VALID_FS;
1834 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1837 fputs(_("Failed to allocate block bitmap when "
1838 "increasing inode size\n"), stderr);
1841 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1843 fputs(_("Not enough space to increase inode size \n"), stderr);
1846 retval = move_block(fs, bmap);
1848 fputs(_("Failed to relocate blocks during inode resize \n"),
1852 retval = inode_scan_and_fix(fs, bmap);
1856 retval = group_desc_scan_and_fix(fs, bmap);
1860 retval = expand_inode_table(fs, new_size);
1864 ext2fs_calculate_summary_stats(fs);
1866 fs->super->s_state |= EXT2_VALID_FS;
1867 /* mark super block and block bitmap as dirty */
1868 ext2fs_mark_super_dirty(fs);
1869 ext2fs_mark_bb_dirty(fs);
1872 free_blk_move_list();
1873 ext2fs_free_block_bitmap(bmap);
1878 free_blk_move_list();
1879 ext2fs_free_block_bitmap(bmap);
1880 fputs(_("Error in resizing the inode size.\n"
1881 "Run e2undo to undo the "
1882 "file system changes. \n"), stderr);
1887 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1889 errcode_t retval = 0;
1890 const char *tdb_dir;
1892 char *dev_name, *tmp_name;
1896 * Configuration via a conf file would be
1899 profile_get_string(profile, "scratch_files",
1903 tmp_name = strdup(name);
1906 com_err(program_name, ENOMEM, "%s",
1907 _("Couldn't allocate memory for tdb filename\n"));
1910 dev_name = basename(tmp_name);
1912 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1914 tdb_dir = "/var/lib/e2fsprogs";
1916 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1917 access(tdb_dir, W_OK))
1920 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1923 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1925 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
1927 com_err(program_name, retval,
1928 _("while trying to delete %s"), tdb_file);
1933 set_undo_io_backing_manager(*io_ptr);
1934 *io_ptr = undo_io_manager;
1935 set_undo_io_backup_file(tdb_file);
1936 printf(_("To undo the tune2fs operation please run "
1937 "the command\n e2undo %s %s\n\n"),
1945 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
1947 int retval, nr_users, start;
1948 journal_superblock_t *jsb;
1952 char uuid[UUID_STR_SIZE];
1953 char buf[SUPERBLOCK_SIZE];
1955 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1956 uuid_is_null(sb->s_journal_uuid))
1959 uuid_unparse(sb->s_journal_uuid, uuid);
1960 journal_path = blkid_get_devname(NULL, "UUID", uuid);
1964 retval = ext2fs_open2(journal_path, io_options,
1965 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
1966 0, 0, unix_io_manager, &jfs);
1968 com_err(program_name, retval,
1969 _("while trying to open %s"),
1974 retval = get_journal_sb(jfs, buf);
1976 if (retval == EXT2_ET_UNSUPP_FEATURE)
1977 fprintf(stderr, _("%s is not a journal device.\n"),
1982 jsb = (journal_superblock_t *) buf;
1983 /* Find the filesystem UUID */
1984 nr_users = ntohl(jsb->s_nr_users);
1986 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
1987 if (j_uuid == NULL) {
1988 fputs(_("Filesystem's UUID not found on journal device.\n"),
1990 return EXT2_ET_LOAD_EXT_JOURNAL;
1993 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
1995 start = ext2fs_journal_sb_start(jfs->blocksize);
1996 /* Write back the journal superblock */
1997 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
1999 com_err(program_name, retval,
2000 "while writing journal superblock.");
2009 int main(int argc, char **argv)
2013 struct ext2_super_block *sb;
2014 io_manager io_ptr, io_ptr_orig = NULL;
2018 setlocale(LC_MESSAGES, "");
2019 setlocale(LC_CTYPE, "");
2020 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2021 textdomain(NLS_CAT_NAME);
2022 set_com_err_gettext(gettext);
2025 program_name = *argv;
2026 add_error_table(&et_ext2_error_table);
2028 #ifdef CONFIG_BUILD_FINDFS
2029 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2030 do_findfs(argc, argv);
2032 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2033 parse_e2label_options(argc, argv);
2035 parse_tune2fs_options(argc, argv);
2037 #ifdef CONFIG_TESTIO_DEBUG
2038 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2039 io_ptr = test_io_manager;
2040 test_io_backing_manager = unix_io_manager;
2043 io_ptr = unix_io_manager;
2046 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2047 open_flag |= EXT2_FLAG_SKIP_MMP;
2049 open_flag |= EXT2_FLAG_64BITS;
2051 /* keep the filesystem struct around to dump MMP data */
2052 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2054 retval = ext2fs_open2(device_name, io_options, open_flag,
2057 com_err(program_name, retval,
2058 _("while trying to open %s"),
2060 if (retval == EXT2_ET_MMP_FSCK_ON ||
2061 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2062 dump_mmp_msg(fs->mmp_buf,
2063 _("If you are sure the filesystem "
2064 "is not in use on any node, run:\n"
2065 "'tune2fs -f -E clear_mmp {device}'\n"));
2066 else if (retval == EXT2_ET_MMP_FAILED)
2067 dump_mmp_msg(fs->mmp_buf, NULL);
2068 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2070 _("MMP block magic is bad. Try to fix it by "
2071 "running:\n'e2fsck -f %s'\n"), device_name);
2072 else if (retval != EXT2_ET_MMP_FAILED)
2073 fprintf(stderr, "%s",
2074 _("Couldn't find valid filesystem superblock.\n"));
2079 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2081 if (I_flag && !io_ptr_orig) {
2083 * Check the inode size is right so we can issue an
2084 * error message and bail before setting up the tdb
2087 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2088 fprintf(stderr, _("The inode size is already %lu\n"),
2093 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2094 fprintf(stderr, "%s",
2095 _("Shrinking inode size is not supported\n"));
2099 if (new_inode_size > fs->blocksize) {
2100 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2101 new_inode_size, fs->blocksize);
2107 * If inode resize is requested use the
2110 io_ptr_orig = io_ptr;
2111 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2116 if (io_ptr != io_ptr_orig) {
2117 ext2fs_close_free(&fs);
2123 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2126 /* For e2label emulation */
2127 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2129 remove_error_table(&et_ext2_error_table);
2133 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2135 com_err("ext2fs_check_if_mount", retval,
2136 _("while determining whether %s is mounted."),
2141 /* Normally we only need to write out the superblock */
2142 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2145 sb->s_max_mnt_count = max_mount_count;
2146 ext2fs_mark_super_dirty(fs);
2147 printf(_("Setting maximal mount count to %d\n"),
2151 sb->s_mnt_count = mount_count;
2152 ext2fs_mark_super_dirty(fs);
2153 printf(_("Setting current mount count to %d\n"), mount_count);
2156 sb->s_errors = errors;
2157 ext2fs_mark_super_dirty(fs);
2158 printf(_("Setting error behavior to %d\n"), errors);
2161 sb->s_def_resgid = resgid;
2162 ext2fs_mark_super_dirty(fs);
2163 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2166 if ((unsigned long long)interval >= (1ULL << 32)) {
2167 com_err(program_name, 0,
2168 _("interval between checks is too big (%lu)"),
2173 sb->s_checkinterval = interval;
2174 ext2fs_mark_super_dirty(fs);
2175 printf(_("Setting interval between checks to %lu seconds\n"),
2179 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2180 ext2fs_blocks_count(sb) / 100.0);
2181 ext2fs_mark_super_dirty(fs);
2182 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2183 reserved_ratio, ext2fs_r_blocks_count(sb));
2186 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2187 com_err(program_name, 0,
2188 _("reserved blocks count is too big (%llu)"),
2193 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2194 ext2fs_mark_super_dirty(fs);
2195 printf(_("Setting reserved blocks count to %llu\n"),
2199 if (sb->s_feature_ro_compat &
2200 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
2201 fputs(_("\nThe filesystem already has sparse "
2202 "superblocks.\n"), stderr);
2203 } else if (sb->s_feature_incompat &
2204 EXT2_FEATURE_INCOMPAT_META_BG) {
2205 fputs(_("\nSetting the sparse superblock flag not "
2206 "supported\nfor filesystems with "
2207 "the meta_bg feature enabled.\n"),
2212 sb->s_feature_ro_compat |=
2213 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2214 sb->s_state &= ~EXT2_VALID_FS;
2215 ext2fs_mark_super_dirty(fs);
2216 printf(_("\nSparse superblock flag set. %s"),
2221 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2227 sb->s_lastcheck = last_check_time;
2228 ext2fs_mark_super_dirty(fs);
2229 printf(_("Setting time filesystem last checked to %s\n"),
2230 ctime(&last_check_time));
2233 sb->s_def_resuid = resuid;
2234 ext2fs_mark_super_dirty(fs);
2235 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2238 if (strlen(new_label) > sizeof(sb->s_volume_name))
2239 fputs(_("Warning: label too long, truncating.\n"),
2241 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2242 strncpy(sb->s_volume_name, new_label,
2243 sizeof(sb->s_volume_name));
2244 ext2fs_mark_super_dirty(fs);
2247 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2248 strncpy(sb->s_last_mounted, new_last_mounted,
2249 sizeof(sb->s_last_mounted));
2250 ext2fs_mark_super_dirty(fs);
2253 rc = update_mntopts(fs, mntopts_cmd);
2258 rc = update_feature_set(fs, features_cmd);
2263 rc = parse_extended_opts(fs, extended_cmd);
2266 if (clear_mmp && !f_flag) {
2267 fputs(_("Error in using clear_mmp. "
2268 "It must be used with -f\n"),
2274 rc = ext2fs_mmp_clear(fs);
2277 if (journal_size || journal_device) {
2278 rc = add_journal(fs);
2284 if (mount_flags & EXT2_MF_MOUNTED) {
2285 fputs(_("The quota feature may only be changed when "
2286 "the filesystem is unmounted.\n"), stderr);
2290 handle_quota_options(fs);
2296 char buf[SUPERBLOCK_SIZE];
2297 __u8 old_uuid[UUID_SIZE];
2299 if (sb->s_feature_ro_compat &
2300 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2302 * Changing the UUID requires rewriting all metadata,
2303 * which can race with a mounted fs. Don't allow that.
2305 if (mount_flags & EXT2_MF_MOUNTED) {
2306 fputs(_("The UUID may only be "
2307 "changed when the filesystem is "
2308 "unmounted.\n"), stderr);
2311 if (check_fsck_needed(fs))
2315 * Determine if the block group checksums are
2316 * correct so we know whether or not to set
2319 for (i = 0; i < fs->group_desc_count; i++)
2320 if (!ext2fs_group_desc_csum_verify(fs, i))
2322 if (i >= fs->group_desc_count)
2326 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2327 if ((strcasecmp(new_UUID, "null") == 0) ||
2328 (strcasecmp(new_UUID, "clear") == 0)) {
2329 uuid_clear(sb->s_uuid);
2330 } else if (strcasecmp(new_UUID, "time") == 0) {
2331 uuid_generate_time(sb->s_uuid);
2332 } else if (strcasecmp(new_UUID, "random") == 0) {
2333 uuid_generate(sb->s_uuid);
2334 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2335 com_err(program_name, 0, "%s",
2336 _("Invalid UUID format\n"));
2341 for (i = 0; i < fs->group_desc_count; i++)
2342 ext2fs_group_desc_csum_set(fs, i);
2343 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2346 /* If this is a journal dev, we need to copy UUID into jsb */
2347 if (!(rc = get_journal_sb(fs, buf))) {
2348 journal_superblock_t *jsb;
2350 jsb = (journal_superblock_t *) buf;
2351 fputs(_("Need to update journal superblock.\n"), stdout);
2352 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2354 /* Writeback the journal superblock */
2355 if ((rc = io_channel_write_blk64(fs->io,
2356 ext2fs_journal_sb_start(fs->blocksize),
2357 -SUPERBLOCK_SIZE, buf)))
2359 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
2362 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
2364 if ((rc = fs_update_journal_user(sb, old_uuid)))
2368 ext2fs_mark_super_dirty(fs);
2371 if (mount_flags & EXT2_MF_MOUNTED) {
2372 fputs(_("The inode size may only be "
2373 "changed when the filesystem is "
2374 "unmounted.\n"), stderr);
2378 if (fs->super->s_feature_incompat &
2379 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2380 fputs(_("Changing the inode size not supported for "
2381 "filesystems with the flex_bg\n"
2382 "feature enabled.\n"),
2388 * We want to update group descriptor also
2389 * with the new free inode count
2391 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2392 if (resize_inode(fs, new_inode_size) == 0) {
2393 printf(_("Setting inode size %lu\n"),
2396 printf("%s", _("Failed to change inode size\n"));
2405 sb->s_raid_stride = stride;
2406 ext2fs_mark_super_dirty(fs);
2407 printf(_("Setting stride size to %d\n"), stride);
2409 if (stripe_width_set) {
2410 sb->s_raid_stripe_width = stripe_width;
2411 ext2fs_mark_super_dirty(fs);
2412 printf(_("Setting stripe width to %d\n"), stripe_width);
2415 if (ext_mount_opts) {
2416 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2417 sizeof(fs->super->s_mount_opts));
2418 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2419 ext2fs_mark_super_dirty(fs);
2420 printf(_("Setting extended default mount options to '%s'\n"),
2422 free(ext_mount_opts);
2425 /* Warn if file system needs recovery and it is opened for writing. */
2426 if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) &&
2427 (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
2428 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER)) {
2430 _("Warning: needs_recovery flag is set. You may wish\n"
2431 "replay the journal then rerun this command, or any\n"
2432 "changes may be overwritten by journal recovery.\n"));
2436 remove_error_table(&et_ext2_error_table);
2440 ext2fs_mmp_stop(fs);
2444 return (ext2fs_close_free(&fs) ? 1 : 0);