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() */
43 #include <strings.h> /* for strcasecmp() */
45 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
50 #include <sys/types.h>
54 #include "ext2fs/ext2_fs.h"
55 #include "ext2fs/ext2fs.h"
56 #include "et/com_err.h"
57 #include "uuid/uuid.h"
61 #include "blkid/blkid.h"
62 #include "quota/mkquota.h"
64 #include "../version.h"
65 #include "nls-enable.h"
67 #define QOPT_ENABLE (1)
68 #define QOPT_DISABLE (-1)
70 extern int ask_yn(const char *string, int def);
72 const char *program_name = "tune2fs";
74 char *new_label, *new_last_mounted, *new_UUID;
76 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
77 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
80 static time_t last_check_time;
81 static int print_label;
82 static int max_mount_count, mount_count, mount_flags;
83 static unsigned long interval;
84 static blk64_t reserved_blocks;
85 static double reserved_ratio;
86 static unsigned long resgid, resuid;
87 static unsigned short errors;
89 static char *features_cmd;
90 static char *mntopts_cmd;
91 static int stride, stripe_width;
92 static int stride_set, stripe_width_set;
93 static char *extended_cmd;
94 static unsigned long new_inode_size;
95 static char *ext_mount_opts;
96 static int usrquota, grpquota;
98 int journal_size, journal_flags;
101 static struct list_head blk_move_list;
104 struct list_head list;
110 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
112 #ifdef CONFIG_BUILD_FINDFS
113 void do_findfs(int argc, char **argv);
116 static void usage(void)
119 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
121 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
122 "\t[-m reserved_blocks_percent] "
123 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
124 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
125 "[-L volume_label]\n"
126 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
128 "\t[-Q quota_options]\n"
130 "\t[-E extended-option[,...]] [-T last_check_time] "
131 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
135 static __u32 ok_features[3] = {
137 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
138 EXT2_FEATURE_COMPAT_DIR_INDEX,
140 EXT2_FEATURE_INCOMPAT_FILETYPE |
141 EXT3_FEATURE_INCOMPAT_EXTENTS |
142 EXT4_FEATURE_INCOMPAT_FLEX_BG |
143 EXT4_FEATURE_INCOMPAT_MMP,
145 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
146 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
147 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
148 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
149 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
151 EXT4_FEATURE_RO_COMPAT_QUOTA |
153 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
156 static __u32 clear_ok_features[3] = {
158 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
159 EXT2_FEATURE_COMPAT_RESIZE_INODE |
160 EXT2_FEATURE_COMPAT_DIR_INDEX,
162 EXT2_FEATURE_INCOMPAT_FILETYPE |
163 EXT4_FEATURE_INCOMPAT_FLEX_BG |
164 EXT4_FEATURE_INCOMPAT_MMP,
166 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
167 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
168 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
169 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
171 EXT4_FEATURE_RO_COMPAT_QUOTA |
173 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
177 * Remove an external journal from the filesystem
179 static int remove_journal_device(ext2_filsys fs)
184 journal_superblock_t *jsb;
187 int commit_remove_journal = 0;
191 commit_remove_journal = 1; /* force removal even if error */
193 uuid_unparse(fs->super->s_journal_uuid, buf);
194 journal_path = blkid_get_devname(NULL, "UUID", buf);
198 ext2fs_find_block_device(fs->super->s_journal_dev);
200 goto no_valid_journal;
203 #ifdef CONFIG_TESTIO_DEBUG
204 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
205 io_ptr = test_io_manager;
206 test_io_backing_manager = unix_io_manager;
209 io_ptr = unix_io_manager;
210 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
211 EXT2_FLAG_JOURNAL_DEV_OK, 0,
212 fs->blocksize, io_ptr, &jfs);
214 com_err(program_name, retval, "%s",
215 _("while trying to open external journal"));
216 goto no_valid_journal;
218 if (!(jfs->super->s_feature_incompat &
219 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
220 fprintf(stderr, _("%s is not a journal device.\n"),
222 goto no_valid_journal;
225 /* Get the journal superblock */
226 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
227 com_err(program_name, retval, "%s",
228 _("while reading journal superblock"));
229 goto no_valid_journal;
232 jsb = (journal_superblock_t *) buf;
233 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
234 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
235 fputs(_("Journal superblock not found!\n"), stderr);
236 goto no_valid_journal;
239 /* Find the filesystem UUID */
240 nr_users = ntohl(jsb->s_nr_users);
241 for (i = 0; i < nr_users; i++) {
242 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
246 fputs(_("Filesystem's UUID not found on journal device.\n"),
248 commit_remove_journal = 1;
249 goto no_valid_journal;
252 for (i = 0; i < nr_users; i++)
253 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
254 jsb->s_nr_users = htonl(nr_users);
256 /* Write back the journal superblock */
257 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
258 com_err(program_name, retval,
259 "while writing journal superblock.");
260 goto no_valid_journal;
263 commit_remove_journal = 1;
266 if (commit_remove_journal == 0) {
267 fputs(_("Cannot locate journal device. It was NOT removed\n"
268 "Use -f option to remove missing journal device.\n"),
272 fs->super->s_journal_dev = 0;
273 uuid_clear(fs->super->s_journal_uuid);
274 ext2fs_mark_super_dirty(fs);
275 fputs(_("Journal removed\n"), stdout);
281 /* Helper function for remove_journal_inode */
282 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
283 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
284 blk64_t ref_block EXT2FS_ATTR((unused)),
285 int ref_offset EXT2FS_ATTR((unused)),
286 void *private EXT2FS_ATTR((unused)))
292 ext2fs_unmark_block_bitmap2(fs->block_map, block);
293 group = ext2fs_group_of_blk2(fs, block);
294 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
295 ext2fs_group_desc_csum_set(fs, group);
296 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
301 * Remove the journal inode from the filesystem
303 static errcode_t remove_journal_inode(ext2_filsys fs)
305 struct ext2_inode inode;
307 ino_t ino = fs->super->s_journal_inum;
309 retval = ext2fs_read_inode(fs, ino, &inode);
311 com_err(program_name, retval, "%s",
312 _("while reading journal inode"));
315 if (ino == EXT2_JOURNAL_INO) {
316 retval = ext2fs_read_bitmaps(fs);
318 com_err(program_name, retval, "%s",
319 _("while reading bitmaps"));
322 retval = ext2fs_block_iterate3(fs, ino,
323 BLOCK_FLAG_READ_ONLY, NULL,
324 release_blocks_proc, NULL);
326 com_err(program_name, retval, "%s",
327 _("while clearing journal inode"));
330 memset(&inode, 0, sizeof(inode));
331 ext2fs_mark_bb_dirty(fs);
332 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
334 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
335 retval = ext2fs_write_inode(fs, ino, &inode);
337 com_err(program_name, retval, "%s",
338 _("while writing journal inode"));
341 fs->super->s_journal_inum = 0;
342 ext2fs_mark_super_dirty(fs);
348 * Update the default mount options
350 static int update_mntopts(ext2_filsys fs, char *mntopts)
352 struct ext2_super_block *sb = fs->super;
354 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
355 fprintf(stderr, _("Invalid mount option set: %s\n"),
359 ext2fs_mark_super_dirty(fs);
364 static int check_fsck_needed(ext2_filsys fs)
366 if (fs->super->s_state & EXT2_VALID_FS)
368 printf("\n%s\n", _(please_fsck));
369 if (mount_flags & EXT2_MF_READONLY)
370 printf(_("(and reboot afterwards!)\n"));
374 static void request_fsck_afterwards(ext2_filsys fs)
376 static int requested = 0;
380 fs->super->s_state &= ~EXT2_VALID_FS;
381 printf("\n%s\n", _(please_fsck));
382 if (mount_flags & EXT2_MF_READONLY)
383 printf("%s", _("(and reboot afterwards!)\n"));
387 * Update the feature set as provided by the user.
389 static int update_feature_set(ext2_filsys fs, char *features)
391 struct ext2_super_block *sb = fs->super;
392 struct ext2_group_desc *gd;
393 __u32 old_features[3];
396 unsigned int mask_err;
398 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
399 ((&sb->s_feature_compat)[(type)] & (mask)))
400 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
401 !((&sb->s_feature_compat)[(type)] & (mask)))
402 #define FEATURE_CHANGED(type, mask) ((mask) & \
403 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
405 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
406 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
407 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
409 if (e2p_edit_feature2(features, &sb->s_feature_compat,
410 ok_features, clear_ok_features,
411 &type_err, &mask_err)) {
414 _("Invalid filesystem option set: %s\n"),
416 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
417 fprintf(stderr, _("Clearing filesystem feature '%s' "
419 e2p_feature2string(type_err &
420 E2P_FEATURE_TYPE_MASK,
423 fprintf(stderr, _("Setting filesystem feature '%s' "
425 e2p_feature2string(type_err, mask_err));
429 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
430 if ((mount_flags & EXT2_MF_MOUNTED) &&
431 !(mount_flags & EXT2_MF_READONLY)) {
432 fputs(_("The has_journal feature may only be "
433 "cleared when the filesystem is\n"
434 "unmounted or mounted "
435 "read-only.\n"), stderr);
438 if (sb->s_feature_incompat &
439 EXT3_FEATURE_INCOMPAT_RECOVER) {
440 fputs(_("The needs_recovery flag is set. "
441 "Please run e2fsck before clearing\n"
442 "the has_journal flag.\n"), stderr);
445 if (sb->s_journal_inum) {
446 if (remove_journal_inode(fs))
449 if (sb->s_journal_dev) {
450 if (remove_journal_device(fs))
454 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
457 if ((mount_flags & EXT2_MF_MOUNTED) ||
458 (mount_flags & EXT2_MF_READONLY)) {
459 fputs(_("The multiple mount protection feature can't\n"
460 "be set if the filesystem is mounted or\n"
461 "read-only.\n"), stderr);
465 error = ext2fs_mmp_init(fs);
467 fputs(_("\nError while enabling multiple mount "
468 "protection feature."), stderr);
473 * We want to update group desc with the new free blocks count
475 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
477 printf(_("Multiple mount protection has been enabled "
478 "with update interval %ds.\n"),
479 sb->s_mmp_update_interval);
482 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
485 if (mount_flags & EXT2_MF_READONLY) {
486 fputs(_("The multiple mount protection feature cannot\n"
487 "be disabled if the filesystem is readonly.\n"),
492 error = ext2fs_read_bitmaps(fs);
494 fputs(_("Error while reading bitmaps\n"), stderr);
498 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
500 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
502 if (error == EXT2_ET_MMP_MAGIC_INVALID)
503 printf(_("Magic number in MMP block does not "
504 "match. expected: %x, actual: %x\n"),
505 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
507 com_err(program_name, error, "%s",
508 _("while reading MMP block."));
512 /* We need to force out the group descriptors as well */
513 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
514 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
517 sb->s_mmp_update_interval = 0;
520 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
522 * If adding a journal flag, let the create journal
523 * code below handle setting the flag and creating the
524 * journal. We supply a default size if necessary.
528 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
531 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
532 if (!sb->s_def_hash_version)
533 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
534 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
535 uuid_generate((unsigned char *) sb->s_hash_seed);
538 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
539 if (ext2fs_check_desc(fs)) {
540 fputs(_("Clearing the flex_bg flag would "
541 "cause the the filesystem to be\n"
542 "inconsistent.\n"), stderr);
547 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
548 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
549 if ((mount_flags & EXT2_MF_MOUNTED) &&
550 !(mount_flags & EXT2_MF_READONLY)) {
551 fputs(_("The huge_file feature may only be "
552 "cleared when the filesystem is\n"
553 "unmounted or mounted "
554 "read-only.\n"), stderr);
559 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
560 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
561 for (i = 0; i < fs->group_desc_count; i++) {
562 gd = ext2fs_group_desc(fs, fs->group_desc, i);
563 gd->bg_itable_unused = 0;
564 gd->bg_flags = EXT2_BG_INODE_ZEROED;
565 ext2fs_group_desc_csum_set(fs, i);
567 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
570 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
571 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
572 for (i = 0; i < fs->group_desc_count; i++) {
573 gd = ext2fs_group_desc(fs, fs->group_desc, i);
574 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
576 * XXX what we really should do is zap
577 * uninitialized inode tables instead.
579 request_fsck_afterwards(fs);
582 gd->bg_itable_unused = 0;
586 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
589 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
590 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
592 * Set the Q_flag here and handle the quota options in the code
597 /* Enable both user quota and group quota by default */
598 usrquota = QOPT_ENABLE;
599 grpquota = QOPT_ENABLE;
601 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
604 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
605 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
607 * Set the Q_flag here and handle the quota options in the code
611 fputs(_("\nWarning: '^quota' option overrides '-Q'"
612 "arguments.\n"), stderr);
614 /* Disable both user quota and group quota by default */
615 usrquota = QOPT_DISABLE;
616 grpquota = QOPT_DISABLE;
619 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
620 (sb->s_feature_compat || sb->s_feature_ro_compat ||
621 sb->s_feature_incompat))
622 ext2fs_update_dynamic_rev(fs);
624 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
625 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
626 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
627 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
628 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
629 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
630 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
631 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
632 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
633 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
634 request_fsck_afterwards(fs);
636 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
637 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
638 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
639 ext2fs_mark_super_dirty(fs);
645 * Add a journal to the filesystem.
647 static int add_journal(ext2_filsys fs)
649 unsigned long journal_blocks;
654 if (fs->super->s_feature_compat &
655 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
656 fputs(_("The filesystem already has a journal.\n"), stderr);
659 if (journal_device) {
660 check_plausibility(journal_device);
661 check_mount(journal_device, 0, _("journal"));
662 #ifdef CONFIG_TESTIO_DEBUG
663 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
664 io_ptr = test_io_manager;
665 test_io_backing_manager = unix_io_manager;
668 io_ptr = unix_io_manager;
669 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
670 EXT2_FLAG_JOURNAL_DEV_OK, 0,
671 fs->blocksize, io_ptr, &jfs);
673 com_err(program_name, retval,
674 _("\n\twhile trying to open journal on %s\n"),
678 printf(_("Creating journal on device %s: "),
682 retval = ext2fs_add_journal_device(fs, jfs);
685 com_err(program_name, retval,
686 _("while adding filesystem to journal on %s"),
690 fputs(_("done\n"), stdout);
691 } else if (journal_size) {
692 fputs(_("Creating journal inode: "), stdout);
694 journal_blocks = figure_journal_size(journal_size, fs);
696 retval = ext2fs_add_journal_inode(fs, journal_blocks,
699 fprintf(stderr, "\n");
700 com_err(program_name, retval, "%s",
701 _("\n\twhile trying to create journal file"));
704 fputs(_("done\n"), stdout);
706 * If the filesystem wasn't mounted, we need to force
707 * the block group descriptors out.
709 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
710 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
712 print_check_message(fs->super->s_max_mnt_count,
713 fs->super->s_checkinterval);
717 free(journal_device);
721 static void handle_quota_options(ext2_filsys fs)
726 if (!usrquota && !grpquota)
730 quota_init_context(&qctx, fs, -1);
732 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
733 quota_compute_usage(qctx);
735 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
736 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
738 quota_update_limits(qctx, qf_ino, USRQUOTA);
739 quota_write_inode(qctx, USRQUOTA);
740 } else if (usrquota == QOPT_DISABLE) {
741 quota_remove_inode(fs, USRQUOTA);
744 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
745 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
747 quota_update_limits(qctx, qf_ino, GRPQUOTA);
748 quota_write_inode(qctx, GRPQUOTA);
749 } else if (grpquota == QOPT_DISABLE) {
750 quota_remove_inode(fs, GRPQUOTA);
753 quota_release_context(&qctx);
755 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
756 fprintf(stderr, "%s", _("\nWarning: the quota feature is still "
757 "under development\n"
758 "See https://ext4.wiki.kernel.org/"
759 "index.php/Quota for more information\n\n"));
760 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
761 ext2fs_mark_super_dirty(fs);
762 } else if (!fs->super->s_usr_quota_inum &&
763 !fs->super->s_grp_quota_inum) {
764 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
765 ext2fs_mark_super_dirty(fs);
772 static void parse_quota_opts(const char *opts)
774 char *buf, *token, *next, *p;
780 fputs(_("Couldn't allocate memory to parse quota "
781 "options!\n"), stderr);
785 for (token = buf; token && *token; token = next) {
786 p = strchr(token, ',');
793 if (strcmp(token, "usrquota") == 0) {
794 usrquota = QOPT_ENABLE;
795 } else if (strcmp(token, "^usrquota") == 0) {
796 usrquota = QOPT_DISABLE;
797 } else if (strcmp(token, "grpquota") == 0) {
798 grpquota = QOPT_ENABLE;
799 } else if (strcmp(token, "^grpquota") == 0) {
800 grpquota = QOPT_DISABLE;
802 fputs(_("\nBad quota options specified.\n\n"
803 "Following valid quota options are available "
804 "(pass by separating with comma):\n"
816 static void parse_e2label_options(int argc, char ** argv)
818 if ((argc < 2) || (argc > 3)) {
819 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
822 io_options = strchr(argv[1], '?');
825 device_name = blkid_get_devname(NULL, argv[1], NULL);
827 com_err("e2label", 0, _("Unable to resolve '%s'"),
831 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
833 open_flag |= EXT2_FLAG_RW;
840 static time_t parse_time(char *str)
844 if (strcmp(str, "now") == 0) {
847 memset(&ts, 0, sizeof(ts));
849 strptime(str, "%Y%m%d%H%M%S", &ts);
851 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
852 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
855 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
856 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
857 ts.tm_min > 59 || ts.tm_sec > 61)
860 if (ts.tm_mday == 0) {
861 com_err(program_name, 0,
862 _("Couldn't parse date/time specifier: %s"),
867 return (mktime(&ts));
870 static void parse_tune2fs_options(int argc, char **argv)
876 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
879 strcat(optstring, "Q:");
883 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
884 while ((c = getopt(argc, argv, optstring)) != EOF)
887 max_mount_count = strtol(optarg, &tmp, 0);
888 if (*tmp || max_mount_count > 16000) {
889 com_err(program_name, 0,
890 _("bad mounts count - %s"),
894 if (max_mount_count == 0)
895 max_mount_count = -1;
897 open_flag = EXT2_FLAG_RW;
900 mount_count = strtoul(optarg, &tmp, 0);
901 if (*tmp || mount_count > 16000) {
902 com_err(program_name, 0,
903 _("bad mounts count - %s"),
908 open_flag = EXT2_FLAG_RW;
911 if (strcmp(optarg, "continue") == 0)
912 errors = EXT2_ERRORS_CONTINUE;
913 else if (strcmp(optarg, "remount-ro") == 0)
914 errors = EXT2_ERRORS_RO;
915 else if (strcmp(optarg, "panic") == 0)
916 errors = EXT2_ERRORS_PANIC;
918 com_err(program_name, 0,
919 _("bad error behavior - %s"),
924 open_flag = EXT2_FLAG_RW;
927 extended_cmd = optarg;
928 open_flag |= EXT2_FLAG_RW;
930 case 'f': /* Force */
934 resgid = strtoul(optarg, &tmp, 0);
936 gr = getgrnam(optarg);
945 com_err(program_name, 0,
946 _("bad gid/group name - %s"),
951 open_flag = EXT2_FLAG_RW;
954 interval = strtoul(optarg, &tmp, 0);
967 case 'M': /* months! */
968 interval *= 86400 * 30;
972 case 'W': /* weeks */
973 interval *= 86400 * 7;
978 com_err(program_name, 0,
979 _("bad interval - %s"), optarg);
983 open_flag = EXT2_FLAG_RW;
988 open_flag = EXT2_FLAG_RW;
991 parse_journal_opts(optarg);
992 open_flag = EXT2_FLAG_RW;
1000 open_flag |= EXT2_FLAG_RW |
1001 EXT2_FLAG_JOURNAL_DEV_OK;
1004 reserved_ratio = strtod(optarg, &tmp);
1005 if (*tmp || reserved_ratio > 50 ||
1006 reserved_ratio < 0) {
1007 com_err(program_name, 0,
1008 _("bad reserved block ratio - %s"),
1013 open_flag = EXT2_FLAG_RW;
1016 new_last_mounted = optarg;
1018 open_flag = EXT2_FLAG_RW;
1022 com_err(program_name, 0, "%s",
1023 _("-o may only be specified once"));
1026 mntopts_cmd = optarg;
1027 open_flag = EXT2_FLAG_RW;
1031 com_err(program_name, 0, "%s",
1032 _("-O may only be specified once"));
1035 features_cmd = optarg;
1036 open_flag = EXT2_FLAG_RW;
1041 parse_quota_opts(optarg);
1042 open_flag = EXT2_FLAG_RW;
1046 reserved_blocks = strtoul(optarg, &tmp, 0);
1048 com_err(program_name, 0,
1049 _("bad reserved blocks count - %s"),
1054 open_flag = EXT2_FLAG_RW;
1056 case 's': /* Deprecated */
1057 s_flag = atoi(optarg);
1058 open_flag = EXT2_FLAG_RW;
1062 last_check_time = parse_time(optarg);
1063 open_flag = EXT2_FLAG_RW;
1066 resuid = strtoul(optarg, &tmp, 0);
1068 pw = getpwnam(optarg);
1072 resuid = pw->pw_uid;
1077 com_err(program_name, 0,
1078 _("bad uid/user name - %s"),
1083 open_flag = EXT2_FLAG_RW;
1088 open_flag = EXT2_FLAG_RW |
1089 EXT2_FLAG_JOURNAL_DEV_OK;
1092 new_inode_size = strtoul(optarg, &tmp, 0);
1094 com_err(program_name, 0,
1095 _("bad inode size - %s"),
1099 if (!((new_inode_size &
1100 (new_inode_size - 1)) == 0)) {
1101 com_err(program_name, 0,
1102 _("Inode size must be a "
1103 "power of two- %s"),
1107 open_flag = EXT2_FLAG_RW;
1113 if (optind < argc - 1 || optind == argc)
1115 if (!open_flag && !l_flag)
1117 io_options = strchr(argv[optind], '?');
1120 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1122 com_err(program_name, 0, _("Unable to resolve '%s'"),
1128 #ifdef CONFIG_BUILD_FINDFS
1129 void do_findfs(int argc, char **argv)
1134 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1135 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1138 dev = blkid_get_devname(NULL, argv[1], NULL);
1140 com_err("findfs", 0, _("Unable to resolve '%s'"),
1149 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1151 char *buf, *token, *next, *p, *arg;
1156 buf = malloc(len+1);
1158 fprintf(stderr, "%s",
1159 _("Couldn't allocate memory to parse options!\n"));
1163 for (token = buf; token && *token; token = next) {
1164 p = strchr(token, ',');
1170 arg = strchr(token, '=');
1175 if (strcmp(token, "clear-mmp") == 0 ||
1176 strcmp(token, "clear_mmp") == 0) {
1178 } else if (strcmp(token, "mmp_update_interval") == 0) {
1184 intv = strtoul(arg, &p, 0);
1187 _("Invalid mmp_update_interval: %s\n"),
1193 intv = EXT4_MMP_UPDATE_INTERVAL;
1194 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1196 _("mmp_update_interval too big: %lu\n"),
1201 printf(P_("Setting multiple mount protection update "
1202 "interval to %lu second\n",
1203 "Setting multiple mount protection update "
1204 "interval to %lu seconds\n", intv),
1206 fs->super->s_mmp_update_interval = intv;
1207 ext2fs_mark_super_dirty(fs);
1208 } else if (!strcmp(token, "test_fs")) {
1209 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1210 printf("Setting test filesystem flag\n");
1211 ext2fs_mark_super_dirty(fs);
1212 } else if (!strcmp(token, "^test_fs")) {
1213 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1214 printf("Clearing test filesystem flag\n");
1215 ext2fs_mark_super_dirty(fs);
1216 } else if (strcmp(token, "stride") == 0) {
1221 stride = strtoul(arg, &p, 0);
1224 _("Invalid RAID stride: %s\n"),
1230 } else if (strcmp(token, "stripe-width") == 0 ||
1231 strcmp(token, "stripe_width") == 0) {
1236 stripe_width = strtoul(arg, &p, 0);
1239 _("Invalid RAID stripe-width: %s\n"),
1244 stripe_width_set = 1;
1245 } else if (strcmp(token, "hash_alg") == 0 ||
1246 strcmp(token, "hash-alg") == 0) {
1251 hash_alg = e2p_string2hash(arg);
1254 _("Invalid hash algorithm: %s\n"),
1259 fs->super->s_def_hash_version = hash_alg;
1260 printf(_("Setting default hash algorithm "
1263 ext2fs_mark_super_dirty(fs);
1264 } else if (!strcmp(token, "mount_opts")) {
1269 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1271 "Extended mount options too long\n");
1274 ext_mount_opts = strdup(arg);
1279 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1280 "Extended options are separated by commas, "
1281 "and may take an argument which\n"
1282 "\tis set off by an equals ('=') sign.\n\n"
1283 "Valid extended options are:\n"
1285 "\thash_alg=<hash algorithm>\n"
1286 "\tmount_opts=<extended default mount options>\n"
1287 "\tstride=<RAID per-disk chunk size in blocks>\n"
1288 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1300 * Fill in the block bitmap bmap with the information regarding the
1301 * blocks to be moved
1303 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1304 ext2fs_block_bitmap bmap)
1308 ext2_badblocks_list bb_list = 0;
1309 blk64_t j, needed_blocks = 0;
1310 blk64_t start_blk, end_blk;
1312 retval = ext2fs_read_bb_inode(fs, &bb_list);
1316 for (i = 0; i < fs->group_desc_count; i++) {
1317 start_blk = ext2fs_inode_table_loc(fs, i) +
1318 fs->inode_blocks_per_group;
1320 end_blk = ext2fs_inode_table_loc(fs, i) +
1321 new_ino_blks_per_grp;
1323 for (j = start_blk; j < end_blk; j++) {
1324 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1326 * IF the block is a bad block we fail
1328 if (ext2fs_badblocks_list_test(bb_list, j)) {
1329 ext2fs_badblocks_list_free(bb_list);
1333 ext2fs_mark_block_bitmap2(bmap, j);
1336 * We are going to use this block for
1337 * inode table. So mark them used.
1339 ext2fs_mark_block_bitmap2(fs->block_map, j);
1342 needed_blocks += end_blk - start_blk;
1345 ext2fs_badblocks_list_free(bb_list);
1346 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1352 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
1355 group = ext2fs_group_of_blk2(fs, blk);
1356 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1358 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1363 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
1365 blk64_t start_blk, end_blk;
1366 start_blk = fs->super->s_first_data_block +
1367 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1369 * We cannot get new block beyond end_blk for for the last block group
1370 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1372 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1373 if (blk >= start_blk && blk <= end_blk)
1378 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1385 blk64_t blk, new_blk, goal;
1386 struct blk_move *bmv;
1388 retval = ext2fs_get_mem(fs->blocksize, &buf);
1392 for (new_blk = blk = fs->super->s_first_data_block;
1393 blk < ext2fs_blocks_count(fs->super); blk++) {
1394 if (!ext2fs_test_block_bitmap2(bmap, blk))
1397 if (ext2fs_is_meta_block(fs, blk)) {
1399 * If the block is mapping a fs meta data block
1400 * like group desc/block bitmap/inode bitmap. We
1401 * should find a block in the same group and fix
1402 * the respective fs metadata pointers. Otherwise
1405 group = ext2fs_group_of_blk2(fs, blk);
1406 goal = ext2fs_group_first_block2(fs, group);
1412 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1416 /* new fs meta data block should be in the same group */
1417 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1422 /* Mark this block as allocated */
1423 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1425 /* Add it to block move list */
1426 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1431 bmv->new_loc = new_blk;
1433 list_add(&(bmv->list), &blk_move_list);
1435 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1439 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1445 ext2fs_free_mem(&buf);
1449 static blk64_t translate_block(blk64_t blk)
1451 struct list_head *entry;
1452 struct blk_move *bmv;
1454 list_for_each(entry, &blk_move_list) {
1455 bmv = list_entry(entry, struct blk_move, list);
1456 if (bmv->old_loc == blk)
1457 return bmv->new_loc;
1463 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1465 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1466 blk64_t ref_block EXT2FS_ATTR((unused)),
1467 int ref_offset EXT2FS_ATTR((unused)),
1472 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1474 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1476 new_blk = translate_block(*block_nr);
1478 *block_nr = new_blk;
1480 * This will force the ext2fs_write_inode in the iterator
1482 ret |= BLOCK_CHANGED;
1488 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1490 errcode_t retval = 0;
1493 char *block_buf = 0;
1494 struct ext2_inode inode;
1495 ext2_inode_scan scan = NULL;
1497 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1501 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1506 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1513 if (inode.i_links_count == 0)
1514 continue; /* inode not in use */
1517 * If we end up modifying the journal inode
1518 * the sb->s_jnl_blocks will differ. But a
1519 * subsequent e2fsck fixes that.
1520 * Do we need to fix this ??
1523 if (ext2fs_file_acl_block(fs, &inode) &&
1524 ext2fs_test_block_bitmap2(bmap,
1525 ext2fs_file_acl_block(fs, &inode))) {
1526 blk = translate_block(ext2fs_file_acl_block(fs,
1531 ext2fs_file_acl_block_set(fs, &inode, blk);
1534 * Write the inode to disk so that inode table
1537 retval = ext2fs_write_inode(fs, ino, &inode);
1542 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1545 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1546 process_block, bmap);
1553 ext2fs_free_mem(&block_buf);
1559 * We need to scan for inode and block bitmaps that may need to be
1560 * moved. This can take place if the filesystem was formatted for
1561 * RAID arrays using the mke2fs's extended option "stride".
1563 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1566 blk64_t blk, new_blk;
1568 for (i = 0; i < fs->group_desc_count; i++) {
1569 blk = ext2fs_block_bitmap_loc(fs, i);
1570 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1571 new_blk = translate_block(blk);
1574 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1577 blk = ext2fs_inode_bitmap_loc(fs, i);
1578 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1579 new_blk = translate_block(blk);
1582 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1588 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1593 int new_ino_blks_per_grp;
1595 char *old_itable = NULL, *new_itable = NULL;
1596 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1597 unsigned long old_ino_size;
1598 int old_itable_size, new_itable_size;
1600 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1601 old_ino_size = EXT2_INODE_SIZE(fs->super);
1603 new_ino_blks_per_grp = ext2fs_div_ceil(
1604 EXT2_INODES_PER_GROUP(fs->super) *
1608 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1610 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1614 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1618 tmp_old_itable = old_itable;
1619 tmp_new_itable = new_itable;
1621 for (i = 0; i < fs->group_desc_count; i++) {
1622 blk = ext2fs_inode_table_loc(fs, i);
1623 retval = io_channel_read_blk64(fs->io, blk,
1624 fs->inode_blocks_per_group, old_itable);
1628 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1629 memcpy(new_itable, old_itable, old_ino_size);
1631 memset(new_itable+old_ino_size, 0,
1632 new_ino_size - old_ino_size);
1634 new_itable += new_ino_size;
1635 old_itable += old_ino_size;
1638 /* reset the pointer */
1639 old_itable = tmp_old_itable;
1640 new_itable = tmp_new_itable;
1642 retval = io_channel_write_blk64(fs->io, blk,
1643 new_ino_blks_per_grp, new_itable);
1648 /* Update the meta data */
1649 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1650 fs->super->s_inode_size = new_ino_size;
1654 ext2fs_free_mem(&old_itable);
1657 ext2fs_free_mem(&new_itable);
1662 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1666 unsigned int group = 0;
1667 unsigned int count = 0;
1672 * First calculate the block statistics
1674 for (blk = fs->super->s_first_data_block;
1675 blk < ext2fs_blocks_count(fs->super); blk++) {
1676 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1681 if ((count == fs->super->s_blocks_per_group) ||
1682 (blk == ext2fs_blocks_count(fs->super)-1)) {
1683 ext2fs_bg_free_blocks_count_set(fs, group++,
1689 total_free = EXT2FS_C2B(fs, total_free);
1690 ext2fs_free_blocks_count_set(fs->super, total_free);
1693 * Next, calculate the inode statistics
1700 /* Protect loop from wrap-around if s_inodes_count maxed */
1701 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1702 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1707 if ((count == fs->super->s_inodes_per_group) ||
1708 (ino == fs->super->s_inodes_count)) {
1709 ext2fs_bg_free_inodes_count_set(fs, group++,
1715 fs->super->s_free_inodes_count = total_free;
1716 ext2fs_mark_super_dirty(fs);
1720 #define list_for_each_safe(pos, pnext, head) \
1721 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1722 pos = pnext, pnext = pos->next)
1724 static void free_blk_move_list(void)
1726 struct list_head *entry, *tmp;
1727 struct blk_move *bmv;
1729 list_for_each_safe(entry, tmp, &blk_move_list) {
1730 bmv = list_entry(entry, struct blk_move, list);
1732 ext2fs_free_mem(&bmv);
1737 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1740 int new_ino_blks_per_grp;
1741 ext2fs_block_bitmap bmap;
1743 retval = ext2fs_read_inode_bitmap(fs);
1745 fputs(_("Failed to read inode bitmap\n"), stderr);
1748 retval = ext2fs_read_block_bitmap(fs);
1750 fputs(_("Failed to read block bitmap\n"), stderr);
1753 INIT_LIST_HEAD(&blk_move_list);
1756 new_ino_blks_per_grp = ext2fs_div_ceil(
1757 EXT2_INODES_PER_GROUP(fs->super)*
1761 /* We may change the file system.
1762 * Mark the file system as invalid so that
1763 * the user is prompted to run fsck.
1765 fs->super->s_state &= ~EXT2_VALID_FS;
1767 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1770 fputs(_("Failed to allocate block bitmap when "
1771 "increasing inode size\n"), stderr);
1774 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1776 fputs(_("Not enough space to increase inode size \n"), stderr);
1779 retval = move_block(fs, bmap);
1781 fputs(_("Failed to relocate blocks during inode resize \n"),
1785 retval = inode_scan_and_fix(fs, bmap);
1789 retval = group_desc_scan_and_fix(fs, bmap);
1793 retval = expand_inode_table(fs, new_size);
1797 ext2fs_calculate_summary_stats(fs);
1799 fs->super->s_state |= EXT2_VALID_FS;
1800 /* mark super block and block bitmap as dirty */
1801 ext2fs_mark_super_dirty(fs);
1802 ext2fs_mark_bb_dirty(fs);
1805 free_blk_move_list();
1806 ext2fs_free_block_bitmap(bmap);
1811 free_blk_move_list();
1812 ext2fs_free_block_bitmap(bmap);
1813 fputs(_("Error in resizing the inode size.\n"
1814 "Run e2undo to undo the "
1815 "file system changes. \n"), stderr);
1820 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1822 errcode_t retval = 0;
1823 const char *tdb_dir;
1825 char *dev_name, *tmp_name;
1829 * Configuration via a conf file would be
1832 profile_get_string(profile, "scratch_files",
1836 tmp_name = strdup(name);
1839 com_err(program_name, ENOMEM, "%s",
1840 _("Couldn't allocate memory for tdb filename\n"));
1843 dev_name = basename(tmp_name);
1845 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1847 tdb_dir = "/var/lib/e2fsprogs";
1849 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1850 access(tdb_dir, W_OK))
1853 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1856 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1858 if (!access(tdb_file, F_OK)) {
1859 if (unlink(tdb_file) < 0) {
1861 com_err(program_name, retval,
1862 _("while trying to delete %s"),
1869 set_undo_io_backing_manager(*io_ptr);
1870 *io_ptr = undo_io_manager;
1871 set_undo_io_backup_file(tdb_file);
1872 printf(_("To undo the tune2fs operation please run "
1873 "the command\n e2undo %s %s\n\n"),
1880 int main(int argc, char **argv)
1884 struct ext2_super_block *sb;
1885 io_manager io_ptr, io_ptr_orig = NULL;
1889 setlocale(LC_MESSAGES, "");
1890 setlocale(LC_CTYPE, "");
1891 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1892 textdomain(NLS_CAT_NAME);
1893 set_com_err_gettext(gettext);
1896 program_name = *argv;
1897 add_error_table(&et_ext2_error_table);
1899 #ifdef CONFIG_BUILD_FINDFS
1900 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1901 do_findfs(argc, argv);
1903 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1904 parse_e2label_options(argc, argv);
1906 parse_tune2fs_options(argc, argv);
1908 #ifdef CONFIG_TESTIO_DEBUG
1909 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1910 io_ptr = test_io_manager;
1911 test_io_backing_manager = unix_io_manager;
1914 io_ptr = unix_io_manager;
1917 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
1918 open_flag |= EXT2_FLAG_SKIP_MMP;
1920 open_flag |= EXT2_FLAG_64BITS;
1922 /* keep the filesystem struct around to dump MMP data */
1923 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
1925 retval = ext2fs_open2(device_name, io_options, open_flag,
1928 com_err(program_name, retval,
1929 _("while trying to open %s"),
1931 if (retval == EXT2_ET_MMP_FSCK_ON ||
1932 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
1933 dump_mmp_msg(fs->mmp_buf,
1934 _("If you are sure the filesystem "
1935 "is not in use on any node, run:\n"
1936 "'tune2fs -f -E clear_mmp {device}'\n"));
1937 else if (retval == EXT2_ET_MMP_FAILED)
1938 dump_mmp_msg(fs->mmp_buf, NULL);
1939 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
1941 _("MMP block magic is bad. Try to fix it by "
1942 "running:\n'e2fsck -f %s'\n"), device_name);
1943 else if (retval != EXT2_ET_MMP_FAILED)
1944 fprintf(stderr, "%s",
1945 _("Couldn't find valid filesystem superblock.\n"));
1950 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
1952 if (I_flag && !io_ptr_orig) {
1954 * Check the inode size is right so we can issue an
1955 * error message and bail before setting up the tdb
1958 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1959 fprintf(stderr, _("The inode size is already %lu\n"),
1964 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1965 fprintf(stderr, "%s",
1966 _("Shrinking inode size is not supported\n"));
1970 if (new_inode_size > fs->blocksize) {
1971 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
1972 new_inode_size, fs->blocksize);
1978 * If inode resize is requested use the
1981 io_ptr_orig = io_ptr;
1982 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1987 if (io_ptr != io_ptr_orig) {
1994 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1997 /* For e2label emulation */
1998 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2000 remove_error_table(&et_ext2_error_table);
2004 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2006 com_err("ext2fs_check_if_mount", retval,
2007 _("while determining whether %s is mounted."),
2012 /* Normally we only need to write out the superblock */
2013 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2016 sb->s_max_mnt_count = max_mount_count;
2017 ext2fs_mark_super_dirty(fs);
2018 printf(_("Setting maximal mount count to %d\n"),
2022 sb->s_mnt_count = mount_count;
2023 ext2fs_mark_super_dirty(fs);
2024 printf(_("Setting current mount count to %d\n"), mount_count);
2027 sb->s_errors = errors;
2028 ext2fs_mark_super_dirty(fs);
2029 printf(_("Setting error behavior to %d\n"), errors);
2032 sb->s_def_resgid = resgid;
2033 ext2fs_mark_super_dirty(fs);
2034 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2037 if (interval >= (1ULL << 32)) {
2038 com_err(program_name, 0,
2039 _("interval between checks is too big (%lu)"),
2044 sb->s_checkinterval = interval;
2045 ext2fs_mark_super_dirty(fs);
2046 printf(_("Setting interval between checks to %lu seconds\n"),
2050 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2051 ext2fs_blocks_count(sb) / 100.0);
2052 ext2fs_mark_super_dirty(fs);
2053 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2054 reserved_ratio, ext2fs_r_blocks_count(sb));
2057 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2058 com_err(program_name, 0,
2059 _("reserved blocks count is too big (%llu)"),
2064 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2065 ext2fs_mark_super_dirty(fs);
2066 printf(_("Setting reserved blocks count to %llu\n"),
2070 if (sb->s_feature_ro_compat &
2071 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
2072 fputs(_("\nThe filesystem already has sparse "
2073 "superblocks.\n"), stderr);
2075 sb->s_feature_ro_compat |=
2076 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2077 sb->s_state &= ~EXT2_VALID_FS;
2078 ext2fs_mark_super_dirty(fs);
2079 printf(_("\nSparse superblock flag set. %s"),
2084 fputs(_("\nClearing the sparse superflag not supported.\n"),
2090 sb->s_lastcheck = last_check_time;
2091 ext2fs_mark_super_dirty(fs);
2092 printf(_("Setting time filesystem last checked to %s\n"),
2093 ctime(&last_check_time));
2096 sb->s_def_resuid = resuid;
2097 ext2fs_mark_super_dirty(fs);
2098 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2101 if (strlen(new_label) > sizeof(sb->s_volume_name))
2102 fputs(_("Warning: label too long, truncating.\n"),
2104 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2105 strncpy(sb->s_volume_name, new_label,
2106 sizeof(sb->s_volume_name));
2107 ext2fs_mark_super_dirty(fs);
2110 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2111 strncpy(sb->s_last_mounted, new_last_mounted,
2112 sizeof(sb->s_last_mounted));
2113 ext2fs_mark_super_dirty(fs);
2116 rc = update_mntopts(fs, mntopts_cmd);
2121 rc = update_feature_set(fs, features_cmd);
2126 rc = parse_extended_opts(fs, extended_cmd);
2129 if (clear_mmp && !f_flag) {
2130 fputs(_("Error in using clear_mmp. "
2131 "It must be used with -f\n"),
2137 rc = ext2fs_mmp_clear(fs);
2140 if (journal_size || journal_device) {
2141 rc = add_journal(fs);
2147 if (mount_flags & EXT2_MF_MOUNTED) {
2148 fputs(_("The quota feature may only be changed when "
2149 "the filesystem is unmounted.\n"), stderr);
2153 handle_quota_options(fs);
2160 if (sb->s_feature_ro_compat &
2161 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2163 * Changing the UUID requires rewriting all metadata,
2164 * which can race with a mounted fs. Don't allow that.
2166 if (mount_flags & EXT2_MF_MOUNTED) {
2167 fputs(_("The UUID may only be "
2168 "changed when the filesystem is "
2169 "unmounted.\n"), stderr);
2172 if (check_fsck_needed(fs))
2176 * Determine if the block group checksums are
2177 * correct so we know whether or not to set
2180 for (i = 0; i < fs->group_desc_count; i++)
2181 if (!ext2fs_group_desc_csum_verify(fs, i))
2183 if (i >= fs->group_desc_count)
2186 if ((strcasecmp(new_UUID, "null") == 0) ||
2187 (strcasecmp(new_UUID, "clear") == 0)) {
2188 uuid_clear(sb->s_uuid);
2189 } else if (strcasecmp(new_UUID, "time") == 0) {
2190 uuid_generate_time(sb->s_uuid);
2191 } else if (strcasecmp(new_UUID, "random") == 0) {
2192 uuid_generate(sb->s_uuid);
2193 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2194 com_err(program_name, 0, "%s",
2195 _("Invalid UUID format\n"));
2200 for (i = 0; i < fs->group_desc_count; i++)
2201 ext2fs_group_desc_csum_set(fs, i);
2202 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2204 ext2fs_mark_super_dirty(fs);
2207 if (mount_flags & EXT2_MF_MOUNTED) {
2208 fputs(_("The inode size may only be "
2209 "changed when the filesystem is "
2210 "unmounted.\n"), stderr);
2214 if (fs->super->s_feature_incompat &
2215 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2216 fputs(_("Changing the inode size not supported for "
2217 "filesystems with the flex_bg\n"
2218 "feature enabled.\n"),
2224 * We want to update group descriptor also
2225 * with the new free inode count
2227 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2228 if (resize_inode(fs, new_inode_size) == 0) {
2229 printf(_("Setting inode size %lu\n"),
2232 printf("%s", _("Failed to change inode size\n"));
2241 sb->s_raid_stride = stride;
2242 ext2fs_mark_super_dirty(fs);
2243 printf(_("Setting stride size to %d\n"), stride);
2245 if (stripe_width_set) {
2246 sb->s_raid_stripe_width = stripe_width;
2247 ext2fs_mark_super_dirty(fs);
2248 printf(_("Setting stripe width to %d\n"), stripe_width);
2250 if (ext_mount_opts) {
2251 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2252 sizeof(fs->super->s_mount_opts));
2253 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2254 ext2fs_mark_super_dirty(fs);
2255 printf(_("Setting extended default mount options to '%s'\n"),
2257 free(ext_mount_opts);
2260 remove_error_table(&et_ext2_error_table);
2264 ext2fs_mmp_stop(fs);
2268 return (ext2fs_close(fs) ? 1 : 0);