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 |
145 EXT4_FEATURE_RO_COMPAT_QUOTA |
147 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
150 static __u32 clear_ok_features[3] = {
152 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
153 EXT2_FEATURE_COMPAT_RESIZE_INODE |
154 EXT2_FEATURE_COMPAT_DIR_INDEX,
156 EXT2_FEATURE_INCOMPAT_FILETYPE |
157 EXT4_FEATURE_INCOMPAT_FLEX_BG |
158 EXT4_FEATURE_INCOMPAT_MMP,
160 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
161 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
162 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
163 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
165 EXT4_FEATURE_RO_COMPAT_QUOTA |
167 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
171 * Remove an external journal from the filesystem
173 static int remove_journal_device(ext2_filsys fs)
178 journal_superblock_t *jsb;
181 int commit_remove_journal = 0;
185 commit_remove_journal = 1; /* force removal even if error */
187 uuid_unparse(fs->super->s_journal_uuid, buf);
188 journal_path = blkid_get_devname(NULL, "UUID", buf);
192 ext2fs_find_block_device(fs->super->s_journal_dev);
194 goto no_valid_journal;
197 #ifdef CONFIG_TESTIO_DEBUG
198 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
199 io_ptr = test_io_manager;
200 test_io_backing_manager = unix_io_manager;
203 io_ptr = unix_io_manager;
204 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
205 EXT2_FLAG_JOURNAL_DEV_OK, 0,
206 fs->blocksize, io_ptr, &jfs);
208 com_err(program_name, retval,
209 _("while trying to open external journal"));
210 goto no_valid_journal;
212 if (!(jfs->super->s_feature_incompat &
213 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
214 fprintf(stderr, _("%s is not a journal device.\n"),
216 goto no_valid_journal;
219 /* Get the journal superblock */
220 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
221 com_err(program_name, retval,
222 _("while reading journal superblock"));
223 goto no_valid_journal;
226 jsb = (journal_superblock_t *) buf;
227 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
228 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
229 fputs(_("Journal superblock not found!\n"), stderr);
230 goto no_valid_journal;
233 /* Find the filesystem UUID */
234 nr_users = ntohl(jsb->s_nr_users);
235 for (i = 0; i < nr_users; i++) {
236 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
240 fputs(_("Filesystem's UUID not found on journal device.\n"),
242 commit_remove_journal = 1;
243 goto no_valid_journal;
246 for (i = 0; i < nr_users; i++)
247 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
248 jsb->s_nr_users = htonl(nr_users);
250 /* Write back the journal superblock */
251 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
252 com_err(program_name, retval,
253 "while writing journal superblock.");
254 goto no_valid_journal;
257 commit_remove_journal = 1;
260 if (commit_remove_journal == 0) {
261 fputs(_("Cannot locate journal device. It was NOT removed\n"
262 "Use -f option to remove missing journal device.\n"),
266 fs->super->s_journal_dev = 0;
267 uuid_clear(fs->super->s_journal_uuid);
268 ext2fs_mark_super_dirty(fs);
269 fputs(_("Journal removed\n"), stdout);
275 /* Helper function for remove_journal_inode */
276 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
277 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
278 blk64_t ref_block EXT2FS_ATTR((unused)),
279 int ref_offset EXT2FS_ATTR((unused)),
280 void *private EXT2FS_ATTR((unused)))
286 ext2fs_unmark_block_bitmap2(fs->block_map, block);
287 group = ext2fs_group_of_blk2(fs, block);
288 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
289 ext2fs_group_desc_csum_set(fs, group);
290 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
295 * Remove the journal inode from the filesystem
297 static errcode_t remove_journal_inode(ext2_filsys fs)
299 struct ext2_inode inode;
301 ino_t ino = fs->super->s_journal_inum;
303 retval = ext2fs_read_inode(fs, ino, &inode);
305 com_err(program_name, retval,
306 _("while reading journal inode"));
309 if (ino == EXT2_JOURNAL_INO) {
310 retval = ext2fs_read_bitmaps(fs);
312 com_err(program_name, retval,
313 _("while reading bitmaps"));
316 retval = ext2fs_block_iterate3(fs, ino,
317 BLOCK_FLAG_READ_ONLY, NULL,
318 release_blocks_proc, NULL);
320 com_err(program_name, retval,
321 _("while clearing journal inode"));
324 memset(&inode, 0, sizeof(inode));
325 ext2fs_mark_bb_dirty(fs);
326 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
328 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
329 retval = ext2fs_write_inode(fs, ino, &inode);
331 com_err(program_name, retval,
332 _("while writing journal inode"));
335 fs->super->s_journal_inum = 0;
336 ext2fs_mark_super_dirty(fs);
342 * Update the default mount options
344 static int update_mntopts(ext2_filsys fs, char *mntopts)
346 struct ext2_super_block *sb = fs->super;
348 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
349 fprintf(stderr, _("Invalid mount option set: %s\n"),
353 ext2fs_mark_super_dirty(fs);
358 static void request_fsck_afterwards(ext2_filsys fs)
360 static int requested = 0;
364 fs->super->s_state &= ~EXT2_VALID_FS;
365 printf("\n%s\n", _(please_fsck));
366 if (mount_flags & EXT2_MF_READONLY)
367 printf(_("(and reboot afterwards!)\n"));
371 * Update the feature set as provided by the user.
373 static int update_feature_set(ext2_filsys fs, char *features)
375 struct ext2_super_block *sb = fs->super;
376 struct ext2_group_desc *gd;
377 __u32 old_features[3];
379 unsigned int mask_err;
381 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
382 ((&sb->s_feature_compat)[(type)] & (mask)))
383 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
384 !((&sb->s_feature_compat)[(type)] & (mask)))
385 #define FEATURE_CHANGED(type, mask) ((mask) & \
386 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
388 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
389 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
390 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
392 if (e2p_edit_feature2(features, &sb->s_feature_compat,
393 ok_features, clear_ok_features,
394 &type_err, &mask_err)) {
397 _("Invalid filesystem option set: %s\n"),
399 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
400 fprintf(stderr, _("Clearing filesystem feature '%s' "
402 e2p_feature2string(type_err &
403 E2P_FEATURE_TYPE_MASK,
406 fprintf(stderr, _("Setting filesystem feature '%s' "
408 e2p_feature2string(type_err, mask_err));
412 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
413 if ((mount_flags & EXT2_MF_MOUNTED) &&
414 !(mount_flags & EXT2_MF_READONLY)) {
415 fputs(_("The has_journal feature may only be "
416 "cleared when the filesystem is\n"
417 "unmounted or mounted "
418 "read-only.\n"), stderr);
421 if (sb->s_feature_incompat &
422 EXT3_FEATURE_INCOMPAT_RECOVER) {
423 fputs(_("The needs_recovery flag is set. "
424 "Please run e2fsck before clearing\n"
425 "the has_journal flag.\n"), stderr);
428 if (sb->s_journal_inum) {
429 if (remove_journal_inode(fs))
432 if (sb->s_journal_dev) {
433 if (remove_journal_device(fs))
437 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
440 if ((mount_flags & EXT2_MF_MOUNTED) ||
441 (mount_flags & EXT2_MF_READONLY)) {
442 fputs(_("The multiple mount protection feature can't\n"
443 "be set if the filesystem is mounted or\n"
444 "read-only.\n"), stderr);
448 error = ext2fs_mmp_init(fs);
450 fputs(_("\nError while enabling multiple mount "
451 "protection feature."), stderr);
456 * We want to update group desc with the new free blocks count
458 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
460 printf(_("Multiple mount protection has been enabled "
461 "with update interval %ds.\n"),
462 sb->s_mmp_update_interval);
465 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
468 if (mount_flags & EXT2_MF_READONLY) {
469 fputs(_("The multiple mount protection feature cannot\n"
470 "be disabled if the filesystem is readonly.\n"),
475 error = ext2fs_read_bitmaps(fs);
477 fputs(_("Error while reading bitmaps\n"), stderr);
481 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
483 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
485 if (error == EXT2_ET_MMP_MAGIC_INVALID)
486 printf(_("Magic number in MMP block does not "
487 "match. expected: %x, actual: %x\n"),
488 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
490 com_err(program_name, error,
491 _("while reading MMP block."));
495 /* We need to force out the group descriptors as well */
496 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
497 ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
500 sb->s_mmp_update_interval = 0;
503 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
505 * If adding a journal flag, let the create journal
506 * code below handle setting the flag and creating the
507 * journal. We supply a default size if necessary.
511 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
514 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
515 if (!sb->s_def_hash_version)
516 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
517 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
518 uuid_generate((unsigned char *) sb->s_hash_seed);
521 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
522 if (ext2fs_check_desc(fs)) {
523 fputs(_("Clearing the flex_bg flag would "
524 "cause the the filesystem to be\n"
525 "inconsistent.\n"), stderr);
530 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
531 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
532 if ((mount_flags & EXT2_MF_MOUNTED) &&
533 !(mount_flags & EXT2_MF_READONLY)) {
534 fputs(_("The huge_file feature may only be "
535 "cleared when the filesystem is\n"
536 "unmounted or mounted "
537 "read-only.\n"), stderr);
542 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
543 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
544 for (i = 0; i < fs->group_desc_count; i++) {
545 gd = ext2fs_group_desc(fs, fs->group_desc, i);
546 gd->bg_itable_unused = 0;
547 gd->bg_flags = EXT2_BG_INODE_ZEROED;
548 ext2fs_group_desc_csum_set(fs, i);
550 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
553 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
554 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
555 for (i = 0; i < fs->group_desc_count; i++) {
556 gd = ext2fs_group_desc(fs, fs->group_desc, i);
557 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
559 * XXX what we really should do is zap
560 * uninitialized inode tables instead.
562 request_fsck_afterwards(fs);
565 gd->bg_itable_unused = 0;
569 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
572 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
573 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
575 * Set the Q_flag here and handle the quota options in the code
580 /* Enable both user quota and group quota by default */
581 usrquota = QOPT_ENABLE;
582 grpquota = QOPT_ENABLE;
584 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
587 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
588 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
590 * Set the Q_flag here and handle the quota options in the code
594 fputs(_("\nWarning: '^quota' option overrides '-Q'"
595 "arguments.\n"), stderr);
597 /* Disable both user quota and group quota by default */
598 usrquota = QOPT_DISABLE;
599 grpquota = QOPT_DISABLE;
602 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
603 (sb->s_feature_compat || sb->s_feature_ro_compat ||
604 sb->s_feature_incompat))
605 ext2fs_update_dynamic_rev(fs);
607 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
608 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
609 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
610 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
611 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
612 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
613 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
614 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
615 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
616 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
617 request_fsck_afterwards(fs);
619 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
620 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
621 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
622 ext2fs_mark_super_dirty(fs);
628 * Add a journal to the filesystem.
630 static int add_journal(ext2_filsys fs)
632 unsigned long journal_blocks;
637 if (fs->super->s_feature_compat &
638 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
639 fputs(_("The filesystem already has a journal.\n"), stderr);
642 if (journal_device) {
643 check_plausibility(journal_device);
644 check_mount(journal_device, 0, _("journal"));
645 #ifdef CONFIG_TESTIO_DEBUG
646 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
647 io_ptr = test_io_manager;
648 test_io_backing_manager = unix_io_manager;
651 io_ptr = unix_io_manager;
652 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
653 EXT2_FLAG_JOURNAL_DEV_OK, 0,
654 fs->blocksize, io_ptr, &jfs);
656 com_err(program_name, retval,
657 _("\n\twhile trying to open journal on %s\n"),
661 printf(_("Creating journal on device %s: "),
665 retval = ext2fs_add_journal_device(fs, jfs);
668 com_err(program_name, retval,
669 _("while adding filesystem to journal on %s"),
673 fputs(_("done\n"), stdout);
674 } else if (journal_size) {
675 fputs(_("Creating journal inode: "), stdout);
677 journal_blocks = figure_journal_size(journal_size, fs);
679 retval = ext2fs_add_journal_inode(fs, journal_blocks,
682 fprintf(stderr, "\n");
683 com_err(program_name, retval,
684 _("\n\twhile trying to create journal file"));
687 fputs(_("done\n"), stdout);
689 * If the filesystem wasn't mounted, we need to force
690 * the block group descriptors out.
692 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
693 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
695 print_check_message(fs->super->s_max_mnt_count,
696 fs->super->s_checkinterval);
700 free(journal_device);
704 void handle_quota_options(ext2_filsys fs)
709 if (!usrquota && !grpquota)
713 quota_init_context(&qctx, fs, -1);
715 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
716 quota_compute_usage(qctx);
718 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
719 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
721 quota_update_limits(qctx, qf_ino, USRQUOTA);
722 quota_write_inode(qctx, USRQUOTA);
723 } else if (usrquota == QOPT_DISABLE) {
724 quota_remove_inode(fs, USRQUOTA);
727 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
728 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
730 quota_update_limits(qctx, qf_ino, GRPQUOTA);
731 quota_write_inode(qctx, GRPQUOTA);
732 } else if (grpquota == QOPT_DISABLE) {
733 quota_remove_inode(fs, GRPQUOTA);
736 quota_release_context(&qctx);
738 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
739 fprintf(stderr, _("\nWarning: the quota feature is still "
740 "under development\n"
741 "See https://ext4.wiki.kernel.org/"
742 "index.php/Quota for more information\n\n"));
743 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
744 ext2fs_mark_super_dirty(fs);
745 } else if (!fs->super->s_usr_quota_inum &&
746 !fs->super->s_grp_quota_inum) {
747 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
748 ext2fs_mark_super_dirty(fs);
754 void parse_quota_opts(const char *opts)
756 char *buf, *token, *next, *p;
762 fputs(_("Couldn't allocate memory to parse quota "
763 "options!\n"), stderr);
767 for (token = buf; token && *token; token = next) {
768 p = strchr(token, ',');
775 if (strcmp(token, "usrquota") == 0) {
776 usrquota = QOPT_ENABLE;
777 } else if (strcmp(token, "^usrquota") == 0) {
778 usrquota = QOPT_DISABLE;
779 } else if (strcmp(token, "grpquota") == 0) {
780 grpquota = QOPT_ENABLE;
781 } else if (strcmp(token, "^grpquota") == 0) {
782 grpquota = QOPT_DISABLE;
784 fputs(_("\nBad quota options specified.\n\n"
785 "Following valid quota options are available "
786 "(pass by separating with comma):\n"
799 static void parse_e2label_options(int argc, char ** argv)
801 if ((argc < 2) || (argc > 3)) {
802 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
805 io_options = strchr(argv[1], '?');
808 device_name = blkid_get_devname(NULL, argv[1], NULL);
810 com_err("e2label", 0, _("Unable to resolve '%s'"),
814 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
816 open_flag |= EXT2_FLAG_RW;
823 static time_t parse_time(char *str)
827 if (strcmp(str, "now") == 0) {
830 memset(&ts, 0, sizeof(ts));
832 strptime(str, "%Y%m%d%H%M%S", &ts);
834 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
835 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
838 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
839 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
840 ts.tm_min > 59 || ts.tm_sec > 61)
843 if (ts.tm_mday == 0) {
844 com_err(program_name, 0,
845 _("Couldn't parse date/time specifier: %s"),
850 return (mktime(&ts));
853 static void parse_tune2fs_options(int argc, char **argv)
862 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
863 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)
866 max_mount_count = strtol(optarg, &tmp, 0);
867 if (*tmp || max_mount_count > 16000) {
868 com_err(program_name, 0,
869 _("bad mounts count - %s"),
873 if (max_mount_count == 0)
874 max_mount_count = -1;
876 open_flag = EXT2_FLAG_RW;
879 mount_count = strtoul(optarg, &tmp, 0);
880 if (*tmp || mount_count > 16000) {
881 com_err(program_name, 0,
882 _("bad mounts count - %s"),
887 open_flag = EXT2_FLAG_RW;
890 if (strcmp(optarg, "continue") == 0)
891 errors = EXT2_ERRORS_CONTINUE;
892 else if (strcmp(optarg, "remount-ro") == 0)
893 errors = EXT2_ERRORS_RO;
894 else if (strcmp(optarg, "panic") == 0)
895 errors = EXT2_ERRORS_PANIC;
897 com_err(program_name, 0,
898 _("bad error behavior - %s"),
903 open_flag = EXT2_FLAG_RW;
906 extended_cmd = optarg;
907 open_flag |= EXT2_FLAG_RW;
909 case 'f': /* Force */
913 resgid = strtoul(optarg, &tmp, 0);
915 gr = getgrnam(optarg);
924 com_err(program_name, 0,
925 _("bad gid/group name - %s"),
930 open_flag = EXT2_FLAG_RW;
933 interval = strtoul(optarg, &tmp, 0);
946 case 'M': /* months! */
947 interval *= 86400 * 30;
951 case 'W': /* weeks */
952 interval *= 86400 * 7;
957 com_err(program_name, 0,
958 _("bad interval - %s"), optarg);
962 open_flag = EXT2_FLAG_RW;
967 open_flag = EXT2_FLAG_RW;
970 parse_journal_opts(optarg);
971 open_flag = EXT2_FLAG_RW;
979 open_flag |= EXT2_FLAG_RW |
980 EXT2_FLAG_JOURNAL_DEV_OK;
983 reserved_ratio = strtod(optarg, &tmp);
984 if (*tmp || reserved_ratio > 50 ||
985 reserved_ratio < 0) {
986 com_err(program_name, 0,
987 _("bad reserved block ratio - %s"),
992 open_flag = EXT2_FLAG_RW;
995 new_last_mounted = optarg;
997 open_flag = EXT2_FLAG_RW;
1001 com_err(program_name, 0,
1002 _("-o may only be specified once"));
1005 mntopts_cmd = optarg;
1006 open_flag = EXT2_FLAG_RW;
1010 com_err(program_name, 0,
1011 _("-O may only be specified once"));
1014 features_cmd = optarg;
1015 open_flag = EXT2_FLAG_RW;
1019 parse_quota_opts(optarg);
1020 open_flag = EXT2_FLAG_RW;
1023 reserved_blocks = strtoul(optarg, &tmp, 0);
1025 com_err(program_name, 0,
1026 _("bad reserved blocks count - %s"),
1031 open_flag = EXT2_FLAG_RW;
1033 case 's': /* Deprecated */
1034 s_flag = atoi(optarg);
1035 open_flag = EXT2_FLAG_RW;
1039 last_check_time = parse_time(optarg);
1040 open_flag = EXT2_FLAG_RW;
1043 resuid = strtoul(optarg, &tmp, 0);
1045 pw = getpwnam(optarg);
1049 resuid = pw->pw_uid;
1054 com_err(program_name, 0,
1055 _("bad uid/user name - %s"),
1060 open_flag = EXT2_FLAG_RW;
1065 open_flag = EXT2_FLAG_RW |
1066 EXT2_FLAG_JOURNAL_DEV_OK;
1069 new_inode_size = strtoul(optarg, &tmp, 0);
1071 com_err(program_name, 0,
1072 _("bad inode size - %s"),
1076 if (!((new_inode_size &
1077 (new_inode_size - 1)) == 0)) {
1078 com_err(program_name, 0,
1079 _("Inode size must be a "
1080 "power of two- %s"),
1084 open_flag = EXT2_FLAG_RW;
1090 if (optind < argc - 1 || optind == argc)
1092 if (!open_flag && !l_flag)
1094 io_options = strchr(argv[optind], '?');
1097 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1099 com_err(program_name, 0, _("Unable to resolve '%s'"),
1105 #ifdef CONFIG_BUILD_FINDFS
1106 void do_findfs(int argc, char **argv)
1111 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1112 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1115 dev = blkid_get_devname(NULL, argv[1], NULL);
1117 com_err("findfs", 0, _("Unable to resolve '%s'"),
1126 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1128 char *buf, *token, *next, *p, *arg;
1133 buf = malloc(len+1);
1136 _("Couldn't allocate memory to parse options!\n"));
1140 for (token = buf; token && *token; token = next) {
1141 p = strchr(token, ',');
1147 arg = strchr(token, '=');
1152 if (strcmp(token, "clear-mmp") == 0 ||
1153 strcmp(token, "clear_mmp") == 0) {
1155 } else if (strcmp(token, "mmp_update_interval") == 0) {
1156 unsigned long interval;
1161 interval = strtoul(arg, &p, 0);
1164 _("Invalid mmp_update_interval: %s\n"),
1169 if (interval == 0) {
1170 interval = EXT4_MMP_UPDATE_INTERVAL;
1171 } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1173 _("mmp_update_interval too big: %lu\n"),
1178 printf(P_("Setting multiple mount protection update "
1179 "interval to %lu second\n",
1180 "Setting multiple mount protection update "
1181 "interval to %lu seconds\n", interval),
1183 fs->super->s_mmp_update_interval = interval;
1184 ext2fs_mark_super_dirty(fs);
1185 } else if (!strcmp(token, "test_fs")) {
1186 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1187 printf("Setting test filesystem flag\n");
1188 ext2fs_mark_super_dirty(fs);
1189 } else if (!strcmp(token, "^test_fs")) {
1190 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1191 printf("Clearing test filesystem flag\n");
1192 ext2fs_mark_super_dirty(fs);
1193 } else if (strcmp(token, "stride") == 0) {
1198 stride = strtoul(arg, &p, 0);
1201 _("Invalid RAID stride: %s\n"),
1207 } else if (strcmp(token, "stripe-width") == 0 ||
1208 strcmp(token, "stripe_width") == 0) {
1213 stripe_width = strtoul(arg, &p, 0);
1216 _("Invalid RAID stripe-width: %s\n"),
1221 stripe_width_set = 1;
1222 } else if (strcmp(token, "hash_alg") == 0 ||
1223 strcmp(token, "hash-alg") == 0) {
1228 hash_alg = e2p_string2hash(arg);
1231 _("Invalid hash algorithm: %s\n"),
1236 fs->super->s_def_hash_version = hash_alg;
1237 printf(_("Setting default hash algorithm "
1240 ext2fs_mark_super_dirty(fs);
1241 } else if (!strcmp(token, "mount_opts")) {
1246 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1248 "Extended mount options too long\n");
1251 ext_mount_opts = strdup(arg);
1256 fprintf(stderr, _("\nBad options specified.\n\n"
1257 "Extended options are separated by commas, "
1258 "and may take an argument which\n"
1259 "\tis set off by an equals ('=') sign.\n\n"
1260 "Valid extended options are:\n"
1262 "\thash_alg=<hash algorithm>\n"
1263 "\tmount_opts=<extended default mount options>\n"
1264 "\tstride=<RAID per-disk chunk size in blocks>\n"
1265 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1277 * Fill in the block bitmap bmap with the information regarding the
1278 * blocks to be moved
1280 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1281 ext2fs_block_bitmap bmap)
1285 ext2_badblocks_list bb_list = 0;
1286 blk64_t j, needed_blocks = 0;
1287 blk64_t start_blk, end_blk;
1289 retval = ext2fs_read_bb_inode(fs, &bb_list);
1293 for (i = 0; i < fs->group_desc_count; i++) {
1294 start_blk = ext2fs_inode_table_loc(fs, i) +
1295 fs->inode_blocks_per_group;
1297 end_blk = ext2fs_inode_table_loc(fs, i) +
1298 new_ino_blks_per_grp;
1300 for (j = start_blk; j < end_blk; j++) {
1301 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1303 * IF the block is a bad block we fail
1305 if (ext2fs_badblocks_list_test(bb_list, j)) {
1306 ext2fs_badblocks_list_free(bb_list);
1310 ext2fs_mark_block_bitmap2(bmap, j);
1313 * We are going to use this block for
1314 * inode table. So mark them used.
1316 ext2fs_mark_block_bitmap2(fs->block_map, j);
1319 needed_blocks += end_blk - start_blk;
1322 ext2fs_badblocks_list_free(bb_list);
1323 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1329 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1332 group = ext2fs_group_of_blk(fs, blk);
1333 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1335 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1340 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1342 blk_t start_blk, end_blk;
1343 start_blk = fs->super->s_first_data_block +
1344 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1346 * We cannot get new block beyond end_blk for for the last block group
1347 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1349 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1350 if (blk >= start_blk && blk <= end_blk)
1355 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1362 blk64_t blk, new_blk, goal;
1363 struct blk_move *bmv;
1365 retval = ext2fs_get_mem(fs->blocksize, &buf);
1369 for (new_blk = blk = fs->super->s_first_data_block;
1370 blk < ext2fs_blocks_count(fs->super); blk++) {
1371 if (!ext2fs_test_block_bitmap2(bmap, blk))
1374 if (ext2fs_is_meta_block(fs, blk)) {
1376 * If the block is mapping a fs meta data block
1377 * like group desc/block bitmap/inode bitmap. We
1378 * should find a block in the same group and fix
1379 * the respective fs metadata pointers. Otherwise
1382 group = ext2fs_group_of_blk(fs, blk);
1383 goal = ext2fs_group_first_block2(fs, group);
1389 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1393 /* new fs meta data block should be in the same group */
1394 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1399 /* Mark this block as allocated */
1400 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1402 /* Add it to block move list */
1403 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1408 bmv->new_loc = new_blk;
1410 list_add(&(bmv->list), &blk_move_list);
1412 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1416 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1422 ext2fs_free_mem(&buf);
1426 static blk64_t translate_block(blk64_t blk)
1428 struct list_head *entry;
1429 struct blk_move *bmv;
1431 list_for_each(entry, &blk_move_list) {
1432 bmv = list_entry(entry, struct blk_move, list);
1433 if (bmv->old_loc == blk)
1434 return bmv->new_loc;
1440 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1442 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1443 blk64_t ref_block EXT2FS_ATTR((unused)),
1444 int ref_offset EXT2FS_ATTR((unused)),
1449 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1451 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1453 new_blk = translate_block(*block_nr);
1455 *block_nr = new_blk;
1457 * This will force the ext2fs_write_inode in the iterator
1459 ret |= BLOCK_CHANGED;
1465 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1467 errcode_t retval = 0;
1470 char *block_buf = 0;
1471 struct ext2_inode inode;
1472 ext2_inode_scan scan = NULL;
1474 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1478 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1483 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1490 if (inode.i_links_count == 0)
1491 continue; /* inode not in use */
1494 * If we end up modifying the journal inode
1495 * the sb->s_jnl_blocks will differ. But a
1496 * subsequent e2fsck fixes that.
1497 * Do we need to fix this ??
1500 if (ext2fs_file_acl_block(fs, &inode) &&
1501 ext2fs_test_block_bitmap2(bmap,
1502 ext2fs_file_acl_block(fs, &inode))) {
1503 blk = translate_block(ext2fs_file_acl_block(fs,
1508 ext2fs_file_acl_block_set(fs, &inode, blk);
1511 * Write the inode to disk so that inode table
1514 retval = ext2fs_write_inode(fs, ino, &inode);
1519 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1522 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1523 process_block, bmap);
1530 ext2fs_free_mem(&block_buf);
1536 * We need to scan for inode and block bitmaps that may need to be
1537 * moved. This can take place if the filesystem was formatted for
1538 * RAID arrays using the mke2fs's extended option "stride".
1540 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1545 for (i = 0; i < fs->group_desc_count; i++) {
1546 blk = ext2fs_block_bitmap_loc(fs, i);
1547 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1548 new_blk = translate_block(blk);
1551 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1554 blk = ext2fs_inode_bitmap_loc(fs, i);
1555 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1556 new_blk = translate_block(blk);
1559 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1565 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1570 int new_ino_blks_per_grp;
1572 char *old_itable = NULL, *new_itable = NULL;
1573 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1574 unsigned long old_ino_size;
1575 int old_itable_size, new_itable_size;
1577 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1578 old_ino_size = EXT2_INODE_SIZE(fs->super);
1580 new_ino_blks_per_grp = ext2fs_div_ceil(
1581 EXT2_INODES_PER_GROUP(fs->super) *
1585 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1587 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1591 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1595 tmp_old_itable = old_itable;
1596 tmp_new_itable = new_itable;
1598 for (i = 0; i < fs->group_desc_count; i++) {
1599 blk = ext2fs_inode_table_loc(fs, i);
1600 retval = io_channel_read_blk64(fs->io, blk,
1601 fs->inode_blocks_per_group, old_itable);
1605 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1606 memcpy(new_itable, old_itable, old_ino_size);
1608 memset(new_itable+old_ino_size, 0,
1609 new_ino_size - old_ino_size);
1611 new_itable += new_ino_size;
1612 old_itable += old_ino_size;
1615 /* reset the pointer */
1616 old_itable = tmp_old_itable;
1617 new_itable = tmp_new_itable;
1619 retval = io_channel_write_blk64(fs->io, blk,
1620 new_ino_blks_per_grp, new_itable);
1625 /* Update the meta data */
1626 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1627 fs->super->s_inode_size = new_ino_size;
1631 ext2fs_free_mem(&old_itable);
1634 ext2fs_free_mem(&new_itable);
1639 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1643 unsigned int group = 0;
1644 unsigned int count = 0;
1649 * First calculate the block statistics
1651 for (blk = fs->super->s_first_data_block;
1652 blk < ext2fs_blocks_count(fs->super); blk++) {
1653 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1658 if ((count == fs->super->s_blocks_per_group) ||
1659 (blk == ext2fs_blocks_count(fs->super)-1)) {
1660 ext2fs_bg_free_blocks_count_set(fs, group++,
1666 total_free = EXT2FS_C2B(fs, total_free);
1667 ext2fs_free_blocks_count_set(fs->super, total_free);
1670 * Next, calculate the inode statistics
1677 /* Protect loop from wrap-around if s_inodes_count maxed */
1678 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1679 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1684 if ((count == fs->super->s_inodes_per_group) ||
1685 (ino == fs->super->s_inodes_count)) {
1686 ext2fs_bg_free_inodes_count_set(fs, group++,
1692 fs->super->s_free_inodes_count = total_free;
1693 ext2fs_mark_super_dirty(fs);
1697 #define list_for_each_safe(pos, pnext, head) \
1698 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1699 pos = pnext, pnext = pos->next)
1701 static void free_blk_move_list(void)
1703 struct list_head *entry, *tmp;
1704 struct blk_move *bmv;
1706 list_for_each_safe(entry, tmp, &blk_move_list) {
1707 bmv = list_entry(entry, struct blk_move, list);
1709 ext2fs_free_mem(&bmv);
1714 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1717 int new_ino_blks_per_grp;
1718 ext2fs_block_bitmap bmap;
1720 retval = ext2fs_read_inode_bitmap(fs);
1722 fputs(_("Failed to read inode bitmap\n"), stderr);
1725 retval = ext2fs_read_block_bitmap(fs);
1727 fputs(_("Failed to read block bitmap\n"), stderr);
1730 INIT_LIST_HEAD(&blk_move_list);
1733 new_ino_blks_per_grp = ext2fs_div_ceil(
1734 EXT2_INODES_PER_GROUP(fs->super)*
1738 /* We may change the file system.
1739 * Mark the file system as invalid so that
1740 * the user is prompted to run fsck.
1742 fs->super->s_state &= ~EXT2_VALID_FS;
1744 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1747 fputs(_("Failed to allocate block bitmap when "
1748 "increasing inode size\n"), stderr);
1751 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1753 fputs(_("Not enough space to increase inode size \n"), stderr);
1756 retval = move_block(fs, bmap);
1758 fputs(_("Failed to relocate blocks during inode resize \n"),
1762 retval = inode_scan_and_fix(fs, bmap);
1766 retval = group_desc_scan_and_fix(fs, bmap);
1770 retval = expand_inode_table(fs, new_size);
1774 ext2fs_calculate_summary_stats(fs);
1776 fs->super->s_state |= EXT2_VALID_FS;
1777 /* mark super block and block bitmap as dirty */
1778 ext2fs_mark_super_dirty(fs);
1779 ext2fs_mark_bb_dirty(fs);
1782 free_blk_move_list();
1783 ext2fs_free_block_bitmap(bmap);
1788 free_blk_move_list();
1789 ext2fs_free_block_bitmap(bmap);
1790 fputs(_("Error in resizing the inode size.\n"
1791 "Run e2undo to undo the "
1792 "file system changes. \n"), stderr);
1797 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1799 errcode_t retval = 0;
1800 const char *tdb_dir;
1802 char *dev_name, *tmp_name;
1806 * Configuration via a conf file would be
1809 profile_get_string(profile, "scratch_files",
1813 tmp_name = strdup(name);
1816 com_err(program_name, ENOMEM,
1817 _("Couldn't allocate memory for tdb filename\n"));
1820 dev_name = basename(tmp_name);
1822 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1824 tdb_dir = "/var/lib/e2fsprogs";
1826 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1827 access(tdb_dir, W_OK))
1830 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1833 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1835 if (!access(tdb_file, F_OK)) {
1836 if (unlink(tdb_file) < 0) {
1838 com_err(program_name, retval,
1839 _("while trying to delete %s"),
1846 set_undo_io_backing_manager(*io_ptr);
1847 *io_ptr = undo_io_manager;
1848 set_undo_io_backup_file(tdb_file);
1849 printf(_("To undo the tune2fs operation please run "
1850 "the command\n e2undo %s %s\n\n"),
1857 int main(int argc, char **argv)
1861 struct ext2_super_block *sb;
1862 io_manager io_ptr, io_ptr_orig = NULL;
1866 setlocale(LC_MESSAGES, "");
1867 setlocale(LC_CTYPE, "");
1868 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1869 textdomain(NLS_CAT_NAME);
1870 set_com_err_gettext(gettext);
1873 program_name = *argv;
1874 add_error_table(&et_ext2_error_table);
1876 #ifdef CONFIG_BUILD_FINDFS
1877 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1878 do_findfs(argc, argv);
1880 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1881 parse_e2label_options(argc, argv);
1883 parse_tune2fs_options(argc, argv);
1885 #ifdef CONFIG_TESTIO_DEBUG
1886 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1887 io_ptr = test_io_manager;
1888 test_io_backing_manager = unix_io_manager;
1891 io_ptr = unix_io_manager;
1894 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
1895 open_flag |= EXT2_FLAG_SKIP_MMP;
1897 open_flag |= EXT2_FLAG_64BITS;
1899 /* keep the filesystem struct around to dump MMP data */
1900 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
1902 retval = ext2fs_open2(device_name, io_options, open_flag,
1905 com_err(program_name, retval,
1906 _("while trying to open %s"),
1908 if (retval == EXT2_ET_MMP_FSCK_ON ||
1909 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
1910 dump_mmp_msg(fs->mmp_buf,
1911 _("If you are sure the filesystem "
1912 "is not in use on any node, run:\n"
1913 "'tune2fs -f -E clear_mmp {device}'\n"));
1914 else if (retval == EXT2_ET_MMP_FAILED)
1915 dump_mmp_msg(fs->mmp_buf, NULL);
1916 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
1918 _("MMP block magic is bad. Try to fix it by "
1919 "running:\n'e2fsck -f %s'\n"), device_name);
1920 else if (retval != EXT2_ET_MMP_FAILED)
1922 _("Couldn't find valid filesystem superblock.\n"));
1927 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
1929 if (I_flag && !io_ptr_orig) {
1931 * Check the inode size is right so we can issue an
1932 * error message and bail before setting up the tdb
1935 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1936 fprintf(stderr, _("The inode size is already %lu\n"),
1941 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1942 fprintf(stderr, _("Shrinking the inode size is "
1943 "not supported\n"));
1949 * If inode resize is requested use the
1952 io_ptr_orig = io_ptr;
1953 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1958 if (io_ptr != io_ptr_orig) {
1965 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1968 /* For e2label emulation */
1969 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1971 remove_error_table(&et_ext2_error_table);
1975 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1977 com_err("ext2fs_check_if_mount", retval,
1978 _("while determining whether %s is mounted."),
1983 /* Normally we only need to write out the superblock */
1984 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1987 sb->s_max_mnt_count = max_mount_count;
1988 ext2fs_mark_super_dirty(fs);
1989 printf(_("Setting maximal mount count to %d\n"),
1993 sb->s_mnt_count = mount_count;
1994 ext2fs_mark_super_dirty(fs);
1995 printf(_("Setting current mount count to %d\n"), mount_count);
1998 sb->s_errors = errors;
1999 ext2fs_mark_super_dirty(fs);
2000 printf(_("Setting error behavior to %d\n"), errors);
2003 sb->s_def_resgid = resgid;
2004 ext2fs_mark_super_dirty(fs);
2005 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2008 if (interval >= (1ULL << 32)) {
2009 com_err(program_name, 0,
2010 _("interval between checks is too big (%lu)"),
2015 sb->s_checkinterval = interval;
2016 ext2fs_mark_super_dirty(fs);
2017 printf(_("Setting interval between checks to %lu seconds\n"),
2021 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2022 ext2fs_blocks_count(sb) / 100.0);
2023 ext2fs_mark_super_dirty(fs);
2024 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2025 reserved_ratio, ext2fs_r_blocks_count(sb));
2028 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2029 com_err(program_name, 0,
2030 _("reserved blocks count is too big (%llu)"),
2035 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2036 ext2fs_mark_super_dirty(fs);
2037 printf(_("Setting reserved blocks count to %llu\n"),
2041 if (sb->s_feature_ro_compat &
2042 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
2043 fputs(_("\nThe filesystem already has sparse "
2044 "superblocks.\n"), stderr);
2046 sb->s_feature_ro_compat |=
2047 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2048 sb->s_state &= ~EXT2_VALID_FS;
2049 ext2fs_mark_super_dirty(fs);
2050 printf(_("\nSparse superblock flag set. %s"),
2055 fputs(_("\nClearing the sparse superflag not supported.\n"),
2061 sb->s_lastcheck = last_check_time;
2062 ext2fs_mark_super_dirty(fs);
2063 printf(_("Setting time filesystem last checked to %s\n"),
2064 ctime(&last_check_time));
2067 sb->s_def_resuid = resuid;
2068 ext2fs_mark_super_dirty(fs);
2069 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2072 if (strlen(new_label) > sizeof(sb->s_volume_name))
2073 fputs(_("Warning: label too long, truncating.\n"),
2075 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2076 strncpy(sb->s_volume_name, new_label,
2077 sizeof(sb->s_volume_name));
2078 ext2fs_mark_super_dirty(fs);
2081 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2082 strncpy(sb->s_last_mounted, new_last_mounted,
2083 sizeof(sb->s_last_mounted));
2084 ext2fs_mark_super_dirty(fs);
2087 rc = update_mntopts(fs, mntopts_cmd);
2092 rc = update_feature_set(fs, features_cmd);
2097 rc = parse_extended_opts(fs, extended_cmd);
2100 if (clear_mmp && !f_flag) {
2101 fputs(_("Error in using clear_mmp. "
2102 "It must be used with -f\n"),
2108 rc = ext2fs_mmp_clear(fs);
2111 if (journal_size || journal_device) {
2112 rc = add_journal(fs);
2118 if (mount_flags & EXT2_MF_MOUNTED) {
2119 fputs(_("The quota feature may only be changed when "
2120 "the filesystem is unmounted.\n"), stderr);
2124 handle_quota_options(fs);
2131 if (sb->s_feature_ro_compat &
2132 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2134 * Determine if the block group checksums are
2135 * correct so we know whether or not to set
2138 for (i = 0; i < fs->group_desc_count; i++)
2139 if (!ext2fs_group_desc_csum_verify(fs, i))
2141 if (i >= fs->group_desc_count)
2144 if ((strcasecmp(new_UUID, "null") == 0) ||
2145 (strcasecmp(new_UUID, "clear") == 0)) {
2146 uuid_clear(sb->s_uuid);
2147 } else if (strcasecmp(new_UUID, "time") == 0) {
2148 uuid_generate_time(sb->s_uuid);
2149 } else if (strcasecmp(new_UUID, "random") == 0) {
2150 uuid_generate(sb->s_uuid);
2151 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2152 com_err(program_name, 0, _("Invalid UUID format\n"));
2157 for (i = 0; i < fs->group_desc_count; i++)
2158 ext2fs_group_desc_csum_set(fs, i);
2159 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2161 ext2fs_mark_super_dirty(fs);
2164 if (mount_flags & EXT2_MF_MOUNTED) {
2165 fputs(_("The inode size may only be "
2166 "changed when the filesystem is "
2167 "unmounted.\n"), stderr);
2171 if (fs->super->s_feature_incompat &
2172 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2173 fputs(_("Changing the inode size not supported for "
2174 "filesystems with the flex_bg\n"
2175 "feature enabled.\n"),
2181 * We want to update group descriptor also
2182 * with the new free inode count
2184 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2185 if (resize_inode(fs, new_inode_size) == 0) {
2186 printf(_("Setting inode size %lu\n"),
2189 printf(_("Failed to change inode size\n"));
2198 sb->s_raid_stride = stride;
2199 ext2fs_mark_super_dirty(fs);
2200 printf(_("Setting stride size to %d\n"), stride);
2202 if (stripe_width_set) {
2203 sb->s_raid_stripe_width = stripe_width;
2204 ext2fs_mark_super_dirty(fs);
2205 printf(_("Setting stripe width to %d\n"), stripe_width);
2207 if (ext_mount_opts) {
2208 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2209 sizeof(fs->super->s_mount_opts));
2210 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2211 ext2fs_mark_super_dirty(fs);
2212 printf(_("Setting extended default mount options to '%s'\n"),
2214 free(ext_mount_opts);
2217 remove_error_table(&et_ext2_error_table);
2221 ext2fs_mmp_stop(fs);
2225 return (ext2fs_close(fs) ? 1 : 0);