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() */
29 #define _BSD_SOURCE /* for inclusion of strcasecmp() */
47 #include <sys/types.h>
51 #include "ext2fs/ext2_fs.h"
52 #include "ext2fs/ext2fs.h"
53 #include "et/com_err.h"
54 #include "uuid/uuid.h"
58 #include "blkid/blkid.h"
59 #include "quota/mkquota.h"
61 #include "../version.h"
62 #include "nls-enable.h"
64 #define QOPT_ENABLE (1)
65 #define QOPT_DISABLE (-1)
67 extern int ask_yn(const char *string, int def);
69 const char *program_name = "tune2fs";
71 char *new_label, *new_last_mounted, *new_UUID;
73 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
74 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
77 static time_t last_check_time;
78 static int print_label;
79 static int max_mount_count, mount_count, mount_flags;
80 static unsigned long interval;
81 static blk64_t reserved_blocks;
82 static double reserved_ratio;
83 static unsigned long resgid, resuid;
84 static unsigned short errors;
86 static char *features_cmd;
87 static char *mntopts_cmd;
88 static int stride, stripe_width;
89 static int stride_set, stripe_width_set;
90 static char *extended_cmd;
91 static unsigned long new_inode_size;
92 static char *ext_mount_opts;
93 static int usrquota, grpquota;
95 int journal_size, journal_flags;
98 static struct list_head blk_move_list;
101 struct list_head list;
107 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
109 #ifdef CONFIG_BUILD_FINDFS
110 void do_findfs(int argc, char **argv);
113 static void usage(void)
116 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
118 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
119 "\t[-m reserved_blocks_percent] "
120 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
121 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
122 "[-L volume_label]\n"
123 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
124 "\t[-E extended-option[,...]] [-T last_check_time] "
125 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
129 static __u32 ok_features[3] = {
131 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
132 EXT2_FEATURE_COMPAT_DIR_INDEX,
134 EXT2_FEATURE_INCOMPAT_FILETYPE |
135 EXT3_FEATURE_INCOMPAT_EXTENTS |
136 EXT4_FEATURE_INCOMPAT_FLEX_BG |
137 EXT4_FEATURE_INCOMPAT_MMP,
139 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
140 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
141 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
142 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
143 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
144 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
145 EXT4_FEATURE_RO_COMPAT_QUOTA
148 static __u32 clear_ok_features[3] = {
150 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
151 EXT2_FEATURE_COMPAT_RESIZE_INODE |
152 EXT2_FEATURE_COMPAT_DIR_INDEX,
154 EXT2_FEATURE_INCOMPAT_FILETYPE |
155 EXT4_FEATURE_INCOMPAT_FLEX_BG |
156 EXT4_FEATURE_INCOMPAT_MMP,
158 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
159 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
160 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
161 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
162 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
163 EXT4_FEATURE_RO_COMPAT_QUOTA
167 * Remove an external journal from the filesystem
169 static int remove_journal_device(ext2_filsys fs)
174 journal_superblock_t *jsb;
177 int commit_remove_journal = 0;
181 commit_remove_journal = 1; /* force removal even if error */
183 uuid_unparse(fs->super->s_journal_uuid, buf);
184 journal_path = blkid_get_devname(NULL, "UUID", buf);
188 ext2fs_find_block_device(fs->super->s_journal_dev);
190 goto no_valid_journal;
193 #ifdef CONFIG_TESTIO_DEBUG
194 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
195 io_ptr = test_io_manager;
196 test_io_backing_manager = unix_io_manager;
199 io_ptr = unix_io_manager;
200 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
201 EXT2_FLAG_JOURNAL_DEV_OK, 0,
202 fs->blocksize, io_ptr, &jfs);
204 com_err(program_name, retval,
205 _("while trying to open external journal"));
206 goto no_valid_journal;
208 if (!(jfs->super->s_feature_incompat &
209 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
210 fprintf(stderr, _("%s is not a journal device.\n"),
212 goto no_valid_journal;
215 /* Get the journal superblock */
216 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
217 com_err(program_name, retval,
218 _("while reading journal superblock"));
219 goto no_valid_journal;
222 jsb = (journal_superblock_t *) buf;
223 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
224 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
225 fputs(_("Journal superblock not found!\n"), stderr);
226 goto no_valid_journal;
229 /* Find the filesystem UUID */
230 nr_users = ntohl(jsb->s_nr_users);
231 for (i = 0; i < nr_users; i++) {
232 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
236 fputs(_("Filesystem's UUID not found on journal device.\n"),
238 commit_remove_journal = 1;
239 goto no_valid_journal;
242 for (i = 0; i < nr_users; i++)
243 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
244 jsb->s_nr_users = htonl(nr_users);
246 /* Write back the journal superblock */
247 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
248 com_err(program_name, retval,
249 "while writing journal superblock.");
250 goto no_valid_journal;
253 commit_remove_journal = 1;
256 if (commit_remove_journal == 0) {
257 fputs(_("Cannot locate journal device. It was NOT removed\n"
258 "Use -f option to remove missing journal device.\n"),
262 fs->super->s_journal_dev = 0;
263 uuid_clear(fs->super->s_journal_uuid);
264 ext2fs_mark_super_dirty(fs);
265 fputs(_("Journal removed\n"), stdout);
271 /* Helper function for remove_journal_inode */
272 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
273 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
274 blk64_t ref_block EXT2FS_ATTR((unused)),
275 int ref_offset EXT2FS_ATTR((unused)),
276 void *private EXT2FS_ATTR((unused)))
282 ext2fs_unmark_block_bitmap2(fs->block_map, block);
283 group = ext2fs_group_of_blk2(fs, block);
284 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
285 ext2fs_group_desc_csum_set(fs, group);
286 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
291 * Remove the journal inode from the filesystem
293 static errcode_t remove_journal_inode(ext2_filsys fs)
295 struct ext2_inode inode;
297 ino_t ino = fs->super->s_journal_inum;
299 retval = ext2fs_read_inode(fs, ino, &inode);
301 com_err(program_name, retval,
302 _("while reading journal inode"));
305 if (ino == EXT2_JOURNAL_INO) {
306 retval = ext2fs_read_bitmaps(fs);
308 com_err(program_name, retval,
309 _("while reading bitmaps"));
312 retval = ext2fs_block_iterate3(fs, ino,
313 BLOCK_FLAG_READ_ONLY, NULL,
314 release_blocks_proc, NULL);
316 com_err(program_name, retval,
317 _("while clearing journal inode"));
320 memset(&inode, 0, sizeof(inode));
321 ext2fs_mark_bb_dirty(fs);
322 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
324 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
325 retval = ext2fs_write_inode(fs, ino, &inode);
327 com_err(program_name, retval,
328 _("while writing journal inode"));
331 fs->super->s_journal_inum = 0;
332 ext2fs_mark_super_dirty(fs);
338 * Update the default mount options
340 static int update_mntopts(ext2_filsys fs, char *mntopts)
342 struct ext2_super_block *sb = fs->super;
344 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
345 fprintf(stderr, _("Invalid mount option set: %s\n"),
349 ext2fs_mark_super_dirty(fs);
354 static void request_fsck_afterwards(ext2_filsys fs)
356 static int requested = 0;
360 fs->super->s_state &= ~EXT2_VALID_FS;
361 printf("\n%s\n", _(please_fsck));
362 if (mount_flags & EXT2_MF_READONLY)
363 printf(_("(and reboot afterwards!)\n"));
367 * Update the feature set as provided by the user.
369 static int update_feature_set(ext2_filsys fs, char *features)
371 struct ext2_super_block *sb = fs->super;
372 struct ext2_group_desc *gd;
373 __u32 old_features[3];
375 unsigned int mask_err;
377 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
378 ((&sb->s_feature_compat)[(type)] & (mask)))
379 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
380 !((&sb->s_feature_compat)[(type)] & (mask)))
381 #define FEATURE_CHANGED(type, mask) ((mask) & \
382 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
384 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
385 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
386 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
388 if (e2p_edit_feature2(features, &sb->s_feature_compat,
389 ok_features, clear_ok_features,
390 &type_err, &mask_err)) {
393 _("Invalid filesystem option set: %s\n"),
395 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
396 fprintf(stderr, _("Clearing filesystem feature '%s' "
398 e2p_feature2string(type_err &
399 E2P_FEATURE_TYPE_MASK,
402 fprintf(stderr, _("Setting filesystem feature '%s' "
404 e2p_feature2string(type_err, mask_err));
408 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
409 if ((mount_flags & EXT2_MF_MOUNTED) &&
410 !(mount_flags & EXT2_MF_READONLY)) {
411 fputs(_("The has_journal feature may only be "
412 "cleared when the filesystem is\n"
413 "unmounted or mounted "
414 "read-only.\n"), stderr);
417 if (sb->s_feature_incompat &
418 EXT3_FEATURE_INCOMPAT_RECOVER) {
419 fputs(_("The needs_recovery flag is set. "
420 "Please run e2fsck before clearing\n"
421 "the has_journal flag.\n"), stderr);
424 if (sb->s_journal_inum) {
425 if (remove_journal_inode(fs))
428 if (sb->s_journal_dev) {
429 if (remove_journal_device(fs))
433 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
436 if ((mount_flags & EXT2_MF_MOUNTED) ||
437 (mount_flags & EXT2_MF_READONLY)) {
438 fputs(_("The multiple mount protection feature can't\n"
439 "be set if the filesystem is mounted or\n"
440 "read-only.\n"), stderr);
444 error = ext2fs_mmp_init(fs);
446 fputs(_("\nError while enabling multiple mount "
447 "protection feature."), stderr);
452 * We want to update group desc with the new free blocks count
454 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
456 printf(_("Multiple mount protection has been enabled "
457 "with update interval %ds.\n"),
458 sb->s_mmp_update_interval);
461 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
464 if (mount_flags & EXT2_MF_READONLY) {
465 fputs(_("The multiple mount protection feature cannot\n"
466 "be disabled if the filesystem is readonly.\n"),
471 error = ext2fs_read_bitmaps(fs);
473 fputs(_("Error while reading bitmaps\n"), stderr);
477 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
479 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
481 if (error == EXT2_ET_MMP_MAGIC_INVALID)
482 printf(_("Magic number in MMP block does not "
483 "match. expected: %x, actual: %x\n"),
484 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
486 com_err(program_name, error,
487 _("while reading MMP block."));
491 /* We need to force out the group descriptors as well */
492 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
493 ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
496 sb->s_mmp_update_interval = 0;
499 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
501 * If adding a journal flag, let the create journal
502 * code below handle setting the flag and creating the
503 * journal. We supply a default size if necessary.
507 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
510 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
511 if (!sb->s_def_hash_version)
512 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
513 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
514 uuid_generate((unsigned char *) sb->s_hash_seed);
517 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
518 if (ext2fs_check_desc(fs)) {
519 fputs(_("Clearing the flex_bg flag would "
520 "cause the the filesystem to be\n"
521 "inconsistent.\n"), stderr);
526 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
527 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
528 if ((mount_flags & EXT2_MF_MOUNTED) &&
529 !(mount_flags & EXT2_MF_READONLY)) {
530 fputs(_("The huge_file feature may only be "
531 "cleared when the filesystem is\n"
532 "unmounted or mounted "
533 "read-only.\n"), stderr);
538 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
539 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
540 for (i = 0; i < fs->group_desc_count; i++) {
541 gd = ext2fs_group_desc(fs, fs->group_desc, i);
542 gd->bg_itable_unused = 0;
543 gd->bg_flags = EXT2_BG_INODE_ZEROED;
544 ext2fs_group_desc_csum_set(fs, i);
546 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
549 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
550 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
551 for (i = 0; i < fs->group_desc_count; i++) {
552 gd = ext2fs_group_desc(fs, fs->group_desc, i);
553 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
555 * XXX what we really should do is zap
556 * uninitialized inode tables instead.
558 request_fsck_afterwards(fs);
561 gd->bg_itable_unused = 0;
565 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
568 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
569 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
571 * Set the Q_flag here and handle the quota options in the code
576 /* Enable both user quota and group quota by default */
577 usrquota = QOPT_ENABLE;
578 grpquota = QOPT_ENABLE;
580 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
583 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
584 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
586 * Set the Q_flag here and handle the quota options in the code
590 fputs(_("\nWarning: '^quota' option overrides '-Q'"
591 "arguments.\n"), stderr);
593 /* Disable both user quota and group quota by default */
594 usrquota = QOPT_DISABLE;
595 grpquota = QOPT_DISABLE;
598 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
599 (sb->s_feature_compat || sb->s_feature_ro_compat ||
600 sb->s_feature_incompat))
601 ext2fs_update_dynamic_rev(fs);
603 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
604 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
605 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
606 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
607 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
608 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
609 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
610 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
611 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
612 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
613 request_fsck_afterwards(fs);
615 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
616 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
617 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
618 ext2fs_mark_super_dirty(fs);
624 * Add a journal to the filesystem.
626 static int add_journal(ext2_filsys fs)
628 unsigned long journal_blocks;
633 if (fs->super->s_feature_compat &
634 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
635 fputs(_("The filesystem already has a journal.\n"), stderr);
638 if (journal_device) {
639 check_plausibility(journal_device);
640 check_mount(journal_device, 0, _("journal"));
641 #ifdef CONFIG_TESTIO_DEBUG
642 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
643 io_ptr = test_io_manager;
644 test_io_backing_manager = unix_io_manager;
647 io_ptr = unix_io_manager;
648 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
649 EXT2_FLAG_JOURNAL_DEV_OK, 0,
650 fs->blocksize, io_ptr, &jfs);
652 com_err(program_name, retval,
653 _("\n\twhile trying to open journal on %s\n"),
657 printf(_("Creating journal on device %s: "),
661 retval = ext2fs_add_journal_device(fs, jfs);
664 com_err(program_name, retval,
665 _("while adding filesystem to journal on %s"),
669 fputs(_("done\n"), stdout);
670 } else if (journal_size) {
671 fputs(_("Creating journal inode: "), stdout);
673 journal_blocks = figure_journal_size(journal_size, fs);
675 retval = ext2fs_add_journal_inode(fs, journal_blocks,
678 fprintf(stderr, "\n");
679 com_err(program_name, retval,
680 _("\n\twhile trying to create journal file"));
683 fputs(_("done\n"), stdout);
685 * If the filesystem wasn't mounted, we need to force
686 * the block group descriptors out.
688 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
689 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
691 print_check_message(fs->super->s_max_mnt_count,
692 fs->super->s_checkinterval);
696 free(journal_device);
700 void handle_quota_options(ext2_filsys fs)
705 if (!usrquota && !grpquota)
709 quota_init_context(&qctx, fs, -1);
711 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
712 quota_compute_usage(qctx);
714 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
715 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
717 if (quota_update_inode(qctx, qf_ino, USRQUOTA) == 0)
718 quota_set_sb_inum(fs, qf_ino, USRQUOTA);
720 quota_write_inode(qctx, USRQUOTA);
722 quota_write_inode(qctx, USRQUOTA);
724 } else if (usrquota == QOPT_DISABLE) {
725 quota_remove_inode(fs, USRQUOTA);
728 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
729 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
731 if (quota_update_inode(qctx, qf_ino, GRPQUOTA) == 0)
732 quota_set_sb_inum(fs, qf_ino, GRPQUOTA);
734 quota_write_inode(qctx, GRPQUOTA);
736 quota_write_inode(qctx, GRPQUOTA);
738 } else if (grpquota == QOPT_DISABLE) {
739 quota_remove_inode(fs, GRPQUOTA);
742 quota_release_context(&qctx);
744 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
745 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
746 ext2fs_mark_super_dirty(fs);
747 } else if ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
748 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
749 ext2fs_mark_super_dirty(fs);
755 void parse_quota_opts(const char *opts)
757 char *buf, *token, *next, *p;
763 fputs(_("Couldn't allocate memory to parse quota "
764 "options!\n"), stderr);
768 for (token = buf; token && *token; token = next) {
769 p = strchr(token, ',');
776 if (strcmp(token, "usrquota") == 0) {
777 usrquota = QOPT_ENABLE;
778 } else if (strcmp(token, "^usrquota") == 0) {
779 usrquota = QOPT_DISABLE;
780 } else if (strcmp(token, "grpquota") == 0) {
781 grpquota = QOPT_ENABLE;
782 } else if (strcmp(token, "^grpquota") == 0) {
783 grpquota = QOPT_DISABLE;
785 fputs(_("\nBad quota options specified.\n\n"
786 "Following valid quota options are available "
787 "(pass by separating with comma):\n"
800 static void parse_e2label_options(int argc, char ** argv)
802 if ((argc < 2) || (argc > 3)) {
803 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
806 io_options = strchr(argv[1], '?');
809 device_name = blkid_get_devname(NULL, argv[1], NULL);
811 com_err("e2label", 0, _("Unable to resolve '%s'"),
815 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
817 open_flag |= EXT2_FLAG_RW;
824 static time_t parse_time(char *str)
828 if (strcmp(str, "now") == 0) {
831 memset(&ts, 0, sizeof(ts));
833 strptime(str, "%Y%m%d%H%M%S", &ts);
835 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
836 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
839 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
840 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
841 ts.tm_min > 59 || ts.tm_sec > 61)
844 if (ts.tm_mday == 0) {
845 com_err(program_name, 0,
846 _("Couldn't parse date/time specifier: %s"),
851 return (mktime(&ts));
854 static void parse_tune2fs_options(int argc, char **argv)
863 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
864 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF)
867 max_mount_count = strtol(optarg, &tmp, 0);
868 if (*tmp || max_mount_count > 16000) {
869 com_err(program_name, 0,
870 _("bad mounts count - %s"),
874 if (max_mount_count == 0)
875 max_mount_count = -1;
877 open_flag = EXT2_FLAG_RW;
880 mount_count = strtoul(optarg, &tmp, 0);
881 if (*tmp || mount_count > 16000) {
882 com_err(program_name, 0,
883 _("bad mounts count - %s"),
888 open_flag = EXT2_FLAG_RW;
891 if (strcmp(optarg, "continue") == 0)
892 errors = EXT2_ERRORS_CONTINUE;
893 else if (strcmp(optarg, "remount-ro") == 0)
894 errors = EXT2_ERRORS_RO;
895 else if (strcmp(optarg, "panic") == 0)
896 errors = EXT2_ERRORS_PANIC;
898 com_err(program_name, 0,
899 _("bad error behavior - %s"),
904 open_flag = EXT2_FLAG_RW;
907 extended_cmd = optarg;
908 open_flag |= EXT2_FLAG_RW;
910 case 'f': /* Force */
914 resgid = strtoul(optarg, &tmp, 0);
916 gr = getgrnam(optarg);
925 com_err(program_name, 0,
926 _("bad gid/group name - %s"),
931 open_flag = EXT2_FLAG_RW;
934 interval = strtoul(optarg, &tmp, 0);
947 case 'M': /* months! */
948 interval *= 86400 * 30;
952 case 'W': /* weeks */
953 interval *= 86400 * 7;
958 com_err(program_name, 0,
959 _("bad interval - %s"), optarg);
963 open_flag = EXT2_FLAG_RW;
968 open_flag = EXT2_FLAG_RW;
971 parse_journal_opts(optarg);
972 open_flag = EXT2_FLAG_RW;
980 open_flag |= EXT2_FLAG_RW |
981 EXT2_FLAG_JOURNAL_DEV_OK;
984 reserved_ratio = strtod(optarg, &tmp);
985 if (*tmp || reserved_ratio > 50 ||
986 reserved_ratio < 0) {
987 com_err(program_name, 0,
988 _("bad reserved block ratio - %s"),
993 open_flag = EXT2_FLAG_RW;
996 new_last_mounted = optarg;
998 open_flag = EXT2_FLAG_RW;
1002 com_err(program_name, 0,
1003 _("-o may only be specified once"));
1006 mntopts_cmd = optarg;
1007 open_flag = EXT2_FLAG_RW;
1011 com_err(program_name, 0,
1012 _("-O may only be specified once"));
1015 features_cmd = optarg;
1016 open_flag = EXT2_FLAG_RW;
1020 parse_quota_opts(optarg);
1021 open_flag = EXT2_FLAG_RW;
1024 reserved_blocks = strtoul(optarg, &tmp, 0);
1026 com_err(program_name, 0,
1027 _("bad reserved blocks count - %s"),
1032 open_flag = EXT2_FLAG_RW;
1034 case 's': /* Deprecated */
1035 s_flag = atoi(optarg);
1036 open_flag = EXT2_FLAG_RW;
1040 last_check_time = parse_time(optarg);
1041 open_flag = EXT2_FLAG_RW;
1044 resuid = strtoul(optarg, &tmp, 0);
1046 pw = getpwnam(optarg);
1050 resuid = pw->pw_uid;
1055 com_err(program_name, 0,
1056 _("bad uid/user name - %s"),
1061 open_flag = EXT2_FLAG_RW;
1066 open_flag = EXT2_FLAG_RW |
1067 EXT2_FLAG_JOURNAL_DEV_OK;
1070 new_inode_size = strtoul(optarg, &tmp, 0);
1072 com_err(program_name, 0,
1073 _("bad inode size - %s"),
1077 if (!((new_inode_size &
1078 (new_inode_size - 1)) == 0)) {
1079 com_err(program_name, 0,
1080 _("Inode size must be a "
1081 "power of two- %s"),
1085 open_flag = EXT2_FLAG_RW;
1091 if (optind < argc - 1 || optind == argc)
1093 if (!open_flag && !l_flag)
1095 io_options = strchr(argv[optind], '?');
1098 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1100 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1106 #ifdef CONFIG_BUILD_FINDFS
1107 void do_findfs(int argc, char **argv)
1112 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1113 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1116 dev = blkid_get_devname(NULL, argv[1], NULL);
1118 com_err("findfs", 0, _("Unable to resolve '%s'"),
1127 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1129 char *buf, *token, *next, *p, *arg;
1134 buf = malloc(len+1);
1137 _("Couldn't allocate memory to parse options!\n"));
1141 for (token = buf; token && *token; token = next) {
1142 p = strchr(token, ',');
1148 arg = strchr(token, '=');
1153 if (strcmp(token, "clear-mmp") == 0 ||
1154 strcmp(token, "clear_mmp") == 0) {
1156 } else if (strcmp(token, "mmp_update_interval") == 0) {
1157 unsigned long interval;
1162 interval = strtoul(arg, &p, 0);
1165 _("Invalid mmp_update_interval: %s\n"),
1170 if (interval == 0) {
1171 interval = EXT4_MMP_UPDATE_INTERVAL;
1172 } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1174 _("mmp_update_interval too big: %lu\n"),
1179 printf(P_("Setting multiple mount protection update "
1180 "interval to %lu second\n",
1181 "Setting multiple mount protection update "
1182 "interval to %lu seconds\n", interval),
1184 fs->super->s_mmp_update_interval = interval;
1185 ext2fs_mark_super_dirty(fs);
1186 } else if (!strcmp(token, "test_fs")) {
1187 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1188 printf("Setting test filesystem flag\n");
1189 ext2fs_mark_super_dirty(fs);
1190 } else if (!strcmp(token, "^test_fs")) {
1191 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1192 printf("Clearing test filesystem flag\n");
1193 ext2fs_mark_super_dirty(fs);
1194 } else if (strcmp(token, "stride") == 0) {
1199 stride = strtoul(arg, &p, 0);
1202 _("Invalid RAID stride: %s\n"),
1208 } else if (strcmp(token, "stripe-width") == 0 ||
1209 strcmp(token, "stripe_width") == 0) {
1214 stripe_width = strtoul(arg, &p, 0);
1217 _("Invalid RAID stripe-width: %s\n"),
1222 stripe_width_set = 1;
1223 } else if (strcmp(token, "hash_alg") == 0 ||
1224 strcmp(token, "hash-alg") == 0) {
1229 hash_alg = e2p_string2hash(arg);
1232 _("Invalid hash algorithm: %s\n"),
1237 fs->super->s_def_hash_version = hash_alg;
1238 printf(_("Setting default hash algorithm "
1241 ext2fs_mark_super_dirty(fs);
1242 } else if (!strcmp(token, "mount_opts")) {
1247 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1249 "Extended mount options too long\n");
1252 ext_mount_opts = strdup(arg);
1257 fprintf(stderr, _("\nBad options specified.\n\n"
1258 "Extended options are separated by commas, "
1259 "and may take an argument which\n"
1260 "\tis set off by an equals ('=') sign.\n\n"
1261 "Valid extended options are:\n"
1263 "\thash_alg=<hash algorithm>\n"
1264 "\tmount_opts=<extended default mount options>\n"
1265 "\tstride=<RAID per-disk chunk size in blocks>\n"
1266 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1278 * Fill in the block bitmap bmap with the information regarding the
1279 * blocks to be moved
1281 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1282 ext2fs_block_bitmap bmap)
1286 ext2_badblocks_list bb_list = 0;
1287 blk64_t j, needed_blocks = 0;
1288 blk64_t start_blk, end_blk;
1290 retval = ext2fs_read_bb_inode(fs, &bb_list);
1294 for (i = 0; i < fs->group_desc_count; i++) {
1295 start_blk = ext2fs_inode_table_loc(fs, i) +
1296 fs->inode_blocks_per_group;
1298 end_blk = ext2fs_inode_table_loc(fs, i) +
1299 new_ino_blks_per_grp;
1301 for (j = start_blk; j < end_blk; j++) {
1302 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1304 * IF the block is a bad block we fail
1306 if (ext2fs_badblocks_list_test(bb_list, j)) {
1307 ext2fs_badblocks_list_free(bb_list);
1311 ext2fs_mark_block_bitmap2(bmap, j);
1314 * We are going to use this block for
1315 * inode table. So mark them used.
1317 ext2fs_mark_block_bitmap2(fs->block_map, j);
1320 needed_blocks += end_blk - start_blk;
1323 ext2fs_badblocks_list_free(bb_list);
1324 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1330 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1333 group = ext2fs_group_of_blk(fs, blk);
1334 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1336 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1341 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1343 blk_t start_blk, end_blk;
1344 start_blk = fs->super->s_first_data_block +
1345 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1347 * We cannot get new block beyond end_blk for for the last block group
1348 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1350 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1351 if (blk >= start_blk && blk <= end_blk)
1356 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1363 blk64_t blk, new_blk, goal;
1364 struct blk_move *bmv;
1366 retval = ext2fs_get_mem(fs->blocksize, &buf);
1370 for (new_blk = blk = fs->super->s_first_data_block;
1371 blk < ext2fs_blocks_count(fs->super); blk++) {
1372 if (!ext2fs_test_block_bitmap2(bmap, blk))
1375 if (ext2fs_is_meta_block(fs, blk)) {
1377 * If the block is mapping a fs meta data block
1378 * like group desc/block bitmap/inode bitmap. We
1379 * should find a block in the same group and fix
1380 * the respective fs metadata pointers. Otherwise
1383 group = ext2fs_group_of_blk(fs, blk);
1384 goal = ext2fs_group_first_block2(fs, group);
1390 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1394 /* new fs meta data block should be in the same group */
1395 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1400 /* Mark this block as allocated */
1401 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1403 /* Add it to block move list */
1404 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1409 bmv->new_loc = new_blk;
1411 list_add(&(bmv->list), &blk_move_list);
1413 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1417 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1423 ext2fs_free_mem(&buf);
1427 static blk64_t translate_block(blk64_t blk)
1429 struct list_head *entry;
1430 struct blk_move *bmv;
1432 list_for_each(entry, &blk_move_list) {
1433 bmv = list_entry(entry, struct blk_move, list);
1434 if (bmv->old_loc == blk)
1435 return bmv->new_loc;
1441 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1443 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1444 blk64_t ref_block EXT2FS_ATTR((unused)),
1445 int ref_offset EXT2FS_ATTR((unused)),
1450 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1452 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1454 new_blk = translate_block(*block_nr);
1456 *block_nr = new_blk;
1458 * This will force the ext2fs_write_inode in the iterator
1460 ret |= BLOCK_CHANGED;
1466 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1468 errcode_t retval = 0;
1471 char *block_buf = 0;
1472 struct ext2_inode inode;
1473 ext2_inode_scan scan = NULL;
1475 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1479 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1484 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1491 if (inode.i_links_count == 0)
1492 continue; /* inode not in use */
1495 * If we end up modifying the journal inode
1496 * the sb->s_jnl_blocks will differ. But a
1497 * subsequent e2fsck fixes that.
1498 * Do we need to fix this ??
1501 if (ext2fs_file_acl_block(fs, &inode) &&
1502 ext2fs_test_block_bitmap2(bmap,
1503 ext2fs_file_acl_block(fs, &inode))) {
1504 blk = translate_block(ext2fs_file_acl_block(fs,
1509 ext2fs_file_acl_block_set(fs, &inode, blk);
1512 * Write the inode to disk so that inode table
1515 retval = ext2fs_write_inode(fs, ino, &inode);
1520 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1523 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1524 process_block, bmap);
1531 ext2fs_free_mem(&block_buf);
1537 * We need to scan for inode and block bitmaps that may need to be
1538 * moved. This can take place if the filesystem was formatted for
1539 * RAID arrays using the mke2fs's extended option "stride".
1541 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1546 for (i = 0; i < fs->group_desc_count; i++) {
1547 blk = ext2fs_block_bitmap_loc(fs, i);
1548 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1549 new_blk = translate_block(blk);
1552 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1555 blk = ext2fs_inode_bitmap_loc(fs, i);
1556 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1557 new_blk = translate_block(blk);
1560 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1566 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1571 int new_ino_blks_per_grp;
1573 char *old_itable = NULL, *new_itable = NULL;
1574 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1575 unsigned long old_ino_size;
1576 int old_itable_size, new_itable_size;
1578 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1579 old_ino_size = EXT2_INODE_SIZE(fs->super);
1581 new_ino_blks_per_grp = ext2fs_div_ceil(
1582 EXT2_INODES_PER_GROUP(fs->super) *
1586 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1588 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1592 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1596 tmp_old_itable = old_itable;
1597 tmp_new_itable = new_itable;
1599 for (i = 0; i < fs->group_desc_count; i++) {
1600 blk = ext2fs_inode_table_loc(fs, i);
1601 retval = io_channel_read_blk64(fs->io, blk,
1602 fs->inode_blocks_per_group, old_itable);
1606 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1607 memcpy(new_itable, old_itable, old_ino_size);
1609 memset(new_itable+old_ino_size, 0,
1610 new_ino_size - old_ino_size);
1612 new_itable += new_ino_size;
1613 old_itable += old_ino_size;
1616 /* reset the pointer */
1617 old_itable = tmp_old_itable;
1618 new_itable = tmp_new_itable;
1620 retval = io_channel_write_blk64(fs->io, blk,
1621 new_ino_blks_per_grp, new_itable);
1626 /* Update the meta data */
1627 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1628 fs->super->s_inode_size = new_ino_size;
1632 ext2fs_free_mem(&old_itable);
1635 ext2fs_free_mem(&new_itable);
1640 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1644 unsigned int group = 0;
1645 unsigned int count = 0;
1650 * First calculate the block statistics
1652 for (blk = fs->super->s_first_data_block;
1653 blk < ext2fs_blocks_count(fs->super); blk++) {
1654 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1659 if ((count == fs->super->s_blocks_per_group) ||
1660 (blk == ext2fs_blocks_count(fs->super)-1)) {
1661 ext2fs_bg_free_blocks_count_set(fs, group++,
1667 total_free = EXT2FS_C2B(fs, total_free);
1668 ext2fs_free_blocks_count_set(fs->super, total_free);
1671 * Next, calculate the inode statistics
1678 /* Protect loop from wrap-around if s_inodes_count maxed */
1679 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1680 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1685 if ((count == fs->super->s_inodes_per_group) ||
1686 (ino == fs->super->s_inodes_count)) {
1687 ext2fs_bg_free_inodes_count_set(fs, group++,
1693 fs->super->s_free_inodes_count = total_free;
1694 ext2fs_mark_super_dirty(fs);
1698 #define list_for_each_safe(pos, pnext, head) \
1699 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1700 pos = pnext, pnext = pos->next)
1702 static void free_blk_move_list(void)
1704 struct list_head *entry, *tmp;
1705 struct blk_move *bmv;
1707 list_for_each_safe(entry, tmp, &blk_move_list) {
1708 bmv = list_entry(entry, struct blk_move, list);
1710 ext2fs_free_mem(&bmv);
1715 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1718 int new_ino_blks_per_grp;
1719 ext2fs_block_bitmap bmap;
1721 retval = ext2fs_read_inode_bitmap(fs);
1723 fputs(_("Failed to read inode bitmap\n"), stderr);
1726 retval = ext2fs_read_block_bitmap(fs);
1728 fputs(_("Failed to read block bitmap\n"), stderr);
1731 INIT_LIST_HEAD(&blk_move_list);
1734 new_ino_blks_per_grp = ext2fs_div_ceil(
1735 EXT2_INODES_PER_GROUP(fs->super)*
1739 /* We may change the file system.
1740 * Mark the file system as invalid so that
1741 * the user is prompted to run fsck.
1743 fs->super->s_state &= ~EXT2_VALID_FS;
1745 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1748 fputs(_("Failed to allocate block bitmap when "
1749 "increasing inode size\n"), stderr);
1752 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1754 fputs(_("Not enough space to increase inode size \n"), stderr);
1757 retval = move_block(fs, bmap);
1759 fputs(_("Failed to relocate blocks during inode resize \n"),
1763 retval = inode_scan_and_fix(fs, bmap);
1767 retval = group_desc_scan_and_fix(fs, bmap);
1771 retval = expand_inode_table(fs, new_size);
1775 ext2fs_calculate_summary_stats(fs);
1777 fs->super->s_state |= EXT2_VALID_FS;
1778 /* mark super block and block bitmap as dirty */
1779 ext2fs_mark_super_dirty(fs);
1780 ext2fs_mark_bb_dirty(fs);
1783 free_blk_move_list();
1784 ext2fs_free_block_bitmap(bmap);
1789 free_blk_move_list();
1790 ext2fs_free_block_bitmap(bmap);
1791 fputs(_("Error in resizing the inode size.\n"
1792 "Run e2undo to undo the "
1793 "file system changes. \n"), stderr);
1798 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1800 errcode_t retval = 0;
1801 const char *tdb_dir;
1803 char *dev_name, *tmp_name;
1807 * Configuration via a conf file would be
1810 profile_get_string(profile, "scratch_files",
1814 tmp_name = strdup(name);
1817 com_err(program_name, ENOMEM,
1818 _("Couldn't allocate memory for tdb filename\n"));
1821 dev_name = basename(tmp_name);
1823 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1825 tdb_dir = "/var/lib/e2fsprogs";
1827 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1828 access(tdb_dir, W_OK))
1831 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1834 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1836 if (!access(tdb_file, F_OK)) {
1837 if (unlink(tdb_file) < 0) {
1839 com_err(program_name, retval,
1840 _("while trying to delete %s"),
1847 set_undo_io_backing_manager(*io_ptr);
1848 *io_ptr = undo_io_manager;
1849 set_undo_io_backup_file(tdb_file);
1850 printf(_("To undo the tune2fs operation please run "
1851 "the command\n e2undo %s %s\n\n"),
1858 int main(int argc, char **argv)
1862 struct ext2_super_block *sb;
1863 io_manager io_ptr, io_ptr_orig = NULL;
1867 setlocale(LC_MESSAGES, "");
1868 setlocale(LC_CTYPE, "");
1869 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1870 textdomain(NLS_CAT_NAME);
1871 set_com_err_gettext(gettext);
1874 program_name = *argv;
1875 add_error_table(&et_ext2_error_table);
1877 #ifdef CONFIG_BUILD_FINDFS
1878 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1879 do_findfs(argc, argv);
1881 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1882 parse_e2label_options(argc, argv);
1884 parse_tune2fs_options(argc, argv);
1886 #ifdef CONFIG_TESTIO_DEBUG
1887 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1888 io_ptr = test_io_manager;
1889 test_io_backing_manager = unix_io_manager;
1892 io_ptr = unix_io_manager;
1895 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
1896 open_flag |= EXT2_FLAG_SKIP_MMP;
1898 open_flag |= EXT2_FLAG_64BITS;
1900 /* keep the filesystem struct around to dump MMP data */
1901 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
1903 retval = ext2fs_open2(device_name, io_options, open_flag,
1906 com_err(program_name, retval,
1907 _("while trying to open %s"),
1909 if (retval == EXT2_ET_MMP_FSCK_ON ||
1910 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
1911 dump_mmp_msg(fs->mmp_buf,
1912 _("If you are sure the filesystem "
1913 "is not in use on any node, run:\n"
1914 "'tune2fs -f -E clear_mmp {device}'\n"));
1915 else if (retval == EXT2_ET_MMP_FAILED)
1916 dump_mmp_msg(fs->mmp_buf, NULL);
1917 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
1919 _("MMP block magic is bad. Try to fix it by "
1920 "running:\n'e2fsck -f %s'\n"), device_name);
1921 else if (retval != EXT2_ET_MMP_FAILED)
1923 _("Couldn't find valid filesystem superblock.\n"));
1928 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
1930 if (I_flag && !io_ptr_orig) {
1932 * Check the inode size is right so we can issue an
1933 * error message and bail before setting up the tdb
1936 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1937 fprintf(stderr, _("The inode size is already %lu\n"),
1942 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1943 fprintf(stderr, _("Shrinking the inode size is "
1944 "not supported\n"));
1950 * If inode resize is requested use the
1953 io_ptr_orig = io_ptr;
1954 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1959 if (io_ptr != io_ptr_orig) {
1966 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1969 /* For e2label emulation */
1970 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1972 remove_error_table(&et_ext2_error_table);
1976 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1978 com_err("ext2fs_check_if_mount", retval,
1979 _("while determining whether %s is mounted."),
1984 /* Normally we only need to write out the superblock */
1985 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1988 sb->s_max_mnt_count = max_mount_count;
1989 ext2fs_mark_super_dirty(fs);
1990 printf(_("Setting maximal mount count to %d\n"),
1994 sb->s_mnt_count = mount_count;
1995 ext2fs_mark_super_dirty(fs);
1996 printf(_("Setting current mount count to %d\n"), mount_count);
1999 sb->s_errors = errors;
2000 ext2fs_mark_super_dirty(fs);
2001 printf(_("Setting error behavior to %d\n"), errors);
2004 sb->s_def_resgid = resgid;
2005 ext2fs_mark_super_dirty(fs);
2006 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2009 if (interval >= (1ULL << 32)) {
2010 com_err(program_name, 0,
2011 _("interval between checks is too big (%lu)"),
2016 sb->s_checkinterval = interval;
2017 ext2fs_mark_super_dirty(fs);
2018 printf(_("Setting interval between checks to %lu seconds\n"),
2022 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2023 ext2fs_blocks_count(sb) / 100.0);
2024 ext2fs_mark_super_dirty(fs);
2025 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2026 reserved_ratio, ext2fs_r_blocks_count(sb));
2029 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2030 com_err(program_name, 0,
2031 _("reserved blocks count is too big (%llu)"),
2036 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2037 ext2fs_mark_super_dirty(fs);
2038 printf(_("Setting reserved blocks count to %llu\n"),
2042 if (sb->s_feature_ro_compat &
2043 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
2044 fputs(_("\nThe filesystem already has sparse "
2045 "superblocks.\n"), stderr);
2047 sb->s_feature_ro_compat |=
2048 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2049 sb->s_state &= ~EXT2_VALID_FS;
2050 ext2fs_mark_super_dirty(fs);
2051 printf(_("\nSparse superblock flag set. %s"),
2056 fputs(_("\nClearing the sparse superflag not supported.\n"),
2062 sb->s_lastcheck = last_check_time;
2063 ext2fs_mark_super_dirty(fs);
2064 printf(_("Setting time filesystem last checked to %s\n"),
2065 ctime(&last_check_time));
2068 sb->s_def_resuid = resuid;
2069 ext2fs_mark_super_dirty(fs);
2070 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2073 if (strlen(new_label) > sizeof(sb->s_volume_name))
2074 fputs(_("Warning: label too long, truncating.\n"),
2076 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2077 strncpy(sb->s_volume_name, new_label,
2078 sizeof(sb->s_volume_name));
2079 ext2fs_mark_super_dirty(fs);
2082 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2083 strncpy(sb->s_last_mounted, new_last_mounted,
2084 sizeof(sb->s_last_mounted));
2085 ext2fs_mark_super_dirty(fs);
2088 rc = update_mntopts(fs, mntopts_cmd);
2093 rc = update_feature_set(fs, features_cmd);
2098 rc = parse_extended_opts(fs, extended_cmd);
2101 if (clear_mmp && !f_flag) {
2102 fputs(_("Error in using clear_mmp. "
2103 "It must be used with -f\n"),
2109 rc = ext2fs_mmp_clear(fs);
2112 if (journal_size || journal_device) {
2113 rc = add_journal(fs);
2119 if (mount_flags & EXT2_MF_MOUNTED) {
2120 fputs(_("The quota feature may only be changed when "
2121 "the filesystem is unmounted.\n"), stderr);
2125 handle_quota_options(fs);
2132 if (sb->s_feature_ro_compat &
2133 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2135 * Determine if the block group checksums are
2136 * correct so we know whether or not to set
2139 for (i = 0; i < fs->group_desc_count; i++)
2140 if (!ext2fs_group_desc_csum_verify(fs, i))
2142 if (i >= fs->group_desc_count)
2145 if ((strcasecmp(new_UUID, "null") == 0) ||
2146 (strcasecmp(new_UUID, "clear") == 0)) {
2147 uuid_clear(sb->s_uuid);
2148 } else if (strcasecmp(new_UUID, "time") == 0) {
2149 uuid_generate_time(sb->s_uuid);
2150 } else if (strcasecmp(new_UUID, "random") == 0) {
2151 uuid_generate(sb->s_uuid);
2152 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2153 com_err(program_name, 0, _("Invalid UUID format\n"));
2158 for (i = 0; i < fs->group_desc_count; i++)
2159 ext2fs_group_desc_csum_set(fs, i);
2160 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2162 ext2fs_mark_super_dirty(fs);
2165 if (mount_flags & EXT2_MF_MOUNTED) {
2166 fputs(_("The inode size may only be "
2167 "changed when the filesystem is "
2168 "unmounted.\n"), stderr);
2172 if (fs->super->s_feature_incompat &
2173 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2174 fputs(_("Changing the inode size not supported for "
2175 "filesystems with the flex_bg\n"
2176 "feature enabled.\n"),
2182 * We want to update group descriptor also
2183 * with the new free inode count
2185 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2186 if (resize_inode(fs, new_inode_size) == 0) {
2187 printf(_("Setting inode size %lu\n"),
2190 printf(_("Failed to change inode size\n"));
2199 sb->s_raid_stride = stride;
2200 ext2fs_mark_super_dirty(fs);
2201 printf(_("Setting stride size to %d\n"), stride);
2203 if (stripe_width_set) {
2204 sb->s_raid_stripe_width = stripe_width;
2205 ext2fs_mark_super_dirty(fs);
2206 printf(_("Setting stripe width to %d\n"), stripe_width);
2209 if (ext_mount_opts) {
2210 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2211 sizeof(fs->super->s_mount_opts));
2212 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2213 ext2fs_mark_super_dirty(fs);
2214 printf(_("Setting extended default mount options to '%s'\n"),
2216 free(ext_mount_opts);
2219 /* Warn if file system needs recovery and it is opened for writing. */
2220 if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) &&
2221 (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
2222 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER)) {
2224 _("Warning: needs_recovery flag is set. You may wish\n"
2225 "replay the journal then rerun this command, or any\n"
2226 "changes may be overwritten by journal recovery.\n"));
2230 remove_error_table(&et_ext2_error_table);
2234 ext2fs_mmp_stop(fs);
2238 return (ext2fs_close(fs) ? 1 : 0);